File: maploader.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (461 lines) | stat: -rw-r--r-- 13,568 bytes parent folder | download | duplicates (3)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ultima/ultima4/ultima4.h"
#include "ultima/ultima4/core/config.h"
#include "ultima/ultima4/map/maploader.h"
#include "ultima/ultima4/map/city.h"
#include "ultima/ultima4/controllers/combat_controller.h"
#include "ultima/ultima4/conversation/conversation.h"
#include "ultima/ultima4/conversation/dialogueloader.h"
#include "ultima/ultima4/map/dungeon.h"
#include "ultima/ultima4/map/map.h"
#include "ultima/ultima4/map/maploader.h"
#include "ultima/ultima4/map/mapmgr.h"
#include "ultima/ultima4/game/object.h"
#include "ultima/ultima4/game/person.h"
#include "ultima/ultima4/game/portal.h"
#include "ultima/ultima4/map/tilemap.h"
#include "ultima/ultima4/map/tileset.h"
#include "ultima/ultima4/map/xml_map.h"
#include "ultima/ultima4/core/utils.h"
#include "ultima/ultima4/gfx/image.h"
#include "ultima/ultima4/gfx/imagemgr.h"
#include "common/file.h"
#include "common/system.h"

namespace Ultima {
namespace Ultima4 {

MapLoaders *g_mapLoaders;

MapLoaders::MapLoaders() {
	g_mapLoaders = this;

	(*this)[Map::CITY] = new CityMapLoader();
	(*this)[Map::SHRINE] = new ConMapLoader();
	(*this)[Map::DUNGEON] = new DngMapLoader();
	(*this)[Map::WORLD] = new WorldMapLoader();
	(*this)[Map::COMBAT] = new ConMapLoader();
	(*this)[Map::XML] = new XMLMapLoader();
}

MapLoaders::~MapLoaders() {
	// Free the loaders
	for (iterator it = begin(); it != end(); ++it)
		delete it->_value;

	g_mapLoaders = nullptr;
}

MapLoader *MapLoaders::getLoader(Map::Type type) {
	if (find(type) == end())
		return nullptr;

	return (*this)[type];
}

/*-------------------------------------------------------------------*/

bool MapLoader::loadData(Map *map, Common::SeekableReadStream &f) {
	uint x, xch, y, ych;

	// Allocate the space we need for the map data
	map->_data.clear();
	map->_data.resize(map->_height * map->_width);

	if (map->_chunkHeight == 0)
		map->_chunkHeight = map->_height;
	if (map->_chunkWidth == 0)
		map->_chunkWidth = map->_width;

	uint32 total = 0;
	f.seek(map->_offset, SEEK_CUR);

	for (ych = 0; ych < (map->_height / map->_chunkHeight); ++ych) {
		for (xch = 0; xch < (map->_width / map->_chunkWidth); ++xch) {
			if (isChunkCompressed(map, ych * map->_chunkWidth + xch)) {
				MapTile water = map->_tileSet->getByName("sea")->getId();
				for (y = 0; y < map->_chunkHeight; ++y) {
					for (x = 0; x < map->_chunkWidth; ++x) {
						map->_data[x + (y * map->_width) + (xch * map->_chunkWidth) + (ych * map->_chunkHeight * map->_width)] = water;
					}
				}
			} else {
				for (y = 0; y < map->_chunkHeight; ++y) {
					for (x = 0; x < map->_chunkWidth; ++x) {
						int c = f.readByte();
						if (c == EOF)
							return false;

						uint32 s = g_system->getMillis();
						MapTile mt = map->translateFromRawTileIndex(c);
						total += g_system->getMillis() - s;

						map->_data[x + (y * map->_width) + (xch * map->_chunkWidth) + (ych * map->_chunkHeight * map->_width)] = mt;
					}
				}
			}
		}
	}

	debug(10, "MapLoader::loadData translation took %d ms", total);

	return true;
}

bool MapLoader::isChunkCompressed(Map *map, int chunk) {
	CompressedChunkList::iterator i;

	for (i = map->_compressedChunks.begin(); i != map->_compressedChunks.end(); i++) {
		if (chunk == *i)
			return true;
	}
	return false;
}

/*-------------------------------------------------------------------*/

bool CityMapLoader::load(Map *map) {
	City *city = dynamic_cast<City *>(map);
	assert(city);

	uint i, j;
	Person *people[CITY_MAX_PERSONS];
	Dialogue *dialogues[CITY_MAX_PERSONS];
	DialogueLoader *dlgLoader = DialogueLoaders::getLoader("application/x-u4tlk");

	Common::File ult, tlk;
	if (!ult.open(city->_fname) || !tlk.open(city->_tlkFname))
		error("unable to load map data");

	// The map must be 32x32 to be read from an .ULT file
	assertMsg(city->_width == CITY_WIDTH, "map width is %d, should be %d", city->_width, CITY_WIDTH);
	assertMsg(city->_height == CITY_HEIGHT, "map height is %d, should be %d", city->_height, CITY_HEIGHT);

	if (!loadData(city, ult))
		return false;

	// Properly construct people for the city
	for (i = 0; i < CITY_MAX_PERSONS; i++)
		people[i] = new Person(map->translateFromRawTileIndex(ult.readByte()));

	for (i = 0; i < CITY_MAX_PERSONS; i++)
		people[i]->getStart().x = ult.readByte();

	for (i = 0; i < CITY_MAX_PERSONS; i++)
		people[i]->getStart().y = ult.readByte();

	for (i = 0; i < CITY_MAX_PERSONS; i++)
		people[i]->setPrevTile(map->translateFromRawTileIndex(ult.readByte()));

	for (i = 0; i < CITY_MAX_PERSONS * 2; i++)
		ult.readByte();           // Read redundant startx/starty

	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		byte c = ult.readByte();
		if (c == 0)
			people[i]->setMovementBehavior(MOVEMENT_FIXED);
		else if (c == 1)
			people[i]->setMovementBehavior(MOVEMENT_WANDER);
		else if (c == 0x80)
			people[i]->setMovementBehavior(MOVEMENT_FOLLOW_AVATAR);
		else if (c == 0xFF)
			people[i]->setMovementBehavior(MOVEMENT_ATTACK_AVATAR);
		else
			return false;
	}

