summaryrefslogtreecommitdiff
path: root/ZAPD/ZRoom/ZRoom.cpp
blob: f38c4d4e9a432ac05d6075a55833433e7cd03c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "ZRoom.h"
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cinttypes>
#include <string_view>

#include "Commands/EndMarker.h"
#include "Commands/SetCutsceneEntryList.h"
#include "Commands/SetActorList.h"
#include "Commands/SetAlternateHeaders.h"
#include "Commands/SetAnimatedMaterialList.h"
#include "Commands/SetCameraSettings.h"
#include "Commands/SetCollisionHeader.h"
#include "Commands/SetCsCamera.h"
#include "Commands/SetCutscenes.h"
#include "Commands/SetEchoSettings.h"
#include "Commands/SetEntranceList.h"
#include "Commands/SetExitList.h"
#include "Commands/SetLightList.h"
#include "Commands/SetLightingSettings.h"
#include "Commands/SetMesh.h"
#include "Commands/SetMinimapChests.h"
#include "Commands/SetMinimapList.h"
#include "Commands/SetObjectList.h"
#include "Commands/SetPathways.h"
#include "Commands/SetRoomBehavior.h"
#include "Commands/SetRoomList.h"
#include "Commands/SetSkyboxModifier.h"
#include "Commands/SetSkyboxSettings.h"
#include "Commands/SetSoundSettings.h"
#include "Commands/SetSpecialObjects.h"
#include "Commands/SetStartPositionList.h"
#include "Commands/SetTimeSettings.h"
#include "Commands/SetTransitionActorList.h"
#include "Commands/SetWind.h"
#include "Commands/SetWorldMapVisited.h"
#include "Commands/Unused09.h"
#include "Commands/Unused1D.h"
#include "Commands/ZRoomCommandUnk.h"
#include "Globals.h"
#include "Utils/File.h"
#include "Utils/Path.h"
#include "Utils/StringHelper.h"
#include "WarningHandler.h"
#include "ZBlob.h"
#include "ZCutscene.h"
#include "ZFile.h"

REGISTER_ZFILENODE(Room, ZRoom);
REGISTER_ZFILENODE(Scene, ZRoom);
REGISTER_ZFILENODE(AltHeader, ZRoom);

ZRoom::ZRoom(ZFile* nParent) : ZResource(nParent)
{
	roomCount = -1;
	canHaveInner = true;
	RegisterOptionalAttribute("HackMode");
}

ZRoom::~ZRoom()
{
	for (ZRoomCommand* cmd : commands)
		delete cmd;
}

void ZRoom::ExtractWithXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex)
{
	ZResource::ExtractWithXML(reader, nRawDataIndex);

	if (hackMode == "syotes_room")
		SyotesRoomFix();
	else
		DeclareVar(name, "");
}

void ZRoom::ExtractFromBinary(uint32_t nRawDataIndex, ZResourceType parentType)
{
	rawDataIndex = nRawDataIndex;
	name = GetDefaultName(parent->GetName());

	zroomType = ZResourceType::AltHeader;
	switch (parentType)
	{
	case ZResourceType::Scene:
	case ZResourceType::Room:
	case ZResourceType::AltHeader:
		parentZroomType = parentType;
		break;

	default:
		// TODO: error message or something
		assert(false);
		break;
	}

	ParseRawData();
	DeclareVar(name, "");
}

void ZRoom::ParseXML(tinyxml2::XMLElement* reader)
{
	ZResource::ParseXML(reader);

	// TODO: HACK: remove this specific check when the repo uses the correct HackMode="syotes_room"
	if (name == "syotes_room_0")
	{
		hackMode = "syotes_room";
	}

	std::string nodeName = std::string(reader->Name());
	if (nodeName == "Scene")
	{
		zroomType = ZResourceType::Scene;
	}
	else if (nodeName == "Room")
		zroomType = ZResourceType::Room;
	else if (nodeName == "AltHeader")
		zroomType = ZResourceType::AltHeader;

	if (reader->Attribute("HackMode") != nullptr)
	{
		hackMode = std::string(reader->Attribute("HackMode"));
		if (hackMode != "syotes_room")
		{
			std::string headerError = StringHelper::Sprintf(
				"invalid value found for 'HackMode' attribute: '%s'", hackMode.c_str());
			HANDLE_ERROR_RESOURCE(WarningType::InvalidAttributeValue, parent, this, rawDataIndex,
			                      headerError, "");
		}
	}
}

