summaryrefslogtreecommitdiff
path: root/ZAPD/ZRoom/Commands/SetActorList.cpp
blob: 919d86f573cadcdd7f7567978dd6a2e4c338088c (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
#include "SetActorList.h"

#include "Globals.h"
#include "Utils/BitConverter.h"
#include "Utils/StringHelper.h"
#include "ZFile.h"
#include "ZRoom/ZNames.h"
#include "ZRoom/ZRoom.h"

SetActorList::SetActorList(ZFile* nParent) : ZRoomCommand(nParent)
{
}

void SetActorList::ParseRawData()
{
	ZRoomCommand::ParseRawData();
	numActors = cmdArg1;
}

void SetActorList::DeclareReferences(const std::string& prefix)
{
	if (numActors != 0 && cmdArg2 != 0)
	{
		std::string varName =
			StringHelper::Sprintf("%sActorList_%06X", prefix.c_str(), segmentOffset);
		parent->AddDeclarationPlaceholder(segmentOffset, varName);
	}
}

void SetActorList::ParseRawDataLate()
{
	ZRoomCommand::ParseRawDataLate();
	size_t actorsAmount = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 0x10;

	uint32_t currentPtr = segmentOffset;

	for (size_t i = 0; i < actorsAmount; i++)
	{
		ActorSpawnEntry entry(parent->GetRawData(), currentPtr);

		currentPtr += entry.GetRawDataSize();
		actors.push_back(entry);
	}
}

void SetActorList::DeclareReferencesLate(const std::string& prefix)
{
	if (actors.empty())
		return;

	std::string declaration;

	size_t largestlength = 0;
	for (const auto& entry : actors)
	{
		size_t actorNameLength = ZNames::GetActorName(entry.GetActorId()).size();
		if (actorNameLength > largestlength)
			largestlength = actorNameLength;
	}

	size_t index = 0;
	for (auto& entry : actors)
	{
		entry.SetLargestActorName(largestlength);
		declaration += StringHelper::Sprintf("\t{ %s },", entry.GetBodySourceCode().c_str());

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

		index++;
	}

	const auto& entry = actors.front();

	std::string varName = StringHelper::Sprintf("%sActorList_%06X", prefix.c_str(), segmentOffset);
	parent->AddDeclarationArray(segmentOffset, DeclarationAlignment::Align4,
	                            actors.size() * entry.GetRawDataSize(), entry.GetSourceTypeName(),
	                            varName, GetActorListArraySize(), declaration);
}

std::string SetActorList::GetBodySourceCode() const
{
	std::string listName;
	Globals::Instance->GetSegmentedPtrName(cmdArg2, parent, "ActorEntry", listName);
	if (numActors != actors.size())
	{
		printf("%s: numActors(%i) ~ actors(%li)\n", parent->GetName().c_str(), numActors,
		       actors.size());
	}
	return StringHelper::Sprintf("SCENE_CMD_ACTOR_LIST(%i, %s)", numActors, listName.c_str());
}

size_t SetActorList::GetActorListArraySize() const
{
	size_t 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 (const auto& entry : actors)
			if (entry.GetActorId() != 0x22)
				actorCount++;
	}
	else
	{
		actorCount = actors.size();
	}

	return actorCount;
}

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

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

ActorSpawnEntry::ActorSpawnEntry(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex)
{
	actorNum = BitConverter::ToInt16BE(rawData, rawDataIndex + 0);
	posX = BitConverter::ToInt16BE(rawData, rawDataIndex + 2);
	posY = BitConverter::ToInt16BE(rawData, rawDataIndex + 4);
	posZ = BitConverter::ToInt16BE(rawData, rawDataIndex + 6);
	rotX = BitConverter::ToUInt16BE(rawData, rawDataIndex + 8);
	rotY = BitConverter::ToUInt16BE(rawData, rawDataIndex + 10);
	rotZ = BitConverter::ToUInt16BE(rawData, rawDataIndex + 12);
	initVar = BitConverter::ToInt16BE(rawData, rawDataIndex + 14);
}

std::string ActorSpawnEntry::GetBodySourceCode() const
{
	std::string body;

	std::string actorNameFmt = StringHelper::Sprintf("%%-%zus ", largestActorName + 1);
	body =
		StringHelper::Sprintf(actorNameFmt.c_str(), (ZNames::GetActorName(actorNum) + ",").c_str());

	body += StringHelper::Sprintf("{ %6i, %6i, %6i }, ", posX, posY, posZ);
	if (Globals::Instance->game == ZGame::MM_RETAIL)
		body += StringHelper::Sprintf("{ SPAWN_ROT_FLAGS(%#5hX, 0x%04X)"
		                              ", SPAWN_ROT_FLAGS(%#5hX, 0x%04X)"
		                              ", SPAWN_ROT_FLAGS(%#5hX, 0x%04X) }, ",
		                              (rotX >> 7) & 0b111111111, rotX & 0b1111111,
		                              (rotY >> 7) & 0b111111111, rotY & 0b1111111,
		                              (rotZ >> 7) & 0b111111111, rotZ & 0b1111111);
	else
		body += StringHelper::Sprintf("{ %#6hX, %#6hX, %#6hX }, ", rotX, rotY, rotZ);
	body += StringHelper::Sprintf("0x%04X", initVar);

	return body;
}

std::string ActorSpawnEntry::GetSourceTypeName() const
{
	return "ActorEntry";
}

int32_t ActorSpawnEntry::GetRawDataSize() const
{
	return 16;
}

uint16_t ActorSpawnEntry::GetActorId() const
{
	return actorNum;
}

void ActorSpawnEntry::SetLargestActorName(size_t nameSize)
{
	largestActorName = nameSize;
}