From 49cbd5adc97d1da7271334d156d8f76c4df59a73 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 7 Dec 2022 22:34:08 +0200 Subject: [PATCH] CGHeroInstance::convertPosition is no longer static method --- AI/Nullkiller/AIGateway.cpp | 14 +++++++------- AI/VCAI/VCAI.cpp | 14 +++++++------- client/CPlayerInterface.cpp | 12 +++++++----- lib/CPathfinder.cpp | 11 ----------- lib/CPathfinder.h | 1 - lib/NetPacksLib.cpp | 2 +- lib/mapObjects/CGHeroInstance.cpp | 12 +++--------- lib/mapObjects/CGHeroInstance.h | 2 +- lib/mapObjects/MiscObjects.cpp | 10 +++++----- lib/mapping/MapFormatH3M.cpp | 2 +- server/CGameHandler.cpp | 2 +- server/CQuery.cpp | 2 +- 12 files changed, 34 insertions(+), 50 deletions(-) diff --git a/AI/Nullkiller/AIGateway.cpp b/AI/Nullkiller/AIGateway.cpp index 352b46e05..9e21337bb 100644 --- a/AI/Nullkiller/AIGateway.cpp +++ b/AI/Nullkiller/AIGateway.cpp @@ -92,8 +92,8 @@ void AIGateway::heroMoved(const TryMoveHero & details, bool verbose) validateObject(details.id); //enemy hero may have left visible area auto hero = cb->getHero(details.id); - const int3 from = CGHeroInstance::convertPosition(details.start, false); - const int3 to = CGHeroInstance::convertPosition(details.end, false); + const int3 from = hero->convertPosition(details.start, false); + const int3 to = hero->convertPosition(details.end, false); const CGObjectInstance * o1 = vstd::frontOrNull(cb->getVisitableObjs(from, verbose)); const CGObjectInstance * o2 = vstd::frontOrNull(cb->getVisitableObjs(to, verbose)); @@ -1178,7 +1178,7 @@ bool AIGateway::moveHeroToTile(int3 dst, HeroPtr h) { //FIXME: this assertion fails also if AI moves onto defeated guarded object assert(cb->getVisitableObjs(dst).size() > 1); //there's no point in revisiting tile where there is no visitable object - cb->moveHero(*h, CGHeroInstance::convertPosition(dst, true)); + cb->moveHero(*h, h->convertPosition(dst, true)); afterMovementCheck(); // TODO: is it feasible to hero get killed there if game work properly? // If revisiting, teleport probing is never done, and so the entries into the list would remain unused and uncleared teleportChannelProbingList.clear(); @@ -1232,14 +1232,14 @@ bool AIGateway::moveHeroToTile(int3 dst, HeroPtr h) auto doMovement = [&](int3 dst, bool transit) { - cb->moveHero(*h, CGHeroInstance::convertPosition(dst, true), transit); + cb->moveHero(*h, h->convertPosition(dst, true), transit); }; auto doTeleportMovement = [&](ObjectInstanceID exitId, int3 exitPos) { destinationTeleport = exitId; if(exitPos.valid()) - destinationTeleportPos = CGHeroInstance::convertPosition(exitPos, true); + destinationTeleportPos = h->convertPosition(exitPos, true); cb->moveHero(*h, h->pos); destinationTeleport = ObjectInstanceID(); destinationTeleportPos = int3(-1); @@ -1248,7 +1248,7 @@ bool AIGateway::moveHeroToTile(int3 dst, HeroPtr h) auto doChannelProbing = [&]() -> void { - auto currentPos = CGHeroInstance::convertPosition(h->pos, false); + auto currentPos = h->convertPosition(h->pos, false); auto currentExit = getObj(currentPos, true)->id; status.setChannelProbing(true); @@ -1265,7 +1265,7 @@ bool AIGateway::moveHeroToTile(int3 dst, HeroPtr h) int3 currentCoord = path.nodes[i].coord; int3 nextCoord = path.nodes[i - 1].coord; - auto currentObject = getObj(currentCoord, currentCoord == CGHeroInstance::convertPosition(h->pos, false)); + auto currentObject = getObj(currentCoord, currentCoord == h->convertPosition(h->pos, false)); auto nextObjectTop = getObj(nextCoord, false); auto nextObject = getObj(nextCoord, true); auto destTeleportObj = getDestTeleportObj(currentObject, nextObjectTop, nextObject); diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index 82c42dc65..69aebf2a9 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -101,8 +101,8 @@ void VCAI::heroMoved(const TryMoveHero & details, bool verbose) validateObject(details.id); //enemy hero may have left visible area auto hero = cb->getHero(details.id); - const int3 from = CGHeroInstance::convertPosition(details.start, false); - const int3 to = CGHeroInstance::convertPosition(details.end, false); + const int3 from = hero->convertPosition(details.start, false); + const int3 to = hero->convertPosition(details.end, false); const CGObjectInstance * o1 = vstd::frontOrNull(cb->getVisitableObjs(from, verbose)); const CGObjectInstance * o2 = vstd::frontOrNull(cb->getVisitableObjs(to, verbose)); @@ -1813,7 +1813,7 @@ bool VCAI::moveHeroToTile(int3 dst, HeroPtr h) { //FIXME: this assertion fails also if AI moves onto defeated guarded object assert(cb->getVisitableObjs(dst).size() > 1); //there's no point in revisiting tile where there is no visitable object - cb->moveHero(*h, CGHeroInstance::convertPosition(dst, true)); + cb->moveHero(*h, h->convertPosition(dst, true)); afterMovementCheck(); // TODO: is it feasible to hero get killed there if game work properly? // If revisiting, teleport probing is never done, and so the entries into the list would remain unused and uncleared teleportChannelProbingList.clear(); @@ -1867,14 +1867,14 @@ bool VCAI::moveHeroToTile(int3 dst, HeroPtr h) auto doMovement = [&](int3 dst, bool transit) { - cb->moveHero(*h, CGHeroInstance::convertPosition(dst, true), transit); + cb->moveHero(*h, h->convertPosition(dst, true), transit); }; auto doTeleportMovement = [&](ObjectInstanceID exitId, int3 exitPos) { destinationTeleport = exitId; if(exitPos.valid()) - destinationTeleportPos = CGHeroInstance::convertPosition(exitPos, true); + destinationTeleportPos = h->convertPosition(exitPos, true); cb->moveHero(*h, h->pos); destinationTeleport = ObjectInstanceID(); destinationTeleportPos = int3(-1); @@ -1883,7 +1883,7 @@ bool VCAI::moveHeroToTile(int3 dst, HeroPtr h) auto doChannelProbing = [&]() -> void { - auto currentPos = CGHeroInstance::convertPosition(h->pos, false); + auto currentPos = h->convertPosition(h->pos, false); auto currentExit = getObj(currentPos, true)->id; status.setChannelProbing(true); @@ -1900,7 +1900,7 @@ bool VCAI::moveHeroToTile(int3 dst, HeroPtr h) int3 currentCoord = path.nodes[i].coord; int3 nextCoord = path.nodes[i - 1].coord; - auto currentObject = getObj(currentCoord, currentCoord == CGHeroInstance::convertPosition(h->pos, false)); + auto currentObject = getObj(currentCoord, currentCoord == h->convertPosition(h->pos, false)); auto nextObjectTop = getObj(nextCoord, false); auto nextObject = getObj(nextCoord, true); auto destTeleportObj = getDestTeleportObj(currentObject, nextObjectTop, nextObject); diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index bbc9e1a72..f9fe1efcb 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -264,8 +264,8 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose) assert(adventureInt->terrain.currentPath->nodes.size() >= 2); std::vector::const_iterator nodesIt = adventureInt->terrain.currentPath->nodes.end() - 1; - if((nodesIt)->coord == CGHeroInstance::convertPosition(details.start, false) - && (nodesIt - 1)->coord == CGHeroInstance::convertPosition(details.end, false)) + if((nodesIt)->coord == hero->convertPosition(details.start, false) + && (nodesIt - 1)->coord == hero->convertPosition(details.end, false)) { //path was between entrance and exit of teleport -> OK, erase node as usual removeLastNodeFromPath(hero); @@ -2405,7 +2405,7 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path) int i = 1; auto getObj = [&](int3 coord, bool ignoreHero) { - return cb->getTile(CGHeroInstance::convertPosition(coord,false))->topVisitableObj(ignoreHero); + return cb->getTile(h->convertPosition(coord,false))->topVisitableObj(ignoreHero); }; auto isTeleportAction = [&](CGPathNode::ENodeAction action) -> bool @@ -2444,7 +2444,9 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path) }; { - path.convert(0); + for (auto & elem : path.nodes) + elem.coord = h->convertPosition(elem.coord,true); + TerrainId currentTerrain = Terrain::BORDER; // not init yet TerrainId newTerrain; int sh = -1; @@ -2501,7 +2503,7 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path) sh = CCS->soundh->playSound(soundBase::horseFlying, -1); #endif { - newTerrain = cb->getTile(CGHeroInstance::convertPosition(currentCoord, false))->terType->id; + newTerrain = cb->getTile(h->convertPosition(currentCoord, false))->terType->id; if(newTerrain != currentTerrain) { CCS->soundh->stopSound(sh); diff --git a/lib/CPathfinder.cpp b/lib/CPathfinder.cpp index 44a31e971..7af2aa2c3 100644 --- a/lib/CPathfinder.cpp +++ b/lib/CPathfinder.cpp @@ -1282,17 +1282,6 @@ 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, const CGHeroInstance * hero_) : sizes(Sizes), hero(hero_) { diff --git a/lib/CPathfinder.h b/lib/CPathfinder.h index 2e9a5803f..01cc372b4 100644 --- a/lib/CPathfinder.h +++ b/lib/CPathfinder.h @@ -170,7 +170,6 @@ struct DLL_LINKAGE CGPath int3 startPos() const; // start point int3 endPos() const; //destination point - void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object' }; struct DLL_LINKAGE CPathsInfo diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 646a7c25c..145604806 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -550,7 +550,7 @@ void TryMoveHero::applyGs(CGameState *gs) if(result == EMBARK) //hero enters boat at destination tile { - const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false)); + const TerrainTile &tt = gs->map->getTile(h->convertPosition(end, false)); assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat CGBoat *boat = static_cast(tt.visitableObjects.back()); diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 62945c9dc..fbb378450 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -122,18 +122,12 @@ TerrainId CGHeroInstance::getNativeTerrain() const return nativeTerrain; } -int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest +int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) const //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest { if (toh3m) - { - src.x+=1; - return src; - } + return src + getVisitableOffset(); else - { - src.x-=1; - return src; - } + return src - getVisitableOffset(); } BattleField CGHeroInstance::getBattlefield() const diff --git a/lib/mapObjects/CGHeroInstance.h b/lib/mapObjects/CGHeroInstance.h index 34af804a8..b96862a5a 100644 --- a/lib/mapObjects/CGHeroInstance.h +++ b/lib/mapObjects/CGHeroInstance.h @@ -198,7 +198,7 @@ public: int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false, const TurnInfo * ti = nullptr) const; - static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest + int3 convertPosition(int3 src, bool toh3m) const; //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest double getFightingStrength() const; // takes attack / defense skill into account double getMagicStrength() const; // takes knowledge / spell power skill into account double getHeroStrength() const; // includes fighting and magic strength diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 5f3f03a13..70860c1f0 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -1074,7 +1074,7 @@ void CGMonolith::onHeroVisit( const CGHeroInstance * h ) const auto exits = cb->getTeleportChannelExits(channel); for(auto exit : exits) { - td.exits.push_back(std::make_pair(exit, CGHeroInstance::convertPosition(cb->getObj(exit)->visitablePos(), true))); + td.exits.push_back(std::make_pair(exit, h->convertPosition(cb->getObj(exit)->visitablePos(), true))); } } @@ -1106,7 +1106,7 @@ void CGMonolith::teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, else if(vstd::isValidIndex(exits, answer)) dPos = exits[answer].second; else - dPos = CGHeroInstance::convertPosition(cb->getObj(randomExit)->visitablePos(), true); + dPos = hero->convertPosition(cb->getObj(randomExit)->visitablePos(), true); cb->moveHero(hero->id, dPos, true); } @@ -1150,7 +1150,7 @@ void CGSubterraneanGate::onHeroVisit( const CGHeroInstance * h ) const else { auto exit = getRandomExit(h); - td.exits.push_back(std::make_pair(exit, CGHeroInstance::convertPosition(cb->getObj(exit)->visitablePos(), true))); + td.exits.push_back(std::make_pair(exit, h->convertPosition(cb->getObj(exit)->visitablePos(), true))); } cb->showTeleportDialog(&td); @@ -1259,7 +1259,7 @@ void CGWhirlpool::onHeroVisit( const CGHeroInstance * h ) const { auto blockedPosList = cb->getObj(exit)->getBlockedPos(); for(auto bPos : blockedPosList) - td.exits.push_back(std::make_pair(exit, CGHeroInstance::convertPosition(bPos, true))); + td.exits.push_back(std::make_pair(exit, h->convertPosition(bPos, true))); } } @@ -1283,7 +1283,7 @@ void CGWhirlpool::teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer auto obj = cb->getObj(exit); std::set tiles = obj->getBlockedPos(); - dPos = CGHeroInstance::convertPosition(*RandomGeneratorUtil::nextItem(tiles, CRandomGenerator::getDefault()), true); + dPos = hero->convertPosition(*RandomGeneratorUtil::nextItem(tiles, CRandomGenerator::getDefault()), true); } cb->moveHero(hero->id, dPos, true); diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index f8a0c6c7d..b72fbdbbc 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -1664,7 +1664,7 @@ CGObjectInstance * CMapLoaderH3M::readHero(ObjectInstanceID idToBeGiven, const i else { nhi->patrol.patrolling = true; - nhi->patrol.initialPos = CGHeroInstance::convertPosition(initialPos, false); + nhi->patrol.initialPos = nhi->convertPosition(initialPos, false); } if(map->version > EMapFormat::ROE) diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 654043873..2d6608c5e 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -2326,7 +2326,7 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, boo } logGlobal->trace("Player %d (%s) wants to move hero %d from %s to %s", asker, asker.getStr(), hid.getNum(), h->pos.toString(), dst.toString()); - const int3 hmpos = CGHeroInstance::convertPosition(dst, false); + const int3 hmpos = h->convertPosition(dst, false); if (!gs->map->isInTheMap(hmpos)) { diff --git a/server/CQuery.cpp b/server/CQuery.cpp index 7c1df47be..38c8e4ca7 100644 --- a/server/CQuery.cpp +++ b/server/CQuery.cpp @@ -489,7 +489,7 @@ void CHeroMovementQuery::onExposure(QueryPtr topQuery) logGlobal->trace("Hero %s after victory over guard finishes visit to %s", hero->name, tmh.end.toString()); //finish movement visitDestAfterVictory = false; - gh->visitObjectOnTile(*gh->getTile(CGHeroInstance::convertPosition(tmh.end, false)), hero); + gh->visitObjectOnTile(*gh->getTile(hero->convertPosition(tmh.end, false)), hero); } owner->popIfTop(*this);