| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 | 
							- #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;
 
- 	useCastleGate = false;
 
- 	lightweightFlyingMode = false;
 
- 	oneTurnSpecialLayersLimit = true;
 
- 	originalMovementRules = true;
 
- }
 
- CPathfinder::CPathfinder(CPathsInfo & _out, CGameState * _gs, const CGHeroInstance * _hero)
 
- 	: CGameInfoCallback(_gs, boost::optional<PlayerColor>()), out(_out), hero(_hero)
 
- {
 
- 	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");
 
- 	}
 
- 	hlp = make_unique<CPathfinderHelper>(hero);
 
- 	if(hlp->hasBonusOfType(Bonus::FLYING_MOVEMENT))
 
- 		options.useFlying = true;
 
- 	if(hlp->hasBonusOfType(Bonus::WATER_WALKING))
 
- 		options.useWaterWalking = true;
 
- 	if(hlp->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
 
- 		options.useTeleportWhirlpool = true;
 
- 	initializeGraph();
 
- 	neighbours.reserve(16);
 
- }
 
- void CPathfinder::calculatePaths()
 
- {
 
- 	auto passOneTurnLimitCheck = [&](bool shouldCheck) -> bool
 
- 	{
 
- 		if(options.oneTurnSpecialLayersLimit && shouldCheck)
 
- 		{
 
- 			if((cp->layer == ELayer::AIR || cp->layer == ELayer::WATER)
 
- 				&& cp->accessible != CGPathNode::ACCESSIBLE)
 
- 			{
 
- 				return false;
 
- 			}
 
- 		}
 
- 		return true;
 
- 	};
 
- 	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 ? ELayer::SAIL : ELayer::LAND);
 
- 	initialNode->turns = 0;
 
- 	initialNode->moveRemains = hero->movement;
 
- 	pq.push(initialNode);
 
- 	while(!pq.empty())
 
- 	{
 
- 		cp = pq.top();
 
- 		pq.pop();
 
- 		cp->locked = true;
 
- 		ct = &gs->map->getTile(cp->coord);
 
- 		ctObj = ct->topVisitableObj(isSourceInitialPosition());
 
- 		int movement = cp->moveRemains, turn = cp->turns;
 
- 		hlp->updateTurnInfo(turn);
 
- 		if(!movement)
 
- 		{
 
- 			hlp->updateTurnInfo(++turn);
 
- 			movement = hlp->getMaxMovePoints(cp->layer);
 
- 		}
 
- 		//add accessible neighbouring nodes to the queue
 
- 		addNeighbours(cp->coord);
 
- 		for(auto & neighbour : neighbours)
 
- 		{
 
- 			dt = &gs->map->getTile(neighbour);
 
- 			dtObj = dt->topVisitableObj();
 
- 			for(ELayer i = ELayer::LAND; i <= ELayer::AIR; i.advance(1))
 
- 			{
 
- 				dp = out.getNode(neighbour, i);
 
- 				if(dp->accessible == CGPathNode::NOT_SET)
 
- 					continue;
 
- 				if(dp->locked)
 
- 					continue;
 
- 				if(!passOneTurnLimitCheck(cp->turns != turn))
 
- 					continue;
 
- 				if(!hlp->isLayerAvailable(i))
 
- 					continue;
 
- 				if(cp->layer != i && !isLayerTransitionPossible())
 
- 					continue;
 
- 				if(!isMovementToDestPossible())
 
- 					continue;
 
- 				destAction = getDestAction();
 
- 				int cost = CPathfinderHelper::getMovementCost(hero, cp->coord, dp->coord, movement, hlp->getTurnInfo());
 
- 				int remains = movement - cost;
 
- 				if(destAction == CGPathNode::EMBARK || destAction == CGPathNode::DISEMBARK)
 
- 				{
 
- 					remains = hero->movementPointsAfterEmbark(movement, cost, destAction - 1, hlp->getTurnInfo());
 
- 					cost = movement - remains;
 
- 				}
 
- 				int turnAtNextTile = turn;
 
- 				if(remains < 0)
 
- 				{
 
- 					//occurs rarely, when hero with low movepoints tries to leave the road
 
- 					hlp->updateTurnInfo(++turnAtNextTile);
 
- 					int moveAtNextTile = hlp->getMaxMovePoints(i);
 
- 					cost = CPathfinderHelper::getMovementCost(hero, cp->coord, dp->coord, moveAtNextTile, hlp->getTurnInfo()); //cost must be updated, movement points changed :(
 
- 					remains = moveAtNextTile - cost;
 
- 				}
 
- 				if(isBetterWay(remains, turnAtNextTile)
 
- 					&& passOneTurnLimitCheck(cp->turns != turnAtNextTile || !remains))
 
- 				{
 
- 					assert(dp != cp->theNodeBefore); //two tiles can't point to each other
 
- 					dp->moveRemains = remains;
 
- 					dp->turns = turnAtNextTile;
 
- 					dp->theNodeBefore = cp;
 
- 					dp->action = destAction;
 
- 					if(isMovementAfterDestPossible())
 
- 						pq.push(dp);
 
- 				}
 
- 			}
 
- 		} //neighbours loop
 
- 		//just add all passable teleport exits
 
- 		addTeleportExits();
 
- 		for(auto & neighbour : neighbours)
 
- 		{
 
- 			dp = out.getNode(neighbour, cp->layer);
 
- 			if(dp->locked)
 
- 				continue;
 
- 			if(isBetterWay(movement, turn))
 
- 			{
 
- 				dp->moveRemains = movement;
 
- 				dp->turns = turn;
 
- 				dp->theNodeBefore = cp;
 
- 				dp->action = CGPathNode::NORMAL;
 
- 				pq.push(dp);
 
- 			}
 
- 		}
 
- 	} //queue loop
 
