1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/lib/mapObjects/CGTownInstance.cpp

1257 lines
33 KiB
C++
Raw Normal View History

/*
* CGTownInstance.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 "CGTownInstance.h"
#include "CGTownBuilding.h"
2016-02-25 21:59:17 +02:00
#include "../spells/CSpellHandler.h"
2023-05-01 19:29:53 +02:00
#include "../bonuses/Bonus.h"
#include "../battle/IBattleInfoCallback.h"
#include "../CConfigHandler.h"
#include "../texts/CGeneralTextHandler.h"
#include "../IGameCallback.h"
#include "../gameState/CGameState.h"
#include "../mapping/CMap.h"
#include "../CPlayerState.h"
#include "../StartInfo.h"
#include "../TerrainHandler.h"
2024-07-21 12:49:40 +02:00
#include "../entities/building/CBuilding.h"
#include "../entities/faction/CTownHandler.h"
#include "../mapObjectConstructors/AObjectTypeHandler.h"
#include "../mapObjectConstructors/CObjectClassesHandler.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../modding/ModScope.h"
#include "../networkPacks/StackLocation.h"
#include "../networkPacks/PacksForClient.h"
#include "../networkPacks/PacksForClientBattle.h"
#include "../serializer/JsonSerializeFormat.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
int CGTownInstance::getSightRadius() const //returns sight distance
{
auto ret = CBuilding::HEIGHT_NO_TOWER;
for(const auto & bid : builtBuildings)
{
if(bid.IsSpecialOrGrail())
{
auto height = town->buildings.at(bid)->height;
if(ret < height)
ret = height;
}
}
return ret;
}
void CGTownInstance::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
{
///this is freakin' overcomplicated solution
switch (what)
{
2023-04-30 22:14:25 +02:00
case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, visitingHero->id);
break;
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, NumericID(0));
break;
case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, garrisonHero->id);
break;
case ObjProperty::BONUS_VALUE_FIRST:
bonusValue.first = identifier.getNum();
break;
case ObjProperty::BONUS_VALUE_SECOND:
bonusValue.second = identifier.getNum();
break;
}
}
CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
{
if (hasBuilt(BuildingID::CASTLE))
return CASTLE;
if (hasBuilt(BuildingID::CITADEL))
return CITADEL;
if (hasBuilt(BuildingID::FORT))
return FORT;
return NONE;
}
int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
{
if (hasBuilt(BuildingID::CAPITOL))
return 3;
if (hasBuilt(BuildingID::CITY_HALL))
return 2;
if (hasBuilt(BuildingID::TOWN_HALL))
return 1;
if (hasBuilt(BuildingID::VILLAGE_HALL))
return 0;
return -1;
}
2016-11-26 21:12:52 +02:00
int CGTownInstance::mageGuildLevel() const
{
if (hasBuilt(BuildingID::MAGES_GUILD_5))
return 5;
if (hasBuilt(BuildingID::MAGES_GUILD_4))
return 4;
if (hasBuilt(BuildingID::MAGES_GUILD_3))
return 3;
if (hasBuilt(BuildingID::MAGES_GUILD_2))
return 2;
if (hasBuilt(BuildingID::MAGES_GUILD_1))
return 1;
return 0;
}
int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
{
return town->hordeLvl.at(HID);
}
int CGTownInstance::creatureGrowth(const int & level) const
{
return getGrowthInfo(level).totalGrowth();
}
GrowthInfo CGTownInstance::getGrowthInfo(int level) const
{
GrowthInfo ret;
2024-08-05 23:03:19 +02:00
if (level<0 || level >=town->creatures.size())
return ret;
if (creatures[level].second.empty())
return ret; //no dwelling
const Creature *creature = creatures[level].second.back().toEntity(VLC);
2023-04-05 02:26:29 +02:00
const int base = creature->getGrowth();
int castleBonus = 0;
2024-07-25 23:04:59 +02:00
if(tempOwner.isValidPlayer())
{
auto * playerSettings = cb->getPlayerSettings(tempOwner);
2024-07-29 22:54:42 +02:00
ret.handicapPercentage = playerSettings->handicap.percentGrowth;
2024-07-25 23:04:59 +02:00
}
else
2024-07-29 22:54:42 +02:00
ret.handicapPercentage = 100;
2024-07-25 23:04:59 +02:00
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(VLC->generaltexth->allTexts[590], base); // \n\nBasic growth %d"
if (hasBuilt(BuildingID::CASTLE))
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(subID, BuildingID::CASTLE, castleBonus = base);
else if (hasBuilt(BuildingID::CITADEL))
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(subID, BuildingID::CITADEL, castleBonus = base / 2);
if(town->hordeLvl.at(0) == level)//horde 1
if(hasBuilt(BuildingID::HORDE_1))
2023-04-05 02:26:29 +02:00
ret.entries.emplace_back(subID, BuildingID::HORDE_1, creature->getHorde());
if(town->hordeLvl.at(1) == level)//horde 2
if(hasBuilt(BuildingID::HORDE_2))
2023-04-05 02:26:29 +02:00
ret.entries.emplace_back(subID, BuildingID::HORDE_2, creature->getHorde());
//statue-of-legion-like bonus: % to base+castle
TConstBonusListPtr bonuses2 = getBonuses(Selector::type()(BonusType::CREATURE_GROWTH_PERCENT));
for(const auto & b : *bonuses2)
{
const auto growth = b->val * (base + castleBonus) / 100;
if (growth)
{
ret.entries.emplace_back(growth, b->Description(growth));
}
}
//other *-of-legion-like bonuses (%d to growth cumulative with grail)
// Note: bonus uses 1-based levels (Pikeman is level 1), town list uses 0-based (Pikeman in 0-th creatures entry)
TConstBonusListPtr bonuses = getBonuses(Selector::typeSubtype(BonusType::CREATURE_GROWTH, BonusCustomSubtype::creatureLevel(level+1)));
for(const auto & b : *bonuses)
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(b->val, b->Description());
int dwellingBonus = 0;
if(const PlayerState *p = cb->getPlayerState(tempOwner, false))
{
dwellingBonus = getDwellingBonus(creatures[level].second, p->dwellings);
}
if(dwellingBonus)
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(VLC->generaltexth->allTexts[591], dwellingBonus); // \nExternal dwellings %+d
if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
2023-02-12 22:39:17 +02:00
ret.entries.emplace_back(subID, BuildingID::GRAIL, ret.totalGrowth() / 2);
return ret;
}
int CGTownInstance::getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const
{
int totalBonus = 0;
for (const auto& dwelling : dwellings)
{
for (const auto& creature : dwelling->creatures)
{
totalBonus += vstd::contains(creatureIds, creature.second[0]) ? 1 : 0;
}
}
return totalBonus;
}
TResources CGTownInstance::dailyIncome() const
{
TResources ret;
2023-02-12 22:39:17 +02:00
for(const auto & p : town->buildings)
2016-02-15 12:34:37 +02:00
{
BuildingID buildingUpgrade;
2023-02-12 22:39:17 +02:00
for(const auto & p2 : town->buildings)
2016-02-15 12:34:37 +02:00
{
if (p2.second->upgrade == p.first)
{
buildingUpgrade = p2.first;
}
}
if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
{
ret += p.second->produce;
}
}
2024-07-25 02:51:00 +02:00
auto playerSettings = cb->gameState()->scenarioOps->getIthPlayersSettings(getOwner());
for(TResources::nziterator it(ret); it.valid(); it++)
2024-07-29 22:54:42 +02:00
// always round up income - we don't want to always produce zero if handicap in use
ret[it->resType] = vstd::divideAndCeil(ret[it->resType] * playerSettings.handicap.percentIncome, 100);
return ret;
}
2021-03-23 16:47:07 +02:00
bool CGTownInstance::hasFort() const
{
return hasBuilt(BuildingID::FORT);
}
2021-03-23 16:47:07 +02:00
bool CGTownInstance::hasCapitol() const
{
return hasBuilt(BuildingID::CAPITOL);
}
2021-03-23 16:47:07 +02:00
2024-01-01 16:37:48 +02:00
CGTownInstance::CGTownInstance(IGameCallback *cb):
CGDwelling(cb),
2023-02-12 22:39:17 +02:00
town(nullptr),
built(0),
2023-02-12 22:39:17 +02:00
destroyed(0),
identifier(0),
alignmentToPlayer(PlayerColor::NEUTRAL)
{
2021-03-23 16:47:07 +02:00
this->setNodeType(CBonusSystemNode::TOWN);
}
CGTownInstance::~CGTownInstance()
{
for (auto & elem : bonusingBuildings)
delete elem;
}
int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
{
if(checkGuild && mageGuildLevel() < level)
return 0;
int ret = 6 - level; //how many spells are available at this level
if (hasBuilt(BuildingSubID::LIBRARY))
ret++;
return ret;
}
bool CGTownInstance::needsLastStack() const
{
2023-02-12 22:39:17 +02:00
return garrisonHero != nullptr;
}
2023-02-12 22:39:17 +02:00
void CGTownInstance::setOwner(const PlayerColor & player) const
2021-03-23 16:47:07 +02:00
{
removeCapitols(player);
cb->setOwner(this, player);
}
void CGTownInstance::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
2023-04-30 22:14:25 +02:00
{
for (auto building : bonusingBuildings)
building->blockingDialogAnswered(hero, answer);
}
void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
{
if(cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ) == PlayerRelations::ENEMIES)
{
if(armedGarrison() || visitingHero)
{
2021-03-23 16:47:07 +02:00
const CGHeroInstance * defendingHero = visitingHero ? visitingHero : garrisonHero;
const CArmedInstance * defendingArmy = defendingHero ? (CArmedInstance *)defendingHero : this;
const bool isBattleOutside = isBattleOutsideTown(defendingHero);
2021-03-23 16:47:07 +02:00
if(!isBattleOutside && visitingHero && defendingHero == visitingHero)
{
//we have two approaches to merge armies: mergeGarrisonOnSiege() and used in the CGameHandler::garrisonSwap(ObjectInstanceID tid)
2023-02-12 22:39:17 +02:00
auto * nodeSiege = defendingHero->whereShouldBeAttachedOnSiege(isBattleOutside);
2021-03-23 16:47:07 +02:00
if(nodeSiege == (CBonusSystemNode *)this)
cb->swapGarrisonOnSiege(this->id);
2021-03-23 16:47:07 +02:00
const_cast<CGHeroInstance *>(defendingHero)->inTownGarrison = false; //hack to return visitor from garrison after battle
}
2021-03-23 16:47:07 +02:00
cb->startBattlePrimary(h, defendingArmy, getSightCenter(), h, defendingHero, false, (isBattleOutside ? nullptr : this));
}
else
{
2021-03-23 16:47:07 +02:00
auto heroColor = h->getOwner();
onTownCaptured(heroColor);
2021-03-23 16:47:07 +02:00
if(cb->gameState()->getPlayerStatus(heroColor) == EPlayerStatus::WINNER)
{
return; //we just won game, we do not need to perform any extra actions
//TODO: check how does H3 behave, visiting town on victory can affect campaigns (spells learned, +1 stat building visited)
}
cb->heroVisitCastle(this, h);
}
}
else
{
assert(h->visitablePos() == this->visitablePos());
bool commander_recover = h->commander && !h->commander->alive;
if (commander_recover) // rise commander from dead
{
SetCommanderProperty scp;
scp.heroid = h->id;
scp.which = SetCommanderProperty::ALIVE;
scp.amount = 1;
cb->sendAndApply(&scp);
}
cb->heroVisitCastle(this, h);
// TODO(vmarkovtsev): implement payment for rising the commander
if (commander_recover) // info window about commander
{
InfoWindow iw;
iw.player = h->tempOwner;
iw.text.appendRawString(h->commander->getName());
iw.components.emplace_back(ComponentType::CREATURE, h->commander->getId(), h->commander->getCount());
cb->showInfoDialog(&iw);
}
}
}
void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
{
//FIXME: find out why this issue appears on random maps
if(visitingHero == h)
{
cb->stopHeroVisitCastle(this, h);
2023-09-28 02:51:44 +02:00
logGlobal->trace("%s correctly left town %s", h->getNameTranslated(), getNameTranslated());
}
else
2023-09-28 02:51:44 +02:00
logGlobal->warn("Warning, %s tries to leave the town %s but hero is not inside.", h->getNameTranslated(), getNameTranslated());
}
std::string CGTownInstance::getObjectName() const
{
2023-09-28 02:51:44 +02:00
return getNameTranslated() + ", " + town->faction->getNameTranslated();
}
2020-10-15 14:03:01 +02:00
bool CGTownInstance::townEnvisagesBuilding(BuildingSubID::EBuildingSubID subId) const
{
2020-10-15 14:03:01 +02:00
return town->getBuildingType(subId) != BuildingID::NONE;
}
void CGTownInstance::initOverriddenBids()
{
for(const auto & bid : builtBuildings)
{
2023-02-12 22:39:17 +02:00
const auto & overrideThem = town->buildings.at(bid)->overrideBids;
2023-02-12 22:39:17 +02:00
for(const auto & overrideIt : overrideThem)
overriddenBuildings.insert(overrideIt);
}
}
2024-08-16 14:57:38 +02:00
void CGTownInstance::initializeConfigurableBuildings(vstd::RNG & rand)
{
for(const auto & kvp : town->buildings)
{
2024-08-16 14:57:38 +02:00
if(!kvp.second->rewardableObjectInfo.getParameters().isNull())
bonusingBuildings.push_back(new CTownRewardableBuilding(this, kvp.second->bid, rand));
}
}
DamageRange CGTownInstance::getTowerDamageRange() const
{
assert(hasBuilt(BuildingID::CASTLE));
// http://heroes.thelazy.net/wiki/Arrow_tower
// base damage, irregardless of town level
2023-02-12 22:39:17 +02:00
static constexpr int baseDamage = 6;
// extra damage, for each building in town
2023-02-12 22:39:17 +02:00
static constexpr int extraDamage = 1;
const int minDamage = baseDamage + extraDamage * getTownLevel();
return {
minDamage,
minDamage * 2
};
}
DamageRange CGTownInstance::getKeepDamageRange() const
{
assert(hasBuilt(BuildingID::CITADEL));
// http://heroes.thelazy.net/wiki/Arrow_tower
// base damage, irregardless of town level
2023-02-12 22:39:17 +02:00
static constexpr int baseDamage = 10;
// extra damage, for each building in town
2023-02-12 22:39:17 +02:00
static constexpr int extraDamage = 2;
const int minDamage = baseDamage + extraDamage * getTownLevel();
return {
minDamage,
minDamage * 2
};
}
void CGTownInstance::deleteTownBonus(BuildingID bid)
{
size_t i = 0;
CGTownBuilding * freeIt = nullptr;
for(i = 0; i != bonusingBuildings.size(); i++)
{
if(bonusingBuildings[i]->getBuildingType() == bid)
{
freeIt = bonusingBuildings[i];
break;
}
}
if(freeIt == nullptr)
return;
bonusingBuildings.erase(bonusingBuildings.begin() + i);
2023-02-12 22:39:17 +02:00
delete freeIt;
}
FactionID CGTownInstance::randomizeFaction(vstd::RNG & rand)
{
if(getOwner().isValidPlayer())
return cb->gameState()->scenarioOps->getIthPlayersSettings(getOwner()).castle;
if(alignmentToPlayer.isValidPlayer())
return cb->gameState()->scenarioOps->getIthPlayersSettings(alignmentToPlayer).castle;
std::vector<FactionID> potentialPicks;
2023-11-02 17:12:58 +02:00
for (FactionID faction(0); faction < FactionID(VLC->townh->size()); ++faction)
if (VLC->factions()->getById(faction)->hasTown())
potentialPicks.push_back(faction);
assert(!potentialPicks.empty());
return *RandomGeneratorUtil::nextItem(potentialPicks, rand);
}
void CGTownInstance::pickRandomObject(vstd::RNG & rand)
{
assert(ID == MapObjectID::TOWN || ID == MapObjectID::RANDOM_TOWN);
if (ID == MapObjectID::RANDOM_TOWN)
{
ID = MapObjectID::TOWN;
subID = randomizeFaction(rand);
}
2023-10-25 20:56:00 +02:00
assert(ID == Obj::TOWN); // just in case
setType(ID, subID);
town = (*VLC->townh)[getFaction()]->town;
randomizeArmy(getFaction());
2023-10-25 20:56:00 +02:00
updateAppearance();
}
void CGTownInstance::initObj(vstd::RNG & rand) ///initialize town structures
{
blockVisit = true;
2020-10-15 14:03:01 +02:00
if(townEnvisagesBuilding(BuildingSubID::PORTAL_OF_SUMMONING)) //Dungeon for example
2024-08-05 21:15:47 +02:00
creatures.resize(town->creatures.size() + 1);
else
2024-08-05 21:15:47 +02:00
creatures.resize(town->creatures.size());
2024-08-05 21:15:47 +02:00
for (int level = 0; level < town->creatures.size(); level++)
{
2024-08-10 00:29:30 +02:00
BuildingID buildID = BuildingID(BuildingID::getDwellingFromLevel(level, 0));
int upgradeNum = 0;
2024-08-05 21:15:47 +02:00
for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(town->creatures.size()))
{
if (hasBuilt(buildID) && town->creatures.at(level).size() > upgradeNum)
creatures[level].second.push_back(town->creatures[level][upgradeNum]);
}
}
initOverriddenBids();
2024-08-16 14:57:38 +02:00
initializeConfigurableBuildings(rand);
recreateBuildingsBonuses();
updateAppearance();
}
void CGTownInstance::newTurn(vstd::RNG & rand) const
{
if (cb->getDate(Date::DAY_OF_WEEK) == 1) //reset on new week
{
//give resources if there's a Mystic Pond
if (hasBuilt(BuildingSubID::MYSTIC_POND)
2021-02-15 14:03:32 +02:00
&& cb->getDate(Date::DAY) != 1
&& (tempOwner.isValidPlayer())
)
{
int resID = rand.nextInt(2, 5); //bonus to random rare resource
resID = (resID==2)?1:resID;
int resVal = rand.nextInt(1, 4);//with size 1..4
2023-04-05 02:26:29 +02:00
cb->giveResource(tempOwner, static_cast<EGameResID>(resID), resVal);
cb->setObjPropertyValue(id, ObjProperty::BONUS_VALUE_FIRST, resID);
cb->setObjPropertyValue(id, ObjProperty::BONUS_VALUE_SECOND, resVal);
}
2023-05-04 13:03:18 +02:00
if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
{
std::vector<SlotID> nativeCrits; //slots
for(const auto & elem : Slots())
{
if (elem.second->type->getFaction() == getFaction()) //native
{
nativeCrits.push_back(elem.first); //collect matching slots
}
}
if(!nativeCrits.empty())
{
SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
StackLocation sl(this, pos);
const CCreature *c = getCreature(pos);
if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
{
cb->changeStackCount(sl, c->getGrowth());
}
else //upgrade
{
cb->changeStackType(sl, c->upgrades.begin()->toCreature());
}
}
if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
{
2024-08-05 23:03:19 +02:00
int i = rand.nextInt(std::min((int)town->creatures.size(), cb->getDate(Date::MONTH) << 1) - 1);
if (!town->creatures[i].empty())
{
CreatureID c = town->creatures[i][0];
SlotID n;
TQuantity count = creatureGrowth(i);
if (!count) // no dwelling
2023-11-02 17:48:48 +02:00
count = VLC->creatures()->getById(c)->getGrowth();
{//no lower tiers or above current month
if ((n = getSlotFor(c)).validSlot())
{
StackLocation sl(this, n);
if (slotEmpty(n))
cb->insertNewStack(sl, c.toCreature(), count);
else //add to existing
cb->changeStackCount(sl, count);
}
}
}
}
}
}
2024-08-16 14:57:38 +02:00
for(const auto * rewardableBuilding : bonusingBuildings)
rewardableBuilding->newTurn(rand);
2024-08-04 17:52:40 +02:00
if(hasBuilt(BuildingSubID::BANK) && bonusValue.second > 0)
{
TResources res;
res[EGameResID::GOLD] = -500;
cb->giveResources(getOwner(), res);
cb->setObjPropertyValue(id, ObjProperty::BONUS_VALUE_SECOND, bonusValue.second - 500);
}
}
/*
int3 CGTownInstance::getSightCenter() const
{
return pos - int3(2,0,0);
}
*/
bool CGTownInstance::passableFor(PlayerColor color) const
{
if (!armedGarrison())//empty castle - anyone can visit
return true;
if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
return false;
return cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES;
}
void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
{
offsets = {int3(-1,2,0), int3(+1,2,0)};
}
2023-06-07 00:55:21 +02:00
CGTownInstance::EGeneratorState CGTownInstance::shipyardStatus() const
{
if (!hasBuilt(BuildingID::SHIPYARD))
return EGeneratorState::UNKNOWN;
return IShipyard::shipyardStatus();
}
const IObjectInterface * CGTownInstance::getObject() const
{
return this;
}
void CGTownInstance::mergeGarrisonOnSiege() const
{
auto getWeakestStackSlot = [&](ui64 powerLimit)
{
std::vector<SlotID> weakSlots;
auto stacksList = visitingHero->stacks;
std::pair<SlotID, CStackInstance *> pair;
2023-02-12 22:39:17 +02:00
while(!stacksList.empty())
{
2023-02-12 22:39:17 +02:00
pair = *vstd::minElementByFun(stacksList, [&](const std::pair<SlotID, CStackInstance *> & elem) { return elem.second->getPower(); });
if(powerLimit > pair.second->getPower() &&
(weakSlots.empty() || pair.second->getPower() == visitingHero->getStack(weakSlots.front()).getPower()))
{
weakSlots.push_back(pair.first);
stacksList.erase(pair.first);
}
else
break;
}
2023-02-12 22:39:17 +02:00
if(!weakSlots.empty())
return *std::max_element(weakSlots.begin(), weakSlots.end());
return SlotID();
};
auto count = static_cast<int>(stacks.size());
for(int i = 0; i < count; i++)
{
2023-02-12 22:39:17 +02:00
auto pair = *vstd::maxElementByFun(stacks, [&](const std::pair<SlotID, CStackInstance *> & elem)
{
ui64 power = elem.second->getPower();
auto dst = visitingHero->getSlotFor(elem.second->getCreatureID());
if(dst.validSlot() && visitingHero->hasStackAtSlot(dst))
power += visitingHero->getStack(dst).getPower();
return power;
});
auto dst = visitingHero->getSlotFor(pair.second->getCreatureID());
if(dst.validSlot())
cb->moveStack(StackLocation(this, pair.first), StackLocation(visitingHero, dst), -1);
else
{
dst = getWeakestStackSlot(static_cast<int>(pair.second->getPower()));
if(dst.validSlot())
cb->swapStacks(StackLocation(this, pair.first), StackLocation(visitingHero, dst));
}
}
}
2023-02-12 22:39:17 +02:00
void CGTownInstance::removeCapitols(const PlayerColor & owner) const
{
if (hasCapitol()) // search if there's an older capitol
{
PlayerState* state = cb->gameState()->getPlayerState(owner); //get all towns owned by player
for (auto i = state->towns.cbegin(); i < state->towns.cend(); ++i)
{
if (*i != this && (*i)->hasCapitol())
{
RazeStructures rs;
rs.tid = id;
rs.bid.insert(BuildingID::CAPITOL);
rs.destroyed = destroyed;
cb->sendAndApply(&rs);
return;
}
}
}
}
void CGTownInstance::clearArmy() const
{
while(!stacks.empty())
{
cb->eraseStack(StackLocation(this, stacks.begin()->first));
}
}
2023-04-20 19:20:51 +02:00
BoatId CGTownInstance::getBoatType() const
{
return town->faction->boatType;
}
int CGTownInstance::getMarketEfficiency() const
{
2020-10-15 14:03:01 +02:00
if(!hasBuiltSomeTradeBuilding())
return 0;
const PlayerState *p = cb->getPlayerState(tempOwner);
assert(p);
int marketCount = 0;
for(const CGTownInstance *t : p->towns)
2020-10-15 14:03:01 +02:00
if(t->hasBuiltSomeTradeBuilding())
marketCount++;
return marketCount;
}
bool CGTownInstance::allowsTrade(EMarketMode mode) const
{
switch(mode)
{
case EMarketMode::RESOURCE_RESOURCE:
case EMarketMode::RESOURCE_PLAYER:
return hasBuilt(BuildingID::MARKETPLACE);
case EMarketMode::ARTIFACT_RESOURCE:
case EMarketMode::RESOURCE_ARTIFACT:
2020-10-15 14:03:01 +02:00
return hasBuilt(BuildingSubID::ARTIFACT_MERCHANT);
case EMarketMode::CREATURE_RESOURCE:
2020-10-15 14:03:01 +02:00
return hasBuilt(BuildingSubID::FREELANCERS_GUILD);
case EMarketMode::CREATURE_UNDEAD:
2020-10-15 14:03:01 +02:00
return hasBuilt(BuildingSubID::CREATURE_TRANSFORMER);
case EMarketMode::RESOURCE_SKILL:
2020-10-15 14:03:01 +02:00
return hasBuilt(BuildingSubID::MAGIC_UNIVERSITY);
2024-04-10 00:18:04 +02:00
case EMarketMode::CREATURE_EXP:
case EMarketMode::ARTIFACT_EXP:
return false;
default:
assert(0);
return false;
}
}
std::vector<TradeItemBuy> CGTownInstance::availableItemsIds(EMarketMode mode) const
{
if(mode == EMarketMode::RESOURCE_ARTIFACT)
{
std::vector<TradeItemBuy> ret;
for(const CArtifact *a : cb->gameState()->map->townMerchantArtifacts)
if(a)
ret.push_back(a->getId());
else
ret.push_back(ArtifactID{});
return ret;
}
else if ( mode == EMarketMode::RESOURCE_SKILL )
{
return cb->gameState()->map->townUniversitySkills;
}
else
return IMarket::availableItemsIds(mode);
}
void CGTownInstance::updateAppearance()
{
auto terrain = cb->gameState()->getTile(visitablePos())->terType->getId();
//FIXME: not the best way to do this
auto app = getObjectHandler()->getOverride(terrain, this);
if (app)
appearance = app;
}
std::string CGTownInstance::nodeName() const
{
2023-09-28 02:51:44 +02:00
return "Town (" + (town ? town->faction->getNameTranslated() : "unknown") + ") of " + getNameTranslated();
}
void CGTownInstance::deserializationFix()
{
attachTo(townAndVis);
//Hero is already handled by CGameState::attachArmedObjects
// if(visitingHero)
// visitingHero->attachTo(&townAndVis);
// if(garrisonHero)
// garrisonHero->attachTo(this);
}
void CGTownInstance::updateMoraleBonusFromArmy()
{
auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
if(!b)
{
b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
addNewBonus(b);
}
if (garrisonHero)
{
b->val = 0;
CBonusSystemNode::treeHasChanged();
}
else
CArmedInstance::updateMoraleBonusFromArmy();
}
void CGTownInstance::recreateBuildingsBonuses()
{
BonusList bl;
getExportedBonusList().getBonuses(bl, Selector::sourceType()(BonusSource::TOWN_STRUCTURE));
2023-02-12 22:39:17 +02:00
for(const auto & b : bl)
removeBonus(b);
for(const auto & bid : builtBuildings)
{
if(vstd::contains(overriddenBuildings, bid)) //tricky! -> checks tavern only if no bratherhood of sword
continue;
2020-12-10 03:05:37 +02:00
auto building = town->buildings.at(bid);
if(building->buildingBonuses.empty())
continue;
2021-03-23 16:47:07 +02:00
for(auto & bonus : building->buildingBonuses)
addNewBonus(bonus);
}
}
void CGTownInstance::setVisitingHero(CGHeroInstance *h)
{
if(visitingHero.get() == h)
return;
if(h)
{
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
assert(p);
h->detachFrom(*p);
h->attachTo(townAndVis);
visitingHero = h;
h->visitedTown = this;
h->inTownGarrison = false;
}
else
{
PlayerState *p = cb->gameState()->getPlayerState(visitingHero->tempOwner);
visitingHero->visitedTown = nullptr;
visitingHero->detachFrom(townAndVis);
visitingHero->attachTo(*p);
visitingHero = nullptr;
}
}
void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
{
if(garrisonHero.get() == h)
return;
if(h)
{
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
assert(p);
h->detachFrom(*p);
h->attachTo(*this);
garrisonHero = h;
h->visitedTown = this;
h->inTownGarrison = true;
}
else
{
PlayerState *p = cb->gameState()->getPlayerState(garrisonHero->tempOwner);
garrisonHero->visitedTown = nullptr;
garrisonHero->inTownGarrison = false;
garrisonHero->detachFrom(*this);
garrisonHero->attachTo(*p);
garrisonHero = nullptr;
}
updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
}
bool CGTownInstance::armedGarrison() const
{
return !stacks.empty() || garrisonHero;
}
const CTown * CGTownInstance::getTown() const
{
if(ID == Obj::RANDOM_TOWN)
return VLC->townh->randomTown;
else
{
if(nullptr == town)
{
return (*VLC->townh)[getFaction()]->town;
}
else
return town;
}
}
int CGTownInstance::getTownLevel() const
{
// count all buildings that are not upgrades
int level = 0;
2021-02-20 03:57:50 +02:00
for(const auto & bid : builtBuildings)
{
if(town->buildings.at(bid)->upgrade == BuildingID::NONE)
level++;
2021-02-20 03:57:50 +02:00
}
return level;
}
CBonusSystemNode & CGTownInstance::whatShouldBeAttached()
{
return townAndVis;
}
std::string CGTownInstance::getNameTranslated() const
{
2023-09-27 23:57:05 +02:00
return VLC->generaltexth->translate(nameTextId);
}
2023-11-26 18:53:34 +02:00
std::string CGTownInstance::getNameTextID() const
{
return nameTextId;
}
2023-09-27 23:57:05 +02:00
void CGTownInstance::setNameTextId( const std::string & newName )
{
2023-09-27 23:57:05 +02:00
nameTextId = newName;
}
const CArmedInstance * CGTownInstance::getUpperArmy() const
{
if(garrisonHero)
return garrisonHero;
return this;
}
2020-10-15 14:03:01 +02:00
bool CGTownInstance::hasBuiltSomeTradeBuilding() const
{
2021-03-23 16:47:07 +02:00
for(const auto & bid : builtBuildings)
2020-10-15 14:03:01 +02:00
{
if(town->buildings.at(bid)->IsTradeBuilding())
return true;
}
return false;
}
bool CGTownInstance::hasBuilt(BuildingSubID::EBuildingSubID buildingID) const
{
for(const auto & bid : builtBuildings)
{
if(town->buildings.at(bid)->subId == buildingID)
return true;
}
return false;
}
2023-02-12 22:39:17 +02:00
bool CGTownInstance::hasBuilt(const BuildingID & buildingID) const
{
return vstd::contains(builtBuildings, buildingID);
}
2023-11-02 17:12:58 +02:00
bool CGTownInstance::hasBuilt(const BuildingID & buildingID, FactionID townID) const
{
2023-11-02 17:12:58 +02:00
if (townID == town->faction->getId() || townID == FactionID::ANY)
return hasBuilt(buildingID);
return false;
}
2023-02-12 22:39:17 +02:00
TResources CGTownInstance::getBuildingCost(const BuildingID & buildingID) const
{
2018-07-26 12:06:55 +02:00
if (vstd::contains(town->buildings, buildingID))
return town->buildings.at(buildingID)->resources;
else
{
2023-09-28 02:51:44 +02:00
logGlobal->error("Town %s at %s has no possible building %d!", getNameTranslated(), pos.toString(), buildingID.toEnum());
2018-07-26 12:06:55 +02:00
return TResources();
}
}
2023-02-12 22:39:17 +02:00
CBuilding::TRequired CGTownInstance::genBuildingRequirements(const BuildingID & buildID, bool deep) const
{
const CBuilding * building = town->buildings.at(buildID);
//TODO: find better solution to prevent infinite loops
std::set<BuildingID> processed;
std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
[&](const BuildingID & id) -> CBuilding::TRequired::Variant
{
if (town->buildings.count(id) == 0)
{
logMod->error("Invalid building ID %d in building dependencies!", id.getNum());
return CBuilding::TRequired::OperatorAll();
}
const CBuilding * build = town->buildings.at(id);
CBuilding::TRequired::OperatorAll requirements;
if (!hasBuilt(id))
{
if (deep)
2023-02-12 22:39:17 +02:00
requirements.expressions.emplace_back(id);
else
return id;
}
if(!vstd::contains(processed, id))
{
processed.insert(id);
if (build->upgrade != BuildingID::NONE)
requirements.expressions.push_back(dependTest(build->upgrade));
requirements.expressions.push_back(build->requirements.morph(dependTest));
}
return requirements;
};
CBuilding::TRequired::OperatorAll requirements;
if (building->upgrade != BuildingID::NONE)
{
const CBuilding * upgr = town->buildings.at(building->upgrade);
requirements.expressions.push_back(dependTest(upgr->bid));
processed.clear();
}
requirements.expressions.push_back(building->requirements.morph(dependTest));
CBuilding::TRequired::Variant variant(requirements);
CBuilding::TRequired ret(variant);
ret.minimize();
return ret;
}
void CGTownInstance::addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID ) const
{
if(visitingHero == h)
cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
else if(garrisonHero == h)
cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
else
{
//should never ever happen
logGlobal->error("Cannot add hero %s to visitors of structure # %d", h->getNameTranslated(), structureInstanceID);
2023-11-22 19:11:40 +02:00
throw std::runtime_error("unexpected hero in CGTownInstance::addHeroToStructureVisitors");
}
}
2021-03-23 16:47:07 +02:00
void CGTownInstance::battleFinished(const CGHeroInstance * hero, const BattleResult & result) const
{
2021-03-23 16:47:07 +02:00
if(result.winner == BattleSide::ATTACKER)
{
clearArmy();
onTownCaptured(hero->getOwner());
}
}
2023-02-12 22:39:17 +02:00
void CGTownInstance::onTownCaptured(const PlayerColor & winner) const
{
setOwner(winner);
cb->changeFogOfWar(getSightCenter(), getSightRadius(), winner, ETileVisibility::REVEALED);
}
void CGTownInstance::afterAddToMap(CMap * map)
{
2023-11-01 16:10:33 +02:00
map->towns.emplace_back(this);
}
2022-09-17 13:04:01 +02:00
void CGTownInstance::afterRemoveFromMap(CMap * map)
{
2023-11-01 16:10:33 +02:00
vstd::erase_if_present(map->towns, this);
2022-09-17 13:04:01 +02:00
}
2016-02-23 15:36:21 +02:00
void CGTownInstance::serializeJsonOptions(JsonSerializeFormat & handler)
{
CGObjectInstance::serializeJsonOwner(handler);
2023-09-17 22:19:45 +02:00
if(!handler.saving)
handler.serializeEnum("tightFormation", formation, NArmyFormation::names); //for old format
CArmedInstance::serializeJsonOptions(handler);
2023-09-27 23:57:05 +02:00
handler.serializeString("name", nameTextId);
2016-02-23 15:36:21 +02:00
{
auto decodeBuilding = [this](const std::string & identifier) -> si32
{
auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), getTown()->getBuildingScope(), identifier);
2016-02-23 15:36:21 +02:00
if(rawId)
2023-04-16 19:42:56 +02:00
return rawId.value();
else
return -1;
};
2016-02-23 15:36:21 +02:00
auto encodeBuilding = [this](si32 index) -> std::string
{
return getTown()->buildings.at(BuildingID(index))->getJsonKey();
};
const std::set<si32> standard = getTown()->getAllBuildings();//by default all buildings are allowed
JsonSerializeFormat::LICSet buildingsLIC(standard, decodeBuilding, encodeBuilding);
if(handler.saving)
{
bool customBuildings = false;
boost::logic::tribool hasFort(false);
for(const BuildingID & id : forbiddenBuildings)
{
buildingsLIC.none.insert(id.getNum());
customBuildings = true;
}
for(const BuildingID & id : builtBuildings)
{
if(id == BuildingID::DEFAULT)
continue;
2016-02-23 15:36:21 +02:00
const CBuilding * building = getTown()->buildings.at(id);
if(building->mode == CBuilding::BUILD_AUTO)
continue;
if(id == BuildingID::FORT)
hasFort = true;
buildingsLIC.all.insert(id.getNum());
customBuildings = true;
}
if(customBuildings)
handler.serializeLIC("buildings", buildingsLIC);
else
handler.serializeBool("hasFort",hasFort);
}
else
{
handler.serializeLIC("buildings", buildingsLIC);
builtBuildings.insert(BuildingID::VILLAGE_HALL);
if(buildingsLIC.none.empty() && buildingsLIC.all.empty())
{
builtBuildings.insert(BuildingID::DEFAULT);
bool hasFort = false;
handler.serializeBool("hasFort",hasFort);
if(hasFort)
builtBuildings.insert(BuildingID::FORT);
}
else
{
for(const si32 item : buildingsLIC.none)
forbiddenBuildings.insert(BuildingID(item));
for(const si32 item : buildingsLIC.all)
builtBuildings.insert(BuildingID(item));
}
}
}
2016-02-25 21:59:17 +02:00
{
handler.serializeIdArray( "possibleSpells", possibleSpells);
handler.serializeIdArray( "obligatorySpells", obligatorySpells);
2016-02-25 21:59:17 +02:00
}
{
auto eventsHandler = handler.enterArray("events");
eventsHandler.syncSize(events, JsonNode::JsonType::DATA_VECTOR);
eventsHandler.serializeStruct(events);
2016-02-25 21:59:17 +02:00
}
}
FactionID CGTownInstance::getFaction() const
{
2023-11-08 19:49:42 +02:00
return FactionID(subID.getNum());
}
TerrainId CGTownInstance::getNativeTerrain() const
{
return town->faction->getNativeTerrain();
}
GrowthInfo::Entry::Entry(const std::string &format, int _count)
: count(_count)
{
MetaString formatter;
formatter.appendRawString(format);
formatter.replacePositiveNumber(count);
description = formatter.toString();
}
2023-02-12 22:39:17 +02:00
GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count)
{
MetaString formatter;
formatter.appendRawString("%s %+d");
formatter.replaceRawString((*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated());
formatter.replacePositiveNumber(count);
description = formatter.toString();
}
2023-02-12 22:39:17 +02:00
GrowthInfo::Entry::Entry(int _count, std::string fullDescription):
count(_count),
description(std::move(fullDescription))
{
}
CTownAndVisitingHero::CTownAndVisitingHero()
{
setNodeType(TOWN_AND_VISITOR);
}
int GrowthInfo::totalGrowth() const
{
int ret = 0;
for(const Entry &entry : entries)
ret += entry.count;
2024-07-29 22:54:42 +02:00
// always round up income - we don't want buildings to always produce zero if handicap in use
return vstd::divideAndCeil(ret * handicapPercentage, 100);
}
2020-10-25 00:04:34 +02:00
void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
{
for(const CGTownInstance::TCreaturesSet::value_type & dwelling : creatures)
{
if (vstd::contains(dwelling.second, stack.type->getId())) //Dwelling with our creature
{
for(const auto & upgrID : dwelling.second)
{
if(vstd::contains(stack.type->upgrades, upgrID)) //possible upgrade
{
info.newID.push_back(upgrID);
info.cost.push_back(upgrID.toCreature()->getFullRecruitCost() - stack.type->getFullRecruitCost());
}
}
}
}
}
VCMI_LIB_NAMESPACE_END