2018-12-01 10:30:37 +02:00
|
|
|
/*
|
|
|
|
* Explore.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Goals.h"
|
|
|
|
#include "../VCAI.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "../AIhelper.h"
|
|
|
|
#include "../FuzzyHelper.h"
|
|
|
|
#include "../ResourceManager.h"
|
|
|
|
#include "../BuildingManager.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../../../lib/constants/StringConstants.h"
|
2018-12-29 15:55:20 +02:00
|
|
|
#include "../../../lib/CPlayerState.h"
|
2018-12-01 10:30:37 +02:00
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
namespace Goals
|
|
|
|
{
|
|
|
|
struct ExplorationHelper
|
|
|
|
{
|
|
|
|
HeroPtr hero;
|
|
|
|
int sightRadius;
|
|
|
|
float bestValue;
|
|
|
|
TSubgoal bestGoal;
|
|
|
|
VCAI * aip;
|
|
|
|
CCallback * cbp;
|
|
|
|
const TeamState * ts;
|
|
|
|
int3 ourPos;
|
|
|
|
bool allowDeadEndCancellation;
|
|
|
|
bool allowGatherArmy;
|
|
|
|
|
|
|
|
ExplorationHelper(HeroPtr h, bool gatherArmy)
|
|
|
|
{
|
2023-07-31 16:00:37 +02:00
|
|
|
cbp = cb;
|
|
|
|
aip = ai;
|
2019-01-14 13:20:46 +02:00
|
|
|
hero = h;
|
|
|
|
ts = cbp->getPlayerTeam(ai->playerID);
|
|
|
|
sightRadius = hero->getSightRadius();
|
|
|
|
bestGoal = sptr(Goals::Invalid());
|
|
|
|
bestValue = 0;
|
2022-12-07 22:51:32 +02:00
|
|
|
ourPos = h->visitablePos();
|
2019-01-14 13:20:46 +02:00
|
|
|
allowDeadEndCancellation = true;
|
|
|
|
allowGatherArmy = gatherArmy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanSector(int scanRadius)
|
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
int3 tile = int3(0, 0, ourPos.z);
|
|
|
|
|
2024-05-07 21:17:05 +02:00
|
|
|
const auto & slice = ts->fogOfWarMap[ourPos.z];
|
2022-09-18 16:39:10 +02:00
|
|
|
|
|
|
|
for(tile.x = ourPos.x - scanRadius; tile.x <= ourPos.x + scanRadius; tile.x++)
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(tile.y = ourPos.y - scanRadius; tile.y <= ourPos.y + scanRadius; tile.y++)
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
|
2022-09-18 16:39:10 +02:00
|
|
|
if(cbp->isInTheMap(tile) && slice[tile.x][tile.y])
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
scanTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanMap()
|
|
|
|
{
|
|
|
|
int3 mapSize = cbp->getMapSize();
|
|
|
|
int perimeter = 2 * sightRadius * (mapSize.x + mapSize.y);
|
|
|
|
|
|
|
|
std::vector<int3> from;
|
|
|
|
std::vector<int3> to;
|
|
|
|
|
|
|
|
from.reserve(perimeter);
|
|
|
|
to.reserve(perimeter);
|
|
|
|
|
|
|
|
foreach_tile_pos([&](const int3 & pos)
|
|
|
|
{
|
2024-05-07 21:17:05 +02:00
|
|
|
if(ts->fogOfWarMap[pos.z][pos.x][pos.y])
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
bool hasInvisibleNeighbor = false;
|
|
|
|
|
|
|
|
foreach_neighbour(cbp, pos, [&](CCallback * cbp, int3 neighbour)
|
|
|
|
{
|
2024-05-07 21:17:05 +02:00
|
|
|
if(!ts->fogOfWarMap[neighbour.z][neighbour.x][neighbour.y])
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
hasInvisibleNeighbor = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(hasInvisibleNeighbor)
|
|
|
|
from.push_back(pos);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
logAi->debug("Exploration scan visible area perimeter for hero %s", hero.name);
|
|
|
|
|
|
|
|
for(const int3 & tile : from)
|
|
|
|
{
|
|
|
|
scanTile(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!bestGoal->invalid())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowDeadEndCancellation = false;
|
|
|
|
|
|
|
|
for(int i = 0; i < sightRadius; i++)
|
|
|
|
{
|
|
|
|
getVisibleNeighbours(from, to);
|
|
|
|
vstd::concatenate(from, to);
|
|
|
|
vstd::removeDuplicates(from);
|
|
|
|
}
|
|
|
|
|
|
|
|
logAi->debug("Exploration scan all possible tiles for hero %s", hero.name);
|
|
|
|
|
|
|
|
for(const int3 & tile : from)
|
|
|
|
{
|
|
|
|
scanTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanTile(const int3 & tile)
|
|
|
|
{
|
|
|
|
if(tile == ourPos
|
|
|
|
|| !aip->ah->isTileAccessible(hero, tile)) //shouldn't happen, but it does
|
|
|
|
return;
|
|
|
|
|
|
|
|
int tilesDiscovered = howManyTilesWillBeDiscovered(tile);
|
|
|
|
if(!tilesDiscovered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto waysToVisit = aip->ah->howToVisitTile(hero, tile, allowGatherArmy);
|
|
|
|
for(auto goal : waysToVisit)
|
|
|
|
{
|
|
|
|
if(goal->evaluationContext.movementCost <= 0.0) // should not happen
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float ourValue = (float)tilesDiscovered * tilesDiscovered / goal->evaluationContext.movementCost;
|
|
|
|
|
|
|
|
if(ourValue > bestValue) //avoid costly checks of tiles that don't reveal much
|
|
|
|
{
|
|
|
|
auto obj = cb->getTopObj(tile);
|
|
|
|
|
|
|
|
// picking up resources does not yield any exploration at all.
|
|
|
|
// if it blocks the way to some explorable tile AIPathfinder will take care of it
|
2023-06-21 15:49:44 +02:00
|
|
|
if(obj && obj->isBlockedVisitable())
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isSafeToVisit(hero, tile))
|
|
|
|
{
|
|
|
|
bestGoal = goal;
|
|
|
|
bestValue = ourValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out) const
|
|
|
|
{
|
|
|
|
for(const int3 & tile : tiles)
|
|
|
|
{
|
|
|
|
foreach_neighbour(cbp, tile, [&](CCallback * cbp, int3 neighbour)
|
|
|
|
{
|
2024-05-07 21:17:05 +02:00
|
|
|
if(ts->fogOfWarMap[neighbour.z][neighbour.x][neighbour.y])
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
out.push_back(neighbour);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:39:10 +02:00
|
|
|
int howManyTilesWillBeDiscovered(const int3 & pos) const
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-09-18 16:39:10 +02:00
|
|
|
int3 npos = int3(0, 0, pos.z);
|
|
|
|
|
2024-05-07 21:17:05 +02:00
|
|
|
const auto & slice = ts->fogOfWarMap[pos.z];
|
2022-09-18 16:39:10 +02:00
|
|
|
|
|
|
|
for(npos.x = pos.x - sightRadius; npos.x <= pos.x + sightRadius; npos.x++)
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
2022-09-18 16:39:10 +02:00
|
|
|
for(npos.y = pos.y - sightRadius; npos.y <= pos.y + sightRadius; npos.y++)
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
if(cbp->isInTheMap(npos)
|
|
|
|
&& pos.dist2d(npos) - 0.5 < sightRadius
|
2022-09-18 16:39:10 +02:00
|
|
|
&& !slice[npos.x][npos.y])
|
2019-01-14 13:20:46 +02:00
|
|
|
{
|
|
|
|
if(allowDeadEndCancellation
|
|
|
|
&& !hasReachableNeighbor(npos))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasReachableNeighbor(const int3 &pos) const
|
|
|
|
{
|
|
|
|
for(crint3 dir : int3::getDirs())
|
|
|
|
{
|
|
|
|
int3 tile = pos + dir;
|
|
|
|
if(cbp->isInTheMap(tile))
|
|
|
|
{
|
|
|
|
auto isAccessible = aip->ah->isTileAccessible(hero, tile);
|
|
|
|
|
|
|
|
if(isAccessible)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
bool Explore::operator==(const Explore & other) const
|
|
|
|
{
|
2019-01-02 22:51:26 +02:00
|
|
|
return other.hero.h == hero.h && other.allowGatherArmy == allowGatherArmy;
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Explore::completeMessage() const
|
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
return "Hero " + hero.get()->getNameTranslated() + " completed exploration";
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal Explore::whatToDoToAchieve()
|
|
|
|
{
|
2019-01-14 13:20:46 +02:00
|
|
|
return fh->chooseSolution(getAllPossibleSubgoals());
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec Explore::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
TGoalVec ret;
|
|
|
|
std::vector<const CGHeroInstance *> heroes;
|
|
|
|
|
|
|
|
if(hero)
|
|
|
|
{
|
|
|
|
heroes.push_back(hero.h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//heroes = ai->getUnblockedHeroes();
|
|
|
|
heroes = cb->getHeroesInfo();
|
|
|
|
vstd::erase_if(heroes, [](const HeroPtr h)
|
|
|
|
{
|
|
|
|
if(ai->getGoal(h)->goalType == EXPLORE) //do not reassign hero who is already explorer
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!ai->isAbleToExplore(h))
|
|
|
|
return true;
|
|
|
|
|
2023-06-21 19:38:26 +02:00
|
|
|
return !h->movementPointsRemaining(); //saves time, immobile heroes are useless anyway
|
2018-12-01 10:30:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//try to use buildings that uncover map
|
|
|
|
std::vector<const CGObjectInstance *> objs;
|
|
|
|
for(auto obj : ai->visitableObjs)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(ai->alreadyVisited, obj))
|
|
|
|
{
|
|
|
|
switch(obj->ID.num)
|
|
|
|
{
|
|
|
|
case Obj::REDWOOD_OBSERVATORY:
|
|
|
|
case Obj::PILLAR_OF_FIRE:
|
|
|
|
case Obj::CARTOGRAPHER:
|
|
|
|
objs.push_back(obj);
|
|
|
|
break;
|
|
|
|
case Obj::MONOLITH_ONE_WAY_ENTRANCE:
|
|
|
|
case Obj::MONOLITH_TWO_WAY:
|
|
|
|
case Obj::SUBTERRANEAN_GATE:
|
|
|
|
auto tObj = dynamic_cast<const CGTeleport *>(obj);
|
|
|
|
assert(ai->knownTeleportChannels.find(tObj->channel) != ai->knownTeleportChannels.end());
|
|
|
|
if(TeleportChannel::IMPASSABLE != ai->knownTeleportChannels[tObj->channel]->passability)
|
|
|
|
objs.push_back(obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(obj->ID.num)
|
|
|
|
{
|
|
|
|
case Obj::MONOLITH_TWO_WAY:
|
|
|
|
case Obj::SUBTERRANEAN_GATE:
|
|
|
|
auto tObj = dynamic_cast<const CGTeleport *>(obj);
|
|
|
|
if(TeleportChannel::IMPASSABLE == ai->knownTeleportChannels[tObj->channel]->passability)
|
|
|
|
break;
|
|
|
|
for(auto exit : ai->knownTeleportChannels[tObj->channel]->exits)
|
|
|
|
{
|
|
|
|
if(!cb->getObj(exit))
|
|
|
|
{ // Always attempt to visit two-way teleports if one of channel exits is not visible
|
|
|
|
objs.push_back(obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto h : heroes)
|
|
|
|
{
|
|
|
|
for(auto obj : objs) //double loop, performance risk?
|
|
|
|
{
|
2019-01-02 22:51:26 +02:00
|
|
|
auto waysToVisitObj = ai->ah->howToVisitObj(h, obj, allowGatherArmy);
|
2018-12-01 10:30:37 +02:00
|
|
|
|
|
|
|
vstd::concatenate(ret, waysToVisitObj);
|
|
|
|
}
|
|
|
|
|
2019-01-02 22:51:26 +02:00
|
|
|
TSubgoal goal = exploreNearestNeighbour(h);
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2019-01-02 22:51:26 +02:00
|
|
|
if(!goal->invalid())
|
2018-12-01 10:30:37 +02:00
|
|
|
{
|
2019-01-02 22:51:26 +02:00
|
|
|
ret.push_back(goal);
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
2019-01-02 22:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ret.empty())
|
|
|
|
{
|
|
|
|
for(auto h : heroes)
|
2018-12-01 10:30:37 +02:00
|
|
|
{
|
2023-01-02 13:27:03 +02:00
|
|
|
logAi->trace("Exploration searching for a new point for hero %s", h->getNameTranslated());
|
2019-01-14 13:20:46 +02:00
|
|
|
|
2019-01-02 22:51:26 +02:00
|
|
|
TSubgoal goal = explorationNewPoint(h);
|
|
|
|
|
|
|
|
if(goal->invalid())
|
|
|
|
{
|
|
|
|
ai->markHeroUnableToExplore(h); //there is no freely accessible tile, do not poll this hero anymore
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.push_back(goal);
|
|
|
|
}
|
2018-12-01 10:30:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2018-12-01 10:30:37 +02:00
|
|
|
//we either don't have hero yet or none of heroes can explore
|
|
|
|
if((!hero || ret.empty()) && ai->canRecruitAnyHero())
|
|
|
|
ret.push_back(sptr(RecruitHero()));
|
|
|
|
|
|
|
|
if(ret.empty())
|
|
|
|
{
|
|
|
|
throw goalFulfilledException(sptr(Explore().sethero(hero)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Explore::fulfillsMe(TSubgoal goal)
|
|
|
|
{
|
|
|
|
if(goal->goalType == EXPLORE)
|
|
|
|
{
|
|
|
|
if(goal->hero)
|
|
|
|
return hero == goal->hero;
|
|
|
|
else
|
|
|
|
return true; //cancel ALL exploration
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
TSubgoal Explore::explorationBestNeighbour(int3 hpos, HeroPtr h) const
|
2018-12-24 15:19:04 +02:00
|
|
|
{
|
2019-01-14 13:20:46 +02:00
|
|
|
ExplorationHelper scanResult(h, allowGatherArmy);
|
2018-12-24 15:19:04 +02:00
|
|
|
|
|
|
|
for(crint3 dir : int3::getDirs())
|
|
|
|
{
|
|
|
|
int3 tile = hpos + dir;
|
|
|
|
if(cb->isInTheMap(tile))
|
|
|
|
{
|
2019-01-14 13:20:46 +02:00
|
|
|
scanResult.scanTile(tile);
|
2018-12-24 15:19:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
return scanResult.bestGoal;
|
2018-12-24 15:19:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
TSubgoal Explore::explorationNewPoint(HeroPtr h) const
|
2019-01-02 22:51:26 +02:00
|
|
|
{
|
2019-01-14 13:20:46 +02:00
|
|
|
ExplorationHelper scanResult(h, allowGatherArmy);
|
2019-01-02 22:51:26 +02:00
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
scanResult.scanSector(10);
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
if(!scanResult.bestGoal->invalid())
|
2018-12-24 15:19:04 +02:00
|
|
|
{
|
2019-01-14 13:20:46 +02:00
|
|
|
return scanResult.bestGoal;
|
2019-01-02 22:51:26 +02:00
|
|
|
}
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
scanResult.scanMap();
|
2018-12-24 15:19:04 +02:00
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
return scanResult.bestGoal;
|
2018-12-24 15:19:04 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 13:20:46 +02:00
|
|
|
|
2019-01-02 22:51:26 +02:00
|
|
|
TSubgoal Explore::exploreNearestNeighbour(HeroPtr h) const
|
2018-12-24 15:19:04 +02:00
|
|
|
{
|
|
|
|
TimeCheck tc("where to explore");
|
|
|
|
int3 hpos = h->visitablePos();
|
|
|
|
|
2019-01-15 05:00:00 +02:00
|
|
|
//look for nearby objs -> visit them if they're close enough
|
2018-12-24 15:19:04 +02:00
|
|
|
const int DIST_LIMIT = 3;
|
2020-10-01 10:38:06 +02:00
|
|
|
const float COST_LIMIT = .2f; //todo: fine tune
|
2018-12-24 15:19:04 +02:00
|
|
|
|
|
|
|
std::vector<const CGObjectInstance *> nearbyVisitableObjs;
|
|
|
|
for(int x = hpos.x - DIST_LIMIT; x <= hpos.x + DIST_LIMIT; ++x) //get only local objects instead of all possible objects on the map
|
|
|
|
{
|
|
|
|
for(int y = hpos.y - DIST_LIMIT; y <= hpos.y + DIST_LIMIT; ++y)
|
|
|
|
{
|
|
|
|
for(auto obj : cb->getVisitableObjs(int3(x, y, hpos.z), false))
|
|
|
|
{
|
2019-01-15 07:52:55 +02:00
|
|
|
if(ai->isGoodForVisit(obj, h, COST_LIMIT))
|
2018-12-24 15:19:04 +02:00
|
|
|
{
|
|
|
|
nearbyVisitableObjs.push_back(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nearbyVisitableObjs.size())
|
|
|
|
{
|
2019-01-02 22:51:26 +02:00
|
|
|
vstd::removeDuplicates(nearbyVisitableObjs); //one object may occupy multiple tiles
|
|
|
|
boost::sort(nearbyVisitableObjs, CDistanceSorter(h.get()));
|
|
|
|
|
2018-12-24 15:19:04 +02:00
|
|
|
TSubgoal pickupNearestObj = fh->chooseSolution(ai->ah->howToVisitObj(h, nearbyVisitableObjs.back(), false));
|
|
|
|
|
|
|
|
if(!pickupNearestObj->invalid())
|
|
|
|
{
|
|
|
|
return pickupNearestObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if nearby tiles allow us to reveal anything - this is quick
|
2019-01-14 13:20:46 +02:00
|
|
|
return explorationBestNeighbour(hpos, h);
|
2019-01-07 23:40:09 +02:00
|
|
|
}
|