- }
 
- void CPathfinder::addNeighbours(const int3 & coord)
 
- {
 
- 	neighbours.clear();
 
- 	std::vector<int3> tiles;
 
- 	CPathfinderHelper::getNeighbours(gs, *ct, coord, tiles, boost::logic::indeterminate, cp->layer == ELayer::SAIL); // TODO: find out if we still need "limitCoastSailing" option
 
- 	if(isSourceVisitableObj())
 
- 	{
 
- 		for(int3 tile: tiles)
 
- 		{
 
- 			if(canMoveBetween(tile, ctObj->visitablePos()))
 
- 				neighbours.push_back(tile);
 
- 		}
 
- 	}
 
- 	else
 
- 		vstd::concatenate(neighbours, tiles);
 
- }
 
- void CPathfinder::addTeleportExits(bool noTeleportExcludes)
 
- {
 
- 	neighbours.clear();
 
- 	if(!isSourceVisitableObj())
 
- 		return;
 
- 	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 *>(ctObj);
 
- 	if(isAllowedTeleportEntrance(sTileTeleport))
 
- 	{
 
- 		for(auto objId : gs->getTeleportChannelExits(sTileTeleport->channel, hero->tempOwner))
 
- 		{
 
- 			auto obj = getObj(objId);
 
- 			if(dynamic_cast<const CGWhirlpool *>(obj))
 
- 			{
 
- 				auto pos = obj->getBlockedPos();
 
- 				for(auto p : pos)
 
- 				{
 
- 					if(gs->getTile(p)->topVisitableId() == obj->ID)
 
- 						neighbours.push_back(p);
 
- 				}
 
- 			}
 
- 			else if(CGTeleport::isExitPassable(gs, hero, obj))
 
- 				neighbours.push_back(obj->visitablePos());
 
- 		}
 
- 	}
 
- 	if(options.useCastleGate
 
- 		&& (ctObj->ID == Obj::TOWN && ctObj->subID == ETownType::INFERNO
 
- 		&& getPlayerRelations(hero->tempOwner, ctObj->tempOwner) != PlayerRelations::ENEMIES))
 
- 	{
 
- 		/// TODO: Find way to reuse CPlayerSpecificInfoCallback::getTownsInfo
 
- 		/// This may be handy if we allow to use teleportation to friendly towns
 
- 		auto towns = gs->getPlayer(hero->tempOwner)->towns;
 
- 		for(const auto & town : towns)
 
- 		{
 
- 			if(town->id != ctObj->id && town->visitingHero == nullptr
 
- 				&& town->hasBuilt(BuildingID::CASTLE_GATE, ETownType::INFERNO))
 
- 			{
 
- 				neighbours.push_back(town->visitablePos());
 
- 			}
 
- 		}
 
- 	}
 
- }
 
- bool CPathfinder::isLayerTransitionPossible() const
 
