2021-05-15 22:04:26 +03:00
|
|
|
/*
|
|
|
|
* Nullkiller.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
|
|
|
|
*
|
|
|
|
*/
|
2021-05-15 19:23:05 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Nullkiller.h"
|
2021-05-16 15:39:38 +03:00
|
|
|
#include "../AIGateway.h"
|
2021-05-15 19:23:11 +03:00
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
|
|
|
#include "../Behaviors/RecruitHeroBehavior.h"
|
2021-05-15 21:57:31 +03:00
|
|
|
#include "../Behaviors/BuyArmyBehavior.h"
|
2021-05-15 22:04:26 +03:00
|
|
|
#include "../Behaviors/StartupBehavior.h"
|
2021-05-16 13:53:32 +03:00
|
|
|
#include "../Behaviors/DefenceBehavior.h"
|
2021-05-16 14:15:03 +03:00
|
|
|
#include "../Behaviors/BuildingBehavior.h"
|
2021-05-16 14:19:07 +03:00
|
|
|
#include "../Behaviors/GatherArmyBehavior.h"
|
2021-05-16 14:45:12 +03:00
|
|
|
#include "../Behaviors/ClusterBehavior.h"
|
2023-09-24 13:07:42 +03:00
|
|
|
#include "../Behaviors/StayAtTownBehavior.h"
|
2024-05-19 10:04:45 +03:00
|
|
|
#include "../Behaviors/ExplorationBehavior.h"
|
2021-05-15 19:23:11 +03:00
|
|
|
#include "../Goals/Invalid.h"
|
2021-05-16 14:56:42 +03:00
|
|
|
#include "../Goals/Composition.h"
|
2024-05-19 10:04:45 +03:00
|
|
|
#include "../../../lib/CPlayerState.h"
|
|
|
|
#include "../../lib/StartInfo.h"
|
2025-01-23 14:39:56 +00:00
|
|
|
#include "../../lib/pathfinder/PathfinderCache.h"
|
|
|
|
#include "../../lib/pathfinder/PathfinderOptions.h"
|
2021-05-15 19:23:11 +03:00
|
|
|
|
2022-09-26 21:01:07 +03:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-16 14:38:26 +03:00
|
|
|
using namespace Goals;
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
// while we play vcmieagles graph can be shared
|
|
|
|
std::unique_ptr<ObjectGraph> Nullkiller::baseGraph;
|
|
|
|
|
2021-05-15 19:23:38 +03:00
|
|
|
Nullkiller::Nullkiller()
|
2024-11-27 21:26:06 +00:00
|
|
|
: activeHero(nullptr)
|
|
|
|
, scanDepth(ScanDepth::MAIN_FULL)
|
|
|
|
, useHeroChain(true)
|
2024-12-23 13:26:54 +00:00
|
|
|
, pathfinderInvalidated(false)
|
2024-11-27 21:26:06 +00:00
|
|
|
, memory(std::make_unique<AIMemory>())
|
2021-05-15 19:23:38 +03:00
|
|
|
{
|
2024-05-19 10:04:45 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-01-23 14:39:56 +00:00
|
|
|
Nullkiller::~Nullkiller() = default;
|
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
bool canUseOpenMap(std::shared_ptr<CCallback> cb, PlayerColor playerID)
|
|
|
|
{
|
|
|
|
if(!cb->getStartInfo()->extraOptionsInfo.cheatsAllowed)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TeamState * team = cb->getPlayerTeam(playerID);
|
|
|
|
|
|
|
|
auto hasHumanInTeam = vstd::contains_if(team->players, [cb](PlayerColor teamMateID) -> bool
|
|
|
|
{
|
|
|
|
return cb->getPlayerState(teamMateID)->isHuman();
|
|
|
|
});
|
|
|
|
|
|
|
|
if(hasHumanInTeam)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-11-27 21:26:06 +00:00
|
|
|
return true;
|
2020-05-04 18:58:43 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
void Nullkiller::init(std::shared_ptr<CCallback> cb, AIGateway * gateway)
|
2020-05-04 18:58:43 +03:00
|
|
|
{
|
|
|
|
this->cb = cb;
|
2024-03-31 18:39:00 +03:00
|
|
|
this->gateway = gateway;
|
2024-11-27 21:26:06 +00:00
|
|
|
this->playerID = gateway->playerID;
|
2024-05-19 10:04:45 +03:00
|
|
|
|
2024-11-27 21:26:06 +00:00
|
|
|
settings = std::make_unique<Settings>(cb->getStartInfo()->difficulty);
|
|
|
|
|
2025-01-23 14:39:56 +00:00
|
|
|
PathfinderOptions pathfinderOptions(cb.get());
|
|
|
|
|
|
|
|
pathfinderOptions.useTeleportTwoWay = true;
|
|
|
|
pathfinderOptions.useTeleportOneWay = settings->isOneWayMonolithUsageAllowed();
|
|
|
|
pathfinderOptions.useTeleportOneWayRandom = settings->isOneWayMonolithUsageAllowed();
|
|
|
|
|
|
|
|
pathfinderCache = std::make_unique<PathfinderCache>(cb.get(), pathfinderOptions);
|
|
|
|
|
2024-11-27 21:26:06 +00:00
|
|
|
if(canUseOpenMap(cb, playerID))
|
|
|
|
{
|
|
|
|
useObjectGraph = settings->isObjectGraphAllowed();
|
|
|
|
openMap = settings->isOpenMap() || useObjectGraph;
|
|
|
|
}
|
|
|
|
else
|
2024-05-19 10:04:45 +03:00
|
|
|
{
|
|
|
|
useObjectGraph = false;
|
|
|
|
openMap = false;
|
|
|
|
}
|
2020-05-04 18:58:43 +03:00
|
|
|
|
2024-02-03 12:20:59 +02:00
|
|
|
baseGraph.reset();
|
|
|
|
|
2020-05-04 18:58:43 +03:00
|
|
|
priorityEvaluator.reset(new PriorityEvaluator(this));
|
2021-05-16 15:08:56 +03:00
|
|
|
priorityEvaluators.reset(
|
|
|
|
new SharedPool<PriorityEvaluator>(
|
|
|
|
[&]()->std::unique_ptr<PriorityEvaluator>
|
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
return std::make_unique<PriorityEvaluator>(this);
|
2021-05-16 15:08:56 +03:00
|
|
|
}));
|
|
|
|
|
2020-05-04 18:58:43 +03:00
|
|
|
dangerHitMap.reset(new DangerHitMapAnalyzer(this));
|
2021-05-16 14:57:33 +03:00
|
|
|
buildAnalyzer.reset(new BuildAnalyzer(this));
|
2020-05-04 18:58:43 +03:00
|
|
|
objectClusterizer.reset(new ObjectClusterizer(this));
|
|
|
|
dangerEvaluator.reset(new FuzzyHelper(this));
|
|
|
|
pathfinder.reset(new AIPathfinder(cb.get(), this));
|
|
|
|
armyManager.reset(new ArmyManager(cb.get(), this));
|
|
|
|
heroManager.reset(new HeroManager(cb.get(), this));
|
2024-03-31 18:39:00 +03:00
|
|
|
decomposer.reset(new DeepDecomposer(this));
|
2023-06-04 16:02:02 +03:00
|
|
|
armyFormation.reset(new ArmyFormation(cb, this));
|
2021-05-15 19:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
TaskPlanItem::TaskPlanItem(TSubgoal task)
|
|
|
|
:task(task), affectedObjects(task->asTask()->getAffectedObjects())
|
2021-05-15 19:23:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
Goals::TTaskVec TaskPlan::getTasks() const
|
2021-05-15 19:23:11 +03:00
|
|
|
{
|
2024-04-14 15:23:44 +03:00
|
|
|
Goals::TTaskVec result;
|
2022-12-14 22:13:26 +02:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
for(auto & item : tasks)
|
|
|
|
{
|
|
|
|
result.push_back(taskptr(*item.task));
|
|
|
|
}
|
2021-05-15 21:27:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
vstd::removeDuplicates(result);
|
2021-05-16 14:57:33 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
return result;
|
|
|
|
}
|
2021-05-16 14:38:53 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
void TaskPlan::merge(TSubgoal task)
|
|
|
|
{
|
|
|
|
TGoalVec blockers;
|
|
|
|
|
2024-07-12 17:39:52 +02:00
|
|
|
if (task->asTask()->priority <= 0)
|
|
|
|
return;
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
for(auto & item : tasks)
|
|
|
|
{
|
|
|
|
for(auto objid : item.affectedObjects)
|
|
|
|
{
|
2024-08-07 01:37:15 +02:00
|
|
|
if(task == item.task || task->asTask()->isObjectAffected(objid) || (task->asTask()->getHero() != nullptr && task->asTask()->getHero() == item.task->asTask()->getHero()))
|
2024-04-14 15:23:44 +03:00
|
|
|
{
|
|
|
|
if(item.task->asTask()->priority >= task->asTask()->priority)
|
|
|
|
return;
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
blockers.push_back(item.task);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 19:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
vstd::erase_if(tasks, [&](const TaskPlanItem & task)
|
|
|
|
{
|
|
|
|
return vstd::contains(blockers, task.task);
|
|
|
|
});
|
|
|
|
|
|
|
|
tasks.emplace_back(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
Goals::TTask Nullkiller::choseBestTask(Goals::TGoalVec & tasks) const
|
|
|
|
{
|
2021-05-16 14:38:53 +03:00
|
|
|
if(tasks.empty())
|
|
|
|
{
|
2024-04-14 15:23:44 +03:00
|
|
|
return taskptr(Invalid());
|
|
|
|
}
|
|
|
|
|
|
|
|
for(TSubgoal & task : tasks)
|
|
|
|
{
|
|
|
|
if(task->asTask()->priority <= 0)
|
|
|
|
task->asTask()->priority = priorityEvaluator->evaluate(task);
|
|
|
|
}
|
2021-05-16 14:38:53 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
auto bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal task) -> float
|
|
|
|
{
|
|
|
|
return task->asTask()->priority;
|
|
|
|
});
|
|
|
|
|
|
|
|
return taskptr(*bestTask);
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:39:52 +02:00
|
|
|
Goals::TTaskVec Nullkiller::buildPlan(TGoalVec & tasks, int priorityTier) const
|
2024-04-14 15:23:44 +03:00
|
|
|
{
|
|
|
|
TaskPlan taskPlan;
|
|
|
|
|
2024-07-12 17:39:52 +02:00
|
|
|
tbb::parallel_for(tbb::blocked_range<size_t>(0, tasks.size()), [this, &tasks, priorityTier](const tbb::blocked_range<size_t> & r)
|
2024-04-14 15:23:44 +03:00
|
|
|
{
|
|
|
|
auto evaluator = this->priorityEvaluators->acquire();
|
|
|
|
|
|
|
|
for(size_t i = r.begin(); i != r.end(); i++)
|
|
|
|
{
|
|
|
|
auto task = tasks[i];
|
2024-09-02 00:16:19 +02:00
|
|
|
if (task->asTask()->priority <= 0 || priorityTier != PriorityEvaluator::PriorityTier::BUILDINGS)
|
2024-07-12 17:39:52 +02:00
|
|
|
task->asTask()->priority = evaluator->evaluate(task, priorityTier);
|
2024-04-14 15:23:44 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
std::sort(tasks.begin(), tasks.end(), [](TSubgoal g1, TSubgoal g2) -> bool
|
|
|
|
{
|
|
|
|
return g2->asTask()->priority < g1->asTask()->priority;
|
|
|
|
});
|
|
|
|
|
|
|
|
for(TSubgoal & task : tasks)
|
|
|
|
{
|
|
|
|
taskPlan.merge(task);
|
2021-05-16 14:38:53 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
return taskPlan.getTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Nullkiller::decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int decompositionMaxDepth) const
|
|
|
|
{
|
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
|
|
|
logAi->debug("Checking behavior %s", behavior->toString());
|
|
|
|
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
decomposer->decompose(result, behavior, decompositionMaxDepth);
|
|
|
|
|
|
|
|
boost::this_thread::interruption_point();
|
2021-05-15 19:23:38 +03:00
|
|
|
|
2021-05-16 14:56:13 +03:00
|
|
|
logAi->debug(
|
2024-04-14 15:23:44 +03:00
|
|
|
"Behavior %s. Time taken %ld",
|
2021-05-16 14:56:13 +03:00
|
|
|
behavior->toString(),
|
|
|
|
timeElapsed(start));
|
2021-05-15 19:23:11 +03:00
|
|
|
}
|
2021-05-15 19:23:05 +03:00
|
|
|
|
2021-05-15 21:57:27 +03:00
|
|
|
void Nullkiller::resetAiState()
|
|
|
|
{
|
2024-06-01 11:48:30 +00:00
|
|
|
std::unique_lock lockGuard(aiStateMutex);
|
2023-12-17 10:11:05 +02:00
|
|
|
|
2021-05-16 15:39:38 +03:00
|
|
|
lockedResources = TResources();
|
2023-07-27 15:58:49 +03:00
|
|
|
scanDepth = ScanDepth::MAIN_FULL;
|
2021-05-15 21:57:27 +03:00
|
|
|
lockedHeroes.clear();
|
2025-01-08 14:38:55 +00:00
|
|
|
dangerHitMap->resetHitmap();
|
2022-09-06 21:14:22 +03:00
|
|
|
useHeroChain = true;
|
2024-04-14 15:23:44 +03:00
|
|
|
objectClusterizer->reset();
|
2024-01-20 22:54:30 +02:00
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
if(!baseGraph && isObjectGraphAllowed())
|
2024-01-20 22:54:30 +02:00
|
|
|
{
|
|
|
|
baseGraph = std::make_unique<ObjectGraph>();
|
|
|
|
baseGraph->updateGraph(this);
|
|
|
|
}
|
2021-05-15 21:57:27 +03:00
|
|
|
}
|
|
|
|
|
2024-12-23 13:26:54 +00:00
|
|
|
void Nullkiller::invalidatePathfinderData()
|
|
|
|
{
|
|
|
|
pathfinderInvalidated = true;
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
void Nullkiller::updateAiState(int pass, bool fast)
|
2021-05-15 21:57:27 +03:00
|
|
|
{
|
2021-05-16 14:57:33 +03:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2024-06-01 11:48:30 +00:00
|
|
|
std::unique_lock lockGuard(aiStateMutex);
|
2023-12-17 10:11:05 +02:00
|
|
|
|
2021-11-23 09:41:03 +03:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-16 14:56:13 +03:00
|
|
|
|
2021-05-16 14:22:41 +03:00
|
|
|
activeHero = nullptr;
|
2023-02-28 09:07:59 +02:00
|
|
|
setTargetObject(-1);
|
2021-05-16 14:22:41 +03:00
|
|
|
|
2023-07-29 18:54:20 +03:00
|
|
|
decomposer->reset();
|
|
|
|
buildAnalyzer->update();
|
|
|
|
|
2024-12-23 13:26:54 +00:00
|
|
|
if (!pathfinderInvalidated)
|
|
|
|
logAi->trace("Skipping paths regeneration - up to date");
|
|
|
|
|
|
|
|
if(!fast && pathfinderInvalidated)
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
|
|
|
memory->removeInvisibleObjects(cb.get());
|
2021-05-16 14:59:32 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
dangerHitMap->updateHitMap();
|
2023-08-08 18:37:42 +03:00
|
|
|
dangerHitMap->calculateTileOwners();
|
2021-05-15 22:03:58 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
boost::this_thread::interruption_point();
|
2021-05-16 14:57:33 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
heroManager->update();
|
|
|
|
logAi->trace("Updating paths");
|
2021-05-16 14:56:13 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
std::map<const CGHeroInstance *, HeroRole> activeHeroes;
|
2021-05-15 21:57:27 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
for(auto hero : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
if(getHeroLockedReason(hero) == HeroLockedReason::DEFENCE)
|
|
|
|
continue;
|
2021-05-16 14:13:56 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
activeHeroes[hero] = heroManager->getHeroRole(hero);
|
|
|
|
}
|
2021-05-15 21:57:27 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
PathfinderSettings cfg;
|
|
|
|
cfg.useHeroChain = useHeroChain;
|
2024-01-27 22:19:27 +02:00
|
|
|
cfg.allowBypassObjects = true;
|
2021-05-16 14:56:21 +03:00
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
if(scanDepth == ScanDepth::SMALL || isObjectGraphAllowed())
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
2024-03-08 14:39:16 +02:00
|
|
|
cfg.mainTurnDistanceLimit = settings->getMainHeroTurnDistanceLimit();
|
2023-07-27 15:58:49 +03:00
|
|
|
}
|
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
if(scanDepth != ScanDepth::ALL_FULL || isObjectGraphAllowed())
|
2023-07-27 15:58:49 +03:00
|
|
|
{
|
2024-03-08 14:39:16 +02:00
|
|
|
cfg.scoutTurnDistanceLimit =settings->getScoutHeroTurnDistanceLimit();
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
2021-05-16 15:01:34 +03:00
|
|
|
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
pathfinder->updatePaths(activeHeroes, cfg);
|
2024-03-08 14:39:16 +02:00
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
if(isObjectGraphAllowed())
|
2024-03-08 14:39:16 +02:00
|
|
|
{
|
2024-03-29 20:39:03 +02:00
|
|
|
pathfinder->updateGraphs(
|
|
|
|
activeHeroes,
|
|
|
|
scanDepth == ScanDepth::SMALL ? 255 : 10,
|
|
|
|
scanDepth == ScanDepth::ALL_FULL ? 255 : 3);
|
2024-03-08 14:39:16 +02:00
|
|
|
}
|
2021-05-16 14:56:13 +03:00
|
|
|
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
objectClusterizer->clusterize();
|
2024-12-23 13:26:54 +00:00
|
|
|
|
|
|
|
pathfinderInvalidated = false;
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
2021-05-16 14:15:03 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
armyManager->update();
|
2021-05-16 14:56:13 +03:00
|
|
|
|
2024-12-23 13:26:54 +00:00
|
|
|
logAi->debug("AI state updated in %ld ms", timeElapsed(start));
|
2021-05-15 21:57:27 +03:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:22:41 +03:00
|
|
|
bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:13:56 +03:00
|
|
|
bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
|
|
|
|
{
|
2021-05-16 14:22:41 +03:00
|
|
|
if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
|
|
|
|
{
|
2022-09-26 21:01:07 +03:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->getObjectName(), path.toString());
|
2021-05-16 14:22:41 +03:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:13:56 +03:00
|
|
|
for(auto & node : path.nodes)
|
|
|
|
{
|
2021-05-16 14:22:41 +03:00
|
|
|
auto lockReason = getHeroLockedReason(node.targetHero);
|
|
|
|
|
|
|
|
if(lockReason != HeroLockedReason::NOT_LOCKED)
|
|
|
|
{
|
2022-09-26 21:01:07 +03:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-07-24 21:26:53 +02:00
|
|
|
logAi->trace("Hero %s is locked by %d. Discarding %s", path.targetHero->getObjectName(), (int)lockReason, path.toString());
|
2021-05-16 14:22:41 +03:00
|
|
|
#endif
|
2021-05-16 14:13:56 +03:00
|
|
|
return true;
|
2021-05-16 14:22:41 +03:00
|
|
|
}
|
2021-05-16 14:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:22:41 +03:00
|
|
|
HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
auto found = lockedHeroes.find(hero);
|
|
|
|
|
|
|
|
return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
|
|
|
|
}
|
|
|
|
|
2021-05-15 19:23:05 +03:00
|
|
|
void Nullkiller::makeTurn()
|
|
|
|
{
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::lock_guard<boost::mutex> sharedStorageLock(AISharedStorage::locker);
|
|
|
|
|
2021-05-16 14:56:42 +03:00
|
|
|
const int MAX_DEPTH = 10;
|
|
|
|
|
2024-09-04 16:41:47 +02:00
|
|
|
resetAiState();
|
|
|
|
|
|
|
|
Goals::TGoalVec bestTasks;
|
|
|
|
|
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-07-19 15:21:56 +02:00
|
|
|
float totalHeroStrength = 0;
|
|
|
|
int totalTownLevel = 0;
|
|
|
|
for (auto heroInfo : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
totalHeroStrength += heroInfo->getTotalStrength();
|
|
|
|
}
|
|
|
|
for (auto townInfo : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
totalTownLevel += townInfo->getTownLevel();
|
|
|
|
}
|
2024-08-29 20:49:01 +02:00
|
|
|
logAi->info("Beginning: Strength: %f Townlevel: %d Resources: %s", totalHeroStrength, totalTownLevel, cb->getResourceAmount().toString());
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-04-20 10:33:37 +03:00
|
|
|
for(int i = 1; i <= settings->getMaxPass() && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME; i++)
|
2021-05-15 19:23:11 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-16 14:56:21 +03:00
|
|
|
updateAiState(i);
|
2021-05-15 19:23:42 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
Goals::TTask bestTask = taskptr(Goals::Invalid());
|
2023-07-27 15:58:49 +03:00
|
|
|
|
2024-12-23 13:26:54 +00:00
|
|
|
for(int j = 1; j <= settings->getMaxPriorityPass() && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME; j++)
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
2024-04-14 15:23:44 +03:00
|
|
|
bestTasks.clear();
|
|
|
|
|
2024-07-24 21:26:53 +02:00
|
|
|
decompose(bestTasks, sptr(RecruitHeroBehavior()), 1);
|
2024-04-14 15:23:44 +03:00
|
|
|
decompose(bestTasks, sptr(BuyArmyBehavior()), 1);
|
|
|
|
decompose(bestTasks, sptr(BuildingBehavior()), 1);
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
bestTask = choseBestTask(bestTasks);
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-07-24 21:26:53 +02:00
|
|
|
if(bestTask->priority > 0)
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-09-14 02:58:23 +02:00
|
|
|
logAi->info("Pass %d: Performing prio 0 task %s with prio: %d", i, bestTask->toString(), bestTask->priority);
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-04-14 15:23:44 +03:00
|
|
|
if(!executeTask(bestTask))
|
|
|
|
return;
|
|
|
|
|
2024-12-10 19:21:23 +01:00
|
|
|
bool fastUpdate = true;
|
|
|
|
|
|
|
|
if (bestTask->getHero() != nullptr)
|
|
|
|
fastUpdate = false;
|
|
|
|
|
|
|
|
updateAiState(i, fastUpdate);
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
2023-07-27 15:58:49 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
decompose(bestTasks, sptr(CaptureObjectsBehavior()), 1);
|
|
|
|
decompose(bestTasks, sptr(ClusterBehavior()), MAX_DEPTH);
|
|
|
|
decompose(bestTasks, sptr(DefenceBehavior()), MAX_DEPTH);
|
|
|
|
decompose(bestTasks, sptr(GatherArmyBehavior()), MAX_DEPTH);
|
|
|
|
decompose(bestTasks, sptr(StayAtTownBehavior()), MAX_DEPTH);
|
2021-05-15 19:23:11 +03:00
|
|
|
|
2024-05-19 10:04:45 +03:00
|
|
|
if(!isOpenMap())
|
|
|
|
decompose(bestTasks, sptr(ExplorationBehavior()), MAX_DEPTH);
|
|
|
|
|
2024-07-24 21:26:53 +02:00
|
|
|
TTaskVec selectedTasks;
|
2024-09-06 17:20:12 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-09-05 23:41:05 +02:00
|
|
|
int prioOfTask = 0;
|
2024-09-06 17:20:12 +02:00
|
|
|
#endif
|
2024-09-02 00:16:19 +02:00
|
|
|
for (int prio = PriorityEvaluator::PriorityTier::INSTAKILL; prio <= PriorityEvaluator::PriorityTier::DEFEND; ++prio)
|
2024-07-24 21:26:53 +02:00
|
|
|
{
|
2024-09-06 17:20:12 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-09-05 23:41:05 +02:00
|
|
|
prioOfTask = prio;
|
2024-09-06 17:20:12 +02:00
|
|
|
#endif
|
2024-07-24 21:26:53 +02:00
|
|
|
selectedTasks = buildPlan(bestTasks, prio);
|
|
|
|
if (!selectedTasks.empty() || settings->isUseFuzzy())
|
|
|
|
break;
|
|
|
|
}
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-07-07 15:12:05 +02:00
|
|
|
std::sort(selectedTasks.begin(), selectedTasks.end(), [](const TTask& a, const TTask& b)
|
|
|
|
{
|
|
|
|
return a->priority > b->priority;
|
|
|
|
});
|
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
logAi->debug("Decision madel in %ld", timeElapsed(start));
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
if(selectedTasks.empty())
|
|
|
|
{
|
2024-06-01 09:08:23 +03:00
|
|
|
selectedTasks.push_back(taskptr(Goals::Invalid()));
|
2024-04-14 15:23:44 +03:00
|
|
|
}
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
bool hasAnySuccess = false;
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
for(auto bestTask : selectedTasks)
|
2021-05-16 15:01:34 +03:00
|
|
|
{
|
2024-04-20 10:33:37 +03:00
|
|
|
if(cb->getPlayerStatus(playerID) != EPlayerStatus::INGAME)
|
|
|
|
return;
|
|
|
|
|
2024-04-27 10:57:30 +03:00
|
|
|
if(!areAffectedObjectsPresent(bestTask))
|
|
|
|
{
|
|
|
|
logAi->debug("Affected object not found. Canceling task.");
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 15:08:56 +03:00
|
|
|
|
2024-04-27 10:57:30 +03:00
|
|
|
std::string taskDescription = bestTask->toString();
|
|
|
|
HeroRole heroRole = getTaskRole(bestTask);
|
2021-05-15 19:23:11 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
if(heroRole != HeroRole::MAIN || bestTask->getHeroExchangeCount() <= 1)
|
|
|
|
useHeroChain = false;
|
2024-03-29 20:39:03 +02:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
// TODO: better to check turn distance here instead of priority
|
|
|
|
if((heroRole != HeroRole::MAIN || bestTask->priority < SMALL_SCAN_MIN_PRIORITY)
|
|
|
|
&& scanDepth == ScanDepth::MAIN_FULL)
|
2023-07-27 15:58:49 +03:00
|
|
|
{
|
2024-04-14 15:23:44 +03:00
|
|
|
useHeroChain = false;
|
|
|
|
scanDepth = ScanDepth::SMALL;
|
|
|
|
|
2024-03-29 20:39:03 +02:00
|
|
|
logAi->trace(
|
2024-04-14 15:23:44 +03:00
|
|
|
"Goal %s has low priority %f so decreasing scan depth to gain performance.",
|
2024-03-29 20:39:03 +02:00
|
|
|
taskDescription,
|
|
|
|
bestTask->priority);
|
2024-04-14 15:23:44 +03:00
|
|
|
}
|
|
|
|
|
2024-07-07 22:51:50 +02:00
|
|
|
if((settings->isUseFuzzy() && bestTask->priority < MIN_PRIORITY) || (!settings->isUseFuzzy() && bestTask->priority <= 0))
|
2024-04-14 15:23:44 +03:00
|
|
|
{
|
|
|
|
auto heroes = cb->getHeroesInfo();
|
|
|
|
auto hasMp = vstd::contains_if(heroes, [](const CGHeroInstance * h) -> bool
|
|
|
|
{
|
|
|
|
return h->movementPointsRemaining() > 100;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(hasMp && scanDepth != ScanDepth::ALL_FULL)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Goal %s has too low priority %f so increasing scan depth to full.",
|
|
|
|
taskDescription,
|
|
|
|
bestTask->priority);
|
|
|
|
|
|
|
|
scanDepth = ScanDepth::ALL_FULL;
|
|
|
|
useHeroChain = false;
|
|
|
|
hasAnySuccess = true;
|
2024-05-06 15:33:30 +00:00
|
|
|
break;
|
2024-04-14 15:23:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
logAi->trace("Goal %s has too low priority. It is not worth doing it.", taskDescription);
|
2023-07-27 15:58:49 +03:00
|
|
|
|
2024-03-29 20:39:03 +02:00
|
|
|
continue;
|
2023-07-27 15:58:49 +03:00
|
|
|
}
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-08-29 20:49:01 +02:00
|
|
|
logAi->info("Pass %d: Performing prio %d task %s with prio: %d", i, prioOfTask, bestTask->toString(), bestTask->priority);
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-12-28 00:54:34 +01:00
|
|
|
int totalMPBefore = 0;
|
|
|
|
int totalMPAfter = 0;
|
|
|
|
for (auto hero : cb->getHeroesInfo(true))
|
|
|
|
{
|
|
|
|
totalMPBefore += hero->movementPointsRemaining();
|
|
|
|
}
|
2024-04-14 15:23:44 +03:00
|
|
|
if(!executeTask(bestTask))
|
|
|
|
{
|
|
|
|
if(hasAnySuccess)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2024-12-28 00:54:34 +01:00
|
|
|
for (auto hero : cb->getHeroesInfo(true))
|
|
|
|
{
|
|
|
|
totalMPAfter += hero->movementPointsRemaining();
|
|
|
|
}
|
|
|
|
if(totalMPBefore > totalMPAfter)
|
|
|
|
hasAnySuccess = true;
|
2021-05-16 14:19:00 +03:00
|
|
|
}
|
|
|
|
|
2024-07-19 15:21:56 +02:00
|
|
|
hasAnySuccess |= handleTrading();
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
if(!hasAnySuccess)
|
|
|
|
{
|
|
|
|
logAi->trace("Nothing was done this turn. Ending turn.");
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-09-06 22:10:14 +02:00
|
|
|
totalHeroStrength = 0;
|
|
|
|
totalTownLevel = 0;
|
2024-09-04 16:41:47 +02:00
|
|
|
for (auto heroInfo : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
totalHeroStrength += heroInfo->getTotalStrength();
|
|
|
|
}
|
|
|
|
for (auto townInfo : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
totalTownLevel += townInfo->getTownLevel();
|
|
|
|
}
|
|
|
|
logAi->info("End: Strength: %f Townlevel: %d Resources: %s", totalHeroStrength, totalTownLevel, cb->getResourceAmount().toString());
|
|
|
|
#endif
|
2024-04-14 15:23:44 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-08-05 13:49:49 +03:00
|
|
|
|
Per-hero artifact scoring for NKAI
AI now has better (but not perfect) system for scoring artifacts.
All artifacts now have their base score, based on bonuses provided by
the artifact. For composite artifacts, AI will also consider bonuses
from its components.
When exploring, AI will now weight artifacts as min(base score, price /
5), meaning even artifacts that AI can't score will still have some
priority, if only to sell them or deny them to enemy
AI will now also consider what to equip when trading with heroes or on
the end of AI pass. When considering what to equip, hero will use base
score of an artifact, adjusted by how relevant particular bonus /
artifact for a hero. Some examples:
- Morale-bonusing artifacts have their score reduced based on percentage
of undeads in the army, all the way to 0 for undead-only troops
- Artifacts that give spells (books, hat) will have score reduced based
on how many of these spells are already known by hero
- Land movement artifacts have zero score while on water and vice versa
- Necromancy artifacts will have zero score if hero does not have
necromancy
- Archery artifacts are scaled by amount of ranged troops, and only used
if hero has archery skill
AI may still equip 'bad' artifact, however this should only happen if
hero has no other artifacts to equip into said slot.
TODO's for future PR's (although not sure if / when I will work on
these):
- avoid equipping duplicates of the same artifact. Most notably with
scrolls, but may happen with other misc artifacts or rings.
- consideration for various spell-immunity neclaces
- consideration for birectional artifacts, like Shackles of War, or some
Spheres - since these artifacts need consideration on what our expected
enemy is to correctly estimate whether
- equipping artifacts based on immediate need - for example, equipping
recovery artifacts before end of day, equipping legion pieces only in
town on day 7, preparing for strong / weak enemies (or even preparing
for specific enemy in advance)
- transferring resource-generating artifacts and such to scout heroes,
allowing main hero to focus on combat
- ensure that AI can equip combined artifacts even if non-primary slots
are in use - by considering whether score of combined artifact is higher
than score of all used slots
2025-01-18 13:14:24 +00:00
|
|
|
for (auto heroInfo : cb->getHeroesInfo())
|
|
|
|
gateway->pickBestArtifacts(heroInfo);
|
|
|
|
|
2024-02-25 12:39:19 +02:00
|
|
|
if(i == settings->getMaxPass())
|
2023-08-05 13:49:49 +03:00
|
|
|
{
|
2024-04-14 15:23:44 +03:00
|
|
|
logAi->warn("Maxpass exceeded. Terminating AI turn.");
|
2023-08-05 13:49:49 +03:00
|
|
|
}
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 15:08:39 +03:00
|
|
|
|
2024-04-27 10:57:30 +03:00
|
|
|
bool Nullkiller::areAffectedObjectsPresent(Goals::TTask task) const
|
|
|
|
{
|
|
|
|
auto affectedObjs = task->getAffectedObjects();
|
|
|
|
|
|
|
|
for(auto oid : affectedObjs)
|
|
|
|
{
|
|
|
|
if(!cb->getObj(oid, false))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeroRole Nullkiller::getTaskRole(Goals::TTask task) const
|
|
|
|
{
|
|
|
|
HeroPtr hero = task->getHero();
|
|
|
|
HeroRole heroRole = HeroRole::MAIN;
|
|
|
|
|
|
|
|
if(hero.validAndSet())
|
|
|
|
heroRole = heroManager->getHeroRole(hero);
|
|
|
|
|
|
|
|
return heroRole;
|
|
|
|
}
|
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
bool Nullkiller::executeTask(Goals::TTask task)
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
2023-03-19 19:04:12 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2022-09-06 21:14:22 +03:00
|
|
|
std::string taskDescr = task->toString();
|
2021-05-15 19:23:11 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
logAi->debug("Trying to realize %s (value %2.3f)", taskDescr, task->priority);
|
2021-05-15 19:23:05 +03:00
|
|
|
|
2022-09-06 21:14:22 +03:00
|
|
|
try
|
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
task->accept(gateway);
|
2023-03-19 19:04:12 +02:00
|
|
|
logAi->trace("Task %s completed in %lld", taskDescr, timeElapsed(start));
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
|
|
|
catch(goalFulfilledException &)
|
|
|
|
{
|
2023-03-19 19:04:12 +02:00
|
|
|
logAi->trace("Task %s completed in %lld", taskDescr, timeElapsed(start));
|
2022-09-06 21:14:22 +03:00
|
|
|
}
|
2022-11-03 21:16:49 +02:00
|
|
|
catch(cannotFulfillGoalException & e)
|
2022-09-06 21:14:22 +03:00
|
|
|
{
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->error("Failed to realize subgoal of type %s, I will stop.", taskDescr);
|
|
|
|
logAi->error("The error message was: %s", e.what());
|
2022-09-06 21:14:22 +03:00
|
|
|
|
2024-04-14 15:23:44 +03:00
|
|
|
return false;
|
2021-05-15 19:23:11 +03:00
|
|
|
}
|
2024-04-14 15:23:44 +03:00
|
|
|
|
|
|
|
return true;
|
2021-05-16 15:39:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TResources Nullkiller::getFreeResources() const
|
|
|
|
{
|
|
|
|
auto freeRes = cb->getResourceAmount() - lockedResources;
|
|
|
|
|
|
|
|
freeRes.positive();
|
|
|
|
|
|
|
|
return freeRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Nullkiller::lockResources(const TResources & res)
|
|
|
|
{
|
|
|
|
lockedResources += res;
|
2021-11-23 09:41:03 +03:00
|
|
|
}
|
2022-09-26 21:01:07 +03:00
|
|
|
|
2024-07-19 15:21:56 +02:00
|
|
|
bool Nullkiller::handleTrading()
|
|
|
|
{
|
|
|
|
bool haveTraded = false;
|
|
|
|
bool shouldTryToTrade = true;
|
|
|
|
int marketId = -1;
|
|
|
|
for (auto town : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
if (town->hasBuiltSomeTradeBuilding())
|
|
|
|
{
|
|
|
|
marketId = town->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (marketId == -1)
|
|
|
|
return false;
|
|
|
|
if (const CGObjectInstance* obj = cb->getObj(ObjectInstanceID(marketId), false))
|
|
|
|
{
|
|
|
|
if (const auto* m = dynamic_cast<const IMarket*>(obj))
|
|
|
|
{
|
|
|
|
while (shouldTryToTrade)
|
|
|
|
{
|
|
|
|
shouldTryToTrade = false;
|
|
|
|
buildAnalyzer->update();
|
|
|
|
TResources required = buildAnalyzer->getTotalResourcesRequired();
|
|
|
|
TResources income = buildAnalyzer->getDailyIncome();
|
2024-08-15 18:15:29 +02:00
|
|
|
TResources available = cb->getResourceAmount();
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2024-08-15 18:15:29 +02:00
|
|
|
logAi->debug("Available %s", available.toString());
|
|
|
|
logAi->debug("Required %s", required.toString());
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-07-19 15:21:56 +02:00
|
|
|
int mostWanted = -1;
|
|
|
|
int mostExpendable = -1;
|
|
|
|
float minRatio = std::numeric_limits<float>::max();
|
|
|
|
float maxRatio = std::numeric_limits<float>::min();
|
|
|
|
|
|
|
|
for (int i = 0; i < required.size(); ++i)
|
|
|
|
{
|
2024-08-15 18:15:29 +02:00
|
|
|
if (required[i] <= 0)
|
2024-07-19 15:21:56 +02:00
|
|
|
continue;
|
|
|
|
float ratio = static_cast<float>(available[i]) / required[i];
|
|
|
|
|
|
|
|
if (ratio < minRatio) {
|
|
|
|
minRatio = ratio;
|
|
|
|
mostWanted = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < required.size(); ++i)
|
|
|
|
{
|
|
|
|
float ratio = available[i];
|
|
|
|
if (required[i] > 0)
|
|
|
|
ratio = static_cast<float>(available[i]) / required[i];
|
2024-08-15 18:15:29 +02:00
|
|
|
else
|
|
|
|
ratio = available[i];
|
2024-07-19 15:21:56 +02:00
|
|
|
|
|
|
|
bool okToSell = false;
|
|
|
|
|
2024-09-09 23:20:53 +02:00
|
|
|
if (i == GameResID::GOLD)
|
2024-07-19 15:21:56 +02:00
|
|
|
{
|
2024-08-18 09:47:37 +02:00
|
|
|
if (income[i] > 0 && !buildAnalyzer->isGoldPressureHigh())
|
2024-07-19 15:21:56 +02:00
|
|
|
okToSell = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-30 16:46:36 +02:00
|
|
|
if (required[i] <= 0 && income[i] > 0)
|
2024-07-19 15:21:56 +02:00
|
|
|
okToSell = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ratio > maxRatio && okToSell) {
|
|
|
|
maxRatio = ratio;
|
|
|
|
mostExpendable = i;
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2024-08-15 18:15:29 +02:00
|
|
|
logAi->debug("mostExpendable: %d mostWanted: %d", mostExpendable, mostWanted);
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-07-19 15:21:56 +02:00
|
|
|
if (mostExpendable == mostWanted || mostWanted == -1 || mostExpendable == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int toGive;
|
|
|
|
int toGet;
|
|
|
|
m->getOffer(mostExpendable, mostWanted, toGive, toGet, EMarketMode::RESOURCE_RESOURCE);
|
|
|
|
//logAi->info("Offer is: I get %d of %s for %d of %s at %s", toGet, mostWanted, toGive, mostExpendable, obj->getObjectName());
|
|
|
|
//TODO trade only as much as needed
|
|
|
|
if (toGive && toGive <= available[mostExpendable]) //don't try to sell 0 resources
|
|
|
|
{
|
2024-08-29 20:49:01 +02:00
|
|
|
cb->trade(m->getObjInstanceID(), EMarketMode::RESOURCE_RESOURCE, GameResID(mostExpendable), GameResID(mostWanted), toGive);
|
2024-09-04 16:41:47 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2024-08-18 09:47:37 +02:00
|
|
|
logAi->info("Traded %d of %s for %d of %s at %s", toGive, mostExpendable, toGet, mostWanted, obj->getObjectName());
|
2024-09-04 16:41:47 +02:00
|
|
|
#endif
|
2024-07-19 15:21:56 +02:00
|
|
|
haveTraded = true;
|
|
|
|
shouldTryToTrade = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return haveTraded;
|
|
|
|
}
|
|
|
|
|
2025-01-23 14:39:56 +00:00
|
|
|
std::shared_ptr<const CPathsInfo> Nullkiller::getPathsInfo(const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
return pathfinderCache->getPathsInfo(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Nullkiller::invalidatePaths()
|
|
|
|
{
|
|
|
|
pathfinderCache->invalidatePaths();
|
|
|
|
}
|
|
|
|
|
2022-09-26 21:01:07 +03:00
|
|
|
}
|