summaryrefslogtreecommitdiff
path: root/src/koopatlas/mapdata.cpp
blob: 4bd6a0b9455a64766cd08a3e0d645eb941512df8 (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
#include "koopatlas/mapdata.h"


// HELPER FUNCTIONS
dKPPath_s *dKPNode_s::getOppositeExitTo(dKPPath_s *path, bool mustBeAvailable) {
	for (int i = 0; i < 4; i++) {
		dKPPath_s *check = exits[i];

		if (check != 0 && check != path) {
			if (mustBeAvailable && !check->isAvailable)
				continue;

			return check;
		}
	}

	return 0;
}

int dKPNode_s::getExitCount(bool mustBeAvailable) {
	int ct = 0;

	for (int i = 0; i < 4; i++)
		if (exits[i] && (mustBeAvailable ? exits[i]->isAvailable : true))
			ct++;

	return ct;
}

bool dKPNode_s::isUnlocked() {
	for (int i = 0; i < 4; i++)
		if (exits[i]) 
			if (exits[i]->isAvailable)
				return true; 
	return false;
}

void dKPNode_s::setupNodeExtra() {
	int world = this->levelNumber[0];
	int level = this->levelNumber[1];

	SaveBlock *save = GetSaveFile()->GetBlock(-1);
	u32 conds = save->GetLevelCondition(world-1, level-1);

	bool isUnlocked = this->isUnlocked();
	bool exitComplete = false;
	bool secretComplete = false;

	if (conds & 0x10)
		exitComplete = true;
	if (conds & 0x20)
		secretComplete = true;

	const char *colour;

	//OSReport("Level %d-%d, isUnlocked: %d, exitComplete: %d", world, level, isUnlocked, exitComplete);

	// default: non-unlocked levels AND completed one-time levels
	colour = "g3d/black.brres";

	// one-time levels
	if ((level >= 30) && (level <= 37)) {
		if (isUnlocked && !exitComplete)
			colour = "g3d/red.brres";
	}
	// the shop
	else if (level == 99)
		colour = "g3d/shop.brres";

	else if (isUnlocked) {
		if (hasSecret) {
			if (exitComplete && secretComplete)
				colour = "g3d/blue.brres";
			else if (exitComplete || secretComplete)
				colour = "g3d/red.brres";
			else
				colour = "g3d/purple.brres";
		} else {
			if (exitComplete)
				colour = "g3d/blue.brres";
			else
				colour = "g3d/red.brres";
		}
	}

	// model time
	this->extra->mallocator.link(-1, GameHeaps[0], 0, 0x20);

	nw4r::g3d::ResFile rg(getResource("cobCourse", colour));
	this->extra->model.setup(rg.GetResMdl("cobCourse"), &this->extra->mallocator, 0x224, 1, 0);
	this->extra->matrix.identity();
	SetupTextures_MapObj(&this->extra->model, 0);

	this->extra->mallocator.unlink();
}

void dKPNode_s::setLayerAlpha(u8 alpha) {
	if (tileLayer)
		tileLayer->alpha = alpha;
	if (doodadLayer)
		doodadLayer->alpha = alpha;
}

void dKPPath_s::setLayerAlpha(u8 alpha) {
	if (tileLayer)
		tileLayer->alpha = alpha;
	if (doodadLayer)
		doodadLayer->alpha = alpha;
}

int dKPLayer_s::findNodeID(dKPNode_s *node) {
	for (int i = 0; i < nodeCount; i++)
		if (nodes[i] == node)
			return i;

	return -1;
}

dKPMapData_c::dKPMapData_c() {
	data = 0;
	fixedUp = false;
	levelNodeExtraArray = 0;
	tilesetLoaders = 0;
	tilesetsLoaded = false;
}

dKPMapData_c::~dKPMapData_c() {
	if (levelNodeExtraArray)
		delete[] levelNodeExtraArray;

	unloadTilesets();

	m_fileLoader.unload();
}

bool dKPMapData_c::load(const char *filename) {
	data = (dKPMapFile_s*)m_fileLoader.load(filename, 0, mHeap::archiveHeap);

	if (data == 0)
		return false;

	if (!fixedUp)
		fixup();

	bool didLoadTilesets = loadTilesets();
	bool didLoadBG = (bgLoader.load(data->backgroundName) != 0);

	return didLoadTilesets && didLoadBG;
}

bool dKPMapData_c::loadTilesets() {
	if (tilesetsLoaded)
		return true;

	if (tilesetLoaders == 0)
		tilesetLoaders = new dDvdLoader_c[data->tilesetCount];

	bool result = true;

	for (int i = 0; i < data->tilesetCount; i++) {
		char *filename = ((char*)data) + (data->tilesets[i].dummy[3] - 0x10000000);
		void *heap = (i < 9) ? mHeap::gameHeaps[2] : mHeap::archiveHeap;
		result &= (tilesetLoaders[i].load(filename, 0, heap) != 0);
	}

	if (result) {
		tilesetsLoaded = true;

		for (int i = 0; i < data->tilesetCount; i++) {
			data->tilesets[i].dummy[3] = (((u32)tilesetLoaders[i].buffer & ~0xC0000000) >> 5);
		}
	}

	return tilesetsLoaded;
}

void dKPMapData_c::unloadTilesets() {
	if (tilesetLoaders != 0) {
		for (int i = 0; i < data->tilesetCount; i++) {
			tilesetLoaders[i].unload();
		}

		delete[] tilesetLoaders;
	}

	bgLoader.unload();
}

void dKPMapData_c::fixup() {
	OSReport("Setting up Nodes");

	fixedUp = true;

	fixRef(data->layers);
	fixRef(data->tilesets);
	fixRef(data->unlockData);
	fixRef(data->sectors);
	fixRef(data->backgroundName);
	if (data->version >= 2) {
		fixRef(data->worlds);
		for (int i = 0; i < data->worldCount; i++) {
			fixRef(data->worlds[i].name);
		}
	}

	for (int iLayer = 0; iLayer < data->layerCount; iLayer++) {
		dKPLayer_s *layer = fixRef(data->layers[iLayer]);

		switch (layer->type) {
			case dKPLayer_s::OBJECTS:
				fixRef(layer->tileset);
				break;

			case dKPLayer_s::DOODADS:
				for (int iDood = 0; iDood < layer->doodadCount; iDood++) {
					dKPDoodad_s *doodad = fixRef(layer->doodads[iDood]);

					fixRef(doodad->texObj);
					fixTexObjSafe(doodad->texObj);
				}
				break;
				
			case dKPLayer_s::PATHS:
				pathLayer = layer;

				fixRef(layer->paths);
				fixRef(layer->nodes);

				for (int iPath = 0; iPath < layer->pathCount; iPath++) {
					dKPPath_s *path = fixRef(layer->paths[iPath]);

					fixRef(path->start);
					fixRef(path->end);

					fixRef(path->tileLayer);
					fixRef(path->doodadLayer);
				}

				for (int iNode = 0; iNode < layer->nodeCount; iNode++) {
					dKPNode_s *node = fixRef(layer->nodes[iNode]);

					for (int i = 0; i < 4; i++)
						fixRef(node->exits[i]);

					fixRef(node->tileLayer);
					fixRef(node->doodadLayer);

					if (node->type == dKPNode_s::CHANGE) {
						fixRef(node->destMap);
						OSReport("Node: %x, %s", node->destMap, node->destMap); }

				}
				break;
		}
	}

	
	// before we finish here, create the Node Extra classes

	// first off, count how many we need...
	int count = 0;
	for (int nodeIdx = 0; nodeIdx < pathLayer->nodeCount; nodeIdx++) {
		if (pathLayer->nodes[nodeIdx]->type == dKPNode_s::LEVEL)
			count++;
	}

	levelNodeExtraArray = new dKPNodeExtra_c[count];

	int extraIdx = 0;

	for (int nodeIdx = 0; nodeIdx < pathLayer->nodeCount; nodeIdx++) {
		if (pathLayer->nodes[nodeIdx]->type == dKPNode_s::LEVEL) {
			pathLayer->nodes[nodeIdx]->extra = &levelNodeExtraArray[extraIdx];
			extraIdx++;
		}
	}
}


const dKPWorldDef_s *dKPMapData_c::findWorldDef(int id) const {
	for (int i = 0; i < data->worldCount; i++) {
		if (data->worlds[i].key == id)
			return &data->worlds[i];
	}

	return 0;
}


/******************************************************************************
 * Generic Layer
 ******************************************************************************/


/******************************************************************************
 * Tile Layer
 ******************************************************************************/


/******************************************************************************
 * Doodad Layer
 ******************************************************************************/


/******************************************************************************
 * Path Layer
 ******************************************************************************/