- {
 
- 	/// No layer transition allowed when previous node action is BATTLE
 
- 	if(cp->action == CGPathNode::BATTLE)
 
- 		return false;
 
- 	switch(cp->layer)
 
- 	{
 
- 	case ELayer::LAND:
 
- 		if(options.lightweightFlyingMode && dp->layer == ELayer::AIR)
 
- 		{
 
- 			if(!isSourceInitialPosition())
 
- 				return false;
 
- 		}
 
- 		else if(dp->layer == ELayer::SAIL)
 
- 		{
 
- 			/// Cannot enter empty water tile from land -> it has to be visitable
 
- 			if(dp->accessible == CGPathNode::ACCESSIBLE)
 
- 				return false;
 
- 		}
 
- 		break;
 
- 	case ELayer::SAIL:
 
- 		if(dp->layer != ELayer::LAND)
 
- 			return false;
 
- 		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;
 
- 		}
 
- 		break;
 
- 	case ELayer::AIR:
 
- 		if(dp->layer != ELayer::LAND)
 
- 			return false;
 
- 		if(options.originalMovementRules)
 
- 		{
 
- 			if((cp->accessible != CGPathNode::ACCESSIBLE &&
 
- 				cp->accessible != CGPathNode::VISITABLE) &&
 
- 				(dp->accessible != CGPathNode::VISITABLE &&
 
- 				 dp->accessible != CGPathNode::ACCESSIBLE))
 
- 			{
 
- 				return false;
 
- 			}
 
- 		}
 
- 		else if(cp->accessible != CGPathNode::ACCESSIBLE &&	dp->accessible != CGPathNode::ACCESSIBLE)
 
- 		{
 
- 			/// Hero that fly can only land on accessible tiles
 
- 			return false;
 
- 		}
 
- 		break;
 
- 	case ELayer::WATER:
 
- 		if(dp->layer != ELayer::LAND)
 
- 			return false;
 
- 		break;
 
- 	}
 
- 	return true;
 
- }
 
- bool CPathfinder::isMovementToDestPossible() const
 
- {
 
- 	switch(dp->layer)
 
- 	{
 
- 	case ELayer::LAND:
 
- 		if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
 
- 			return false;
 
- 		if(isSourceGuarded())
 
- 		{
 
- 			if(!(options.originalMovementRules && cp->layer == ELayer::AIR) &&
 
- 				!isDestinationGuardian()) // Can step into tile of guard
 
- 			{
 
- 				return false;
 
- 			}
 
- 		}
 
- 		break;
 
- 	case ELayer::SAIL:
 
- 		if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible == CGPathNode::BLOCKED)
 
- 			return false;
 
- 		if(isSourceGuarded())
 
- 		{
 
- 			// Hero embarked a boat standing on a guarded tile -> we must allow to move away from that tile
 
- 			if(cp->action != CGPathNode::EMBARK && !isDestinationGuardian())
 
- 				return false;
 
- 		}
 
- 		if(cp->layer == ELayer::LAND)
 
- 		{
 
- 			if(!dtObj)
 
- 				return false;
 
- 			if(dtObj->ID != Obj::BOAT && dtObj->ID != Obj::HERO)
 
- 				return false;
 
- 		}
 
- 		else if(dtObj && dtObj->ID == Obj::BOAT)
 
- 		{
 
- 			/// Hero in boat can't visit empty boats
 
- 			return false;
 
- 		}
 
- 		break;
 
- 	case ELayer::WATER:
 
- 		if(!canMoveBetween(cp->coord, dp->coord) || dp->accessible != CGPathNode::ACCESSIBLE)
 
- 			return false;
 
- 		if(isDestinationGuarded())
 
- 			return false;
 
- 		break;
 
- 	}
 
- 	return true;
 
- }
 
- bool CPathfinder::isMovementAfterDestPossible() const
 