void ZRoom::ParseRawData()
{
	if (hackMode == "syotes_room")
		return;

	bool shouldContinue = true;
	uint32_t currentIndex = 0;
	uint32_t currentPtr = rawDataIndex;

	const auto& rawData = parent->GetRawData();
	while (shouldContinue)
	{
		RoomCommand opcode = static_cast<RoomCommand>(rawData.at(currentPtr));

		ZRoomCommand* cmd = nullptr;

		auto start = std::chrono::steady_clock::now();

		switch (opcode)
		{
		case RoomCommand::SetStartPositionList:
			cmd = new SetStartPositionList(parent);
			break;  // 0x00
		case RoomCommand::SetActorList:
			cmd = new SetActorList(parent);
			break;  // 0x01
		case RoomCommand::SetCsCamera:
			cmd = new SetCsCamera(parent);
			break;  // 0x02 (MM-ONLY)
		case RoomCommand::SetCollisionHeader:
			cmd = new SetCollisionHeader(parent);
			break;  // 0x03
		case RoomCommand::SetRoomList:
			cmd = new SetRoomList(parent);
			break;  // 0x04
		case RoomCommand::SetWind:
			cmd = new SetWind(parent);
			break;  // 0x05
		case RoomCommand::SetEntranceList:
			cmd = new SetEntranceList(parent);
			break;  // 0x06
		case RoomCommand::SetSpecialObjects:
			cmd = new SetSpecialObjects(parent);
			break;  // 0x07
		case RoomCommand::SetRoomBehavior:
			cmd = new SetRoomBehavior(parent);
			break;  // 0x08
		case RoomCommand::Unused09:
			cmd = new Unused09(parent);
			break;  // 0x09
		case RoomCommand::SetMesh:
			cmd = new SetMesh(parent);
			break;  // 0x0A
		case RoomCommand::SetObjectList:
			cmd = new SetObjectList(parent);
			break;  // 0x0B
		case RoomCommand::SetLightList:
			cmd = new SetLightList(parent);
			break;  // 0x0C (MM-ONLY)
		case RoomCommand::SetPathways:
			cmd = new SetPathways(parent);
			break;  // 0x0D
		case RoomCommand::SetTransitionActorList:
			cmd = new SetTransitionActorList(parent);
			break;  // 0x0E
		case RoomCommand::SetLightingSettings:
			cmd = new SetLightingSettings(parent);
			break;  // 0x0F
		case RoomCommand::SetTimeSettings:
			cmd = new SetTimeSettings(parent);
			break;  // 0x10
		case RoomCommand::SetSkyboxSettings:
			cmd = new SetSkyboxSettings(parent);
			break;  // 0x11
		case RoomCommand::SetSkyboxModifier:
			cmd = new SetSkyboxModifier(parent);
			break;  // 0x12
		case RoomCommand::SetExitList:
			cmd = new SetExitList(parent);
			break;  // 0x13
		case RoomCommand::EndMarker:
			cmd = new EndMarker(parent);
			break;  // 0x14
		case RoomCommand::SetSoundSettings:
			cmd = new SetSoundSettings(parent);
			break;  // 0x15
		case RoomCommand::SetEchoSettings:
			cmd = new SetEchoSettings(parent);
			break;  // 0x16
		case RoomCommand::SetCutscenes:
			cmd = new SetCutscenes(parent);
			break;  // 0x17
		case RoomCommand::SetAlternateHeaders:
			cmd = new SetAlternateHeaders(parent);
			break;  // 0x18
		case RoomCommand::SetCameraSettings:
			if (Globals::Instance->game == ZGame::MM_RETAIL)
				cmd = new SetWorldMapVisited(parent);
			else
				cmd = new SetCameraSettings(parent);
			break;  // 0x19
		case RoomCommand::SetAnimatedMaterialList:
			cmd = new SetAnimatedMaterialList(parent);
			break;  // 0x1A (MM-ONLY)
		case RoomCommand::SetActorCutsceneList:
			cmd = new SetActorCutsceneList(parent);
			break;  // 0x1B (MM-ONLY)
		case RoomCommand::SetMinimapList:
			cmd = new SetMinimapList(parent);
			break;  // 0x1C (MM-ONLY)
		case RoomCommand::Unused1D:
			cmd = new Unused1D(parent);
			break;  // 0x1D
		case RoomCommand::SetMinimapChests:
			cmd = new SetMinimapChests(parent);
			break;  // 0x1E (MM-ONLY)
		default:
			cmd = new ZRoomCommandUnk(parent);
		}

		cmd->commandSet = rawDataIndex;
		cmd->ExtractCommandFromRoom(this, currentPtr);

		if (Globals::Instance->profile)
		{
			auto end = std::chrono::steady_clock::now();
			int64_t diff =
				std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
			if (diff > 50)
				printf("OP: %s, TIME: %" PRIi64 "ms\n", cmd->GetCommandCName().c_str(), diff);
		}

		cmd->cmdIndex = currentIndex;

		commands.push_back(cmd);

		if (opcode == RoomCommand::EndMarker)
			shouldContinue = false;

		currentPtr += 8;
		currentIndex++;
	}
}

