-
Notifications
You must be signed in to change notification settings - Fork 444
/
CPathfinder.cpp
628 lines (545 loc) · 16.3 KB
/
CPathfinder.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "StdInc.h"
#include "CPathfinder.h"
#include "CHeroHandler.h"
#include "mapping/CMap.h"
#include "CGameState.h"
#include "mapObjects/CGHeroInstance.h"
#include "GameConstants.h"
#include "CStopWatch.h"
/*
* CPathfinder.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
CPathfinder::PathfinderOptions::PathfinderOptions()
{
useFlying = false;
useWaterWalking = false;
useEmbarkAndDisembark = true;
useTeleportTwoWay = true;
useTeleportOneWay = true;
useTeleportOneWayRandom = false;
useTeleportWhirlpool = false;
lightweightFlyingMode = false;
}
CPathfinder::CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero) : CGameInfoCallback(_gs, boost::optional<PlayerColor>()), out(_out), hero(_hero), FoW(getPlayerTeam(hero->tempOwner)->fogOfWarMap)
{
assert(hero);
assert(hero == getHero(hero->id));
out.hero = hero;
out.hpos = hero->getPosition(false);
if(!gs->map->isInTheMap(out.hpos)/* || !gs->map->isInTheMap(dest)*/) //check input
{
logGlobal->errorStream() << "CGameState::calculatePaths: Hero outside the gs->map? How dare you...";
throw std::runtime_error("Wrong checksum");
}
if(hero->canFly())
options.useFlying = true;
if(hero->canWalkOnSea())
options.useWaterWalking = true;
if(CGWhirlpool::isProtected(hero))
options.useTeleportWhirlpool = true;
initializeGraph();
neighbours.reserve(16);
}
void CPathfinder::calculatePaths()
{
int maxMovePointsLand = hero->maxMovePoints(true);
int maxMovePointsWater = hero->maxMovePoints(false);
auto maxMovePoints = [&](CGPathNode *cp) -> int
{
return cp->land ? maxMovePointsLand : maxMovePointsWater;
};
auto isBetterWay = [&](int remains, int turn) -> bool
{
if(dp->turns == 0xff) //we haven't been here before
return true;
else if(dp->turns > turn)
return true;
else if(dp->turns >= turn && dp->moveRemains < remains) //this route is faster
return true;
return false;
};
//logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
//initial tile - set cost on 0 and add to the queue
CGPathNode *initialNode = out.getNode(out.hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND);
initialNode->turns = 0;
initialNode->moveRemains = hero->movement;
mq.push_back(initialNode);
while(!mq.empty())
{
cp = mq.front();
mq.pop_front();
int movement = cp->moveRemains, turn = cp->turns;
if(!movement)
{
movement = maxMovePoints(cp);
turn++;
}
//add accessible neighbouring nodes to the queue
addNeighbours(cp->coord);
for(auto & neighbour : neighbours)
{
dt = &gs->map->getTile(neighbour);
for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
{
useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark
dp = out.getNode(neighbour, i);
if(dp->accessible == CGPathNode::NOT_SET)
continue;
if(cp->layer != i && !isLayerTransitionPossible())
continue;
if(!isMovementToDestPossible())
continue;
int cost = gs->getMovementCost(hero, cp->coord, dp->coord, movement);
int remains = movement - cost;
if(useEmbarkCost)
{
remains = hero->movementPointsAfterEmbark(movement, cost, useEmbarkCost - 1);
cost = movement - remains;
}
int turnAtNextTile = turn;
if(remains < 0)
{
//occurs rarely, when hero with low movepoints tries to leave the road
turnAtNextTile++;
int moveAtNextTile = maxMovePoints(cp);
cost = gs->getMovementCost(hero, cp->coord, dp->coord, moveAtNextTile); //cost must be updated, movement points changed :(
remains = moveAtNextTile - cost;
}
if(isBetterWay(remains, turnAtNextTile))
{
assert(dp != cp->theNodeBefore); //two tiles can't point to each other
dp->moveRemains = remains;
dp->turns = turnAtNextTile;
dp->theNodeBefore = cp;
if(isMovementAfterDestPossible())
mq.push_back(dp);
}
}
} //neighbours loop
//just add all passable teleport exits
if(sTileObj && canVisitObject())
{
addTeleportExits();
for(auto & neighbour : neighbours)
{
dp = out.getNode(neighbour, cp->layer);
if(isBetterWay(movement, turn))
{
dp->moveRemains = movement;
dp->turns = turn;
dp->theNodeBefore = cp;
mq.push_back(dp);
}
}
}
} //queue loop
}
void CPathfinder::addNeighbours(const int3 &coord)
{
neighbours.clear();
ct = &gs->map->getTile(coord);
std::vector<int3> tiles;
gs->getNeighbours(*ct, coord, tiles, boost::logic::indeterminate, !cp->land);
sTileObj = ct->topVisitableObj(coord == CGHeroInstance::convertPosition(hero->pos, false));
if(canVisitObject())
{
if(sTileObj)
{
for(int3 tile: tiles)
{
if(canMoveBetween(tile, sTileObj->visitablePos()))
neighbours.push_back(tile);
}
}
else
vstd::concatenate(neighbours, tiles);
}
else
vstd::concatenate(neighbours, tiles);
}
void CPathfinder::addTeleportExits(bool noTeleportExcludes)
{
assert(sTileObj);
neighbours.clear();
auto isAllowedTeleportEntrance = [&](const CGTeleport * obj) -> bool
{
if(!gs->isTeleportEntrancePassable(obj, hero->tempOwner))
return false;
if(noTeleportExcludes)
return true;
auto whirlpool = dynamic_cast<const CGWhirlpool *>(obj);
if(whirlpool)
{
if(addTeleportWhirlpool(whirlpool))
return true;
}
else if(addTeleportTwoWay(obj) || addTeleportOneWay(obj) || addTeleportOneWayRandom(obj))
return true;
return false;
};
const CGTeleport *sTileTeleport = dynamic_cast<const CGTeleport *>(sTileObj);
if(isAllowedTeleportEntrance(sTileTeleport))
{
for(auto objId : gs->getTeleportChannelExits(sTileTeleport->channel, hero->tempOwner))
{
auto obj = getObj(objId);
if(CGTeleport::isExitPassable(gs, hero, obj))
neighbours.push_back(obj->visitablePos());
}
}
}
bool CPathfinder::isLayerTransitionPossible()
{
if((cp->layer == EPathfindingLayer::AIR || cp->layer == EPathfindingLayer::WATER)
&& dp->layer != EPathfindingLayer::LAND)
{
return false;
}
else if(cp->layer == EPathfindingLayer::LAND && dp->layer == EPathfindingLayer::AIR)
{
if(options.lightweightFlyingMode && cp->coord != hero->getPosition(false))
return false;
}
else if(cp->layer == EPathfindingLayer::SAIL && dp->layer != EPathfindingLayer::LAND)
return false;
else if(cp->layer == EPathfindingLayer::SAIL && dp->layer == EPathfindingLayer::LAND)
{
if(!dt->isCoastal())
return false;
//tile must be accessible -> exception: unblocked blockvis tiles -> clear but guarded by nearby monster coast
if((dp->accessible != CGPathNode::ACCESSIBLE && (dp->accessible != CGPathNode::BLOCKVIS || dt->blocked))
|| dt->visitable) //TODO: passableness problem -> town says it's passable (thus accessible) but we obviously can't disembark onto town gate
return false;
useEmbarkCost = 2;
}
else if(cp->layer == EPathfindingLayer::LAND && dp->layer == EPathfindingLayer::SAIL)
{
Obj destTopVisObjID = dt->topVisitableId();
if(dp->accessible == CGPathNode::ACCESSIBLE || destTopVisObjID < 0) //cannot enter empty water tile from land -> it has to be visitable
return false;
if(destTopVisObjID != Obj::HERO && destTopVisObjID != Obj::BOAT) //only boat or hero can be accessed from land
return false;
if(destTopVisObjID == Obj::BOAT)
useEmbarkCost = 1;
}
return true;
}
bool CPathfinder::isMovementToDestPossible()
{
switch (dp->layer)
{
case EPathfindingLayer::LAND:
if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
return false;
if(isSourceGuarded() && !isDestinationGuardian()) // Can step into tile of guard
return false;
break;
case EPathfindingLayer::SAIL:
if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
return false;
if(isSourceGuarded() && !isDestinationGuardian()) // Can step into tile of guard
return false;
break;
case EPathfindingLayer::AIR:
//if(!canMoveBetween(cp->coord, dp->coord))
// return false;
break;
case EPathfindingLayer::WATER:
if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
return false;
if(isDestinationGuarded())
return false;
break;
}
return true;
}
bool CPathfinder::isMovementAfterDestPossible()
{
switch (dp->layer)
{
case EPathfindingLayer::LAND:
case EPathfindingLayer::SAIL:
if(dp->accessible == CGPathNode::ACCESSIBLE)
return true;
if(dp->coord == CGHeroInstance::convertPosition(hero->pos, false))
return true; // This one is tricky, we can ignore fact that tile is not ACCESSIBLE in case if it's our hero block it. Though this need investigation
if(dp->accessible == CGPathNode::VISITABLE && CGTeleport::isTeleport(dt->topVisitableObj()))
return true; // For now we'll always allow transit for teleporters
if(useEmbarkCost && options.useEmbarkAndDisembark)
return true;
if(isDestinationGuarded() && !isSourceGuarded())
return true; // Can step into a hostile tile once
break;
case EPathfindingLayer::AIR:
case EPathfindingLayer::WATER:
return true;
break;
}
return false;
}
int3 CPathfinder::getSourceGuardPosition()
{
return gs->map->guardingCreaturePositions[cp->coord.x][cp->coord.y][cp->coord.z];
}
bool CPathfinder::isSourceGuarded()
{
//map can start with hero on guarded tile or teleport there using dimension door
//so threat tile hero standing on like it's not guarded because it's should be possible to move out of here
if(getSourceGuardPosition() != int3(-1, -1, -1)
&& cp->coord != hero->getPosition(false))
{
//special case -> hero embarked a boat standing on a guarded tile -> we must allow to move away from that tile
if(cp->accessible != CGPathNode::VISITABLE
|| !cp->theNodeBefore->land
|| ct->topVisitableId() != Obj::BOAT)
{
return true;
}
}
return false;
}
bool CPathfinder::isDestinationGuarded()
{
if(gs->map->guardingCreaturePositions[dp->coord.x][dp->coord.y][dp->coord.z].valid()
&& dp->accessible == CGPathNode::BLOCKVIS)
{
return true;
}
return false;
}
bool CPathfinder::isDestinationGuardian()
{
return getSourceGuardPosition() == dp->coord;
}
void CPathfinder::initializeGraph()
{
auto initializeNode = [&](int3 pos, EPathfindingLayer layer, const TerrainTile *tinfo)
{
auto node = out.getNode(pos, layer);
node->accessible = evaluateAccessibility(pos, tinfo);
node->turns = 0xff;
node->moveRemains = 0;
node->coord = pos;
node->land = tinfo->terType != ETerrainType::WATER;
node->theNodeBefore = nullptr;
node->layer = layer;
};
int3 pos;
for(pos.x=0; pos.x < out.sizes.x; ++pos.x)
{
for(pos.y=0; pos.y < out.sizes.y; ++pos.y)
{
for(pos.z=0; pos.z < out.sizes.z; ++pos.z)
{
const TerrainTile *tinfo = &gs->map->getTile(pos);
switch (tinfo->terType)
{
case ETerrainType::WRONG:
case ETerrainType::BORDER:
case ETerrainType::ROCK:
break;
case ETerrainType::WATER:
initializeNode(pos, EPathfindingLayer::SAIL, tinfo);
if(options.useFlying)
initializeNode(pos, EPathfindingLayer::AIR, tinfo);
if(options.useWaterWalking)
initializeNode(pos, EPathfindingLayer::WATER, tinfo);
break;
default:
initializeNode(pos, EPathfindingLayer::LAND, tinfo);
if(options.useFlying)
initializeNode(pos, EPathfindingLayer::AIR, tinfo);
break;
}
}
}
}
}
CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const
{
CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE);
if(tinfo->terType == ETerrainType::ROCK || !FoW[pos.x][pos.y][pos.z])
return CGPathNode::BLOCKED;
if(tinfo->visitable)
{
if(tinfo->visitableObjects.front()->ID == Obj::SANCTUARY && tinfo->visitableObjects.back()->ID == Obj::HERO && tinfo->visitableObjects.back()->tempOwner != hero->tempOwner) //non-owned hero stands on Sanctuary
{
return CGPathNode::BLOCKED;
}
else
{
for(const CGObjectInstance *obj : tinfo->visitableObjects)
{
if(obj->passableFor(hero->tempOwner))
{
ret = CGPathNode::ACCESSIBLE;
}
else if(obj->blockVisit)
{
return CGPathNode::BLOCKVIS;
}
else if(obj->ID != Obj::EVENT) //pathfinder should ignore placed events
{
ret = CGPathNode::VISITABLE;
}
}
}
}
else if(gs->map->guardingCreaturePositions[pos.x][pos.y][pos.z].valid()
&& !tinfo->blocked)
{
// Monster close by; blocked visit for battle.
return CGPathNode::BLOCKVIS;
}
return ret;
}
bool CPathfinder::canMoveBetween(const int3 &a, const int3 &b) const
{
return gs->checkForVisitableDir(a, b);
}
bool CPathfinder::addTeleportTwoWay(const CGTeleport * obj) const
{
return options.useTeleportTwoWay && gs->isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
}
bool CPathfinder::addTeleportOneWay(const CGTeleport * obj) const
{
if(options.useTeleportOneWay && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
{
auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
if(passableExits.size() == 1)
return true;
}
return false;
}
bool CPathfinder::addTeleportOneWayRandom(const CGTeleport * obj) const
{
if(options.useTeleportOneWayRandom && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
{
auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
if(passableExits.size() > 1)
return true;
}
return false;
}
bool CPathfinder::addTeleportWhirlpool(const CGWhirlpool * obj) const
{
return options.useTeleportWhirlpool && obj;
}
bool CPathfinder::canVisitObject() const
{
//hero can't visit objects while walking on water or flying
return cp->layer == EPathfindingLayer::LAND || cp->layer == EPathfindingLayer::SAIL;
}
CGPathNode::CGPathNode()
:coord(-1,-1,-1)
{
accessible = NOT_SET;
land = 0;
moveRemains = 0;
turns = 255;
theNodeBefore = nullptr;
layer = EPathfindingLayer::WRONG;
}
bool CGPathNode::reachable() const
{
return turns < 255;
}
int3 CGPath::startPos() const
{
return nodes[nodes.size()-1].coord;
}
int3 CGPath::endPos() const
{
return nodes[0].coord;
}
void CGPath::convert( ui8 mode )
{
if(mode==0)
{
for(auto & elem : nodes)
{
elem.coord = CGHeroInstance::convertPosition(elem.coord,true);
}
}
}
CPathsInfo::CPathsInfo( const int3 &Sizes )
:sizes(Sizes)
{
hero = nullptr;
nodes = new CGPathNode***[sizes.x];
for(int i = 0; i < sizes.x; i++)
{
nodes[i] = new CGPathNode**[sizes.y];
for(int j = 0; j < sizes.y; j++)
{
nodes[i][j] = new CGPathNode*[sizes.z];
for (int z = 0; z < sizes.z; z++)
{
nodes[i][j][z] = new CGPathNode[EPathfindingLayer::NUM_LAYERS];
}
}
}
}
CPathsInfo::~CPathsInfo()
{
for(int i = 0; i < sizes.x; i++)
{
for(int j = 0; j < sizes.y; j++)
{
for (int z = 0; z < sizes.z; z++)
{
delete [] nodes[i][j][z];
}
delete [] nodes[i][j];
}
delete [] nodes[i];
}
delete [] nodes;
}
const CGPathNode * CPathsInfo::getPathInfo(const int3 &tile, const EPathfindingLayer &layer) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
if(tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z || layer >= EPathfindingLayer::NUM_LAYERS)
return nullptr;
return getNode(tile, layer);
}
bool CPathsInfo::getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
out.nodes.clear();
const CGPathNode *curnode = getNode(dst, layer);
if(!curnode->theNodeBefore)
return false;
while(curnode)
{
CGPathNode cpn = *curnode;
curnode = curnode->theNodeBefore;
out.nodes.push_back(cpn);
}
return true;
}
int CPathsInfo::getDistance(const int3 &tile, const EPathfindingLayer &layer) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
CGPath ret;
if(getPath(ret, tile, layer))
return ret.nodes.size();
else
return 255;
}
CGPathNode *CPathsInfo::getNode(const int3 &coord, const EPathfindingLayer &layer) const
{
if(layer != EPathfindingLayer::AUTO)
return &nodes[coord.x][coord.y][coord.z][layer];
auto landNode = &nodes[coord.x][coord.y][coord.z][EPathfindingLayer::LAND];
if(landNode->theNodeBefore)
return landNode;
else
return &nodes[coord.x][coord.y][coord.z][EPathfindingLayer::SAIL];
}