- {
 
- 	switch(destAction)
 
- 	{
 
- 	/// TODO: Investigate what kind of limitation is possible to apply on movement from visitable tiles
 
- 	/// Likely in many cases we don't need to add visitable tile to queue when hero don't fly
 
- 	case CGPathNode::VISIT:
 
- 		/// For now we only add visitable tile into queue when it's teleporter that allow transit
 
- 		/// Movement from visitable tile when hero is standing on it is possible into any layer
 
- 		if(CGTeleport::isTeleport(dtObj))
 
- 		{
 
- 			/// For now we'll always allow transit over teleporters
 
- 			/// Transit over whirlpools only allowed when hero protected
 
- 			auto whirlpool = dynamic_cast<const CGWhirlpool *>(dtObj);
 
- 			if(!whirlpool || options.useTeleportWhirlpool)
 
- 				return true;
 
- 		}
 
- 		break;
 
- 	case CGPathNode::NORMAL:
 
- 		return true;
 
- 	case CGPathNode::EMBARK:
 
- 		if(options.useEmbarkAndDisembark)
 
- 			return true;
 
- 		break;
 
- 	case CGPathNode::DISEMBARK:
 
- 		if(options.useEmbarkAndDisembark && !isDestinationGuarded())
 
- 			return true;
 
- 		break;
 
- 	case CGPathNode::BATTLE:
 
- 		/// Movement after BATTLE action only possible from guarded tile to guardian tile
 
- 		if(isDestinationGuarded())
 
- 			return true;
 
- 		break;
 
- 	}
 
- 	return false;
 
- }
 
- CGPathNode::ENodeAction CPathfinder::getDestAction() const
 
- {
 
- 	CGPathNode::ENodeAction action = CGPathNode::NORMAL;
 
- 	switch(dp->layer)
 
- 	{
 
- 	case ELayer::LAND:
 
- 		if(cp->layer == ELayer::SAIL)
 
- 		{
 
- 			// TODO: Handle dismebark into guarded areaa
 
- 			action = CGPathNode::DISEMBARK;
 
- 			break;
 
- 		}
 
- 	case ELayer::SAIL:
 
- 		if(dtObj)
 
- 		{
 
- 			auto objRel = getPlayerRelations(dtObj->tempOwner, hero->tempOwner);
 
- 			if(dtObj->ID == Obj::BOAT)
 
- 				action = CGPathNode::EMBARK;
 
- 			else if(dtObj->ID == Obj::HERO)
 
- 			{
 
- 				if(objRel == PlayerRelations::ENEMIES)
 
- 					action = CGPathNode::BATTLE;
 
- 				else
 
- 					action = CGPathNode::BLOCKING_VISIT;
 
- 			}
 
- 			else if(dtObj->ID == Obj::TOWN && objRel == PlayerRelations::ENEMIES)
 
- 			{
 
- 				const CGTownInstance * townObj = dynamic_cast<const CGTownInstance *>(dtObj);
 
- 				if(townObj->armedGarrison())
 
- 					action = CGPathNode::BATTLE;
 
- 			}
 
- 			else if(dtObj->ID == Obj::GARRISON || dtObj->ID == Obj::GARRISON2)
 
- 			{
 
- 				const CGGarrison * garrisonObj = dynamic_cast<const CGGarrison *>(dtObj);
 
- 				if((garrisonObj->stacksCount() && objRel == PlayerRelations::ENEMIES) || isDestinationGuarded(true))
 
- 					action = CGPathNode::BATTLE;
 
- 			}
 
- 			else if(isDestinationGuardian())
 
- 				action = CGPathNode::BATTLE;
 
- 			else if(dtObj->blockVisit && (!options.useCastleGate || dtObj->ID != Obj::TOWN))
 
- 				action = CGPathNode::BLOCKING_VISIT;
 
- 			if(action == CGPathNode::NORMAL)
 
- 			{
 
- 				if(options.originalMovementRules && isDestinationGuarded())
 
- 					action = CGPathNode::BATTLE;
 
- 				else
 
- 					action = CGPathNode::VISIT;
 
- 			}
 
- 		}
 
- 		else if(isDestinationGuarded())
 
- 			action = CGPathNode::BATTLE;
 
- 		break;
 
- 	}
 
- 	return action;
 
- }
 
- bool CPathfinder::isSourceInitialPosition() const
 
- {
 
- 	return cp->coord == out.hpos;
 
- }
 
- bool CPathfinder::isSourceVisitableObj() const
 
- {
 
- 	/// Hero can't visit objects while walking on water or flying
 
- 	return ctObj != nullptr && (cp->layer == ELayer::LAND || cp->layer == ELayer::SAIL);
 
- }
 
- bool CPathfinder::isSourceGuarded() const
 
