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


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

		if (check != 0 && check != path)
			return check;
	}

	return 0;
}


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;
	tilesetLoaders = 0;
	tilesetsLoaded = false;
}

dKPMapData_c::~dKPMapData_c() {
	unloadTilesets();

	m_fileLoader.unload();
}

bool dKPMapData_c::load(const char *filename) {
	OSReport("Loading %s\n", filename);
	data = (dKPMapFile_s*)m_fileLoader.load(filename);
	OSReport("%p\n", data);

	if (data == 0)
		return false;

	if (!fixedUp)
		fixup();

	return loadTilesets();
}

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

	OSReport("Loading tilesets: %d\n", data->tilesetCount);
	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);
		OSReport("Loading tileset: %s\n", filename);
		result &= (tilesetLoaders[i].load(filename) != 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;
	}
}

void dKPMapData_c::fixup() {
	fixedUp = true;

	fixRef(data->layers);

	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);
				}
				break;
		}
	}

	fixRef(data->tilesets);
}


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


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


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


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