1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Merge branch 'develop' into issue/2306

Need to reapply changes to pathfiner

Conflicts:
	AI/VCAI/VCAI.cpp
	lib/CGameState.cpp
	lib/CGameStateFwd.h
This commit is contained in:
AlexVinS 2015-11-01 01:49:57 +03:00
commit d46364c4c3
28 changed files with 953 additions and 644 deletions

View File

@ -359,7 +359,7 @@ int3 whereToExplore(HeroPtr h)
int radius = h->getSightRadious();
int3 hpos = h->visitablePos();
SectorMap sm(h);
SectorMap &sm = ai->getCachedSectorMap(h);
//look for nearby objs -> visit them if they're close enouh
const int DIST_LIMIT = 3;

View File

@ -302,6 +302,8 @@ Goals::TSubgoal FuzzyHelper::chooseSolution (Goals::TGoalVec vec)
if (vec.empty()) //no possibilities found
return sptr(Goals::Invalid());
ai->cachedSectorMaps.clear();
//a trick to switch between heroes less often - calculatePaths is costly
auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
{
@ -480,8 +482,7 @@ float FuzzyHelper::evaluate (Goals::ClearWayTo & g)
if (!g.hero.h)
throw cannotFulfillGoalException("ClearWayTo called without hero!");
SectorMap sm(g.hero);
int3 t = sm.firstTileToGet(g.hero, g.tile);
int3 t = ai->getCachedSectorMap(g.hero).firstTileToGet(g.hero, g.tile);
if (t.valid())
{
@ -529,4 +530,4 @@ float FuzzyHelper::evaluate (Goals::AbstractGoal & g)
void FuzzyHelper::setPriority (Goals::TSubgoal & g)
{
g->setpriority(g->accept(this)); //this enforces returned value is set
}
}

View File

@ -15,6 +15,7 @@
class VCAI;
class CArmedInstance;
class CBank;
class SectorMap;
class engineBase
{
@ -54,6 +55,8 @@ class FuzzyHelper
~EvalVisitTile();
} vt;
public:
enum RuleBlocks {BANK_DANGER, TACTICAL_ADVANTAGE, VISIT_TILE};
//blocks should be initialized in this order, which may be confusing :/

View File

@ -483,7 +483,7 @@ TGoalVec ClearWayTo::getAllPossibleSubgoals()
//if our hero is trapped, make sure we request clearing the way from OUR perspective
SectorMap sm(h);
SectorMap &sm = ai->getCachedSectorMap(h);
int3 tileToHit = sm.firstTileToGet(h, tile);
if (!tileToHit.valid())
@ -518,10 +518,10 @@ TGoalVec ClearWayTo::getAllPossibleSubgoals()
{
//TODO: we should be able to return apriopriate quest here (VCAI::striveToQuest)
logAi->debugStream() << "Quest guard blocks the way to " + tile();
continue; //do not access quets guard if we can't complete the quest
}
}
}
if (isSafeToVisit(h, tileToHit)) //this makes sense only if tile is guarded, but there i no quest object
{
ret.push_back (sptr (Goals::VisitTile(tileToHit).sethero(h)));
@ -633,7 +633,7 @@ TGoalVec Explore::getAllPossibleSubgoals()
for (auto h : heroes)
{
SectorMap sm(h);
SectorMap &sm = ai->getCachedSectorMap(h);
for (auto obj : objs) //double loop, performance risk?
{
@ -963,7 +963,7 @@ TGoalVec Conquer::getAllPossibleSubgoals()
for (auto h : cb->getHeroesInfo())
{
SectorMap sm(h);
SectorMap &sm = ai->getCachedSectorMap(h);
std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
for (auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
@ -971,11 +971,30 @@ TGoalVec Conquer::getAllPossibleSubgoals()
if (conquerable(obj))
ourObjs.push_back(obj);
}
for (auto obj : ourObjs) //double loop, performance risk?
for (auto obj : ourObjs)
{
auto t = sm.firstTileToGet(h, obj->visitablePos()); //we assume that no more than one tile on the way is guarded
if (ai->isTileNotReserved(h, t))
ret.push_back (sptr(Goals::ClearWayTo(obj->visitablePos(), h).setisAbstract(true)));
int3 dest = obj->visitablePos();
auto t = sm.firstTileToGet(h, dest); //we assume that no more than one tile on the way is guarded
if (t.valid()) //we know any path at all
{
if (ai->isTileNotReserved(h, t)) //no other hero wants to conquer that tile
{
if (isSafeToVisit(h, dest))
{
if (dest != t) //there is something blocking our way
ret.push_back(sptr(Goals::ClearWayTo(dest, h).setisAbstract(true)));
else
{
if (obj->ID.num == Obj::HERO) //enemy hero may move to other position
ret.push_back(sptr(Goals::VisitHero(obj->id.getNum()).sethero(h).setisAbstract(true)));
else //just visit that tile
ret.push_back(sptr(Goals::VisitTile(dest).sethero(h).setisAbstract(true)));
}
}
else //we need to get army in order to conquer that place
ret.push_back(sptr(Goals::GatherArmy(evaluateDanger(dest, h) * SAFE_ATTACK_CONSTANT).sethero(h).setisAbstract(true)));
}
}
}
}
if (!objs.empty() && ai->canRecruitAnyHero()) //probably no point to recruit hero if we see no objects to capture
@ -1074,7 +1093,7 @@ TGoalVec GatherArmy::getAllPossibleSubgoals()
}
for(auto h : cb->getHeroesInfo())
{
SectorMap sm(h);
SectorMap &sm = ai->getCachedSectorMap(h);
for (auto obj : objs)
{ //find safe dwelling
auto pos = obj->visitablePos();

View File

@ -134,6 +134,7 @@ public:
isAbstract = false;
value = 0;
aid = -1;
objid = -1;
resID = -1;
tile = int3(-1, -1, -1);
town = nullptr;
@ -300,7 +301,7 @@ public:
VisitHero(int hid) : CGoal (Goals::VISIT_HERO){objid = hid; priority = 4;};
TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
TSubgoal whatToDoToAchieve() override;
//bool operator== (VisitHero &g) {return g.objid == objid;}
bool operator== (VisitHero &g) { return g.goalType == goalType && g.objid == objid; }
bool fulfillsMe (TSubgoal goal) override;
std::string completeMessage() const override;
};
@ -322,7 +323,7 @@ public:
VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {tile = Tile; priority = 5;};
TGoalVec getAllPossibleSubgoals() override;
TSubgoal whatToDoToAchieve() override;
//bool operator== (VisitTile &g) {return g.tile == tile;}
bool operator== (VisitTile &g) { return g.goalType == goalType && g.tile == tile; }
std::string completeMessage() const override;
};
class ClearWayTo : public CGoal<ClearWayTo>
@ -334,7 +335,7 @@ public:
ClearWayTo(int3 Tile, HeroPtr h) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile; hero = h; priority = 5;};
TGoalVec getAllPossibleSubgoals() override;
TSubgoal whatToDoToAchieve() override;
bool operator== (ClearWayTo &g) {return g.tile == tile;}
bool operator== (ClearWayTo &g) { return g.goalType == goalType && g.tile == tile; }
};
class DigAtTile : public CGoal<DigAtTile>
//elementar with hero on tile
@ -345,7 +346,7 @@ public:
DigAtTile(int3 Tile) : CGoal (Goals::DIG_AT_TILE) {tile = Tile; priority = 20;};
TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
TSubgoal whatToDoToAchieve() override;
bool operator== (DigAtTile &g) {return g.tile == tile;}
bool operator== (DigAtTile &g) { return g.goalType == goalType && g.tile == tile; }
};
class CIssueCommand : public CGoal<CIssueCommand>