- {
 
- 	/// Hero can move from guarded tile if movement started on that tile
 
- 	/// It's possible at least in these cases:
 
- 	/// - Map start with hero on guarded tile
 
- 	/// - Dimention door used
 
- 	///  TODO: check what happen when there is several guards
 
- 	if(gs->guardingCreaturePosition(cp->coord) != int3(-1, -1, -1) && !isSourceInitialPosition())
 
- 	{
 
- 		return true;
 
- 	}
 
- 	return false;
 
- }
 
- bool CPathfinder::isDestinationGuarded(const bool ignoreAccessibility) const
 
- {
 
- 	if(gs->guardingCreaturePosition(dp->coord).valid()
 
- 		&& (ignoreAccessibility || dp->accessible == CGPathNode::BLOCKVIS))
 
- 	{
 
- 		return true;
 
- 	}
 
- 	return false;
 
- }
 
- bool CPathfinder::isDestinationGuardian() const
 
- {
 
- 	return gs->guardingCreaturePosition(cp->coord) == dp->coord;
 
- }
 
- void CPathfinder::initializeGraph()
 
- {
 
- 	auto updateNode = [&](int3 pos, ELayer layer, const TerrainTile * tinfo, bool blockNotAccessible)
 
- 	{
 
- 		auto node = out.getNode(pos, layer);
 
- 		auto accessibility = evaluateAccessibility(pos, tinfo);
 
- 		/// TODO: Probably this shouldn't be handled by initializeGraph
 
- 		if(blockNotAccessible
 
- 			&& (accessibility != CGPathNode::ACCESSIBLE || tinfo->terType == ETerrainType::WATER))
 
- 		{
 
- 			accessibility = CGPathNode::BLOCKED;
 
- 		}
 
- 		node->update(pos, layer, accessibility);
 
- 	};
 
- 	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::ROCK:
 
- 						break;
 
- 					case ETerrainType::WATER:
 
- 						updateNode(pos, ELayer::SAIL, tinfo, false);
 
- 						if(options.useFlying)
 
- 							updateNode(pos, ELayer::AIR, tinfo, true);
 
- 						if(options.useWaterWalking)
 
- 							updateNode(pos, ELayer::WATER, tinfo, false);
 
- 						break;
 
- 					default:
 
- 						updateNode(pos, ELayer::LAND, tinfo, false);
 
- 						if(options.useFlying)
 
- 							updateNode(pos, ELayer::AIR, tinfo, true);
 
- 						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 || !isVisible(pos, hero->tempOwner))
 
- 		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->guardingCreaturePosition(pos).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;
 
- }
 
- TurnInfo::TurnInfo(const CGHeroInstance * Hero, const int turn)
 
- 	: hero(Hero), maxMovePointsLand(-1), maxMovePointsWater(-1)
 
- {
 
- 	bonuses = hero->getAllBonuses(Selector::days(turn), nullptr);
 
- }
 
- bool TurnInfo::isLayerAvailable(const EPathfindingLayer layer) const
 
- {
 
- 	switch(layer)
 
- 	{
 
- 	case EPathfindingLayer::AIR:
 
- 		if(!hasBonusOfType(Bonus::FLYING_MOVEMENT))
 
- 			return false;
 
- 		break;
 
- 	case EPathfindingLayer::WATER:
 
- 		if(!hasBonusOfType(Bonus::WATER_WALKING))
 
- 			return false;
 
- 		break;
 
- 	}
 
- 	return true;
 
- }
 
- bool TurnInfo::hasBonusOfType(Bonus::BonusType type, int subtype) const
 
- {
 
- 	return bonuses->getFirst(Selector::type(type).And(Selector::subtype(subtype)));
 
- }
 
- int TurnInfo::valOfBonuses(Bonus::BonusType type, int subtype) const
 
- {
 
- 	return bonuses->valOfBonuses(Selector::type(type).And(Selector::subtype(subtype)));
 
- }
 
- int TurnInfo::getMaxMovePoints(const EPathfindingLayer layer) const
 
- {
 
- 	if(maxMovePointsLand == -1)
 
- 		maxMovePointsLand = hero->maxMovePoints(true, this);
 
- 	if(maxMovePointsWater == -1)
 
- 		maxMovePointsWater = hero->maxMovePoints(false, this);
 
- 	return layer == EPathfindingLayer::SAIL ? maxMovePointsWater : maxMovePointsLand;
 
- }
 
- CPathfinderHelper::CPathfinderHelper(const CGHeroInstance * Hero)
 
- 	: turn(-1), hero(Hero)
 
