2015-12-02 21:05:10 +02:00
|
|
|
/*
|
2014-06-05 20:26:50 +03:00
|
|
|
* CGTownInstance.cpp, part of VCMI engine
|
2014-06-05 19:52:14 +03:00
|
|
|
*
|
|
|
|
* 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"
|
2024-08-16 16:49:42 +02:00
|
|
|
|
|
|
|
#include "TownBuildingInstance.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"
|
2023-03-23 17:49:33 +02:00
|
|
|
#include "../battle/IBattleInfoCallback.h"
|
2024-08-31 23:04:32 +02:00
|
|
|
#include "../battle/BattleLayout.h"
|
2023-01-14 16:55:08 +02:00
|
|
|
#include "../CConfigHandler.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../IGameCallback.h"
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../gameState/CGameState.h"
|
2017-05-28 15:23:42 +02:00
|
|
|
#include "../mapping/CMap.h"
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "../CPlayerState.h"
|
2023-10-25 12:50:11 +02:00
|
|
|
#include "../StartInfo.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../TerrainHandler.h"
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../entities/building/CBuilding.h"
|
|
|
|
#include "../entities/faction/CTownHandler.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../mapObjectConstructors/CObjectClassesHandler.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/ModScope.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../networkPacks/StackLocation.h"
|
|
|
|
#include "../networkPacks/PacksForClient.h"
|
|
|
|
#include "../networkPacks/PacksForClientBattle.h"
|
2016-02-22 01:37:19 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2016-01-31 17:01:58 +02:00
|
|
|
int CGTownInstance::getSightRadius() const //returns sight distance
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2020-10-02 23:55:46 +02:00
|
|
|
auto ret = CBuilding::HEIGHT_NO_TOWER;
|
|
|
|
|
|
|
|
for(const auto & bid : builtBuildings)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
auto height = getTown()->buildings.at(bid)->height;
|
2024-08-26 23:25:39 +02:00
|
|
|
if(ret < height)
|
|
|
|
ret = height;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2020-10-02 23:55:46 +02:00
|
|
|
return ret;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-11-06 18:27:16 +02:00
|
|
|
void CGTownInstance::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
///this is freakin' overcomplicated solution
|
|
|
|
switch (what)
|
|
|
|
{
|
2023-04-30 22:14:25 +02:00
|
|
|
case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
|
2024-08-16 16:49:42 +02:00
|
|
|
rewardableBuildings.at(identifier.getNum())->setProperty(ObjProperty::VISITORS, visitingHero->id);
|
2014-06-05 19:52:14 +03:00
|
|
|
break;
|
|
|
|
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
|
2024-08-16 16:49:42 +02:00
|
|
|
rewardableBuildings.at(identifier.getNum())->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, NumericID(0));
|
2014-06-05 19:52:14 +03:00
|
|
|
break;
|
|
|
|
case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
|
2024-08-16 16:49:42 +02:00
|
|
|
rewardableBuildings.at(identifier.getNum())->setProperty(ObjProperty::VISITORS, garrisonHero->id);
|
2014-06-05 19:52:14 +03:00
|
|
|
break;
|
|
|
|
case ObjProperty::BONUS_VALUE_FIRST:
|
2023-11-06 18:27:16 +02:00
|
|
|
bonusValue.first = identifier.getNum();
|
2014-06-05 19:52:14 +03:00
|
|
|
break;
|
|
|
|
case ObjProperty::BONUS_VALUE_SECOND:
|
2023-11-06 18:27:16 +02:00
|
|
|
bonusValue.second = identifier.getNum();
|
2014-06-05 19:52:14 +03:00
|
|
|
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
|
|
|
|
2014-06-05 19:52:14 +03: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
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return getTown()->hordeLvl.at(HID);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CGTownInstance::creatureGrowth(const int & level) const
|
|
|
|
{
|
|
|
|
return getGrowthInfo(level).totalGrowth();
|
|
|
|
}
|
|
|
|
|
|
|
|
GrowthInfo CGTownInstance::getGrowthInfo(int level) const
|
|
|
|
{
|
|
|
|
GrowthInfo ret;
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if (level<0 || level >=getTown()->creatures.size())
|
2014-06-05 19:52:14 +03:00
|
|
|
return ret;
|
|
|
|
if (creatures[level].second.empty())
|
|
|
|
return ret; //no dwelling
|
|
|
|
|
2023-11-05 18:58:07 +02:00
|
|
|
const Creature *creature = creatures[level].second.back().toEntity(VLC);
|
2023-04-05 02:26:29 +02:00
|
|
|
const int base = creature->getGrowth();
|
2014-06-05 19:52:14 +03:00
|
|
|
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"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
if (hasBuilt(BuildingID::CASTLE))
|
2023-02-12 22:39:17 +02:00
|
|
|
ret.entries.emplace_back(subID, BuildingID::CASTLE, castleBonus = base);
|
2014-06-05 19:52:14 +03:00
|
|
|
else if (hasBuilt(BuildingID::CITADEL))
|
2023-02-12 22:39:17 +02:00
|
|
|
ret.entries.emplace_back(subID, BuildingID::CITADEL, castleBonus = base / 2);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if(getTown()->hordeLvl.at(0) == level)//horde 1
|
2014-06-05 19:52:14 +03:00
|
|
|
if(hasBuilt(BuildingID::HORDE_1))
|
2023-04-05 02:26:29 +02:00
|
|
|
ret.entries.emplace_back(subID, BuildingID::HORDE_1, creature->getHorde());
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if(getTown()->hordeLvl.at(1) == level)//horde 2
|
2014-06-05 19:52:14 +03:00
|
|
|
if(hasBuilt(BuildingID::HORDE_2))
|
2023-04-05 02:26:29 +02:00
|
|
|
ret.entries.emplace_back(subID, BuildingID::HORDE_2, creature->getHorde());
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2023-01-24 17:35:07 +02:00
|
|
|
//statue-of-legion-like bonus: % to base+castle
|
2023-05-01 00:20:01 +02:00
|
|
|
TConstBonusListPtr bonuses2 = getBonuses(Selector::type()(BonusType::CREATURE_GROWTH_PERCENT));
|
2023-01-24 17:35:07 +02:00
|
|
|
for(const auto & b : *bonuses2)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-01-24 17:35:30 +02:00
|
|
|
const auto growth = b->val * (base + castleBonus) / 100;
|
2024-04-26 19:22:20 +02:00
|
|
|
if (growth)
|
|
|
|
{
|
2024-10-25 20:36:02 +02:00
|
|
|
ret.entries.emplace_back(growth, b->Description(cb, growth));
|
2024-04-26 19:22:20 +02:00
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//other *-of-legion-like bonuses (%d to growth cumulative with grail)
|
2024-01-05 16:57:44 +02:00
|
|
|
// 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)));
|
2020-10-01 07:55:41 +02:00
|
|
|
for(const auto & b : *bonuses)
|
2024-10-25 20:36:02 +02:00
|
|
|
ret.entries.emplace_back(b->val, b->Description(cb));
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
int dwellingBonus = 0;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(const PlayerState *p = cb->getPlayerState(tempOwner, false))
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-08-25 17:04:44 +02:00
|
|
|
dwellingBonus = getDwellingBonus(creatures[level].second, p->getOwnedObjects());
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
if(dwellingBonus)
|
2023-02-12 22:39:17 +02:00
|
|
|
ret.entries.emplace_back(VLC->generaltexth->allTexts[591], dwellingBonus); // \nExternal dwellings %+d
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
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);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-25 17:04:44 +02:00
|
|
|
int CGTownInstance::getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<const CGObjectInstance * >& dwellings) const
|
2017-10-07 16:42:33 +02:00
|
|
|
{
|
|
|
|
int totalBonus = 0;
|
|
|
|
for (const auto& dwelling : dwellings)
|
|
|
|
{
|
2024-08-25 17:04:44 +02:00
|
|
|
const auto & dwellingCreatures = dwelling->asOwnable()->providedCreatures();
|
|
|
|
bool hasMatch = false;
|
|
|
|
for (const auto& creature : dwellingCreatures)
|
|
|
|
hasMatch = vstd::contains(creatureIds, creature);
|
|
|
|
|
|
|
|
if (hasMatch)
|
|
|
|
totalBonus += 1;
|
2017-10-07 16:42:33 +02:00
|
|
|
}
|
|
|
|
return totalBonus;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
TResources CGTownInstance::dailyIncome() const
|
|
|
|
{
|
|
|
|
TResources ret;
|
2024-10-05 21:37:52 +02:00
|
|
|
for(const auto & p : getTown()->buildings)
|
2016-02-15 12:34:37 +02:00
|
|
|
{
|
2014-06-05 19:52:14 +03:00
|
|
|
BuildingID buildingUpgrade;
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
for(const auto & p2 : getTown()->buildings)
|
2016-02-15 12:34:37 +02:00
|
|
|
{
|
2014-06-05 19:52:14 +03: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
|
|
|
|
2024-08-29 17:45:46 +02:00
|
|
|
if (!getOwner().isValidPlayer())
|
|
|
|
return ret;
|
|
|
|
|
2024-08-24 22:42:19 +02:00
|
|
|
const auto & playerSettings = cb->getPlayerSettings(getOwner());
|
|
|
|
ret.applyHandicap(playerSettings->handicap.percentIncome);
|
2014-06-05 19:52:14 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2021-03-23 16:47:07 +02:00
|
|
|
|
2024-08-25 17:04:44 +02:00
|
|
|
std::vector<CreatureID> CGTownInstance::providedCreatures() const
|
2024-08-24 22:42:19 +02:00
|
|
|
{
|
2024-08-25 17:04:44 +02:00
|
|
|
return {};
|
2024-08-24 22:42:19 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
bool CGTownInstance::hasFort() const
|
|
|
|
{
|
|
|
|
return hasBuilt(BuildingID::FORT);
|
|
|
|
}
|
2021-03-23 16:47:07 +02:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
bool CGTownInstance::hasCapitol() const
|
|
|
|
{
|
|
|
|
return hasBuilt(BuildingID::CAPITOL);
|
|
|
|
}
|
2021-03-23 16:47:07 +02:00
|
|
|
|
2024-08-28 21:33:56 +02:00
|
|
|
TownFortifications CGTownInstance::fortificationsLevel() const
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
auto result = getTown()->fortifications;
|
2024-08-28 21:33:56 +02:00
|
|
|
|
|
|
|
for (auto const & buildingID : builtBuildings)
|
2024-10-05 21:37:52 +02:00
|
|
|
result += getTown()->buildings.at(buildingID)->fortifications;
|
2024-08-28 21:33:56 +02:00
|
|
|
|
|
|
|
if (result.wallsHealth == 0)
|
|
|
|
return TownFortifications();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CGTownInstance::CGTownInstance(IGameCallback *cb):
|
|
|
|
CGDwelling(cb),
|
2024-06-24 03:23:26 +02:00
|
|
|
built(0),
|
2023-02-12 22:39:17 +02:00
|
|
|
destroyed(0),
|
|
|
|
identifier(0),
|
2024-09-28 01:18:10 +02:00
|
|
|
alignmentToPlayer(PlayerColor::NEUTRAL),
|
2024-10-01 16:32:28 +02:00
|
|
|
spellResearchCounterDay(0),
|
|
|
|
spellResearchAcceptedCounter(0),
|
2024-09-28 14:25:11 +02:00
|
|
|
spellResearchAllowed(true)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2021-03-23 16:47:07 +02:00
|
|
|
this->setNodeType(CBonusSystemNode::TOWN);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CGTownInstance::~CGTownInstance()
|
|
|
|
{
|
2024-08-16 16:49:42 +02:00
|
|
|
for (auto & elem : rewardableBuildings)
|
|
|
|
delete elem.second;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-02 23:55:46 +02:00
|
|
|
if (hasBuilt(BuildingSubID::LIBRARY))
|
2014-06-05 19:52:14 +03:00
|
|
|
ret++;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGTownInstance::needsLastStack() const
|
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
return garrisonHero != nullptr;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
|
|
|
|
{
|
2023-08-19 20:43:50 +02:00
|
|
|
if(cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ) == PlayerRelations::ENEMIES)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if(armedGarrison() || visitingHero)
|
|
|
|
{
|
2021-03-23 16:47:07 +02:00
|
|
|
const CGHeroInstance * defendingHero = visitingHero ? visitingHero : garrisonHero;
|
2021-09-12 13:30:54 +02:00
|
|
|
const CArmedInstance * defendingArmy = defendingHero ? (CArmedInstance *)defendingHero : this;
|
|
|
|
const bool isBattleOutside = isBattleOutsideTown(defendingHero);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
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);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
if(nodeSiege == (CBonusSystemNode *)this)
|
2021-09-12 13:30:54 +02:00
|
|
|
cb->swapGarrisonOnSiege(this->id);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
const_cast<CGHeroInstance *>(defendingHero)->inTownGarrison = false; //hack to return visitor from garrison after battle
|
2016-02-21 21:23:47 +02:00
|
|
|
}
|
2024-08-31 23:04:32 +02:00
|
|
|
cb->startBattle(h, defendingArmy, getSightCenter(), h, defendingHero, BattleLayout::createDefaultLayout(cb, h, defendingArmy), (isBattleOutside ? nullptr : this));
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-23 16:47:07 +02:00
|
|
|
auto heroColor = h->getOwner();
|
2021-10-29 10:45:10 +02:00
|
|
|
onTownCaptured(heroColor);
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
if(cb->gameState()->getPlayerStatus(heroColor) == EPlayerStatus::WINNER)
|
2019-04-23 20:04:07 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
cb->heroVisitCastle(this, h);
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 15:06:32 +02:00
|
|
|
else
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-08-12 15:06:32 +02:00
|
|
|
assert(h->visitablePos() == this->visitablePos());
|
2016-10-29 17:00:42 +02:00
|
|
|
bool commander_recover = h->commander && !h->commander->alive;
|
|
|
|
if (commander_recover) // rise commander from dead
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
SetCommanderProperty scp;
|
|
|
|
scp.heroid = h->id;
|
|
|
|
scp.which = SetCommanderProperty::ALIVE;
|
|
|
|
scp.amount = 1;
|
2024-10-04 20:59:51 +02:00
|
|
|
cb->sendAndApply(scp);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
cb->heroVisitCastle(this, h);
|
2016-10-29 17:00:42 +02:00
|
|
|
// TODO(vmarkovtsev): implement payment for rising the commander
|
|
|
|
if (commander_recover) // info window about commander
|
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(h->commander->getName());
|
2023-10-31 11:09:56 +02:00
|
|
|
iw.components.emplace_back(ComponentType::CREATURE, h->commander->getId(), h->commander->getCount());
|
2016-10-29 17:00:42 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
|
|
|
|
{
|
2014-07-01 13:07:53 +03:00
|
|
|
//FIXME: find out why this issue appears on random maps
|
2017-08-11 19:03:05 +02:00
|
|
|
if(visitingHero == h)
|
2014-07-01 13:07:53 +03:00
|
|
|
{
|
|
|
|
cb->stopHeroVisitCastle(this, h);
|
2023-09-28 02:51:44 +02:00
|
|
|
logGlobal->trace("%s correctly left town %s", h->getNameTranslated(), getNameTranslated());
|
2014-07-01 13:07:53 +03:00
|
|
|
}
|
|
|
|
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());
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
std::string CGTownInstance::getObjectName() const
|
|
|
|
{
|
2024-12-03 18:27:24 +02:00
|
|
|
return getNameTranslated() + ", " + (ID == Obj::RANDOM_TOWN ? "Random town" : getTown()->faction->getNameTranslated());
|
2014-06-24 20:39:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:03:01 +02:00
|
|
|
bool CGTownInstance::townEnvisagesBuilding(BuildingSubID::EBuildingSubID subId) const
|
2020-10-02 23:55:46 +02:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return getTown()->getBuildingType(subId) != BuildingID::NONE;
|
2020-10-15 14:03:01 +02:00
|
|
|
}
|
|
|
|
|
2024-08-16 14:57:38 +02:00
|
|
|
void CGTownInstance::initializeConfigurableBuildings(vstd::RNG & rand)
|
2020-10-19 21:38:06 +02:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
for(const auto & kvp : getTown()->buildings)
|
2020-10-19 21:38:06 +02:00
|
|
|
{
|
2024-08-16 14:57:38 +02:00
|
|
|
if(!kvp.second->rewardableObjectInfo.getParameters().isNull())
|
2024-08-16 16:49:42 +02:00
|
|
|
rewardableBuildings[kvp.first] = new TownRewardableBuildingInstance(this, kvp.second->bid, rand);
|
2020-10-19 21:38:06 +02:00
|
|
|
}
|
2020-10-02 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 17:49:33 +02:00
|
|
|
DamageRange CGTownInstance::getTowerDamageRange() const
|
2023-01-16 18:28:05 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2023-01-16 18:28:05 +02:00
|
|
|
// extra damage, for each building in town
|
2023-02-12 22:39:17 +02:00
|
|
|
static constexpr int extraDamage = 1;
|
2023-01-16 18:28:05 +02:00
|
|
|
|
|
|
|
const int minDamage = baseDamage + extraDamage * getTownLevel();
|
|
|
|
|
|
|
|
return {
|
|
|
|
minDamage,
|
|
|
|
minDamage * 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:49:33 +02:00
|
|
|
DamageRange CGTownInstance::getKeepDamageRange() const
|
2023-01-16 18:28:05 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2023-01-16 18:28:05 +02:00
|
|
|
// extra damage, for each building in town
|
2023-02-12 22:39:17 +02:00
|
|
|
static constexpr int extraDamage = 2;
|
2023-01-16 18:28:05 +02:00
|
|
|
|
|
|
|
const int minDamage = baseDamage + extraDamage * getTownLevel();
|
|
|
|
|
|
|
|
return {
|
|
|
|
minDamage,
|
|
|
|
minDamage * 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
FactionID CGTownInstance::randomizeFaction(vstd::RNG & rand)
|
2023-10-25 12:50:11 +02:00
|
|
|
{
|
|
|
|
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)
|
2023-10-25 12:50:11 +02:00
|
|
|
if (VLC->factions()->getById(faction)->hasTown())
|
|
|
|
potentialPicks.push_back(faction);
|
|
|
|
|
|
|
|
assert(!potentialPicks.empty());
|
|
|
|
return *RandomGeneratorUtil::nextItem(potentialPicks, rand);
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CGTownInstance::pickRandomObject(vstd::RNG & rand)
|
2023-10-25 12:50:11 +02:00
|
|
|
{
|
|
|
|
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);
|
2024-10-05 21:37:52 +02:00
|
|
|
randomizeArmy(getFactionID());
|
2023-10-25 20:56:00 +02:00
|
|
|
updateAppearance();
|
2023-10-25 12:50:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CGTownInstance::initObj(vstd::RNG & rand) ///initialize town structures
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
blockVisit = true;
|
|
|
|
|
2020-10-15 14:03:01 +02:00
|
|
|
if(townEnvisagesBuilding(BuildingSubID::PORTAL_OF_SUMMONING)) //Dungeon for example
|
2024-10-05 21:37:52 +02:00
|
|
|
creatures.resize(getTown()->creatures.size() + 1);
|
2014-06-05 19:52:14 +03:00
|
|
|
else
|
2024-10-05 21:37:52 +02:00
|
|
|
creatures.resize(getTown()->creatures.size());
|
2020-10-02 23:55:46 +02:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
for (int level = 0; level < getTown()->creatures.size(); level++)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-08-10 00:29:30 +02:00
|
|
|
BuildingID buildID = BuildingID(BuildingID::getDwellingFromLevel(level, 0));
|
2014-06-05 19:52:14 +03:00
|
|
|
int upgradeNum = 0;
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
for (; getTown()->buildings.count(buildID); upgradeNum++, BuildingID::advanceDwelling(buildID))
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if (hasBuilt(buildID) && getTown()->creatures.at(level).size() > upgradeNum)
|
|
|
|
creatures[level].second.push_back(getTown()->creatures[level][upgradeNum]);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-16 14:57:38 +02:00
|
|
|
initializeConfigurableBuildings(rand);
|
2024-08-29 17:46:22 +02:00
|
|
|
initializeNeutralTownGarrison(rand);
|
2014-06-05 19:52:14 +03:00
|
|
|
recreateBuildingsBonuses();
|
2014-06-16 19:27:26 +03:00
|
|
|
updateAppearance();
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2024-08-29 17:46:22 +02:00
|
|
|
void CGTownInstance::initializeNeutralTownGarrison(vstd::RNG & rand)
|
|
|
|
{
|
|
|
|
struct RandomGuardsInfo{
|
|
|
|
int tier;
|
|
|
|
int chance;
|
|
|
|
int min;
|
|
|
|
int max;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr std::array<RandomGuardsInfo, 4> randomGuards = {
|
|
|
|
RandomGuardsInfo{ 0, 33, 8, 15 },
|
|
|
|
RandomGuardsInfo{ 1, 33, 5, 7 },
|
|
|
|
RandomGuardsInfo{ 2, 20, 3, 5 },
|
|
|
|
RandomGuardsInfo{ 3, 14, 1, 3 },
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only neutral towns may get initial garrison
|
|
|
|
if (getOwner().isValidPlayer())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Only towns with garrison not set in map editor may get initial garrison
|
|
|
|
// FIXME: H3 editor allow explicitly empty garrison, but vcmi loses this flag on load
|
|
|
|
if (stacksCount() > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto const & guard : randomGuards)
|
|
|
|
{
|
|
|
|
if (rand.nextInt(99) >= guard.chance)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CreatureID guardID = getTown()->creatures[guard.tier].at(0);
|
|
|
|
int guardSize = rand.nextInt(guard.min, guard.max);
|
|
|
|
|
|
|
|
putStack(getFreeSlot(), new CStackInstance(guardID, guardSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CGTownInstance::newTurn(vstd::RNG & rand) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-08-16 16:49:42 +02:00
|
|
|
for(const auto & building : rewardableBuildings)
|
|
|
|
building.second->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);
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2024-08-29 16:19:23 +02:00
|
|
|
|
2014-06-24 02:26:36 +03:00
|
|
|
bool CGTownInstance::passableFor(PlayerColor color) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if (!armedGarrison())//empty castle - anyone can visit
|
2014-06-24 02:26:36 +03:00
|
|
|
return true;
|
2014-06-05 19:52:14 +03:00
|
|
|
if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
|
2014-06-24 02:26:36 +03:00
|
|
|
return false;
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2019-03-20 21:58:15 +02:00
|
|
|
return cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
|
|
|
|
{
|
2023-06-20 21:38:47 +02:00
|
|
|
offsets = {int3(-1,2,0), int3(+1,2,0)};
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-21 21:23:47 +02:00
|
|
|
void CGTownInstance::mergeGarrisonOnSiege() const
|
|
|
|
{
|
2020-10-02 23:55:46 +02:00
|
|
|
auto getWeakestStackSlot = [&](ui64 powerLimit)
|
2016-02-21 21:23:47 +02:00
|
|
|
{
|
|
|
|
std::vector<SlotID> weakSlots;
|
|
|
|
auto stacksList = visitingHero->stacks;
|
|
|
|
std::pair<SlotID, CStackInstance *> pair;
|
2023-02-12 22:39:17 +02:00
|
|
|
while(!stacksList.empty())
|
2016-02-21 21:23:47 +02:00
|
|
|
{
|
2023-02-12 22:39:17 +02:00
|
|
|
pair = *vstd::minElementByFun(stacksList, [&](const std::pair<SlotID, CStackInstance *> & elem) { return elem.second->getPower(); });
|
2016-02-21 21:23:47 +02:00
|
|
|
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())
|
2016-02-21 21:23:47 +02:00
|
|
|
return *std::max_element(weakSlots.begin(), weakSlots.end());
|
|
|
|
|
|
|
|
return SlotID();
|
|
|
|
};
|
|
|
|
|
2020-10-02 23:55:46 +02:00
|
|
|
auto count = static_cast<int>(stacks.size());
|
|
|
|
|
2016-02-21 21:23:47 +02:00
|
|
|
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)
|
2016-02-21 21:23:47 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
dst = getWeakestStackSlot(static_cast<int>(pair.second->getPower()));
|
2016-02-21 21:23:47 +02:00
|
|
|
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
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if (hasCapitol()) // search if there's an older capitol
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
PlayerState* state = cb->gameState()->getPlayerState(owner); //get all towns owned by player
|
2024-10-05 21:37:52 +02:00
|
|
|
for (const auto & otherTown : state->getTowns())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if (otherTown != this && otherTown->hasCapitol())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
RazeStructures rs;
|
|
|
|
rs.tid = id;
|
|
|
|
rs.bid.insert(BuildingID::CAPITOL);
|
|
|
|
rs.destroyed = destroyed;
|
2024-10-04 20:59:51 +02:00
|
|
|
cb->sendAndApply(rs);
|
2014-06-05 19:52:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 02:33:15 +02:00
|
|
|
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
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return getTown()->faction->boatType;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CGTownInstance::getMarketEfficiency() const
|
|
|
|
{
|
2020-10-15 14:03:01 +02:00
|
|
|
if(!hasBuiltSomeTradeBuilding())
|
2014-06-05 19:52:14 +03:00
|
|
|
return 0;
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
const PlayerState *p = cb->getPlayerState(tempOwner);
|
2014-06-05 19:52:14 +03:00
|
|
|
assert(p);
|
|
|
|
|
|
|
|
int marketCount = 0;
|
2024-08-25 00:21:26 +02:00
|
|
|
for(const CGTownInstance *t : p->getTowns())
|
2020-10-15 14:03:01 +02:00
|
|
|
if(t->hasBuiltSomeTradeBuilding())
|
2014-06-05 19:52:14 +03:00
|
|
|
marketCount++;
|
|
|
|
|
|
|
|
return marketCount;
|
|
|
|
}
|
|
|
|
|
2023-11-08 17:49:08 +02:00
|
|
|
std::vector<TradeItemBuy> CGTownInstance::availableItemsIds(EMarketMode mode) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if(mode == EMarketMode::RESOURCE_ARTIFACT)
|
|
|
|
{
|
2023-11-08 17:49:08 +02:00
|
|
|
std::vector<TradeItemBuy> ret;
|
2024-10-12 20:19:58 +02:00
|
|
|
for(const ArtifactID a : cb->gameState()->map->townMerchantArtifacts)
|
|
|
|
ret.push_back(a);
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if ( mode == EMarketMode::RESOURCE_SKILL )
|
|
|
|
{
|
2024-01-09 16:43:36 +02:00
|
|
|
return cb->gameState()->map->townUniversitySkills;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return IMarket::availableItemsIds(mode);
|
|
|
|
}
|
|
|
|
|
2024-08-20 16:15:50 +02:00
|
|
|
ObjectInstanceID CGTownInstance::getObjInstanceID() const
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
void CGTownInstance::updateAppearance()
|
|
|
|
{
|
2024-07-13 20:37:13 +02:00
|
|
|
auto terrain = cb->gameState()->getTile(visitablePos())->getTerrainID();
|
2014-06-15 19:43:01 +03:00
|
|
|
//FIXME: not the best way to do this
|
2023-10-24 16:11:25 +02:00
|
|
|
auto app = getObjectHandler()->getOverride(terrain, this);
|
2014-06-15 19:43:01 +03:00
|
|
|
if (app)
|
2022-09-11 15:12:35 +02:00
|
|
|
appearance = app;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGTownInstance::nodeName() const
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return "Town (" + getTown()->faction->getNameTranslated() + ") of " + getNameTranslated();
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::deserializationFix()
|
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
attachTo(townAndVis);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
//Hero is already handled by CGameState::attachArmedObjects
|
|
|
|
|
|
|
|
// if(visitingHero)
|
|
|
|
// visitingHero->attachTo(&townAndVis);
|
|
|
|
// if(garrisonHero)
|
|
|
|
// garrisonHero->attachTo(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::updateMoraleBonusFromArmy()
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
|
2014-06-05 19:52:14 +03:00
|
|
|
if(!b)
|
|
|
|
{
|
2023-10-21 13:50:42 +02:00
|
|
|
b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
|
2014-06-05 19:52:14 +03:00
|
|
|
addNewBonus(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (garrisonHero)
|
2015-12-11 15:13:18 +02:00
|
|
|
{
|
2014-06-05 19:52:14 +03:00
|
|
|
b->val = 0;
|
2015-12-11 15:13:18 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
else
|
|
|
|
CArmedInstance::updateMoraleBonusFromArmy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::recreateBuildingsBonuses()
|
|
|
|
{
|
|
|
|
BonusList bl;
|
2023-05-01 00:20:01 +02:00
|
|
|
getExportedBonusList().getBonuses(bl, Selector::sourceType()(BonusSource::TOWN_STRUCTURE));
|
2021-01-14 00:02:13 +02:00
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
for(const auto & b : bl)
|
2014-06-05 19:52:14 +03:00
|
|
|
removeBonus(b);
|
|
|
|
|
2024-08-16 20:09:09 +02:00
|
|
|
|
|
|
|
|
2022-05-19 14:14:50 +02:00
|
|
|
for(const auto & bid : builtBuildings)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-08-16 20:09:09 +02:00
|
|
|
bool bonusesReplacedByUpgrade = false;
|
|
|
|
|
|
|
|
for(const auto & upgradeID : builtBuildings)
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
const auto & upgrade = getTown()->buildings.at(upgradeID);
|
2024-08-16 20:09:09 +02:00
|
|
|
if (upgrade->getBase() == bid && upgrade->upgradeReplacesBonuses)
|
|
|
|
bonusesReplacedByUpgrade = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bonuses from this building are disabled and replaced by bonuses from an upgrade
|
|
|
|
if (bonusesReplacedByUpgrade)
|
|
|
|
continue;
|
2020-12-10 03:05:37 +02:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
auto building = getTown()->buildings.at(bid);
|
2020-10-02 23:55:46 +02:00
|
|
|
|
2021-01-14 00:02:13 +02:00
|
|
|
if(building->buildingBonuses.empty())
|
|
|
|
continue;
|
2020-10-02 23:55:46 +02:00
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
for(auto & bonus : building->buildingBonuses)
|
2023-04-06 19:03:17 +02:00
|
|
|
addNewBonus(bonus);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::setVisitingHero(CGHeroInstance *h)
|
|
|
|
{
|
2023-05-02 02:44:09 +02:00
|
|
|
if(visitingHero.get() == h)
|
|
|
|
return;
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
if(h)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
|
2014-06-05 19:52:14 +03:00
|
|
|
assert(p);
|
2022-11-06 01:26:13 +02:00
|
|
|
h->detachFrom(*p);
|
|
|
|
h->attachTo(townAndVis);
|
2014-06-05 19:52:14 +03:00
|
|
|
visitingHero = h;
|
|
|
|
h->visitedTown = this;
|
|
|
|
h->inTownGarrison = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
PlayerState *p = cb->gameState()->getPlayerState(visitingHero->tempOwner);
|
2014-06-05 19:52:14 +03:00
|
|
|
visitingHero->visitedTown = nullptr;
|
2022-11-06 01:26:13 +02:00
|
|
|
visitingHero->detachFrom(townAndVis);
|
|
|
|
visitingHero->attachTo(*p);
|
2014-06-05 19:52:14 +03:00
|
|
|
visitingHero = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
|
|
|
|
{
|
2023-05-02 02:44:09 +02:00
|
|
|
if(garrisonHero.get() == h)
|
|
|
|
return;
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
if(h)
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
|
2014-06-05 19:52:14 +03:00
|
|
|
assert(p);
|
2022-11-06 01:26:13 +02:00
|
|
|
h->detachFrom(*p);
|
|
|
|
h->attachTo(*this);
|
2014-06-05 19:52:14 +03:00
|
|
|
garrisonHero = h;
|
|
|
|
h->visitedTown = this;
|
|
|
|
h->inTownGarrison = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
PlayerState *p = cb->gameState()->getPlayerState(garrisonHero->tempOwner);
|
2014-06-05 19:52:14 +03:00
|
|
|
garrisonHero->visitedTown = nullptr;
|
|
|
|
garrisonHero->inTownGarrison = false;
|
2022-11-06 01:26:13 +02:00
|
|
|
garrisonHero->detachFrom(*this);
|
|
|
|
garrisonHero->attachTo(*p);
|
2014-06-05 19:52:14 +03:00
|
|
|
garrisonHero = nullptr;
|
|
|
|
}
|
|
|
|
updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGTownInstance::armedGarrison() const
|
|
|
|
{
|
2021-10-29 10:45:10 +02:00
|
|
|
return !stacks.empty() || garrisonHero;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CGTownInstance::getTownLevel() const
|
|
|
|
{
|
|
|
|
// count all buildings that are not upgrades
|
2020-10-02 23:55:46 +02:00
|
|
|
int level = 0;
|
|
|
|
|
2021-02-20 03:57:50 +02:00
|
|
|
for(const auto & bid : builtBuildings)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if(getTown()->buildings.at(bid)->upgrade == BuildingID::NONE)
|
2020-10-02 23:55:46 +02:00
|
|
|
level++;
|
2021-02-20 03:57:50 +02:00
|
|
|
}
|
2020-10-02 23:55:46 +02:00
|
|
|
return level;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
CBonusSystemNode & CGTownInstance::whatShouldBeAttached()
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
return townAndVis;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-01-04 15:17:50 +02:00
|
|
|
std::string CGTownInstance::getNameTranslated() const
|
|
|
|
{
|
2023-09-27 23:57:05 +02:00
|
|
|
return VLC->generaltexth->translate(nameTextId);
|
2023-01-04 15:17:50 +02:00
|
|
|
}
|
|
|
|
|
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-01-04 15:17:50 +02:00
|
|
|
{
|
2023-09-27 23:57:05 +02:00
|
|
|
nameTextId = newName;
|
2023-01-04 15:17:50 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
const CArmedInstance * CGTownInstance::getUpperArmy() const
|
|
|
|
{
|
|
|
|
if(garrisonHero)
|
|
|
|
return garrisonHero;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:03:01 +02:00
|
|
|
bool CGTownInstance::hasBuiltSomeTradeBuilding() const
|
|
|
|
{
|
2024-08-24 12:05:11 +02:00
|
|
|
return availableModes().empty() ? false : true;
|
2020-10-15 14:03:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 23:55:46 +02:00
|
|
|
bool CGTownInstance::hasBuilt(BuildingSubID::EBuildingSubID buildingID) const
|
|
|
|
{
|
|
|
|
for(const auto & bid : builtBuildings)
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if(getTown()->buildings.at(bid)->subId == buildingID)
|
2020-10-02 23:55:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
bool CGTownInstance::hasBuilt(const BuildingID & buildingID) const
|
2020-10-02 23:55:46 +02:00
|
|
|
{
|
|
|
|
return vstd::contains(builtBuildings, buildingID);
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:12:58 +02:00
|
|
|
bool CGTownInstance::hasBuilt(const BuildingID & buildingID, FactionID townID) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if (townID == getTown()->faction->getId() || townID == FactionID::ANY)
|
2014-06-05 19:52:14 +03:00
|
|
|
return hasBuilt(buildingID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-16 17:00:02 +02:00
|
|
|
void CGTownInstance::addBuilding(const BuildingID & buildingID)
|
|
|
|
{
|
|
|
|
if(buildingID == BuildingID::NONE)
|
|
|
|
return;
|
|
|
|
|
2024-08-27 15:44:30 +02:00
|
|
|
builtBuildings.insert(buildingID);
|
2024-08-16 17:00:02 +02:00
|
|
|
}
|
|
|
|
|
2024-08-27 15:44:30 +02:00
|
|
|
std::set<EMarketMode> CGTownInstance::availableModes() const
|
2024-08-26 22:52:37 +02:00
|
|
|
{
|
2024-08-27 15:44:30 +02:00
|
|
|
std::set<EMarketMode> result;
|
|
|
|
for (const auto & buildingID : builtBuildings)
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
const auto * buildingPtr = getTown()->buildings.at(buildingID).get();
|
2024-08-27 15:44:30 +02:00
|
|
|
result.insert(buildingPtr->marketModes.begin(), buildingPtr->marketModes.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-08-26 22:52:37 +02:00
|
|
|
}
|
|
|
|
|
2024-08-16 17:00:02 +02:00
|
|
|
void CGTownInstance::removeBuilding(const BuildingID & buildingID)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(builtBuildings, buildingID))
|
|
|
|
return;
|
|
|
|
|
2024-08-27 15:44:30 +02:00
|
|
|
builtBuildings.erase(buildingID);
|
2024-08-16 17:00:02 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 21:06:48 +02:00
|
|
|
void CGTownInstance::removeAllBuildings()
|
|
|
|
{
|
|
|
|
builtBuildings.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<BuildingID> CGTownInstance::getBuildings() const
|
|
|
|
{
|
|
|
|
return builtBuildings;
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
TResources CGTownInstance::getBuildingCost(const BuildingID & buildingID) const
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if (vstd::contains(getTown()->buildings, buildingID))
|
|
|
|
return getTown()->buildings.at(buildingID)->resources;
|
2018-07-26 12:06:55 +02:00
|
|
|
else
|
|
|
|
{
|
2024-10-02 18:40:06 +02:00
|
|
|
logGlobal->error("Town %s at %s has no possible building %d!", getNameTranslated(), anchorPos().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
|
2014-08-07 19:40:22 +03:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
const CBuilding * building = getTown()->buildings.at(buildID);
|
2014-08-07 19:40:22 +03:00
|
|
|
|
2016-10-15 03:19:47 +02:00
|
|
|
//TODO: find better solution to prevent infinite loops
|
|
|
|
std::set<BuildingID> processed;
|
2016-10-14 22:46:24 +02:00
|
|
|
|
2014-08-07 19:40:22 +03:00
|
|
|
std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
|
|
|
|
[&](const BuildingID & id) -> CBuilding::TRequired::Variant
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
if (getTown()->buildings.count(id) == 0)
|
2023-08-10 20:18:52 +02:00
|
|
|
{
|
|
|
|
logMod->error("Invalid building ID %d in building dependencies!", id.getNum());
|
|
|
|
return CBuilding::TRequired::OperatorAll();
|
|
|
|
}
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
const CBuilding * build = getTown()->buildings.at(id);
|
2016-09-10 19:38:49 +02:00
|
|
|
CBuilding::TRequired::OperatorAll requirements;
|
2014-08-07 19:40:22 +03:00
|
|
|
|
|
|
|
if (!hasBuilt(id))
|
2016-09-10 19:38:49 +02:00
|
|
|
{
|
2016-10-14 22:46:24 +02:00
|
|
|
if (deep)
|
2023-02-12 22:39:17 +02:00
|
|
|
requirements.expressions.emplace_back(id);
|
2016-10-14 22:46:24 +02:00
|
|
|
else
|
|
|
|
return id;
|
2016-09-10 19:38:49 +02:00
|
|
|
}
|
2014-08-07 19:40:22 +03:00
|
|
|
|
2016-10-15 03:19:47 +02:00
|
|
|
if(!vstd::contains(processed, id))
|
|
|
|
{
|
|
|
|
processed.insert(id);
|
|
|
|
if (build->upgrade != BuildingID::NONE)
|
|
|
|
requirements.expressions.push_back(dependTest(build->upgrade));
|
2016-08-30 20:53:56 +02:00
|
|
|
|
2016-10-15 03:19:47 +02:00
|
|
|
requirements.expressions.push_back(build->requirements.morph(dependTest));
|
|
|
|
}
|
2016-08-30 20:53:56 +02:00
|
|
|
return requirements;
|
2014-08-07 19:40:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
CBuilding::TRequired::OperatorAll requirements;
|
|
|
|
if (building->upgrade != BuildingID::NONE)
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
const CBuilding * upgr = getTown()->buildings.at(building->upgrade);
|
2014-08-07 19:40:22 +03:00
|
|
|
|
2016-08-30 20:53:56 +02:00
|
|
|
requirements.expressions.push_back(dependTest(upgr->bid));
|
2016-10-15 03:19:47 +02:00
|
|
|
processed.clear();
|
2014-08-07 19:40:22 +03:00
|
|
|
}
|
|
|
|
requirements.expressions.push_back(building->requirements.morph(dependTest));
|
|
|
|
|
|
|
|
CBuilding::TRequired::Variant variant(requirements);
|
|
|
|
CBuilding::TRequired ret(variant);
|
|
|
|
ret.minimize();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:32 +02:00
|
|
|
void CGTownInstance::addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID ) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if(visitingHero == h)
|
2023-11-06 18:27:16 +02:00
|
|
|
cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
|
2014-06-05 19:52:14 +03:00
|
|
|
else if(garrisonHero == h)
|
2023-11-06 18:27:16 +02:00
|
|
|
cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
|
2014-06-05 19:52:14 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//should never ever happen
|
2023-01-02 13:27:03 +02:00
|
|
|
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");
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
void CGTownInstance::battleFinished(const CGHeroInstance * hero, const BattleResult & result) const
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2021-03-23 16:47:07 +02:00
|
|
|
if(result.winner == BattleSide::ATTACKER)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2016-02-22 02:33:15 +02:00
|
|
|
clearArmy();
|
2021-10-29 10:45:10 +02:00
|
|
|
onTownCaptured(hero->getOwner());
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
void CGTownInstance::onTownCaptured(const PlayerColor & winner) const
|
2021-10-29 10:45:10 +02:00
|
|
|
{
|
|
|
|
setOwner(winner);
|
2023-10-04 14:31:42 +02:00
|
|
|
cb->changeFogOfWar(getSightCenter(), getSightRadius(), winner, ETileVisibility::REVEALED);
|
2021-10-29 10:45:10 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 15:23:42 +02:00
|
|
|
void CGTownInstance::afterAddToMap(CMap * map)
|
|
|
|
{
|
2023-11-01 16:10:33 +02:00
|
|
|
map->towns.emplace_back(this);
|
2017-05-28 15:23:42 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2016-02-22 01:37:19 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
auto decodeBuilding = [this](const std::string & identifier) -> si32
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), getTown()->getBuildingScope(), identifier);
|
2016-02-23 15:36:21 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(rawId)
|
2023-04-16 19:42:56 +02:00
|
|
|
return rawId.value();
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
};
|
2016-02-23 15:36:21 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
auto encodeBuilding = [this](si32 index) -> std::string
|
|
|
|
{
|
2023-01-04 15:17:50 +02:00
|
|
|
return getTown()->buildings.at(BuildingID(index))->getJsonKey();
|
2016-11-13 12:38:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-05-19 14:14:50 +02:00
|
|
|
for(const BuildingID & id : forbiddenBuildings)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2023-11-08 17:35:17 +02:00
|
|
|
buildingsLIC.none.insert(id.getNum());
|
2016-11-13 12:38:42 +02:00
|
|
|
customBuildings = true;
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:14:50 +02:00
|
|
|
for(const BuildingID & id : builtBuildings)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
if(id == BuildingID::DEFAULT)
|
|
|
|
continue;
|
2016-02-23 15:36:21 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
const CBuilding * building = getTown()->buildings.at(id);
|
|
|
|
|
|
|
|
if(building->mode == CBuilding::BUILD_AUTO)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(id == BuildingID::FORT)
|
|
|
|
hasFort = true;
|
|
|
|
|
2023-11-08 17:35:17 +02:00
|
|
|
buildingsLIC.all.insert(id.getNum());
|
2016-11-13 12:38:42 +02:00
|
|
|
customBuildings = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(customBuildings)
|
|
|
|
handler.serializeLIC("buildings", buildingsLIC);
|
|
|
|
else
|
|
|
|
handler.serializeBool("hasFort",hasFort);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handler.serializeLIC("buildings", buildingsLIC);
|
|
|
|
|
2024-08-16 17:00:02 +02:00
|
|
|
addBuilding(BuildingID::VILLAGE_HALL);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if(buildingsLIC.none.empty() && buildingsLIC.all.empty())
|
|
|
|
{
|
2024-08-16 17:00:02 +02:00
|
|
|
addBuilding(BuildingID::DEFAULT);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
bool hasFort = false;
|
|
|
|
handler.serializeBool("hasFort",hasFort);
|
|
|
|
if(hasFort)
|
2024-08-16 17:00:02 +02:00
|
|
|
addBuilding(BuildingID::FORT);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(const si32 item : buildingsLIC.none)
|
|
|
|
forbiddenBuildings.insert(BuildingID(item));
|
|
|
|
for(const si32 item : buildingsLIC.all)
|
2024-08-16 17:00:02 +02:00
|
|
|
addBuilding(BuildingID(item));
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 01:37:19 +02:00
|
|
|
|
2016-02-25 21:59:17 +02:00
|
|
|
{
|
2023-11-05 16:35:51 +02:00
|
|
|
handler.serializeIdArray( "possibleSpells", possibleSpells);
|
|
|
|
handler.serializeIdArray( "obligatorySpells", obligatorySpells);
|
2016-02-25 21:59:17 +02:00
|
|
|
}
|
2024-07-14 19:06:39 +02:00
|
|
|
|
2024-07-16 21:16:26 +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
|
|
|
}
|
2016-02-22 01:37:19 +02:00
|
|
|
}
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
const CFaction * CGTownInstance::getFaction() const
|
|
|
|
{
|
|
|
|
return getFactionID().toFaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CTown * CGTownInstance::getTown() const
|
|
|
|
{
|
|
|
|
if(ID == Obj::RANDOM_TOWN)
|
|
|
|
return VLC->townh->randomTown;
|
|
|
|
|
|
|
|
return getFaction()->town;
|
|
|
|
}
|
|
|
|
|
|
|
|
FactionID CGTownInstance::getFactionID() const
|
2023-04-09 17:26:32 +02:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return FactionID(subID.getNum());
|
2023-04-09 17:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TerrainId CGTownInstance::getNativeTerrain() const
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
return getTown()->faction->getNativeTerrain();
|
2023-04-09 17:26:32 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 17:29:47 +02:00
|
|
|
ArtifactID CGTownInstance::getWarMachineInBuilding(BuildingID building) const
|
|
|
|
{
|
|
|
|
if (builtBuildings.count(building) == 0)
|
|
|
|
return ArtifactID::NONE;
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if (building == BuildingID::BLACKSMITH && getTown()->warMachineDeprecated.hasValue())
|
|
|
|
return getTown()->warMachineDeprecated.toCreature()->warMachine;
|
2024-08-23 17:29:47 +02:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
return getTown()->buildings.at(building)->warMachine;
|
2024-08-23 17:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGTownInstance::isWarMachineAvailable(ArtifactID warMachine) const
|
|
|
|
{
|
|
|
|
for (auto const & buildingID : builtBuildings)
|
2024-10-05 21:37:52 +02:00
|
|
|
if (getTown()->buildings.at(buildingID)->warMachine == warMachine)
|
2024-08-23 17:29:47 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (builtBuildings.count(BuildingID::BLACKSMITH) &&
|
2024-10-05 21:37:52 +02:00
|
|
|
getTown()->warMachineDeprecated.hasValue() &&
|
|
|
|
getTown()->warMachineDeprecated.toCreature()->warMachine == warMachine)
|
2024-08-23 17:29:47 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
GrowthInfo::Entry::Entry(const std::string &format, int _count)
|
|
|
|
: count(_count)
|
|
|
|
{
|
2023-12-10 16:33:22 +02:00
|
|
|
MetaString formatter;
|
|
|
|
formatter.appendRawString(format);
|
|
|
|
formatter.replacePositiveNumber(count);
|
|
|
|
|
|
|
|
description = formatter.toString();
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2023-12-10 16:33:22 +02:00
|
|
|
MetaString formatter;
|
|
|
|
formatter.appendRawString("%s %+d");
|
2024-10-05 21:37:52 +02:00
|
|
|
formatter.replaceRawString(FactionID(subID).toFaction()->town->buildings.at(building)->getNameTranslated());
|
2023-12-10 16:33:22 +02:00
|
|
|
formatter.replacePositiveNumber(count);
|
|
|
|
|
|
|
|
description = formatter.toString();
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
GrowthInfo::Entry::Entry(int _count, std::string fullDescription):
|
|
|
|
count(_count),
|
|
|
|
description(std::move(fullDescription))
|
2015-09-04 18:38:42 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
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
|
2024-08-01 22:48:55 +02:00
|
|
|
return vstd::divideAndCeil(ret * handicapPercentage, 100);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2020-10-25 00:04:34 +02:00
|
|
|
|
2023-06-06 18:19:30 +02:00
|
|
|
void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
|
|
|
|
{
|
|
|
|
for(const CGTownInstance::TCreaturesSet::value_type & dwelling : creatures)
|
|
|
|
{
|
2024-10-12 18:02:35 +02:00
|
|
|
if (vstd::contains(dwelling.second, stack.getId())) //Dwelling with our creature
|
2023-06-06 18:19:30 +02:00
|
|
|
{
|
|
|
|
for(const auto & upgrID : dwelling.second)
|
|
|
|
{
|
2024-10-12 18:02:35 +02:00
|
|
|
if(vstd::contains(stack.getCreature()->upgrades, upgrID)) //possible upgrade
|
2023-06-06 18:19:30 +02:00
|
|
|
{
|
|
|
|
info.newID.push_back(upgrID);
|
2024-10-12 18:02:35 +02:00
|
|
|
info.cost.push_back(upgrID.toCreature()->getFullRecruitCost() - stack.getType()->getFullRecruitCost());
|
2023-06-06 18:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
2024-08-16 15:16:53 +02:00
|
|
|
void CGTownInstance::postDeserialize()
|
|
|
|
{
|
|
|
|
setNodeType(CBonusSystemNode::TOWN);
|
2024-08-16 16:49:42 +02:00
|
|
|
for(auto & building : rewardableBuildings)
|
|
|
|
building.second->town = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<BuildingID, TownRewardableBuildingInstance*> CGTownInstance::convertOldBuildings(std::vector<TownRewardableBuildingInstance*> oldVector)
|
|
|
|
{
|
|
|
|
std::map<BuildingID, TownRewardableBuildingInstance*> result;
|
|
|
|
|
|
|
|
for(auto & building : oldVector)
|
|
|
|
{
|
|
|
|
result[building->getBuildingType()] = new TownRewardableBuildingInstance(*building);
|
|
|
|
delete building;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-08-16 15:16:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|