forked from gecube/opencaesar3
/
oc3_tilemap.cpp
512 lines (429 loc) · 11.7 KB
/
oc3_tilemap.cpp
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// This file is part of openCaesar3.
//
// openCaesar3 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.
//
// openCaesar3 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 openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "oc3_tilemap.hpp"
#include <iostream>
#include "oc3_pic_loader.hpp"
#include "oc3_building.hpp"
#include "oc3_exception.hpp"
#include "oc3_positioni.hpp"
TerrainTile::TerrainTile()
{
reset();
}
void TerrainTile::reset()
{
_isWater = false;
_isRock = false;
_isTree = false;
_isBuilding = false;
_isRoad = false;
_isAqueduct = false;
_isGarden = false;
_isMeadow = false;
_overlay = NULL; // BUG? What will be with old overlay?
}
bool TerrainTile::isConstructible() const
{
return !(_isWater || _isRock || _isTree || _isBuilding || _isRoad);
}
bool TerrainTile::isDestructible() const
{
return (_isTree || _isBuilding || _isRoad);
}
bool TerrainTile::isWalkable(const bool allLand) const
{
// TODO: test building to allow garden, gatehouse, granary, ...
bool walkable = (_isRoad || (allLand && !_isWater && !_isTree && !_isRock));
if( _overlay )
{
walkable &= _overlay->isWalkable();
}
return walkable;
}
void TerrainTile::setOverlay(LandOverlay *overlay)
{
_overlay = overlay;
}
LandOverlay *TerrainTile::getOverlay() const
{
return _overlay;
}
int TerrainTile::encode() const
{
int res = 0;
if (isTree())
{
res += 0x1;
res += 0x10;
}
if (isRock()) { res += 0x2; }
if (isWater()) { res += 0x4; }
if (isBuilding()) { res += 0x8; }
if (isRoad()) { res += 0x40; }
if (isMeadow()) { res += 0x800; }
return res;
}
void TerrainTile::decode(const int bitset)
{
reset();
if (bitset & 0x1) { setTree(true); }
if (bitset & 0x2) { setRock(true); }
if (bitset & 0x4) { setWater(true); }
if (bitset & 0x8) { setBuilding(true); }
if (bitset & 0x10) { setTree(true); }
if (bitset & 0x20) { setGarden(true); }
if (bitset & 0x40) { setRoad(true); }
if (bitset & 0x100) { setAqueduct(true); }
// if (bitset & 0x200) { setElevation(true);}
if (bitset & 0x400) {
int i=0;
setRock( true );
//setAccessRamp(true);
}
if (bitset & 0x800) { setMeadow(true); }
// if (bitset & 0x4000) { setWall(true); }
}
void TerrainTile::serialize(OutputSerialStream &stream)
{
int terrainBitset = encode();
stream.write_int(terrainBitset, 2, 0, 65535);
stream.write_objectID(_overlay);
}
void TerrainTile::unserialize(InputSerialStream &stream)
{
int terrainBitset = stream.read_int(2, 0, 65535);
decode(terrainBitset);
stream.read_objectID((void**)&_overlay);
}
void TerrainTile::setOriginalImgId( unsigned int id )
{
_imgId = id;
}
unsigned int TerrainTile::getOriginalImgId() const
{
return _imgId;
}
Tile::Tile(const int i, const int j)
{
_i = i;
_j = j;
_picture = NULL;
_master_tile = NULL;
_terrain.reset();
}
Tile::Tile(const Tile& clone)
{
_i = clone._i;
_j = clone._j;
_picture = clone._picture;
_master_tile = clone._master_tile;
_terrain = clone._terrain;
}
int Tile::getI() const { return _i; }
int Tile::getJ() const { return _j; }
void Tile::set_picture(Picture *picture)
{
_picture = picture;
}
Picture& Tile::get_picture() const
{
if (_picture==NULL) THROW("error: picture is null");
return *_picture;
}
Tile* Tile::get_master_tile() const
{
return _master_tile;
}
void Tile::set_master_tile(Tile* master)
{
_master_tile = master;
}
const TerrainTile& Tile::get_terrain() const
{
return _terrain;
}
TerrainTile& Tile::get_terrain()
{
return _terrain;
}
bool Tile::is_flat() const
{
return !(_terrain.isRock() || _terrain.isTree() || _terrain.isBuilding() || _terrain.isAqueduct());
}
TilePos Tile::getIJ() const
{
return TilePos( _i, _j );
}
bool Tile::is_master_tile() const
{
return (_master_tile == this);
}
Tilemap::Tilemap()
{
_size = 0;
}
void Tilemap::init(const int size)
{
_size = size;
// resize the tile array
_tile_array.resize(_size);
for (int i = 0; i < _size; ++i)
{
_tile_array[i].reserve(_size);
for (int j = 0; j < _size; ++j)
{
_tile_array[i].push_back(Tile(i, j));
}
}
}
bool Tilemap::is_inside(const TilePos& pos ) const
{
return ( pos.getI() >= 0 && pos.getJ()>=0 && pos.getI()<_size && pos.getJ()<_size);
}
Tile& Tilemap::at(const int i, const int j)
{
return _tile_array.at(i).at(j);
}
const Tile& Tilemap::at(const int i, const int j) const
{
return _tile_array.at(i).at(j);
}
Tile& Tilemap::at( const TilePos& ij )
{
return _tile_array.at(ij.getI()).at(ij.getJ());
// bad! No check of arrays borders!!!
}
const Tile& Tilemap::at( const TilePos& ij ) const
{
return _tile_array.at(ij.getI()).at(ij.getJ());
}
int Tilemap::getSize() const
{
return _size;
}
std::list<Tile*> Tilemap::getRectangle( const TilePos& start, const TilePos& stop, const bool corners /*= true*/ )
{
std::list<Tile*> res;
int delta_corners = 0;
if (! corners)
{
delta_corners = 1;
}
/*Rect maxRect( 0, 0, _size, _size );
Rect rect( start.getI()+delta_corners, start.getJ()+delta_corners,
stop.getI()-delta_corners, stop.getJ()-delta_corners );
rect.constrainTo( maxRect );
for( int i=rect.getLeft(); i < rect.getRight(); i++ )
for( int j=rect.getTop(); j < rect.getBottom(); j++ )
ret.push_back( &at( TilePos( i, j ) ) );
*/
for(int i = start.getI()+delta_corners; i <= stop.getI()-delta_corners; ++i)
{
if (is_inside( TilePos( i, start.getJ() ) ))
{
res.push_back( &at(i, start.getJ() ));
}
if (is_inside( TilePos( i, stop.getJ() ) ))
{
res.push_back( &at( i, stop.getJ() ));
}
}
for (int j = start.getJ()+1; j <= stop.getJ()-1; ++j) // corners have been handled already
{
if (is_inside( TilePos( start.getI(), j ) ))
{
res.push_back(&at(start.getI(), j));
}
if (is_inside( TilePos( stop.getI(), j ) ))
{
res.push_back(&at(stop.getI(), j));
}
}
return res;
}
std::list<Tile*> Tilemap::getRectangle( const TilePos& pos, const Size& size, const bool corners /*= true */ )
{
return getRectangle( pos, pos + TilePos( size.getWidth(), size.getHeight()), corners );
}
// Get tiles inside of rectangle
std::list<Tile*> Tilemap::getFilledRectangle(const TilePos& start, const TilePos& stop )
{
std::list<Tile*> res;
for (int i = start.getI(); i <= stop.getI(); ++i)
{
for (int j = start.getJ(); j <= stop.getJ(); ++j)
{
if( is_inside( TilePos( i, j ) ))
{
res.push_back(&at( TilePos( i, j ) ) );
}
}
}
return res;
}
std::list<Tile*> Tilemap::getFilledRectangle( const TilePos& start, const Size& size )
{
return getFilledRectangle( start, start + TilePos( size.getWidth(), size.getHeight() ) );
}
void Tilemap::serialize(OutputSerialStream &stream)
{
stream.write_int(_size, 2, 0, 1000);
// saves the graphics map
PicLoader &picLoader = PicLoader::instance();
std::string pic_name;
short int imgId; // 16bits
for (int i=0; i<_size; ++i)
{
for (int j=0; j<_size; ++j)
{
Tile &tile = at(i, j);
TerrainTile &terrain = tile.get_terrain();
imgId = 0;
if (&tile == tile.get_master_tile() || tile.get_master_tile() == NULL)
{
// this is a master tile
if ( !terrain.isBuilding() )
{
// this is not a building
pic_name = tile.get_picture().get_name();
imgId = TerrainTileHelper::convPicName2Id(pic_name);
}
}
stream.write_int(imgId, 2, 0, 65535);
terrain.serialize(stream);
}
}
}
void Tilemap::unserialize(InputSerialStream &stream)
{
_size = stream.read_int(2, 0, 1000);
init(_size);
// loads the graphics map
PicLoader &picLoader = PicLoader::instance();
short int imgId; // 16bits
for (int i=0; i<_size; ++i)
{
for (int j=0; j<_size; ++j)
{
Tile &tile = at(i, j);
TerrainTile &terrain = tile.get_terrain();
imgId = (short)stream.read_int(2, 0, 65535);
// terrain
terrain.unserialize(stream);
// cannot access the overlay at this stage (it has not be unserialized yet)
if (imgId != 0 && !terrain.isBuilding())
{
// master landscape tile!
std::string picName = TerrainTileHelper::convId2PicName( imgId );
Picture& pic = PicLoader::instance().get_picture( picName );
int tile_size = (pic.get_width()+2)/60; // size of the multi-tile. the multi-tile is a square.
// master is the left-most subtile
Tile* master;
if (tile_size == 1)
{
master = NULL;
}
else
{
master = &at(i, j);
}
for (int di=0; di<tile_size; ++di)
{
// for each subrow of the multi-tile
for (int dj=0; dj<tile_size; ++dj)
{
// for each subcol of the multi-tile
Tile &sub_tile = at(i+di, j+dj);
sub_tile.set_master_tile(master);
sub_tile.set_picture(&pic);
}
}
}
}
}
}
std::string TerrainTileHelper::convId2PicName( const unsigned int imgId )
{
// example: for land1a_00004.png, pfx=land1a and id=4
std::string res_pfx; // resource name prefix
int res_id = imgId; // id of resource
if (201<=imgId && imgId < 245)
{
res_pfx = "plateau";
res_id = imgId - 200;
}
else if (245<=imgId && imgId < 548)
{
res_pfx = "land1a";
res_id = imgId - 244;
}
else if (548<=imgId && imgId < 779)
{
res_pfx = "land2a";
res_id = imgId - 547;
}
else if (779<=imgId && imgId < 871)
{
res_pfx = "land3a";
res_id = imgId - 778;
}
else
{
res_pfx = "land1a";
res_id = 1;
// std::cout.setf(std::ios::hex, std::ios::basefield);
// std::cout << "Unknown image Id " << imgId << std::endl;
// std::cout.unsetf(std::ios::hex);
if (imgId == 0xb10 || imgId == 0xb0d) {res_pfx = "housng1a", res_id = 51;} // TERRIBLE HACK!
// THROW("Unknown image Id " << imgId);
}
char ret_str[128];
snprintf( ret_str, 127, "%s_%05d.png", res_pfx.c_str(), res_id );
return std::string( ret_str );
}
int TerrainTileHelper::convPicName2Id( std::string &pic_name )
{
// example: for land1a_00004.png, return 244+4=248
std::string res_pfx; // resource name prefix = land1a
int res_id; // idx of resource = 4
// extract the name and idx from name (ex: [land1a, 4])
int pos = pic_name.find("_");
res_pfx = pic_name.substr(0, pos);
std::stringstream ss(pic_name.substr(pos+1));
ss >> res_id;
if (res_pfx == "plateau")
{
res_id += 200;
}
else if (res_pfx == "land1a")
{
res_id += 244;
}
else if (res_pfx == "land2a")
{
res_id += 547;
}
else if (res_pfx == "land3a")
{
res_id += 778;
}
else
{
THROW("Unknown image " << pic_name);
}
return res_id;
}