mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-26 03:52:01 +02:00
First part of AI refactoring. Trying to split AI into three distinct parts:
- Core with callback handling and processing - Logic (goal decomposition) - Utility and helper functions based on simple mechanics Also, Goals will now be organized in object-oriented fashion.
This commit is contained in:
parent
f838cceddd
commit
9950bc6ec3
373
AI/VCAI/AIUtility.cpp
Normal file
373
AI/VCAI/AIUtility.cpp
Normal file
@ -0,0 +1,373 @@
|
||||
#include "StdInc.h"
|
||||
#include "AIUtility.h"
|
||||
#include "VCAI.h"
|
||||
|
||||
#include "../../lib/UnlockGuard.h"
|
||||
#include "../../lib/CObjectHandler.h"
|
||||
#include "../../lib/CConfigHandler.h"
|
||||
#include "../../lib/CHeroHandler.h""
|
||||
|
||||
/*
|
||||
* AIUtility.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
|
||||
*
|
||||
*/
|
||||
|
||||
extern boost::thread_specific_ptr<CCallback> cb;
|
||||
extern boost::thread_specific_ptr<VCAI> ai;
|
||||
extern FuzzyHelper *fh;
|
||||
|
||||
//extern static const int3 dirs[8];
|
||||
|
||||
void foreach_tile_pos(std::function<void(const int3& pos)> foo)
|
||||
{
|
||||
for(int i = 0; i < cb->getMapSize().x; i++)
|
||||
for(int j = 0; j < cb->getMapSize().y; j++)
|
||||
for(int k = 0; k < cb->getMapSize().z; k++)
|
||||
foo(int3(i,j,k));
|
||||
}
|
||||
|
||||
void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo)
|
||||
{
|
||||
for(const int3 &dir : dirs)
|
||||
{
|
||||
const int3 n = pos + dir;
|
||||
if(cb->isInTheMap(n))
|
||||
foo(pos+dir);
|
||||
}
|
||||
}
|
||||
|
||||
std::string strFromInt3(int3 pos)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << pos;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
bool isCloser(const CGObjectInstance *lhs, const CGObjectInstance *rhs)
|
||||
{
|
||||
const CGPathNode *ln = cb->getPathInfo(lhs->visitablePos()), *rn = cb->getPathInfo(rhs->visitablePos());
|
||||
if(ln->turns != rn->turns)
|
||||
return ln->turns < rn->turns;
|
||||
|
||||
return (ln->moveRemains > rn->moveRemains);
|
||||
}
|
||||
|
||||
bool compareMovement(HeroPtr lhs, HeroPtr rhs)
|
||||
{
|
||||
return lhs->movement > rhs->movement;
|
||||
}
|
||||
|
||||
ui64 evaluateDanger(crint3 tile)
|
||||
{
|
||||
const TerrainTile *t = cb->getTile(tile, false);
|
||||
if(!t) //we can know about guard but can't check its tile (the edge of fow)
|
||||
return 190000000; //MUCH
|
||||
|
||||
ui64 objectDanger = 0, guardDanger = 0;
|
||||
|
||||
auto visObjs = cb->getVisitableObjs(tile);
|
||||
if(visObjs.size())
|
||||
objectDanger = evaluateDanger(visObjs.back());
|
||||
|
||||
int3 guardPos = cb->guardingCreaturePosition(tile);
|
||||
if(guardPos.x >= 0 && guardPos != tile)
|
||||
guardDanger = evaluateDanger(guardPos);
|
||||
|
||||
//TODO mozna odwiedzic blockvis nie ruszajac straznika
|
||||
return std::max(objectDanger, guardDanger);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor)
|
||||
{
|
||||
const TerrainTile *t = cb->getTile(tile, false);
|
||||
if(!t) //we can know about guard but can't check its tile (the edge of fow)
|
||||
return 190000000; //MUCH
|
||||
|
||||
ui64 objectDanger = 0, guardDanger = 0;
|
||||
|
||||
auto visitableObjects = cb->getVisitableObjs(tile);
|
||||
// in some scenarios hero happens to be "under" the object (eg town). Then we consider ONLY the hero.
|
||||
if(vstd::contains_if(visitableObjects, objWithID<Obj::HERO>))
|
||||
vstd::erase_if(visitableObjects, [](const CGObjectInstance * obj)
|
||||
{
|
||||
return !objWithID<Obj::HERO>(obj);
|
||||
});
|
||||
|
||||
if(const CGObjectInstance * dangerousObject = vstd::backOrNull(visitableObjects))
|
||||
{
|
||||
objectDanger = evaluateDanger(dangerousObject); //unguarded objects can also be dangerous or unhandled
|
||||
if (objectDanger)
|
||||
{
|
||||
//TODO: don't downcast objects AI shouldnt know about!
|
||||
auto armedObj = dynamic_cast<const CArmedInstance*>(dangerousObject);
|
||||
if(armedObj)
|
||||
objectDanger *= fh->getTacticalAdvantage(visitor, armedObj); //this line tends to go infinite for allied towns (?)
|
||||
}
|
||||
}
|
||||
|
||||
auto guards = cb->getGuardingCreatures(tile);
|
||||
for (auto cre : guards)
|
||||
{
|
||||
vstd::amax (guardDanger, evaluateDanger(cre) * fh->getTacticalAdvantage(visitor, dynamic_cast<const CArmedInstance*>(cre))); //we are interested in strongest monster around
|
||||
}
|
||||
|
||||
//TODO mozna odwiedzic blockvis nie ruszajac straznika
|
||||
return std::max(objectDanger, guardDanger);
|
||||
}
|
||||
|
||||
ui64 evaluateDanger(const CGObjectInstance *obj)
|
||||
{
|
||||
if(obj->tempOwner < PlayerColor::PLAYER_LIMIT && cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES) //owned or allied objects don't pose any threat
|
||||
return 0;
|
||||
|
||||
switch(obj->ID)
|
||||
{
|
||||
case Obj::HERO:
|
||||
{
|
||||
InfoAboutHero iah;
|
||||
cb->getHeroInfo(obj, iah);
|
||||
return iah.army.getStrength();
|
||||
}
|
||||
case Obj::TOWN:
|
||||
case Obj::GARRISON: case Obj::GARRISON2: //garrison
|
||||
{
|
||||
InfoAboutTown iat;
|
||||
cb->getTownInfo(obj, iat);
|
||||
return iat.army.getStrength();
|
||||
}
|
||||
case Obj::MONSTER:
|
||||
{
|
||||
//TODO!!!!!!!!
|
||||
const CGCreature *cre = dynamic_cast<const CGCreature*>(obj);
|
||||
return cre->getArmyStrength();
|
||||
}
|
||||
case Obj::CREATURE_GENERATOR1:
|
||||
{
|
||||
const CGDwelling *d = dynamic_cast<const CGDwelling*>(obj);
|
||||
return d->getArmyStrength();
|
||||
}
|
||||
case Obj::MINE:
|
||||
case Obj::ABANDONED_MINE:
|
||||
{
|
||||
const CArmedInstance * a = dynamic_cast<const CArmedInstance*>(obj);
|
||||
return a->getArmyStrength();
|
||||
}
|
||||
case Obj::CRYPT: //crypt
|
||||
case Obj::CREATURE_BANK: //crebank
|
||||
case Obj::DRAGON_UTOPIA:
|
||||
case Obj::SHIPWRECK: //shipwreck
|
||||
case Obj::DERELICT_SHIP: //derelict ship
|
||||
// case Obj::PYRAMID:
|
||||
return fh->estimateBankDanger (VLC->objh->bankObjToIndex(obj));
|
||||
case Obj::PYRAMID:
|
||||
{
|
||||
if(obj->subID == 0)
|
||||
return fh->estimateBankDanger (VLC->objh->bankObjToIndex(obj));
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool compareDanger(const CGObjectInstance *lhs, const CGObjectInstance *rhs)
|
||||
{
|
||||
return evaluateDanger(lhs) < evaluateDanger(rhs);
|
||||
}
|
||||
|
||||
bool isSafeToVisit(HeroPtr h, crint3 tile)
|
||||
{
|
||||
const ui64 heroStrength = h->getTotalStrength(),
|
||||
dangerStrength = evaluateDanger(tile, *h);
|
||||
if(dangerStrength)
|
||||
{
|
||||
if(heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength)
|
||||
{
|
||||
logAi->debugStream() << boost::format("It's, safe for %s to visit tile %s") % h->name % tile;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; //there's no danger
|
||||
}
|
||||
|
||||
bool isReachable(const CGObjectInstance *obj)
|
||||
{
|
||||
return cb->getPathInfo(obj->visitablePos())->turns < 255;
|
||||
}
|
||||
|
||||
bool canBeEmbarkmentPoint(const TerrainTile *t)
|
||||
{
|
||||
//tile must be free of with unoccupied boat
|
||||
return !t->blocked
|
||||
|| (t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT);
|
||||
}
|
||||
|
||||
int3 whereToExplore(HeroPtr h)
|
||||
{
|
||||
//TODO it's stupid and ineffective, write sth better
|
||||
cb->setSelection(*h);
|
||||
int radius = h->getSightRadious();
|
||||
int3 hpos = h->visitablePos();
|
||||
|
||||
//look for nearby objs -> visit them if they're close enouh
|
||||
const int DIST_LIMIT = 3;
|
||||
std::vector<const CGObjectInstance *> nearbyVisitableObjs;
|
||||
for(const CGObjectInstance *obj : ai->getPossibleDestinations(h))
|
||||
{
|
||||
int3 op = obj->visitablePos();
|
||||
CGPath p;
|
||||
cb->getPath2(op, p);
|
||||
if(p.nodes.size() && p.endPos() == op && p.nodes.size() <= DIST_LIMIT)
|
||||
nearbyVisitableObjs.push_back(obj);
|
||||
}
|
||||
boost::sort(nearbyVisitableObjs, isCloser);
|
||||
if(nearbyVisitableObjs.size())
|
||||
return nearbyVisitableObjs.back()->visitablePos();
|
||||
|
||||
try
|
||||
{
|
||||
return ai->explorationBestNeighbour(hpos, radius, h);
|
||||
}
|
||||
catch(cannotFulfillGoalException &e)
|
||||
{
|
||||
std::vector<std::vector<int3> > tiles; //tiles[distance_to_fow]
|
||||
try
|
||||
{
|
||||
return ai->explorationNewPoint(radius, h, tiles);
|
||||
}
|
||||
catch(cannotFulfillGoalException &e)
|
||||
{
|
||||
std::map<int, std::vector<int3> > profits;
|
||||
{
|
||||
TimeCheck tc("Evaluating exploration possibilities");
|
||||
tiles[0].clear(); //we can't reach FoW anyway
|
||||
for(auto &vt : tiles)
|
||||
for(auto &tile : vt)
|
||||
profits[howManyTilesWillBeDiscovered(tile, radius)].push_back(tile);
|
||||
}
|
||||
|
||||
if(profits.empty())
|
||||
return int3 (-1,-1,-1);
|
||||
|
||||
auto bestDest = profits.end();
|
||||
bestDest--;
|
||||
return bestDest->second.front(); //TODO which is the real best tile?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isBlockedBorderGate(int3 tileToHit)
|
||||
{
|
||||
return cb->getTile(tileToHit)->topVisitableId() == Obj::BORDER_GATE
|
||||
&& cb->getPathInfo(tileToHit)->accessible != CGPathNode::ACCESSIBLE;
|
||||
}
|
||||
|
||||
int howManyTilesWillBeDiscovered(const int3 &pos, int radious)
|
||||
{ //TODO: do not explore dead-end boundaries
|
||||
int ret = 0;
|
||||
for(int x = pos.x - radious; x <= pos.x + radious; x++)
|
||||
{
|
||||
for(int y = pos.y - radious; y <= pos.y + radious; y++)
|
||||
{
|
||||
int3 npos = int3(x,y,pos.z);
|
||||
if(cb->isInTheMap(npos) && pos.dist2d(npos) - 0.5 < radious && !cb->isVisible(npos))
|
||||
{
|
||||
if (!boundaryBetweenTwoPoints (pos, npos))
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2) //determines if two points are separated by known barrier
|
||||
{
|
||||
int xMin = std::min (pos1.x, pos2.x);
|
||||
int xMax = std::max (pos1.x, pos2.x);
|
||||
int yMin = std::min (pos1.y, pos2.y);
|
||||
int yMax = std::max (pos1.y, pos2.y);
|
||||
|
||||
for (int x = xMin; x <= xMax; ++x)
|
||||
{
|
||||
for (int y = yMin; y <= yMax; ++y)
|
||||
{
|
||||
int3 tile = int3(x, y, pos1.z); //use only on same level, ofc
|
||||
if (abs(pos1.dist2d(tile) - pos2.dist2d(tile)) < 1.5)
|
||||
{
|
||||
if (!(cb->isVisible(tile) && cb->getTile(tile)->blocked)) //if there's invisible or unblocked tile inbetween, it's good
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true; //if all are visible and blocked, we're at dead end
|
||||
}
|
||||
|
||||
int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir)
|
||||
{
|
||||
return howManyTilesWillBeDiscovered(pos + dir, radious);
|
||||
}
|
||||
|
||||
void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out)
|
||||
{
|
||||
for(const int3 &tile : tiles)
|
||||
{
|
||||
foreach_neighbour(tile, [&](int3 neighbour)
|
||||
{
|
||||
if(cb->isVisible(neighbour))
|
||||
out.push_back(neighbour);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t)
|
||||
{
|
||||
ui64 ret = 0;
|
||||
int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount();
|
||||
std::vector<const CStackInstance *> toMove;
|
||||
for(auto const slot : t->Slots())
|
||||
{
|
||||
//can be merged woth another stack?
|
||||
SlotID dst = h->getSlotFor(slot.second->getCreatureID());
|
||||
if(h->hasStackAtSlot(dst))
|
||||
ret += t->getPower(slot.first);
|
||||
else
|
||||
toMove.push_back(slot.second);
|
||||
}
|
||||
boost::sort(toMove, [](const CStackInstance *lhs, const CStackInstance *rhs)
|
||||
{
|
||||
return lhs->getPower() < rhs->getPower();
|
||||
});
|
||||
for (auto & stack : boost::adaptors::reverse(toMove))
|
||||
{
|
||||
if(freeHeroSlots)
|
||||
{
|
||||
ret += stack->getPower();
|
||||
freeHeroSlots--;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
|
||||
{
|
||||
return h1->getTotalStrength() < h2->getTotalStrength();
|
||||
}
|
||||
|
||||
bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2)
|
||||
{
|
||||
return a1->getArmyStrength() < a2->getArmyStrength();
|
||||
}
|
201
AI/VCAI/AIUtility.h
Normal file
201
AI/VCAI/AIUtility.h
Normal file
@ -0,0 +1,201 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/VCMI_Lib.h"
|
||||
#include "../../lib/CBuildingHandler.h"
|
||||
#include "../../lib/CCreatureHandler.h"
|
||||
#include "../../lib/CTownHandler.h"
|
||||
#include "../../lib/CSpellHandler.h"
|
||||
#include "../../lib/CObjectHandler.h"
|
||||
#include "../../lib/Connection.h"
|
||||
#include "../../lib/CGameState.h"
|
||||
#include "../../lib/mapping/CMap.h"
|
||||
#include "../../lib/NetPacks.h"
|
||||
#include "../../lib/CStopWatch.h"
|
||||
|
||||
/*
|
||||
* AIUtility.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
|
||||
//provisional class for AI to store a reference to an owned hero object
|
||||
//checks if it's valid on access, should be used in place of const CGHeroInstance*
|
||||
|
||||
typedef const int3& crint3;
|
||||
typedef const std::string& crstring;
|
||||
|
||||
const int HERO_GOLD_COST = 2500;
|
||||
const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
|
||||
const int ACTUAL_RESOURCE_COUNT = 7;
|
||||
const int ALLOWED_ROAMING_HEROES = 8;
|
||||
|
||||
//implementation-dependent
|
||||
extern const double SAFE_ATTACK_CONSTANT;
|
||||
extern const int GOLD_RESERVE;
|
||||
|
||||
struct HeroPtr
|
||||
{
|
||||
const CGHeroInstance *h;
|
||||
ObjectInstanceID hid;
|
||||
|
||||
public:
|
||||
std::string name;
|
||||
|
||||
|
||||
HeroPtr();
|
||||
HeroPtr(const CGHeroInstance *H);
|
||||
~HeroPtr();
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return validAndSet();
|
||||
}
|
||||
|
||||
bool operator<(const HeroPtr &rhs) const;
|
||||
const CGHeroInstance *operator->() const;
|
||||
const CGHeroInstance *operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
|
||||
|
||||
const CGHeroInstance *get(bool doWeExpectNull = false) const;
|
||||
bool validAndSet() const;
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & this->h & hid & name;
|
||||
}
|
||||
};
|
||||
|
||||
enum BattleState
|
||||
{
|
||||
NO_BATTLE,
|
||||
UPCOMING_BATTLE,
|
||||
ONGOING_BATTLE,
|
||||
ENDING_BATTLE
|
||||
};
|
||||
|
||||
// AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
|
||||
// This class stores object id, so we can detect when we lose access to the underlying object.
|
||||
struct ObjectIdRef
|
||||
{
|
||||
ObjectInstanceID id;
|
||||
|
||||
const CGObjectInstance *operator->() const;
|
||||
operator const CGObjectInstance *() const;
|
||||
|
||||
ObjectIdRef(ObjectInstanceID _id);
|
||||
ObjectIdRef(const CGObjectInstance *obj);
|
||||
|
||||
bool operator<(const ObjectIdRef &rhs) const;
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & id;
|
||||
}
|
||||
};
|
||||
|
||||
struct TimeCheck
|
||||
{
|
||||
CStopWatch time;
|
||||
std::string txt;
|
||||
TimeCheck(crstring TXT) : txt(TXT)
|
||||
{
|
||||
}
|
||||
|
||||
~TimeCheck()
|
||||
{
|
||||
logAi->debugStream() << boost::format("Time of %s was %d ms.") % txt % time.getDiff();
|
||||
}
|
||||
};
|
||||
|
||||
struct AtScopeExit
|
||||
{
|
||||
std::function<void()> foo;
|
||||
AtScopeExit(const std::function<void()> &FOO) : foo(FOO)
|
||||
{}
|
||||
~AtScopeExit()
|
||||
{
|
||||
foo();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class ObjsVector : public std::vector<ObjectIdRef>
|
||||
{
|
||||
private:
|
||||
};
|
||||
|
||||
template<int id>
|
||||
bool objWithID(const CGObjectInstance *obj)
|
||||
{
|
||||
return obj->ID == id;
|
||||
}
|
||||
|
||||
template <typename Container, typename Item>
|
||||
bool erase_if_present(Container &c, const Item &item)
|
||||
{
|
||||
auto i = std::find(c.begin(), c.end(), item);
|
||||
if (i != c.end())
|
||||
{
|
||||
c.erase(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename V, typename Item, typename Item2>
|
||||
bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
|
||||
{
|
||||
auto i = c.find(item);
|
||||
if (i != c.end())
|
||||
{
|
||||
c.erase(i);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename Container, typename Pred>
|
||||
void erase(Container &c, Pred pred)
|
||||
{
|
||||
c.erase(boost::remove_if(c, pred), c.end());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void removeDuplicates(std::vector<T> &vec)
|
||||
{
|
||||
boost::sort(vec);
|
||||
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
|
||||
}
|
||||
|
||||
std::string strFromInt3(int3 pos);
|
||||
void foreach_tile_pos(std::function<void(const int3& pos)> foo);
|
||||
void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
|
||||
|
||||
int howManyTilesWillBeDiscovered(const int3 &pos, int radious);
|
||||
int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
|
||||
void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out);
|
||||
|
||||
bool canBeEmbarkmentPoint(const TerrainTile *t);
|
||||
bool isBlockedBorderGate(int3 tileToHit);
|
||||
bool isReachable(const CGObjectInstance *obj);
|
||||
bool isCloser(const CGObjectInstance *lhs, const CGObjectInstance *rhs);
|
||||
|
||||
bool isWeeklyRevisitable (const CGObjectInstance * obj);
|
||||
bool shouldVisit (HeroPtr h, const CGObjectInstance * obj);
|
||||
|
||||
ui64 evaluateDanger(const CGObjectInstance *obj);
|
||||
ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor);
|
||||
bool isSafeToVisit(HeroPtr h, crint3 tile);
|
||||
bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2);
|
||||
|
||||
bool compareMovement(HeroPtr lhs, HeroPtr rhs);
|
||||
bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
|
||||
bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2);
|
||||
ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t);
|
||||
int3 whereToExplore(HeroPtr h);
|
788
AI/VCAI/Goals.cpp
Normal file
788
AI/VCAI/Goals.cpp
Normal file
@ -0,0 +1,788 @@
|
||||
#include "StdInc.h"
|
||||
#include "Goals.h"
|
||||
#include "VCAI.h"
|
||||
|
||||
/*
|
||||
* Goals.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
|
||||
*
|
||||
*/
|
||||
|
||||
extern boost::thread_specific_ptr<CCallback> cb;
|
||||
extern boost::thread_specific_ptr<VCAI> ai;
|
||||
|
||||
using namespace vstd;
|
||||
|
||||
std::string CGoal::name() const
|
||||
{
|
||||
switch (goalType)
|
||||
{
|
||||
case INVALID:
|
||||
return "INVALID";
|
||||
case WIN:
|
||||
return "WIN";
|
||||
case DO_NOT_LOSE:
|
||||
return "DO NOT LOOSE";
|
||||
case CONQUER:
|
||||
return "CONQUER";
|
||||
case BUILD:
|
||||
return "BUILD";
|
||||
case EXPLORE:
|
||||
return "EXPLORE";
|
||||
case GATHER_ARMY:
|
||||
return "GATHER ARMY";
|
||||
case BOOST_HERO:
|
||||
return "BOOST_HERO (unsupported)";
|
||||
case RECRUIT_HERO:
|
||||
return "RECRUIT HERO";
|
||||
case BUILD_STRUCTURE:
|
||||
return "BUILD STRUCTURE";
|
||||
case COLLECT_RES:
|
||||
return "COLLECT RESOURCE";
|
||||
case GATHER_TROOPS:
|
||||
return "GATHER TROOPS";
|
||||
case GET_OBJ:
|
||||
return "GET OBJECT " + boost::lexical_cast<std::string>(objid);
|
||||
case FIND_OBJ:
|
||||
return "FIND OBJECT " + boost::lexical_cast<std::string>(objid);
|
||||
case VISIT_HERO:
|
||||
return "VISIT HERO " + boost::lexical_cast<std::string>(objid);
|
||||
case GET_ART_TYPE:
|
||||
return "GET ARTIFACT OF TYPE " + VLC->arth->artifacts[aid]->Name();
|
||||
case ISSUE_COMMAND:
|
||||
return "ISSUE COMMAND (unsupported)";
|
||||
case VISIT_TILE:
|
||||
return "VISIT TILE " + tile();
|
||||
case CLEAR_WAY_TO:
|
||||
return "CLEAR WAY TO " + tile();
|
||||
case DIG_AT_TILE:
|
||||
return "DIG AT TILE " + tile();
|
||||
default:
|
||||
return boost::lexical_cast<std::string>(goalType);
|
||||
}
|
||||
}
|
||||
|
||||
#define I_AM_ELEMENTAR return CGoal(*this).setisElementar(true)
|
||||
|
||||
TSubgoal Win::whatToDoToAchieve()
|
||||
{
|
||||
const VictoryCondition &vc = cb->getMapHeader()->victoryCondition;
|
||||
EVictoryConditionType::EVictoryConditionType cond = vc.condition;
|
||||
|
||||
if(!vc.appliesToAI)
|
||||
{
|
||||
//TODO deduce victory from human loss condition
|
||||
cond = EVictoryConditionType::WINSTANDARD;
|
||||
}
|
||||
|
||||
switch(cond)
|
||||
{
|
||||
case EVictoryConditionType::ARTIFACT:
|
||||
return CGoal(GET_ART_TYPE).setaid(vc.objectId);
|
||||
case EVictoryConditionType::BEATHERO:
|
||||
return CGoal(GET_OBJ).setobjid(vc.obj->id.getNum());
|
||||
case EVictoryConditionType::BEATMONSTER:
|
||||
return CGoal(GET_OBJ).setobjid(vc.obj->id.getNum());
|
||||
case EVictoryConditionType::BUILDCITY:
|
||||
//TODO build castle/capitol
|
||||
break;
|
||||
case EVictoryConditionType::BUILDGRAIL:
|
||||
{
|
||||
if(auto h = ai->getHeroWithGrail())
|
||||
{
|
||||
//hero is in a town that can host Grail
|
||||
if(h->visitedTown && !vstd::contains(h->visitedTown->forbiddenBuildings, BuildingID::GRAIL))
|
||||
{
|
||||
const CGTownInstance *t = h->visitedTown;
|
||||
return CGoal(BUILD_STRUCTURE).setbid(BuildingID::GRAIL).settown(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto towns = cb->getTownsInfo();
|
||||
towns.erase(boost::remove_if(towns,
|
||||
[](const CGTownInstance *t) -> bool
|
||||
{
|
||||
return vstd::contains(t->forbiddenBuildings, BuildingID::GRAIL);
|
||||
}),
|
||||
towns.end());
|
||||
boost::sort(towns, isCloser);
|
||||
if(towns.size())
|
||||
{
|
||||
return CGoal(VISIT_TILE).sethero(h).settile(towns.front()->visitablePos());
|
||||
}
|
||||
}
|
||||
}
|
||||
double ratio = 0;
|
||||
int3 grailPos = cb->getGrailPos(ratio);
|
||||
if(ratio > 0.99)
|
||||
{
|
||||
return CGoal(DIG_AT_TILE).settile(grailPos);
|
||||
} //TODO: use FIND_OBJ
|
||||
else if(const CGObjectInstance * obj = ai->getUnvisitedObj(objWithID<Obj::OBELISK>)) //there are unvisited Obelisks
|
||||
{
|
||||
return CGoal(GET_OBJ).setobjid(obj->id.getNum());
|
||||
}
|
||||
else
|
||||
return CGoal(EXPLORE);
|
||||
}
|
||||
break;
|
||||
case EVictoryConditionType::CAPTURECITY:
|
||||
return CGoal(GET_OBJ).setobjid(vc.obj->id.getNum());
|
||||
case EVictoryConditionType::GATHERRESOURCE:
|
||||
return CGoal(COLLECT_RES).setresID(static_cast<Res::ERes>(vc.objectId)).setvalue(vc.count);
|
||||
//TODO mines? piles? marketplace?
|
||||
//save?
|
||||
break;
|
||||
case EVictoryConditionType::GATHERTROOP:
|
||||
return CGoal(GATHER_TROOPS).setobjid(vc.objectId).setvalue(vc.count);
|
||||
break;
|
||||
case EVictoryConditionType::TAKEDWELLINGS:
|
||||
break;
|
||||
case EVictoryConditionType::TAKEMINES:
|
||||
break;
|
||||
case EVictoryConditionType::TRANSPORTITEM:
|
||||
break;
|
||||
case EVictoryConditionType::WINSTANDARD:
|
||||
return CGoal(CONQUER);
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
TSubgoal FindObj::whatToDoToAchieve()
|
||||
{
|
||||
const CGObjectInstance * o = nullptr;
|
||||
if (resID > -1) //specified
|
||||
{
|
||||
for(const CGObjectInstance *obj : ai->visitableObjs)
|
||||
{
|
||||
if(obj->ID == objid && obj->subID == resID)
|
||||
{
|
||||
o = obj;
|
||||
break; //TODO: consider multiple objects and choose best
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(const CGObjectInstance *obj : ai->visitableObjs)
|
||||
{
|
||||
if(obj->ID == objid)
|
||||
{
|
||||
o = obj;
|
||||
break; //TODO: consider multiple objects and choose best
|
||||
}
|
||||
}
|
||||
}
|
||||
if (o && isReachable(o))
|
||||
return CGoal(GET_OBJ).setobjid(o->id.getNum());
|
||||
else
|
||||
return CGoal(EXPLORE);
|
||||
}
|
||||
TSubgoal GetObj::whatToDoToAchieve()
|
||||
{
|
||||
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
|
||||
if(!obj)
|
||||
return CGoal(EXPLORE);
|
||||
int3 pos = obj->visitablePos();
|
||||
return CGoal(VISIT_TILE).settile(pos);
|
||||
}
|
||||
|
||||
TSubgoal VisitHero::whatToDoToAchieve()
|
||||
{
|
||||
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
|
||||
if(!obj)
|
||||
return CGoal(EXPLORE);
|
||||
int3 pos = obj->visitablePos();
|
||||
|
||||
if (hero && ai->isAccessibleForHero(pos, hero, true) && isSafeToVisit(hero, pos)) //enemy heroes can get reinforcements
|
||||
return CGoal(*this).settile(pos).setisElementar(true);
|
||||
}
|
||||
|
||||
TSubgoal GetArtOfType::whatToDoToAchieve()
|
||||
{
|
||||
TSubgoal alternativeWay = CGoal::lookForArtSmart(aid); //TODO: use
|
||||
if(alternativeWay.invalid())
|
||||
return CGoal(FIND_OBJ).setobjid(Obj::ARTIFACT).setresID(aid);
|
||||
}
|
||||
|
||||
TSubgoal ClearWayTo::whatToDoToAchieve()
|
||||
{
|
||||
assert(tile.x >= 0); //set tile
|
||||
if(!cb->isVisible(tile))
|
||||
{
|
||||
logAi->errorStream() << "Clear way should be used with visible tiles!";
|
||||
return CGoal(EXPLORE);
|
||||
}
|
||||
|
||||
HeroPtr h = hero ? hero : ai->primaryHero();
|
||||
if(!h)
|
||||
return CGoal(RECRUIT_HERO);
|
||||
|
||||
cb->setSelection(*h);
|
||||
|
||||
SectorMap sm;
|
||||
bool dropToFile = false;
|
||||
if(dropToFile) //for debug purposes
|
||||
sm.write("test.txt");
|
||||
|
||||
int3 tileToHit = sm.firstTileToGet(h, tile);
|
||||
//if(isSafeToVisit(h, tileToHit))
|
||||
if(isBlockedBorderGate(tileToHit))
|
||||
{ //FIXME: this way we'll not visit gate and activate quest :?
|
||||
return CGoal(FIND_OBJ).setobjid(Obj::KEYMASTER).setresID(cb->getTile(tileToHit)->visitableObjects.back()->subID);
|
||||
}
|
||||
|
||||
//FIXME: this code shouldn't be necessary
|
||||
if(tileToHit == tile)
|
||||
{
|
||||
logAi->errorStream() << boost::format("Very strange, tile to hit is %s and tile is also %s, while hero %s is at %s\n")
|
||||
% tileToHit % tile % h->name % h->visitablePos();
|
||||
throw cannotFulfillGoalException("Retrieving first tile to hit failed (probably)!");
|
||||
}
|
||||
|
||||
auto topObj = backOrNull(cb->getVisitableObjs(tileToHit));
|
||||
if(topObj && topObj->ID == Obj::HERO && cb->getPlayerRelations(h->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
|
||||
{
|
||||
std::string problem = boost::str(boost::format("%s stands in the way of %s.\n") % topObj->getHoverText() % h->getHoverText());
|
||||
throw cannotFulfillGoalException(problem);
|
||||
}
|
||||
|
||||
return CGoal(VISIT_TILE).settile(tileToHit).sethero(h); //FIXME:: attempts to visit completely unreachable tile with hero results in stall
|
||||
|
||||
//TODO czy istnieje lepsza droga?
|
||||
|
||||
throw cannotFulfillGoalException("Cannot reach given tile!"); //how and when could this be used?
|
||||
}
|
||||
|
||||
TSubgoal Explore::whatToDoToAchieve()
|
||||
{
|
||||
auto objs = ai->visitableObjs; //try to use buildings that uncover map
|
||||
erase_if(objs, [&](const CGObjectInstance *obj) -> bool
|
||||
{
|
||||
if (vstd::contains(ai->alreadyVisited, obj))
|
||||
return true;
|
||||
switch (obj->ID.num)
|
||||
{
|
||||
case Obj::REDWOOD_OBSERVATORY:
|
||||
case Obj::PILLAR_OF_FIRE:
|
||||
case Obj::CARTOGRAPHER:
|
||||
case Obj::SUBTERRANEAN_GATE: //TODO: check ai->knownSubterraneanGates
|
||||
//case Obj::MONOLITH1:
|
||||
//case obj::MONOLITH2:
|
||||
//case obj::MONOLITH3:
|
||||
//case Obj::WHIRLPOOL:
|
||||
return false; //do not erase
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (objs.size())
|
||||
{
|
||||
if (hero.get(true))
|
||||
{
|
||||
for (auto obj : objs)
|
||||
{
|
||||
auto pos = obj->visitablePos();
|
||||
//FIXME: this confition fails if everything but guarded subterranen gate was explored. in this case we should gather army for hero
|
||||
if (isSafeToVisit(hero, pos) && ai->isAccessibleForHero(pos, hero))
|
||||
return CGoal(VISIT_TILE).settile(pos).sethero(hero);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto obj : objs)
|
||||
{
|
||||
auto pos = obj->visitablePos();
|
||||
if (ai->isAccessible (pos)) //TODO: check safety?
|
||||
return CGoal(VISIT_TILE).settile(pos).sethero(hero);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hero)
|
||||
{
|
||||
int3 t = whereToExplore(hero);
|
||||
if (t.z == -1) //no safe tile to explore - we need to break!
|
||||
{
|
||||
erase_if (objs, [&](const CGObjectInstance *obj) -> bool
|
||||
{
|
||||
switch (obj->ID.num)
|
||||
{
|
||||
case Obj::CARTOGRAPHER:
|
||||
case Obj::SUBTERRANEAN_GATE:
|
||||
//case Obj::MONOLITH1:
|
||||
//case obj::MONOLITH2:
|
||||
//case obj::MONOLITH3:
|
||||
//case Obj::WHIRLPOOL:
|
||||
return false; //do not erase
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (objs.size())
|
||||
{
|
||||
return CGoal (VISIT_TILE).settile(objs.front()->visitablePos()).sethero(hero).setisAbstract(true);
|
||||
}
|
||||
else
|
||||
throw cannotFulfillGoalException("Cannot explore - no possible ways found!");
|
||||
}
|
||||
return CGoal(VISIT_TILE).settile(t).sethero(hero);
|
||||
}
|
||||
|
||||
auto hs = cb->getHeroesInfo();
|
||||
int howManyHeroes = hs.size();
|
||||
|
||||
erase(hs, [](const CGHeroInstance *h)
|
||||
{
|
||||
return contains(ai->lockedHeroes, h);
|
||||
});
|
||||
if(hs.empty()) //all heroes are busy. buy new one
|
||||
{
|
||||
if (howManyHeroes < 3 && ai->findTownWithTavern()) //we may want to recruit second hero. TODO: make it smart finally
|
||||
return CGoal(RECRUIT_HERO);
|
||||
else //find mobile hero with weakest army
|
||||
{
|
||||
hs = cb->getHeroesInfo();
|
||||
erase_if(hs, [](const CGHeroInstance *h)
|
||||
{
|
||||
return !h->movement; //only hero with movement are of interest for us
|
||||
});
|
||||
if (hs.empty())
|
||||
{
|
||||
if (howManyHeroes < GameConstants::MAX_HEROES_PER_PLAYER)
|
||||
return CGoal(RECRUIT_HERO);
|
||||
else
|
||||
throw cannotFulfillGoalException("No heroes with remaining MPs for exploring!\n");
|
||||
}
|
||||
boost::sort(hs, compareMovement); //closer to what?
|
||||
}
|
||||
}
|
||||
|
||||
const CGHeroInstance *h = hs.front();
|
||||
|
||||
return (*this).sethero(h).setisAbstract(true);
|
||||
|
||||
I_AM_ELEMENTAR; //FIXME: how can this be called?
|
||||
};
|
||||
|
||||
TSubgoal RecruitHero::whatToDoToAchieve()
|
||||
{
|
||||
const CGTownInstance *t = ai->findTownWithTavern();
|
||||
if(!t)
|
||||
return CGoal(BUILD_STRUCTURE).setbid(BuildingID::TAVERN);
|
||||
|
||||
if(cb->getResourceAmount(Res::GOLD) < HERO_GOLD_COST)
|
||||
return CGoal(COLLECT_RES).setresID(Res::GOLD).setvalue(HERO_GOLD_COST);
|
||||
|
||||
I_AM_ELEMENTAR;
|
||||
}
|
||||
|
||||
TSubgoal VisitTile::whatToDoToAchieve()
|
||||
{
|
||||
if(!cb->isVisible(tile))
|
||||
return CGoal(EXPLORE);
|
||||
|
||||
if(hero && !ai->isAccessibleForHero(tile, hero))
|
||||
hero = nullptr;
|
||||
|
||||
if(!hero)
|
||||
{
|
||||
if(cb->getHeroesInfo().empty())
|
||||
{
|
||||
return CGoal(RECRUIT_HERO);
|
||||
}
|
||||
|
||||
for(const CGHeroInstance *h : cb->getHeroesInfo())
|
||||
{
|
||||
if(ai->isAccessibleForHero(tile, h))
|
||||
{
|
||||
hero = h;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(hero)
|
||||
{
|
||||
if(isSafeToVisit(hero, tile))
|
||||
return CGoal(*this).setisElementar(true);
|
||||
else
|
||||
{
|
||||
return CGoal(GATHER_ARMY).sethero(hero).setvalue(evaluateDanger(tile, *hero) * SAFE_ATTACK_CONSTANT); //TODO: should it be abstract?
|
||||
}
|
||||
}
|
||||
else //inaccessible for all heroes
|
||||
{
|
||||
return CGoal(CLEAR_WAY_TO).settile(tile);
|
||||
}
|
||||
}
|
||||
|
||||
TSubgoal DigAtTile::whatToDoToAchieve()
|
||||
{
|
||||
const CGObjectInstance *firstObj = frontOrNull(cb->getVisitableObjs(tile));
|
||||
if(firstObj && firstObj->ID == Obj::HERO && firstObj->tempOwner == ai->playerID) //we have hero at dest
|
||||
{
|
||||
const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(firstObj);
|
||||
return CGoal(*this).sethero(h).setisElementar(true);
|
||||
}
|
||||
|
||||
return CGoal(VISIT_TILE).settile(tile);
|
||||
}
|
||||
|
||||
TSubgoal BuildThis::whatToDoToAchieve()
|
||||
{
|
||||
//TODO check res
|
||||
//look for town
|
||||
//prerequisites?
|
||||
I_AM_ELEMENTAR;
|
||||
}
|
||||
|
||||
TSubgoal CollectRes::whatToDoToAchieve()
|
||||
{
|
||||
std::vector<const IMarket*> markets;
|
||||
|
||||
std::vector<const CGObjectInstance*> visObjs;
|
||||
ai->retreiveVisitableObjs(visObjs, true);
|
||||
for(const CGObjectInstance *obj : visObjs)
|
||||
{
|
||||
if(const IMarket *m = IMarket::castFrom(obj, false))
|
||||
{
|
||||
if(obj->ID == Obj::TOWN && obj->tempOwner == ai->playerID && m->allowsTrade(EMarketMode::RESOURCE_RESOURCE))
|
||||
markets.push_back(m);
|
||||
else if(obj->ID == Obj::TRADING_POST) //TODO a moze po prostu test na pozwalanie handlu?
|
||||
markets.push_back(m);
|
||||
}
|
||||
}
|
||||
|
||||
boost::sort(markets, [](const IMarket *m1, const IMarket *m2) -> bool
|
||||
{
|
||||
return m1->getMarketEfficiency() < m2->getMarketEfficiency();
|
||||
});
|
||||
|
||||
markets.erase(boost::remove_if(markets, [](const IMarket *market) -> bool
|
||||
{
|
||||
return !(market->o->ID == Obj::TOWN && market->o->tempOwner == ai->playerID)
|
||||
&& !ai->isAccessible(market->o->visitablePos());
|
||||
}),markets.end());
|
||||
|
||||
if(!markets.size())
|
||||
{
|
||||
for(const CGTownInstance *t : cb->getTownsInfo())
|
||||
{
|
||||
if(cb->canBuildStructure(t, BuildingID::MARKETPLACE) == EBuildingState::ALLOWED)
|
||||
return CGoal(BUILD_STRUCTURE).settown(t).setbid(BuildingID::MARKETPLACE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const IMarket *m = markets.back();
|
||||
//attempt trade at back (best prices)
|
||||
int howManyCanWeBuy = 0;
|
||||
for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
|
||||
{
|
||||
if(i == resID) continue;
|
||||
int toGive = -1, toReceive = -1;
|
||||
m->getOffer(i, resID, toGive, toReceive, EMarketMode::RESOURCE_RESOURCE);
|
||||
assert(toGive > 0 && toReceive > 0);
|
||||
howManyCanWeBuy += toReceive * (cb->getResourceAmount(i) / toGive);
|
||||
}
|
||||
|
||||
if(howManyCanWeBuy + cb->getResourceAmount(static_cast<Res::ERes>(resID)) >= value)
|
||||
{
|
||||
auto backObj = backOrNull(cb->getVisitableObjs(m->o->visitablePos())); //it'll be a hero if we have one there; otherwise marketplace
|
||||
assert(backObj);
|
||||
if(backObj->tempOwner != ai->playerID)
|
||||
return CGoal(GET_OBJ).setobjid(m->o->id.getNum());
|
||||
return setobjid(m->o->id.getNum()).setisElementar(true);
|
||||
}
|
||||
}
|
||||
return CGoal(INVALID); //FIXME: unused?
|
||||
}
|
||||
|
||||
TSubgoal GatherTroops::whatToDoToAchieve()
|
||||
{
|
||||
std::vector<const CGDwelling *> dwellings;
|
||||
for(const CGTownInstance *t : cb->getTownsInfo())
|
||||
{
|
||||
auto creature = VLC->creh->creatures[objid];
|
||||
if (t->subID == creature->faction) //TODO: how to force AI to build unupgraded creatures? :O
|
||||
{
|
||||
auto creatures = vstd::tryAt(t->town->creatures, creature->level - 1);
|
||||
if(!creatures)
|
||||
continue;
|
||||
|
||||
int upgradeNumber = vstd::find_pos(*creatures, creature->idNumber);
|
||||
if(upgradeNumber < 0)
|
||||
continue;
|
||||
|
||||
BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
|
||||
if (t->hasBuilt(bid)) //this assumes only creatures with dwellings are assigned to faction
|
||||
{
|
||||
dwellings.push_back(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CGoal (BUILD_STRUCTURE).settown(t).setbid(bid);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto obj : ai->visitableObjs)
|
||||
{
|
||||
if (obj->ID != Obj::CREATURE_GENERATOR1) //TODO: what with other creature generators?
|
||||
continue;
|
||||
|
||||
auto d = dynamic_cast<const CGDwelling *>(obj);
|
||||
for (auto creature : d->creatures)
|
||||
{
|
||||
if (creature.first) //there are more than 0 creatures avaliabe
|
||||
{
|
||||
for (auto type : creature.second)
|
||||
{
|
||||
if (type == objid && ai->freeResources().canAfford(VLC->creh->creatures[type]->cost))
|
||||
dwellings.push_back(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dwellings.size())
|
||||
{
|
||||
boost::sort(dwellings, isCloser);
|
||||
return CGoal(GET_OBJ).setobjid (dwellings.front()->id.getNum());
|
||||
}
|
||||
else
|
||||
return CGoal(EXPLORE);
|
||||
//TODO: exchange troops between heroes
|
||||
}
|
||||
|
||||
TSubgoal Conquer::whatToDoToAchieve()
|
||||
{
|
||||
auto hs = cb->getHeroesInfo();
|
||||
int howManyHeroes = hs.size();
|
||||
|
||||
erase(hs, [](const CGHeroInstance *h)
|
||||
{
|
||||
return contains(ai->lockedHeroes, h);
|
||||
});
|
||||
if(hs.empty()) //all heroes are busy. buy new one
|
||||
{
|
||||
if (howManyHeroes < 3 && ai->findTownWithTavern()) //we may want to recruit second hero. TODO: make it smart finally
|
||||
return CGoal(RECRUIT_HERO);
|
||||
else //find mobile hero with weakest army
|
||||
{
|
||||
hs = cb->getHeroesInfo();
|
||||
erase_if(hs, [](const CGHeroInstance *h)
|
||||
{
|
||||
return !h->movement; //only hero with movement are of interest for us
|
||||
});
|
||||
if (hs.empty())
|
||||
{
|
||||
if (howManyHeroes < GameConstants::MAX_HEROES_PER_PLAYER)
|
||||
return CGoal(RECRUIT_HERO);
|
||||
else
|
||||
throw cannotFulfillGoalException("No heroes with remaining MPs for exploring!\n");
|
||||
}
|
||||
boost::sort(hs, compareHeroStrength);
|
||||
}
|
||||
}
|
||||
|
||||
const CGHeroInstance *h = hs.back();
|
||||
cb->setSelection(h);
|
||||
std::vector<const CGObjectInstance *> objs; //here we'll gather enemy towns and heroes
|
||||
ai->retreiveVisitableObjs(objs);
|
||||
erase_if(objs, [&](const CGObjectInstance *obj)
|
||||
{
|
||||
return (obj->ID != Obj::TOWN && obj->ID != Obj::HERO) //not town/hero
|
||||
|| cb->getPlayerRelations(ai->playerID, obj->tempOwner) != PlayerRelations::ENEMIES;
|
||||
});
|
||||
|
||||
if (objs.empty()) //experiment - try to conquer dwellings and mines, it should pay off
|
||||
{
|
||||
ai->retreiveVisitableObjs(objs);
|
||||
erase_if(objs, [&](const CGObjectInstance *obj)
|
||||
{
|
||||
return (obj->ID != Obj::CREATURE_GENERATOR1 && obj->ID != Obj::MINE) //not dwelling or mine
|
||||
|| cb->getPlayerRelations(ai->playerID, obj->tempOwner) != PlayerRelations::ENEMIES;
|
||||
});
|
||||
}
|
||||
|
||||
if(objs.empty())
|
||||
return CGoal(EXPLORE); //we need to find an enemy
|
||||
|
||||
erase_if(objs, [&](const CGObjectInstance *obj)
|
||||
{
|
||||
return !isSafeToVisit(h, obj->visitablePos()) || vstd::contains (ai->reservedObjs, obj); //no need to capture same object twice
|
||||
});
|
||||
|
||||
if(objs.empty())
|
||||
I_AM_ELEMENTAR;
|
||||
|
||||
boost::sort(objs, isCloser);
|
||||
for(const CGObjectInstance *obj : objs)
|
||||
{
|
||||
if (ai->isAccessibleForHero(obj->visitablePos(), h))
|
||||
{
|
||||
ai->reserveObject(h, obj); //no one else will capture same object until we fail
|
||||
|
||||
if (obj->ID == Obj::HERO)
|
||||
return CGoal(VISIT_HERO).sethero(h).setobjid(obj->id.getNum()).setisAbstract(true); //track enemy hero
|
||||
else
|
||||
return CGoal(VISIT_TILE).sethero(h).settile(obj->visitablePos());
|
||||
}
|
||||
}
|
||||
|
||||
return CGoal(EXPLORE); //enemy is inaccessible
|
||||
}
|
||||
|
||||
TSubgoal Build::whatToDoToAchieve()
|
||||
{
|
||||
I_AM_ELEMENTAR;
|
||||
}
|
||||
|
||||
TSubgoal Invalid::whatToDoToAchieve()
|
||||
{
|
||||
I_AM_ELEMENTAR;
|
||||
}
|
||||
|
||||
TSubgoal GatherArmy::whatToDoToAchieve()
|
||||
{
|
||||
//TODO: find hero if none set
|
||||
assert(hero);
|
||||
|
||||
cb->setSelection(*hero);
|
||||
auto compareReinforcements = [this](const CGTownInstance *lhs, const CGTownInstance *rhs) -> bool
|
||||
{
|
||||
return howManyReinforcementsCanGet(hero, lhs) < howManyReinforcementsCanGet(hero, rhs);
|
||||
};
|
||||
|
||||
std::vector<const CGTownInstance *> townsReachable;
|
||||
for(const CGTownInstance *t : cb->getTownsInfo())
|
||||
{
|
||||
if(!t->visitingHero && howManyReinforcementsCanGet(hero,t))
|
||||
{
|
||||
if(isReachable(t) && !vstd::contains (ai->townVisitsThisWeek[hero], t))
|
||||
townsReachable.push_back(t);
|
||||
}
|
||||
}
|
||||
|
||||
if(townsReachable.size()) //try towns first
|
||||
{
|
||||
boost::sort(townsReachable, compareReinforcements);
|
||||
return CGoal(VISIT_TILE).sethero(hero).settile(townsReachable.back()->visitablePos());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hero == ai->primaryHero()) //we can get army from other heroes
|
||||
{
|
||||
auto otherHeroes = cb->getHeroesInfo();
|
||||
auto heroDummy = hero;
|
||||
erase_if(otherHeroes, [heroDummy](const CGHeroInstance * h)
|
||||
{
|
||||
return (h == heroDummy.h || !ai->isAccessibleForHero(heroDummy->visitablePos(), h, true) || !ai->canGetArmy(heroDummy.h, h));
|
||||
});
|
||||
if (otherHeroes.size())
|
||||
{
|
||||
boost::sort(otherHeroes, compareArmyStrength); //TODO: check if hero has at least one stack more powerful than ours? not likely to fail
|
||||
int primaryPath, secondaryPath;
|
||||
auto h = otherHeroes.back();
|
||||
cb->setSelection(hero.h);
|
||||
primaryPath = cb->getPathInfo(h->visitablePos())->turns;
|
||||
cb->setSelection(h);
|
||||
secondaryPath = cb->getPathInfo(hero->visitablePos())->turns;
|
||||
|
||||
if (primaryPath < secondaryPath)
|
||||
return CGoal(VISIT_HERO).setisAbstract(true).setobjid(h->id.getNum()).sethero(hero); //go to the other hero if we are faster
|
||||
else
|
||||
return CGoal(VISIT_HERO).setisAbstract(true).setobjid(hero->id.getNum()).sethero(h); //let the other hero come to us
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<const CGObjectInstance *> objs; //here we'll gather all dwellings
|
||||
ai->retreiveVisitableObjs(objs, true);
|
||||
erase_if(objs, [&](const CGObjectInstance *obj)
|
||||
{
|
||||
if(obj->ID != Obj::CREATURE_GENERATOR1)
|
||||
return true;
|
||||
|
||||
auto relationToOwner = cb->getPlayerRelations(obj->getOwner(), ai->playerID);
|
||||
if(relationToOwner == PlayerRelations::ALLIES)
|
||||
return true;
|
||||
|
||||
//Use flagged dwellings only when there are available creatures that we can afford
|
||||
if(relationToOwner == PlayerRelations::SAME_PLAYER)
|
||||
{
|
||||
auto dwelling = dynamic_cast<const CGDwelling*>(obj);
|
||||
for(auto & creLevel : dwelling->creatures)
|
||||
{
|
||||
if(creLevel.first)
|
||||
{
|
||||
for(auto & creatureID : creLevel.second)
|
||||
{
|
||||
auto creature = VLC->creh->creatures[creatureID];
|
||||
if(ai->freeResources().canAfford(creature->cost))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
if(objs.empty()) //no possible objects, we did eveyrthing already
|
||||
return CGoal(EXPLORE).sethero(hero);
|
||||
//TODO: check if we can recruit any creatures there, evaluate army
|
||||
else
|
||||
{
|
||||
boost::sort(objs, isCloser);
|
||||
HeroPtr h = nullptr;
|
||||
for(const CGObjectInstance *obj : objs)
|
||||
{ //find safe dwelling
|
||||
auto pos = obj->visitablePos();
|
||||
if (shouldVisit (hero, obj)) //creatures fit in army
|
||||
h = hero;
|
||||
else
|
||||
{
|
||||
for(auto ourHero : cb->getHeroesInfo()) //make use of multiple heroes
|
||||
{
|
||||
if (shouldVisit(ourHero, obj))
|
||||
h = ourHero;
|
||||
}
|
||||
}
|
||||
if (h && isSafeToVisit(h, pos) && ai->isAccessibleForHero(pos, h))
|
||||
return CGoal(VISIT_TILE).sethero(h).settile(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CGoal(EXPLORE).sethero(hero); //find dwelling. use current hero to prevent him from doing nothing.
|
||||
}
|
||||
|
||||
TSubgoal CGoal::whatToDoToAchieve()
|
||||
{
|
||||
logAi->debugStream() << boost::format("Decomposing goal of type %s") % name();
|
||||
return CGoal(EXPLORE);
|
||||
}
|
||||
|
||||
TSubgoal CGoal::goVisitOrLookFor(const CGObjectInstance *obj)
|
||||
{
|
||||
if(obj)
|
||||
return CGoal(GET_OBJ).setobjid(obj->id.getNum());
|
||||
else
|
||||
return CGoal(EXPLORE);
|
||||
}
|
||||
|
||||
TSubgoal CGoal::lookForArtSmart(int aid)
|
||||
{
|
||||
return CGoal(INVALID);
|
||||
}
|
||||
|
||||
bool CGoal::invalid() const
|
||||
{
|
||||
return goalType == INVALID;
|
||||
}
|
217
AI/VCAI/Goals.h
Normal file
217
AI/VCAI/Goals.h
Normal file
@ -0,0 +1,217 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/VCMI_Lib.h"
|
||||
#include "../../lib/CObjectHandler.h"
|
||||
#include "../../lib/CBuildingHandler.h"
|
||||
#include "../../lib/CCreatureHandler.h"
|
||||
#include "../../lib/CTownHandler.h"
|
||||
#include "AIUtility.h"
|
||||
|
||||
/*
|
||||
* Goals.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
struct HeroPtr;
|
||||
|
||||
enum EGoals
|
||||
{
|
||||
INVALID = -1,
|
||||
WIN, DO_NOT_LOSE, CONQUER, BUILD, //build needs to get a real reasoning
|
||||
EXPLORE, GATHER_ARMY, BOOST_HERO,
|
||||
RECRUIT_HERO,
|
||||
BUILD_STRUCTURE, //if hero set, then in visited town
|
||||
COLLECT_RES,
|
||||
GATHER_TROOPS, // val of creatures with objid
|
||||
|
||||
OBJECT_GOALS_BEGIN,
|
||||
GET_OBJ, //visit or defeat or collect the object
|
||||
FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
|
||||
VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
|
||||
|
||||
GET_ART_TYPE,
|
||||
|
||||
//BUILD_STRUCTURE,
|
||||
ISSUE_COMMAND,
|
||||
|
||||
VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
|
||||
CLEAR_WAY_TO,
|
||||
DIG_AT_TILE //elementar with hero on tile
|
||||
};
|
||||
|
||||
struct CGoal;
|
||||
typedef CGoal TSubgoal;
|
||||
|
||||
#define SETTER(type, field) CGoal &set ## field(const type &rhs) { field = rhs; return *this; }
|
||||
#if 0
|
||||
#define SETTER
|
||||
#endif // _DEBUG
|
||||
|
||||
enum {LOW_PR = -1};
|
||||
|
||||
struct CGoal
|
||||
{
|
||||
EGoals goalType;
|
||||
bool isElementar; SETTER(bool, isElementar)
|
||||
bool isAbstract; SETTER(bool, isAbstract) //allows to remember abstract goals
|
||||
int priority; SETTER(bool, priority)
|
||||
std::string name() const;
|
||||
|
||||
virtual TSubgoal whatToDoToAchieve();
|
||||
|
||||
CGoal(EGoals goal = INVALID) : goalType(goal)
|
||||
{
|
||||
priority = 0;
|
||||
isElementar = false;
|
||||
isAbstract = false;
|
||||
value = 0;
|
||||
aid = -1;
|
||||
resID = -1;
|
||||
tile = int3(-1, -1, -1);
|
||||
town = nullptr;
|
||||
}
|
||||
|
||||
bool invalid() const;
|
||||
|
||||
static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is nullptr, then we'll explore
|
||||
static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
|
||||
static TSubgoal tryRecruitHero();
|
||||
|
||||
int value; SETTER(int, value)
|
||||
int resID; SETTER(int, resID)
|
||||
int objid; SETTER(int, objid)
|
||||
int aid; SETTER(int, aid)
|
||||
int3 tile; SETTER(int3, tile)
|
||||
HeroPtr hero; SETTER(HeroPtr, hero)
|
||||
const CGTownInstance *town; SETTER(CGTownInstance *, town)
|
||||
int bid; SETTER(int, bid)
|
||||
|
||||
bool operator== (CGoal &g)
|
||||
{
|
||||
switch (goalType)
|
||||
{
|
||||
case EGoals::GET_OBJ:
|
||||
return objid == g.objid;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & goalType & isElementar & isAbstract & priority;
|
||||
h & value & resID & objid & aid & tile & hero & town & bid;
|
||||
}
|
||||
};
|
||||
|
||||
class Invalid : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class Win : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class NotLose : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class Conquer : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class Build : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class Explore : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class GatherArmy : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class BoostHero : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class RecruitHero : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class BuildThis : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class CollectRes : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class GatherTroops : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class GetObj : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class FindObj : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class VisitHero : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class GetArtOfType : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class IssueCommand : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class VisitTile : public CGoal //tile, in conjunction with hero elementar; assumes tile is reachable
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class ClearWayTo : public CGoal
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
class DigAtTile : public CGoal //elementar with hero on tile
|
||||
{
|
||||
public:
|
||||
TSubgoal whatToDoToAchieve() override;
|
||||
};
|
||||
|
||||
struct CIssueCommand : CGoal
|
||||
{
|
||||
std::function<bool()> command;
|
||||
|
||||
CIssueCommand(std::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
|
||||
};
|
1211
AI/VCAI/VCAI.cpp
1211
AI/VCAI/VCAI.cpp
File diff suppressed because it is too large
Load Diff
195
AI/VCAI/VCAI.h
195
AI/VCAI/VCAI.h
@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "AIUtility.h"
|
||||
#include "Goals.h"
|
||||
#include "../../lib/AI_Base.h"
|
||||
#include "../../CCallback.h"
|
||||
#include "../../lib/CDefObjInfoHandler.h"
|
||||
#include "../../lib/CObjectHandler.h"
|
||||
|
||||
#include "../../lib/CThreadHelper.h"
|
||||
|
||||
@ -18,54 +19,21 @@
|
||||
#include "../../lib/mapping/CMap.h"
|
||||
#include "../../lib/NetPacks.h"
|
||||
#include "../../lib/CondSh.h"
|
||||
#include "../../lib/CStopWatch.h"
|
||||
|
||||
static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
|
||||
int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
|
||||
|
||||
struct QuestInfo;
|
||||
|
||||
typedef const int3& crint3;
|
||||
typedef const std::string& crstring;
|
||||
|
||||
//provisional class for AI to store a reference to an owned hero object
|
||||
//checks if it's valid on access, should be used in place of const CGHeroInstance*
|
||||
struct HeroPtr
|
||||
{
|
||||
const CGHeroInstance *h;
|
||||
ObjectInstanceID hid;
|
||||
|
||||
public:
|
||||
std::string name;
|
||||
|
||||
|
||||
HeroPtr();
|
||||
HeroPtr(const CGHeroInstance *H);
|
||||
~HeroPtr();
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return validAndSet();
|
||||
}
|
||||
|
||||
bool operator<(const HeroPtr &rhs) const;
|
||||
const CGHeroInstance *operator->() const;
|
||||
const CGHeroInstance *operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
|
||||
|
||||
const CGHeroInstance *get(bool doWeExpectNull = false) const;
|
||||
bool validAndSet() const;
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & this->h & hid & name;
|
||||
}
|
||||
};
|
||||
|
||||
enum BattleState
|
||||
{
|
||||
NO_BATTLE,
|
||||
UPCOMING_BATTLE,
|
||||
ONGOING_BATTLE,
|
||||
ENDING_BATTLE
|
||||
};
|
||||
/*
|
||||
* VCAI.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
|
||||
class AIStatus
|
||||
{
|
||||
@ -104,96 +72,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
enum EGoals
|
||||
{
|
||||
INVALID = -1,
|
||||
WIN, DO_NOT_LOSE, CONQUER, BUILD, //build needs to get a real reasoning
|
||||
EXPLORE, GATHER_ARMY, BOOST_HERO,
|
||||
RECRUIT_HERO,
|
||||
BUILD_STRUCTURE, //if hero set, then in visited town
|
||||
COLLECT_RES,
|
||||
GATHER_TROOPS, // val of creatures with objid
|
||||
|
||||
OBJECT_GOALS_BEGIN,
|
||||
GET_OBJ, //visit or defeat or collect the object
|
||||
FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
|
||||
VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
|
||||
|
||||
GET_ART_TYPE,
|
||||
|
||||
//BUILD_STRUCTURE,
|
||||
ISSUE_COMMAND,
|
||||
|
||||
VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
|
||||
CLEAR_WAY_TO,
|
||||
DIG_AT_TILE //elementar with hero on tile
|
||||
};
|
||||
|
||||
struct CGoal;
|
||||
typedef CGoal TSubgoal;
|
||||
|
||||
#define SETTER(type, field) CGoal &set ## field(const type &rhs) { field = rhs; return *this; }
|
||||
#if 0
|
||||
#define SETTER
|
||||
#endif // _DEBUG
|
||||
|
||||
enum {LOW_PR = -1};
|
||||
|
||||
struct CGoal
|
||||
{
|
||||
EGoals goalType;
|
||||
bool isElementar; SETTER(bool, isElementar)
|
||||
bool isAbstract; SETTER(bool, isAbstract) //allows to remember abstract goals
|
||||
int priority; SETTER(bool, priority)
|
||||
std::string name() const;
|
||||
|
||||
virtual TSubgoal whatToDoToAchieve();
|
||||
|
||||
CGoal(EGoals goal = INVALID) : goalType(goal)
|
||||
{
|
||||
priority = 0;
|
||||
isElementar = false;
|
||||
isAbstract = false;
|
||||
value = 0;
|
||||
aid = -1;
|
||||
resID = -1;
|
||||
tile = int3(-1, -1, -1);
|
||||
town = nullptr;
|
||||
}
|
||||
|
||||
bool invalid() const;
|
||||
|
||||
static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is nullptr, then we'll explore
|
||||
static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
|
||||
static TSubgoal tryRecruitHero();
|
||||
|
||||
int value; SETTER(int, value)
|
||||
int resID; SETTER(int, resID)
|
||||
int objid; SETTER(int, objid)
|
||||
int aid; SETTER(int, aid)
|
||||
int3 tile; SETTER(int3, tile)
|
||||
HeroPtr hero; SETTER(HeroPtr, hero)
|
||||
const CGTownInstance *town; SETTER(CGTownInstance *, town)
|
||||
int bid; SETTER(int, bid)
|
||||
|
||||
bool operator== (CGoal &g)
|
||||
{
|
||||
switch (goalType)
|
||||
{
|
||||
case EGoals::GET_OBJ:
|
||||
return objid == g.objid;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & goalType & isElementar & isAbstract & priority;
|
||||
h & value & resID & objid & aid & tile & hero & town & bid;
|
||||
}
|
||||
};
|
||||
|
||||
enum {NOT_VISIBLE = 0, NOT_CHECKED = 1, NOT_AVAILABLE};
|
||||
|
||||
struct SectorMap
|
||||
@ -231,39 +109,6 @@ struct SectorMap
|
||||
int3 firstTileToGet(HeroPtr h, crint3 dst); //if h wants to reach tile dst, which tile he should visit to clear the way?
|
||||
};
|
||||
|
||||
struct CIssueCommand : CGoal
|
||||
{
|
||||
std::function<bool()> command;
|
||||
|
||||
CIssueCommand(std::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
|
||||
};
|
||||
|
||||
|
||||
// AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
|
||||
// This class stores object id, so we can detect when we lose access to the underlying object.
|
||||
struct ObjectIdRef
|
||||
{
|
||||
ObjectInstanceID id;
|
||||
|
||||
const CGObjectInstance *operator->() const;
|
||||
operator const CGObjectInstance *() const;
|
||||
|
||||
ObjectIdRef(ObjectInstanceID _id);
|
||||
ObjectIdRef(const CGObjectInstance *obj);
|
||||
|
||||
bool operator<(const ObjectIdRef &rhs) const;
|
||||
|
||||
|
||||
template <typename Handler> void serialize(Handler &h, const int version)
|
||||
{
|
||||
h & id;
|
||||
}
|
||||
};
|
||||
|
||||
class ObjsVector : public std::vector<ObjectIdRef>
|
||||
{
|
||||
private:
|
||||
};
|
||||
|
||||
class VCAI : public CAdventureAI
|
||||
{
|
||||
@ -482,15 +327,5 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<int id>
|
||||
bool objWithID(const CGObjectInstance *obj)
|
||||
{
|
||||
return obj->ID == id;
|
||||
}
|
||||
bool isBlockedBorderGate(int3 tileToHit);
|
||||
|
||||
bool isWeeklyRevisitable (const CGObjectInstance * obj);
|
||||
bool shouldVisit (HeroPtr h, const CGObjectInstance * obj);
|
||||
|
||||
void makePossibleUpgrades(const CArmedInstance *obj);
|
||||
bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user