File: tileset.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 (331 lines) | stat: -rw-r--r-- 8,738 bytes parent folder | download | duplicates (2)
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
/* 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/map/tileset.h"
#include "ultima/ultima4/core/config.h"
#include "ultima/ultima4/gfx/screen.h"
#include "ultima/ultima4/core/settings.h"
#include "ultima/ultima4/map/tile.h"
#include "ultima/ultima4/map/tilemap.h"

namespace Ultima {
namespace Ultima4 {

TileRules *g_tileRules;
TileSets *g_tileSets;

TileRules::TileRules() {
	g_tileRules = this;
}

TileRules::~TileRules() {
	// Delete the tile rules
	for (iterator it = begin(); it != end(); ++it)
		delete it->_value;

	g_tileRules = nullptr;
}

void TileRules::load() {
	const Config *config = Config::getInstance();
	Std::vector<ConfigElement> rules = config->getElement("tileRules").getChildren();

	for (Std::vector<ConfigElement>::iterator i = rules.begin(); i != rules.end(); i++) {
		TileRule *rule = new TileRule();
		rule->initFromConf(*i);
		(*this)[rule->_name] = rule;
	}

	if (findByName("default") == nullptr)
		error("no 'default' rule found in tile rules");
}

TileRule *TileRules::findByName(const Common::String &name) {
	TileRuleMap::iterator i = find(name);
	if (i != end())
		return i->_value;
	return nullptr;
}

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

TileSets::TileSets() {
	g_tileSets = this;
	loadAll();
}

TileSets::~TileSets() {
	unloadAll();
	g_tileSets = nullptr;
}

void TileSets::loadAll() {
	const Config *config = Config::getInstance();
	Std::vector<ConfigElement> conf;

	unloadAll();

	// Get the config element for all tilesets
	conf = config->getElement("tilesets").getChildren();

	// Load tile rules
	if (g_tileRules->empty())
		g_tileRules->load();

	// Load all of the tilesets
	for (Std::vector<ConfigElement>::iterator i = conf.begin(); i != conf.end(); i++) {
		if (i->getName() == "tileset") {

			Tileset *tileset = new Tileset();
			tileset->load(*i);

			(*this)[tileset->_name] = tileset;
		}
	}
}

void TileSets::unloadAll() {
	iterator i;

	for (i = begin(); i != end(); i++) {
		i->_value->unload();
		delete i->_value;
	}
	clear();

	Tile::resetNextId();
}

void TileSets::unloadAllImages() {
	iterator i;

	for (i = begin(); i != end(); i++) {
		i->_value->unloadImages();
	}

	Tile::resetNextId();
}

Tileset *TileSets::get(const Common::String &name) {
	if (find(name) != end())
		return (*this)[name];
	else
		return nullptr;
}

Tile *TileSets::findTileByName(const Common::String &name) {
	iterator i;
	for (i = begin(); i != end(); i++) {
		Tile *t = i->_value->getByName(name);
		if (t)
			return t;
	}

	return nullptr;
}

Tile *TileSets::findTileById(TileId id) {
	iterator i;
	for (i = begin(); i != end(); i++) {
		Tile *t = i->_value->get(id);
		if (t)
			return t;
	}

	return nullptr;
}


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

bool TileRule::initFromConf(const ConfigElement &conf) {
	uint i;

	static const struct {
		const char *name;
		uint mask;
	} booleanAttributes[] = {
		{ "dispel", MASK_DISPEL },
		{ "talkover", MASK_TALKOVER },
		{ "door", MASK_DOOR },
		{ "lockeddoor", MASK_LOCKEDDOOR },
		{ "chest", MASK_CHEST },
		{ "ship", MASK_SHIP },
		{ "horse", MASK_HORSE },
		{ "balloon", MASK_BALLOON },
		{ "canattackover", MASK_ATTACKOVER },
		{ "canlandballoon", MASK_CANLANDBALLOON },
		{ "replacement", MASK_REPLACEMENT },
		{ "foreground", MASK_FOREGROUND },
		{ "onWaterOnlyReplacement", MASK_WATER_REPLACEMENT},
		{ "livingthing", MASK_LIVING_THING }
	};

	static const struct {
		const char *_name;
		uint _mask;
	} movementBooleanAttr[] = {
		{ "swimable", MASK_SWIMABLE },
		{ "sailable", MASK_SAILABLE },
		{ "unflyable", MASK_UNFLYABLE },
		{ "creatureunwalkable", MASK_CREATURE_UNWALKABLE }
	};
	static const char *const speedEnumStrings[] = { "fast", "slow", "vslow", "vvslow", nullptr };
	static const char *const effectsEnumStrings[] = { "none", "fire", "sleep", "poison", "poisonField", "electricity", "lava", nullptr };

	_mask = 0;
	_movementMask = 0;
	_speed = FAST;
	_effect = EFFECT_NONE;
	_walkOnDirs = MASK_DIR_ALL;
	_walkOffDirs = MASK_DIR_ALL;
	_name = conf.getString("name");

	for (i = 0; i < sizeof(booleanAttributes) / sizeof(booleanAttributes[0]); i++) {
		if (conf.getBool(booleanAttributes[i].name))
			_mask |= booleanAttributes[i].mask;
	}

	for (i = 0; i < sizeof(movementBooleanAttr) / sizeof(movementBooleanAttr[0]); i++) {
		if (conf.getBool(movementBooleanAttr[i]._name))
			_movementMask |= movementBooleanAttr[i]._mask;
	}

	Common::String cantwalkon = conf.getString("cantwalkon");
	if (cantwalkon == "all")
		_walkOnDirs = 0;
	else if (cantwalkon == "west")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_WEST, _walkOnDirs);
	else if (cantwalkon == "north")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_NORTH, _walkOnDirs);
	else if (cantwalkon == "east")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_EAST, _walkOnDirs);
	else if (cantwalkon == "south")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_SOUTH, _walkOnDirs);
	else if (cantwalkon == "advance")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_ADVANCE, _walkOnDirs);
	else if (cantwalkon == "retreat")
		_walkOnDirs = DIR_REMOVE_FROM_MASK(DIR_RETREAT, _walkOnDirs);

	Common::String cantwalkoff = conf.getString("cantwalkoff");
	if (cantwalkoff == "all")
		_walkOffDirs = 0;
	else if (cantwalkoff == "west")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_WEST, _walkOffDirs);
	else if (cantwalkoff == "north")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_NORTH, _walkOffDirs);
	else if (cantwalkoff == "east")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_EAST, _walkOffDirs);
	else if (cantwalkoff == "south")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_SOUTH, _walkOffDirs);
	else if (cantwalkoff == "advance")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_ADVANCE, _walkOffDirs);
	else if (cantwalkoff == "retreat")
		_walkOffDirs = DIR_REMOVE_FROM_MASK(DIR_RETREAT, _walkOffDirs);

	_speed = static_cast<TileSpeed>(conf.getEnum("speed", speedEnumStrings));
	_effect = static_cast<TileEffect>(conf.getEnum("effect", effectsEnumStrings));

	return true;
}

void Tileset::load(const ConfigElement &tilesetConf) {
	_name = tilesetConf.getString("name");
	if (tilesetConf.exists("imageName"))
		_imageName = tilesetConf.getString("imageName");
	if (tilesetConf.exists("extends"))
		_extends = g_tileSets->get(tilesetConf.getString("extends"));
	else
		_extends = nullptr;

	int index = 0;
	Std::vector<ConfigElement> children = tilesetConf.getChildren();
	for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
		if (i->getName() != "tile")
			continue;

		Tile *tile = new Tile(this);
		tile->loadProperties(*i);

		// Add the tile to our tileset
		_tiles[tile->getId()] = tile;
		_nameMap[tile->getName()] = tile;

		index += tile->getFrames();
	}
	_totalFrames = index;
}

void Tileset::unloadImages() {
	Tileset::TileIdMap::iterator i;

	// Free all the image memory and nullify so that reloading can automatically take place lazily
	for (i = _tiles.begin(); i != _tiles.end(); i++) {
		i->_value->deleteImage();
	}
}

void Tileset::unload() {
	Tileset::TileIdMap::iterator i;

	// Free all the memory for the tiles
	for (i = _tiles.begin(); i != _tiles.end(); i++)
		delete i->_value;

	_tiles.clear();
	_totalFrames = 0;
	_imageName.clear();
}

Tile *Tileset::get(TileId id) {
	if (_tiles.find(id) != _tiles.end())
		return _tiles[id];
	else if (_extends)
		return _extends->get(id);
	return nullptr;
}

Tile *Tileset::getByName(const Common::String &name) {
	if (_nameMap.find(name) != _nameMap.end())
		return _nameMap[name];
	else if (_extends)
		return _extends->getByName(name);
	else
		return nullptr;
}

Common::String Tileset::getImageName() const {
	if (_imageName.empty() && _extends)
		return _extends->getImageName();
	else
		return _imageName;
}

uint Tileset::numTiles() const {
	return _tiles.size();
}

uint Tileset::numFrames() const {
	return _totalFrames;
}

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