	byte conv_idx[CITY_MAX_PERSONS];
	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		conv_idx[i] = ult.readByte();
	}

	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		people[i]->getStart().z = 0;
	}

	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		dialogues[i] = dlgLoader->load(&tlk);

		if (!dialogues[i])
			break;

		/*
		 * Match up dialogues with their respective people
		 */
		bool found = false;
		for (j = 0; j < CITY_MAX_PERSONS; j++) {
			if (conv_idx[j] == i + 1) {
				people[j]->setDialogue(dialogues[i]);
				found = true;
			}
		}
		/*
		 * if the dialogue doesn't match up with a person, attach it
		 * to the city; Isaac the ghost in Skara Brae is handled like
		 * this
		 */
		if (!found) {
			city->_extraDialogues.push_back(dialogues[i]);
		}
	}

	/*
	 * Assign roles to certain people
	 */
	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		PersonRoleList::iterator current;

		for (current = city->_personRoles.begin(); current != city->_personRoles.end(); current++) {
			if ((unsigned)(*current)->_id == (i + 1)) {
				if ((*current)->_role == NPC_LORD_BRITISH)
					people[i]->setDialogue(DialogueLoaders::getLoader("application/x-u4lbtlk")->load(nullptr));
				else if ((*current)->_role == NPC_HAWKWIND)
					people[i]->setDialogue(DialogueLoaders::getLoader("application/x-u4hwtlk")->load(nullptr));
				people[i]->setNpcType(static_cast<PersonNpcType>((*current)->_role));
			}
		}
	}

	/**
	 * Add the people to the city structure
	 */
	for (i = 0; i < CITY_MAX_PERSONS; i++) {
		if (people[i]->getTile() != 0)
			city->_persons.push_back(people[i]);
		else
			delete people[i];
	}

	return true;
}