View File

@ -115,6 +115,7 @@ void VCAI::heroMoved(const TryMoveHero & details)
NET_EVENT_HANDLER;
validateObject(details.id); //enemy hero may have left visible area
cachedSectorMaps.clear();
if(details.result == TryMoveHero::TELEPORTATION)
{
@ -296,7 +297,7 @@ void VCAI::tileRevealed(const std::unordered_set<int3, ShashInt3> &pos)
for(const CGObjectInstance *obj : myCb->getVisitableObjs(tile))
addVisitableObj(obj);
clearHeroesUnableToExplore();
clearPathsInfo();
}
void VCAI::heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query)
@ -382,7 +383,7 @@ void VCAI::newObject(const CGObjectInstance * obj)
if(obj->isVisitable())
addVisitableObj(obj);
clearHeroesUnableToExplore();
cachedSectorMaps.clear();
}
void VCAI::objectRemoved(const CGObjectInstance *obj)
@ -396,6 +397,8 @@ void VCAI::objectRemoved(const CGObjectInstance *obj)
for (auto h : cb->getHeroesInfo())
unreserveObject(h, obj);
cachedSectorMaps.clear(); //invalidate all paths
//TODO
//there are other places where CGObjectinstance ptrs are stored...
//
@ -701,12 +704,12 @@ void makePossibleUpgrades(const CArmedInstance *obj)
void VCAI::makeTurn()
{
logGlobal->infoStream() << boost::format("Player %d starting turn") % static_cast<int>(playerID.getNum());
MAKING_TURN;
boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
setThreadName("VCAI::makeTurn");
logGlobal->infoStream() << boost::format("Player %d starting turn") % static_cast<int>(playerID.getNum());
switch(cb->getDate(Date::DAY_OF_WEEK))
{
case 1:
@ -840,7 +843,7 @@ void VCAI::makeTurnInternal()
bool VCAI::goVisitObj(const CGObjectInstance * obj, HeroPtr h)
{
int3 dst = obj->visitablePos();
SectorMap sm(h);
SectorMap &sm = getCachedSectorMap(h);
logAi->debugStream() << boost::format("%s will try to visit %s at (%s)") % h->name % obj->getObjectName() % strFromInt3(dst);
int3 pos = sm.firstTileToGet(h, dst);
if (!pos.valid()) //rare case when we are already standing on one of potential objects
@ -1378,7 +1381,7 @@ std::vector<const CGObjectInstance *> VCAI::getPossibleDestinations(HeroPtr h)
{
validateVisitableObjs();
std::vector<const CGObjectInstance *> possibleDestinations;
SectorMap sm(h);
SectorMap &sm = getCachedSectorMap(h);
for(const CGObjectInstance *obj : visitableObjs)
{
if (isGoodForVisit(obj, h, sm))
@ -1436,7 +1439,7 @@ void VCAI::wander(HeroPtr h)
validateVisitableObjs();
std::vector <ObjectIdRef> dests, tmp;
SectorMap sm(h);
SectorMap &sm = getCachedSectorMap(h);
range::copy(reservedHeroesMap[h], std::back_inserter(tmp)); //also visit our reserved objects - but they are not prioritized to avoid running back and forth
for (auto obj : tmp)
@ -1649,9 +1652,10 @@ bool VCAI::isAbleToExplore (HeroPtr h)
{
return !vstd::contains (heroesUnableToExplore, h);
}
void VCAI::clearHeroesUnableToExplore()
void VCAI::clearPathsInfo()
{
heroesUnableToExplore.clear();
cachedSectorMaps.clear();
}
void VCAI::validateVisitableObjs()
@ -1932,6 +1936,8 @@ bool VCAI::moveHeroToTile(int3 dst, HeroPtr h)
if(h) //we could have lost hero after last move
{
completeGoal (sptr(Goals::VisitTile(dst).sethero(h))); //we stepped on some tile, anyway
completeGoal (sptr(Goals::ClearWayTo(dst).sethero(h)));
if (!ret) //reserve object we are heading towards
{
auto obj = frontOrNull(cb->getVisitableObjs(dst));
@ -2455,11 +2461,13 @@ void VCAI::buildArmyIn(const CGTownInstance * t)
int3 VCAI::explorationBestNeighbour(int3 hpos, int radius, HeroPtr h)
{
int3 ourPos = h->convertPosition(h->pos, false);
std::map<int3, int> dstToRevealedTiles;
for(crint3 dir : dirs)
if(cb->isInTheMap(hpos+dir))
if (isSafeToVisit(h, hpos + dir) && isAccessibleForHero (hpos + dir, h))
dstToRevealedTiles[hpos + dir] = howManyTilesWillBeDiscovered(radius, hpos, dir);
if (ourPos != dir) //don't stand in place
if (isSafeToVisit(h, hpos + dir) && isAccessibleForHero (hpos + dir, h))
dstToRevealedTiles[hpos + dir] = howManyTilesWillBeDiscovered(radius, hpos, dir);
if (dstToRevealedTiles.empty()) //yes, it DID happen!
throw cannotFulfillGoalException("No neighbour will bring new discoveries!");
@ -2481,14 +2489,13 @@ int3 VCAI::explorationBestNeighbour(int3 hpos, int radius, HeroPtr h)
int3 VCAI::explorationNewPoint(HeroPtr h)
{
//logAi->debugStream() << "Looking for an another place for exploration...";
int radius = h->getSightRadious();
CCallback * cbp = cb.get();
const CGHeroInstance * hero = h.get();
std::vector<std::vector<int3> > tiles; //tiles[distance_to_fow]
tiles.resize(radius);
CCallback * cbp = cb.get();
foreach_tile_pos([&](const int3 &pos)
{
if(!cbp->isVisible(pos))
@ -2497,6 +2504,7 @@ int3 VCAI::explorationNewPoint(HeroPtr h)
float bestValue = 0; //discovered tile to node distance ratio
int3 bestTile(-1,-1,-1);
int3 ourPos = h->convertPosition(h->pos, false);
for (int i = 1; i < radius; i++)
{
@ -2505,11 +2513,13 @@ int3 VCAI::explorationNewPoint(HeroPtr h)
for(const int3 &tile : tiles[i])
{
if (!cb->getPathsInfo(h.get())->getPathInfo(tile)->reachable()) //this will remove tiles that are guarded by monsters (or removable objects)
if (tile == ourPos) //shouldn't happen, but it does
continue;
if (!cb->getPathsInfo(hero)->getPathInfo(tile)->reachable()) //this will remove tiles that are guarded by monsters (or removable objects)
continue;
CGPath path;
cb->getPathsInfo(h.get())->getPath(tile, path);
cb->getPathsInfo(hero)->getPath(tile, path);
float ourValue = (float)howManyTilesWillBeDiscovered(tile, radius, cbp) / (path.nodes.size() + 1); //+1 prevents erratic jumps
if (ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
@ -2527,8 +2537,7 @@ int3 VCAI::explorationNewPoint(HeroPtr h)
int3 VCAI::explorationDesperate(HeroPtr h)
{
//logAi->debugStream() << "Looking for an another place for exploration...";
SectorMap sm(h);
SectorMap &sm = getCachedSectorMap(h);
int radius = h->getSightRadious();
std::vector<std::vector<int3> > tiles; //tiles[distance_to_fow]
@ -2679,6 +2688,7 @@ void VCAI::lostHero(HeroPtr h)
erase_if_present(reservedObjs, obj); //unreserve all objects for that hero
}
erase_if_present(reservedHeroesMap, h);
erase_if_present(cachedSectorMaps, h);
}
void VCAI::answerQuery(QueryID queryID, int selection)
@ -2739,6 +2749,18 @@ TResources VCAI::freeResources() const
return myRes;
}
SectorMap& VCAI::getCachedSectorMap(HeroPtr h)
{
auto it = cachedSectorMaps.find(h);
if (it != cachedSectorMaps.end())
return it->second;
else
{
cachedSectorMaps.insert(std::make_pair(h, SectorMap(h)));
return cachedSectorMaps[h];
}
}
AIStatus::AIStatus()
{
battle = NO_BATTLE;
@ -2767,18 +2789,20 @@ BattleState AIStatus::getBattle()
}
void AIStatus::addQuery(QueryID ID, std::string description)
{
boost::unique_lock<boost::mutex> lock(mx);
{
if(ID == QueryID(-1))
{
logAi->debugStream() << boost::format("The \"query\" has an id %d, it'll be ignored as non-query. Description: %s") % ID % description;
return;
}
assert(!vstd::contains(remainingQueries, ID));
assert(ID.getNum() >= 0);
boost::unique_lock<boost::mutex> lock(mx);
assert(!vstd::contains(remainingQueries, ID));
remainingQueries[ID] = description;
cv.notify_all();
logAi->debugStream() << boost::format("Adding query %d - %s. Total queries count: %d") % ID % description % remainingQueries.size();
}
@ -2790,6 +2814,7 @@ void AIStatus::removeQuery(QueryID ID)
std::string description = remainingQueries[ID];
remainingQueries.erase(ID);
cv.notify_all();
logAi->debugStream() << boost::format("Removing query %d - %s. Total queries count: %d") % ID % description % remainingQueries.size();
}
@ -2900,7 +2925,7 @@ SectorMap::SectorMap(HeroPtr h)
makeParentBFS(h->visitablePos());
}
bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
bool SectorMap::markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
{
if(t->blocked && !t->visitable)
{
@ -2911,13 +2936,15 @@ bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
return false;
}
bool markIfBlocked(ui8 &sec, crint3 pos)
bool SectorMap::markIfBlocked(ui8 &sec, crint3 pos)
{
return markIfBlocked(sec, pos, cb->getTile(pos));
return markIfBlocked(sec, pos, getTile(pos));
}
void SectorMap::update()
{
visibleTiles = cb->getAllVisibleTiles();
clear();
int curSector = 3; //0 is invisible, 1 is not explored
@ -2943,7 +2970,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
{
Sector &s = infoOnSectors[num];
s.id = num;
s.water = cbp->getTile(pos)->isWater();
s.water = getTile(pos)->isWater();
std::queue<int3> toVisit;
toVisit.push(pos);
@ -2954,7 +2981,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
ui8 &sec = retreiveTile(curPos);
if(sec == NOT_CHECKED)
{
const TerrainTile *t = cbp->getTile(curPos);
const TerrainTile *t = getTile(curPos);
if(!markIfBlocked(sec, curPos, t))
{
if(t->isWater() == s.water) //sector is only-water or only-land
@ -2968,7 +2995,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
toVisit.push(neighPos);
//parent[neighPos] = curPos;
}
const TerrainTile *nt = cbp->getTile(neighPos, false);
const TerrainTile *nt = getTile(neighPos);
if(nt && nt->isWater() != s.water && canBeEmbarkmentPoint(nt, s.water))
{
s.embarkmentPoints.push_back(neighPos);
@ -3209,7 +3236,7 @@ For ship construction etc, another function (goal?) is needed
//embark on ship -> look for an EP with a boat
auto firstEP = boost::find_if(src->embarkmentPoints, [=](crint3 pos) -> bool
{
const TerrainTile *t = cb->getTile(pos);
const TerrainTile *t = getTile(pos);
return t && t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT
&& retreiveTile(pos) == sectorToReach->id;
});
@ -3299,7 +3326,7 @@ For ship construction etc, another function (goal?) is needed
{
//make sure no hero block the way
auto pos = ai->knownSubterraneanGates[gate]->visitablePos();
const TerrainTile *t = cb->getTile(pos);
const TerrainTile *t = getTile(pos);
return t && t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::SUBTERRANEAN_GATE
&& retreiveTile(pos) == sectorToReach->id;
});
@ -3339,7 +3366,7 @@ int3 SectorMap::findFirstVisitableTile (HeroPtr h, crint3 dst)
while(curtile != h->visitablePos())
{
auto topObj = cb->getTopObj(curtile);
if (topObj && topObj->ID == Obj::HERO && topObj != h.h)
if (topObj && topObj->ID == Obj::HERO && h->tempOwner == topObj->tempOwner && topObj != h.h)
{
logAi->warnStream() << ("Another allied hero stands in our way");
return ret;
@ -3399,3 +3426,10 @@ unsigned char & SectorMap::retreiveTile(crint3 pos)
{
return retreiveTileN(sector, pos);
}
TerrainTile* SectorMap::getTile(crint3 pos) const
{
//out of bounds access should be handled by boost::multi_array
//still we cached this array to avoid any checks
return visibleTiles->operator[](pos.x)[pos.y][pos.z];
}

View File

@ -95,6 +95,7 @@ struct SectorMap
//std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
std::map<int, Sector> infoOnSectors;
shared_ptr<boost::multi_array<TerrainTile*, 3>> visibleTiles;
SectorMap();
SectorMap(HeroPtr h);
@ -103,7 +104,10 @@ struct SectorMap
void exploreNewSector(crint3 pos, int num, CCallback * cbp);
void write(crstring fname);
bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t);
bool markIfBlocked(ui8 &sec, crint3 pos);
unsigned char &retreiveTile(crint3 pos);
TerrainTile* getTile(crint3 pos) const;
void makeParentBFS(crint3 source);
@ -157,6 +161,8 @@ public:
std::set<const CGObjectInstance *> alreadyVisited;
std::set<const CGObjectInstance *> reservedObjs; //to be visited by specific hero
std::map <HeroPtr, SectorMap> cachedSectorMaps; //TODO: serialize? not necessary
TResources saving;
AIStatus status;
@ -292,7 +298,7 @@ public:
void markHeroUnableToExplore (HeroPtr h);
void markHeroAbleToExplore (HeroPtr h);
bool isAbleToExplore (HeroPtr h);
void clearHeroesUnableToExplore();
void clearPathsInfo();
void validateObject(const CGObjectInstance *obj); //checks if object is still visible and if not, removes references to it
void validateObject(ObjectIdRef obj); //checks if object is still visible and if not, removes references to it
@ -307,6 +313,8 @@ public:
const CGObjectInstance *getUnvisitedObj(const std::function<bool(const CGObjectInstance *)> &predicate);
bool isAccessibleForHero(const int3 & pos, HeroPtr h, bool includeAllies = false) const;
//optimization - use one SM for every hero call
SectorMap& getCachedSectorMap(HeroPtr h);
const CGTownInstance *findTownWithTavern() const;
bool canRecruitAnyHero(const CGTownInstance * t = NULL) const;

View File

@ -292,7 +292,7 @@ bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
int CCallback::getMovementCost(const CGHeroInstance * hero, int3 dest)
{
return gs->getMovementCost(hero, hero->visitablePos(), dest, hero->hasBonusOfType (Bonus::FLYING_MOVEMENT), hero->movement);
return gs->getMovementCost(hero, hero->visitablePos(), dest, hero->movement);
}
const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)

View File

@ -158,6 +158,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
#include <boost/thread.hpp>
#include <boost/variant.hpp>
#include <boost/math/special_functions/round.hpp>
#include <boost/multi_array.hpp>
#ifndef M_PI
# define M_PI 3.14159265358979323846

View File

@ -373,11 +373,21 @@ void Graphics::loadFonts()
CDefEssential * Graphics::getDef( const CGObjectInstance * obj )
{
if (obj->appearance.animationFile.empty())
{
logGlobal->warnStream() << boost::format("Def name for obj %d (%d,%d) is empty!") % obj->id % obj->ID % obj->subID;
return nullptr;
}
return advmapobjGraphics[obj->appearance.animationFile];
}
CDefEssential * Graphics::getDef( const ObjectTemplate & info )
{
if (info.animationFile.empty())
{
logGlobal->warnStream() << boost::format("Def name for obj (%d,%d) is empty!") % info.id % info.subid;
return nullptr;
}
return advmapobjGraphics[info.animationFile];
}

View File

@ -860,8 +860,15 @@ void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const Terra
if (!graphics->getDef(obj))
processDef(obj->appearance);
if (!graphics->getDef(obj) && !obj->appearance.animationFile.empty())
logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
if (!graphics->getDef(obj))
{
if (!obj->appearance.animationFile.empty())
logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
else
logGlobal->warnStream() << boost::format("Def name for obj %d (%d,%d) is empty!") % obj->id % obj->ID % obj->subID;
continue;
}
if (!canDrawObject(obj))
continue;
@ -1322,22 +1329,54 @@ bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = fals
curt.objects.insert(i, toAdd);
}
} // for(int fy=0; fy<tilesH; ++fy)
} //for(int fx=0; fx<tilesW; ++fx)
}
}
return true;
}
bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
{
// do we actually need to search through the whole map for this?
for (size_t i=0; i<map->width; i++)
//optimized version which reveals weird bugs with missing def name
//auto pos = obj->pos;
//for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
//{
// for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
// {
// int3 t(i, j, pos.z);
// if (!map->isInTheMap(t))
// continue;
// auto &objs = ttiles[i][j][pos.z].objects;
// for (size_t x = 0; x < objs.size(); x++)
// {
// auto ourObj = objs[x].obj;
// if (ourObj && ourObj->id == obj->id)
// {
// if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
// {
// if (startObjectFade(objs[x], false, t))
// objs[x].obj = nullptr; //set original pointer to null
// else
// objs.erase(objs.begin() + x);
// }
// else
// objs.erase(objs.begin() + x);
// break;
// }
// }
// }
//}
for (size_t i = 0; i<map->width; i++)
{
for (size_t j=0; j<map->height; j++)
for (size_t j = 0; j<map->height; j++)
{
for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
{
auto &objs = ttiles[i][j][k].objects;
for(size_t x=0; x < objs.size(); x++)
for (size_t x = 0; x < objs.size(); x++)
{
if (objs[x].obj && objs[x].obj->id == obj->id)
{
@ -1356,6 +1395,7 @@ bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = fals
}
}
}
return true;
}
bool CMapHandler::removeObject(CGObjectInstance *obj, bool fadeout /* = false */)

View File

@ -1058,10 +1058,9 @@
{
"bonuses" : [
{
"subtype" : 1,
"type" : "FLYING_MOVEMENT",
"val" : 0,
"valueType" : "BASE_NUMBER"
"valueType" : "INDEPENDENT_MIN"
}
],
"index" : 72,
@ -1276,10 +1275,9 @@
{
"bonuses" : [
{
"subtype" : 1,
"type" : "WATER_WALKING",
"val" : 0,
"valueType" : "BASE_NUMBER"
"valueType" : "INDEPENDENT_MIN"
}
],
"index" : 90,

View File

@ -176,23 +176,23 @@
"effects" : {
"fly" : {
"type" : "FLYING_MOVEMENT",
"subtype" : 2,
"duration" : "ONE_DAY",
"val" : 0 //in fact unused
"val" : 40,
"valueType" : "INDEPENDENT_MIN"
}
}
},
"advanced":{
"effects" : {
"fly" : {
"subtype" : 1
"val" : 20
}
}
},
"expert":{
"effects" : {
"fly" : {
"subtype" : 1
"val" : 0
}
}
}
@ -214,23 +214,23 @@
"effects" : {
"waterWalk" : {
"type" : "WATER_WALKING",
"subtype" : 2,
"duration" : "ONE_DAY",
"val" : 0 //in fact unused
"val" : 40,
"valueType" : "INDEPENDENT_MIN"
}
}
},
"advanced":{
"effects" : {
"waterWalk" : {
"subtype" : 1
"val" : 20
}
}
},
"expert":{
"effects" : {
"waterWalk" : {
"subtype" : 1
"val" : 0
}
}
}

View File

@ -61,17 +61,26 @@ bool CGameInfoCallback::isAllowed( int type, int id )
const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose) const
{
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!hasAccess(color), verbose, "Cannot access player " << color << "info!", nullptr);
//if (!vstd::contains(gs->players, color))
//{
// logGlobal->errorStream() << "Cannot access player " << color << "info!";
// return nullptr; //macros are not really useful when debugging :?
//}
//else
//{
ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!vstd::contains(gs->players,color), verbose, "Cannot find player " << color << "info!", nullptr);
return &gs->players[color];
//}
//funtion written from scratch since it's accessed A LOT by AI
auto player = gs->players.find(color);
if (player != gs->players.end())
{
if (hasAccess(color))
return &player->second;
else
{
if (verbose)
logGlobal->errorStream() << boost::format("Cannot access player %d info!") % color;
return nullptr;
}
}
else
{
if (verbose)
logGlobal->errorStream() << boost::format("Cannot find player %d info!") % color;
return nullptr;
}
}
const CTown * CGameInfoCallback::getNativeTown(PlayerColor color) const
@ -455,6 +464,31 @@ const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
return &gs->map->getTile(tile);
}
//TODO: typedef?
shared_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const
{
assert(player.is_initialized());
auto team = getPlayerTeam(player.get());
size_t width = gs->map->width;
size_t height = gs->map->height;
size_t levels = (gs->map->twoLevel ? 2 : 1);
boost::multi_array<TerrainTile*, 3> tileArray(boost::extents[width][height][levels]);
for (size_t x = 0; x < width; x++)
for (size_t y = 0; y < height; y++)
for (size_t z = 0; z < levels; z++)
{
if (team->fogOfWarMap[x][y][z])
tileArray[x][y][z] = &gs->map->getTile(int3(x, y, z));
else
tileArray[x][y][z] = nullptr;
}
return make_shared<boost::multi_array<TerrainTile*, 3>>(tileArray);
}
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
{
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
@ -747,18 +781,43 @@ TResources CPlayerSpecificInfoCallback::getResourceAmount() const
const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
{
ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << teamID, nullptr);
const TeamState *ret = &gs->teams[teamID];
ERROR_RET_VAL_IF(!!player && !vstd::contains(ret->players, *player), "Illegal attempt to access team data!", nullptr);
return ret;
//rewritten by hand, AI calls this function a lot
auto team = gs->teams.find(teamID);
if (team != gs->teams.end())
{
const TeamState *ret = &team->second;
if (!player.is_initialized()) //neutral (or invalid) player
return ret;
else
{
if (vstd::contains(ret->players, *player)) //specific player
return ret;
else
{
logGlobal->errorStream() << boost::format("Illegal attempt to access team data!");
return nullptr;
}
}
}
else
{
logGlobal->errorStream() << boost::format("Cannot find info for team %d") % teamID;
return nullptr;
}
}
const TeamState * CGameInfoCallback::getPlayerTeam( PlayerColor color ) const
{
const PlayerState * ps = getPlayer(color);
if (ps)
return getTeam(ps->team);
return nullptr;
auto player = gs->players.find(color);
if (player != gs->players.end())
{
return getTeam (player->second.team);
}
else
{
return nullptr;
}
}
const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const

View File

@ -91,6 +91,7 @@ public:
const CMapHeader * getMapHeader()const;
int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
const TerrainTile * getTile(int3 tile, bool verbose = true) const;
shared_ptr<boost::multi_array<TerrainTile*, 3>> getAllVisibleTiles() const;
bool isInTheMap(const int3 &pos) const;
//town

View File

@ -27,6 +27,7 @@
#include "rmg/CMapGenerator.h"
#include "CStopWatch.h"
#include "mapping/CMapEditManager.h"
#include "CPathfinder.h"
class CGObjectInstance;
@ -2099,7 +2100,7 @@ void CGameState::getNeighbours(const TerrainTile &srct, int3 tile, std::vector<i
}
}
int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, bool flying, int remainingMovePoints, bool checkLast)
int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints, bool checkLast)
{
if(src == dest) //same tile
return 0;
@ -2110,21 +2111,18 @@ int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const
//get basic cost
int ret = h->getTileCost(d,s);
if(d.blocked && flying)
if(d.blocked && h->canFly())
{
bool freeFlying = h->getBonusesCount(Selector::typeSubtype(Bonus::FLYING_MOVEMENT, 1)) > 0;
if(!freeFlying)
{
ret *= 1.4; //40% penalty for movement over blocked tile
}
ret *= (100.0 + h->valOfBonuses(Bonus::FLYING_MOVEMENT)) / 100.0;
}
else if (d.terType == ETerrainType::WATER)
else if(d.terType == ETerrainType::WATER)
{
if(h->boat && s.hasFavourableWinds() && d.hasFavourableWinds()) //Favourable Winds
ret *= 0.666;
else if (!h->boat && h->getBonusesCount(Selector::typeSubtype(Bonus::WATER_WALKING, 1)) > 0)
ret *= 1.4; //40% penalty for water walking
else if(!h->boat && h->canWalkOnSea())
{
ret *= (100.0 + h->valOfBonuses(Bonus::WATER_WALKING)) / 100.0;
}
}
if(src.x != dest.x && src.y != dest.y) //it's diagonal move
@ -2144,10 +2142,10 @@ int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const
{
std::vector<int3> vec;
vec.reserve(8); //optimization
getNeighbours(d, dest, vec, s.terType != ETerrainType::WATER, true);
getNeighbours(d, dest, vec, s.terType != ETerrainType::WATER, true);
for(auto & elem : vec)
{
int fcost = getMovementCost(h,dest, elem, flying, left, false);
int fcost = getMovementCost(h, dest, elem, left, false);
if(fcost <= left)
{
return ret;
@ -2884,109 +2882,6 @@ CGHeroInstance * CGameState::getUsedHero(HeroTypeID hid) const
return nullptr;
}
CGPathNode::CGPathNode()
:coord(-1,-1,-1)
{
accessible = NOT_SET;
land = 0;
moveRemains = 0;
turns = 255;
theNodeBefore = nullptr;
}
bool CGPathNode::reachable() const
{
return turns < 255;
}
const CGPathNode * CPathsInfo::getPathInfo( const int3& tile ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
if (tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z ||
tile.x < 0 || tile.y < 0 || tile.z < 0)
return nullptr;
return &nodes[tile.x][tile.y][tile.z];
}
int CPathsInfo::getDistance( int3 tile ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
CGPath ret;
if (getPath(tile, ret))
return ret.nodes.size();
else
return 255;
}
bool CPathsInfo::getPath( const int3 &dst, CGPath &out ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
out.nodes.clear();
const CGPathNode *curnode = &nodes[dst.x][dst.y][dst.z];
if(!curnode->theNodeBefore)
return false;
while(curnode)
{
CGPathNode cpn = *curnode;
curnode = curnode->theNodeBefore;
out.nodes.push_back(cpn);
}
return 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];
}
}
}
CPathsInfo::~CPathsInfo()
{
for(int i = 0; i < sizes.x; i++)
{
for (int j = 0; j < sizes.y; j++)
{
delete [] nodes[i][j];
}
delete [] nodes[i];
}
delete [] nodes;
}
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);
}
}
}
PlayerState::PlayerState()
: color(-1), enteredWinningCheatCode(0),
enteredLosingCheatCode(0), status(EPlayerStatus::INGAME)
@ -3274,341 +3169,8 @@ TeamState::TeamState()
setNodeType(TEAM);
}
void CPathfinder::initializeGraph()
{
CGPathNode ***graph = out.nodes;
for(size_t i=0; i < out.sizes.x; ++i)
{
for(size_t j=0; j < out.sizes.y; ++j)
{
for(size_t k=0; k < out.sizes.z; ++k)
{
curPos = int3(i,j,k);
const TerrainTile *tinfo = &gs->map->getTile(int3(i, j, k));
CGPathNode &node = graph[i][j][k];
node.accessible = evaluateAccessibility(tinfo);
node.turns = 0xff;
node.moveRemains = 0;
node.coord.x = i;
node.coord.y = j;
node.coord.z = k;
node.land = tinfo->terType != ETerrainType::WATER;
node.theNodeBefore = nullptr;
}
}
}
}
void CPathfinder::calculatePaths()
{
bool flying = hero->hasBonusOfType(Bonus::FLYING_MOVEMENT);
int maxMovePointsLand = hero->maxMovePoints(true);
int maxMovePointsWater = hero->maxMovePoints(false);
int3 src = hero->getPosition(false);
auto maxMovePoints = [&](CGPathNode *cp) -> int
{
return cp->land ? maxMovePointsLand : maxMovePointsWater;
};
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...";
return;
}
//logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
initializeGraph();
//initial tile - set cost on 0 and add to the queue
CGPathNode &initialNode = *getNode(out.hpos);
initialNode.turns = 0;
initialNode.moveRemains = hero->movement;
mq.push_back(&initialNode);
std::vector<int3> neighbours;
neighbours.reserve(16);
while(!mq.empty())
{
cp = mq.front();
mq.pop_front();
const int3 sourceGuardPosition = gs->map->guardingCreaturePositions[cp->coord.x][cp->coord.y][cp->coord.z];
bool guardedSource = (sourceGuardPosition != int3(-1, -1, -1) && cp->coord != src);
ct = &gs->map->getTile(cp->coord);
int movement = cp->moveRemains, turn = cp->turns;
if(!movement)
{
movement = maxMovePoints(cp);
turn++;
}
//add accessible neighbouring nodes to the queue
neighbours.clear();
auto isAllowedTeleportEntrance = [&](const CGTeleport * obj) -> bool
{
if(!gs->isTeleportEntrancePassable(obj, hero->tempOwner))
return false;
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;
};
auto sObj = ct->topVisitableObj(cp->coord == CGHeroInstance::convertPosition(hero->pos, false));
auto cObj = dynamic_cast<const CGTeleport *>(sObj);
if(isAllowedTeleportEntrance(cObj))
{
for(auto objId : gs->getTeleportChannelExits(cObj->channel, hero->tempOwner))
{
auto obj = getObj(objId);
if(CGTeleport::isExitPassable(gs, hero, obj))
neighbours.push_back(obj->visitablePos());
}
}
std::vector<int3> neighbour_tiles;
gs->getNeighbours(*ct, cp->coord, neighbour_tiles, boost::logic::indeterminate, !cp->land);
if(sObj)
{
for(int3 neighbour_tile: neighbour_tiles)
{
if(canMoveBetween(neighbour_tile, sObj->visitablePos()))
neighbours.push_back(neighbour_tile);
}
}
else
vstd::concatenate(neighbours, neighbour_tiles);
for(auto & neighbour : neighbours)
{
const int3 &n = neighbour; //current neighbor
dp = getNode(n);
dt = &gs->map->getTile(n);
destTopVisObjID = dt->topVisitableId();
useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark
const bool destIsGuardian = sourceGuardPosition == n;
auto dObj = dynamic_cast<const CGTeleport*>(dt->topVisitableObj());
if(!goodForLandSeaTransition()
|| (!canMoveBetween(cp->coord, dp->coord) && !CGTeleport::isConnected(cObj, dObj))
|| dp->accessible == CGPathNode::BLOCKED)
{
continue;
}
//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 && guardedSource && cp->theNodeBefore->land && ct->topVisitableId() == Obj::BOAT)
guardedSource = false;
int cost = gs->getMovementCost(hero, cp->coord, dp->coord, flying, movement);
//special case -> moving from src Subterranean gate to dest gate -> it's free
if(CGTeleport::isConnected(cObj, dObj))
cost = 0;
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, flying, moveAtNextTile); //cost must be updated, movement points changed :(
remains = moveAtNextTile - cost;
}
if((dp->turns==0xff //we haven't been here before
|| dp->turns > turnAtNextTile
|| (dp->turns >= turnAtNextTile && dp->moveRemains < remains)) //this route is faster
&& (!guardedSource || destIsGuardian)) // Can step into tile of guard
{
assert(dp != cp->theNodeBefore); //two tiles can't point to each other
dp->moveRemains = remains;
dp->turns = turnAtNextTile;
dp->theNodeBefore = cp;
const bool guardedDst = gs->map->guardingCreaturePositions[dp->coord.x][dp->coord.y][dp->coord.z].valid()
&& dp->accessible == CGPathNode::BLOCKVIS;
auto checkDestinationTile = [&]() -> bool
{
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 walways allos transit for teleports
if(useEmbarkCost && allowEmbarkAndDisembark)
return true;
if(gs->isTeleportEntrancePassable(dObj, hero->tempOwner))
return true; // Always add entry teleport with non-dummy channel
if(CGTeleport::isConnected(cObj, dObj))
return true; // Always add exit points of teleport
if(guardedDst && !guardedSource)
return true; // Can step into a hostile tile once
return false;
};
if(checkDestinationTile())
mq.push_back(dp);
}
} //neighbours loop
} //queue loop
}
CGPathNode *CPathfinder::getNode(const int3 &coord)
{
return &out.nodes[coord.x][coord.y][coord.z];
}
bool CPathfinder::canMoveBetween(const int3 &a, const int3 &b) const
{
return gs->checkForVisitableDir(a, b);
}
CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const TerrainTile *tinfo) const
{
CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE);
if(tinfo->terType == ETerrainType::ROCK || !FoW[curPos.x][curPos.y][curPos.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[curPos.x][curPos.y][curPos.z].valid()
&& !tinfo->blocked)
{
// Monster close by; blocked visit for battle.
return CGPathNode::BLOCKVIS;
}
return ret;
}
bool CPathfinder::goodForLandSeaTransition()
{
if(cp->land != dp->land) //hero can traverse land<->sea only in special circumstances
{
if(cp->land) //from land to sea -> embark or assault hero on boat
{
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;
}
else //disembark
{
//can disembark only on coastal tiles
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;
}
}
return true;
}
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));
allowEmbarkAndDisembark = true;
allowTeleportTwoWay = true;
allowTeleportOneWay = true;
allowTeleportOneWayRandom = false;
allowTeleportWhirlpool = false;
if (CGWhirlpool::isProtected(hero))
allowTeleportWhirlpool = true;
}
CRandomGenerator & CGameState::getRandomGenerator()
{
//logGlobal->traceStream() << "Fetching CGameState::rand with seed " << rand.nextInt();
return rand;
}
bool CPathfinder::addTeleportTwoWay(const CGTeleport * obj) const
{
return allowTeleportTwoWay && gs->isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
}
bool CPathfinder::addTeleportOneWay(const CGTeleport * obj) const
{
if(allowTeleportOneWay && 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(allowTeleportOneWayRandom && 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 allowTeleportWhirlpool && obj;
}

View File

@ -16,6 +16,7 @@
#include "int3.h"
#include "CRandomGenerator.h"
#include "CGameStateFwd.h"
#include "CPathfinder.h"
/*
* CGameState.h, part of VCMI engine
@ -44,7 +45,6 @@ class CMap;
struct StartInfo;
struct SDL_Surface;
class CMapHandler;
class CPathfinder;
struct SetObjectProperty;
struct MetaString;
struct CPack;
@ -276,46 +276,6 @@ struct DLL_EXPORT DuelParameters
}
};
class CPathfinder : private CGameInfoCallback
{
private:
bool allowEmbarkAndDisembark;
bool allowTeleportTwoWay; // Two-way monoliths and Subterranean Gate
bool allowTeleportOneWay; // One-way monoliths with one known exit only
bool allowTeleportOneWayRandom; // One-way monoliths with more than one known exit
bool allowTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
CPathsInfo &out;
const CGHeroInstance *hero;
const std::vector<std::vector<std::vector<ui8> > > &FoW;
std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
int3 curPos;
CGPathNode *cp; //current (source) path node -> we took it from the queue
CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
const TerrainTile *ct, *dt; //tile info for both nodes
ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
Obj destTopVisObjID;
CGPathNode *getNode(const int3 &coord);
void initializeGraph();
bool goodForLandSeaTransition(); //checks if current move will be between sea<->land. If so, checks it legality (returns false if movement is not possible) and sets useEmbarkCost
CGPathNode::EAccessibility evaluateAccessibility(const TerrainTile *tinfo) const;
bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
bool addTeleportTwoWay(const CGTeleport * obj) const;
bool addTeleportOneWay(const CGTeleport * obj) const;
bool addTeleportOneWayRandom(const CGTeleport * obj) const;
bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
public:
CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
};
struct BattleInfo;
@ -380,7 +340,7 @@ public:
bool isVisible(const CGObjectInstance *obj, boost::optional<PlayerColor> player);
void getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing);
int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, bool flying, int remainingMovePoints=-1, bool checkLast=true);
int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints=-1, bool checkLast=true);
int getDate(Date::EDateType mode=Date::DAY) const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
// ----- getters, setters -----

View File

@ -117,50 +117,3 @@ struct DLL_LINKAGE QuestInfo //universal interface for human and AI
h & quest & obj & tile;
}
};
struct DLL_LINKAGE CGPathNode
{
enum EAccessibility
{
NOT_SET = 0,
ACCESSIBLE = 1, //tile can be entered and passed
VISITABLE, //tile can be entered as the last tile in path
BLOCKVIS, //visitable from neighbouring tile but not passable
BLOCKED //tile can't be entered nor visited
};
EAccessibility accessible;
ui8 land;
ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
ui32 moveRemains; //remaining tiles after hero reaches the tile
CGPathNode * theNodeBefore;
int3 coord; //coordinates
CGPathNode();
bool reachable() const;
};
struct DLL_LINKAGE CGPath
{
std::vector<CGPathNode> nodes; //just get node by node
int3 startPos() const; // start point
int3 endPos() const; //destination point
void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
};
struct DLL_LINKAGE CPathsInfo
{
mutable boost::mutex pathMx;
const CGHeroInstance *hero;
int3 hpos;
int3 sizes;
CGPathNode ***nodes; //[w][h][level]
const CGPathNode * getPathInfo( const int3& tile ) const;
bool getPath(const int3 &dst, CGPath &out) const;
int getDistance( int3 tile ) const;
CPathsInfo(const int3 &Sizes);
~CPathsInfo();
};

View File

@ -95,6 +95,7 @@ set(lib_SRCS
IGameCallback.cpp
CGameInfoCallback.cpp
CPathfinder.cpp
CGameState.cpp
Connection.cpp
NetPacksLib.cpp

519
lib/CPathfinder.cpp Normal file
View File

@ -0,0 +1,519 @@
#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;
}
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");
}
initializeGraph();
if(hero->canFly())
options.useFlying = true;
if(hero->canWalkOnSea())
options.useWaterWalking = true;
if(CGWhirlpool::isProtected(hero))
options.useTeleportWhirlpool = true;
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 = *getNode(out.hpos);
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)
{
dp = getNode(neighbour);
dt = &gs->map->getTile(neighbour);
useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark
if(!isMovementPossible())
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(checkDestinationTile())
mq.push_back(dp);
}
} //neighbours loop
//just add all passable teleport exits
if(sTileObj)
{
addTeleportExits();
for(auto & neighbour : neighbours)
{
dp = getNode(neighbour);
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(sTileObj)
{
for(int3 tile: tiles)
{
if(canMoveBetween(tile, sTileObj->visitablePos()))
neighbours.push_back(tile);
}
}
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::isMovementPossible()
{
if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
return false;
Obj destTopVisObjID = dt->topVisitableId();
if(cp->land != dp->land) //hero can traverse land<->sea only in special circumstances
{
if(cp->land) //from land to sea -> embark or assault hero on boat
{
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;
}
else //disembark
{
//can disembark only on coastal tiles
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;
}
}
if(isSourceGuarded() && !isDestinationGuardian()) // Can step into tile of guard
return false;
return true;
}
bool CPathfinder::checkDestinationTile()
{
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
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()
{
int3 pos;
CGPathNode ***graph = out.nodes;
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);
CGPathNode &node = graph[pos.x][pos.y][pos.z];
node.accessible = evaluateAccessibility(pos, tinfo);
node.turns = 0xff;
node.moveRemains = 0;
node.coord = pos;
node.land = tinfo->terType != ETerrainType::WATER;
node.theNodeBefore = nullptr;
}
}
}
}
CGPathNode *CPathfinder::getNode(const int3 &coord)
{
return &out.nodes[coord.x][coord.y][coord.z];
}
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;
}
CGPathNode::CGPathNode()
:coord(-1,-1,-1)
{
accessible = NOT_SET;
land = 0;
moveRemains = 0;
turns = 255;
theNodeBefore = nullptr;
}
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];
}
}
}
CPathsInfo::~CPathsInfo()
{
for(int i = 0; i < sizes.x; i++)
{
for(int j = 0; j < sizes.y; j++)
{
delete [] nodes[i][j];
}
delete [] nodes[i];
}
delete [] nodes;
}
const CGPathNode * CPathsInfo::getPathInfo( int3 tile ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
if(tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z)
return nullptr;
return &nodes[tile.x][tile.y][tile.z];
}
bool CPathsInfo::getPath( const int3 &dst, CGPath &out ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
out.nodes.clear();
const CGPathNode *curnode = &nodes[dst.x][dst.y][dst.z];
if(!curnode->theNodeBefore)
return false;
while(curnode)
{
CGPathNode cpn = *curnode;
curnode = curnode->theNodeBefore;
out.nodes.push_back(cpn);
}
return true;
}
int CPathsInfo::getDistance( int3 tile ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
CGPath ret;
if(getPath(tile, ret))
return ret.nodes.size();
else
return 255;
}

124
lib/CPathfinder.h Normal file
View File

@ -0,0 +1,124 @@
#pragma once
#include "VCMI_Lib.h"
#include "mapping/CMap.h"
#include "IGameCallback.h"
#include "int3.h"
/*
* CPathfinder.h, 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
*
*/
class CGHeroInstance;
class CGObjectInstance;
struct TerrainTile;
struct DLL_LINKAGE CGPathNode
{
enum EAccessibility
{
NOT_SET = 0,
ACCESSIBLE = 1, //tile can be entered and passed
VISITABLE, //tile can be entered as the last tile in path
BLOCKVIS, //visitable from neighbouring tile but not passable
BLOCKED //tile can't be entered nor visited
};
EAccessibility accessible;
ui8 land;
ui8 turns; //how many turns we have to wait before reachng the tile - 0 means current turn
ui32 moveRemains; //remaining tiles after hero reaches the tile
CGPathNode * theNodeBefore;
int3 coord; //coordinates
CGPathNode();
bool reachable() const;
};
struct DLL_LINKAGE CGPath
{
std::vector<CGPathNode> nodes; //just get node by node
int3 startPos() const; // start point
int3 endPos() const; //destination point
void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
};
struct DLL_LINKAGE CPathsInfo
{
mutable boost::mutex pathMx;
const CGHeroInstance *hero;
int3 hpos;
int3 sizes;
CGPathNode ***nodes; //[w][h][level]
CPathsInfo(const int3 &Sizes);
~CPathsInfo();
const CGPathNode * getPathInfo( int3 tile ) const;
bool getPath(const int3 &dst, CGPath &out) const;
int getDistance( int3 tile ) const;
};
class CPathfinder : private CGameInfoCallback
{
public:
CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
private:
struct PathfinderOptions
{
bool useFlying;
bool useWaterWalking;
bool useEmbarkAndDisembark;
bool useTeleportTwoWay; // Two-way monoliths and Subterranean Gate
bool useTeleportOneWay; // One-way monoliths with one known exit only
bool useTeleportOneWayRandom; // One-way monoliths with more than one known exit
bool useTeleportWhirlpool; // Force enabled if hero protected or unaffected (have one stack of one creature)
PathfinderOptions();
} options;
CPathsInfo &out;
const CGHeroInstance *hero;
const std::vector<std::vector<std::vector<ui8> > > &FoW;
std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
std::vector<int3> neighbours;
CGPathNode *cp; //current (source) path node -> we took it from the queue
CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
const TerrainTile *ct, *dt; //tile info for both nodes
const CGObjectInstance *sTileObj;
ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
void addNeighbours(const int3 &coord);
void addTeleportExits(bool noTeleportExcludes = false);
bool isMovementPossible(); //checks if current move will be between sea<->land. If so, checks it legality (returns false if movement is not possible) and sets useEmbarkCost
bool checkDestinationTile();
int3 getSourceGuardPosition();
bool isSourceGuarded();
bool isDestinationGuarded();
bool isDestinationGuardian();
void initializeGraph();
CGPathNode *getNode(const int3 &coord);
CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
bool addTeleportTwoWay(const CGTeleport * obj) const;
bool addTeleportOneWay(const CGTeleport * obj) const;
bool addTeleportOneWayRandom(const CGTeleport * obj) const;
bool addTeleportWhirlpool(const CGWhirlpool * obj) const;
};

View File

@ -86,13 +86,13 @@ public:
BONUS_NAME(SECONDARY_SKILL_PREMY) /*%*/ \
BONUS_NAME(SURRENDER_DISCOUNT) /*%*/ \
BONUS_NAME(STACKS_SPEED) /*additional info - percent of speed bonus applied after direct bonuses; >0 - added, <0 - subtracted to this part*/ \
BONUS_NAME(FLYING_MOVEMENT) /*subtype 1 - without penalty, 2 - with penalty*/ \
BONUS_NAME(FLYING_MOVEMENT) /*value - penalty percentage*/ \
BONUS_NAME(SPELL_DURATION) \
BONUS_NAME(AIR_SPELL_DMG_PREMY) \
BONUS_NAME(EARTH_SPELL_DMG_PREMY) \
BONUS_NAME(FIRE_SPELL_DMG_PREMY) \
BONUS_NAME(WATER_SPELL_DMG_PREMY) \
BONUS_NAME(WATER_WALKING) /*subtype 1 - without penalty, 2 - with penalty*/ \
BONUS_NAME(WATER_WALKING) /*value - penalty percentage*/ \
BONUS_NAME(NEGATE_ALL_NATURAL_IMMUNITIES) \
BONUS_NAME(STACK_HEALTH) \
BONUS_NAME(BLOCK_MORALE) \

View File

@ -158,6 +158,8 @@
<Unit filename="CModHandler.h" />
<Unit filename="CObstacleInstance.cpp" />
<Unit filename="CObstacleInstance.h" />
<Unit filename="CPathfinder.cpp" />
<Unit filename="CPathfinder.h" />
<Unit filename="CRandomGenerator.cpp" />
<Unit filename="CRandomGenerator.h" />
<Unit filename="CScriptingModule.h" />

View File

@ -184,6 +184,7 @@
<ClCompile Include="CModHandler.cpp" />
<ClCompile Include="CObstacleInstance.cpp" />
<ClCompile Include="Connection.cpp" />
<ClCompile Include="CPathfinder.cpp" />
<ClCompile Include="CThreadHelper.cpp" />
<ClCompile Include="CTownHandler.cpp" />
<ClCompile Include="CRandomGenerator.cpp" />
@ -291,6 +292,7 @@
<ClInclude Include="CondSh.h" />
<ClInclude Include="Connection.h" />
<ClInclude Include="ConstTransitivePtr.h" />
<ClInclude Include="CPathfinder.h" />
<ClInclude Include="CRandomGenerator.h" />
<ClInclude Include="CScriptingModule.h" />
<ClInclude Include="CStopWatch.h" />

View File

@ -226,6 +226,7 @@
<Filter>spells</Filter>
</ClCompile>
<ClCompile Include="mapping\CDrawRoadsOperation.cpp" />
<ClCompile Include="CPathfinder.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CCreatureSet.h">
@ -537,5 +538,8 @@
<ClInclude Include="spells\Magic.h">
<Filter>spells</Filter>
</ClInclude>
<ClInclude Include="CPathfinder.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -129,9 +129,14 @@ int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position o
}
}
bool CGHeroInstance::canFly() const
{
return hasBonusOfType(Bonus::FLYING_MOVEMENT);
}
bool CGHeroInstance::canWalkOnSea() const
{
return hasBonusOfType(Bonus::FLYING_MOVEMENT) || hasBonusOfType(Bonus::WATER_WALKING);
return hasBonusOfType(Bonus::WATER_WALKING);
}
ui8 CGHeroInstance::getSecSkillLevel(SecondarySkill skill) const
@ -860,7 +865,7 @@ ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell, int *outSelectedSc
spell->forEachSchool([&, this](const SpellSchoolInfo & cnf, bool & stop)
{
int thisSchool = std::max<int>(getSecSkillLevel(cnf.skill), valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1 << ((ui8)cnf.id)));
int thisSchool = std::max<int>(getSecSkillLevel(cnf.skill), valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1 << ((ui8)cnf.id))); //FIXME: Bonus shouldn't be additive (Witchking Artifacts : Crown of Skies)
if(thisSchool > skill)
{
skill = thisSchool;
@ -872,7 +877,8 @@ ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell, int *outSelectedSc
vstd::amax(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
vstd::amax(skill, valOfBonuses(Bonus::SPELL, spell->id.toEnum())); //given by artifact or other effect
assert(skill >= 0 && skill <= 3);
vstd::amax(skill, 0); //in case we don't know any school
vstd::amin(skill, 3);
return skill;
}

View File

@ -133,6 +133,7 @@ public:
ui32 getLowestCreatureSpeed() const;
int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
bool canFly() const;
bool canWalkOnSea() const;
int getCurrentLuck(int stack=-1, bool town=false) const;
int getSpellCost(const CSpell *sp) const; //do not use during battles -> bonuses from army would be ignored

View File

@ -1511,20 +1511,20 @@ void CGameHandler::newTurn()
}
//count days without town for all players, regardless of their turn order
for (auto p = gs->players.begin(); p != gs->players.end(); p++)
for (auto &p : gs->players)
{
PlayerState * playerState = &p->second;
if (playerState->status == EPlayerStatus::INGAME)
PlayerState & playerState = p.second;
if (playerState.status == EPlayerStatus::INGAME)
{
if (playerState->towns.empty())
if (playerState.towns.empty())
{
if (playerState->daysWithoutCastle)
++(*playerState->daysWithoutCastle);
else playerState->daysWithoutCastle = 0;
if (playerState.daysWithoutCastle)
++(*playerState.daysWithoutCastle);
else playerState.daysWithoutCastle = 0;
}
else
{
playerState->daysWithoutCastle = boost::none;
playerState.daysWithoutCastle = boost::none;
}
}
}
@ -1764,7 +1764,7 @@ bool CGameHandler::moveHero( ObjectInstanceID hid, int3 dst, ui8 teleporting, bo
}
const TerrainTile t = *gs->getTile(hmpos);
const int cost = gs->getMovementCost(h, h->getPosition(), hmpos, h->hasBonusOfType(Bonus::FLYING_MOVEMENT), h->movement);
const int cost = gs->getMovementCost(h, h->getPosition(), hmpos, h->movement);
const int3 guardPos = gs->guardingCreaturePosition(hmpos);
const bool embarking = !h->boat && !t.visitableObjects.empty() && t.visitableObjects.back()->ID == Obj::BOAT;
@ -4480,7 +4480,7 @@ void CGameHandler::handleTownEvents(CGTownInstance * town, NewTurn &n)
for(auto & i : ev.buildings)
{
if ( town->hasBuilt(i))
if(!town->hasBuilt(i))
{
buildStructure(town->id, i, true);
iw.components.push_back(Component(Component::BUILDING, town->subID, i, 0));
@ -4825,10 +4825,10 @@ void CGameHandler::checkVictoryLossConditionsForPlayer(PlayerColor player)
std::set<PlayerColor> playerColors;
//do not copy player state (CBonusSystemNode) by value
for (auto p = gs->players.begin(); p != gs->players.end(); p++) //players may have different colors, iterate over players and not integers
for (auto &p : gs->players) //players may have different colors, iterate over players and not integers
{
if (p->first != player)
playerColors.insert(p->first);
if (p.first != player)
playerColors.insert(p.first);
}
//notify all players