2017-07-13 10:26:03 +02:00
/*
* VCAI . 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
*
*/
2012-02-14 21:04:45 +03:00
# include "StdInc.h"
# include "VCAI.h"
2014-06-29 17:23:06 +03:00
# include "Fuzzy.h"
2018-07-26 12:06:55 +02:00
# include "ResourceManager.h"
2014-06-29 17:23:06 +03:00
2012-02-20 00:03:43 +03:00
# include "../../lib/UnlockGuard.h"
2014-06-05 19:52:14 +03:00
# include "../../lib/mapObjects/MapObjects.h"
2012-09-29 13:59:43 +03:00
# include "../../lib/CConfigHandler.h"
2012-11-16 00:29:22 +03:00
# include "../../lib/CHeroHandler.h"
2014-06-25 17:11:07 +03:00
# include "../../lib/CModHandler.h"
2015-12-02 21:39:53 +02:00
# include "../../lib/CGameState.h"
2015-12-02 22:10:46 +02:00
# include "../../lib/NetPacks.h"
2016-09-10 02:32:40 +02:00
# include "../../lib/serializer/CTypeList.h"
# include "../../lib/serializer/BinarySerializer.h"
# include "../../lib/serializer/BinaryDeserializer.h"
2014-04-26 17:23:35 +03:00
2018-07-26 12:06:55 +02:00
# include "AIhelper.h"
2018-04-07 10:44:14 +02:00
extern FuzzyHelper * fh ;
2012-03-03 13:08:01 +03:00
2012-03-05 22:11:28 +03:00
class CGVisitableOPW ;
2012-03-04 13:43:46 +03:00
const double SAFE_ATTACK_CONSTANT = 1.5 ;
2013-10-18 23:17:25 +03:00
2012-02-14 21:04:45 +03:00
//one thread may be turn of AI and another will be handling a side effect for AI2
boost : : thread_specific_ptr < CCallback > cb ;
boost : : thread_specific_ptr < VCAI > ai ;
2018-07-26 12:06:55 +02:00
extern boost : : thread_specific_ptr < AIhelper > ah ;
2012-03-03 13:08:01 +03:00
2012-07-01 02:48:40 +03:00
//std::map<int, std::map<int, int> > HeroView::infosCount;
2012-02-14 21:04:45 +03:00
//helper RAII to manage global ai/cb ptrs
struct SetGlobalState
{
SetGlobalState ( VCAI * AI )
{
assert ( ! ai . get ( ) ) ;
assert ( ! cb . get ( ) ) ;
ai . reset ( AI ) ;
2013-06-22 17:47:20 +03:00
cb . reset ( AI - > myCb . get ( ) ) ;
2018-07-26 12:06:55 +02:00
if ( ! ah . get ( ) )
ah . reset ( new AIhelper ( ) ) ;
ah - > setAI ( AI ) ; //does this make any sense?
ah - > setCB ( cb . get ( ) ) ;
2012-02-14 21:04:45 +03:00
}
~ SetGlobalState ( )
{
2018-07-26 12:06:55 +02:00
//TODO: how to handle rm? shouldn't be called after ai is destroyed, hopefully
//TODO: to ensure that, make rm unique_ptr
2012-02-14 21:04:45 +03:00
ai . release ( ) ;
cb . release ( ) ;
}
} ;
2012-04-17 15:46:21 +03:00
2012-02-14 21:04:45 +03:00
# define SET_GLOBAL_STATE(ai) SetGlobalState _hlpSetState(ai);
# define NET_EVENT_HANDLER SET_GLOBAL_STATE(this)
# define MAKING_TURN SET_GLOBAL_STATE(this)
2018-04-07 10:44:14 +02:00
void foreach_tile ( std : : vector < std : : vector < std : : vector < unsigned char > > > & vectors , std : : function < void ( unsigned char & in ) > foo )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
for ( auto & vector : vectors )
{
for ( auto j = vector . begin ( ) ; j ! = vector . end ( ) ; j + + )
{
for ( auto & elem : * j )
2013-06-29 16:05:48 +03:00
foo ( elem ) ;
2018-04-07 10:44:14 +02:00
}
}
2012-02-14 21:04:45 +03:00
}
struct ObjInfo
{
int3 pos ;
std : : string name ;
ObjInfo ( ) { }
2018-04-07 10:44:14 +02:00
ObjInfo ( const CGObjectInstance * obj )
: pos ( obj - > pos ) , name ( obj - > getObjectName ( ) )
2012-02-14 21:04:45 +03:00
{
}
} ;
std : : map < const CGObjectInstance * , ObjInfo > helperObjInfo ;
2018-01-13 10:43:26 +02:00
VCAI : : VCAI ( )
2012-03-03 13:08:01 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2013-05-22 01:11:44 +03:00
makingTurn = nullptr ;
2015-03-08 16:47:58 +02:00
destinationTeleport = ObjectInstanceID ( ) ;
2015-12-04 00:54:25 +02:00
destinationTeleportPos = int3 ( - 1 ) ;
2012-02-14 21:04:45 +03:00
}
2018-01-13 10:43:26 +02:00
VCAI : : ~ VCAI ( )
2012-03-03 13:08:01 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2016-10-22 16:22:00 +02:00
finish ( ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : availableCreaturesChanged ( const CGDwelling * town )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : heroMoved ( const TryMoveHero & details )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2013-04-21 15:11:13 +03:00
validateObject ( details . id ) ; //enemy hero may have left visible area
2016-09-14 02:44:35 +02:00
auto hero = cb - > getHero ( details . id ) ;
2015-10-26 17:38:17 +02:00
cachedSectorMaps . clear ( ) ;
2013-04-21 15:11:13 +03:00
2018-04-07 10:44:14 +02:00
const int3 from = CGHeroInstance : : convertPosition ( details . start , false ) ;
const int3 to = CGHeroInstance : : convertPosition ( details . end , false ) ;
const CGObjectInstance * o1 = vstd : : frontOrNull ( cb - > getVisitableObjs ( from ) ) ;
const CGObjectInstance * o2 = vstd : : frontOrNull ( cb - > getVisitableObjs ( to ) ) ;
2016-09-14 02:44:35 +02:00
2012-02-14 21:04:45 +03:00
if ( details . result = = TryMoveHero : : TELEPORTATION )
{
2015-03-08 16:47:58 +02:00
auto t1 = dynamic_cast < const CGTeleport * > ( o1 ) ;
auto t2 = dynamic_cast < const CGTeleport * > ( o2 ) ;
if ( t1 & & t2 )
2012-04-14 10:22:08 +03:00
{
2015-03-09 01:13:40 +02:00
if ( cb - > isTeleportChannelBidirectional ( t1 - > channel ) )
2015-03-08 16:47:58 +02:00
{
2015-03-09 01:27:49 +02:00
if ( o1 - > ID = = Obj : : SUBTERRANEAN_GATE & & o1 - > ID = = o2 - > ID ) // We need to only add subterranean gates in knownSubterraneanGates. Used for features not yet ported to use teleport channels
2015-03-08 16:47:58 +02:00
{
knownSubterraneanGates [ o1 ] = o2 ;
knownSubterraneanGates [ o2 ] = o1 ;
2017-08-11 19:03:05 +02:00
logAi - > debug ( " Found a pair of subterranean gates between %s and %s! " , from . toString ( ) , to . toString ( ) ) ;
2015-03-08 16:47:58 +02:00
}
}
2012-02-14 21:04:45 +03:00
}
2016-12-19 22:46:01 +02:00
//FIXME: teleports are not correctly visited
unreserveObject ( hero , t1 ) ;
unreserveObject ( hero , t2 ) ;
2012-02-14 21:04:45 +03:00
}
2016-09-14 02:44:35 +02:00
else if ( details . result = = TryMoveHero : : EMBARK & & hero )
{
//make sure AI not attempt to visit used boat
validateObject ( hero - > boat ) ;
}
else if ( details . result = = TryMoveHero : : DISEMBARK & & o1 )
{
auto boat = dynamic_cast < const CGBoat * > ( o1 ) ;
if ( boat )
addVisitableObj ( boat ) ;
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroInGarrisonChange ( const CGTownInstance * town )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : centerView ( int3 pos , int focusTime )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " focusTime '%i' " , focusTime ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : artifactMoved ( const ArtifactLocation & src , const ArtifactLocation & dst )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : artifactAssembled ( const ArtifactLocation & al )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showTavernWindow ( const CGObjectInstance * townOrTavern )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showThievesGuildWindow ( const CGObjectInstance * obj )
2012-03-07 17:05:54 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-07 17:05:54 +03:00
NET_EVENT_HANDLER ;
}
2013-09-28 02:46:58 +03:00
void VCAI : : playerBlocked ( int reason , bool start )
2012-02-14 21:04:45 +03:00
{
2013-09-28 02:46:58 +03:00
LOG_TRACE_PARAMS ( logAi , " reason '%i', start '%i' " , reason % start ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2018-04-07 10:44:14 +02:00
if ( start & & reason = = PlayerBlocked : : UPCOMING_BATTLE )
2012-02-14 21:04:45 +03:00
status . setBattle ( UPCOMING_BATTLE ) ;
2013-09-28 02:46:58 +03:00
if ( reason = = PlayerBlocked : : ONGOING_MOVEMENT )
status . setMove ( start ) ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : showPuzzleMap ( )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showShipyardDialog ( const IShipyard * obj )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2013-11-17 20:57:04 +03:00
void VCAI : : gameOver ( PlayerColor player , const EVictoryLossCheckResult & victoryLossCheckResult )
2012-02-14 21:04:45 +03:00
{
2014-06-26 02:18:59 +03:00
LOG_TRACE_PARAMS ( logAi , " victoryLossCheckResult '%s' " , victoryLossCheckResult . messageToSelf ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Player %d (%s): I heard that player %d (%s) %s. " , playerID , playerID . getStr ( ) , player , player . getStr ( ) , ( victoryLossCheckResult . victory ( ) ? " won " : " lost " ) ) ;
2012-02-14 21:04:45 +03:00
if ( player = = playerID )
{
2013-11-17 20:57:04 +03:00
if ( victoryLossCheckResult . victory ( ) )
2012-02-14 21:04:45 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " VCAI: I won! Incredible! " ) ;
logAi - > debug ( " Turn nr %d " , myCb - > getDate ( ) ) ;
2012-02-14 21:04:45 +03:00
}
else
{
2016-09-16 04:45:00 +02:00
logAi - > debug ( " VCAI: Player %d (%s) lost. It's me. What a disappointment! :( " , player , player . getStr ( ) ) ;
2012-02-14 21:04:45 +03:00
}
2012-02-22 16:41:27 +03:00
finish ( ) ;
2012-02-14 21:04:45 +03:00
}
}
2018-04-07 10:44:14 +02:00
void VCAI : : artifactPut ( const ArtifactLocation & al )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : artifactRemoved ( const ArtifactLocation & al )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : artifactDisassembled ( const ArtifactLocation & al )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroVisit ( const CGHeroInstance * visitor , const CGObjectInstance * visitedObj , bool start )
2012-02-14 21:04:45 +03:00
{
2014-06-24 20:39:36 +03:00
LOG_TRACE_PARAMS ( logAi , " start '%i'; obj '%s' " , start % ( visitedObj ? visitedObj - > getObjectName ( ) : std : : string ( " n/a " ) ) ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2016-11-29 19:11:52 +02:00
if ( start & & visitedObj ) //we can end visit with null object, anyway
2012-03-05 22:11:28 +03:00
{
2018-04-07 10:44:14 +02:00
markObjectVisited ( visitedObj ) ;
2014-02-23 23:37:33 +03:00
unreserveObject ( visitor , visitedObj ) ;
2018-04-07 10:44:14 +02:00
completeGoal ( sptr ( Goals : : GetObj ( visitedObj - > id . getNum ( ) ) . sethero ( visitor ) ) ) ; //we don't need to visit it anymore
2013-11-24 22:15:08 +03:00
//TODO: what if we visited one-time visitable object that was reserved by another hero (shouldn't, but..)
2012-03-05 22:11:28 +03:00
}
2013-09-28 02:46:58 +03:00
status . heroVisit ( visitedObj , start ) ;
2012-02-14 21:04:45 +03:00
}
2017-07-15 13:08:20 +02:00
void VCAI : : availableArtifactsChanged ( const CGBlackMarket * bm )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroVisitsTown ( const CGHeroInstance * hero , const CGTownInstance * town )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
//buildArmyIn(town);
//moveCreaturesToHero(town);
}
2018-04-07 10:44:14 +02:00
void VCAI : : tileHidden ( const std : : unordered_set < int3 , ShashInt3 > & pos )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2013-04-21 15:11:13 +03:00
validateVisitableObjs ( ) ;
2016-08-18 02:51:00 +02:00
clearPathsInfo ( ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : tileRevealed ( const std : : unordered_set < int3 , ShashInt3 > & pos )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2013-06-29 16:05:48 +03:00
for ( int3 tile : pos )
2018-04-07 10:44:14 +02:00
{
for ( const CGObjectInstance * obj : myCb - > getVisitableObjs ( tile ) )
2012-02-14 21:04:45 +03:00
addVisitableObj ( obj ) ;
2018-04-07 10:44:14 +02:00
}
2014-03-23 19:00:43 +03:00
2015-10-26 17:38:17 +02:00
clearPathsInfo ( ) ;
2012-02-14 21:04:45 +03:00
}
2013-05-27 13:53:28 +03:00
void VCAI : : heroExchangeStarted ( ObjectInstanceID hero1 , ObjectInstanceID hero2 , QueryID query )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-08-28 18:04:05 +03:00
auto firstHero = cb - > getHero ( hero1 ) ;
auto secondHero = cb - > getHero ( hero2 ) ;
2016-09-14 12:34:22 +02:00
status . addQuery ( query , boost : : str ( boost : : format ( " Exchange between heroes %s (%d) and %s (%d) " ) % firstHero - > name % firstHero - > tempOwner % secondHero - > name % secondHero - > tempOwner ) ) ;
2013-05-27 13:53:28 +03:00
2012-08-28 18:04:05 +03:00
requestActionASAP ( [ = ] ( )
{
2014-02-17 10:36:03 +03:00
float goalpriority1 = 0 , goalpriority2 = 0 ;
auto firstGoal = getGoal ( firstHero ) ;
2018-04-07 10:44:14 +02:00
if ( firstGoal - > goalType = = Goals : : GATHER_ARMY )
2014-02-17 10:36:03 +03:00
goalpriority1 = firstGoal - > priority ;
auto secondGoal = getGoal ( secondHero ) ;
2018-04-07 10:44:14 +02:00
if ( secondGoal - > goalType = = Goals : : GATHER_ARMY )
2014-02-17 10:36:03 +03:00
goalpriority2 = secondGoal - > priority ;
2018-04-07 10:44:14 +02:00
auto transferFrom2to1 = [ this ] ( const CGHeroInstance * h1 , const CGHeroInstance * h2 ) - > void
2015-04-07 22:48:35 +02:00
{
this - > pickBestCreatures ( h1 , h2 ) ;
this - > pickBestArtifacts ( h1 , h2 ) ;
} ;
2016-09-14 12:34:22 +02:00
//Do not attempt army or artifacts exchange if we visited ally player
//Visits can still be useful if hero have skills like Scholar
if ( firstHero - > tempOwner ! = secondHero - > tempOwner )
2018-04-07 10:44:14 +02:00
{
2016-09-14 12:34:22 +02:00
logAi - > debug ( " Heroes owned by different players. Do not exchange army or artifacts. " ) ;
2018-04-07 10:44:14 +02:00
}
2016-09-14 12:34:22 +02:00
else if ( goalpriority1 > goalpriority2 )
2018-04-07 10:44:14 +02:00
{
transferFrom2to1 ( firstHero , secondHero ) ;
}
2016-09-14 12:34:22 +02:00
else if ( goalpriority1 < goalpriority2 )
2018-04-07 10:44:14 +02:00
{
transferFrom2to1 ( secondHero , firstHero ) ;
}
2014-02-17 10:36:03 +03:00
else //regular criteria
{
2018-04-07 10:44:14 +02:00
if ( firstHero - > getFightingStrength ( ) > secondHero - > getFightingStrength ( ) & & canGetArmy ( firstHero , secondHero ) )
transferFrom2to1 ( firstHero , secondHero ) ;
else if ( canGetArmy ( secondHero , firstHero ) )
transferFrom2to1 ( secondHero , firstHero ) ;
2015-04-07 08:43:10 +02:00
}
2012-08-28 18:04:05 +03:00
2015-04-07 08:43:10 +02:00
completeGoal ( sptr ( Goals : : VisitHero ( firstHero - > id . getNum ( ) ) ) ) ; //TODO: what if we were visited by other hero in the meantime?
completeGoal ( sptr ( Goals : : VisitHero ( secondHero - > id . getNum ( ) ) ) ) ;
2013-05-27 13:53:28 +03:00
answerQuery ( query , 0 ) ;
2012-08-28 18:04:05 +03:00
} ) ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : heroPrimarySkillChanged ( const CGHeroInstance * hero , int which , si64 val )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " which '%i', val '%i' " , which % val ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showRecruitmentDialog ( const CGDwelling * dwelling , const CArmedInstance * dst , int level )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " level '%i' " , level ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : heroMovePointsChanged ( const CGHeroInstance * hero )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-03-10 23:19:36 +02:00
void VCAI : : garrisonsChanged ( ObjectInstanceID id1 , ObjectInstanceID id2 )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : newObject ( const CGObjectInstance * obj )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
if ( obj - > isVisitable ( ) )
addVisitableObj ( obj ) ;
2014-03-23 19:00:43 +03:00
2015-10-26 17:38:17 +02:00
cachedSectorMaps . clear ( ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : objectRemoved ( const CGObjectInstance * obj )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-07-01 02:48:40 +03:00
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( visitableObjs , obj ) ;
vstd : : erase_if_present ( alreadyVisited , obj ) ;
2013-05-09 14:09:23 +03:00
2018-04-07 10:44:14 +02:00
for ( auto h : cb - > getHeroesInfo ( ) )
2014-03-30 00:39:19 +03:00
unreserveObject ( h , obj ) ;
2012-04-17 15:46:21 +03:00
2016-09-04 14:44:42 +02:00
//TODO: Find better way to handle hero boat removal
if ( auto hero = dynamic_cast < const CGHeroInstance * > ( obj ) )
{
if ( hero - > boat )
{
vstd : : erase_if_present ( visitableObjs , hero - > boat ) ;
vstd : : erase_if_present ( alreadyVisited , hero - > boat ) ;
2016-09-14 02:44:35 +02:00
2018-04-07 10:44:14 +02:00
for ( auto h : cb - > getHeroesInfo ( ) )
2016-09-14 02:44:35 +02:00
unreserveObject ( h , hero - > boat ) ;
2016-09-04 14:44:42 +02:00
}
}
2015-10-26 17:38:17 +02:00
cachedSectorMaps . clear ( ) ; //invalidate all paths
2012-04-17 15:46:21 +03:00
//TODO
//there are other places where CGObjectinstance ptrs are stored...
2012-07-19 21:52:44 +03:00
//
2012-07-01 02:48:40 +03:00
2018-04-07 10:44:14 +02:00
if ( obj - > ID = = Obj : : HERO & & obj - > tempOwner = = playerID )
2012-07-01 02:48:40 +03:00
{
2013-04-21 15:11:13 +03:00
lostHero ( cb - > getHero ( obj - > id ) ) ; //we can promote, since objectRemoved is called just before actual deletion
2012-07-01 02:48:40 +03:00
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showHillFortWindow ( const CGObjectInstance * object , const CGHeroInstance * visitor )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-08-29 12:19:20 +03:00
requestActionASAP ( [ = ] ( )
{
makePossibleUpgrades ( visitor ) ;
} ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : playerBonusChanged ( const Bonus & bonus , bool gain )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " gain '%i' " , gain ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroCreated ( const CGHeroInstance * h )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2018-04-07 10:44:14 +02:00
if ( h - > visitedTown )
2013-12-25 16:38:20 +03:00
townVisitsThisWeek [ HeroPtr ( h ) ] . insert ( h - > visitedTown ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : advmapSpellCast ( const CGHeroInstance * caster , int spellID )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " spellID '%i " , spellID ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showInfoDialog ( const std : : string & text , const std : : vector < Component > & components , int soundID )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " soundID '%i' " , soundID ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : requestRealized ( PackageApplied * pa )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
if ( status . haveTurn ( ) )
{
if ( pa - > packType = = typeList . getTypeID < EndTurn > ( ) )
2018-04-07 10:44:14 +02:00
{
2012-02-14 21:04:45 +03:00
if ( pa - > result )
status . madeTurn ( ) ;
2018-04-07 10:44:14 +02:00
}
2012-02-14 21:04:45 +03:00
}
if ( pa - > packType = = typeList . getTypeID < QueryReply > ( ) )
{
2012-07-15 18:34:00 +03:00
status . receivedAnswerConfirmation ( pa - > requestID , pa - > result ) ;
2012-02-14 21:04:45 +03:00
}
}
2016-11-26 14:14:43 +02:00
void VCAI : : receivedResource ( )
2012-02-14 21:04:45 +03:00
{
2016-11-26 14:14:43 +02:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showUniversityWindow ( const IMarket * market , const CGHeroInstance * visitor )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : heroManaPointsChanged ( const CGHeroInstance * hero )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : heroSecondarySkillChanged ( const CGHeroInstance * hero , int which , int val )
{
2013-04-20 14:34:01 +03:00
LOG_TRACE_PARAMS ( logAi , " which '%d', val '%d' " , which % val ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : battleResultsApplied ( )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
assert ( status . getBattle ( ) = = ENDING_BATTLE ) ;
status . setBattle ( NO_BATTLE ) ;
}
void VCAI : : objectPropertyChanged ( const SetObjectProperty * sop )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
if ( sop - > what = = ObjProperty : : OWNER )
{
2016-09-13 22:39:32 +02:00
if ( myCb - > getPlayerRelations ( playerID , ( PlayerColor ) sop - > val ) = = PlayerRelations : : ENEMIES )
2015-01-24 22:38:22 +02:00
{
//we want to visit objects owned by oppponents
auto obj = myCb - > getObj ( sop - > id , false ) ;
2018-04-07 10:44:14 +02:00
if ( obj )
2015-01-24 22:38:22 +02:00
{
2016-09-13 22:39:32 +02:00
addVisitableObj ( obj ) ; // TODO: Remove once save compatability broken. In past owned objects were removed from this set
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( alreadyVisited , obj ) ;
2015-01-24 22:38:22 +02:00
}
}
2012-02-14 21:04:45 +03:00
}
}
2018-04-07 10:44:14 +02:00
void VCAI : : buildChanged ( const CGTownInstance * town , BuildingID buildingID , int what )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " what '%i' " , what ) ;
2018-07-26 12:06:55 +02:00
if ( town - > getOwner ( ) = = playerID & & what = = 1 ) //built
completeGoal ( sptr ( Goals : : BuildThis ( buildingID , town ) ) ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroBonusChanged ( const CGHeroInstance * hero , const Bonus & bonus , bool gain )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " gain '%i' " , gain ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showMarketWindow ( const IMarket * market , const CGHeroInstance * visitor )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
}
2015-02-26 16:15:17 +02:00
void VCAI : : showWorldViewEx ( const std : : vector < ObjectPosInfo > & objectPositions )
{
//TODO: AI support for ViewXXX spell
LOG_TRACE ( logAi ) ;
NET_EVENT_HANDLER ;
}
2015-12-29 04:43:33 +02:00
void VCAI : : init ( std : : shared_ptr < CCallback > CB )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-02-14 21:04:45 +03:00
myCb = CB ;
cbc = CB ;
2018-07-26 12:06:55 +02:00
ah . reset ( new AIhelper ( ) ) ;
NET_EVENT_HANDLER ; //sets ah->rm->cb
2013-02-11 17:42:09 +03:00
playerID = * myCb - > getMyColor ( ) ;
2012-02-14 21:04:45 +03:00
myCb - > waitTillRealize = true ;
2012-02-17 00:46:28 +03:00
myCb - > unlockGsWhenWaiting = true ;
2012-02-16 20:10:58 +03:00
2012-03-10 22:14:45 +03:00
if ( ! fh )
fh = new FuzzyHelper ( ) ;
2018-02-10 20:52:23 +02:00
retrieveVisitableObjs ( ) ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : yourTurn ( )
{
2013-04-16 16:16:58 +03:00
LOG_TRACE ( logAi ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
status . startedTurn ( ) ;
2013-05-09 14:09:23 +03:00
makingTurn = make_unique < boost : : thread > ( & VCAI : : makeTurn , this ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : heroGotLevel ( const CGHeroInstance * hero , PrimarySkill : : PrimarySkill pskill , std : : vector < SecondarySkill > & skills , QueryID queryID )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " queryID '%i' " , queryID ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-07-15 18:34:00 +03:00
status . addQuery ( queryID , boost : : str ( boost : : format ( " Hero %s got level %d " ) % hero - > name % hero - > level ) ) ;
2017-07-17 14:35:57 +02:00
requestActionASAP ( [ = ] ( ) { answerQuery ( queryID , 0 ) ; } ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : commanderGotLevel ( const CCommanderInstance * commander , std : : vector < ui32 > skills , QueryID queryID )
2012-07-06 17:09:34 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " queryID '%i' " , queryID ) ;
2012-07-06 17:09:34 +03:00
NET_EVENT_HANDLER ;
2012-07-15 18:34:00 +03:00
status . addQuery ( queryID , boost : : str ( boost : : format ( " Commander %s of %s got level %d " ) % commander - > name % commander - > armyObj - > nodeName ( ) % ( int ) commander - > level ) ) ;
2017-07-17 14:35:57 +02:00
requestActionASAP ( [ = ] ( ) { answerQuery ( queryID , 0 ) ; } ) ;
2012-07-06 17:09:34 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : showBlockingDialog ( const std : : string & text , const std : : vector < Component > & components , QueryID askID , const int soundID , bool selection , bool cancel )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " text '%s', askID '%i', soundID '%i', selection '%i', cancel '%i' " , text % askID % soundID % selection % cancel ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
int sel = 0 ;
2012-07-19 21:52:44 +03:00
status . addQuery ( askID , boost : : str ( boost : : format ( " Blocking dialog query with %d components - %s " )
2012-07-15 18:34:00 +03:00
% components . size ( ) % text ) ) ;
2012-02-14 21:04:45 +03:00
if ( selection ) //select from multiple components -> take the last one (they're indexed [1-size])
sel = components . size ( ) ;
if ( ! selection & & cancel ) //yes&no -> always answer yes, we are a brave AI :)
sel = 1 ;
2012-03-26 14:38:51 +03:00
requestActionASAP ( [ = ] ( )
2012-03-26 01:46:14 +03:00
{
2012-07-15 18:34:00 +03:00
answerQuery ( askID , sel ) ;
2012-03-26 01:46:14 +03:00
} ) ;
2012-02-14 21:04:45 +03:00
}
2015-11-28 01:41:30 +02:00
void VCAI : : showTeleportDialog ( TeleportChannelID channel , TTeleportExitsList exits , bool impassable , QueryID askID )
2015-03-08 16:47:58 +02:00
{
2015-12-02 16:56:26 +02:00
// LOG_TRACE_PARAMS(logAi, "askID '%i', exits '%s'", askID % exits);
2015-03-08 16:47:58 +02:00
NET_EVENT_HANDLER ;
status . addQuery ( askID , boost : : str ( boost : : format ( " Teleport dialog query with %d exits " )
% exits . size ( ) ) ) ;
2015-12-02 16:56:26 +02:00
int choosenExit = - 1 ;
2015-03-08 16:47:58 +02:00
if ( impassable )
2018-04-07 10:44:14 +02:00
{
2015-03-08 16:47:58 +02:00
knownTeleportChannels [ channel ] - > passability = TeleportChannel : : IMPASSABLE ;
2018-04-07 10:44:14 +02:00
}
2015-12-03 16:20:03 +02:00
else if ( destinationTeleport ! = ObjectInstanceID ( ) & & destinationTeleportPos . valid ( ) )
2015-03-08 16:47:58 +02:00
{
2015-12-02 16:56:26 +02:00
auto neededExit = std : : make_pair ( destinationTeleport , destinationTeleportPos ) ;
if ( destinationTeleport ! = ObjectInstanceID ( ) & & vstd : : contains ( exits , neededExit ) )
choosenExit = vstd : : find_pos ( exits , neededExit ) ;
2015-12-03 16:20:03 +02:00
}
2015-03-08 16:47:58 +02:00
2015-12-03 16:20:03 +02:00
for ( auto exit : exits )
{
if ( status . channelProbing ( ) & & exit . first = = destinationTeleport )
2015-03-08 16:47:58 +02:00
{
2015-12-03 16:20:03 +02:00
choosenExit = vstd : : find_pos ( exits , exit ) ;
break ;
}
else
{
2015-12-03 20:18:40 +02:00
// TODO: Implement checking if visiting that teleport will uncovert any FoW
// So far this is the best option to handle decision about probing
auto obj = cb - > getObj ( exit . first , false ) ;
2018-04-07 10:44:14 +02:00
if ( obj = = nullptr & & ! vstd : : contains ( teleportChannelProbingList , exit . first ) )
2015-03-08 16:47:58 +02:00
{
2018-04-07 10:44:14 +02:00
if ( exit . first ! = destinationTeleport )
teleportChannelProbingList . push_back ( exit . first ) ;
2015-12-02 18:26:24 +02:00
}
2015-03-08 16:47:58 +02:00
}
}
requestActionASAP ( [ = ] ( )
{
2015-12-02 16:56:26 +02:00
answerQuery ( askID , choosenExit ) ;
2015-03-08 16:47:58 +02:00
} ) ;
}
2018-04-07 10:44:14 +02:00
void VCAI : : showGarrisonDialog ( const CArmedInstance * up , const CGHeroInstance * down , bool removableUnits , QueryID queryID )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " removableUnits '%i', queryID '%i' " , removableUnits % queryID ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2012-07-15 18:34:00 +03:00
std : : string s1 = up ? up - > nodeName ( ) : " NONE " ;
std : : string s2 = down ? down - > nodeName ( ) : " NONE " ;
status . addQuery ( queryID , boost : : str ( boost : : format ( " Garrison dialog with %s and %s " ) % s1 % s2 ) ) ;
2012-03-11 19:29:01 +03:00
//you can't request action from action-response thread
2012-04-08 13:34:23 +03:00
requestActionASAP ( [ = ] ( )
2012-03-26 01:46:14 +03:00
{
2016-09-19 02:30:55 +02:00
if ( removableUnits )
pickBestCreatures ( down , up ) ;
2012-07-15 18:34:00 +03:00
answerQuery ( queryID , 0 ) ;
2012-03-26 01:46:14 +03:00
} ) ;
2012-02-14 21:04:45 +03:00
}
2017-06-06 06:53:51 +02:00
void VCAI : : showMapObjectSelectDialog ( QueryID askID , const Component & icon , const MetaString & title , const MetaString & description , const std : : vector < ObjectInstanceID > & objects )
{
status . addQuery ( askID , " Map object select query " ) ;
2017-07-17 14:35:57 +02:00
requestActionASAP ( [ = ] ( ) { answerQuery ( askID , 0 ) ; } ) ;
2017-06-06 06:53:51 +02:00
//TODO: Town portal destination selection goes here
}
2016-09-10 02:32:40 +02:00
void VCAI : : saveGame ( BinarySerializer & h , const int version )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " version '%i' " , version ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2013-08-18 18:46:28 +03:00
validateVisitableObjs ( ) ;
2014-02-20 23:18:49 +03:00
2018-02-25 09:31:17 +02:00
#if 0
//disabled due to issue 2890
2014-02-20 23:18:49 +03:00
registerGoals ( h ) ;
2018-02-25 09:31:17 +02:00
# endif // 0
2013-05-09 14:09:23 +03:00
CAdventureAI : : saveGame ( h , version ) ;
serializeInternal ( h , version ) ;
2012-02-14 21:04:45 +03:00
}
2016-09-10 02:32:40 +02:00
void VCAI : : loadGame ( BinaryDeserializer & h , const int version )
2012-02-14 21:04:45 +03:00
{
2013-04-16 16:16:58 +03:00
LOG_TRACE_PARAMS ( logAi , " version '%i' " , version ) ;
2012-03-03 13:08:01 +03:00
NET_EVENT_HANDLER ;
2014-02-20 23:18:49 +03:00
2018-02-25 09:31:17 +02:00
#if 0
//disabled due to issue 2890
2014-02-20 23:18:49 +03:00
registerGoals ( h ) ;
2018-02-25 09:31:17 +02:00
# endif // 0
2013-05-09 14:09:23 +03:00
CAdventureAI : : loadGame ( h , version ) ;
serializeInternal ( h , version ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void makePossibleUpgrades ( const CArmedInstance * obj )
2012-02-14 21:04:45 +03:00
{
if ( ! obj )
return ;
for ( int i = 0 ; i < GameConstants : : ARMY_SIZE ; i + + )
{
2018-04-07 10:44:14 +02:00
if ( const CStackInstance * s = obj - > getStackPtr ( SlotID ( i ) ) )
2012-02-14 21:04:45 +03:00
{
UpgradeInfo ui ;
2013-02-16 17:03:47 +03:00
cb - > getUpgradeInfo ( obj , SlotID ( i ) , ui ) ;
2012-02-14 21:04:45 +03:00
if ( ui . oldID > = 0 & & cb - > getResourceAmount ( ) . canAfford ( ui . cost [ 0 ] * s - > count ) )
{
2013-02-16 17:03:47 +03:00
cb - > upgradeCreature ( obj , SlotID ( i ) , ui . newID [ 0 ] ) ;
2012-02-14 21:04:45 +03:00
}
}
}
}
void VCAI : : makeTurn ( )
{
2016-09-16 04:45:00 +02:00
logGlobal - > info ( " Player %d (%s) starting turn " , playerID , playerID . getStr ( ) ) ;
2015-08-31 09:18:24 +02:00
2012-02-14 21:04:45 +03:00
MAKING_TURN ;
2017-06-14 06:59:41 +02:00
boost : : shared_lock < boost : : shared_mutex > gsLock ( CGameState : : mutex ) ;
2012-06-27 23:44:01 +03:00
setThreadName ( " VCAI::makeTurn " ) ;
2012-02-14 21:04:45 +03:00
2013-02-02 11:29:57 +03:00
switch ( cb - > getDate ( Date : : DAY_OF_WEEK ) )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
case 1 :
{
townVisitsThisWeek . clear ( ) ;
std : : vector < const CGObjectInstance * > objs ;
retrieveVisitableObjs ( objs , true ) ;
for ( const CGObjectInstance * obj : objs )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( isWeeklyRevisitable ( obj ) )
2012-03-13 15:47:47 +03:00
{
2018-04-07 10:44:14 +02:00
addVisitableObj ( obj ) ;
vstd : : erase_if_present ( alreadyVisited , obj ) ;
2012-03-13 15:47:47 +03:00
}
}
2018-04-07 10:44:14 +02:00
break ;
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
markHeroAbleToExplore ( primaryHero ( ) ) ;
2014-03-23 19:00:43 +03:00
2012-02-14 21:04:45 +03:00
makeTurnInternal ( ) ;
}
2018-07-23 20:48:38 +02:00
/*This method defines core of AI behavior. It is not consistent system, just "a bit of everything for everyone" done in some example order.
It is not supposed to work this way in final version of VCAI . It consists of few actions / loops done in particular order , hard parts are explained below with focus on explaining hero management logic */
2012-02-14 21:04:45 +03:00
void VCAI : : makeTurnInternal ( )
{
2012-03-11 04:26:11 +03:00
//it looks messy here, but it's better to have armed heroes before attempting realizing goals
2018-04-07 10:44:14 +02:00
for ( const CGTownInstance * t : cb - > getTownsInfo ( ) )
2012-03-11 04:26:11 +03:00
moveCreaturesToHero ( t ) ;
2012-02-14 21:04:45 +03:00
try
{
2018-07-23 20:48:38 +02:00
/*Below loop causes heroes with objects locked to them to keep trying to realize previous goal. By design when object is locked another heroes do not attempt to visit it.
Object lock happens on turn when some hero gets assigned visit tile with appropiate map object . So basically all heroes that had VisitTile goal with object assigned and not completed
will be in the loop . Sometimes heroes get assigned more important objectives , but still keep reserved objects for later . There is a problem with that - they have
reserved objects in the list , so they fall to this loop at start of turn and visiting object isn ' t delayed . Comments for that function are supposed to help newer VCAI maintainers */
//Pick objects reserved in previous turn - we expect only nearby objects there
2012-09-28 22:50:09 +03:00
auto reservedHeroesCopy = reservedHeroesMap ; //work on copy => the map may be changed while iterating (eg because hero died when attempting a goal)
2018-04-07 10:44:14 +02:00
for ( auto hero : reservedHeroesCopy )
2012-05-08 11:10:40 +03:00
{
2013-09-28 15:31:06 +03:00
if ( reservedHeroesMap . count ( hero . first ) )
continue ; //hero might have been removed while we were in this loop
if ( ! hero . first . validAndSet ( ) )
{
2016-08-15 07:30:06 +02:00
logAi - > error ( " Hero %s present on reserved map. Shouldn't be. " , hero . first . name ) ;
2013-09-28 15:31:06 +03:00
continue ;
}
2013-12-25 16:38:20 +03:00
std : : vector < const CGObjectInstance * > vec ( hero . second . begin ( ) , hero . second . end ( ) ) ;
2018-04-07 10:44:14 +02:00
boost : : sort ( vec , CDistanceSorter ( hero . first . get ( ) ) ) ;
for ( auto obj : vec )
2012-05-08 11:10:40 +03:00
{
2014-01-03 02:48:38 +03:00
if ( ! obj | | ! cb - > getObj ( obj - > id ) )
2013-08-18 18:46:28 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > error ( " Error: there is wrong object on list for hero %s " , hero . first - > name ) ;
2013-08-18 18:46:28 +03:00
continue ;
}
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : VisitTile ( obj - > visitablePos ( ) ) . sethero ( hero . first ) ) ) ;
2012-05-08 11:10:40 +03:00
}
}
//now try to win
2018-07-23 20:48:38 +02:00
/*below line performs goal decomposition, result of the function is ONE goal for ONE hero to realize.*/
2013-11-23 15:30:10 +03:00
striveToGoal ( sptr ( Goals : : Win ( ) ) ) ;
2012-05-05 09:42:18 +03:00
2018-07-26 12:06:55 +02:00
//TODO: add ResourceManager goals to the pool and process them all at once
if ( ah - > hasTasksLeft ( ) )
striveToGoal ( ah - > whatToDo ( ) ) ;
2018-07-23 20:48:38 +02:00
/*Explanation of below loop: At the time of writing this - goals get decomposited either to GatherArmy or Visit Tile.
Visit tile that is about visiting object gets processed at beginning of MakeTurnInternal without re - evaluation .
Rest of goals that got started via striveToGoal ( sptr ( Goals : : Win ( ) ) ) ; in previous turns and not finished get continued here .
Also they are subject for re - evaluation to see if there is better goal to start ( still talking only about heroes that got goals started by via striveToGoal ( sptr ( Goals : : Win ( ) ) ) ; in previous turns . */
2012-07-01 02:48:40 +03:00
//finally, continue our abstract long-term goals
2013-12-26 12:53:37 +03:00
int oldMovement = 0 ;
int newMovement = 0 ;
2018-04-07 10:44:14 +02:00
while ( true )
2012-03-13 15:47:47 +03:00
{
2013-12-26 12:53:37 +03:00
oldMovement = newMovement ; //remember old value
newMovement = 0 ;
2018-04-07 10:44:14 +02:00
std : : vector < std : : pair < HeroPtr , Goals : : TSubgoal > > safeCopy ;
for ( auto mission : lockedHeroes )
2013-12-26 12:53:37 +03:00
{
2018-04-07 10:44:14 +02:00
fh - > setPriority ( mission . second ) ; //re-evaluate
if ( canAct ( mission . first ) )
2013-12-26 12:53:37 +03:00
{
newMovement + = mission . first - > movement ;
2018-04-07 10:44:14 +02:00
safeCopy . push_back ( mission ) ;
2013-12-26 12:53:37 +03:00
}
}
2018-04-07 10:44:14 +02:00
if ( newMovement = = oldMovement ) //means our heroes didn't move or didn't re-assign their goals
2012-05-05 11:57:32 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > warn ( " Our heroes don't move anymore, exhaustive decomposition failed " ) ;
2013-12-26 12:53:37 +03:00
break ;
2014-01-30 14:08:37 +03:00
}
2018-04-07 10:44:14 +02:00
if ( safeCopy . empty ( ) )
{
2013-12-26 12:53:37 +03:00
break ; //all heroes exhausted their locked goals
2018-04-07 10:44:14 +02:00
}
2013-12-26 12:53:37 +03:00
else
{
typedef std : : pair < HeroPtr , Goals : : TSubgoal > TItrType ;
auto lockedHeroesSorter = [ ] ( TItrType m1 , TItrType m2 ) - > bool
{
return m1 . second - > priority < m2 . second - > priority ;
} ;
2018-07-19 17:36:10 +02:00
striveToGoal ( boost : : max_element ( safeCopy , lockedHeroesSorter ) - > second ) ;
2012-05-05 11:57:32 +03:00
}
2012-03-13 15:47:47 +03:00
}
2012-05-05 09:42:18 +03:00
2012-07-18 13:10:14 +03:00
auto quests = myCb - > getMyQuests ( ) ;
2018-04-07 10:44:14 +02:00
for ( auto quest : quests )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToQuest ( quest ) ;
2012-07-18 13:10:14 +03:00
}
2018-07-26 12:06:55 +02:00
//TODO: striveToGoal
2013-11-23 15:30:10 +03:00
striveToGoal ( sptr ( Goals : : Build ( ) ) ) ; //TODO: smarter building management
2018-07-23 20:48:38 +02:00
/*Below function is also responsible for hero movement via internal wander function. By design it is separate logic for heroes that have nothing to do.
Heroes that were not picked by striveToGoal ( sptr ( Goals : : Win ( ) ) ) ; recently ( so they do not have new goals and cannot continue / reevaluate previously locked goals ) will do logic in wander ( ) . */
2013-12-26 12:53:37 +03:00
performTypicalActions ( ) ;
//for debug purpose
2018-04-07 10:44:14 +02:00
for ( auto h : cb - > getHeroesInfo ( ) )
2013-12-26 12:53:37 +03:00
{
2018-04-07 10:44:14 +02:00
if ( h - > movement )
2016-08-15 07:30:06 +02:00
logAi - > warn ( " Hero %s has %d MP left " , h - > name , h - > movement ) ;
2015-10-31 17:04:06 +02:00
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
catch ( boost : : thread_interrupted & e )
2012-02-14 21:04:45 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Making turn thread has been interrupted. We'll end without calling endTurn. " ) ;
2012-02-14 21:04:45 +03:00
return ;
}
2018-04-07 10:44:14 +02:00
catch ( std : : exception & e )
2012-02-14 21:04:45 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Making turn thread has caught an exception: %s " , e . what ( ) ) ;
2012-02-14 21:04:45 +03:00
}
endTurn ( ) ;
}
2012-07-01 02:48:40 +03:00
bool VCAI : : goVisitObj ( const CGObjectInstance * obj , HeroPtr h )
2012-02-14 21:04:45 +03:00
{
int3 dst = obj - > visitablePos ( ) ;
2015-12-04 00:10:51 +02:00
auto sm = getCachedSectorMap ( h ) ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " %s will try to visit %s at (%s) " , h - > name , obj - > getObjectName ( ) , dst . toString ( ) ) ;
2015-12-04 00:10:51 +02:00
int3 pos = sm - > firstTileToGet ( h , dst ) ;
2018-04-07 10:44:14 +02:00
if ( ! pos . valid ( ) ) //rare case when we are already standing on one of potential objects
2015-04-05 21:13:47 +02:00
return false ;
return moveHeroToTile ( pos , h ) ;
2012-02-14 21:04:45 +03:00
}
2012-07-01 02:48:40 +03:00
void VCAI : : performObjectInteraction ( const CGObjectInstance * obj , HeroPtr h )
2012-03-05 22:11:28 +03:00
{
2017-08-11 19:03:05 +02:00
LOG_TRACE_PARAMS ( logAi , " Hero %s and object %s at %s " , h - > name % obj - > getObjectName ( ) % obj - > pos . toString ( ) ) ;
2018-04-07 10:44:14 +02:00
switch ( obj - > ID )
2012-03-05 22:11:28 +03:00
{
2018-04-07 10:44:14 +02:00
case Obj : : CREATURE_GENERATOR1 :
recruitCreatures ( dynamic_cast < const CGDwelling * > ( obj ) , h . get ( ) ) ;
checkHeroArmy ( h ) ;
break ;
case Obj : : TOWN :
moveCreaturesToHero ( dynamic_cast < const CGTownInstance * > ( obj ) ) ;
if ( h - > visitedTown ) //we are inside, not just attacking
{
townVisitsThisWeek [ h ] . insert ( h - > visitedTown ) ;
2018-07-26 12:06:55 +02:00
if ( ! h - > hasSpellbook ( ) & & ah - > freeGold ( ) > = GameConstants : : SPELLBOOK_GOLD_COST )
2012-10-01 21:25:43 +03:00
{
2018-04-07 10:44:14 +02:00
if ( h - > visitedTown - > hasBuilt ( BuildingID : : MAGES_GUILD_1 ) )
2013-02-07 20:34:50 +03:00
cb - > buyArtifact ( h . get ( ) , ArtifactID : : SPELLBOOK ) ;
2012-10-01 21:25:43 +03:00
}
2018-04-07 10:44:14 +02:00
}
break ;
2012-03-05 22:11:28 +03:00
}
2018-04-07 10:44:14 +02:00
completeGoal ( sptr ( Goals : : GetObj ( obj - > id . getNum ( ) ) . sethero ( h ) ) ) ;
2012-03-05 22:11:28 +03:00
}
2012-02-14 21:04:45 +03:00
void VCAI : : moveCreaturesToHero ( const CGTownInstance * t )
{
2013-08-18 18:46:28 +03:00
if ( t - > visitingHero & & t - > armedGarrison ( ) & & t - > visitingHero - > tempOwner = = t - > tempOwner )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
pickBestCreatures ( t - > visitingHero , t ) ;
2012-03-06 21:49:23 +03:00
}
}
2018-04-07 10:44:14 +02:00
bool VCAI : : canGetArmy ( const CGHeroInstance * army , const CGHeroInstance * source )
{
//TODO: merge with pickBestCreatures
2014-02-19 19:23:47 +03:00
//if (ai->primaryHero().h == source)
2013-08-18 18:46:28 +03:00
if ( army - > tempOwner ! = source - > tempOwner )
{
2016-08-15 07:30:06 +02:00
logAi - > error ( " Why are we even considering exchange between heroes from different players? " ) ;
2013-08-18 18:46:28 +03:00
return false ;
}
2018-04-07 10:44:14 +02:00
const CArmedInstance * armies [ ] = { army , source } ;
2015-10-31 17:04:06 +02:00
2012-08-29 18:23:47 +03:00
//we calculate total strength for each creature type available in armies
2018-04-07 10:44:14 +02:00
std : : map < const CCreature * , int > creToPower ;
2013-06-29 16:05:48 +03:00
for ( auto armyPtr : armies )
2018-04-07 10:44:14 +02:00
{
for ( auto & i : armyPtr - > Slots ( ) )
2012-08-29 12:19:20 +03:00
{
2014-02-19 19:23:47 +03:00
//TODO: allow splitting stacks?
2012-08-29 18:23:47 +03:00
creToPower [ i . second - > type ] + = i . second - > getPower ( ) ;
2012-08-29 12:19:20 +03:00
}
2018-04-07 10:44:14 +02:00
}
2012-08-29 18:23:47 +03:00
//TODO - consider more than just power (ie morale penalty, hero specialty in certain stacks, etc)
2014-02-19 19:23:47 +03:00
int armySize = creToPower . size ( ) ;
2018-04-07 10:44:14 +02:00
armySize = std : : min ( ( source - > needsLastStack ( ) ? armySize - 1 : armySize ) , GameConstants : : ARMY_SIZE ) ; //can't move away last stack
2012-08-29 18:23:47 +03:00
std : : vector < const CCreature * > bestArmy ; //types that'll be in final dst army
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < armySize ; i + + ) //pick the creatures from which we can get most power, as many as dest can fit
2012-08-29 18:23:47 +03:00
{
2018-04-07 10:44:14 +02:00
typedef const std : : pair < const CCreature * , int > & CrePowerPair ;
2012-08-29 18:23:47 +03:00
auto creIt = boost : : max_element ( creToPower , [ ] ( CrePowerPair lhs , CrePowerPair rhs )
2018-04-07 10:44:14 +02:00
{
return lhs . second < rhs . second ;
} ) ;
2012-08-29 18:23:47 +03:00
bestArmy . push_back ( creIt - > first ) ;
creToPower . erase ( creIt ) ;
if ( creToPower . empty ( ) )
break ;
2012-08-29 12:19:20 +03:00
}
2012-08-29 18:23:47 +03:00
//foreach best type -> iterate over slots in both armies and if it's the appropriate type, send it to the slot where it belongs
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < bestArmy . size ( ) ; i + + ) //i-th strongest creature type will go to i-th slot
2012-08-29 12:19:20 +03:00
{
2013-06-29 16:05:48 +03:00
for ( auto armyPtr : armies )
2018-04-07 10:44:14 +02:00
{
for ( int j = 0 ; j < GameConstants : : ARMY_SIZE ; j + + )
2012-08-29 12:19:20 +03:00
{
2018-04-07 10:44:14 +02:00
if ( armyPtr - > getCreature ( SlotID ( j ) ) = = bestArmy [ i ] & & armyPtr ! = army ) //it's a searched creature not in dst ARMY
2014-02-19 19:23:47 +03:00
{
//FIXME: line below is useless when simulating exchange between two non-singular armies
2015-12-24 20:30:57 +02:00
if ( ! ( armyPtr - > needsLastStack ( ) & & armyPtr - > stacksCount ( ) = = 1 ) ) //can't take away last creature
2012-08-29 18:23:47 +03:00
return true ; //at least one exchange will be performed
2014-02-19 19:23:47 +03:00
else
return false ; //no further exchange possible
}
2012-08-29 12:19:20 +03:00
}
2018-04-07 10:44:14 +02:00
}
2012-08-29 12:19:20 +03:00
}
return false ;
}
2012-03-06 21:49:23 +03:00
void VCAI : : pickBestCreatures ( const CArmedInstance * army , const CArmedInstance * source )
{
2012-03-11 04:26:11 +03:00
//TODO - what if source is a hero (the last stack problem) -> it'd good to create a single stack of weakest cre
2018-04-07 10:44:14 +02:00
const CArmedInstance * armies [ ] = { army , source } ;
2014-02-19 19:23:47 +03:00
2012-03-11 04:26:11 +03:00
//we calculate total strength for each creature type available in armies
2018-04-07 10:44:14 +02:00
std : : map < const CCreature * , int > creToPower ;
2013-06-29 16:05:48 +03:00
for ( auto armyPtr : armies )
2018-04-07 10:44:14 +02:00
{
for ( auto & i : armyPtr - > Slots ( ) )
{
//TODO: allow splitting stacks?
2012-07-19 21:52:44 +03:00
creToPower [ i . second - > type ] + = i . second - > getPower ( ) ;
2012-08-29 18:23:47 +03:00
}
2018-04-07 10:44:14 +02:00
}
2012-03-11 04:26:11 +03:00
//TODO - consider more than just power (ie morale penalty, hero specialty in certain stacks, etc)
2014-02-19 19:23:47 +03:00
int armySize = creToPower . size ( ) ;
2012-03-11 04:26:11 +03:00
2018-04-07 10:44:14 +02:00
armySize = std : : min ( ( source - > needsLastStack ( ) ? armySize - 1 : armySize ) , GameConstants : : ARMY_SIZE ) ; //can't move away last stack
2012-03-11 04:26:11 +03:00
std : : vector < const CCreature * > bestArmy ; //types that'll be in final dst army
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < armySize ; i + + ) //pick the creatures from which we can get most power, as many as dest can fit
2012-03-06 21:49:23 +03:00
{
2018-04-07 10:44:14 +02:00
typedef const std : : pair < const CCreature * , int > & CrePowerPair ;
2012-03-11 04:26:11 +03:00
auto creIt = boost : : max_element ( creToPower , [ ] ( CrePowerPair lhs , CrePowerPair rhs )
2018-04-07 10:44:14 +02:00
{
return lhs . second < rhs . second ;
} ) ;
2012-03-11 04:26:11 +03:00
bestArmy . push_back ( creIt - > first ) ;
creToPower . erase ( creIt ) ;
if ( creToPower . empty ( ) )
break ;
2012-02-14 21:04:45 +03:00
}
2012-07-19 21:52:44 +03:00
2012-03-11 04:26:11 +03:00
//foreach best type -> iterate over slots in both armies and if it's the appropriate type, send it to the slot where it belongs
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < bestArmy . size ( ) ; i + + ) //i-th strongest creature type will go to i-th slot
2012-08-29 12:19:20 +03:00
{
2013-06-29 16:05:48 +03:00
for ( auto armyPtr : armies )
2018-04-07 10:44:14 +02:00
{
for ( int j = 0 ; j < GameConstants : : ARMY_SIZE ; j + + )
2012-08-29 12:19:20 +03:00
{
2018-04-07 10:44:14 +02:00
if ( armyPtr - > getCreature ( SlotID ( j ) ) = = bestArmy [ i ] & & ( i ! = j | | armyPtr ! = army ) ) //it's a searched creature not in dst SLOT
{
2015-12-24 20:30:57 +02:00
if ( ! ( armyPtr - > needsLastStack ( ) & & armyPtr - > stacksCount ( ) = = 1 ) ) //can't take away last creature
2013-02-16 17:03:47 +03:00
cb - > mergeOrSwapStacks ( armyPtr , army , SlotID ( j ) , SlotID ( i ) ) ;
2018-04-07 10:44:14 +02:00
}
2012-08-29 12:19:20 +03:00
}
2018-04-07 10:44:14 +02:00
}
2012-08-29 12:19:20 +03:00
}
2012-03-11 04:26:11 +03:00
//TODO - having now strongest possible army, we may want to think about arranging stacks
2012-07-18 13:10:14 +03:00
auto hero = dynamic_cast < const CGHeroInstance * > ( army ) ;
2018-04-07 10:44:14 +02:00
if ( hero )
checkHeroArmy ( hero ) ;
2012-02-14 21:04:45 +03:00
}
2015-04-07 22:48:35 +02:00
void VCAI : : pickBestArtifacts ( const CGHeroInstance * h , const CGHeroInstance * other )
2015-10-31 17:04:06 +02:00
{
2015-04-09 09:53:17 +02:00
auto equipBest = [ ] ( const CGHeroInstance * h , const CGHeroInstance * otherh , bool giveStuffToFirstHero ) - > void
2015-04-07 22:48:35 +02:00
{
2015-04-09 09:53:17 +02:00
bool changeMade = false ;
do
2015-04-07 22:48:35 +02:00
{
2015-04-09 09:53:17 +02:00
changeMade = false ;
2015-04-07 22:48:35 +02:00
2015-04-09 09:53:17 +02:00
//we collect gear always in same order
std : : vector < ArtifactLocation > allArtifacts ;
2018-04-07 10:44:14 +02:00
if ( giveStuffToFirstHero )
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
for ( auto p : h - > artifactsWorn )
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
if ( p . second . artifact )
2015-04-09 09:53:17 +02:00
allArtifacts . push_back ( ArtifactLocation ( h , p . first ) ) ;
}
}
2018-04-07 10:44:14 +02:00
for ( auto slot : h - > artifactsInBackpack )
2015-04-09 09:53:17 +02:00
allArtifacts . push_back ( ArtifactLocation ( h , h - > getArtPos ( slot . artifact ) ) ) ;
2015-04-07 22:48:35 +02:00
2018-04-07 10:44:14 +02:00
if ( otherh )
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
for ( auto p : otherh - > artifactsWorn )
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
if ( p . second . artifact )
2015-04-09 09:53:17 +02:00
allArtifacts . push_back ( ArtifactLocation ( otherh , p . first ) ) ;
}
2018-04-07 10:44:14 +02:00
for ( auto slot : otherh - > artifactsInBackpack )
2015-04-09 09:53:17 +02:00
allArtifacts . push_back ( ArtifactLocation ( otherh , otherh - > getArtPos ( slot . artifact ) ) ) ;
}
//we give stuff to one hero or another, depending on giveStuffToFirstHero
2015-04-07 22:48:35 +02:00
2015-04-09 09:53:17 +02:00
const CGHeroInstance * target = nullptr ;
2018-04-07 10:44:14 +02:00
if ( giveStuffToFirstHero | | ! otherh )
2015-04-09 09:53:17 +02:00
target = h ;
else
target = otherh ;
2015-04-07 22:48:35 +02:00
2018-04-07 10:44:14 +02:00
for ( auto location : allArtifacts )
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
if ( location . relatedObj ( ) = = target & & location . slot < ArtifactPosition : : AFTER_LAST )
2015-04-09 09:53:17 +02:00
continue ; //don't reequip artifact we already wear
2015-04-07 22:48:35 +02:00
2015-12-09 17:49:22 +02:00
if ( location . slot = = ArtifactPosition : : MACH4 ) // don't attempt to move catapult
continue ;
2015-04-09 09:53:17 +02:00
auto s = location . getSlot ( ) ;
2018-04-07 10:44:14 +02:00
if ( ! s | | s - > locked ) //we can't move locks
2015-04-09 09:53:17 +02:00
continue ;
auto artifact = s - > artifact ;
2018-04-07 10:44:14 +02:00
if ( ! artifact )
2015-04-09 09:53:17 +02:00
continue ;
//FIXME: why are the above possible to be null?
bool emptySlotFound = false ;
2018-04-07 10:44:14 +02:00
for ( auto slot : artifact - > artType - > possibleSlots . at ( target - > bearerType ( ) ) )
2015-04-09 09:53:17 +02:00
{
2015-08-30 15:15:04 +02:00
ArtifactLocation destLocation ( target , slot ) ;
2018-04-07 10:44:14 +02:00
if ( target - > isPositionFree ( slot ) & & artifact - > canBePutAt ( destLocation , true ) ) //combined artifacts are not always allowed to move
2015-04-09 09:53:17 +02:00
{
2015-08-30 15:15:04 +02:00
cb - > swapArtifacts ( location , destLocation ) ; //just put into empty slot
2015-04-09 09:53:17 +02:00
emptySlotFound = true ;
changeMade = true ;
break ;
}
}
2018-04-07 10:44:14 +02:00
if ( ! emptySlotFound ) //try to put that atifact in already occupied slot
2015-04-09 09:53:17 +02:00
{
2018-04-07 10:44:14 +02:00
for ( auto slot : artifact - > artType - > possibleSlots . at ( target - > bearerType ( ) ) )
2015-04-09 09:53:17 +02:00
{
auto otherSlot = target - > getSlot ( slot ) ;
2018-04-07 10:44:14 +02:00
if ( otherSlot & & otherSlot - > artifact ) //we need to exchange artifact for better one
2015-04-09 09:53:17 +02:00
{
2015-08-30 15:15:04 +02:00
ArtifactLocation destLocation ( target , slot ) ;
//if that artifact is better than what we have, pick it
2018-04-07 10:44:14 +02:00
if ( compareArtifacts ( artifact , otherSlot - > artifact ) & & artifact - > canBePutAt ( destLocation , true ) ) //combined artifacts are not always allowed to move
2015-04-09 09:53:17 +02:00
{
cb - > swapArtifacts ( location , ArtifactLocation ( target , target - > getArtPos ( otherSlot - > artifact ) ) ) ;
changeMade = true ;
2016-08-18 05:35:29 +02:00
break ;
2015-04-09 09:53:17 +02:00
}
}
}
}
2018-04-07 10:44:14 +02:00
if ( changeMade )
2015-04-09 09:53:17 +02:00
break ; //start evaluating artifacts from scratch
}
2018-04-07 10:44:14 +02:00
}
while ( changeMade ) ;
2015-04-09 09:53:17 +02:00
} ;
2018-04-07 10:44:14 +02:00
equipBest ( h , other , true ) ;
2015-04-09 09:53:17 +02:00
2018-04-07 10:44:14 +02:00
if ( other )
2015-04-09 09:53:17 +02:00
equipBest ( h , other , false ) ;
2015-04-07 22:48:35 +02:00
}
2014-09-19 00:18:49 +03:00
void VCAI : : recruitCreatures ( const CGDwelling * d , const CArmedInstance * recruiter )
2012-02-14 21:04:45 +03:00
{
2018-07-26 12:06:55 +02:00
//now used only for visited dwellings / towns, not BuyArmy goal
2012-03-05 22:11:28 +03:00
for ( int i = 0 ; i < d - > creatures . size ( ) ; i + + )
2012-02-14 21:04:45 +03:00
{
2012-03-05 22:11:28 +03:00
if ( ! d - > creatures [ i ] . second . size ( ) )
2012-02-14 21:04:45 +03:00
continue ;
2012-03-05 22:11:28 +03:00
int count = d - > creatures [ i ] . first ;
2013-02-11 02:24:57 +03:00
CreatureID creID = d - > creatures [ i ] . second . back ( ) ;
2012-02-14 21:04:45 +03:00
2018-07-26 12:06:55 +02:00
vstd : : amin ( count , ah - > freeResources ( ) / VLC - > creh - > creatures [ creID ] - > cost ) ;
2012-02-14 21:04:45 +03:00
if ( count > 0 )
2014-09-19 00:18:49 +03:00
cb - > recruitCreatures ( d , recruiter , creID , count , i ) ;
2012-02-14 21:04:45 +03:00
}
}
2018-07-26 12:06:55 +02:00
bool VCAI : : tryBuildThisStructure ( const CGTownInstance * t , BuildingID building , unsigned int maxDays )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( maxDays = = 0 )
2013-12-19 20:29:35 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > warn ( " Request to build building %d in 0 days! " , building . toEnum ( ) ) ;
2013-12-19 20:29:35 +03:00
return false ;
}
2018-04-07 10:44:14 +02:00
if ( ! vstd : : contains ( t - > town - > buildings , building ) )
2012-09-06 13:39:48 +03:00
return false ; // no such building in town
2018-04-07 10:44:14 +02:00
if ( t - > hasBuilt ( building ) ) //Already built? Shouldn't happen in general
2012-05-19 19:22:34 +03:00
return true ;
2013-12-02 14:58:02 +03:00
const CBuilding * buildPtr = t - > town - > buildings . at ( building ) ;
2012-08-26 15:26:07 +03:00
2013-12-02 14:58:02 +03:00
auto toBuild = buildPtr - > requirements . getFulfillmentCandidates ( [ & ] ( const BuildingID & buildID )
2012-08-26 15:26:07 +03:00
{
2013-12-02 14:58:02 +03:00
return t - > hasBuilt ( buildID ) ;
} ) ;
toBuild . push_back ( building ) ;
2012-02-14 21:04:45 +03:00
2013-06-29 16:05:48 +03:00
for ( BuildingID buildID : toBuild )
2012-07-07 16:32:37 +03:00
{
2013-02-09 15:56:35 +03:00
EBuildingState : : EBuildingState canBuild = cb - > canBuildStructure ( t , buildID ) ;
2018-04-07 10:44:14 +02:00
if ( canBuild = = EBuildingState : : HAVE_CAPITAL | | canBuild = = EBuildingState : : FORBIDDEN | | canBuild = = EBuildingState : : NO_WATER )
2012-07-07 16:32:37 +03:00
return false ; //we won't be able to build this
}
2018-04-07 10:44:14 +02:00
if ( maxDays & & toBuild . size ( ) > maxDays )
2012-05-19 19:22:34 +03:00
return false ;
2012-02-14 21:04:45 +03:00
2018-07-26 12:06:55 +02:00
//TODO: calculate if we have enough resources to build it in maxDays?
2013-11-17 20:57:04 +03:00
for ( const auto & buildID : toBuild )
{
2018-04-07 10:44:14 +02:00
const CBuilding * b = t - > town - > buildings . at ( buildID ) ;
2013-11-17 20:57:04 +03:00
EBuildingState : : EBuildingState canBuild = cb - > canBuildStructure ( t , buildID ) ;
2018-07-26 12:06:55 +02:00
if ( canBuild = = EBuildingState : : ALLOWED )
2012-02-14 21:04:45 +03:00
{
2018-07-26 12:06:55 +02:00
buildStructure ( t , buildID ) ;
return true ;
2013-11-17 20:57:04 +03:00
}
2018-07-26 12:06:55 +02:00
else if ( canBuild = = EBuildingState : : PREREQUIRES )
2013-11-17 20:57:04 +03:00
{
2018-07-26 12:06:55 +02:00
// can happen when dependencies have their own missing dependencies
if ( tryBuildThisStructure ( t , buildID , maxDays - 1 ) )
return true ;
2012-05-19 19:22:34 +03:00
}
2018-07-26 12:06:55 +02:00
else if ( canBuild = = EBuildingState : : MISSING_BASE )
2013-12-02 14:58:02 +03:00
{
2018-07-26 12:06:55 +02:00
if ( tryBuildThisStructure ( t , b - > upgrade , maxDays - 1 ) )
2013-12-02 14:58:02 +03:00
return true ;
}
2018-07-26 12:06:55 +02:00
else if ( canBuild = = EBuildingState : : NO_RESOURCES )
2013-12-02 14:58:02 +03:00
{
2018-07-26 12:06:55 +02:00
//we may need to gather resources for those
PotentialBuilding pb ;
pb . bid = buildID ;
pb . price = t - > getBuildingCost ( buildID ) ;
potentialBuildings . push_back ( pb ) ; //these are checked again in try
return false ;
2013-12-02 14:58:02 +03:00
}
2018-07-26 12:06:55 +02:00
else
return false ;
2012-05-19 19:22:34 +03:00
}
return false ;
}
2018-07-26 12:06:55 +02:00
bool VCAI : : tryBuildAnyStructure ( const CGTownInstance * t , std : : vector < BuildingID > buildList , unsigned int maxDays )
2012-05-19 19:22:34 +03:00
{
2013-06-29 16:05:48 +03:00
for ( const auto & building : buildList )
2012-05-19 19:22:34 +03:00
{
if ( t - > hasBuilt ( building ) )
continue ;
2018-07-26 12:06:55 +02:00
return tryBuildThisStructure ( t , building , maxDays ) ;
2012-05-19 19:22:34 +03:00
}
return false ; //Can't build anything
}
2012-02-14 21:04:45 +03:00
2018-07-19 20:39:37 +02:00
BuildingID VCAI : : canBuildAnyStructure ( const CGTownInstance * t , std : : vector < BuildingID > buildList , unsigned int maxDays ) const
2013-12-23 23:46:01 +03:00
{
for ( const auto & building : buildList )
{
if ( t - > hasBuilt ( building ) )
continue ;
2018-04-07 10:44:14 +02:00
if ( cb - > canBuildStructure ( t , building ) )
2013-12-23 23:46:01 +03:00
return building ;
}
return BuildingID : : NONE ; //Can't build anything
}
2018-07-26 12:06:55 +02:00
bool VCAI : : tryBuildNextStructure ( const CGTownInstance * t , std : : vector < BuildingID > buildList , unsigned int maxDays )
2012-05-19 19:22:34 +03:00
{
2013-06-29 16:05:48 +03:00
for ( const auto & building : buildList )
2012-05-19 19:22:34 +03:00
{
if ( t - > hasBuilt ( building ) )
2012-02-14 21:04:45 +03:00
continue ;
2018-07-26 12:06:55 +02:00
return tryBuildThisStructure ( t , building , maxDays ) ;
2012-05-19 19:22:34 +03:00
}
2018-04-07 10:44:14 +02:00
return false ; //Nothing to build
2012-05-19 19:22:34 +03:00
}
2018-07-26 12:06:55 +02:00
void VCAI : : buildStructure ( const CGTownInstance * t , BuildingID building )
{
auto name = t - > town - > buildings . at ( building ) - > Name ( ) ;
logAi - > debug ( " Player %d will build %s in town of %s at %s " , playerID , name , t - > name , t - > pos . toString ( ) ) ;
cb - > buildBuilding ( t , building ) ; //just do this;
}
2015-12-04 01:06:02 +02:00
//Set of buildings for different goals. Does not include any prerequisites.
static const BuildingID essential [ ] = { BuildingID : : TAVERN , BuildingID : : TOWN_HALL } ;
static const BuildingID goldSource [ ] = { BuildingID : : TOWN_HALL , BuildingID : : CITY_HALL , BuildingID : : CAPITOL } ;
2017-07-15 00:15:08 +02:00
static const BuildingID capitolRequirements [ ] = { BuildingID : : FORT , BuildingID : : CITADEL } ;
2015-12-04 01:06:02 +02:00
static const BuildingID unitsSource [ ] = { BuildingID : : DWELL_LVL_1 , BuildingID : : DWELL_LVL_2 , BuildingID : : DWELL_LVL_3 ,
BuildingID : : DWELL_LVL_4 , BuildingID : : DWELL_LVL_5 , BuildingID : : DWELL_LVL_6 , BuildingID : : DWELL_LVL_7 } ;
static const BuildingID unitsUpgrade [ ] = { BuildingID : : DWELL_LVL_1_UP , BuildingID : : DWELL_LVL_2_UP , BuildingID : : DWELL_LVL_3_UP ,
BuildingID : : DWELL_LVL_4_UP , BuildingID : : DWELL_LVL_5_UP , BuildingID : : DWELL_LVL_6_UP , BuildingID : : DWELL_LVL_7_UP } ;
static const BuildingID unitGrowth [ ] = { BuildingID : : FORT , BuildingID : : CITADEL , BuildingID : : CASTLE , BuildingID : : HORDE_1 ,
BuildingID : : HORDE_1_UPGR , BuildingID : : HORDE_2 , BuildingID : : HORDE_2_UPGR } ;
2017-07-20 06:08:49 +02:00
static const BuildingID _spells [ ] = { BuildingID : : MAGES_GUILD_1 , BuildingID : : MAGES_GUILD_2 , BuildingID : : MAGES_GUILD_3 ,
2015-12-04 01:06:02 +02:00
BuildingID : : MAGES_GUILD_4 , BuildingID : : MAGES_GUILD_5 } ;
static const BuildingID extra [ ] = { BuildingID : : RESOURCE_SILO , BuildingID : : SPECIAL_1 , BuildingID : : SPECIAL_2 , BuildingID : : SPECIAL_3 ,
BuildingID : : SPECIAL_4 , BuildingID : : SHIPYARD } ; // all remaining buildings
2018-07-26 12:06:55 +02:00
bool VCAI : : tryBuildStructure ( const CGTownInstance * t )
2012-05-19 19:22:34 +03:00
{
//TODO make *real* town development system
2012-05-20 00:38:01 +03:00
//TODO: faction-specific development: use special buildings, build dwellings in better order, etc
2012-05-19 19:22:34 +03:00
//TODO: build resource silo, defences when needed
2012-05-20 00:38:01 +03:00
//Possible - allow "locking" on specific building (build prerequisites and then building itself)
2012-05-19 19:22:34 +03:00
2017-07-15 00:15:08 +02:00
//below algorithm focuses on economy growth at start of the game.
2012-05-19 19:22:34 +03:00
TResources currentRes = cb - > getResourceAmount ( ) ;
2014-04-26 17:23:35 +03:00
TResources currentIncome = t - > dailyIncome ( ) ;
2012-05-19 19:22:34 +03:00
2017-07-15 00:15:08 +02:00
if ( tryBuildAnyStructure ( t , std : : vector < BuildingID > ( essential , essential + ARRAY_COUNT ( essential ) ) ) )
2018-07-26 12:06:55 +02:00
return true ;
2012-05-19 19:22:34 +03:00
2017-07-15 00:15:08 +02:00
//the more gold the better and less problems later
if ( tryBuildNextStructure ( t , std : : vector < BuildingID > ( goldSource , goldSource + ARRAY_COUNT ( goldSource ) ) ) )
2018-07-26 12:06:55 +02:00
return true ;
2017-07-15 00:15:08 +02:00
//workaround for mantis #2696 - build fort and citadel - building castle will be handled without bug
2018-07-26 12:06:55 +02:00
if ( vstd : : contains ( t - > builtBuildings , BuildingID : : CITY_HALL ) & &
cb - > canBuildStructure ( t , BuildingID : : CAPITOL ) ! = EBuildingState : : HAVE_CAPITAL )
2018-04-07 10:44:14 +02:00
{
if ( cb - > canBuildStructure ( t , BuildingID : : CAPITOL ) ! = EBuildingState : : FORBIDDEN )
{
2018-07-26 12:06:55 +02:00
if ( tryBuildNextStructure ( t , std : : vector < BuildingID > ( capitolRequirements ,
capitolRequirements + ARRAY_COUNT ( capitolRequirements ) ) ) )
return true ;
2018-04-07 10:44:14 +02:00
}
}
2012-05-19 19:22:34 +03:00
2018-07-26 12:06:55 +02:00
//TODO: save money for capitol or city hall if capitol unavailable
//do not build other things (unless gold source buildings are disabled in map editor)
2017-07-15 00:15:08 +02:00
2018-04-07 10:44:14 +02:00
if ( cb - > getDate ( Date : : DAY_OF_WEEK ) > 6 ) // last 2 days of week - try to focus on growth
2012-05-19 19:22:34 +03:00
{
2017-07-15 00:15:08 +02:00
if ( tryBuildNextStructure ( t , std : : vector < BuildingID > ( unitGrowth , unitGrowth + ARRAY_COUNT ( unitGrowth ) ) , 2 ) )
2018-07-26 12:06:55 +02:00
return true ;
2012-05-19 19:22:34 +03:00
}
// first in-game week or second half of any week: try build dwellings
2017-07-15 00:15:08 +02:00
if ( cb - > getDate ( Date : : DAY ) < 7 | | cb - > getDate ( Date : : DAY_OF_WEEK ) > 3 )
2018-04-07 10:44:14 +02:00
{
2018-07-26 12:06:55 +02:00
if ( tryBuildAnyStructure ( t , std : : vector < BuildingID > ( unitsSource ,
unitsSource + ARRAY_COUNT ( unitsSource ) ) , 8 - cb - > getDate ( Date : : DAY_OF_WEEK ) ) )
return true ;
2018-04-07 10:44:14 +02:00
}
2012-05-19 19:22:34 +03:00
//try to upgrade dwelling
for ( int i = 0 ; i < ARRAY_COUNT ( unitsUpgrade ) ; i + + )
{
2017-07-15 00:15:08 +02:00
if ( t - > hasBuilt ( unitsSource [ i ] ) & & ! t - > hasBuilt ( unitsUpgrade [ i ] ) )
2012-05-19 19:22:34 +03:00
{
2018-07-26 12:06:55 +02:00
if ( tryBuildThisStructure ( t , unitsUpgrade [ i ] ) )
return true ;
2012-02-14 21:04:45 +03:00
}
}
2012-05-19 19:22:34 +03:00
//remaining tasks
2017-07-20 06:08:49 +02:00
if ( tryBuildNextStructure ( t , std : : vector < BuildingID > ( _spells , _spells + ARRAY_COUNT ( _spells ) ) ) )
2018-07-26 12:06:55 +02:00
return true ;
2017-07-15 00:15:08 +02:00
if ( tryBuildAnyStructure ( t , std : : vector < BuildingID > ( extra , extra + ARRAY_COUNT ( extra ) ) ) )
2018-07-26 12:06:55 +02:00
return true ;
2017-02-18 19:40:44 +02:00
2017-02-18 22:10:59 +02:00
//at the end, try to get and build any extra buildings with nonstandard slots (for example HotA 3rd level dwelling)
std : : vector < BuildingID > extraBuildings ;
2017-07-15 00:15:08 +02:00
for ( auto buildingInfo : t - > town - > buildings )
2018-04-07 10:44:14 +02:00
{
2017-07-15 00:15:08 +02:00
if ( buildingInfo . first > 43 )
2017-02-18 22:10:59 +02:00
extraBuildings . push_back ( buildingInfo . first ) ;
2018-04-07 10:44:14 +02:00
}
2017-07-15 00:15:08 +02:00
if ( tryBuildAnyStructure ( t , extraBuildings ) )
2018-07-26 12:06:55 +02:00
return true ;
return false ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
bool VCAI : : isGoodForVisit ( const CGObjectInstance * obj , HeroPtr h , SectorMap & sm )
2012-02-14 21:04:45 +03:00
{
2013-12-21 22:17:27 +03:00
const int3 pos = obj - > visitablePos ( ) ;
2015-04-05 21:13:47 +02:00
const int3 targetPos = sm . firstTileToGet ( h , pos ) ;
2018-04-07 10:44:14 +02:00
if ( ! targetPos . valid ( ) )
return false ;
if ( ! isTileNotReserved ( h . get ( ) , targetPos ) )
return false ;
if ( obj - > wasVisited ( playerID ) )
return false ;
if ( cb - > getPlayerRelations ( ai - > playerID , obj - > tempOwner ) ! = PlayerRelations : : ENEMIES & & ! isWeeklyRevisitable ( obj ) )
return false ; // Otherwise we flag or get weekly resources / creatures
if ( ! isSafeToVisit ( h , pos ) )
return false ;
if ( ! shouldVisit ( h , obj ) )
return false ;
if ( vstd : : contains ( alreadyVisited , obj ) )
return false ;
if ( vstd : : contains ( reservedObjs , obj ) )
return false ;
if ( ! isAccessibleForHero ( targetPos , h ) )
2015-04-10 08:50:21 +02:00
return false ;
2013-12-21 22:17:27 +03:00
2018-04-07 10:44:14 +02:00
const CGObjectInstance * topObj = cb - > getVisitableObjs ( obj - > visitablePos ( ) ) . back ( ) ; //it may be hero visiting this obj
//we don't try visiting object on which allied or owned hero stands
// -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
if ( topObj - > ID = = Obj : : HERO & & cb - > getPlayerRelations ( h - > tempOwner , topObj - > tempOwner ) ! = PlayerRelations : : ENEMIES )
return false ;
else
return true ; //all of the following is met
2013-12-21 22:17:27 +03:00
}
2015-04-10 08:50:21 +02:00
bool VCAI : : isTileNotReserved ( const CGHeroInstance * h , int3 t )
2014-02-17 20:28:39 +03:00
{
2018-04-07 10:44:14 +02:00
if ( t . valid ( ) )
2014-02-17 20:28:39 +03:00
{
auto obj = cb - > getTopObj ( t ) ;
2018-04-07 10:44:14 +02:00
if ( obj & & vstd : : contains ( ai - > reservedObjs , obj ) & & ! vstd : : contains ( reservedHeroesMap [ h ] , obj ) )
2014-02-20 23:18:49 +03:00
return false ; //do not capture object reserved by another hero
2014-02-17 20:28:39 +03:00
else
2014-02-20 23:18:49 +03:00
return true ;
2014-02-17 20:28:39 +03:00
}
2014-02-20 23:18:49 +03:00
else
2018-04-07 10:44:14 +02:00
{
2014-02-20 23:18:49 +03:00
return false ;
2018-04-07 10:44:14 +02:00
}
2014-02-17 20:28:39 +03:00
}
2018-04-07 10:44:14 +02:00
bool VCAI : : canRecruitAnyHero ( const CGTownInstance * t ) const
2013-12-19 23:21:21 +03:00
{
//TODO: make gathering gold, building tavern or conquering town (?) possible subgoals
2018-04-07 10:44:14 +02:00
if ( ! t )
2013-12-19 23:21:21 +03:00
t = findTownWithTavern ( ) ;
2018-04-07 10:44:14 +02:00
if ( ! t )
return false ;
2018-07-26 12:06:55 +02:00
if ( cb - > getResourceAmount ( Res : : GOLD ) < GameConstants : : HERO_GOLD_COST ) //TODO: use ResourceManager
2018-04-07 10:44:14 +02:00
return false ;
if ( cb - > getHeroesInfo ( ) . size ( ) > = ALLOWED_ROAMING_HEROES )
2013-12-19 23:21:21 +03:00
return false ;
2018-04-07 10:44:14 +02:00
if ( ! cb - > getAvailableHeroes ( t ) . size ( ) )
return false ;
return true ;
2013-12-19 23:21:21 +03:00
}
2012-07-01 02:48:40 +03:00
void VCAI : : wander ( HeroPtr h )
2012-02-14 21:04:45 +03:00
{
2014-02-07 23:09:15 +03:00
//unclaim objects that are now dangerous for us
2014-03-01 15:53:09 +03:00
auto reservedObjsSetCopy = reservedHeroesMap [ h ] ;
2018-04-07 10:44:14 +02:00
for ( auto obj : reservedObjsSetCopy )
2014-02-07 23:09:15 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ! isSafeToVisit ( h , obj - > visitablePos ( ) ) )
2014-02-23 23:37:33 +03:00
unreserveObject ( h , obj ) ;
}
2014-02-07 23:09:15 +03:00
2013-12-23 23:46:01 +03:00
TimeCheck tc ( " looking for wander destination " ) ;
2014-02-05 00:49:04 +03:00
2018-04-07 10:44:14 +02:00
while ( h - > movement )
2012-02-14 21:04:45 +03:00
{
2013-08-18 18:46:28 +03:00
validateVisitableObjs ( ) ;
2018-04-07 10:44:14 +02:00
std : : vector < ObjectIdRef > dests ;
2013-11-24 22:15:08 +03:00
2015-12-04 00:10:51 +02:00
auto sm = getCachedSectorMap ( h ) ;
2015-04-05 21:13:47 +02:00
2016-08-11 15:53:05 +02:00
//also visit our reserved objects - but they are not prioritized to avoid running back and forth
vstd : : copy_if ( reservedHeroesMap [ h ] , std : : back_inserter ( dests ) , [ & ] ( ObjectIdRef obj ) - > bool
2013-11-24 22:15:08 +03:00
{
2015-12-04 00:10:51 +02:00
int3 pos = sm - > firstTileToGet ( h , obj - > visitablePos ( ) ) ;
2016-08-11 15:53:05 +02:00
if ( pos . valid ( ) & & isAccessibleForHero ( pos , h ) ) //even nearby objects could be blocked by other heroes :(
return true ;
return false ;
} ) ;
2016-08-12 08:05:11 +02:00
int pass = 0 ;
while ( ! dests . size ( ) & & pass < 3 )
2016-08-11 15:53:05 +02:00
{
2016-08-12 08:05:11 +02:00
if ( pass < 2 ) // optimization - first check objects in current sector; then in sectors around
{
auto objs = sm - > getNearbyObjs ( h , pass ) ;
vstd : : copy_if ( objs , std : : back_inserter ( dests ) , [ & ] ( ObjectIdRef obj ) - > bool
{
return isGoodForVisit ( obj , h , * sm ) ;
} ) ;
}
else // we only check full objects list if for some reason there are no objects in closest sectors
{
vstd : : copy_if ( visitableObjs , std : : back_inserter ( dests ) , [ & ] ( ObjectIdRef obj ) - > bool
{
return isGoodForVisit ( obj , h , * sm ) ;
} ) ;
}
pass + + ;
}
2013-11-24 22:15:08 +03:00
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( dests , [ & ] ( ObjectIdRef obj ) - > bool
2015-04-05 21:13:47 +02:00
{
2015-12-04 00:10:51 +02:00
return ! isSafeToVisit ( h , sm - > firstTileToGet ( h , obj - > visitablePos ( ) ) ) ;
2015-04-05 21:13:47 +02:00
} ) ;
2012-04-17 15:46:21 +03:00
2012-03-13 23:33:00 +03:00
if ( ! dests . size ( ) )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( cb - > getVisitableObjs ( h - > visitablePos ( ) ) . size ( ) > 1 )
2013-12-23 23:46:01 +03:00
moveHeroToTile ( h - > visitablePos ( ) , h ) ; //just in case we're standing on blocked subterranean gate
2018-04-07 10:44:14 +02:00
auto compareReinforcements = [ h ] ( const CGTownInstance * lhs , const CGTownInstance * rhs ) - > bool
2014-02-05 00:49:04 +03:00
{
2012-03-29 21:26:06 +03:00
return howManyReinforcementsCanGet ( h , lhs ) < howManyReinforcementsCanGet ( h , rhs ) ;
2014-02-05 00:49:04 +03:00
} ;
std : : vector < const CGTownInstance * > townsReachable ;
std : : vector < const CGTownInstance * > townsNotReachable ;
2018-04-07 10:44:14 +02:00
for ( const CGTownInstance * t : cb - > getTownsInfo ( ) )
2014-02-05 00:49:04 +03:00
{
2018-07-26 14:37:39 +02:00
if ( ! t - > visitingHero & & ! vstd : : contains ( townVisitsThisWeek [ h ] , t ) )
2014-02-05 00:49:04 +03:00
{
2018-04-07 10:44:14 +02:00
if ( isAccessibleForHero ( t - > visitablePos ( ) , h ) )
2012-03-29 21:26:06 +03:00
townsReachable . push_back ( t ) ;
2014-02-05 00:49:04 +03:00
else
2012-03-29 21:26:06 +03:00
townsNotReachable . push_back ( t ) ;
2014-02-05 00:49:04 +03:00
}
2012-03-29 21:26:06 +03:00
}
2018-07-26 14:37:39 +02:00
if ( townsReachable . size ( ) ) //travel to town with largest garrison, or empty - better than nothing
2014-02-05 00:49:04 +03:00
{
2018-07-26 14:37:39 +02:00
dests . push_back ( * boost : : max_element ( townsReachable , compareReinforcements ) ) ;
2012-03-29 21:26:06 +03:00
}
else if ( townsNotReachable . size ( ) )
2018-07-19 17:36:10 +02:00
{
2013-12-23 23:46:01 +03:00
//TODO pick the truly best
2018-07-19 17:36:10 +02:00
const CGTownInstance * t = * boost : : max_element ( townsNotReachable , compareReinforcements ) ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " %s can't reach any town, we'll try to make our way to %s at %s " , h - > name , t - > name , t - > visitablePos ( ) . toString ( ) ) ;
2012-03-29 21:26:06 +03:00
int3 pos1 = h - > pos ;
2013-12-23 23:46:01 +03:00
striveToGoal ( sptr ( Goals : : ClearWayTo ( t - > visitablePos ( ) ) . sethero ( h ) ) ) ;
//if out hero is stuck, we may need to request another hero to clear the way we see
2018-04-07 10:44:14 +02:00
if ( pos1 = = h - > pos & & h = = primaryHero ( ) ) //hero can't move
2012-03-29 21:26:06 +03:00
{
2018-04-07 10:44:14 +02:00
if ( canRecruitAnyHero ( t ) )
2013-12-19 23:21:21 +03:00
recruitHero ( t ) ;
2012-03-29 21:26:06 +03:00
}
2013-12-23 23:46:01 +03:00
break ;
2012-03-29 21:26:06 +03:00
}
2015-12-06 23:13:58 +02:00
else if ( cb - > getResourceAmount ( Res : : GOLD ) > = GameConstants : : HERO_GOLD_COST )
2013-12-23 23:46:01 +03:00
{
2012-03-29 21:26:06 +03:00
std : : vector < const CGTownInstance * > towns = cb - > getTownsInfo ( ) ;
2018-04-07 10:44:14 +02:00
vstd : : erase_if ( towns , [ ] ( const CGTownInstance * t ) - > bool
2012-03-29 21:26:06 +03:00
{
2018-04-07 10:44:14 +02:00
for ( const CGHeroInstance * h : cb - > getHeroesInfo ( ) )
{
if ( ! t - > getArmyStrength ( ) | | howManyReinforcementsCanGet ( h , t ) )
return true ;
}
2012-03-29 21:26:06 +03:00
return false ;
} ) ;
2018-07-19 17:36:10 +02:00
if ( towns . size ( ) )
{
recruitHero ( * boost : : max_element ( towns , compareArmyStrength ) ) ;
}
2013-12-23 23:46:01 +03:00
break ;
}
else
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Nowhere more to go... " ) ;
2012-03-29 21:26:06 +03:00
break ;
}
2012-02-14 21:04:45 +03:00
}
2014-02-05 00:49:04 +03:00
//end of objs empty
2018-04-07 10:44:14 +02:00
if ( dests . size ( ) ) //performance improvement
2012-02-14 21:04:45 +03:00
{
2018-07-19 17:36:10 +02:00
const ObjectIdRef & dest = * boost : : min_element ( dests , CDistanceSorter ( h . get ( ) ) ) ; //find next closest one
2015-04-07 08:55:13 +02:00
2014-02-05 00:49:04 +03:00
//wander should not cause heroes to be reserved - they are always considered free
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Of all %d destinations, object oid=%d seems nice " , dests . size ( ) , dest . id . getNum ( ) ) ;
2014-02-05 00:49:04 +03:00
if ( ! goVisitObj ( dest , h ) )
2012-04-17 15:46:21 +03:00
{
2014-02-05 00:49:04 +03:00
if ( ! dest )
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Visit attempt made the object (id=%d) gone... " , dest . id . getNum ( ) ) ;
2014-02-05 00:49:04 +03:00
}
else
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Hero %s apparently used all MPs (%d left) " , h - > name , h - > movement ) ;
2014-02-06 09:05:45 +03:00
return ;
2014-02-05 00:49:04 +03:00
}
2012-04-17 15:46:21 +03:00
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
if ( h - > visitedTown )
2012-02-14 21:04:45 +03:00
{
2013-12-25 16:38:20 +03:00
townVisitsThisWeek [ h ] . insert ( h - > visitedTown ) ;
2012-02-14 21:04:45 +03:00
buildArmyIn ( h - > visitedTown ) ;
}
}
}
2013-11-23 21:16:25 +03:00
void VCAI : : setGoal ( HeroPtr h , Goals : : TSubgoal goal )
2016-11-27 19:10:20 +02:00
{
2015-12-29 01:14:08 +02:00
if ( goal - > invalid ( ) )
2018-04-07 10:44:14 +02:00
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( lockedHeroes , h ) ;
2018-04-07 10:44:14 +02:00
}
2012-05-05 09:42:18 +03:00
else
2013-11-24 11:56:02 +03:00
{
2013-11-23 21:16:25 +03:00
lockedHeroes [ h ] = goal ;
2016-11-27 19:10:20 +02:00
goal - > setisElementar ( false ) ; //Force always evaluate goals before realizing
2013-11-24 11:56:02 +03:00
}
2012-03-13 15:47:47 +03:00
}
2016-11-27 19:10:20 +02:00
void VCAI : : evaluateGoal ( HeroPtr h )
{
2018-04-07 10:44:14 +02:00
if ( vstd : : contains ( lockedHeroes , h ) )
2016-11-27 19:10:20 +02:00
fh - > setPriority ( lockedHeroes [ h ] ) ;
}
2012-03-13 15:47:47 +03:00
2018-04-07 10:44:14 +02:00
void VCAI : : completeGoal ( Goals : : TSubgoal goal )
2012-03-13 15:47:47 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > trace ( " Completing goal: %s " , goal - > name ( ) ) ;
2018-07-26 12:06:55 +02:00
ah - > notifyGoalCompleted ( goal ) ;
2018-04-07 10:44:14 +02:00
if ( const CGHeroInstance * h = goal - > hero . get ( true ) )
2012-05-08 11:10:40 +03:00
{
auto it = lockedHeroes . find ( h ) ;
2018-04-07 10:44:14 +02:00
if ( it ! = lockedHeroes . end ( ) )
{
2018-07-26 12:06:55 +02:00
if ( it - > second = = goal | | it - > second - > fulfillsMe ( goal ) ) //FIXME this is overspecified, fulfillsMe shoudl be complete
2013-11-24 11:21:51 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( goal - > completeMessage ( ) ) ;
2012-05-08 11:10:40 +03:00
lockedHeroes . erase ( it ) ; //goal fulfilled, free hero
2013-11-24 11:21:51 +03:00
}
2018-04-07 10:44:14 +02:00
}
2012-05-08 11:10:40 +03:00
}
2013-11-24 11:21:51 +03:00
else //complete goal for all heroes maybe?
{
2014-06-08 20:12:16 +03:00
vstd : : erase_if ( lockedHeroes , [ goal ] ( std : : pair < HeroPtr , Goals : : TSubgoal > p )
2013-11-24 11:21:51 +03:00
{
2018-07-26 12:06:55 +02:00
if ( p . second = = goal | | p . second - > fulfillsMe ( goal ) ) //we could have fulfilled goals of other heroes by chance
2013-11-24 11:21:51 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( p . second - > completeMessage ( ) ) ;
2014-06-08 20:12:16 +03:00
return true ;
2013-11-24 11:21:51 +03:00
}
2014-06-08 20:12:16 +03:00
return false ;
} ) ;
2013-11-24 11:21:51 +03:00
}
2012-05-08 11:10:40 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : battleStart ( const CCreatureSet * army1 , const CCreatureSet * army2 , int3 tile , const CGHeroInstance * hero1 , const CGHeroInstance * hero2 , bool side )
2012-02-14 21:04:45 +03:00
{
2012-04-17 15:46:21 +03:00
NET_EVENT_HANDLER ;
2013-03-03 20:06:03 +03:00
assert ( playerID > PlayerColor : : PLAYER_LIMIT | | status . getBattle ( ) = = UPCOMING_BATTLE ) ;
2012-02-14 21:04:45 +03:00
status . setBattle ( ONGOING_BATTLE ) ;
2018-04-07 10:44:14 +02:00
const CGObjectInstance * presumedEnemy = vstd : : backOrNull ( cb - > getVisitableObjs ( tile ) ) ; //may be nullptr in some very are cases -> eg. visited monolith and fighting with an enemy at the FoW covered exit
2017-08-11 19:03:05 +02:00
battlename = boost : : str ( boost : : format ( " Starting battle of %s attacking %s at %s " ) % ( hero1 ? hero1 - > name : " a army " ) % ( presumedEnemy ? presumedEnemy - > getObjectName ( ) : " unknown enemy " ) % tile . toString ( ) ) ;
2012-02-14 21:04:45 +03:00
CAdventureAI : : battleStart ( army1 , army2 , tile , hero1 , hero2 , side ) ;
}
2018-04-07 10:44:14 +02:00
void VCAI : : battleEnd ( const BattleResult * br )
2012-02-14 21:04:45 +03:00
{
2012-04-17 15:46:21 +03:00
NET_EVENT_HANDLER ;
2012-02-14 21:04:45 +03:00
assert ( status . getBattle ( ) = = ONGOING_BATTLE ) ;
status . setBattle ( ENDING_BATTLE ) ;
bool won = br - > winner = = myCb - > battleGetMySide ( ) ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Player %d (%s): I %s the %s! " , playerID , playerID . getStr ( ) , ( won ? " won " : " lost " ) , battlename ) ;
2012-02-14 21:04:45 +03:00
battlename . clear ( ) ;
CAdventureAI : : battleEnd ( br ) ;
}
void VCAI : : waitTillFree ( )
{
2017-06-14 06:59:41 +02:00
auto unlock = vstd : : makeUnlockSharedGuard ( CGameState : : mutex ) ;
2012-02-14 21:04:45 +03:00
status . waitTillFree ( ) ;
}
2018-04-07 10:44:14 +02:00
void VCAI : : markObjectVisited ( const CGObjectInstance * obj )
2012-03-14 16:02:38 +03:00
{
2016-12-19 22:46:01 +02:00
if ( ! obj )
return ;
2018-04-07 10:44:14 +02:00
if ( dynamic_cast < const CGVisitableOPH * > ( obj ) ) //we may want to visit it with another hero
return ;
if ( dynamic_cast < const CGBonusingObject * > ( obj ) ) //or another time
return ;
if ( obj - > ID = = Obj : : MONSTER )
2012-03-14 16:02:38 +03:00
return ;
2013-12-25 16:38:20 +03:00
alreadyVisited . insert ( obj ) ;
2012-03-14 16:02:38 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : reserveObject ( HeroPtr h , const CGObjectInstance * obj )
2012-03-29 21:26:06 +03:00
{
2013-12-25 16:38:20 +03:00
reservedObjs . insert ( obj ) ;
reservedHeroesMap [ h ] . insert ( obj ) ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " reserved object id=%d; address=%p; name=%s " , obj - > id , obj , obj - > getObjectName ( ) ) ;
2012-03-29 21:26:06 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : unreserveObject ( HeroPtr h , const CGObjectInstance * obj )
2014-02-23 23:37:33 +03:00
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( reservedObjs , obj ) ; //unreserve objects
vstd : : erase_if_present ( reservedHeroesMap [ h ] , obj ) ;
2014-02-23 23:37:33 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : markHeroUnableToExplore ( HeroPtr h )
2014-03-23 19:00:43 +03:00
{
heroesUnableToExplore . insert ( h ) ;
}
2018-04-07 10:44:14 +02:00
void VCAI : : markHeroAbleToExplore ( HeroPtr h )
2014-03-23 19:00:43 +03:00
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( heroesUnableToExplore , h ) ;
2014-03-23 19:00:43 +03:00
}
2018-04-07 10:44:14 +02:00
bool VCAI : : isAbleToExplore ( HeroPtr h )
2014-03-23 19:00:43 +03:00
{
2018-04-07 10:44:14 +02:00
return ! vstd : : contains ( heroesUnableToExplore , h ) ;
2014-03-23 19:00:43 +03:00
}
2015-10-26 17:38:17 +02:00
void VCAI : : clearPathsInfo ( )
2014-03-23 19:00:43 +03:00
{
heroesUnableToExplore . clear ( ) ;
2015-10-26 17:38:17 +02:00
cachedSectorMaps . clear ( ) ;
2014-03-23 19:00:43 +03:00
}
2012-02-14 21:04:45 +03:00
void VCAI : : validateVisitableObjs ( )
{
2013-08-18 18:46:28 +03:00
std : : string errorMsg ;
2018-04-07 10:44:14 +02:00
auto shouldBeErased = [ & ] ( const CGObjectInstance * obj ) - > bool
2013-04-21 15:11:13 +03:00
{
2018-04-07 10:44:14 +02:00
if ( obj )
2015-12-08 03:18:31 +02:00
return ! cb - > getObj ( obj - > id , false ) ; // no verbose output needed as we check object visibility
2015-08-30 09:14:54 +02:00
else
2013-04-21 15:11:13 +03:00
return true ;
2013-08-18 18:46:28 +03:00
} ;
//errorMsg is captured by ref so lambda will take the new text
errorMsg = " shouldn't be on the visitable objects list! " ;
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( visitableObjs , shouldBeErased ) ;
2013-08-18 18:46:28 +03:00
2014-10-01 14:26:04 +03:00
//FIXME: how comes our own heroes become inaccessible?
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( reservedHeroesMap , [ ] ( std : : pair < HeroPtr , std : : set < const CGObjectInstance * > > hp ) - > bool
2014-10-01 14:26:04 +03:00
{
return ! hp . first . get ( true ) ;
} ) ;
2018-04-07 10:44:14 +02:00
for ( auto & p : reservedHeroesMap )
2013-08-18 18:46:28 +03:00
{
errorMsg = " shouldn't be on list for hero " + p . first - > name + " ! " ;
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( p . second , shouldBeErased ) ;
2013-08-18 18:46:28 +03:00
}
errorMsg = " shouldn't be on the reserved objs list! " ;
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( reservedObjs , shouldBeErased ) ;
2013-08-18 18:46:28 +03:00
//TODO overkill, hidden object should not be removed. However, we can't know if hidden object is erased from game.
errorMsg = " shouldn't be on the already visited objs list! " ;
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( alreadyVisited , shouldBeErased ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : retrieveVisitableObjs ( std : : vector < const CGObjectInstance * > & out , bool includeOwned ) const
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
foreach_tile_pos ( [ & ] ( const int3 & pos )
2012-04-14 10:22:08 +03:00
{
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : myCb - > getVisitableObjs ( pos , false ) )
2012-04-14 10:22:08 +03:00
{
if ( includeOwned | | obj - > tempOwner ! = playerID )
out . push_back ( obj ) ;
}
} ) ;
2012-02-14 21:04:45 +03:00
}
2015-03-08 16:38:09 +02:00
2018-02-10 20:52:23 +02:00
void VCAI : : retrieveVisitableObjs ( )
2013-12-25 16:38:20 +03:00
{
2018-04-07 10:44:14 +02:00
foreach_tile_pos ( [ & ] ( const int3 & pos )
2013-12-25 16:38:20 +03:00
{
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : myCb - > getVisitableObjs ( pos , false ) )
2013-12-25 16:38:20 +03:00
{
2015-03-08 16:56:59 +02:00
if ( obj - > tempOwner ! = playerID )
2015-03-08 16:38:09 +02:00
addVisitableObj ( obj ) ;
2013-12-25 16:38:20 +03:00
}
} ) ;
}
2012-02-14 21:04:45 +03:00
std : : vector < const CGObjectInstance * > VCAI : : getFlaggedObjects ( ) const
{
std : : vector < const CGObjectInstance * > ret ;
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : visitableObjs )
2012-02-14 21:04:45 +03:00
{
2016-08-11 11:19:08 +02:00
if ( obj - > tempOwner = = playerID )
2016-08-11 02:58:24 +02:00
ret . push_back ( obj ) ;
}
2012-02-14 21:04:45 +03:00
return ret ;
}
2018-04-07 10:44:14 +02:00
void VCAI : : addVisitableObj ( const CGObjectInstance * obj )
2012-02-14 21:04:45 +03:00
{
2013-12-25 16:38:20 +03:00
visitableObjs . insert ( obj ) ;
2012-02-14 21:04:45 +03:00
helperObjInfo [ obj ] = ObjInfo ( obj ) ;
2015-03-08 16:38:09 +02:00
// All teleport objects seen automatically assigned to appropriate channels
auto teleportObj = dynamic_cast < const CGTeleport * > ( obj ) ;
if ( teleportObj )
CGTeleport : : addToChannel ( knownTeleportChannels , teleportObj ) ;
2012-02-14 21:04:45 +03:00
}
const CGObjectInstance * VCAI : : lookForArt ( int aid ) const
{
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : ai - > visitableObjs )
2012-02-14 21:04:45 +03:00
{
2015-12-08 06:33:13 +02:00
if ( obj - > ID = = Obj : : ARTIFACT & & obj - > subID = = aid )
2012-02-14 21:04:45 +03:00
return obj ;
}
2013-06-26 14:18:27 +03:00
return nullptr ;
2012-02-14 21:04:45 +03:00
//TODO what if more than one artifact is available? return them all or some slection criteria
}
2018-04-07 10:44:14 +02:00
bool VCAI : : isAccessible ( const int3 & pos )
2012-02-14 21:04:45 +03:00
{
//TODO precalculate for speed
2018-04-07 10:44:14 +02:00
for ( const CGHeroInstance * h : cb - > getHeroesInfo ( ) )
2012-02-14 21:04:45 +03:00
{
if ( isAccessibleForHero ( pos , h ) )
return true ;
}
return false ;
}
2012-07-01 02:48:40 +03:00
HeroPtr VCAI : : getHeroWithGrail ( ) const
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
for ( const CGHeroInstance * h : cb - > getHeroesInfo ( ) )
{
2012-02-14 21:04:45 +03:00
if ( h - > hasArt ( 2 ) ) //grail
return h ;
2018-04-07 10:44:14 +02:00
}
2013-06-26 14:18:27 +03:00
return nullptr ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
const CGObjectInstance * VCAI : : getUnvisitedObj ( const std : : function < bool ( const CGObjectInstance * ) > & predicate )
2012-02-14 21:04:45 +03:00
{
//TODO smarter definition of unvisited
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : visitableObjs )
{
2012-02-14 21:04:45 +03:00
if ( predicate ( obj ) & & ! vstd : : contains ( alreadyVisited , obj ) )
return obj ;
2018-04-07 10:44:14 +02:00
}
2013-06-26 14:18:27 +03:00
return nullptr ;
2012-02-14 21:04:45 +03:00
}
2017-07-15 13:08:20 +02:00
bool VCAI : : isAccessibleForHero ( const int3 & pos , HeroPtr h , bool includeAllies ) const
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
// Don't visit tile occupied by allied hero
if ( ! includeAllies )
{
for ( auto obj : cb - > getVisitableObjs ( pos ) )
2012-05-09 10:56:39 +03:00
{
2018-04-07 10:44:14 +02:00
if ( obj - > ID = = Obj : : HERO & & cb - > getPlayerRelations ( ai - > playerID , obj - > tempOwner ) ! = PlayerRelations : : ENEMIES )
{
if ( obj ! = h . get ( ) )
return false ;
}
2012-05-09 10:56:39 +03:00
}
}
2014-09-21 16:42:08 +03:00
return cb - > getPathsInfo ( h . get ( ) ) - > getPathInfo ( pos ) - > reachable ( ) ;
2012-02-14 21:04:45 +03:00
}
2012-07-01 02:48:40 +03:00
bool VCAI : : moveHeroToTile ( int3 dst , HeroPtr h )
2012-02-14 21:04:45 +03:00
{
2016-11-28 20:29:11 +02:00
//TODO: consider if blockVisit objects change something in our checks: AIUtility::isBlockVisitObj()
2015-03-08 16:47:58 +02:00
auto afterMovementCheck = [ & ] ( ) - > void
{
waitTillFree ( ) ; //movement may cause battle or blocking dialog
if ( ! h )
{
lostHero ( h ) ;
2015-03-10 02:23:36 +02:00
teleportChannelProbingList . clear ( ) ;
2018-04-07 10:44:14 +02:00
if ( status . channelProbing ( ) ) // if hero lost during channel probing we need to switch this mode off
2015-03-08 16:47:58 +02:00
status . setChannelProbing ( false ) ;
2015-03-09 16:09:34 +02:00
throw cannotFulfillGoalException ( " Hero was lost! " ) ;
2015-03-08 16:47:58 +02:00
}
} ;
2017-08-11 19:03:05 +02:00
logAi - > debug ( " Moving hero %s to tile %s " , h - > name , dst . toString ( ) ) ;
2012-02-14 21:04:45 +03:00
int3 startHpos = h - > visitablePos ( ) ;
bool ret = false ;
if ( startHpos = = dst )
{
2013-11-24 11:21:51 +03:00
//FIXME: this assertion fails also if AI moves onto defeated guarded object
2012-04-14 10:22:08 +03:00
assert ( cb - > getVisitableObjs ( dst ) . size ( ) > 1 ) ; //there's no point in revisiting tile where there is no visitable object
2012-07-01 02:48:40 +03:00
cb - > moveHero ( * h , CGHeroInstance : : convertPosition ( dst , true ) ) ;
2018-04-07 10:44:14 +02:00
afterMovementCheck ( ) ; // TODO: is it feasible to hero get killed there if game work properly?
// not sure if AI can currently reconsider to attack bank while staying on it. Check issue 2084 on mantis for more information.
2012-02-14 21:04:45 +03:00
ret = true ;
}
else
{
CGPath path ;
2015-11-02 10:14:32 +02:00
cb - > getPathsInfo ( h . get ( ) ) - > getPath ( path , dst ) ;
2012-02-14 21:04:45 +03:00
if ( path . nodes . empty ( ) )
{
2017-08-11 19:03:05 +02:00
logAi - > error ( " Hero %s cannot reach %s. " , h - > name , dst . toString ( ) ) ;
2018-04-07 10:44:14 +02:00
throw goalFulfilledException ( sptr ( Goals : : VisitTile ( dst ) . sethero ( h ) ) ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
int i = path . nodes . size ( ) - 1 ;
2012-02-14 21:04:45 +03:00
2015-03-10 11:06:45 +02:00
auto getObj = [ & ] ( int3 coord , bool ignoreHero )
2015-03-08 16:47:58 +02:00
{
2015-08-30 15:15:04 +02:00
auto tile = cb - > getTile ( coord , false ) ;
assert ( tile ) ;
return tile - > topVisitableObj ( ignoreHero ) ;
//return cb->getTile(coord,false)->topVisitableObj(ignoreHero);
2015-03-08 16:47:58 +02:00
} ;
2015-12-11 08:42:30 +02:00
auto isTeleportAction = [ & ] ( CGPathNode : : ENodeAction action ) - > bool
{
2018-04-07 10:44:14 +02:00
if ( action ! = CGPathNode : : TELEPORT_NORMAL & & action ! = CGPathNode : : TELEPORT_BLOCKING_VISIT )
2015-12-11 08:42:30 +02:00
{
2018-04-07 10:44:14 +02:00
if ( action ! = CGPathNode : : TELEPORT_BATTLE )
{
return false ;
}
2015-12-11 08:42:30 +02:00
}
return true ;
} ;
auto getDestTeleportObj = [ & ] ( const CGObjectInstance * currentObject , const CGObjectInstance * nextObjectTop , const CGObjectInstance * nextObject ) - > const CGObjectInstance *
{
if ( CGTeleport : : isConnected ( currentObject , nextObjectTop ) )
return nextObjectTop ;
2018-04-07 10:44:14 +02:00
if ( nextObjectTop & & nextObjectTop - > ID = = Obj : : HERO )
2015-12-11 08:42:30 +02:00
{
2018-04-07 10:44:14 +02:00
if ( CGTeleport : : isConnected ( currentObject , nextObject ) )
return nextObject ;
2015-12-11 08:42:30 +02:00
}
return nullptr ;
} ;
2015-03-10 11:06:45 +02:00
auto doMovement = [ & ] ( int3 dst , bool transit )
2015-03-08 16:47:58 +02:00
{
cb - > moveHero ( * h , CGHeroInstance : : convertPosition ( dst , true ) , transit ) ;
} ;
2015-12-03 16:20:03 +02:00
auto doTeleportMovement = [ & ] ( ObjectInstanceID exitId , int3 exitPos )
2015-03-08 16:47:58 +02:00
{
destinationTeleport = exitId ;
2015-12-03 20:18:40 +02:00
if ( exitPos . valid ( ) )
destinationTeleportPos = CGHeroInstance : : convertPosition ( exitPos , true ) ;
2015-12-03 16:20:03 +02:00
cb - > moveHero ( * h , h - > pos ) ;
2015-03-08 16:47:58 +02:00
destinationTeleport = ObjectInstanceID ( ) ;
2015-12-04 00:54:25 +02:00
destinationTeleportPos = int3 ( - 1 ) ;
2015-03-08 16:47:58 +02:00
afterMovementCheck ( ) ;
} ;
auto doChannelProbing = [ & ] ( ) - > void
{
2018-04-07 10:44:14 +02:00
auto currentPos = CGHeroInstance : : convertPosition ( h - > pos , false ) ;
2015-12-04 04:30:43 +02:00
auto currentExit = getObj ( currentPos , true ) - > id ;
2015-03-08 16:47:58 +02:00
status . setChannelProbing ( true ) ;
for ( auto exit : teleportChannelProbingList )
2015-12-04 00:54:25 +02:00
doTeleportMovement ( exit , int3 ( - 1 ) ) ;
2015-03-08 16:47:58 +02:00
teleportChannelProbingList . clear ( ) ;
status . setChannelProbing ( false ) ;
2015-12-04 04:30:43 +02:00
doTeleportMovement ( currentExit , currentPos ) ;
2015-03-08 16:47:58 +02:00
} ;
2018-04-07 10:44:14 +02:00
for ( ; i > 0 ; i - - )
2012-02-14 21:04:45 +03:00
{
2015-03-08 16:47:58 +02:00
int3 currentCoord = path . nodes [ i ] . coord ;
2018-04-07 10:44:14 +02:00
int3 nextCoord = path . nodes [ i - 1 ] . coord ;
2015-03-08 16:47:58 +02:00
2018-04-07 10:44:14 +02:00
auto currentObject = getObj ( currentCoord , currentCoord = = CGHeroInstance : : convertPosition ( h - > pos , false ) ) ;
2015-12-11 08:42:30 +02:00
auto nextObjectTop = getObj ( nextCoord , false ) ;
auto nextObject = getObj ( nextCoord , true ) ;
auto destTeleportObj = getDestTeleportObj ( currentObject , nextObjectTop , nextObject ) ;
2018-04-07 10:44:14 +02:00
if ( isTeleportAction ( path . nodes [ i - 1 ] . action ) & & destTeleportObj ! = nullptr )
{
//we use special login if hero standing on teleporter it's mean we need
2015-12-11 08:42:30 +02:00
doTeleportMovement ( destTeleportObj - > id , nextCoord ) ;
2015-03-08 16:47:58 +02:00
if ( teleportChannelProbingList . size ( ) )
doChannelProbing ( ) ;
2016-12-19 22:46:01 +02:00
markObjectVisited ( destTeleportObj ) ; //FIXME: Monoliths are not correctly visited
2015-03-08 16:47:58 +02:00
continue ;
}
2012-02-14 21:04:45 +03:00
//stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
2018-04-07 10:44:14 +02:00
if ( path . nodes [ i - 1 ] . turns )
2012-02-14 21:04:45 +03:00
{
2012-03-13 15:47:47 +03:00
//blockedHeroes.insert(h); //to avoid attempts of moving heroes with very little MPs
2012-02-14 21:04:45 +03:00
break ;
}
2018-04-07 10:44:14 +02:00
int3 endpos = path . nodes [ i - 1 ] . coord ;
2012-02-14 21:04:45 +03:00
if ( endpos = = h - > visitablePos ( ) )
continue ;
2013-12-27 16:20:40 +03:00
2018-04-07 10:44:14 +02:00
bool isConnected = false ;
bool isNextObjectTeleport = false ;
// Check there is node after next one; otherwise transit is pointless
if ( i - 2 > = 0 )
{
isConnected = CGTeleport : : isConnected ( nextObjectTop , getObj ( path . nodes [ i - 2 ] . coord , false ) ) ;
isNextObjectTeleport = CGTeleport : : isTeleport ( nextObjectTop ) ;
}
if ( isConnected | | isNextObjectTeleport )
{
// Hero should be able to go through object if it's allow transit
2015-03-08 16:47:58 +02:00
doMovement ( endpos , true ) ;
2012-03-13 15:47:47 +03:00
}
2018-04-07 10:44:14 +02:00
else if ( path . nodes [ i - 1 ] . layer = = EPathfindingLayer : : AIR )
{
2015-11-05 09:02:13 +02:00
doMovement ( endpos , true ) ;
2018-04-07 10:44:14 +02:00
}
2015-03-08 16:47:58 +02:00
else
2018-04-07 10:44:14 +02:00
{
2015-03-10 11:06:45 +02:00
doMovement ( endpos , false ) ;
2018-04-07 10:44:14 +02:00
}
2015-03-08 16:47:58 +02:00
afterMovementCheck ( ) ;
2012-02-14 21:04:45 +03:00
2015-03-08 16:47:58 +02:00
if ( teleportChannelProbingList . size ( ) )
doChannelProbing ( ) ;
2012-02-14 21:04:45 +03:00
}
}
2018-04-07 10:44:14 +02:00
if ( h )
2012-03-29 21:26:06 +03:00
{
2015-12-29 01:14:08 +02:00
if ( auto visitedObject = vstd : : frontOrNull ( cb - > getVisitableObjs ( h - > visitablePos ( ) ) ) ) //we stand on something interesting
2014-05-18 14:13:31 +03:00
{
2018-04-07 10:44:14 +02:00
if ( visitedObject ! = * h )
performObjectInteraction ( visitedObject , h ) ;
2014-05-18 14:13:31 +03:00
}
2012-03-29 21:26:06 +03:00
}
2012-08-26 12:07:48 +03:00
if ( h ) //we could have lost hero after last move
2012-05-05 11:57:32 +03:00
{
2018-04-07 10:44:14 +02:00
completeGoal ( sptr ( Goals : : VisitTile ( dst ) . sethero ( h ) ) ) ; //we stepped on some tile, anyway
completeGoal ( sptr ( Goals : : ClearWayTo ( dst ) . sethero ( h ) ) ) ;
2015-10-25 12:16:43 +02:00
2016-01-18 21:57:19 +02:00
ret = ( dst = = h - > visitablePos ( ) ) ;
2015-12-29 01:14:08 +02:00
if ( ! ret ) //reserve object we are heading towards
2013-12-23 23:46:01 +03:00
{
2015-12-29 01:14:08 +02:00
auto obj = vstd : : frontOrNull ( cb - > getVisitableObjs ( dst ) ) ;
if ( obj & & obj ! = * h )
2013-12-23 23:46:01 +03:00
reserveObject ( h , obj ) ;
}
2015-12-29 01:14:08 +02:00
if ( startHpos = = h - > visitablePos ( ) & & ! ret ) //we didn't move and didn't reach the target
2012-05-08 11:10:40 +03:00
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( lockedHeroes , h ) ; //hero seemingly is confused
2013-12-26 12:53:37 +03:00
throw cannotFulfillGoalException ( " Invalid path found! " ) ; //FIXME: should never happen
2012-05-08 11:10:40 +03:00
}
2016-11-27 19:10:20 +02:00
evaluateGoal ( h ) ; //new hero position means new game situation
2017-08-11 19:03:05 +02:00
logAi - > debug ( " Hero %s moved from %s to %s. Returning %d. " , h - > name , startHpos . toString ( ) , h - > visitablePos ( ) . toString ( ) , ret ) ;
2012-05-05 11:57:32 +03:00
}
2012-02-14 21:04:45 +03:00
return ret ;
}
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : Explore & g )
2013-10-19 08:52:30 +03:00
{
2016-01-18 21:57:19 +02:00
throw cannotFulfillGoalException ( " EXPLORE is not an elementar goal! " ) ;
2013-10-19 08:52:30 +03:00
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : RecruitHero & g )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( const CGTownInstance * t = findTownWithTavern ( ) )
2012-02-14 21:04:45 +03:00
{
2013-10-19 08:52:30 +03:00
recruitHero ( t , true ) ;
//TODO try to free way to blocked town
//TODO: adventure map tavern or prison?
}
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : VisitTile & g )
2013-10-19 08:52:30 +03:00
{
if ( ! g . hero - > movement )
throw cannotFulfillGoalException ( " Cannot visit tile: hero is out of MPs! " ) ;
2018-04-07 10:44:14 +02:00
if ( g . tile = = g . hero - > visitablePos ( ) & & cb - > getVisitableObjs ( g . hero - > visitablePos ( ) ) . size ( ) < 2 )
2013-10-19 08:52:30 +03:00
{
2017-08-11 19:03:05 +02:00
logAi - > warn ( " Why do I want to move hero %s to tile %s? Already standing on that tile! " , g . hero - > name , g . tile . toString ( ) ) ;
2018-04-07 10:44:14 +02:00
throw goalFulfilledException ( sptr ( g ) ) ;
2013-10-19 08:52:30 +03:00
}
2018-04-07 10:44:14 +02:00
if ( ai - > moveHeroToTile ( g . tile , g . hero . get ( ) ) )
2013-12-25 01:01:16 +03:00
{
2018-04-07 10:44:14 +02:00
throw goalFulfilledException ( sptr ( g ) ) ;
2013-12-25 01:01:16 +03:00
}
2013-10-19 08:52:30 +03:00
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : VisitHero & g )
2013-10-19 08:52:30 +03:00
{
if ( ! g . hero - > movement )
2013-12-27 16:20:40 +03:00
throw cannotFulfillGoalException ( " Cannot visit target hero: hero is out of MPs! " ) ;
const CGObjectInstance * obj = cb - > getObj ( ObjectInstanceID ( g . objid ) ) ;
2018-04-07 10:44:14 +02:00
if ( obj )
2013-10-19 08:52:30 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ai - > moveHeroToTile ( obj - > visitablePos ( ) , g . hero . get ( ) ) )
2013-12-27 16:20:40 +03:00
{
2018-04-07 10:44:14 +02:00
throw goalFulfilledException ( sptr ( g ) ) ;
2013-12-27 16:20:40 +03:00
}
2013-10-19 08:52:30 +03:00
}
2013-12-27 16:20:40 +03:00
else
2018-04-07 10:44:14 +02:00
{
2013-12-27 16:20:40 +03:00
throw cannotFulfillGoalException ( " Cannot visit hero: object not found! " ) ;
2018-04-07 10:44:14 +02:00
}
2013-10-19 08:52:30 +03:00
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : BuildThis & g )
2013-10-19 08:52:30 +03:00
{
2018-07-26 12:06:55 +02:00
auto b = BuildingID ( g . bid ) ;
auto t = g . town ;
2013-10-19 08:52:30 +03:00
2018-07-26 12:06:55 +02:00
if ( t )
2013-10-19 08:52:30 +03:00
{
2018-07-26 12:06:55 +02:00
if ( cb - > canBuildStructure ( t , b ) = = EBuildingState : : ALLOWED )
2013-10-19 08:52:30 +03:00
{
2018-07-26 12:06:55 +02:00
logAi - > debug ( " Player %d will build %s in town of %s at %s " ,
playerID , t - > town - > buildings . at ( b ) - > Name ( ) , t - > name , t - > pos . toString ( ) ) ;
cb - > buildBuilding ( t , b ) ;
throw goalFulfilledException ( sptr ( g ) ) ;
2012-02-14 21:04:45 +03:00
}
2013-10-19 08:52:30 +03:00
}
throw cannotFulfillGoalException ( " Cannot build a given structure! " ) ;
}
2012-02-16 20:10:58 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : DigAtTile & g )
2013-10-19 08:52:30 +03:00
{
assert ( g . hero - > visitablePos ( ) = = g . tile ) ; //surely we want to crash here?
2018-04-07 10:44:14 +02:00
if ( g . hero - > diggingStatus ( ) = = EDiggingStatus : : CAN_DIG )
2013-10-19 08:52:30 +03:00
{
cb - > dig ( g . hero . get ( ) ) ;
2013-12-26 12:53:37 +03:00
completeGoal ( sptr ( g ) ) ; // finished digging
2013-10-19 08:52:30 +03:00
}
else
{
2013-11-23 21:16:25 +03:00
ai - > lockedHeroes [ g . hero ] = sptr ( g ) ; //hero who tries to dig shouldn't do anything else
2013-10-19 08:52:30 +03:00
throw cannotFulfillGoalException ( " A hero can't dig! \n " ) ;
}
}
2012-09-28 19:20:18 +03:00
2018-07-26 12:06:55 +02:00
void VCAI : : tryRealize ( Goals : : CollectRes & g ) //trade
2013-10-19 08:52:30 +03:00
{
2018-07-26 12:06:55 +02:00
if ( ah - > freeResources ( ) [ g . resID ] > = g . value ) //goal is already fulfilled. Why we need this chek, anyway?
throw goalFulfilledException ( sptr ( g ) ) ;
2013-10-19 08:52:30 +03:00
2018-04-07 10:44:14 +02:00
if ( const CGObjectInstance * obj = cb - > getObj ( ObjectInstanceID ( g . objid ) , false ) )
2013-10-19 08:52:30 +03:00
{
2018-04-07 10:44:14 +02:00
if ( const IMarket * m = IMarket : : castFrom ( obj , false ) )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
for ( Res : : ERes i = Res : : WOOD ; i < = Res : : GOLD ; vstd : : advance ( i , 1 ) )
2012-02-14 21:04:45 +03:00
{
2018-07-26 12:06:55 +02:00
if ( i = = g . resID ) //sell any other resource
2018-04-07 10:44:14 +02:00
continue ;
2018-07-26 12:06:55 +02:00
//TODO: check all our reserved resources and avoid them
2013-10-19 08:52:30 +03:00
int toGive , toGet ;
m - > getOffer ( i , g . resID , toGive , toGet , EMarketMode : : RESOURCE_RESOURCE ) ;
toGive = toGive * ( cb - > getResourceAmount ( i ) / toGive ) ;
//TODO trade only as much as needed
cb - > trade ( obj , EMarketMode : : RESOURCE_RESOURCE , i , g . resID , toGive ) ;
2018-07-26 12:06:55 +02:00
if ( ah - > freeResources ( ) [ g . resID ] > = g . value )
throw goalFulfilledException ( sptr ( g ) ) ;
2015-10-31 17:04:06 +02:00
}
2012-09-28 19:20:18 +03:00
2013-10-19 08:52:30 +03:00
throw cannotFulfillGoalException ( " I cannot get needed resources by trade! " ) ;
2012-02-14 21:04:45 +03:00
}
else
{
2013-10-19 08:52:30 +03:00
throw cannotFulfillGoalException ( " I don't know how to use this object to raise resources! " ) ;
2012-02-14 21:04:45 +03:00
}
2013-10-19 08:52:30 +03:00
}
else
{
throw cannotFulfillGoalException ( " No object that could be used to raise resources! " ) ;
}
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : Build & g )
2013-10-19 08:52:30 +03:00
{
2018-07-26 12:06:55 +02:00
bool didWeBuildSomething = false ;
2018-04-07 10:44:14 +02:00
for ( const CGTownInstance * t : cb - > getTownsInfo ( ) )
2013-12-26 12:53:37 +03:00
{
2016-08-15 07:30:06 +02:00
logAi - > debug ( " Looking into %s " , t - > name ) ;
2018-07-26 12:06:55 +02:00
potentialBuildings . clear ( ) ; //start fresh with every town
if ( tryBuildStructure ( t ) )
didWeBuildSomething = true ;
else if ( potentialBuildings . size ( ) )
2013-12-26 12:53:37 +03:00
{
2018-07-26 12:06:55 +02:00
auto pb = potentialBuildings . front ( ) ; //gather resources for any we can't afford
auto goal = ah - > whatToDo ( pb . price , sptr ( Goals : : BuildThis ( pb . bid , t ) ) ) ;
if ( goal - > goalType = = Goals : : BUILD_STRUCTURE )
{
logAi - > error ( " We were supposed to NOT afford any building " ) ;
buildStructure ( t , pb . bid ) ; //do it right now
didWeBuildSomething = true ;
}
else
{
//TODO: right now we do that for every town in order. Consider comparison of all potential goals.
striveToGoal ( goal ) ; //gather resources, or something else?
}
2013-12-26 12:53:37 +03:00
}
}
2018-07-26 12:06:55 +02:00
if ( ! didWeBuildSomething )
throw cannotFulfillGoalException ( " BUILD has been realized as much as possible. " ) ; //who catches it and what for?
2013-10-19 08:52:30 +03:00
}
2018-07-26 12:06:55 +02:00
void VCAI : : tryRealize ( Goals : : BuyArmy & g )
{
auto t = g . town ;
ui64 valueBought = 0 ;
//buy the stacks with largest AI value
while ( valueBought < g . value )
{
auto res = ah - > allResources ( ) ;
std : : vector < creInfo > creaturesInDwellings ;
for ( int i = 0 ; i < t - > creatures . size ( ) ; i + + )
{
auto ci = infoFromDC ( t - > creatures [ i ] ) ;
ci . level = i ; //this is important for Dungeon Summoning Portal
creaturesInDwellings . push_back ( ci ) ;
}
vstd : : erase_if ( creaturesInDwellings , [ ] ( const creInfo & ci ) - > bool
{
return ! ci . count | | ci . creID = = - 1 ;
} ) ;
if ( creaturesInDwellings . empty ( ) )
throw cannotFulfillGoalException ( " Can't buy any more creatures! " ) ;
creInfo ci =
* boost : : max_element ( creaturesInDwellings , [ & res ] ( const creInfo & lhs , const creInfo & rhs )
{
//max value of creatures we can buy with our res
int value1 = lhs . cre - > AIValue * ( std : : min ( lhs . count , res / lhs . cre - > cost ) ) ,
value2 = rhs . cre - > AIValue * ( std : : min ( rhs . count , res / rhs . cre - > cost ) ) ;
return value1 < value2 ;
} ) ;
vstd : : amin ( ci . count , res / ci . cre - > cost ) ; //max count we can afford
if ( ci . count > 0 )
{
cb - > recruitCreatures ( t , t - > getUpperArmy ( ) , ci . creID , ci . count , ci . level ) ;
valueBought + = ci . count * ci . cre - > AIValue ;
}
else
throw cannotFulfillGoalException ( " Can't buy any more creatures! " ) ;
}
throw goalFulfilledException ( sptr ( g ) ) ; //we bought as many creatures as we wanted
}
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : Invalid & g )
2013-10-19 08:52:30 +03:00
{
throw cannotFulfillGoalException ( " I don't know how to fulfill this! " ) ;
}
2012-02-14 21:04:45 +03:00
2013-11-23 21:16:25 +03:00
void VCAI : : tryRealize ( Goals : : AbstractGoal & g )
2013-10-19 08:52:30 +03:00
{
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Attempting realizing goal with code %s " , g . name ( ) ) ;
2016-03-12 03:41:27 +02:00
throw cannotFulfillGoalException ( " Unknown type of goal ! " ) ;
2012-02-14 21:04:45 +03:00
}
const CGTownInstance * VCAI : : findTownWithTavern ( ) const
{
2018-04-07 10:44:14 +02:00
for ( const CGTownInstance * t : cb - > getTownsInfo ( ) )
2013-02-11 02:24:57 +03:00
if ( t - > hasBuilt ( BuildingID : : TAVERN ) & & ! t - > visitingHero )
2012-02-14 21:04:45 +03:00
return t ;
2013-06-26 14:18:27 +03:00
return nullptr ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
Goals : : TSubgoal VCAI : : getGoal ( HeroPtr h ) const
2014-02-17 10:36:03 +03:00
{
auto it = lockedHeroes . find ( h ) ;
2018-04-07 10:44:14 +02:00
if ( it ! = lockedHeroes . end ( ) )
2014-02-17 10:36:03 +03:00
return it - > second ;
else
return sptr ( Goals : : Invalid ( ) ) ;
}
2012-07-01 02:48:40 +03:00
std : : vector < HeroPtr > VCAI : : getUnblockedHeroes ( ) const
2012-02-14 21:04:45 +03:00
{
2012-07-01 02:48:40 +03:00
std : : vector < HeroPtr > ret ;
2018-04-07 10:44:14 +02:00
for ( auto h : cb - > getHeroesInfo ( ) )
2012-03-13 15:47:47 +03:00
{
2013-12-26 12:53:37 +03:00
//&& !vstd::contains(lockedHeroes, h)
//at this point we assume heroes exhausted their locked goals
2018-04-07 10:44:14 +02:00
if ( canAct ( h ) )
2013-12-26 12:53:37 +03:00
ret . push_back ( h ) ;
2012-03-13 15:47:47 +03:00
}
2012-02-14 21:04:45 +03:00
return ret ;
}
2018-04-07 10:44:14 +02:00
bool VCAI : : canAct ( HeroPtr h ) const
2013-12-26 12:53:37 +03:00
{
auto mission = lockedHeroes . find ( h ) ;
2018-04-07 10:44:14 +02:00
if ( mission ! = lockedHeroes . end ( ) )
2013-12-26 12:53:37 +03:00
{
//FIXME: I'm afraid there can be other conditions when heroes can act but not move :?
2018-04-07 10:44:14 +02:00
if ( mission - > second - > goalType = = Goals : : DIG_AT_TILE & & ! mission - > second - > isElementar )
2013-12-26 12:53:37 +03:00
return false ;
}
return h - > movement ;
}
2012-07-01 02:48:40 +03:00
HeroPtr VCAI : : primaryHero ( ) const
2012-02-14 21:04:45 +03:00
{
auto hs = cb - > getHeroesInfo ( ) ;
2018-07-19 17:36:10 +02:00
if ( hs . empty ( ) )
2013-06-26 14:18:27 +03:00
return nullptr ;
2018-07-19 17:36:10 +02:00
else
return * boost : : max_element ( hs , compareHeroStrength ) ;
2012-02-14 21:04:45 +03:00
}
void VCAI : : endTurn ( )
{
2016-09-16 04:45:00 +02:00
logAi - > info ( " Player %d (%s) ends turn " , playerID , playerID . getStr ( ) ) ;
2012-02-14 21:04:45 +03:00
if ( ! status . haveTurn ( ) )
{
2016-08-15 07:30:06 +02:00
logAi - > error ( " Not having turn at the end of turn??? " ) ;
2012-02-14 21:04:45 +03:00
}
2017-08-11 19:03:05 +02:00
logAi - > debug ( " Resources at the end of turn: %s " , cb - > getResourceAmount ( ) . toString ( ) ) ;
2012-02-16 20:10:58 +03:00
do
2012-02-14 21:04:45 +03:00
{
cb - > endTurn ( ) ;
2018-04-07 10:44:14 +02:00
}
while ( status . haveTurn ( ) ) ; //for some reasons, our request may fail -> stop requesting end of turn only after we've received a confirmation that it's over
2012-02-14 21:04:45 +03:00
2016-09-16 04:45:00 +02:00
logGlobal - > info ( " Player %d (%s) ended turn " , playerID , playerID . getStr ( ) ) ;
2012-02-14 21:04:45 +03:00
}
2013-11-23 15:30:10 +03:00
void VCAI : : striveToGoal ( Goals : : TSubgoal ultimateGoal )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ultimateGoal - > invalid ( ) )
2012-03-13 23:33:00 +03:00
return ;
2012-05-08 11:10:40 +03:00
2013-11-24 18:30:17 +03:00
//we are looking for abstract goals
2018-04-07 10:44:14 +02:00
auto abstractGoal = striveToGoalInternal ( ultimateGoal , false ) ;
2013-11-24 18:30:17 +03:00
2018-04-07 10:44:14 +02:00
if ( abstractGoal - > invalid ( ) )
2013-11-24 18:30:17 +03:00
return ;
2014-02-19 19:23:47 +03:00
//we received abstract goal, need to find concrete goals
2018-04-07 10:44:14 +02:00
striveToGoalInternal ( abstractGoal , true ) ;
2013-11-24 18:30:17 +03:00
//TODO: save abstract goals not related to hero
}
Goals : : TSubgoal VCAI : : striveToGoalInternal ( Goals : : TSubgoal ultimateGoal , bool onlyAbstract )
{
2014-02-19 19:23:47 +03:00
const int searchDepth = 30 ;
2018-04-07 10:44:14 +02:00
const int searchDepth2 = searchDepth - 2 ;
2013-11-23 15:30:10 +03:00
Goals : : TSubgoal abstractGoal = sptr ( Goals : : Invalid ( ) ) ;
2012-05-08 11:10:40 +03:00
2012-02-14 21:04:45 +03:00
while ( 1 )
{
2013-11-24 18:30:17 +03:00
Goals : : TSubgoal goal = ultimateGoal ;
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Striving to goal of type %s " , ultimateGoal - > name ( ) ) ;
2014-02-19 19:23:47 +03:00
int maxGoals = searchDepth ; //preventing deadlock for mutually dependent goals
2013-11-24 18:30:17 +03:00
while ( ! goal - > isElementar & & maxGoals & & ( onlyAbstract | | ! goal - > isAbstract ) )
2012-02-14 21:04:45 +03:00
{
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Considering goal %s " , goal - > name ( ) ) ;
2012-02-14 21:04:45 +03:00
try
{
boost : : this_thread : : interruption_point ( ) ;
2013-11-24 18:30:17 +03:00
goal = goal - > whatToDoToAchieve ( ) ;
2012-03-13 15:47:47 +03:00
- - maxGoals ;
2018-07-26 12:06:55 +02:00
if ( goal = = ultimateGoal ) //compare objects by value
2014-02-19 19:23:47 +03:00
throw cannotFulfillGoalException ( " Goal dependency loop detected! " ) ;
}
2018-04-07 10:44:14 +02:00
catch ( goalFulfilledException & e )
2014-02-19 19:23:47 +03:00
{
//it is impossible to continue some goals (like exploration, for example)
2018-04-07 10:44:14 +02:00
completeGoal ( goal ) ;
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Goal %s decomposition failed: goal was completed as much as possible " , goal - > name ( ) ) ;
2014-02-19 19:23:47 +03:00
return sptr ( Goals : : Invalid ( ) ) ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
catch ( std : : exception & e )
2012-02-14 21:04:45 +03:00
{
2017-08-10 19:17:10 +02:00
logAi - > debug ( " Goal %s decomposition failed: %s " , goal - > name ( ) , e . what ( ) ) ;
2013-11-24 18:30:17 +03:00
return sptr ( Goals : : Invalid ( ) ) ;
2012-02-14 21:04:45 +03:00
}
}
try
{
boost : : this_thread : : interruption_point ( ) ;
2012-05-08 11:10:40 +03:00
2018-04-07 10:44:14 +02:00
if ( ! maxGoals ) //we counted down to 0 and found no solution
2013-02-23 22:13:15 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ultimateGoal - > hero ) // we seemingly don't know what to do with hero, free him
2017-06-11 08:01:41 +02:00
vstd : : erase_if_present ( lockedHeroes , ultimateGoal - > hero ) ;
2018-07-26 12:06:55 +02:00
throw ( std : : runtime_error ( " Too many subgoals, don't know what to do " ) ) ;
2013-02-23 22:13:15 +03:00
}
2018-07-26 12:06:55 +02:00
else //we found elementar goal and can proceed
2012-03-13 15:47:47 +03:00
{
2018-04-07 10:44:14 +02:00
if ( goal - > hero ) //lock this hero to fulfill ultimate goal
2012-03-13 15:47:47 +03:00
{
2013-11-23 21:16:25 +03:00
setGoal ( goal - > hero , goal ) ;
2012-03-13 15:47:47 +03:00
}
}
2012-05-08 11:10:40 +03:00
2018-04-07 10:44:14 +02:00
if ( goal - > isAbstract )
2012-05-08 11:10:40 +03:00
{
abstractGoal = goal ; //allow only one abstract goal per call
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Choosing abstract goal %s " , goal - > name ( ) ) ;
2012-05-08 11:10:40 +03:00
break ;
}
2018-07-26 12:06:55 +02:00
else //try realize
2014-02-05 00:49:04 +03:00
{
2017-08-10 19:17:10 +02:00
logAi - > debug ( " Trying to realize %s (value %2.3f) " , goal - > name ( ) , goal - > priority ) ;
2013-11-23 21:16:25 +03:00
goal - > accept ( this ) ;
2014-02-05 00:49:04 +03:00
}
2012-05-08 11:10:40 +03:00
2012-02-14 21:04:45 +03:00
boost : : this_thread : : interruption_point ( ) ;
}
2018-04-07 10:44:14 +02:00
catch ( boost : : thread_interrupted & e )
2012-02-14 21:04:45 +03:00
{
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Player %d: Making turn thread received an interruption! " , playerID ) ;
2012-02-14 21:04:45 +03:00
throw ; //rethrow, we want to truly end this thread
}
2018-04-07 10:44:14 +02:00
catch ( goalFulfilledException & e )
2012-05-05 09:42:18 +03:00
{
2018-07-26 12:06:55 +02:00
//the sub-goal was completed successfully
completeGoal ( e . goal ) ;
//local goal was also completed... TODO: or not?
2018-04-07 10:44:14 +02:00
completeGoal ( goal ) ;
2014-02-19 19:23:47 +03:00
//completed goal was main goal //TODO: find better condition
2018-04-07 10:44:14 +02:00
if ( ultimateGoal - > fulfillsMe ( goal ) | | maxGoals > searchDepth2 )
2015-10-31 17:04:06 +02:00
return sptr ( Goals : : Invalid ( ) ) ;
2012-05-05 09:42:18 +03:00
}
2018-04-07 10:44:14 +02:00
catch ( std : : exception & e )
2012-02-14 21:04:45 +03:00
{
2017-08-10 19:17:10 +02:00
logAi - > debug ( " Failed to realize subgoal of type %s (greater goal type was %s), I will stop. " , goal - > name ( ) , ultimateGoal - > name ( ) ) ;
2017-08-10 18:39:27 +02:00
logAi - > debug ( " The error message was: %s " , e . what ( ) ) ;
2012-02-14 21:04:45 +03:00
break ;
}
}
2013-11-24 18:30:17 +03:00
return abstractGoal ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : striveToQuest ( const QuestInfo & q )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > missionType & & q . quest - > progress ! = CQuest : : COMPLETE )
2012-07-18 13:10:14 +03:00
{
MetaString ms ;
2012-10-03 17:49:29 +03:00
q . quest - > getRolloverText ( ms , false ) ;
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Trying to realize quest: %s " , ms . toString ( ) ) ;
2012-07-21 10:15:53 +03:00
auto heroes = cb - > getHeroesInfo ( ) ;
2018-04-07 10:44:14 +02:00
switch ( q . quest - > missionType )
{
case CQuest : : MISSION_ART :
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
for ( auto hero : heroes ) //TODO: remove duplicated code?
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( hero ) )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) . sethero ( hero ) ) ) ;
return ;
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
for ( auto art : q . quest - > m5arts )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetArtOfType ( art ) ) ) ; //TODO: transport?
2012-07-18 13:10:14 +03:00
}
2018-04-07 10:44:14 +02:00
break ;
}
case CQuest : : MISSION_HERO :
{
//striveToGoal (CGoal(RECRUIT_HERO));
for ( auto hero : heroes )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( hero ) )
2012-07-21 10:15:53 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) . sethero ( hero ) ) ) ;
return ;
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : FindObj ( Obj : : PRISON ) ) ) ; //rule of a thumb - quest heroes usually are locked in prisons
//BNLOG ("Don't know how to recruit hero with id %d\n", q.quest->m13489val);
break ;
}
case CQuest : : MISSION_ARMY :
{
for ( auto hero : heroes )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( hero ) ) //very bad info - stacks can be split between multiple heroes :(
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) . sethero ( hero ) ) ) ;
return ;
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
for ( auto creature : q . quest - > m6creatures )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GatherTroops ( creature . type - > idNumber , creature . count ) ) ) ;
2012-07-18 13:10:14 +03:00
}
2018-04-07 10:44:14 +02:00
//TODO: exchange armies... oh my
//BNLOG ("Don't know how to recruit %d of %s\n", (int)(creature.count) % creature.type->namePl);
break ;
}
case CQuest : : MISSION_RESOURCES :
{
if ( heroes . size ( ) )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( heroes . front ( ) ) ) //it doesn't matter which hero it is
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) ) ) ;
2012-07-18 13:10:14 +03:00
}
2018-04-07 10:44:14 +02:00
else
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < q . quest - > m7resources . size ( ) ; + + i )
{
if ( q . quest - > m7resources [ i ] )
striveToGoal ( sptr ( Goals : : CollectRes ( i , q . quest - > m7resources [ i ] ) ) ) ;
}
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
else
striveToGoal ( sptr ( Goals : : RecruitHero ( ) ) ) ; //FIXME: checkQuest requires any hero belonging to player :(
break ;
}
case CQuest : : MISSION_KILL_HERO :
case CQuest : : MISSION_KILL_CREATURE :
{
auto obj = cb - > getObjByQuestIdentifier ( q . quest - > m13489val ) ;
if ( obj )
striveToGoal ( sptr ( Goals : : GetObj ( obj - > id . getNum ( ) ) ) ) ;
else
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) ) ) ; //visit seer hut
break ;
}
case CQuest : : MISSION_PRIMARY_STAT :
{
auto heroes = cb - > getHeroesInfo ( ) ;
for ( auto hero : heroes )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( hero ) )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) . sethero ( hero ) ) ) ;
return ;
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
for ( int i = 0 ; i < q . quest - > m2stats . size ( ) ; + + i )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Don't know how to increase primary stat %d " , i ) ;
2012-07-18 13:10:14 +03:00
}
2018-04-07 10:44:14 +02:00
break ;
}
case CQuest : : MISSION_LEVEL :
{
auto heroes = cb - > getHeroesInfo ( ) ;
for ( auto hero : heroes )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( hero ) )
{
striveToGoal ( sptr ( Goals : : GetObj ( q . obj - > id . getNum ( ) ) . sethero ( hero ) ) ) ; //TODO: causes infinite loop :/
return ;
}
2012-07-18 13:10:14 +03:00
}
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Don't know how to reach hero level %d " , q . quest - > m13489val ) ;
break ;
}
case CQuest : : MISSION_PLAYER :
{
if ( playerID . getNum ( ) ! = q . quest - > m13489val )
logAi - > debug ( " Can't be player of color %d " , q . quest - > m13489val ) ;
break ;
}
case CQuest : : MISSION_KEYMASTER :
{
striveToGoal ( sptr ( Goals : : FindObj ( Obj : : KEYMASTER , q . obj - > subID ) ) ) ;
break ;
}
2012-07-18 13:10:14 +03:00
}
}
}
2012-02-14 21:04:45 +03:00
void VCAI : : performTypicalActions ( )
{
2013-06-29 16:05:48 +03:00
for ( auto h : getUnblockedHeroes ( ) )
2012-02-14 21:04:45 +03:00
{
2016-09-16 03:52:17 +02:00
if ( ! h ) //hero might be lost. getUnblockedHeroes() called once on start of turn
continue ;
2017-08-10 19:17:10 +02:00
logAi - > debug ( " Looking into %s, MP=%d " , h - > name . c_str ( ) , h - > movement ) ;
2012-07-01 02:48:40 +03:00
makePossibleUpgrades ( * h ) ;
2015-04-07 22:48:35 +02:00
pickBestArtifacts ( * h ) ;
2012-03-26 01:46:14 +03:00
try
{
wander ( h ) ;
}
2018-04-07 10:44:14 +02:00
catch ( std : : exception & e )
2012-03-26 01:46:14 +03:00
{
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Cannot use this hero anymore, received exception: %s " , e . what ( ) ) ;
2012-03-26 01:46:14 +03:00
continue ;
}
2012-02-14 21:04:45 +03:00
}
}
void VCAI : : buildArmyIn ( const CGTownInstance * t )
{
makePossibleUpgrades ( t - > visitingHero ) ;
makePossibleUpgrades ( t ) ;
2014-09-23 14:28:55 +03:00
recruitCreatures ( t , t - > getUpperArmy ( ) ) ;
2012-02-14 21:04:45 +03:00
moveCreaturesToHero ( t ) ;
}
2012-07-01 02:48:40 +03:00
int3 VCAI : : explorationBestNeighbour ( int3 hpos , int radius , HeroPtr h )
2012-02-14 21:04:45 +03:00
{
std : : map < int3 , int > dstToRevealedTiles ;
2018-06-16 13:56:49 +02:00
2018-06-24 12:30:17 +02:00
for ( crint3 dir : int3 : : getDirs ( ) )
2016-11-28 20:29:11 +02:00
{
int3 tile = hpos + dir ;
2018-06-24 12:30:17 +02:00
if ( cb - > isInTheMap ( tile ) )
2018-04-07 10:44:14 +02:00
{
2018-06-24 12:30:17 +02:00
if ( isBlockVisitObj ( tile ) )
2018-06-16 13:56:49 +02:00
continue ;
2018-06-24 12:30:17 +02:00
if ( isSafeToVisit ( h , tile ) & & isAccessibleForHero ( tile , h ) )
2018-04-07 10:44:14 +02:00
{
2018-06-16 13:56:49 +02:00
auto distance = hpos . dist2d ( tile ) ; // diagonal movement opens more tiles but spends more mp
2018-06-24 12:30:17 +02:00
dstToRevealedTiles [ tile ] = howManyTilesWillBeDiscovered ( tile , radius , cb . get ( ) , h ) / distance ;
2018-04-07 10:44:14 +02:00
}
}
2016-11-28 20:29:11 +02:00
}
2012-02-14 21:04:45 +03:00
2018-04-07 10:44:14 +02:00
if ( dstToRevealedTiles . empty ( ) ) //yes, it DID happen!
2014-02-16 00:32:49 +03:00
throw cannotFulfillGoalException ( " No neighbour will bring new discoveries! " ) ;
2012-02-14 21:04:45 +03:00
auto best = dstToRevealedTiles . begin ( ) ;
2018-04-07 10:44:14 +02:00
for ( auto i = dstToRevealedTiles . begin ( ) ; i ! = dstToRevealedTiles . end ( ) ; i + + )
2012-02-14 21:04:45 +03:00
{
2018-06-24 12:30:17 +02:00
const CGPathNode * pn = cb - > getPathsInfo ( h . get ( ) ) - > getPathInfo ( i - > first ) ;
2012-02-16 20:10:58 +03:00
//const TerrainTile *t = cb->getTile(i->first);
2013-12-26 12:53:37 +03:00
if ( best - > second < i - > second & & pn - > reachable ( ) & & pn - > accessible = = CGPathNode : : ACCESSIBLE )
2012-02-14 21:04:45 +03:00
best = i ;
}
if ( best - > second )
return best - > first ;
throw cannotFulfillGoalException ( " No neighbour will bring new discoveries! " ) ;
}
2014-02-17 20:28:39 +03:00
int3 VCAI : : explorationNewPoint ( HeroPtr h )
2012-02-14 21:04:45 +03:00
{
2016-01-31 17:01:58 +02:00
int radius = h - > getSightRadius ( ) ;
2015-10-25 09:21:15 +02:00
CCallback * cbp = cb . get ( ) ;
const CGHeroInstance * hero = h . get ( ) ;
2013-12-23 23:46:01 +03:00
2018-04-07 10:44:14 +02:00
std : : vector < std : : vector < int3 > > tiles ; //tiles[distance_to_fow]
2012-02-14 21:04:45 +03:00
tiles . resize ( radius ) ;
2018-04-07 10:44:14 +02:00
foreach_tile_pos ( [ & ] ( const int3 & pos )
2012-02-14 21:04:45 +03:00
{
2014-04-01 14:53:28 +03:00
if ( ! cbp - > isVisible ( pos ) )
2012-02-14 21:04:45 +03:00
tiles [ 0 ] . push_back ( pos ) ;
} ) ;
2014-02-07 23:09:15 +03:00
float bestValue = 0 ; //discovered tile to node distance ratio
2018-04-07 10:44:14 +02:00
int3 bestTile ( - 1 , - 1 , - 1 ) ;
2015-10-25 09:21:15 +02:00
int3 ourPos = h - > convertPosition ( h - > pos , false ) ;
2013-12-23 23:46:01 +03:00
2018-04-07 10:44:14 +02:00
for ( int i = 1 ; i < radius ; i + + )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
getVisibleNeighbours ( tiles [ i - 1 ] , tiles [ i ] ) ;
2015-12-29 01:14:08 +02:00
vstd : : removeDuplicates ( tiles [ i ] ) ;
2012-02-14 21:04:45 +03:00
2018-04-07 10:44:14 +02:00
for ( const int3 & tile : tiles [ i ] )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( tile = = ourPos ) //shouldn't happen, but it does
2015-10-25 09:21:15 +02:00
continue ;
2018-04-07 10:44:14 +02:00
if ( ! cb - > getPathsInfo ( hero ) - > getPathInfo ( tile ) - > reachable ( ) ) //this will remove tiles that are guarded by monsters (or removable objects)
2014-02-07 23:09:15 +03:00
continue ;
CGPath path ;
2018-06-24 12:30:17 +02:00
cb - > getPathsInfo ( hero ) - > getPath ( path , tile ) ;
float ourValue = ( float ) howManyTilesWillBeDiscovered ( tile , radius , cbp , h ) / ( path . nodes . size ( ) + 1 ) ; //+1 prevents erratic jumps
2014-02-07 23:09:15 +03:00
2018-04-07 10:44:14 +02:00
if ( ourValue > bestValue ) //avoid costly checks of tiles that don't reveal much
2012-02-14 21:04:45 +03:00
{
2016-11-28 20:29:11 +02:00
if ( isSafeToVisit ( h , tile ) )
2013-12-23 23:46:01 +03:00
{
2018-04-07 10:44:14 +02:00
if ( isBlockVisitObj ( tile ) ) //we can't stand on that object
2016-11-28 20:29:11 +02:00
continue ;
2014-02-17 10:36:03 +03:00
bestTile = tile ;
2013-12-23 23:46:01 +03:00
bestValue = ourValue ;
}
2012-02-14 21:04:45 +03:00
}
}
}
2014-02-17 10:36:03 +03:00
return bestTile ;
}
2014-02-17 20:28:39 +03:00
int3 VCAI : : explorationDesperate ( HeroPtr h )
2014-02-17 10:36:03 +03:00
{
2015-12-04 00:10:51 +02:00
auto sm = getCachedSectorMap ( h ) ;
2016-01-31 17:01:58 +02:00
int radius = h - > getSightRadius ( ) ;
2015-10-31 17:04:06 +02:00
2018-04-07 10:44:14 +02:00
std : : vector < std : : vector < int3 > > tiles ; //tiles[distance_to_fow]
2014-02-17 10:36:03 +03:00
tiles . resize ( radius ) ;
2014-04-01 14:53:28 +03:00
CCallback * cbp = cb . get ( ) ;
2018-04-07 10:44:14 +02:00
foreach_tile_pos ( [ & ] ( const int3 & pos )
2014-02-17 10:36:03 +03:00
{
2014-04-01 14:53:28 +03:00
if ( ! cbp - > isVisible ( pos ) )
2014-02-17 10:36:03 +03:00
tiles [ 0 ] . push_back ( pos ) ;
} ) ;
ui64 lowestDanger = - 1 ;
2018-04-07 10:44:14 +02:00
int3 bestTile ( - 1 , - 1 , - 1 ) ;
2014-02-17 10:36:03 +03:00
2015-12-29 01:14:08 +02:00
for ( int i = 1 ; i < radius ; i + + )
2014-02-17 10:36:03 +03:00
{
2018-04-07 10:44:14 +02:00
getVisibleNeighbours ( tiles [ i - 1 ] , tiles [ i ] ) ;
2015-12-29 01:14:08 +02:00
vstd : : removeDuplicates ( tiles [ i ] ) ;
2014-02-17 10:36:03 +03:00
2018-04-07 10:44:14 +02:00
for ( const int3 & tile : tiles [ i ] )
2014-02-17 10:36:03 +03:00
{
2018-04-07 10:44:14 +02:00
if ( cbp - > getTile ( tile ) - > blocked ) //does it shorten the time?
2014-02-17 10:36:03 +03:00
continue ;
2018-06-24 12:30:17 +02:00
if ( ! howManyTilesWillBeDiscovered ( tile , radius , cbp , h ) ) //avoid costly checks of tiles that don't reveal much
2014-02-17 10:36:03 +03:00
continue ;
2015-12-04 00:10:51 +02:00
auto t = sm - > firstTileToGet ( h , tile ) ;
2018-04-07 10:44:14 +02:00
if ( t . valid ( ) )
2014-02-17 10:36:03 +03:00
{
ui64 ourDanger = evaluateDanger ( t , h . h ) ;
2018-04-07 10:44:14 +02:00
if ( ourDanger < lowestDanger )
2014-02-17 10:36:03 +03:00
{
2016-11-28 20:29:11 +02:00
if ( ! isBlockVisitObj ( t ) )
2014-02-17 10:36:03 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ! ourDanger ) //at least one safe place found
2014-02-17 10:36:03 +03:00
return t ;
bestTile = t ;
lowestDanger = ourDanger ;
}
}
}
}
}
2013-12-23 23:46:01 +03:00
return bestTile ;
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : checkHeroArmy ( HeroPtr h )
2012-07-18 13:10:14 +03:00
{
auto it = lockedHeroes . find ( h ) ;
2018-04-07 10:44:14 +02:00
if ( it ! = lockedHeroes . end ( ) )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( it - > second - > goalType = = Goals : : GATHER_ARMY & & it - > second - > value < = h - > getArmyStrength ( ) )
2013-11-23 21:16:25 +03:00
completeGoal ( sptr ( Goals : : GatherArmy ( it - > second - > value ) . sethero ( h ) ) ) ;
2012-07-18 13:10:14 +03:00
}
}
2013-02-09 20:37:38 +03:00
void VCAI : : recruitHero ( const CGTownInstance * t , bool throwing )
2012-02-14 21:04:45 +03:00
{
2017-08-11 19:03:05 +02:00
logAi - > debug ( " Trying to recruit a hero in %s at %s " , t - > name , t - > visitablePos ( ) . toString ( ) ) ;
2013-02-09 20:37:38 +03:00
2013-12-23 23:46:01 +03:00
auto heroes = cb - > getAvailableHeroes ( t ) ;
if ( heroes . size ( ) )
{
auto hero = heroes [ 0 ] ;
2018-04-07 10:44:14 +02:00
if ( heroes . size ( ) > = 2 ) //makes sense to recruit two heroes with starting amries in first week
2013-12-23 23:46:01 +03:00
{
2018-04-07 10:44:14 +02:00
if ( heroes [ 1 ] - > getTotalStrength ( ) > hero - > getTotalStrength ( ) )
2013-12-23 23:46:01 +03:00
hero = heroes [ 1 ] ;
}
cb - > recruitHero ( t , hero ) ;
2018-07-26 12:06:55 +02:00
throw goalFulfilledException ( sptr ( Goals : : RecruitHero ( ) . settown ( t ) ) ) ;
2013-12-23 23:46:01 +03:00
}
2013-02-09 20:37:38 +03:00
else if ( throwing )
2018-04-07 10:44:14 +02:00
{
2013-02-09 20:37:38 +03:00
throw cannotFulfillGoalException ( " No available heroes in tavern in " + t - > nodeName ( ) ) ;
2018-04-07 10:44:14 +02:00
}
2012-02-14 21:04:45 +03:00
}
2012-02-22 16:41:27 +03:00
void VCAI : : finish ( )
{
if ( makingTurn )
2016-10-22 16:22:00 +02:00
{
2012-02-22 16:41:27 +03:00
makingTurn - > interrupt ( ) ;
2016-10-22 16:22:00 +02:00
makingTurn - > join ( ) ;
2017-07-12 17:26:20 +02:00
makingTurn . reset ( ) ;
2016-10-22 16:22:00 +02:00
}
2012-02-22 16:41:27 +03:00
}
2013-06-26 14:18:27 +03:00
void VCAI : : requestActionASAP ( std : : function < void ( ) > whatToDo )
2012-03-26 01:46:14 +03:00
{
2018-04-07 10:44:14 +02:00
boost : : thread newThread ( [ this , whatToDo ] ( )
2012-03-26 01:46:14 +03:00
{
2015-10-31 17:04:06 +02:00
setThreadName ( " VCAI::requestActionASAP::whatToDo " ) ;
2012-03-26 01:46:14 +03:00
SET_GLOBAL_STATE ( this ) ;
2017-06-14 06:59:41 +02:00
boost : : shared_lock < boost : : shared_mutex > gsLock ( CGameState : : mutex ) ;
2012-03-26 01:46:14 +03:00
whatToDo ( ) ;
} ) ;
}
2012-07-01 02:48:40 +03:00
void VCAI : : lostHero ( HeroPtr h )
{
2017-08-10 18:39:27 +02:00
logAi - > debug ( " I lost my hero %s. It's best to forget and move on. " , h . name ) ;
2012-07-01 02:48:40 +03:00
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( lockedHeroes , h ) ;
2013-06-29 16:05:48 +03:00
for ( auto obj : reservedHeroesMap [ h ] )
2012-07-01 02:48:40 +03:00
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( reservedObjs , obj ) ; //unreserve all objects for that hero
2012-07-01 02:48:40 +03:00
}
2015-12-29 01:14:08 +02:00
vstd : : erase_if_present ( reservedHeroesMap , h ) ;
vstd : : erase_if_present ( cachedSectorMaps , h ) ;
2012-07-01 02:48:40 +03:00
}
2013-05-27 13:53:28 +03:00
void VCAI : : answerQuery ( QueryID queryID , int selection )
2012-07-15 18:34:00 +03:00
{
2017-08-10 19:17:10 +02:00
logAi - > debug ( " I'll answer the query %d giving the choice %d " , queryID , selection ) ;
2013-05-27 13:53:28 +03:00
if ( queryID ! = QueryID ( - 1 ) )
2012-07-15 18:34:00 +03:00
{
2012-07-19 21:52:44 +03:00
cb - > selectionMade ( selection , queryID ) ;
2012-07-15 18:34:00 +03:00
}
else
{
2017-08-10 18:39:27 +02:00
logAi - > debug ( " Since the query ID is %d, the answer won't be sent. This is not a real query! " , queryID ) ;
2012-07-15 18:34:00 +03:00
//do nothing
}
}
2018-04-07 10:44:14 +02:00
void VCAI : : requestSent ( const CPackForServer * pack , int requestID )
2012-07-15 18:34:00 +03:00
{
//BNLOG("I have sent request of type %s", typeid(*pack).name());
2018-04-07 10:44:14 +02:00
if ( auto reply = dynamic_cast < const QueryReply * > ( pack ) )
2012-07-15 18:34:00 +03:00
{
status . attemptedAnsweringQuery ( reply - > qid , requestID ) ;
}
}
2012-09-29 13:59:43 +03:00
std : : string VCAI : : getBattleAIName ( ) const
{
2017-11-26 23:18:18 +02:00
if ( settings [ " server " ] [ " enemyAI " ] . getType ( ) = = JsonNode : : JsonType : : DATA_STRING )
2017-01-17 13:17:37 +02:00
return settings [ " server " ] [ " enemyAI " ] . String ( ) ;
2012-09-29 13:59:43 +03:00
else
2017-01-17 13:17:37 +02:00
return " BattleAI " ;
2012-09-29 13:59:43 +03:00
}
2018-04-07 10:44:14 +02:00
void VCAI : : validateObject ( const CGObjectInstance * obj )
2013-04-21 15:11:13 +03:00
{
validateObject ( obj - > id ) ;
}
void VCAI : : validateObject ( ObjectIdRef obj )
{
2018-04-07 10:44:14 +02:00
auto matchesId = [ & ] ( const CGObjectInstance * hlpObj ) - > bool
{
return hlpObj - > id = = obj . id ;
} ;
2013-04-21 15:11:13 +03:00
if ( ! obj )
{
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( visitableObjs , matchesId ) ;
2013-04-21 15:11:13 +03:00
2018-04-07 10:44:14 +02:00
for ( auto & p : reservedHeroesMap )
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( p . second , matchesId ) ;
2013-08-18 18:46:28 +03:00
2015-12-29 01:14:08 +02:00
vstd : : erase_if ( reservedObjs , matchesId ) ;
2013-04-21 15:11:13 +03:00
}
}
2015-12-04 00:10:51 +02:00
std : : shared_ptr < SectorMap > VCAI : : getCachedSectorMap ( HeroPtr h )
2015-10-26 17:38:17 +02:00
{
auto it = cachedSectorMaps . find ( h ) ;
2018-04-07 10:44:14 +02:00
if ( it ! = cachedSectorMaps . end ( ) )
{
2015-10-26 17:38:17 +02:00
return it - > second ;
2018-04-07 10:44:14 +02:00
}
2015-10-26 17:38:17 +02:00
else
{
2015-12-04 00:10:51 +02:00
cachedSectorMaps [ h ] = std : : make_shared < SectorMap > ( h ) ;
2015-10-26 17:38:17 +02:00
return cachedSectorMaps [ h ] ;
}
}
2012-02-14 21:04:45 +03:00
AIStatus : : AIStatus ( )
{
battle = NO_BATTLE ;
havingTurn = false ;
2013-09-28 03:30:12 +03:00
ongoingHeroMovement = false ;
2015-03-08 16:23:56 +02:00
ongoingChannelProbing = false ;
2012-02-14 21:04:45 +03:00
}
AIStatus : : ~ AIStatus ( )
{
}
void AIStatus : : setBattle ( BattleState BS )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
2013-04-20 14:34:01 +03:00
LOG_TRACE_PARAMS ( logAi , " battle state=%d " , ( int ) BS ) ;
2012-02-14 21:04:45 +03:00
battle = BS ;
cv . notify_all ( ) ;
}
BattleState AIStatus : : getBattle ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
return battle ;
}
2013-05-27 13:53:28 +03:00
void AIStatus : : addQuery ( QueryID ID , std : : string description )
2016-01-18 21:57:19 +02:00
{
2013-05-27 13:53:28 +03:00
if ( ID = = QueryID ( - 1 ) )
2012-07-15 18:34:00 +03:00
{
2017-08-10 20:59:55 +02:00
logAi - > debug ( " The \" query \" has an id %d, it'll be ignored as non-query. Description: %s " , ID , description ) ;
2012-07-15 18:34:00 +03:00
return ;
}
2013-05-27 13:53:28 +03:00
assert ( ID . getNum ( ) > = 0 ) ;
2015-08-31 09:18:24 +02:00
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
assert ( ! vstd : : contains ( remainingQueries , ID ) ) ;
2012-07-15 18:34:00 +03:00
remainingQueries [ ID ] = description ;
2015-08-31 09:18:24 +02:00
2012-02-14 21:04:45 +03:00
cv . notify_all ( ) ;
2017-08-10 20:59:55 +02:00
logAi - > debug ( " Adding query %d - %s. Total queries count: %d " , ID , description , remainingQueries . size ( ) ) ;
2012-02-14 21:04:45 +03:00
}
2013-05-27 13:53:28 +03:00
void AIStatus : : removeQuery ( QueryID ID )
2012-02-14 21:04:45 +03:00
{
2012-07-15 18:34:00 +03:00
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
assert ( vstd : : contains ( remainingQueries , ID ) ) ;
2012-02-14 21:04:45 +03:00
2012-07-15 18:34:00 +03:00
std : : string description = remainingQueries [ ID ] ;
remainingQueries . erase ( ID ) ;
2016-01-18 21:57:19 +02:00
2012-07-15 18:34:00 +03:00
cv . notify_all ( ) ;
2018-04-07 10:44:14 +02:00
logAi - > debug ( " Removing query %d - %s. Total queries count: %d " , ID , description , remainingQueries . size ( ) ) ;
2012-02-14 21:04:45 +03:00
}
int AIStatus : : getQueriesCount ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
2012-07-15 18:34:00 +03:00
return remainingQueries . size ( ) ;
2012-02-14 21:04:45 +03:00
}
void AIStatus : : startedTurn ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
havingTurn = true ;
cv . notify_all ( ) ;
}
void AIStatus : : madeTurn ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
havingTurn = false ;
cv . notify_all ( ) ;
}
void AIStatus : : waitTillFree ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
2013-11-03 15:51:25 +03:00
while ( battle ! = NO_BATTLE | | ! remainingQueries . empty ( ) | | ! objectsBeingVisited . empty ( ) | | ongoingHeroMovement )
2013-09-28 03:30:12 +03:00
cv . timed_wait ( lock , boost : : posix_time : : milliseconds ( 100 ) ) ;
2012-02-14 21:04:45 +03:00
}
bool AIStatus : : haveTurn ( )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
return havingTurn ;
}
2013-05-27 13:53:28 +03:00
void AIStatus : : attemptedAnsweringQuery ( QueryID queryID , int answerRequestID )
2012-07-15 18:34:00 +03:00
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
assert ( vstd : : contains ( remainingQueries , queryID ) ) ;
std : : string description = remainingQueries [ queryID ] ;
2017-08-10 20:59:55 +02:00
logAi - > debug ( " Attempted answering query %d - %s. Request id=%d. Waiting for results... " , queryID , description , answerRequestID ) ;
2012-07-15 18:34:00 +03:00
requestToQueryID [ answerRequestID ] = queryID ;
}
void AIStatus : : receivedAnswerConfirmation ( int answerRequestID , int result )
{
assert ( vstd : : contains ( requestToQueryID , answerRequestID ) ) ;
2013-05-27 13:53:28 +03:00
QueryID query = requestToQueryID [ answerRequestID ] ;
2012-07-15 18:34:00 +03:00
assert ( vstd : : contains ( remainingQueries , query ) ) ;
requestToQueryID . erase ( answerRequestID ) ;
if ( result )
{
removeQuery ( query ) ;
}
else
{
2017-08-10 20:59:55 +02:00
logAi - > error ( " Something went really wrong, failed to answer query %d : %s " , query . getNum ( ) , remainingQueries [ query ] ) ;
2012-07-15 18:34:00 +03:00
//TODO safely retry
}
}
2018-04-07 10:44:14 +02:00
void AIStatus : : heroVisit ( const CGObjectInstance * obj , bool started )
2013-09-28 02:46:58 +03:00
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
if ( started )
2018-04-07 10:44:14 +02:00
{
2013-09-28 02:46:58 +03:00
objectsBeingVisited . push_back ( obj ) ;
2018-04-07 10:44:14 +02:00
}
2013-09-28 02:46:58 +03:00
else
{
2015-10-31 17:04:06 +02:00
// There can be more than one object visited at the time (eg. hero visits Subterranean Gate
// causing visit to hero on the other side.
2013-10-13 18:24:02 +03:00
// However, we are guaranteed that start/end visit notification maintain stack order.
assert ( ! objectsBeingVisited . empty ( ) ) ;
objectsBeingVisited . pop_back ( ) ;
2013-09-28 02:46:58 +03:00
}
cv . notify_all ( ) ;
}
void AIStatus : : setMove ( bool ongoing )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
ongoingHeroMovement = ongoing ;
cv . notify_all ( ) ;
}
2015-03-08 16:23:56 +02:00
void AIStatus : : setChannelProbing ( bool ongoing )
{
boost : : unique_lock < boost : : mutex > lock ( mx ) ;
2015-12-04 00:54:25 +02:00
ongoingChannelProbing = ongoing ;
2015-03-08 16:23:56 +02:00
cv . notify_all ( ) ;
}
bool AIStatus : : channelProbing ( )
{
return ongoingChannelProbing ;
}
2013-10-18 23:17:25 +03:00
SectorMap : : SectorMap ( )
2012-02-14 21:04:45 +03:00
{
2013-10-18 23:17:25 +03:00
update ( ) ;
}
2012-02-14 21:04:45 +03:00
2014-02-15 22:39:03 +03:00
SectorMap : : SectorMap ( HeroPtr h )
{
update ( ) ;
makeParentBFS ( h - > visitablePos ( ) ) ;
}
2018-04-07 10:44:14 +02:00
bool SectorMap : : markIfBlocked ( TSectorID & sec , crint3 pos , const TerrainTile * t )
2013-10-18 23:17:25 +03:00
{
if ( t - > blocked & & ! t - > visitable )
2012-02-14 21:04:45 +03:00
{
2013-10-18 23:17:25 +03:00
sec = NOT_AVAILABLE ;
return true ;
2012-02-14 21:04:45 +03:00
}
2013-10-18 23:17:25 +03:00
return false ;
}
2012-02-14 21:04:45 +03:00
2018-04-07 10:44:14 +02:00
bool SectorMap : : markIfBlocked ( TSectorID & sec , crint3 pos )
2013-10-18 23:17:25 +03:00
{
2015-08-31 07:39:03 +02:00
return markIfBlocked ( sec , pos , getTile ( pos ) ) ;
2012-02-14 21:04:45 +03:00
}
2013-10-18 23:17:25 +03:00
void SectorMap : : update ( )
2012-02-14 21:04:45 +03:00
{
2015-08-31 08:25:33 +02:00
visibleTiles = cb - > getAllVisibleTiles ( ) ;
2016-12-12 22:38:12 +02:00
auto shape = visibleTiles - > shape ( ) ;
sector . resize ( boost : : extents [ shape [ 0 ] ] [ shape [ 1 ] ] [ shape [ 2 ] ] ) ;
2015-08-31 07:39:03 +02:00
2013-10-18 23:17:25 +03:00
clear ( ) ;
int curSector = 3 ; //0 is invisible, 1 is not explored
2014-04-01 14:53:28 +03:00
CCallback * cbp = cb . get ( ) ; //optimization
2013-10-18 23:17:25 +03:00
foreach_tile_pos ( [ & ] ( crint3 pos )
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
if ( retrieveTile ( pos ) = = NOT_CHECKED )
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
if ( ! markIfBlocked ( retrieveTile ( pos ) , pos ) )
2014-04-01 14:53:28 +03:00
exploreNewSector ( pos , curSector + + , cbp ) ;
2012-02-14 21:04:45 +03:00
}
} ) ;
valid = true ;
}
2018-02-10 20:52:23 +02:00
SectorMap : : TSectorID & SectorMap : : retrieveTileN ( SectorMap : : TSectorArray & a , const int3 & pos )
2016-12-12 22:38:12 +02:00
{
return a [ pos . x ] [ pos . y ] [ pos . z ] ;
}
2018-02-10 20:52:23 +02:00
const SectorMap : : TSectorID & SectorMap : : retrieveTileN ( const SectorMap : : TSectorArray & a , const int3 & pos )
2016-12-12 22:38:12 +02:00
{
return a [ pos . x ] [ pos . y ] [ pos . z ] ;
}
2012-02-14 21:04:45 +03:00
void SectorMap : : clear ( )
{
2016-12-12 22:38:12 +02:00
//TODO: rotate to [z][x][y]
auto fow = cb - > getVisibilityMap ( ) ;
2016-12-12 23:52:18 +02:00
//TODO: any magic to automate this? will need array->array conversion
//std::transform(fow.begin(), fow.end(), sector.begin(), [](const ui8 &f) -> unsigned short
//{
// return f; //type conversion
//});
2016-12-12 22:38:12 +02:00
auto width = fow . size ( ) ;
auto height = fow . front ( ) . size ( ) ;
auto depth = fow . front ( ) . front ( ) . size ( ) ;
2018-04-07 10:44:14 +02:00
for ( size_t x = 0 ; x < width ; x + + )
{
for ( size_t y = 0 ; y < height ; y + + )
{
for ( size_t z = 0 ; z < depth ; z + + )
2016-12-12 22:38:12 +02:00
sector [ x ] [ y ] [ z ] = fow [ x ] [ y ] [ z ] ;
2018-04-07 10:44:14 +02:00
}
}
2012-02-14 21:04:45 +03:00
valid = false ;
}
2014-04-01 14:53:28 +03:00
void SectorMap : : exploreNewSector ( crint3 pos , int num , CCallback * cbp )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
Sector & s = infoOnSectors [ num ] ;
2012-02-14 21:04:45 +03:00
s . id = num ;
2015-08-31 07:39:03 +02:00
s . water = getTile ( pos ) - > isWater ( ) ;
2012-02-14 21:04:45 +03:00
std : : queue < int3 > toVisit ;
toVisit . push ( pos ) ;
2013-11-03 15:51:25 +03:00
while ( ! toVisit . empty ( ) )
2012-02-14 21:04:45 +03:00
{
int3 curPos = toVisit . front ( ) ;
toVisit . pop ( ) ;
2018-02-10 20:52:23 +02:00
TSectorID & sec = retrieveTile ( curPos ) ;
2012-02-14 21:04:45 +03:00
if ( sec = = NOT_CHECKED )
{
2018-04-07 10:44:14 +02:00
const TerrainTile * t = getTile ( curPos ) ;
2012-02-14 21:04:45 +03:00
if ( ! markIfBlocked ( sec , curPos , t ) )
{
if ( t - > isWater ( ) = = s . water ) //sector is only-water or only-land
{
sec = num ;
s . tiles . push_back ( curPos ) ;
2014-04-01 14:53:28 +03:00
foreach_neighbour ( cbp , curPos , [ & ] ( CCallback * cbp , crint3 neighPos )
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
if ( retrieveTile ( neighPos ) = = NOT_CHECKED )
2012-02-14 21:04:45 +03:00
{
toVisit . push ( neighPos ) ;
//parent[neighPos] = curPos;
}
2018-04-07 10:44:14 +02:00
const TerrainTile * nt = getTile ( neighPos ) ;
2014-05-18 14:13:31 +03:00
if ( nt & & nt - > isWater ( ) ! = s . water & & canBeEmbarkmentPoint ( nt , s . water ) )
2012-02-14 21:04:45 +03:00
{
s . embarkmentPoints . push_back ( neighPos ) ;
}
} ) ;
2015-10-31 17:04:06 +02:00
2014-02-20 23:18:49 +03:00
if ( t - > visitable )
{
auto obj = t - > visitableObjects . front ( ) ;
2016-08-18 02:33:23 +02:00
if ( cb - > getObj ( obj - > id , false ) ) // FIXME: we have to filter invisible objcts like events, but probably TerrainTile shouldn't be used in SectorMap at all
s . visitableObjs . push_back ( obj ) ;
2014-02-20 23:18:49 +03:00
}
2012-02-14 21:04:45 +03:00
}
}
}
}
2015-12-29 01:14:08 +02:00
vstd : : removeDuplicates ( s . embarkmentPoints ) ;
2012-02-14 21:04:45 +03:00
}
void SectorMap : : write ( crstring fname )
{
std : : ofstream out ( fname ) ;
for ( int k = 0 ; k < cb - > getMapSize ( ) . z ; k + + )
{
for ( int j = 0 ; j < cb - > getMapSize ( ) . y ; j + + )
{
for ( int i = 0 ; i < cb - > getMapSize ( ) . x ; i + + )
{
out < < ( int ) sector [ i ] [ j ] [ k ] < < ' \t ' ;
}
out < < std : : endl ;
}
out < < std : : endl ;
}
}
2018-04-07 10:44:14 +02:00
bool isWeeklyRevisitable ( const CGObjectInstance * obj )
{
//TODO: allow polling of remaining creatures in dwelling
if ( dynamic_cast < const CGVisitableOPW * > ( obj ) ) // ensures future compatibility, unlike IDs
2012-03-05 22:11:28 +03:00
return true ;
2018-04-07 10:44:14 +02:00
if ( dynamic_cast < const CGDwelling * > ( obj ) )
return true ;
if ( dynamic_cast < const CBank * > ( obj ) ) //banks tend to respawn often in mods
return true ;
switch ( obj - > ID )
2012-03-06 21:49:23 +03:00
{
2018-04-07 10:44:14 +02:00
case Obj : : STABLES :
case Obj : : MAGIC_WELL :
case Obj : : HILL_FORT :
return true ;
case Obj : : BORDER_GATE :
case Obj : : BORDERGUARD :
return ( dynamic_cast < const CGKeys * > ( obj ) ) - > wasMyColorVisited ( ai - > playerID ) ; //FIXME: they could be revisited sooner than in a week
2012-03-06 21:49:23 +03:00
}
2012-03-05 22:11:28 +03:00
return false ;
}
2012-07-01 02:48:40 +03:00
bool shouldVisit ( HeroPtr h , const CGObjectInstance * obj )
2012-03-14 16:02:38 +03:00
{
2018-04-07 10:44:14 +02:00
switch ( obj - > ID )
2015-10-31 17:04:06 +02:00
{
2018-04-07 10:44:14 +02:00
case Obj : : TOWN :
case Obj : : HERO : //never visit our heroes at random
return obj - > tempOwner ! = h - > tempOwner ; //do not visit our towns at random
case Obj : : BORDER_GATE :
{
for ( auto q : ai - > myCb - > getMyQuests ( ) )
2012-10-03 17:49:29 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . obj = = obj )
2012-10-03 17:49:29 +03:00
{
2018-04-07 10:44:14 +02:00
return false ; // do not visit guards or gates when wandering
2012-10-03 17:49:29 +03:00
}
}
2018-04-07 10:44:14 +02:00
return true ; //we don't have this quest yet
}
case Obj : : BORDERGUARD : //open borderguard if possible
return ( dynamic_cast < const CGKeys * > ( obj ) ) - > wasMyColorVisited ( ai - > playerID ) ;
case Obj : : SEER_HUT :
case Obj : : QUEST_GUARD :
{
for ( auto q : ai - > myCb - > getMyQuests ( ) )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . obj = = obj )
2012-07-18 13:10:14 +03:00
{
2018-04-07 10:44:14 +02:00
if ( q . quest - > checkQuest ( h . h ) )
return true ; //we completed the quest
else
return false ; //we can't complete this quest
2012-07-18 13:10:14 +03:00
}
}
2018-04-07 10:44:14 +02:00
return true ; //we don't have this quest yet
}
case Obj : : CREATURE_GENERATOR1 :
{
if ( obj - > tempOwner ! = h - > tempOwner )
return true ; //flag just in case
bool canRecruitCreatures = false ;
const CGDwelling * d = dynamic_cast < const CGDwelling * > ( obj ) ;
for ( auto level : d - > creatures )
2012-03-14 16:02:38 +03:00
{
2018-04-07 10:44:14 +02:00
for ( auto c : level . second )
2012-03-14 16:02:38 +03:00
{
2018-04-07 10:44:14 +02:00
if ( h - > getSlotFor ( CreatureID ( c ) ) ! = SlotID ( ) )
canRecruitCreatures = true ;
2012-03-14 16:02:38 +03:00
}
}
2018-04-07 10:44:14 +02:00
return canRecruitCreatures ;
}
case Obj : : HILL_FORT :
{
for ( auto slot : h - > Slots ( ) )
2015-10-31 17:04:06 +02:00
{
2018-04-07 10:44:14 +02:00
if ( slot . second - > type - > upgrades . size ( ) )
return true ; //TODO: check price?
2012-08-29 12:19:20 +03:00
}
2018-04-07 10:44:14 +02:00
return false ;
}
case Obj : : MONOLITH_ONE_WAY_ENTRANCE :
case Obj : : MONOLITH_ONE_WAY_EXIT :
case Obj : : MONOLITH_TWO_WAY :
case Obj : : WHIRLPOOL :
return false ;
case Obj : : SCHOOL_OF_MAGIC :
case Obj : : SCHOOL_OF_WAR :
{
2018-07-26 12:06:55 +02:00
if ( ah - > freeGold ( ) < 1000 )
2012-03-14 16:02:38 +03:00
return false ;
2018-04-07 10:44:14 +02:00
break ;
}
case Obj : : LIBRARY_OF_ENLIGHTENMENT :
if ( h - > level < 12 )
2012-09-28 23:49:23 +03:00
return false ;
2018-04-07 10:44:14 +02:00
break ;
case Obj : : TREE_OF_KNOWLEDGE :
{
2018-07-26 12:06:55 +02:00
TResources myRes = ah - > freeResources ( ) ;
if ( myRes [ Res : : GOLD ] < 2000 | | myRes [ Res : : GEMS ] < 10 )
2018-04-07 10:44:14 +02:00
return false ;
break ;
}
case Obj : : MAGIC_WELL :
return h - > mana < h - > manaLimit ( ) ;
case Obj : : PRISON :
return ai - > myCb - > getHeroesInfo ( ) . size ( ) < VLC - > modh - > settings . MAX_HEROES_ON_MAP_PER_PLAYER ;
case Obj : : TAVERN :
{
//TODO: make AI actually recruit heroes
//TODO: only on request
if ( ai - > myCb - > getHeroesInfo ( ) . size ( ) > = VLC - > modh - > settings . MAX_HEROES_ON_MAP_PER_PLAYER )
return false ;
2018-07-26 12:06:55 +02:00
else if ( ah - > freeGold ( ) < GameConstants : : HERO_GOLD_COST )
2018-04-07 10:44:14 +02:00
return false ;
break ;
}
case Obj : : BOAT :
return false ;
//Boats are handled by pathfinder
case Obj : : EYE_OF_MAGI :
return false ; //this object is useless to visit, but could be visited indefinitely
2012-03-14 16:02:38 +03:00
}
2012-03-29 21:26:06 +03:00
2018-04-07 10:44:14 +02:00
if ( obj - > wasVisited ( * h ) ) //it must pointer to hero instance, heroPtr calls function wasVisited(ui8 player);
2012-03-29 21:26:06 +03:00
return false ;
2012-03-14 16:02:38 +03:00
return true ;
}
2014-02-15 19:38:51 +03:00
/*
this functions returns one target tile or invalid tile . We will use it to poll possible destinations
For ship construction etc , another function ( goal ? ) is needed
*/
2018-04-07 10:44:14 +02:00
int3 SectorMap : : firstTileToGet ( HeroPtr h , crint3 dst )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
int3 ret ( - 1 , - 1 , - 1 ) ;
2014-02-15 19:38:51 +03:00
2018-04-07 10:44:14 +02:00
int sourceSector = retrieveTile ( h - > visitablePos ( ) ) ;
int destinationSector = retrieveTile ( dst ) ;
2012-02-14 21:04:45 +03:00
2018-04-07 10:44:14 +02:00
const Sector * src = & infoOnSectors [ sourceSector ] ;
const Sector * dest = & infoOnSectors [ destinationSector ] ;
2014-02-23 19:55:42 +03:00
if ( sourceSector ! = destinationSector ) //use ships, shipyards etc..
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( ai - > isAccessibleForHero ( dst , h ) ) //pathfinder can find a way using ships and gates if tile is not blocked by objects
2014-02-23 19:55:42 +03:00
return dst ;
2012-02-14 21:04:45 +03:00
2018-04-07 10:44:14 +02:00
std : : map < const Sector * , const Sector * > preds ;
2014-02-21 10:48:38 +03:00
std : : queue < const Sector * > sectorQueue ;
sectorQueue . push ( src ) ;
while ( ! sectorQueue . empty ( ) )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
const Sector * s = sectorQueue . front ( ) ;
2014-02-21 10:48:38 +03:00
sectorQueue . pop ( ) ;
2012-02-14 21:04:45 +03:00
2013-06-29 16:05:48 +03:00
for ( int3 ep : s - > embarkmentPoints )
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
Sector * neigh = & infoOnSectors [ retrieveTile ( ep ) ] ;
2012-02-14 21:04:45 +03:00
//preds[s].push_back(neigh);
if ( ! preds [ neigh ] )
{
preds [ neigh ] = s ;
2014-02-21 10:48:38 +03:00
sectorQueue . push ( neigh ) ;
2012-02-14 21:04:45 +03:00
}
}
}
2014-02-21 10:48:38 +03:00
if ( ! preds [ dest ] )
2012-02-14 21:04:45 +03:00
{
2014-02-21 10:48:38 +03:00
//write("test.txt");
2014-02-15 19:38:51 +03:00
return ret ;
2018-04-07 10:44:14 +02:00
//throw cannotFulfillGoalException(boost::str(boost::format("Cannot find connection between sectors %d and %d") % src->id % dst->id));
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
std : : vector < const Sector * > toTraverse ;
2014-02-21 10:48:38 +03:00
toTraverse . push_back ( dest ) ;
2012-02-14 21:04:45 +03:00
while ( toTraverse . back ( ) ! = src )
{
toTraverse . push_back ( preds [ toTraverse . back ( ) ] ) ;
}
2014-02-21 10:48:38 +03:00
if ( preds [ dest ] )
2012-02-14 21:04:45 +03:00
{
2014-02-23 19:55:42 +03:00
//TODO: would be nice to find sectors in loop
2018-04-07 10:44:14 +02:00
const Sector * sectorToReach = toTraverse . at ( toTraverse . size ( ) - 2 ) ;
2014-02-23 19:55:42 +03:00
2012-02-14 21:04:45 +03:00
if ( ! src - > water & & sectorToReach - > water ) //embark
{
//embark on ship -> look for an EP with a boat
auto firstEP = boost : : find_if ( src - > embarkmentPoints , [ = ] ( crint3 pos ) - > bool
{
2018-04-07 10:44:14 +02:00
const TerrainTile * t = getTile ( pos ) ;
if ( t & & t - > visitableObjects . size ( ) = = 1 & & t - > topVisitableId ( ) = = Obj : : BOAT )
{
if ( retrieveTile ( pos ) = = sectorToReach - > id )
return true ;
}
return false ;
2012-02-14 21:04:45 +03:00
} ) ;
if ( firstEP ! = src - > embarkmentPoints . end ( ) )
{
return * firstEP ;
}
else
{
//we need to find a shipyard with an access to the desired sector's EP
//TODO what about Summon Boat spell?
std : : vector < const IShipyard * > shipyards ;
2018-04-07 10:44:14 +02:00
for ( const CGTownInstance * t : cb - > getTownsInfo ( ) )
2012-02-14 21:04:45 +03:00
{
2013-02-11 02:24:57 +03:00
if ( t - > hasBuilt ( BuildingID : : SHIPYARD ) )
2012-02-14 21:04:45 +03:00
shipyards . push_back ( t ) ;
}
2018-04-07 10:44:14 +02:00
for ( const CGObjectInstance * obj : ai - > getFlaggedObjects ( ) )
2012-02-14 21:04:45 +03:00
{
2012-09-23 21:01:04 +03:00
if ( obj - > ID ! = Obj : : TOWN ) //towns were handled in the previous loop
2018-04-07 10:44:14 +02:00
{
if ( const IShipyard * shipyard = IShipyard : : castFrom ( obj ) )
2012-02-14 21:04:45 +03:00
shipyards . push_back ( shipyard ) ;
2018-04-07 10:44:14 +02:00
}
2012-02-14 21:04:45 +03:00
}
2018-04-07 10:44:14 +02:00
shipyards . erase ( boost : : remove_if ( shipyards , [ = ] ( const IShipyard * shipyard ) - > bool
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
return shipyard - > shipyardStatus ( ) ! = 0 | | retrieveTile ( shipyard - > bestLocation ( ) ) ! = sectorToReach - > id ;
2018-04-07 10:44:14 +02:00
} ) , shipyards . end ( ) ) ;
2012-02-14 21:04:45 +03:00
if ( ! shipyards . size ( ) )
{
//TODO consider possibility of building shipyard in a town
2014-02-15 19:38:51 +03:00
return ret ;
//throw cannotFulfillGoalException("There is no known shipyard!");
2012-02-14 21:04:45 +03:00
}
//we have only shipyards that possibly can build ships onto the appropriate EP
2018-04-07 10:44:14 +02:00
auto ownedGoodShipyard = boost : : find_if ( shipyards , [ ] ( const IShipyard * s ) - > bool
2012-02-14 21:04:45 +03:00
{
return s - > o - > tempOwner = = ai - > playerID ;
} ) ;
if ( ownedGoodShipyard ! = shipyards . end ( ) )
{
2018-04-07 10:44:14 +02:00
const IShipyard * s = * ownedGoodShipyard ;
2012-02-14 21:04:45 +03:00
TResources shipCost ;
s - > getBoatCost ( shipCost ) ;
if ( cb - > getResourceAmount ( ) . canAfford ( shipCost ) )
{
int3 ret = s - > bestLocation ( ) ;
2014-02-15 19:38:51 +03:00
cb - > buildBoat ( s ) ; //TODO: move actions elsewhere
2012-02-14 21:04:45 +03:00
return ret ;
}
else
{
//TODO gather res
2014-02-15 19:38:51 +03:00
return ret ;
//throw cannotFulfillGoalException("Not enough resources to build a boat");
2012-02-14 21:04:45 +03:00
}
}
else
{
//TODO pick best shipyard to take over
2013-01-06 07:48:05 +03:00
return shipyards . front ( ) - > o - > visitablePos ( ) ;
2012-02-14 21:04:45 +03:00
}
}
}
else if ( src - > water & & ! sectorToReach - > water )
{
//TODO
//disembark
2014-02-15 19:38:51 +03:00
return ret ;
2012-02-14 21:04:45 +03:00
}
2015-12-24 12:28:14 +02:00
else //use subterranean gates - not needed since gates are now handled via Pathfinder
2012-02-14 21:04:45 +03:00
{
2014-02-15 19:38:51 +03:00
return ret ;
//throw cannotFulfillGoalException("Land-land and water-water inter-sector transitions are not implemented!");
2012-02-14 21:04:45 +03:00
}
}
else
{
2014-02-15 19:38:51 +03:00
return ret ;
//throw cannotFulfillGoalException("Inter-sector route detection failed: not connected sectors?");
2012-02-14 21:04:45 +03:00
}
}
else
{
2014-02-23 19:55:42 +03:00
return findFirstVisitableTile ( h , dst ) ;
}
}
2018-04-07 10:44:14 +02:00
int3 SectorMap : : findFirstVisitableTile ( HeroPtr h , crint3 dst )
2014-02-23 19:55:42 +03:00
{
2018-04-07 10:44:14 +02:00
int3 ret ( - 1 , - 1 , - 1 ) ;
2014-02-23 19:55:42 +03:00
int3 curtile = dst ;
while ( curtile ! = h - > visitablePos ( ) )
{
auto topObj = cb - > getTopObj ( curtile ) ;
2018-04-07 10:44:14 +02:00
if ( topObj & & topObj - > ID = = Obj : : HERO & & topObj ! = h . h )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( cb - > getPlayerRelations ( h - > tempOwner , topObj - > tempOwner ) ! = PlayerRelations : : ENEMIES )
{
logAi - > warn ( " Another allied hero stands in our way " ) ;
return ret ;
}
2014-02-23 19:55:42 +03:00
}
2014-09-21 16:42:08 +03:00
if ( ai - > myCb - > getPathsInfo ( h . get ( ) ) - > getPathInfo ( curtile ) - > reachable ( ) )
2014-02-23 19:55:42 +03:00
{
return curtile ;
}
else
{
auto i = parent . find ( curtile ) ;
if ( i ! = parent . end ( ) )
2012-02-14 21:04:45 +03:00
{
2014-02-23 19:55:42 +03:00
assert ( curtile ! = i - > second ) ;
curtile = i - > second ;
2012-02-14 21:04:45 +03:00
}
else
{
2014-02-23 19:55:42 +03:00
return ret ;
//throw cannotFulfillGoalException("Unreachable tile in sector? Should not happen!");
2012-02-14 21:04:45 +03:00
}
}
}
2014-02-15 19:38:51 +03:00
return ret ;
2012-02-14 21:04:45 +03:00
}
void SectorMap : : makeParentBFS ( crint3 source )
{
parent . clear ( ) ;
2018-02-10 20:52:23 +02:00
int mySector = retrieveTile ( source ) ;
2012-02-14 21:04:45 +03:00
std : : queue < int3 > toVisit ;
toVisit . push ( source ) ;
2013-11-03 15:51:25 +03:00
while ( ! toVisit . empty ( ) )
2012-02-14 21:04:45 +03:00
{
int3 curPos = toVisit . front ( ) ;
toVisit . pop ( ) ;
2018-02-10 20:52:23 +02:00
TSectorID & sec = retrieveTile ( curPos ) ;
2012-02-14 21:04:45 +03:00
assert ( sec = = mySector ) ; //consider only tiles from the same sector
2013-11-09 16:49:36 +03:00
UNUSED ( sec ) ;
2015-10-31 17:04:06 +02:00
2012-02-14 21:04:45 +03:00
foreach_neighbour ( curPos , [ & ] ( crint3 neighPos )
{
2018-02-10 20:52:23 +02:00
if ( retrieveTile ( neighPos ) = = mySector & & ! vstd : : contains ( parent , neighPos ) )
2012-02-14 21:04:45 +03:00
{
2018-04-07 10:44:14 +02:00
if ( cb - > canMoveBetween ( curPos , neighPos ) )
2014-02-15 22:39:03 +03:00
{
toVisit . push ( neighPos ) ;
parent [ neighPos ] = curPos ;
}
2012-02-14 21:04:45 +03:00
}
2014-02-19 19:23:47 +03:00
} ) ;
2012-02-14 21:04:45 +03:00
}
}
2018-02-10 20:52:23 +02:00
SectorMap : : TSectorID & SectorMap : : retrieveTile ( crint3 pos )
2012-02-14 21:04:45 +03:00
{
2018-02-10 20:52:23 +02:00
return retrieveTileN ( sector , pos ) ;
2012-04-16 20:12:39 +03:00
}
2012-04-17 15:46:21 +03:00
2018-04-07 10:44:14 +02:00
TerrainTile * SectorMap : : getTile ( crint3 pos ) const
2015-08-31 07:39:03 +02:00
{
//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 ] ;
}
2016-08-12 08:05:11 +02:00
std : : vector < const CGObjectInstance * > SectorMap : : getNearbyObjs ( HeroPtr h , bool sectorsAround )
{
2018-02-10 20:52:23 +02:00
const Sector * heroSector = & infoOnSectors [ retrieveTile ( h - > visitablePos ( ) ) ] ;
2016-08-12 08:05:11 +02:00
if ( sectorsAround )
{
std : : vector < const CGObjectInstance * > ret ;
for ( auto embarkPoint : heroSector - > embarkmentPoints )
{
2018-02-10 20:52:23 +02:00
const Sector * embarkSector = & infoOnSectors [ retrieveTile ( embarkPoint ) ] ;
2016-08-12 08:05:11 +02:00
range : : copy ( embarkSector - > visitableObjs , std : : back_inserter ( ret ) ) ;
}
return ret ;
}
return heroSector - > visitableObjs ;
}