/*-------------------------------------------------------------------*/

bool ConMapLoader::load(Map *map) {
	int i;

	Common::File con;
	if (!con.open(map->_fname))
		error("unable to load map data");

	// The map must be 11x11 to be read from an .CON file
	assertMsg(map->_width == CON_WIDTH, "map width is %d, should be %d", map->_width, CON_WIDTH);
	assertMsg(map->_height == CON_HEIGHT, "map height is %d, should be %d", map->_height, CON_HEIGHT);

	if (map->_type != Map::SHRINE) {
		CombatMap *cm = getCombatMap(map);

		for (i = 0; i < AREA_CREATURES; i++)
			cm->creature_start[i] = MapCoords(con.readByte());

		for (i = 0; i < AREA_CREATURES; i++)
			cm->creature_start[i].y = con.readByte();

		for (i = 0; i < AREA_PLAYERS; i++)
			cm->player_start[i] = MapCoords(con.readByte());

		for (i = 0; i < AREA_PLAYERS; i++)
			cm->player_start[i].y = con.readByte();

		con.seek(16L, SEEK_CUR);
	}

	if (!loadData(map, con))
		return false;

	return true;
}

/*-------------------------------------------------------------------*/

bool DngMapLoader::load(Map *map) {
	Dungeon *dungeon = dynamic_cast<Dungeon *>(map);
	assert(dungeon);

	Common::File dng;
	if (!dng.open(dungeon->_fname))
		error("unable to load map data");

	// The map must be 11x11 to be read from an .CON file
	assertMsg(dungeon->_width == DNG_WIDTH, "map width is %d, should be %d", dungeon->_width, DNG_WIDTH);
	assertMsg(dungeon->_height == DNG_HEIGHT, "map height is %d, should be %d", dungeon->_height, DNG_HEIGHT);

	// Load the dungeon map
	uint i, j;
	for (i = 0; i < (DNG_HEIGHT * DNG_WIDTH * dungeon->_levels); i++) {
		byte mapData = dng.readByte();
		MapTile tile = map->translateFromRawTileIndex(mapData);

		// Determine what type of tile it is
		dungeon->_data.push_back(tile);
		dungeon->_dataSubTokens.push_back(mapData % 16);
	}

	// Read in the dungeon rooms
	// FIXME: needs a cleanup function to free this memory later
	dungeon->_rooms = new DngRoom[dungeon->_nRooms];

	for (i = 0; i < dungeon->_nRooms; i++) {
		byte room_tiles[121];

		for (j = 0; j < DNGROOM_NTRIGGERS; j++) {
			int tmp;

			dungeon->_rooms[i]._triggers[j]._tile = g_tileMaps->get("base")->translate(dng.readByte())._id;

			tmp = dng.readByte();
			if (tmp == EOF)
				return false;
			dungeon->_rooms[i]._triggers[j].x = (tmp >> 4) & 0x0F;
			dungeon->_rooms[i]._triggers[j].y = tmp & 0x0F;

			tmp = dng.readByte();
			if (tmp == EOF)
				return false;
			dungeon->_rooms[i]._triggers[j]._changeX1 = (tmp >> 4) & 0x0F;
			dungeon->_rooms[i]._triggers[j]._changeY1 = tmp & 0x0F;

			tmp = dng.readByte();
			if (tmp == EOF)
				return false;
			dungeon->_rooms[i]._triggers[j].changeX2 = (tmp >> 4) & 0x0F;
			dungeon->_rooms[i]._triggers[j].changeY2 = tmp & 0x0F;
		}

		dungeon->_rooms[i].load(dng);
		dng.read(room_tiles, sizeof(room_tiles));
		dng.read(dungeon->_rooms[i]._buffer, sizeof(dungeon->_rooms[i]._buffer));

		// Translate each creature tile to a tile id
		for (j = 0; j < sizeof(dungeon->_rooms[i]._creatureTiles); j++)
			dungeon->_rooms[i]._creatureTiles[j] = g_tileMaps->get("base")->translate(dungeon->_rooms[i]._creatureTiles[j])._id;

		// Translate each map tile to a tile id
		for (j = 0; j < sizeof(room_tiles); j++)
			dungeon->_rooms[i]._mapData.push_back(g_tileMaps->get("base")->translate(room_tiles[j]));

		//
		// dungeon room fixup
		//
		if (map->_id == MAP_HYTHLOTH) {
			if (i == 0x7) {
				dungeon->_rooms[i].hythlothFix7();
			} else if (i == 0x9) {
				dungeon->_rooms[i].hythlothFix9();
			}
		}
	}

	dungeon->_roomMaps = new CombatMap *[dungeon->_nRooms];
	for (i = 0; i < dungeon->_nRooms; i++)
		initDungeonRoom(dungeon, i);

	return true;
}

