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

First attempt to replace rule-of-a-thumb behavior with actual reasoning

- GATHER_ARMY goal replaces wander function
- Heroes will remember their own goals until they are fulfilled
- Goal search handles mutual dependency
- AI will recruit multiple heroes for exploration
- Lots of tweaks
This commit is contained in:
DjWarmonger 2012-03-13 12:47:47 +00:00
parent f2f39f1497
commit 9518474ec7
5 changed files with 543 additions and 470 deletions

View File

@ -191,6 +191,18 @@ bool remove_if_present(Container &c, const Item &item)
return false;
}
template <typename V, typename Item, typename Item2>
bool remove_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)
{
@ -284,15 +296,11 @@ ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor)
if(t->visitable)
{
dangerousObject = dynamic_cast<CArmedInstance*>(t->visitableObjects.back());
objectDanger = evaluateDanger(t->visitableObjects.back()); //unguarded objects can also be dangerous or unhandled
if (dangerousObject)
{
objectDanger = evaluateDanger(dangerousObject);
objectDanger *= fh->getTacticalAdvantage (visitor, dangerousObject);
}
else
{
objectDanger = 0;
}
}
int3 guardPos = cb->guardingCreaturePosition(tile);
@ -331,6 +339,11 @@ ui64 evaluateDanger(const CGObjectInstance *obj)
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::CRYPT: //crypt
case Obj::CREATURE_BANK: //crebank
case Obj::DRAGON_UTOPIA:
@ -834,7 +847,9 @@ void VCAI::makeTurn()
BNLOG("Player %d starting turn", playerID);
INDENT;
if(cb->getDate(1) == 1)
switch(cb->getDate(1))
{
case 1:
{
townVisitsThisWeek.clear();
std::vector<const CGObjectInstance *> objs;
@ -851,6 +866,30 @@ void VCAI::makeTurn()
}
}
}
case 7: //reconsider strategy
{
const CGHeroInstance * h = primaryHero();
if (h) //check if our primary hero can ahndle danger
{
ui64 totalDanger = 0;
int dangerousObjects = 0;
std::vector<const CGObjectInstance *> objs;
retreiveVisitableObjs(objs, false);
BOOST_FOREACH (auto obj, objs)
{
if (evaluateDanger(obj)) //potentilaly dnagerous
{
totalDanger += evaluateDanger (obj->visitablePos(), h);
++dangerousObjects;
}
}
if (dangerousObjects && totalDanger / dangerousObjects > h->getHeroStrength())
{
setGoal (h, CGoal(GATHER_ARMY).sethero(h));
}
}
}
}
if(cb->getSelectedHero())
cb->recalculatePaths();
@ -862,7 +901,6 @@ void VCAI::makeTurn()
void VCAI::makeTurnInternal()
{
blockedHeroes.clear();
saving = 0;
//it looks messy here, but it's better to have armed heroes before attempting realizing goals
@ -872,8 +910,12 @@ void VCAI::makeTurnInternal()
try
{
striveToGoal(CGoal(WIN));
striveToGoal(CGoal(BUILD));
striveToGoal(CGoal(EXPLORE)); //if we have any MPs left, why not use them?
for (auto hg = lockedHeroes.begin(); hg != lockedHeroes.end(); hg++) //continue our goals
{
if (!hg->second.invalid())
striveToGoal (hg->second);
}
striveToGoal(CGoal(BUILD)); //TODO: smarter building management
}
catch(boost::thread_interrupted &e)
{
@ -1058,76 +1100,12 @@ void VCAI::wander(const CGHeroInstance * h)
while(1)
{
auto dests = getPossibleDestinations(h);
if(!dests.size())
{
auto compareReinforcements = [h](const CGTownInstance *lhs, const CGTownInstance *rhs) -> bool
{
return howManyReinforcementsCanGet(h, lhs) < howManyReinforcementsCanGet(h, rhs);
};
std::vector<const CGTownInstance *> townsReachable;
std::vector<const CGTownInstance *> townsNotReachable;
BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
{
if(!t->visitingHero && howManyReinforcementsCanGet(h,t) && !vstd::contains(townVisitsThisWeek[h], t))
{
if(isReachable(t))
townsReachable.push_back(t);
else
townsNotReachable.push_back(t);
}
}
// towns.erase(boost::remove_if(towns, [=](const CGTownInstance *t) -> bool
// {
// return !!t->visitingHero || !isReachable(t) || !howManyReinforcementsCanGet(h,t) || vstd::contains(townVisitsThisWeek[h], t);
// }),towns.end());
if(townsReachable.size())
{
boost::sort(townsReachable, compareReinforcements);
dests.push_back(townsReachable.back());
}
else if(townsNotReachable.size())
{
boost::sort(townsNotReachable, compareReinforcements);
//TODO pick the truly best
const CGTownInstance *t = townsNotReachable.back();
BNLOG("%s can't reach any town, we'll try to make our way to %s at %s", h->name % t->name % t->visitablePos());
int3 pos1 = h->pos;
striveToGoal(CGoal(CLEAR_WAY_TO).settile(t->visitablePos()).sethero(h));
if(pos1 == h->pos && h == primaryHero()) //hero can't move
{
/*boost::sort(unreachableTowns, compareArmyStrength);*/
//BOOST_FOREACH(const CGTownInstance *t, unreachableTowns)
if(cb->getResourceAmount(Res::GOLD) >= HERO_GOLD_COST && cb->getHeroesInfo().size() < ALLOWED_ROAMING_HEROES && cb->getAvailableHeroes(t).size())
recruitHero(t);
}
break;
}
else if(cb->getResourceAmount(Res::GOLD) >= HERO_GOLD_COST)
{
std::vector<const CGTownInstance *> towns = cb->getTownsInfo();
erase_if(towns, [](const CGTownInstance *t) -> bool
{
BOOST_FOREACH(const CGHeroInstance *h, cb->getHeroesInfo())
if(!t->getArmyStrength() || howManyReinforcementsCanGet(h, t))
return true;
return false;
});
boost::sort(towns, compareArmyStrength);
if(towns.size())
recruitHero(towns.back());
break;
}
else
if(!dests.size()) //TODO: merge with GATHER_ARMY goal
{
PNLOG("Nowhere more to go...\n");
setGoal (h, INVALID);
break;
}
}
if(!goVisitObj(dests.front(), h))
{
@ -1144,6 +1122,16 @@ void VCAI::wander(const CGHeroInstance * h)
}
}
void VCAI::setGoal (const CGHeroInstance *h, const CGoal goal)
{ //TODO: check for presence?
lockedHeroes[h] = goal;
}
void VCAI::setGoal (const CGHeroInstance *h, EGoals goalType)
{
lockedHeroes[h] = CGoal(goalType);
}
void VCAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
{
assert(playerID > GameConstants::PLAYER_LIMIT || status.getBattle() == UPCOMING_BATTLE);
@ -1313,7 +1301,7 @@ bool VCAI::moveHeroToTile(int3 dst, const CGHeroInstance * h)
//stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
if(path.nodes[i-1].turns)
{
blockedHeroes.insert(h); //to avoid attempts of moving heroes with very little MPs
//blockedHeroes.insert(h); //to avoid attempts of moving heroes with very little MPs
break;
}
@ -1331,7 +1319,10 @@ bool VCAI::moveHeroToTile(int3 dst, const CGHeroInstance * h)
waitTillFree(); //movement may cause battle or blocking dialog
boost::this_thread::interruption_point();
if(h->tempOwner != playerID) //we lost hero
{
remove_if_present(lockedHeroes, h);
break;
}
}
ret = !i;
@ -1408,7 +1399,8 @@ void VCAI::tryRealize(CGoal g)
throw cannotFulfillGoalException("Cannot visit tile: hero is out of MPs!");
if(!g.isBlockedBorderGate(g.tile))
{
ai->moveHeroToTile(g.tile, g.hero);
if (ai->moveHeroToTile(g.tile, g.hero));
setGoal (g.hero, INVALID); //this hero reached target and no goal
}
else
throw cannotFulfillGoalException("There's a blocked gate!");
@ -1447,10 +1439,13 @@ void VCAI::tryRealize(CGoal g)
{
assert(g.hero->visitablePos() == g.tile);
if (g.hero->diggingStatus() == CGHeroInstance::CAN_DIG)
{
cb->dig (g.hero);
setGoal (g.hero, INVALID); // finished digging
}
else
{
ai->blockedHeroes.insert(g.hero);
ai->lockedHeroes[g.hero] = g; //hero who tries to dig shouldn't do anything else
throw cannotFulfillGoalException("A hero can't dig!\n");
}
}
@ -1484,6 +1479,8 @@ void VCAI::tryRealize(CGoal g)
}
case CONQUER:
case GATHER_ARMY:
case BOOST_HERO:
// TODO: conquer??
throw cannotFulfillGoalException("I don't know how to fulfill this!");
@ -1495,14 +1492,14 @@ void VCAI::tryRealize(CGoal g)
throw cannotFulfillGoalException("I don't know how to fulfill this!");
default:
assert(0);
throw cannotFulfillGoalException("Unknown type of goal !");
}
}
const CGTownInstance * VCAI::findTownWithTavern() const
{
BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
if(vstd::contains(t->builtBuildings, EBuilding::TAVERN))
if(vstd::contains(t->builtBuildings, EBuilding::TAVERN) && !t->visitingHero)
return t;
return NULL;
@ -1511,9 +1508,11 @@ const CGTownInstance * VCAI::findTownWithTavern() const
std::vector<const CGHeroInstance *> VCAI::getUnblockedHeroes() const
{
std::vector<const CGHeroInstance *> ret = cb->getHeroesInfo();
BOOST_FOREACH(const CGHeroInstance *h, blockedHeroes)
remove_if_present(ret, h);
BOOST_FOREACH(auto h, lockedHeroes)
{
if (!h.second.invalid()) //we can use heroes without valid goal
remove_if_present(ret, h.first);
}
return ret;
}
@ -1550,7 +1549,8 @@ void VCAI::striveToGoal(const CGoal &ultimateGoal)
{
CGoal goal = ultimateGoal;
BNLOG("Striving to goal of type %d", ultimateGoal.goalType);
while(!goal.isElementar)
int maxGoals = 100; //preventing deadlock for mutually dependent goals
while(!goal.isElementar && maxGoals)
{
INDENT;
BNLOG("Considering goal %d.", goal.goalType);
@ -1558,6 +1558,7 @@ void VCAI::striveToGoal(const CGoal &ultimateGoal)
{
boost::this_thread::interruption_point();
goal = goal.whatToDoToAchieve();
--maxGoals;
}
catch(std::exception &e)
{
@ -1569,6 +1570,17 @@ void VCAI::striveToGoal(const CGoal &ultimateGoal)
try
{
boost::this_thread::interruption_point();
if (goal.hero) //lock this hero to fulfill ultimate goal
{
if (maxGoals)
{
setGoal (goal.hero, goal);
}
else
{
setGoal (goal.hero, INVALID); // we seemingly don't know what to do with hero
}
}
tryRealize(goal);
boost::this_thread::interruption_point();
}
@ -2026,19 +2038,34 @@ TSubgoal CGoal::whatToDoToAchieve()
//return CGoal(EXPLORE); // TODO improve
case EXPLORE:
{
if(cb->getHeroesInfo().empty())
return CGoal(RECRUIT_HERO);
auto hs = cb->getHeroesInfo();
assert(hs.size());
int howManyHeroes = hs.size();
erase(hs, [](const CGHeroInstance *h)
{
return !h->movement || contains(ai->blockedHeroes, h); //only hero with movement are of interest for us
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.front();
@ -2090,7 +2117,7 @@ TSubgoal CGoal::whatToDoToAchieve()
if(isSafeToVisit(hero, tile))
return CGoal(*this).setisElementar(true);
else
return CGoal(INVALID); //todo can gather army?
return CGoal(GATHER_ARMY).sethero(hero);
}
else //inaccessible for all heroes
return CGoal(CLEAR_WAY_TO).settile(tile);
@ -2181,7 +2208,7 @@ TSubgoal CGoal::whatToDoToAchieve()
std::vector<const CGHeroInstance *> heroes = cb->getHeroesInfo();
erase_if(heroes, [](const CGHeroInstance *h)
{
return vstd::contains(ai->blockedHeroes, h) || !h->movement;
return vstd::contains(ai->lockedHeroes, h) || !h->movement;
});
boost::sort(heroes, compareHeroStrength);
@ -2217,13 +2244,58 @@ TSubgoal CGoal::whatToDoToAchieve()
}
return CGoal(EXPLORE); //enemy is inaccessible
;
}
break;
case BUILD:
I_AM_ELEMENTAR;
case INVALID:
I_AM_ELEMENTAR;
case GATHER_ARMY:
{
const CGHeroInstance *h = hero;
auto compareReinforcements = [h](const CGTownInstance *lhs, const CGTownInstance *rhs) -> bool
{
return howManyReinforcementsCanGet(h, lhs) < howManyReinforcementsCanGet(h, rhs);
};
std::vector<const CGTownInstance *> townsReachable;
BOOST_FOREACH(const CGTownInstance *t, cb->getTownsInfo())
{
if(!t->visitingHero && howManyReinforcementsCanGet(h,t))
{
if(isReachable(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
{
std::vector<const CGObjectInstance *> objs; //here we'll gather all dwellings
ai->retreiveVisitableObjs(objs);
erase_if(objs, [&](const CGObjectInstance *obj)
{
return (obj->ID != Obj::CREATURE_GENERATOR1); //not town/ dwelling
});
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
boost::sort(objs, isCloser);
BOOST_FOREACH(const CGObjectInstance *obj, objs)
{ //find safe dwelling
if (isSafeToVisit(hero, obj->visitablePos())) //TODO: make use of multiple heroes
return CGoal(VISIT_TILE).sethero(hero).settile(obj->visitablePos());
}
}
return CGoal(EXPLORE); //find dwelling
}
break;
default:
assert(0);
}

View File

@ -37,7 +37,7 @@ public:
enum EGoals
{
INVALID = -1,
WIN, DO_NOT_LOSE, CONQUER, BUILD, EXPLORE, //GATHER_ARMY,// BOOST_HERO,
WIN, DO_NOT_LOSE, CONQUER, BUILD, EXPLORE, GATHER_ARMY, BOOST_HERO,
RECRUIT_HERO,
BUILD_STRUCTURE, //if hero set, then in visited town
COLLECT_RES,
@ -156,7 +156,7 @@ public:
std::vector<const CGObjectInstance *> visitedThisWeek; //only OPWs
std::map<const CGHeroInstance *, std::vector<const CGTownInstance *> > townVisitsThisWeek;
std::set<const CGHeroInstance *> blockedHeroes; //they won't get any new action
std::map<const CGHeroInstance *, CGoal> lockedHeroes; //TODO: allow non-elementar objectives
std::vector<const CGObjectInstance *> visitableObjs;
std::vector<const CGObjectInstance *> alreadyVisited;
@ -170,8 +170,6 @@ public:
VCAI(void);
~VCAI(void);
CGoal currentGoal;
CGObjectInstance * visitedObject; //remember currently viisted object
boost::thread *makingTurn;
@ -250,6 +248,8 @@ public:
void striveToGoal(const CGoal &ultimateGoal);
void endTurn();
void wander(const CGHeroInstance * h);
void setGoal (const CGHeroInstance *h, const CGoal goal);
void setGoal (const CGHeroInstance *h, EGoals goalType = INVALID);
void recruitHero(const CGTownInstance * t);
std::vector<const CGObjectInstance *> getPossibleDestinations(const CGHeroInstance *h);

View File

@ -64,6 +64,7 @@ namespace GameConstants
const int CRE_LEVELS = 10;
const int F_NUMBER = 9; //factions (town types) quantity
const int PLAYER_LIMIT = 8; //player limit per map
const int MAX_HEROES_PER_PLAYER = 8;
const int ALL_PLAYERS = 255; //bitfield
const int HEROES_PER_TYPE=8; //amount of heroes of each type
const int SKILL_QUANTITY=28;

View File

@ -2889,7 +2889,7 @@ bool CGameHandler::hireHero(const CGObjectInstance *obj, ui8 hid, ui8 player)
//common preconditions
if( (p->resources[Res::GOLD]<2500 && complain("Not enough gold for buying hero!"))
|| (getHeroCount(player, false) >= 8 && complain("Cannot hire hero, only 8 wandering heroes are allowed!")))
|| (getHeroCount(player, false) >= GameConstants::MAX_HEROES_PER_PLAYER && complain("Cannot hire hero, only 8 wandering heroes are allowed!")))
return false;
if(t) //tavern in town