- {
 
- 	turnsInfo.reserve(16);
 
- 	updateTurnInfo();
 
- }
 
- void CPathfinderHelper::updateTurnInfo(const int Turn)
 
- {
 
- 	if(turn != Turn)
 
- 	{
 
- 		turn = Turn;
 
- 		if(turn >= turnsInfo.size())
 
- 		{
 
- 			auto ti = new TurnInfo(hero, turn);
 
- 			turnsInfo.push_back(ti);
 
- 		}
 
- 	}
 
- }
 
- bool CPathfinderHelper::isLayerAvailable(const EPathfindingLayer layer) const
 
- {
 
- 	return turnsInfo[turn]->isLayerAvailable(layer);
 
- }
 
- const TurnInfo * CPathfinderHelper::getTurnInfo() const
 
- {
 
- 	return turnsInfo[turn];
 
- }
 
- bool CPathfinderHelper::hasBonusOfType(const Bonus::BonusType type, const int subtype) const
 
- {
 
- 	return turnsInfo[turn]->hasBonusOfType(type, subtype);
 
- }
 
- int CPathfinderHelper::getMaxMovePoints(const EPathfindingLayer layer) const
 
- {
 
- 	return turnsInfo[turn]->getMaxMovePoints(layer);
 
- }
 
- void CPathfinderHelper::getNeighbours(CGameState * gs, const TerrainTile & srct, const int3 & tile, std::vector<int3> & vec, const boost::logic::tribool & onLand, const bool limitCoastSailing)
 
- {
 
- 	static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
 
- 					int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
 
- 	//vec.reserve(8); //optimization
 
- 	for(auto & dir : dirs)
 
- 	{
 
- 		const int3 hlp = tile + dir;
 
- 		if(!gs->isInTheMap(hlp))
 
- 			continue;
 
- 		const TerrainTile & hlpt = gs->map->getTile(hlp);
 
- // 		//we cannot visit things from blocked tiles
 
- // 		if(srct.blocked && !srct.visitable && hlpt.visitable && srct.blockingObjects.front()->ID != HEROI_TYPE)
 
- // 		{
 
- // 			continue;
 
- // 		}
 
- 		if(srct.terType == ETerrainType::WATER && limitCoastSailing && hlpt.terType == ETerrainType::WATER && dir.x && dir.y) //diagonal move through water
 
- 		{
 
- 			int3 hlp1 = tile,
 
- 				hlp2 = tile;
 
- 			hlp1.x += dir.x;
 
- 			hlp2.y += dir.y;
 
- 			if(gs->map->getTile(hlp1).terType != ETerrainType::WATER || gs->map->getTile(hlp2).terType != ETerrainType::WATER)
 
- 				continue;
 
- 		}
 
- 		if((indeterminate(onLand) || onLand == (hlpt.terType!=ETerrainType::WATER) )
 
- 			&& hlpt.terType != ETerrainType::ROCK)
 
- 		{
 
- 			vec.push_back(hlp);
 
- 		}
 
- 	}
 
- }
 
- int CPathfinderHelper::getMovementCost(const CGHeroInstance * h, const int3 & src, const int3 & dst, const int remainingMovePoints, const TurnInfo * ti, const bool checkLast)
 
- {
 
- 	if(src == dst) //same tile
 
- 		return 0;
 
- 	if(!ti)
 
- 		ti = new TurnInfo(h);
 
- 	auto s = h->cb->getTile(src), d = h->cb->getTile(dst);
 
- 	int ret = h->getTileCost(*d, *s, ti);
 
- 	if(d->blocked && ti->hasBonusOfType(Bonus::FLYING_MOVEMENT))
 
- 	{
 
- 		ret *= (100.0 + ti->valOfBonuses(Bonus::FLYING_MOVEMENT)) / 100.0;
 
- 	}
 
- 	else if(d->terType == ETerrainType::WATER)
 
- 	{
 
- 		if(h->boat && s->hasFavourableWinds() && d->hasFavourableWinds()) //Favourable Winds
 
- 			ret *= 0.666;
 
- 		else if(!h->boat && ti->hasBonusOfType(Bonus::WATER_WALKING))
 
- 		{
 
- 			ret *= (100.0 + ti->valOfBonuses(Bonus::WATER_WALKING)) / 100.0;
 
- 		}
 
- 	}
 
- 	if(src.x != dst.x && src.y != dst.y) //it's diagonal move
 
- 	{
 
- 		int old = ret;
 
- 		ret *= 1.414213;
 
- 		//diagonal move costs too much but normal move is possible - allow diagonal move for remaining move points
 
- 		if(ret > remainingMovePoints && remainingMovePoints >= old)
 
- 			return remainingMovePoints;
 
- 	}
 
- 	int left = remainingMovePoints-ret;
 
- 	if(checkLast && left > 0 && remainingMovePoints-ret < 250) //it might be the last tile - if no further move possible we take all move points
 
- 	{
 
- 		std::vector<int3> vec;
 
- 		vec.reserve(8); //optimization
 
- 		getNeighbours(h->cb->gameState(), *d, dst, vec, s->terType != ETerrainType::WATER, true);
 
- 		for(auto & elem : vec)
 
- 		{
 
- 			int fcost = getMovementCost(h, dst, elem, left, ti, false);
 
- 			if(fcost <= left)
 
- 				return ret;
 
- 		}
 
- 		ret = remainingMovePoints;
 
- 	}
 
- 	return ret;
 
- }
 