void ZRoom::DeclareReferences(const std::string& prefix)
{
	for (auto& cmd : commands)
		cmd->DeclareReferences(prefix);
}

void ZRoom::ParseRawDataLate()
{
	for (auto& cmd : commands)
		cmd->ParseRawDataLate();
}

void ZRoom::DeclareReferencesLate(const std::string& prefix)
{
	for (auto& cmd : commands)
		cmd->DeclareReferencesLate(prefix);
}

Declaration* ZRoom::DeclareVar(const std::string& prefix, const std::string& body)
{
	std::string auxName = name;
	if (auxName == "")
		auxName = GetDefaultName(prefix);
	if (zroomType == ZResourceType::Scene || zroomType == ZResourceType::Room)
		auxName = StringHelper::Sprintf("%sCommands", name.c_str());

	Declaration* decl =
		parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(),
	                                GetSourceTypeName(), auxName, commands.size(), body);
	decl->staticConf = staticConf;
	return decl;
}

std::string ZRoom::GetBodySourceCode() const
{
	std::string declaration;

	for (size_t i = 0; i < commands.size(); i++)
	{
		ZRoomCommand* cmd = commands[i];
		declaration += StringHelper::Sprintf("\t%s,", cmd->GetBodySourceCode().c_str());

		if (i + 1 < commands.size())
			declaration += "\n";
	}

	return declaration;
}

std::string ZRoom::GetDefaultName(const std::string& prefix) const
{
	return StringHelper::Sprintf("%sSet_%06X", prefix.c_str(), rawDataIndex);
}

/*
 * There is one room in Ocarina of Time that lacks a header. Room 120, "Syotes", dates
 * back to very early in the game's development. Since this room is a special case,
 * declare automatically the data its contains whitout the need of a header.
 */
void ZRoom::SyotesRoomFix()
{
	PolygonType2 poly(parent, 0, this);

	poly.ParseRawData();
	poly.DeclareReferences(GetName());
	parent->AddDeclaration(0, poly.GetDeclarationAlignment(), poly.GetRawDataSize(),
	                       poly.GetSourceTypeName(), poly.GetDefaultName(GetName()),
	                       poly.GetBodySourceCode());
}

ZRoomCommand* ZRoom::FindCommandOfType(RoomCommand cmdType)
{
	for (size_t i = 0; i < commands.size(); i++)
	{
		if (commands[i]->GetRoomCommand() == cmdType)
			return commands[i];
	}

	return nullptr;
}

size_t ZRoom::GetCommandSizeFromNeighbor(ZRoomCommand* cmd)
{
	int32_t cmdIndex = -1;

	for (size_t i = 0; i < commands.size(); i++)
	{
		if (commands[i] == cmd)
		{
			cmdIndex = i;
			break;
		}
	}

	if (cmdIndex != -1)
	{
		if (cmdIndex + 1 < (int32_t)commands.size())
			return commands[cmdIndex + 1]->cmdAddress - commands[cmdIndex]->cmdAddress;
		else
			return parent->GetRawData().size() - commands[cmdIndex]->cmdAddress;
	}

	return 0;
}

void ZRoom::GetSourceOutputCode([[maybe_unused]] const std::string& prefix)
{
	if (hackMode != "syotes_room")
		DeclareVar(prefix, GetBodySourceCode());
}

size_t ZRoom::GetRawDataSize() const
{
	size_t size = 0;

	for (ZRoomCommand* cmd : commands)
		size += cmd->GetRawDataSize();

	return size;
}

std::string ZRoom::GetSourceTypeName() const
{
	return "SceneCmd";
}

ZResourceType ZRoom::GetResourceType() const
{
	assert(zroomType == ZResourceType::Scene || zroomType == ZResourceType::Room ||
	       zroomType == ZResourceType::AltHeader);
	return zroomType;
}