2021-05-15 20:36:50 +02:00
|
|
|
/*
|
|
|
|
* BuildingManager.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 "ArmyManager.h"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-16 13:55:57 +02:00
|
|
|
#include "../../../CCallback.h"
|
|
|
|
#include "../../../lib/mapObjects/MapObjects.h"
|
2023-07-09 08:56:58 +02:00
|
|
|
#include "../../../lib/GameConstants.h"
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
2021-05-16 13:39:20 +02:00
|
|
|
class StackUpgradeInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CreatureID initialCreature;
|
|
|
|
CreatureID upgradedCreature;
|
|
|
|
TResources cost;
|
|
|
|
int count;
|
|
|
|
uint64_t upgradeValue;
|
|
|
|
|
|
|
|
StackUpgradeInfo(CreatureID initial, CreatureID upgraded, int count)
|
|
|
|
:initialCreature(initial), upgradedCreature(upgraded), count(count)
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
cost = (upgradedCreature.toCreature()->getFullRecruitCost() - initialCreature.toCreature()->getFullRecruitCost()) * count;
|
|
|
|
upgradeValue = (upgradedCreature.toCreature()->getAIValue() - initialCreature.toCreature()->getAIValue()) * count;
|
2021-05-16 13:39:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-11 18:21:50 +02:00
|
|
|
void ArmyUpgradeInfo::addArmyToBuy(std::vector<SlotInfo> army)
|
|
|
|
{
|
|
|
|
for(auto slot : army)
|
|
|
|
{
|
|
|
|
resultingArmy.push_back(slot);
|
|
|
|
|
|
|
|
upgradeValue += slot.power;
|
|
|
|
upgradeCost += slot.creature->getFullRecruitCost() * slot.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmyUpgradeInfo::addArmyToGet(std::vector<SlotInfo> army)
|
|
|
|
{
|
|
|
|
for(auto slot : army)
|
|
|
|
{
|
|
|
|
resultingArmy.push_back(slot);
|
|
|
|
|
|
|
|
upgradeValue += slot.power;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SlotInfo> ArmyManager::toSlotInfo(std::vector<creInfo> army) const
|
|
|
|
{
|
|
|
|
std::vector<SlotInfo> result;
|
|
|
|
|
|
|
|
for(auto i : army)
|
|
|
|
{
|
|
|
|
SlotInfo slot;
|
|
|
|
|
|
|
|
slot.creature = VLC->creh->objects[i.cre->getId()];
|
|
|
|
slot.count = i.count;
|
|
|
|
slot.power = evaluateStackPower(i.cre, i.count);
|
|
|
|
|
|
|
|
result.push_back(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
uint64_t ArmyManager::howManyReinforcementsCanGet(const CGHeroInstance * hero, const CCreatureSet * source) const
|
|
|
|
{
|
|
|
|
return howManyReinforcementsCanGet(hero, hero, source);
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:36:50 +02:00
|
|
|
std::vector<SlotInfo> ArmyManager::getSortedSlots(const CCreatureSet * target, const CCreatureSet * source) const
|
|
|
|
{
|
|
|
|
const CCreatureSet * armies[] = { target, source };
|
|
|
|
|
|
|
|
//we calculate total strength for each creature type available in armies
|
|
|
|
std::map<const CCreature *, SlotInfo> creToPower;
|
|
|
|
std::vector<SlotInfo> resultingArmy;
|
|
|
|
|
|
|
|
for(auto armyPtr : armies)
|
|
|
|
{
|
|
|
|
for(auto & i : armyPtr->Slots())
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
auto cre = dynamic_cast<const CCreature*>(i.second->type);
|
|
|
|
auto & slotInfp = creToPower[cre];
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
slotInfp.creature = cre;
|
2021-05-15 20:36:50 +02:00
|
|
|
slotInfp.power += i.second->getPower();
|
|
|
|
slotInfp.count += i.second->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 11:42:44 +02:00
|
|
|
for(auto & pair : creToPower)
|
2021-05-15 20:36:50 +02:00
|
|
|
resultingArmy.push_back(pair.second);
|
|
|
|
|
2019-04-07 06:31:47 +02:00
|
|
|
boost::sort(resultingArmy, [](const SlotInfo & left, const SlotInfo & right) -> bool
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
|
|
|
return left.power > right.power;
|
|
|
|
});
|
|
|
|
|
|
|
|
return resultingArmy;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SlotInfo>::iterator ArmyManager::getWeakestCreature(std::vector<SlotInfo> & army) const
|
|
|
|
{
|
2019-04-07 06:31:47 +02:00
|
|
|
auto weakest = boost::min_element(army, [](const SlotInfo & left, const SlotInfo & right) -> bool
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
if(left.creature->getLevel() != right.creature->getLevel())
|
|
|
|
return left.creature->getLevel() < right.creature->getLevel();
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
return left.creature->speed() > right.creature->speed();
|
2021-05-15 20:36:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return weakest;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:53 +02:00
|
|
|
class TemporaryArmy : public CArmedInstance
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void armyChanged() override {}
|
2021-03-01 21:38:50 +02:00
|
|
|
TemporaryArmy()
|
2021-05-23 13:28:43 +02:00
|
|
|
:CArmedInstance(true)
|
2021-03-01 21:38:50 +02:00
|
|
|
{
|
|
|
|
}
|
2021-05-16 13:56:53 +02:00
|
|
|
};
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
std::vector<SlotInfo> ArmyManager::getBestArmy(const IBonusBearer * armyCarrier, const CCreatureSet * target, const CCreatureSet * source) const
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
auto sortedSlots = getSortedSlots(target, source);
|
2023-04-09 17:26:32 +02:00
|
|
|
std::map<FactionID, uint64_t> alignmentMap;
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
for(auto & slot : sortedSlots)
|
|
|
|
{
|
2023-04-09 17:26:32 +02:00
|
|
|
alignmentMap[slot.creature->getFaction()] += slot.power;
|
2021-05-16 13:56:42 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 17:26:32 +02:00
|
|
|
std::set<FactionID> allowedFactions;
|
2021-05-16 13:56:42 +02:00
|
|
|
std::vector<SlotInfo> resultingArmy;
|
|
|
|
uint64_t armyValue = 0;
|
|
|
|
|
2021-05-16 13:56:53 +02:00
|
|
|
TemporaryArmy newArmyInstance;
|
2023-05-01 00:20:01 +02:00
|
|
|
auto bonusModifiers = armyCarrier->getBonuses(Selector::type()(BonusType::MORALE));
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
for(auto bonus : *bonusModifiers)
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
// army bonuses will change and object bonuses are temporary
|
2023-05-01 00:20:01 +02:00
|
|
|
if(bonus->source != BonusSource::ARMY && bonus->source != BonusSource::OBJECT)
|
2021-05-16 13:56:42 +02:00
|
|
|
{
|
2021-05-16 14:09:44 +02:00
|
|
|
newArmyInstance.addNewBonus(std::make_shared<Bonus>(*bonus));
|
2021-05-16 13:56:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(allowedFactions.size() < alignmentMap.size())
|
|
|
|
{
|
2023-04-09 17:26:32 +02:00
|
|
|
auto strongestAlignment = vstd::maxElementByFun(alignmentMap, [&](std::pair<FactionID, uint64_t> pair) -> uint64_t
|
2021-05-16 13:56:42 +02:00
|
|
|
{
|
|
|
|
return vstd::contains(allowedFactions, pair.first) ? 0 : pair.second;
|
|
|
|
});
|
|
|
|
|
|
|
|
allowedFactions.insert(strongestAlignment->first);
|
|
|
|
|
|
|
|
std::vector<SlotInfo> newArmy;
|
|
|
|
uint64_t newValue = 0;
|
2023-08-07 18:13:02 +02:00
|
|
|
newArmyInstance.clearSlots();
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
for(auto & slot : sortedSlots)
|
|
|
|
{
|
2023-04-09 17:26:32 +02:00
|
|
|
if(vstd::contains(allowedFactions, slot.creature->getFaction()))
|
2021-05-16 13:56:42 +02:00
|
|
|
{
|
2023-06-11 18:21:50 +02:00
|
|
|
auto slotID = newArmyInstance.getSlotFor(slot.creature->getId());
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
if(slotID.validSlot())
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
newArmyInstance.setCreature(slotID, slot.creature->getId(), slot.count);
|
2021-05-16 13:56:42 +02:00
|
|
|
newArmy.push_back(slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:53 +02:00
|
|
|
newArmyInstance.updateMoraleBonusFromArmy();
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
for(auto & slot : newArmyInstance.Slots())
|
|
|
|
{
|
2023-05-02 00:05:59 +02:00
|
|
|
auto morale = slot.second->moraleVal();
|
2021-05-16 13:56:42 +02:00
|
|
|
auto multiplier = 1.0f;
|
|
|
|
|
2021-05-16 19:53:11 +02:00
|
|
|
const float BadMoraleChance = 0.083f;
|
|
|
|
const float HighMoraleChance = 0.04f;
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
if(morale < 0)
|
|
|
|
{
|
2021-05-16 19:53:11 +02:00
|
|
|
multiplier += morale * BadMoraleChance;
|
2021-05-16 13:56:42 +02:00
|
|
|
}
|
2021-05-16 13:56:53 +02:00
|
|
|
else if(morale > 0)
|
|
|
|
{
|
2021-05-16 19:53:11 +02:00
|
|
|
multiplier += morale * HighMoraleChance;
|
2021-05-16 13:56:53 +02:00
|
|
|
}
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
newValue += multiplier * slot.second->getPower();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(armyValue >= newValue)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
resultingArmy = newArmy;
|
|
|
|
armyValue = newValue;
|
2021-05-15 20:36:50 +02:00
|
|
|
}
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
if(resultingArmy.size() <= GameConstants::ARMY_SIZE
|
|
|
|
&& allowedFactions.size() == alignmentMap.size()
|
|
|
|
&& source->needsLastStack())
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
|
|
|
auto weakest = getWeakestCreature(resultingArmy);
|
|
|
|
|
2023-03-11 13:54:05 +02:00
|
|
|
if(weakest->count == 1)
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2023-08-23 17:46:30 +02:00
|
|
|
if (resultingArmy.size() == 1)
|
|
|
|
logAi->warn("Unexpected resulting army size!");
|
2023-03-11 13:54:05 +02:00
|
|
|
|
2021-05-15 20:36:50 +02:00
|
|
|
resultingArmy.erase(weakest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
weakest->power -= weakest->power / weakest->count;
|
|
|
|
weakest->count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultingArmy;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui64 ArmyManager::howManyReinforcementsCanBuy(const CCreatureSet * h, const CGDwelling * t) const
|
2021-05-16 13:59:35 +02:00
|
|
|
{
|
2021-05-16 14:39:38 +02:00
|
|
|
return howManyReinforcementsCanBuy(h, t, ai->getFreeResources());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CCreatureSet> ArmyManager::getArmyAvailableToBuyAsCCreatureSet(
|
|
|
|
const CGDwelling * dwelling,
|
|
|
|
TResources availableRes) const
|
|
|
|
{
|
|
|
|
std::vector<creInfo> creaturesInDwellings;
|
|
|
|
auto army = std::make_shared<TemporaryArmy>();
|
|
|
|
|
|
|
|
for(int i = dwelling->creatures.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto ci = infoFromDC(dwelling->creatures[i]);
|
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if(!ci.count || ci.creID == CreatureID::NONE)
|
2021-05-16 14:39:38 +02:00
|
|
|
continue;
|
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
vstd::amin(ci.count, availableRes / ci.cre->getFullRecruitCost()); //max count we can afford
|
2021-05-16 14:39:38 +02:00
|
|
|
|
|
|
|
if(!ci.count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SlotID dst = army->getFreeSlot();
|
|
|
|
|
|
|
|
if(!dst.validSlot())
|
|
|
|
break;
|
|
|
|
|
|
|
|
army->setCreature(dst, ci.creID, ci.count);
|
2023-04-05 02:26:29 +02:00
|
|
|
availableRes -= ci.cre->getFullRecruitCost() * ci.count;
|
2021-05-16 14:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return army;
|
2021-05-16 13:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui64 ArmyManager::howManyReinforcementsCanBuy(
|
2021-05-16 14:39:38 +02:00
|
|
|
const CCreatureSet * targetArmy,
|
|
|
|
const CGDwelling * dwelling,
|
2023-06-04 15:02:02 +02:00
|
|
|
const TResources & availableResources,
|
|
|
|
uint8_t turn) const
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
|
|
|
ui64 aivalue = 0;
|
2021-05-16 14:39:38 +02:00
|
|
|
auto army = getArmyAvailableToBuy(targetArmy, dwelling, availableResources);
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2021-05-16 13:13:48 +02:00
|
|
|
for(const creInfo & ci : army)
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
aivalue += ci.count * ci.cre->getAIValue();
|
2021-05-16 13:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return aivalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<creInfo> ArmyManager::getArmyAvailableToBuy(const CCreatureSet * hero, const CGDwelling * dwelling) const
|
|
|
|
{
|
2021-05-16 14:39:38 +02:00
|
|
|
return getArmyAvailableToBuy(hero, dwelling, ai->getFreeResources());
|
2021-05-16 13:57:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
std::vector<creInfo> ArmyManager::getArmyAvailableToBuy(
|
|
|
|
const CCreatureSet * hero,
|
|
|
|
const CGDwelling * dwelling,
|
2023-06-04 15:02:02 +02:00
|
|
|
TResources availableRes,
|
|
|
|
uint8_t turn) const
|
2021-05-16 13:57:26 +02:00
|
|
|
{
|
2021-05-16 13:13:48 +02:00
|
|
|
std::vector<creInfo> creaturesInDwellings;
|
|
|
|
int freeHeroSlots = GameConstants::ARMY_SIZE - hero->stacksCount();
|
2023-06-04 15:02:02 +02:00
|
|
|
bool countGrowth = (cb->getDate(Date::DAY_OF_WEEK) + turn) > 7;
|
|
|
|
|
2023-07-09 08:56:58 +02:00
|
|
|
const CGTownInstance * town = dwelling->ID == Obj::TOWN
|
2023-06-04 15:02:02 +02:00
|
|
|
? dynamic_cast<const CGTownInstance *>(dwelling)
|
|
|
|
: nullptr;
|
2021-05-16 13:13:48 +02:00
|
|
|
|
|
|
|
for(int i = dwelling->creatures.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto ci = infoFromDC(dwelling->creatures[i]);
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2023-08-19 19:48:28 +02:00
|
|
|
if(ci.creID == CreatureID::NONE) continue;
|
2023-06-04 15:02:02 +02:00
|
|
|
|
|
|
|
if(i < GameConstants::CREATURES_PER_TOWN && countGrowth)
|
|
|
|
{
|
|
|
|
ci.count += town ? town->creatureGrowth(i) : ci.cre->getGrowth();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ci.count) continue;
|
2021-05-15 20:36:50 +02:00
|
|
|
|
2021-05-16 13:13:48 +02:00
|
|
|
SlotID dst = hero->getSlotFor(ci.creID);
|
|
|
|
if(!hero->hasStackAtSlot(dst)) //need another new slot for this stack
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2021-05-16 13:13:48 +02:00
|
|
|
if(!freeHeroSlots) //no more place for stacks
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
freeHeroSlots--; //new slot will be occupied
|
2021-05-15 20:36:50 +02:00
|
|
|
}
|
2021-05-16 13:13:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
vstd::amin(ci.count, availableRes / ci.cre->getFullRecruitCost()); //max count we can afford
|
2021-05-16 13:13:48 +02:00
|
|
|
|
2023-06-04 15:02:02 +02:00
|
|
|
if(!ci.count) continue;
|
2021-05-16 13:13:48 +02:00
|
|
|
|
|
|
|
ci.level = i; //this is important for Dungeon Summoning Portal
|
|
|
|
creaturesInDwellings.push_back(ci);
|
2023-04-05 02:26:29 +02:00
|
|
|
availableRes -= ci.cre->getFullRecruitCost() * ci.count;
|
2021-05-15 20:36:50 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:48 +02:00
|
|
|
return creaturesInDwellings;
|
2021-05-15 20:36:50 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
ui64 ArmyManager::howManyReinforcementsCanGet(const IBonusBearer * armyCarrier, const CCreatureSet * target, const CCreatureSet * source) const
|
2021-05-15 20:36:50 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
auto bestArmy = getBestArmy(armyCarrier, target, source);
|
2021-05-15 20:36:50 +02:00
|
|
|
uint64_t newArmy = 0;
|
|
|
|
uint64_t oldArmy = target->getArmyStrength();
|
|
|
|
|
|
|
|
for(auto & slot : bestArmy)
|
|
|
|
{
|
|
|
|
newArmy += slot.power;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArmy > oldArmy ? newArmy - oldArmy : 0;
|
|
|
|
}
|
2021-05-16 13:15:03 +02:00
|
|
|
|
2023-06-11 18:21:50 +02:00
|
|
|
uint64_t ArmyManager::evaluateStackPower(const Creature * creature, int count) const
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
return creature->getAIValue() * count;
|
2021-05-16 13:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SlotInfo ArmyManager::getTotalCreaturesAvailable(CreatureID creatureID) const
|
|
|
|
{
|
|
|
|
auto creatureInfo = totalArmy.find(creatureID);
|
|
|
|
|
|
|
|
return creatureInfo == totalArmy.end() ? SlotInfo() : creatureInfo->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmyManager::update()
|
|
|
|
{
|
|
|
|
logAi->trace("Start analysing army");
|
|
|
|
|
|
|
|
std::vector<const CCreatureSet *> total;
|
|
|
|
auto heroes = cb->getHeroesInfo();
|
|
|
|
auto towns = cb->getTownsInfo();
|
|
|
|
|
|
|
|
std::copy(heroes.begin(), heroes.end(), std::back_inserter(total));
|
|
|
|
std::copy(towns.begin(), towns.end(), std::back_inserter(total));
|
|
|
|
|
|
|
|
totalArmy.clear();
|
|
|
|
|
|
|
|
for(auto army : total)
|
|
|
|
{
|
|
|
|
for(auto slot : army->Slots())
|
|
|
|
{
|
|
|
|
totalArmy[slot.second->getCreatureID()].count += slot.second->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 04:33:08 +02:00
|
|
|
for(auto & army : totalArmy)
|
2021-05-16 13:15:03 +02:00
|
|
|
{
|
|
|
|
army.second.creature = army.first.toCreature();
|
|
|
|
army.second.power = evaluateStackPower(army.second.creature, army.second.count);
|
|
|
|
}
|
2021-05-16 13:15:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SlotInfo> ArmyManager::convertToSlots(const CCreatureSet * army) const
|
|
|
|
{
|
|
|
|
std::vector<SlotInfo> result;
|
|
|
|
|
|
|
|
for(auto slot : army->Slots())
|
|
|
|
{
|
|
|
|
SlotInfo slotInfo;
|
|
|
|
|
|
|
|
slotInfo.creature = slot.second->getCreatureID().toCreature();
|
|
|
|
slotInfo.count = slot.second->count;
|
|
|
|
slotInfo.power = evaluateStackPower(slotInfo.creature, slotInfo.count);
|
|
|
|
|
|
|
|
result.push_back(slotInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> ArmyManager::getHillFortUpgrades(const CCreatureSet * army) const
|
2021-05-16 13:15:12 +02:00
|
|
|
{
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> upgrades;
|
2021-05-16 13:15:12 +02:00
|
|
|
|
|
|
|
for(auto creature : army->Slots())
|
|
|
|
{
|
|
|
|
CreatureID initial = creature.second->getCreatureID();
|
|
|
|
auto possibleUpgrades = initial.toCreature()->upgrades;
|
|
|
|
|
|
|
|
if(possibleUpgrades.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CreatureID strongestUpgrade = *vstd::minElementByFun(possibleUpgrades, [](CreatureID cre) -> uint64_t
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
return cre.toCreature()->getAIValue();
|
2021-05-16 13:15:12 +02:00
|
|
|
});
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
StackUpgradeInfo upgrade = StackUpgradeInfo(initial, strongestUpgrade, creature.second->count);
|
2021-05-16 13:15:12 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
if(initial.toCreature()->getLevel() == 1)
|
2021-05-16 13:15:12 +02:00
|
|
|
upgrade.cost = TResources();
|
|
|
|
|
|
|
|
upgrades.push_back(upgrade);
|
|
|
|
}
|
|
|
|
|
|
|
|
return upgrades;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> ArmyManager::getDwellingUpgrades(const CCreatureSet * army, const CGDwelling * dwelling) const
|
2021-05-16 13:15:12 +02:00
|
|
|
{
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> upgrades;
|
2021-05-16 13:15:12 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
for(auto creature : army->Slots())
|
|
|
|
{
|
|
|
|
CreatureID initial = creature.second->getCreatureID();
|
|
|
|
auto possibleUpgrades = initial.toCreature()->upgrades;
|
|
|
|
|
|
|
|
vstd::erase_if(possibleUpgrades, [&](CreatureID creID) -> bool
|
|
|
|
{
|
|
|
|
for(auto pair : dwelling->creatures)
|
|
|
|
{
|
|
|
|
if(vstd::contains(pair.second, creID))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(possibleUpgrades.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CreatureID strongestUpgrade = *vstd::minElementByFun(possibleUpgrades, [](CreatureID cre) -> uint64_t
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
return cre.toCreature()->getAIValue();
|
2021-05-16 13:19:00 +02:00
|
|
|
});
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
StackUpgradeInfo upgrade = StackUpgradeInfo(initial, strongestUpgrade, creature.second->count);
|
2021-05-16 13:19:00 +02:00
|
|
|
|
|
|
|
upgrades.push_back(upgrade);
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:15:12 +02:00
|
|
|
return upgrades;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> ArmyManager::getPossibleUpgrades(const CCreatureSet * army, const CGObjectInstance * upgrader) const
|
2021-05-16 13:15:12 +02:00
|
|
|
{
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> upgrades;
|
2021-05-16 13:15:12 +02:00
|
|
|
|
|
|
|
if(upgrader->ID == Obj::HILL_FORT)
|
|
|
|
{
|
|
|
|
upgrades = getHillFortUpgrades(army);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto dwelling = dynamic_cast<const CGDwelling *>(upgrader);
|
|
|
|
|
|
|
|
if(dwelling)
|
|
|
|
{
|
|
|
|
upgrades = getDwellingUpgrades(army, dwelling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return upgrades;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:53:11 +02:00
|
|
|
ArmyUpgradeInfo ArmyManager::calculateCreaturesUpgrade(
|
2021-05-16 13:15:12 +02:00
|
|
|
const CCreatureSet * army,
|
|
|
|
const CGObjectInstance * upgrader,
|
|
|
|
const TResources & availableResources) const
|
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
if(!upgrader)
|
|
|
|
return ArmyUpgradeInfo();
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
std::vector<StackUpgradeInfo> upgrades = getPossibleUpgrades(army, upgrader);
|
2021-05-16 13:15:12 +02:00
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
vstd::erase_if(upgrades, [&](const StackUpgradeInfo & u) -> bool
|
2021-05-16 13:15:12 +02:00
|
|
|
{
|
|
|
|
return !availableResources.canAfford(u.cost);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(upgrades.empty())
|
|
|
|
return ArmyUpgradeInfo();
|
|
|
|
|
2021-05-16 13:39:20 +02:00
|
|
|
std::sort(upgrades.begin(), upgrades.end(), [](const StackUpgradeInfo & u1, const StackUpgradeInfo & u2) -> bool
|
2021-05-16 13:15:12 +02:00
|
|
|
{
|
|
|
|
return u1.upgradeValue > u2.upgradeValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
TResources resourcesLeft = availableResources;
|
|
|
|
ArmyUpgradeInfo result;
|
|
|
|
|
|
|
|
result.resultingArmy = convertToSlots(army);
|
|
|
|
|
|
|
|
for(auto upgrade : upgrades)
|
|
|
|
{
|
|
|
|
if(resourcesLeft.canAfford(upgrade.cost))
|
|
|
|
{
|
|
|
|
SlotInfo upgradedArmy;
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
upgradedArmy.creature = upgrade.upgradedCreature.toCreature();
|
2021-05-16 13:15:12 +02:00
|
|
|
upgradedArmy.count = upgrade.count;
|
|
|
|
upgradedArmy.power = evaluateStackPower(upgradedArmy.creature, upgradedArmy.count);
|
|
|
|
|
|
|
|
auto slotToReplace = std::find_if(result.resultingArmy.begin(), result.resultingArmy.end(), [&](const SlotInfo & slot) -> bool {
|
2023-04-05 02:26:29 +02:00
|
|
|
return slot.count == upgradedArmy.count && slot.creature->getId() == upgrade.initialCreature;
|
2021-05-16 13:15:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
resourcesLeft -= upgrade.cost;
|
|
|
|
result.upgradeCost += upgrade.cost;
|
|
|
|
result.upgradeValue += upgrade.upgradeValue;
|
|
|
|
|
|
|
|
*slotToReplace = upgradedArmy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-09-22 10:49:55 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|