- int CPathfinderHelper::getMovementCost(const CGHeroInstance * h, const int3 & dst)
 
- {
 
- 	return getMovementCost(h, h->visitablePos(), dst, h->movement);
 
- }
 
- CGPathNode::CGPathNode()
 
- 	: coord(int3(-1, -1, -1)), layer(ELayer::WRONG)
 
- {
 
- 	reset();
 
- }
 
- void CGPathNode::reset()
 
- {
 
- 	locked = false;
 
- 	accessible = NOT_SET;
 
- 	moveRemains = 0;
 
- 	turns = 255;
 
- 	theNodeBefore = nullptr;
 
- 	action = UNKNOWN;
 
- }
 
- void CGPathNode::update(const int3 & Coord, const ELayer Layer, const EAccessibility Accessible)
 
- {
 
- 	if(layer == ELayer::WRONG)
 
- 	{
 
- 		coord = Coord;
 
- 		layer = Layer;
 
- 	}
 
- 	else
 
- 		reset();
 
- 	accessible = Accessible;
 
- }
 
- 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.resize(boost::extents[sizes.x][sizes.y][sizes.z][ELayer::NUM_LAYERS]);
 
- }
 
- CPathsInfo::~CPathsInfo()
 
- {
 
- }
 
- const CGPathNode * CPathsInfo::getPathInfo(const int3 & tile) const
 
- {
 
- 	assert(vstd::iswithin(tile.x, 0, sizes.x));
 
- 	assert(vstd::iswithin(tile.y, 0, sizes.y));
 
- 	assert(vstd::iswithin(tile.z, 0, sizes.z));
 
- 	boost::unique_lock<boost::mutex> pathLock(pathMx);
 
- 	return getNode(tile);
 
- }
 
- bool CPathsInfo::getPath(CGPath & out, const int3 & dst) const
 
- {
 
- 	boost::unique_lock<boost::mutex> pathLock(pathMx);
 
- 	out.nodes.clear();
 
- 	const CGPathNode * curnode = getNode(dst);
 
- 	if(!curnode->theNodeBefore)
 
- 		return false;
 
- 	while(curnode)
 
- 	{
 
- 		const CGPathNode cpn = * curnode;
 
- 		curnode = curnode->theNodeBefore;
 
- 		out.nodes.push_back(cpn);
 
- 	}
 
- 	return true;
 
- }
 
- int CPathsInfo::getDistance(const int3 & tile) const
 
- {
 
- 	boost::unique_lock<boost::mutex> pathLock(pathMx);
 
- 	CGPath ret;
 
- 	if(getPath(ret, tile))
 
- 		return ret.nodes.size();
 
- 	else
 
- 		return 255;
 
- }
 
- const CGPathNode * CPathsInfo::getNode(const int3 & coord) const
 
- {
 
- 	auto landNode = &nodes[coord.x][coord.y][coord.z][ELayer::LAND];
 
- 	if(landNode->reachable())
 
- 		return landNode;
 
- 	else
 
- 		return &nodes[coord.x][coord.y][coord.z][ELayer::SAIL];
 
- }
 
- CGPathNode * CPathsInfo::getNode(const int3 & coord, const ELayer layer)
 
- {
 
- 	return &nodes[coord.x][coord.y][coord.z][layer];
 
- }
 
 
  |