/*-------------------------------------------------------------------*/

void DngMapLoader::initDungeonRoom(Dungeon *dng, int room) {
	dng->_roomMaps[room] = dynamic_cast<CombatMap *>(mapMgr->initMap(Map::COMBAT));

	dng->_roomMaps[room]->_id = 0;
	dng->_roomMaps[room]->_borderBehavior = Map::BORDER_FIXED;
	dng->_roomMaps[room]->_width = dng->_roomMaps[room]->_height = 11;
	dng->_roomMaps[room]->_data = dng->_rooms[room]._mapData; // Load map data
	dng->_roomMaps[room]->_music = Music::COMBAT;
	dng->_roomMaps[room]->_type = Map::COMBAT;
	dng->_roomMaps[room]->_flags |= NO_LINE_OF_SIGHT;
	dng->_roomMaps[room]->_tileSet = g_tileSets->get("base");
}

/*-------------------------------------------------------------------*/

bool WorldMapLoader::load(Map *map) {
	Common::File world;
	if (!world.open(map->_fname))
		error("unable to load map data");

	if (!loadData(map, world))
		return false;

	// Check for any tile overrides for the portals
	for (uint idx = 0; idx < map->_portals.size(); ++idx) {
		const Portal *p = map->_portals[idx];
		if (p->_tile != -1) {
			MapTile mt = map->translateFromRawTileIndex(p->_tile);
			map->_data[p->_coords.x + p->_coords.y * map->_width] = mt;
		}
	}

	return true;
}

/*-------------------------------------------------------------------*/

bool XMLMapLoader::load(Map *map) {
	XMLMap *xmlMap = dynamic_cast<XMLMap *>(map);
	assert(xmlMap);
	Common::String text = xmlMap->_tilesText;
	text.trim();

	// Allocate the space we need for the map data
	map->_data.clear();
	map->_data.resize(map->_width * map->_height);

	// Split up the text lines
	Common::StringArray lines, cols;
	split(text, lines, '\n');
	assert(lines.size() == map->_height);

	// Iterate through the lines
	for (uint y = 0; y < map->_height; ++y) {
		text = lines[y];
		text.trim();
		split(text, cols, ',');
		assert(cols.size() == map->_width);

		for (uint x = 0; x < map->_width; ++x) {
			int id = atoi(cols[x].c_str());
			MapTile mt = map->translateFromRawTileIndex(id);
			map->_data[x + y * map->_width] = mt;
		}
	}

	return true;
}

void XMLMapLoader::split(const Common::String &text, Common::StringArray &values, char c) {
	values.clear();

	Common::String str = text;
	size_t pos;
	while ((pos = str.findFirstOf(c)) != Common::String::npos) {
		values.push_back(Common::String(str.c_str(), pos));
		str = Common::String(str.c_str() + pos + 1);
	}

	values.push_back(str);
}

} // End of namespace Ultima4
} // End of namespace Ultima