2021-05-15 19:59:43 +02:00
|
|
|
/*
|
|
|
|
* AINodeStorage.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 "Actors.h"
|
2019-03-17 23:27:09 +02:00
|
|
|
#include "../VCAI.h"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-15 19:59:43 +02:00
|
|
|
#include "../../../CCallback.h"
|
|
|
|
#include "../../../lib/mapping/CMap.h"
|
|
|
|
#include "../../../lib/mapObjects/MapObjects.h"
|
2021-05-16 13:57:26 +02:00
|
|
|
#include "Actions/BuyArmyAction.h"
|
2021-05-15 19:59:43 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
CCreatureSet emptyArmy;
|
|
|
|
|
2021-05-15 20:56:08 +02:00
|
|
|
bool HeroExchangeArmy::needsLastStack() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
std::shared_ptr<SpecialAction> HeroExchangeArmy::getActorAction() const
|
|
|
|
{
|
|
|
|
std::shared_ptr<SpecialAction> result;
|
|
|
|
|
|
|
|
if(requireBuyArmy)
|
|
|
|
{
|
|
|
|
result.reset(new AIPathfinding::BuyArmyAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
ChainActor::ChainActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask)
|
|
|
|
:hero(hero), heroRole(heroRole), isMovable(true), chainMask(chainMask), creatureSet(hero),
|
2021-05-16 13:57:26 +02:00
|
|
|
baseActor(this), carrierParent(nullptr), otherParent(nullptr), actorExchangeCount(1), armyCost(), actorAction()
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
|
|
|
initialPosition = hero->visitablePos();
|
|
|
|
layer = hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND;
|
|
|
|
initialMovement = hero->movement;
|
|
|
|
initialTurn = 0;
|
2019-03-17 23:27:09 +02:00
|
|
|
armyValue = hero->getArmyStrength();
|
2021-05-15 20:54:58 +02:00
|
|
|
heroFightingStrength = hero->getFightingStrength();
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ChainActor::ChainActor(const ChainActor * carrier, const ChainActor * other, const CCreatureSet * heroArmy)
|
2021-05-16 13:56:13 +02:00
|
|
|
:hero(carrier->hero), heroRole(carrier->heroRole), isMovable(true), creatureSet(heroArmy), chainMask(carrier->chainMask | other->chainMask),
|
2021-05-15 20:54:58 +02:00
|
|
|
baseActor(this), carrierParent(carrier), otherParent(other), heroFightingStrength(carrier->heroFightingStrength),
|
2021-05-16 13:57:26 +02:00
|
|
|
actorExchangeCount(carrier->actorExchangeCount + other->actorExchangeCount), armyCost(carrier->armyCost + other->armyCost), actorAction()
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
armyValue = heroArmy->getArmyStrength();
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
ChainActor::ChainActor(const CGObjectInstance * obj, const CCreatureSet * creatureSet, uint64_t chainMask, int initialTurn)
|
2021-05-16 13:56:13 +02:00
|
|
|
:hero(nullptr), heroRole(HeroRole::MAIN), isMovable(false), creatureSet(creatureSet), chainMask(chainMask),
|
2021-05-15 20:54:58 +02:00
|
|
|
baseActor(this), carrierParent(nullptr), otherParent(nullptr), initialTurn(initialTurn), initialMovement(0),
|
2021-05-16 13:57:26 +02:00
|
|
|
heroFightingStrength(0), actorExchangeCount(1), armyCost(), actorAction()
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
initialPosition = obj->visitablePos();
|
|
|
|
layer = EPathfindingLayer::LAND;
|
|
|
|
armyValue = creatureSet->getArmyStrength();
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:54:28 +02:00
|
|
|
std::string ChainActor::toString() const
|
|
|
|
{
|
|
|
|
return hero->name;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
ObjectActor::ObjectActor(const CGObjectInstance * obj, const CCreatureSet * army, uint64_t chainMask, int initialTurn)
|
|
|
|
:ChainActor(obj, army, chainMask, initialTurn), object(obj)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGObjectInstance * ObjectActor::getActorObject() const
|
|
|
|
{
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ObjectActor::toString() const
|
|
|
|
{
|
|
|
|
return object->getObjectName() + " at " + object->visitablePos().toString();
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
HeroActor::HeroActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask, const Nullkiller * ai)
|
|
|
|
:ChainActor(hero, heroRole, chainMask)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2021-05-16 13:59:32 +02:00
|
|
|
exchangeMap.reset(new HeroExchangeMap(this, ai));
|
2021-05-15 19:59:43 +02:00
|
|
|
setupSpecialActors();
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
HeroActor::HeroActor(
|
|
|
|
const ChainActor * carrier,
|
|
|
|
const ChainActor * other,
|
2021-05-16 13:57:26 +02:00
|
|
|
const HeroExchangeArmy * army,
|
2020-05-04 17:58:43 +02:00
|
|
|
const Nullkiller * ai)
|
2019-03-17 23:27:09 +02:00
|
|
|
:ChainActor(carrier, other, army)
|
|
|
|
{
|
2021-05-16 13:59:32 +02:00
|
|
|
exchangeMap.reset(new HeroExchangeMap(this, ai));
|
2021-05-16 13:57:26 +02:00
|
|
|
armyCost += army->armyCost;
|
|
|
|
actorAction = army->getActorAction();
|
2019-03-17 23:27:09 +02:00
|
|
|
setupSpecialActors();
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChainActor::setBaseActor(HeroActor * base)
|
|
|
|
{
|
|
|
|
baseActor = base;
|
|
|
|
hero = base->hero;
|
2021-05-16 13:56:13 +02:00
|
|
|
heroRole = base->heroRole;
|
2021-05-15 19:59:43 +02:00
|
|
|
layer = base->layer;
|
|
|
|
initialMovement = base->initialMovement;
|
|
|
|
initialTurn = base->initialTurn;
|
|
|
|
armyValue = base->armyValue;
|
|
|
|
chainMask = base->chainMask;
|
|
|
|
creatureSet = base->creatureSet;
|
2021-05-15 20:04:48 +02:00
|
|
|
isMovable = base->isMovable;
|
2021-05-15 20:54:58 +02:00
|
|
|
heroFightingStrength = base->heroFightingStrength;
|
2021-05-15 20:55:29 +02:00
|
|
|
armyCost = base->armyCost;
|
2021-05-16 13:57:26 +02:00
|
|
|
actorAction = base->actorAction;
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeroActor::setupSpecialActors()
|
|
|
|
{
|
|
|
|
auto allActors = std::vector<ChainActor *>{ this };
|
|
|
|
|
2021-05-15 20:04:48 +02:00
|
|
|
for(ChainActor & specialActor : specialActors)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
|
|
|
specialActor.setBaseActor(this);
|
|
|
|
allActors.push_back(&specialActor);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i <= SPECIAL_ACTORS_COUNT; i++)
|
|
|
|
{
|
|
|
|
ChainActor * actor = allActors[i];
|
|
|
|
|
2021-05-15 20:04:48 +02:00
|
|
|
actor->allowBattle = (i & 1) > 0;
|
|
|
|
actor->allowSpellCast = (i & 2) > 0;
|
|
|
|
actor->allowUseResources = (i & 4) > 0;
|
2021-05-15 19:59:43 +02:00
|
|
|
actor->battleActor = allActors[i | 1];
|
|
|
|
actor->castActor = allActors[i | 2];
|
|
|
|
actor->resourceActor = allActors[i | 4];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
ChainActor * ChainActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
return baseActor->exchange(specialActor, other);
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChainActor::canExchange(const ChainActor * other) const
|
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
return isMovable && baseActor->canExchange(other);
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
template <class M, class Key, class F>
|
|
|
|
typename M::mapped_type & getOrCompute(M &m, Key const& k, F f)
|
|
|
|
{
|
|
|
|
typedef typename M::mapped_type V;
|
|
|
|
|
|
|
|
std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
|
|
|
|
V &v = r.first->second;
|
|
|
|
|
|
|
|
if(r.second)
|
|
|
|
f(v);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
bool HeroActor::canExchange(const ChainActor * other) const
|
|
|
|
{
|
|
|
|
return exchangeMap->canExchange(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeroExchangeMap::canExchange(const ChainActor * other)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
|
|
|
return vstd::getOrCompute(canExchangeCache, other, [&](bool & result) {
|
2019-03-17 23:27:09 +02:00
|
|
|
result = (actor->chainMask & other->chainMask) == 0;
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
TResources resources = ai->cb->getResourceAmount();
|
2021-05-15 20:55:29 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
if(!resources.canAfford(actor->armyCost + other->armyCost))
|
|
|
|
{
|
|
|
|
result = false;
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-16 13:19:00 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Can not afford exchange because of total cost %s but we have %s",
|
|
|
|
(actor->armyCost + other->armyCost).toString(),
|
|
|
|
resources.toString());
|
|
|
|
#endif
|
|
|
|
return;
|
2021-05-15 20:55:29 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:59:35 +02:00
|
|
|
TResources availableResources = resources - actor->armyCost - other->armyCost;
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
auto upgradeInfo = ai->armyManager->calculateCreateresUpgrade(
|
2021-05-16 13:19:00 +02:00
|
|
|
actor->creatureSet,
|
|
|
|
other->getActorObject(),
|
2021-05-16 13:59:35 +02:00
|
|
|
availableResources);
|
2021-05-16 13:19:00 +02:00
|
|
|
|
|
|
|
uint64_t reinforcment = upgradeInfo.upgradeValue;
|
|
|
|
|
|
|
|
if(other->creatureSet->Slots().size())
|
2021-05-16 13:56:42 +02:00
|
|
|
reinforcment += ai->armyManager->howManyReinforcementsCanGet(actor->hero, actor->creatureSet, other->creatureSet);
|
2021-05-16 13:57:26 +02:00
|
|
|
|
|
|
|
auto obj = other->getActorObject();
|
|
|
|
if(obj && obj->ID == Obj::TOWN)
|
|
|
|
{
|
2021-05-16 13:59:35 +02:00
|
|
|
reinforcment += ai->armyManager->howManyReinforcementsCanBuy(
|
|
|
|
actor->creatureSet,
|
|
|
|
ai->cb->getTown(obj->id),
|
|
|
|
availableResources - upgradeInfo.upgradeCost);
|
2021-05-16 13:57:26 +02:00
|
|
|
}
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
#if PATHFINDER_TRACE_LEVEL >= 2
|
2021-05-16 13:19:00 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Exchange %s->%s reinforcement: %d, %f%%",
|
|
|
|
actor->toString(),
|
|
|
|
other->toString(),
|
|
|
|
reinforcment,
|
|
|
|
100.0f * reinforcment / actor->armyValue);
|
|
|
|
#endif
|
2019-03-17 23:27:09 +02:00
|
|
|
|
|
|
|
result = reinforcment > actor->armyValue / 10 || reinforcment > 1000;
|
|
|
|
}
|
2021-05-15 19:59:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
ChainActor * HeroActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
const ChainActor * otherBase = other->baseActor;
|
|
|
|
HeroActor * result = exchangeMap->exchange(otherBase);
|
2021-05-15 19:59:43 +02:00
|
|
|
|
|
|
|
if(specialActor == this)
|
|
|
|
return result;
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
int index = vstd::find_pos_if(specialActors, [specialActor](const ChainActor & actor) -> bool
|
|
|
|
{
|
2021-05-15 19:59:43 +02:00
|
|
|
return &actor == specialActor;
|
|
|
|
});
|
|
|
|
|
|
|
|
return &result->specialActors[index];
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:59:32 +02:00
|
|
|
HeroExchangeMap::HeroExchangeMap(const HeroActor * actor, const Nullkiller * ai)
|
|
|
|
:actor(actor), ai(ai)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:56:08 +02:00
|
|
|
HeroExchangeMap::~HeroExchangeMap()
|
|
|
|
{
|
|
|
|
for(auto & exchange : exchangeMap)
|
|
|
|
{
|
|
|
|
delete exchange.second->creatureSet;
|
|
|
|
delete exchange.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangeMap.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
HeroActor * HeroExchangeMap::exchange(const ChainActor * other)
|
|
|
|
{
|
|
|
|
HeroActor * result;
|
|
|
|
|
|
|
|
if(vstd::contains(exchangeMap, other))
|
|
|
|
result = exchangeMap.at(other);
|
|
|
|
else
|
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
TResources availableResources = ai->cb->getResourceAmount() - actor->armyCost - other->armyCost;
|
2021-05-16 13:57:26 +02:00
|
|
|
HeroExchangeArmy * upgradedInitialArmy = tryUpgrade(actor->creatureSet, other->getActorObject(), availableResources);
|
|
|
|
HeroExchangeArmy * newArmy;
|
2021-05-16 13:19:00 +02:00
|
|
|
|
|
|
|
if(other->creatureSet->Slots().size())
|
|
|
|
{
|
|
|
|
if(upgradedInitialArmy)
|
|
|
|
{
|
|
|
|
newArmy = pickBestCreatures(upgradedInitialArmy, other->creatureSet);
|
2021-05-16 13:57:26 +02:00
|
|
|
newArmy->armyCost = upgradedInitialArmy->armyCost;
|
2021-05-16 14:00:24 +02:00
|
|
|
newArmy->requireBuyArmy = upgradedInitialArmy->requireBuyArmy;
|
2021-05-16 13:57:26 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
delete upgradedInitialArmy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newArmy = pickBestCreatures(actor->creatureSet, other->creatureSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newArmy = upgradedInitialArmy;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
result = new HeroActor(actor, other, newArmy, ai);
|
2021-05-16 13:57:26 +02:00
|
|
|
result->armyCost += newArmy->armyCost;
|
2019-03-17 23:27:09 +02:00
|
|
|
exchangeMap[other] = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
HeroExchangeArmy * HeroExchangeMap::tryUpgrade(
|
|
|
|
const CCreatureSet * army,
|
|
|
|
const CGObjectInstance * upgrader,
|
|
|
|
TResources resources) const
|
2021-05-16 13:19:00 +02:00
|
|
|
{
|
2021-05-16 13:57:26 +02:00
|
|
|
HeroExchangeArmy * target = new HeroExchangeArmy();
|
2020-05-04 17:58:43 +02:00
|
|
|
auto upgradeInfo = ai->armyManager->calculateCreateresUpgrade(army, upgrader, resources);
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
if(upgradeInfo.upgradeValue)
|
|
|
|
{
|
|
|
|
for(auto & slotInfo : upgradeInfo.resultingArmy)
|
|
|
|
{
|
|
|
|
auto targetSlot = target->getFreeSlot();
|
|
|
|
|
|
|
|
target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
|
|
|
|
}
|
|
|
|
|
|
|
|
resources -= upgradeInfo.upgradeCost;
|
|
|
|
target->armyCost += upgradeInfo.upgradeCost;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(auto slot : army->Slots())
|
|
|
|
{
|
|
|
|
auto targetSlot = target->getSlotFor(slot.second->getCreatureID());
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
target->addToSlot(targetSlot, slot.second->getCreatureID(), slot.second->count);
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
if(upgrader->ID == Obj::TOWN)
|
2021-05-16 13:19:00 +02:00
|
|
|
{
|
2021-05-16 13:57:26 +02:00
|
|
|
auto buyArmy = ai->armyManager->getArmyAvailableToBuy(target, ai->cb->getTown(upgrader->id), resources);
|
|
|
|
|
|
|
|
for(auto creatureToBuy : buyArmy)
|
|
|
|
{
|
|
|
|
auto targetSlot = target->getSlotFor(creatureToBuy.cre);
|
2021-05-16 13:19:00 +02:00
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
target->addToSlot(targetSlot, creatureToBuy.creID, creatureToBuy.count);
|
|
|
|
target->armyCost += creatureToBuy.cre->cost * creatureToBuy.count;
|
|
|
|
target->requireBuyArmy = true;
|
|
|
|
}
|
2021-05-16 13:19:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
if(target->getArmyStrength() <= army->getArmyStrength())
|
2021-05-16 13:59:32 +02:00
|
|
|
{
|
|
|
|
delete target;
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
return nullptr;
|
2021-05-16 13:59:32 +02:00
|
|
|
}
|
2021-05-16 13:57:26 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:57:26 +02:00
|
|
|
HeroExchangeArmy * HeroExchangeMap::pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2021-05-16 13:57:26 +02:00
|
|
|
HeroExchangeArmy * target = new HeroExchangeArmy();
|
2021-05-16 13:56:42 +02:00
|
|
|
auto bestArmy = ai->armyManager->getBestArmy(actor->hero, army1, army2);
|
2021-05-15 19:59:43 +02:00
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
for(auto & slotInfo : bestArmy)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
auto targetSlot = target->getFreeSlot();
|
|
|
|
|
|
|
|
target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
return target;
|
|
|
|
}
|
2021-05-15 19:59:43 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
HillFortActor::HillFortActor(const CGObjectInstance * hillFort, uint64_t chainMask)
|
|
|
|
:ObjectActor(hillFort, &emptyArmy, chainMask, 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek)
|
2021-05-16 13:19:00 +02:00
|
|
|
:ObjectActor(
|
2019-03-17 23:27:09 +02:00
|
|
|
dwelling,
|
|
|
|
getDwellingCreatures(dwelling, waitForGrowth),
|
|
|
|
chainMask,
|
2021-05-15 20:54:28 +02:00
|
|
|
getInitialTurn(waitForGrowth, dayOfWeek)),
|
|
|
|
dwelling(dwelling)
|
2019-03-17 23:27:09 +02:00
|
|
|
{
|
2021-05-15 20:55:29 +02:00
|
|
|
for(auto & slot : creatureSet->Slots())
|
|
|
|
{
|
|
|
|
armyCost += slot.second->getCreatureID().toCreature()->cost * slot.second->count;
|
|
|
|
}
|
2019-03-17 23:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DwellingActor::~DwellingActor()
|
|
|
|
{
|
|
|
|
delete creatureSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DwellingActor::getInitialTurn(bool waitForGrowth, int dayOfWeek)
|
|
|
|
{
|
|
|
|
if(!waitForGrowth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 8 - dayOfWeek;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:54:28 +02:00
|
|
|
std::string DwellingActor::toString() const
|
|
|
|
{
|
|
|
|
return dwelling->typeName + dwelling->visitablePos().toString();
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth)
|
|
|
|
{
|
|
|
|
CCreatureSet * dwellingCreatures = new CCreatureSet();
|
|
|
|
|
|
|
|
for(auto & creatureInfo : dwelling->creatures)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
if(!creatureInfo.second.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto creature = creatureInfo.second.back().toCreature();
|
|
|
|
auto count = creatureInfo.first;
|
|
|
|
|
|
|
|
if(waitForGrowth)
|
2021-05-15 19:59:43 +02:00
|
|
|
{
|
2019-03-17 23:27:09 +02:00
|
|
|
const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(dwelling);
|
|
|
|
|
|
|
|
count += town ? town->creatureGrowth(creature->level) : creature->growth;
|
|
|
|
}
|
2021-05-15 19:59:43 +02:00
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
dwellingCreatures->addToSlot(
|
|
|
|
dwellingCreatures->getSlotFor(creature),
|
|
|
|
creature->idNumber,
|
|
|
|
TQuantity(creatureInfo.first));
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:27:09 +02:00
|
|
|
return dwellingCreatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
TownGarrisonActor::TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask)
|
2021-05-16 13:19:00 +02:00
|
|
|
:ObjectActor(town, town->getUpperArmy(), chainMask, 0), town(town)
|
2021-05-15 20:54:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TownGarrisonActor::toString() const
|
2019-03-17 23:27:09 +02:00
|
|
|
{
|
2021-05-15 20:54:28 +02:00
|
|
|
return town->name;
|
2021-05-15 19:59:43 +02:00
|
|
|
}
|