summaryrefslogtreecommitdiff
path: root/ZAPD/ZRoom/Commands/SetActorList.cpp
blob: 35c2af5c7980bf76dd59e466fc744e4b73b59d03 (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
#include "SetActorList.h"
#include "../../ZFile.h"
#include "../ZRoom.h"
#include "../ActorList.h"
#include "../../BitConverter.h"
#include "../../StringHelper.h"
#include "../../Globals.h"

using namespace std;

SetActorList::SetActorList(ZRoom* nZRoom, std::vector<uint8_t> rawData, int rawDataIndex) : ZRoomCommand(nZRoom, rawData, rawDataIndex)
{
	numActors = rawData[rawDataIndex + 1];
	segmentOffset = SEG2FILESPACE(BitConverter::ToInt32BE(rawData, rawDataIndex + 4));

	_rawData = rawData;
	_rawDataIndex = rawDataIndex;

	if (segmentOffset != 0)
		zRoom->parent->AddDeclarationPlaceholder(segmentOffset);
}

SetActorList::~SetActorList()
{
	for (ActorSpawnEntry* entry : actors)
		delete entry;

	actors.clear();
}

string SetActorList::GetSourceOutputCode(std::string prefix)
{
	return "";
}

string SetActorList::GenerateSourceCodePass1(string roomName, int baseAddress)
{
	return "";
}


string SetActorList::GenerateSourceCodePass2(string roomName, int baseAddress)
{
	string sourceOutput = "";
	int numActorsReal = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 16;
	actors = vector<ActorSpawnEntry*>();
	uint32_t currentPtr = segmentOffset;

	for (int i = 0; i < numActorsReal; i++)
	{
		ActorSpawnEntry* entry = new ActorSpawnEntry(_rawData, currentPtr);
		actors.push_back(entry);

		currentPtr += 16;
	}

	sourceOutput += StringHelper::Sprintf("%s 0x%02X, (u32)%sActorList0x%06X };", ZRoomCommand::GenerateSourceCodePass1(roomName, baseAddress).c_str(), numActors, roomName.c_str(), segmentOffset);

	//zRoom->parent->AddDeclaration(segmentOffset, DeclarationAlignment::None, DeclarationPadding::None, GetRawDataSize(),
		//"SCmdActorList", ZRoomCommand::GenerateSourceCodePass1(roomName, baseAddress), sourceOutput);

	string declaration = "";

	int index = 0;
	for (ActorSpawnEntry* entry : actors)
	{
		uint16_t actorNum = entry->actorNum;

		// SW97 Actor 0x22 was removed, so we want to not output a working actor.
		if (actorNum == 0x22 && Globals::Instance->game == ZGame::OOT_SW97)
			declaration += StringHelper::Sprintf("\t//{ %s, %i, %i, %i, %i, %i, %i, 0x%04X }, //0x%06X", StringHelper::Sprintf("SW_REMOVED_0x%04X", actorNum), entry->posX, entry->posY, entry->posZ, entry->rotX, entry->rotY, entry->rotZ, (uint16_t)entry->initVar, segmentOffset + (index * 16));
		else
		{
			// SW97 Actor 0x23 and above are shifted up by one because 0x22 was removed between SW97 and retail.
			// We need to shift down by one
			if (Globals::Instance->game == ZGame::OOT_SW97 && actorNum >= 0x23)
				actorNum--;

			if (actorNum < sizeof(ActorList) / sizeof(ActorList[0]))
				declaration += StringHelper::Sprintf("\t{ %s, %i, %i, %i, %i, %i, %i, 0x%04X }, //0x%06X", ActorList[actorNum].c_str(), entry->posX, entry->posY, entry->posZ, entry->rotX, entry->rotY, entry->rotZ, (uint16_t)entry->initVar, segmentOffset + (index * 16));
			else
				declaration += StringHelper::Sprintf("\t{ 0x%04X, %i, %i, %i, %i, %i, %i, 0x%04X }, //0x%06X", actorNum, entry->posX, entry->posY, entry->posZ, entry->rotX, entry->rotY, entry->rotZ, (uint16_t)entry->initVar, segmentOffset + (index * 16));

			if (index < actors.size() - 1)
				declaration += "\n";
		}

		index++;
	}

	zRoom->parent->AddDeclarationArray(segmentOffset, DeclarationAlignment::None, DeclarationPadding::Pad16, actors.size() * 16,
		"ActorEntry", StringHelper::Sprintf("%sActorList0x%06X", roomName.c_str(), segmentOffset), GetActorListArraySize(), declaration);

	return sourceOutput;
}

int32_t SetActorList::GetRawDataSize()
{
	return ZRoomCommand::GetRawDataSize() + ((int)actors.size() * 16);
}

int SetActorList::GetActorListArraySize()
{
	int actorCount = 0;

	// Doing an else-if here so we only do the loop when the game is SW97.
	// Actor 0x22 is removed from SW97, so we need to ensure that we don't increment the actor count for it.
	if (Globals::Instance->game == ZGame::OOT_SW97)
	{
		actorCount = 0;

		for (ActorSpawnEntry* entry : actors)
			if (entry->actorNum != 0x22)
				actorCount++;
	}
	else
	{
		actorCount = (int)actors.size();
	}

	return actorCount;
}

string SetActorList::GenerateExterns()
{
	return StringHelper::Sprintf("extern ActorEntry %sActorList0x%06X[%i];\n", zRoom->GetName().c_str(), segmentOffset, GetActorListArraySize());
}

string SetActorList::GetCommandCName()
{
	return "SCmdActorList";
}

RoomCommand SetActorList::GetRoomCommand()
{
	return RoomCommand::SetActorList;
}

ActorSpawnEntry::ActorSpawnEntry(std::vector<uint8_t> rawData, int rawDataIndex)
{
	const uint8_t* data = rawData.data();

	actorNum = BitConverter::ToInt16BE(data, rawDataIndex + 0);
	posX = BitConverter::ToInt16BE(data, rawDataIndex + 2);
	posY = BitConverter::ToInt16BE(data, rawDataIndex + 4);
	posZ = BitConverter::ToInt16BE(data, rawDataIndex + 6);
	rotX = BitConverter::ToInt16BE(data, rawDataIndex + 8);
	rotY = BitConverter::ToInt16BE(data, rawDataIndex + 10);
	rotZ = BitConverter::ToInt16BE(data, rawDataIndex + 12);
	initVar = BitConverter::ToInt16BE(data, rawDataIndex + 14);
}