-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathMapNode.cxx
More file actions
530 lines (490 loc) · 19.6 KB
/
Copy pathMapNode.cxx
File metadata and controls
530 lines (490 loc) · 19.6 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <cstdlib>
#include "MapNode.hxx"
#include "LOG.hxx"
#include "../map/MapLayers.hxx"
#include "GameStates.hxx"
#include "Settings.hxx"
#include "Engine.hxx"
MapNode::MapNode(Point isoCoordinates, const std::string &terrainID, const std::string &tileID)
: m_originalZ(isoCoordinates.z),
m_isoCoordinates(std::move(isoCoordinates)), m_sprite{std::make_unique<Sprite>(m_isoCoordinates)},
m_autotileOrientation(LAYERS_COUNT, TileOrientation::TILE_DEFAULT_ORIENTATION),
m_mapNodeData{std::vector(LAYERS_COUNT, MapNodeData{"", nullptr, 0, m_isoCoordinates, true, TileMap::DEFAULT})},
m_autotileBitmask(LAYERS_COUNT)
{
setTileID(terrainID, isoCoordinates);
if (!tileID.empty()) // in case tileID is not supplied skip it
{
setTileID(tileID, isoCoordinates);
}
// always add blueprint tiles too when creating the node
setTileID("terrain_blueprint", isoCoordinates);
const Layer layer = TileManager::instance().getTileLayer(tileID);
updateTexture(layer);
}
bool MapNode::changeHeight(const bool higher)
{
constexpr int minHeight = 0;
auto &height = m_isoCoordinates.height;
if ((higher && (height < maxHeight)) || (!higher && (height > minHeight)))
{
higher ? ++height : --height;
m_sprite->isoCoordinates = m_isoCoordinates;
return true;
}
return false;
}
void MapNode::render(Layer layer) const { m_sprite->render(layer); }
void MapNode::setBitmask(unsigned char elevationBitmask, std::vector<uint8_t> autotileBitmask)
{
m_elevationBitmask = elevationBitmask;
m_autotileBitmask = autotileBitmask;
updateTexture();
}
void MapNode::setTileID(const std::string &tileID, const Point &origCornerPoint)
{
TileData *tileData = TileManager::instance().getTileData(tileID);
if (tileData && !tileID.empty())
{
std::vector<Point> targetCoordinates = TileManager::instance().getTargetCoordsOfTileID(origCornerPoint, tileID);
if (targetCoordinates.size() > 1 && m_isoCoordinates == origCornerPoint)
{ // multibuilding placed on this node
int minZ = m_isoCoordinates.z;
LOG(LOG_INFO) << "old z " << minZ;
int minY=0;
for (auto coord : targetCoordinates)
{
if(coord.x == origCornerPoint.x)
{
minZ = std::min(minZ, Engine::instance().map->getMapNode(coord).getCoordinates().z);
LOG(LOG_INFO) << "Setting new z " << minZ;
}
}
for (auto coord : targetCoordinates)
{
if (coord == origCornerPoint)
{
// LOG(LOG_INFO) << "i'm the origin coordinate";
}
else
{
m_multiTileNodes.push_back(&Engine::instance().map->getMapNode(coord));
Engine::instance().map->getMapNode(coord).setRenderFlag(Layer::BUILDINGS, false);
// Engine::instance().map->getMapNode(coord).setRenderFlag(Layer::TERRAIN, false);
Engine::instance().map->getMapNode(coord).updateTexture(Layer::BUILDINGS);
// Engine::instance().map->getMapNode(coord).updateTexture(Layer::TERRAIN);
Engine::instance().map->getMapNode(coord).setTileID(tileID, origCornerPoint);
Engine::instance().map->getMapNode(coord).setTileID("terrain_basalt", origCornerPoint);
Engine::instance().map->getMapNode(coord).setZIndex(minZ);
// LOG(LOG_INFO) << "i'm a multinode";
}
}
m_isoCoordinates.z = minZ;
LOG(LOG_INFO) << "new z " << minZ;
// m_isoCoordinates.z = m_isoCoordinates.z - Settings::instance().mapSize;
}
const Layer layer = TileManager::instance().getTileLayer(tileID);
switch (layer)
{
case Layer::ZONE:
this->setNodeTransparency(Settings::instance().zoneLayerTransparency, Layer::ZONE);
break;
case Layer::WATER:
demolishLayer(Layer::ROAD);
//TODO: we need to modify neighbors TileTypes to Shore.
// no break on purpose.
case Layer::ROAD:
// in case it's allowed then maybe a Tree Tile already exist, so we remove it.
demolishLayer(Layer::BUILDINGS);
break;
case Layer::BUILDINGS:
if (tileData->category != "Flora")
{
this->setNodeTransparency(0.6, Layer::BLUEPRINT);
}
m_mapNodeData[Layer::ZONE].shouldRender = false;
break;
default:
break;
}
m_mapNodeData[layer].origCornerPoint = origCornerPoint;
m_previousTileID = m_mapNodeData[layer].tileID;
m_mapNodeData[layer].tileData = tileData;
m_mapNodeData[layer].tileID = tileID;
// Determine if the tile should have a random rotation or not.
if (m_mapNodeData[layer].tileData->tiles.pickRandomTile && m_mapNodeData[layer].tileData->tiles.count > 1)
{
/** set tileIndex to a rand between 1 and count, this will be the displayed image of the entire tileset
* if this tile has ordered frames, like roads then pickRandomTile must be set to 0.
**/
m_mapNodeData[layer].tileIndex = rand() % m_mapNodeData[layer].tileData->tiles.count;
}
else
{
/** must be reset to 0 otherwise overwritting tiles would keep the old
* tile's tileIndex which creates problems if it's supposed to be 0
**/
m_mapNodeData[layer].tileIndex = 0;
}
updateTexture(layer);
}
}
Layer MapNode::getTopMostActiveLayer() const
{
if (MapLayers::isLayerActive(Layer::BUILDINGS) && m_mapNodeData[Layer::BUILDINGS].tileData)
{
return Layer::BUILDINGS;
}
else if (MapLayers::isLayerActive(Layer::BLUEPRINT) && m_mapNodeData[Layer::UNDERGROUND].tileData)
{
return Layer::UNDERGROUND;
}
else if (MapLayers::isLayerActive(Layer::BLUEPRINT) && m_mapNodeData[Layer::BLUEPRINT].tileData)
{
return Layer::BLUEPRINT;
}
else if (MapLayers::isLayerActive(Layer::GROUND_DECORATION) && m_mapNodeData[Layer::GROUND_DECORATION].tileData)
{
return Layer::GROUND_DECORATION;
}
// terrain is our fallback, since there's always terrain.
else if (MapLayers::isLayerActive(Layer::TERRAIN) && m_mapNodeData[Layer::TERRAIN].tileData)
{
return Layer::TERRAIN;
}
return Layer::NONE;
}
void MapNode::setNodeTransparency(const float transparencyFactor, const Layer &layer) const
{
// TODO refactoring: Consider replacing magic number (255) with constexpr.
unsigned char alpha = (1 - transparencyFactor) * 255;
m_sprite->setSpriteTranparencyFactor(layer, alpha);
}
bool MapNode::isPlacableOnSlope(const std::string &tileID) const
{
TileData *tileData = TileManager::instance().getTileData(tileID);
if (tileData && tileData->tileType == +TileType::ZONE)
{
// zones are allowed to pass slopes.
return true;
}
if (tileData && m_elevationOrientation != TileSlopes::DEFAULT_ORIENTATION)
{
// we need to check the terrain layer for it's orientation so we can calculate the resulting x offset in the spritesheet.
const int clipRectX = tileData->slopeTiles.clippingWidth * static_cast<int>(m_autotileOrientation[Layer::TERRAIN]);
// while loading game, m_previousTileID will be equal to "terrain" for terrin tiles while it's empty "" when starting new game.
// so the check here on m_previousTileID is needed both (temporary), empty and "terrain", this will be fixed in new PR.
if (clipRectX >= static_cast<int>(tileData->slopeTiles.count) * tileData->slopeTiles.clippingWidth &&
(m_previousTileID.empty() || m_previousTileID == "terrain"))
{
return false;
}
}
return true;
}
bool MapNode::isPlacementAllowed(const std::string &newTileID) const
{
TileData *tileData = TileManager::instance().getTileData(newTileID);
if (tileData)
{
const Layer layer = TileManager::instance().getTileLayer(newTileID);
// layer specific checks:
switch (layer)
{
case Layer::ZONE:
// zones can overplace themselves and everything else
return true;
case Layer::ROAD:
if ((isLayerOccupied(Layer::BUILDINGS) && (m_mapNodeData[Layer::BUILDINGS].tileData->category != "Flora")) ||
isLayerOccupied(Layer::WATER) || !isPlacableOnSlope(newTileID))
{ // roads cannot be placed:
// - on buildings that are not category flora.
// - on water
// - on slopetiles that don't have a tileID
return false;
}
return true;
case Layer::GROUND_DECORATION:
if (m_mapNodeData[Layer::GROUND_DECORATION].tileData || m_mapNodeData[Layer::BUILDINGS].tileData)
{ // allow placement of ground decoration on existing ground decoration and on buildings.
return true;
}
break;
case Layer::BUILDINGS:
TileData *tileDataBuildings = m_mapNodeData[Layer::BUILDINGS].tileData;
if (tileDataBuildings && tileDataBuildings->isOverPlacable)
{ // buildings with overplacable flag
return true;
}
if (isLayerOccupied(Layer::ROAD))
{ // buildings cannot be placed on roads
return false;
}
break;
}
// checks for all layers:
if (isLayerOccupied(Layer::WATER))
{
if (tileData->tileType != +TileType::WATER && !tileData->placeOnWater)
// Disallow placement on water for tiles that are:
// not of tiletype water
// not flag placeOnWater enabled
{
return false;
}
}
else // not water
{
if (!tileData->placeOnGround)
// Disallow placement on ground (meaning a tile that is not water) for tiles that have:
// not flag placeOnGround enabled
{
return false;
}
}
if (!isPlacableOnSlope(newTileID))
{ // Check if a tile has slope frames and therefore can be placed on a node with a slope
return false;
}
if (tileData->tileType == +TileType::UNDERGROUND)
{ // Underground tiletype (pipes, metro tunnels, ... ) can overplace each other
return true;
}
if (m_mapNodeData[layer].tileID.empty())
{ // of course allow placement on empty tiles
return true;
}
}
// every case that is not handled is false
return false;
}
void MapNode::updateTexture(const Layer &layer)
{
SDL_Rect clipRect{0, 0, 0, 0};
//TODO: Refactor this
m_elevationOrientation = TileManager::instance().calculateSlopeOrientation(m_elevationBitmask);
std::vector<Layer> layersToGoOver;
if (layer != Layer::NONE)
{
// in case this is not the default value (which is NONE), we need to update only 1 layer.
layersToGoOver.push_back(layer);
}
else
{
layersToGoOver.insert(layersToGoOver.begin(), std::begin(allLayersOrdered), std::end(allLayersOrdered));
}
for (auto currentLayer : layersToGoOver)
{
if (m_mapNodeData[currentLayer].tileData)
{
size_t spriteCount = 1;
m_mapNodeData[currentLayer].tileMap = TileMap::DEFAULT;
if (m_elevationOrientation == TileSlopes::DEFAULT_ORIENTATION)
{
if (m_mapNodeData[currentLayer].tileData->tileType == +TileType::WATER ||
m_mapNodeData[currentLayer].tileData->tileType == +TileType::TERRAIN ||
m_mapNodeData[currentLayer].tileData->tileType == +TileType::BLUEPRINT)
{
m_autotileOrientation[currentLayer] = TileManager::instance().calculateTileOrientation(m_autotileBitmask[currentLayer]);
m_mapNodeData[currentLayer].tileMap = TileMap::DEFAULT;
if (currentLayer == Layer::TERRAIN && m_autotileOrientation[currentLayer] != TileOrientation::TILE_DEFAULT_ORIENTATION)
{
m_mapNodeData[Layer::TERRAIN].tileMap = TileMap::SHORE;
// for shore tiles, we need to reset the tileIndex to 0, else a random tile would be picked. This is a little bit hacky.
m_mapNodeData[Layer::TERRAIN].tileIndex = 0;
}
}
// if the node can autotile, calculate it's tile orientation
else if (TileManager::instance().isTileIDAutoTile(getTileID(currentLayer)))
{
m_autotileOrientation[currentLayer] = TileManager::instance().calculateTileOrientation(m_autotileBitmask[currentLayer]);
}
}
else if (m_elevationOrientation >= TileSlopes::N && m_elevationOrientation <= TileSlopes::BETWEEN)
{
if (m_mapNodeData[currentLayer].tileData->slopeTiles.fileName.empty())
{
m_mapNodeData[currentLayer].tileMap = TileMap::DEFAULT;
m_autotileOrientation[currentLayer] = TileOrientation::TILE_DEFAULT_ORIENTATION;
}
else
{
m_mapNodeData[currentLayer].tileMap = TileMap::SLOPES; // TileSlopes [N,E,w,S]
m_autotileOrientation[currentLayer] = static_cast<TileOrientation>(m_elevationOrientation);
}
}
switch (m_mapNodeData[currentLayer].tileMap)
{
case TileMap::DEFAULT:
m_clippingWidth = m_mapNodeData[currentLayer].tileData->tiles.clippingWidth;
if (m_mapNodeData[currentLayer].tileIndex != 0)
{
clipRect.x = m_clippingWidth * m_mapNodeData[currentLayer].tileIndex;
}
else
{
// only check for rectangular roads when there are frames for it. Spritesheets with rect-roads have 20 items
if (GameStates::instance().rectangularRoads && m_mapNodeData[currentLayer].tileData->tiles.count == 20)
{
switch (m_autotileOrientation[currentLayer])
{
case TileOrientation::TILE_S_AND_W:
m_autotileOrientation[currentLayer] = TileOrientation::TILE_S_AND_W_RECT;
break;
case TileOrientation::TILE_S_AND_E:
m_autotileOrientation[currentLayer] = TileOrientation::TILE_S_AND_E_RECT;
break;
case TileOrientation::TILE_N_AND_E:
m_autotileOrientation[currentLayer] = TileOrientation::TILE_N_AND_E_RECT;
break;
case TileOrientation::TILE_N_AND_W:
m_autotileOrientation[currentLayer] = TileOrientation::TILE_N_AND_W_RECT;
break;
default:
break;
}
}
clipRect.x = m_clippingWidth * static_cast<int>(m_autotileOrientation[currentLayer]);
}
if (!m_mapNodeData[currentLayer].tileID.empty())
{
m_sprite->setClipRect({clipRect.x + m_clippingWidth * m_mapNodeData[currentLayer].tileData->tiles.offset, 0,
m_clippingWidth, m_mapNodeData[currentLayer].tileData->tiles.clippingHeight},
static_cast<Layer>(currentLayer));
if (m_mapNodeData[currentLayer].shouldRender)
{
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID),
static_cast<Layer>(currentLayer));
}
}
spriteCount = m_mapNodeData[currentLayer].tileData->tiles.count;
break;
case TileMap::SHORE:
m_clippingWidth = m_mapNodeData[currentLayer].tileData->shoreTiles.clippingWidth;
if (m_mapNodeData[currentLayer].tileIndex != 0)
{
clipRect.x = m_clippingWidth * m_mapNodeData[currentLayer].tileIndex;
}
else
{
clipRect.x = m_clippingWidth * static_cast<int>(m_autotileOrientation[currentLayer]);
}
if (!m_mapNodeData[currentLayer].tileID.empty())
{
m_sprite->setClipRect({clipRect.x + m_clippingWidth * m_mapNodeData[currentLayer].tileData->shoreTiles.offset, 0,
m_clippingWidth, m_mapNodeData[currentLayer].tileData->shoreTiles.clippingHeight},
static_cast<Layer>(currentLayer));
if (m_mapNodeData[currentLayer].shouldRender)
{
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID + "_shore"),
static_cast<Layer>(currentLayer));
}
}
spriteCount = m_mapNodeData[currentLayer].tileData->shoreTiles.count;
break;
case TileMap::SLOPES:
if (m_mapNodeData[currentLayer].tileData->slopeTiles.fileName.empty())
{
break;
}
m_clippingWidth = m_mapNodeData[currentLayer].tileData->slopeTiles.clippingWidth;
clipRect.x = m_mapNodeData[currentLayer].tileData->slopeTiles.clippingWidth *
static_cast<int>(m_autotileOrientation[currentLayer]);
spriteCount = m_mapNodeData[currentLayer].tileData->slopeTiles.count;
if (clipRect.x <= static_cast<int>(spriteCount) * m_clippingWidth)
{
m_sprite->setClipRect({clipRect.x + m_mapNodeData[currentLayer].tileData->slopeTiles.offset * m_clippingWidth, 0,
m_clippingWidth, m_mapNodeData[currentLayer].tileData->slopeTiles.clippingHeight},
static_cast<Layer>(currentLayer));
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID),
static_cast<Layer>(currentLayer));
}
break;
default:
break;
}
if (clipRect.x >= static_cast<int>(spriteCount) * m_clippingWidth)
{
m_mapNodeData[currentLayer].tileID = m_previousTileID;
if (m_previousTileID.empty())
{
m_mapNodeData[currentLayer].tileData = nullptr;
}
updateTexture(currentLayer);
}
m_sprite->spriteCount = spriteCount;
}
}
}
bool MapNode::isSlopeNode() const { return m_mapNodeData[Layer::TERRAIN].tileMap == TileMap::SLOPES; }
void MapNode::setCoordinates(const Point &newIsoCoordinates)
{
m_isoCoordinates = newIsoCoordinates;
m_sprite->isoCoordinates = m_isoCoordinates;
}
const MapNodeData &MapNode::getActiveMapNodeData() const { return m_mapNodeData[getTopMostActiveLayer()]; }
void MapNode::setMapNodeData(std::vector<MapNodeData> &&mapNodeData, const Point &currNodeIsoCoordinates)
{
m_mapNodeData.swap(mapNodeData);
this->setNodeTransparency(Settings::instance().zoneLayerTransparency, Layer::ZONE);
// updates the pointers to the tiles, after loading tileIDs from json
for (auto &it : m_mapNodeData)
{
delete it.tileData;
it.tileData = TileManager::instance().getTileData(it.tileID);
if (it.origCornerPoint != currNodeIsoCoordinates)
{
it.shouldRender = false;
}
}
}
void MapNode::demolishLayer(const Layer &layer)
{
m_mapNodeData[layer].tileData = nullptr;
m_mapNodeData[layer].tileID = "";
m_autotileOrientation[layer] =
TileOrientation::TILE_DEFAULT_ORIENTATION; // We need to reset TileOrientation, in case it's set (demolishing autotiles)
m_mapNodeData[layer].origCornerPoint = this->getCoordinates();
m_mapNodeData[Layer::ZONE].shouldRender = true;
//LOG(LOG_INFO) << "reset render to true";
m_sprite->clearSprite(layer);
}
void MapNode::demolishNode(const Layer &demolishLayer)
{
// allow to delete a single layer only
std::vector<Layer> layersToDemolish;
if (demolishLayer == Layer::NONE)
{
layersToDemolish = {Layer::BUILDINGS, Layer::UNDERGROUND, Layer::GROUND_DECORATION, Layer::ZONE, Layer::ROAD};
}
else
{
layersToDemolish.push_back(demolishLayer);
}
for (auto &layer : layersToDemolish)
{
if (MapLayers::isLayerActive(layer) && m_mapNodeData[layer].tileData)
{
if ((GameStates::instance().demolishMode == DemolishMode::DEFAULT &&
m_mapNodeData[layer].tileData->tileType == +TileType::ZONE) ||
(GameStates::instance().demolishMode == DemolishMode::DE_ZONE &&
m_mapNodeData[layer].tileData->tileType != +TileType::ZONE) ||
(GameStates::instance().demolishMode == DemolishMode::GROUND_DECORATION &&
m_mapNodeData[layer].tileData->tileType != +TileType::GROUNDDECORATION))
{
continue;
}
this->demolishLayer(layer);
if (layer == Layer::BUILDINGS)
{
for (auto *node : m_multiTileNodes)
{
node->demolishNode(layer);
node->setRenderFlag(layer, true);
}
this->setNodeTransparency(0, Layer::BLUEPRINT);
}
updateTexture(demolishLayer);
}
}
}