2015-12-02 21:39:53 +02:00
|
|
|
/*
|
2014-06-05 20:26:50 +03:00
|
|
|
* CArmedInstance.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"
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "CArmedInstance.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
#include "../CTownHandler.h"
|
|
|
|
#include "../CCreatureHandler.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#include "../CGameState.h"
|
2015-12-02 21:39:53 +02:00
|
|
|
#include "../CPlayerState.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
void CArmedInstance::randomizeArmy(int type)
|
|
|
|
{
|
|
|
|
for (auto & elem : stacks)
|
|
|
|
{
|
|
|
|
int & randID = elem.second->idRand;
|
|
|
|
if(randID >= 0)
|
|
|
|
{
|
|
|
|
int level = randID / 2;
|
|
|
|
bool upgrade = randID % 2;
|
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
|
|
|
elem.second->setType((*VLC->townh)[type]->town->creatures[level][upgrade]);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
randID = -1;
|
|
|
|
}
|
|
|
|
assert(elem.second->valid(false));
|
|
|
|
assert(elem.second->armyObj == this);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-28 15:19:27 +02:00
|
|
|
// Take Angelic Alliance troop-mixing freedom of non-evil units into account.
|
2020-11-11 21:43:40 +02:00
|
|
|
CSelector CArmedInstance::nonEvilAlignmentMixSelector = Selector::type()(Bonus::NONEVIL_ALIGNMENT_MIX);
|
2020-06-28 15:19:27 +02:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
CArmedInstance::CArmedInstance()
|
2021-05-23 13:28:43 +02:00
|
|
|
:CArmedInstance(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CArmedInstance::CArmedInstance(bool isHypotetic)
|
|
|
|
:CBonusSystemNode(isHypotetic), nonEvilAlignmentMix(this, nonEvilAlignmentMixSelector)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
battle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArmedInstance::updateMoraleBonusFromArmy()
|
|
|
|
{
|
|
|
|
if(!validTypes(false)) //object not randomized, don't bother
|
|
|
|
return;
|
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
auto b = getExportedBonusList().getFirst(Selector::sourceType()(Bonus::ARMY).And(Selector::type()(Bonus::MORALE)));
|
2016-09-19 23:36:35 +02:00
|
|
|
if(!b)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
b = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
|
2014-06-05 19:52:14 +03:00
|
|
|
addNewBonus(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
//number of alignments and presence of undead
|
|
|
|
std::set<TFaction> factions;
|
|
|
|
bool hasUndead = false;
|
|
|
|
|
2020-06-28 15:19:27 +02:00
|
|
|
const std::string undeadCacheKey = "type_UNDEAD";
|
2020-11-11 21:43:40 +02:00
|
|
|
static const CSelector undeadSelector = Selector::type()(Bonus::UNDEAD);
|
2020-06-28 15:19:27 +02:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
for(auto slot : Slots())
|
|
|
|
{
|
|
|
|
const CStackInstance * inst = slot.second;
|
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 CCreature * creature = VLC->creh->objects[inst->getCreatureID()];
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
factions.insert(creature->faction);
|
|
|
|
// Check for undead flag instead of faction (undead mummies are neutral)
|
2022-09-14 11:00:40 +02:00
|
|
|
if (!hasUndead)
|
|
|
|
{
|
|
|
|
//this is costly check, let's skip it at first undead
|
|
|
|
hasUndead |= inst->hasBonus(undeadSelector, undeadCacheKey);
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t factionsInArmy = factions.size(); //town garrison seems to take both sets into account
|
|
|
|
|
2020-06-28 15:19:27 +02:00
|
|
|
if (nonEvilAlignmentMix.getHasBonus())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
size_t mixableFactions = 0;
|
|
|
|
|
|
|
|
for(TFaction f : factions)
|
|
|
|
{
|
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 ((*VLC->townh)[f]->alignment != EAlignment::EVIL)
|
2014-06-05 19:52:14 +03:00
|
|
|
mixableFactions++;
|
|
|
|
}
|
|
|
|
if (mixableFactions > 0)
|
|
|
|
factionsInArmy -= mixableFactions - 1;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:53:11 +02:00
|
|
|
std::string description;
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
if(factionsInArmy == 1)
|
|
|
|
{
|
|
|
|
b->val = +1;
|
2021-05-16 19:53:11 +02:00
|
|
|
description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
|
|
|
|
description = description.substr(0, description.size()-3);//trim "+1"
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
else if (!factions.empty()) // no bonus from empty garrison
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
b->val = 2 - (si32)factionsInArmy;
|
2021-05-16 19:53:11 +02:00
|
|
|
description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factionsInArmy % b->val); //Troops of %d alignments %d
|
|
|
|
description = b->description.substr(0, description.size()-2);//trim value
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2021-05-16 19:53:11 +02:00
|
|
|
|
|
|
|
boost::algorithm::trim(description);
|
|
|
|
b->description = description;
|
|
|
|
|
2015-12-11 15:13:18 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
//-1 modifier for any Undead unit in army
|
|
|
|
const ui8 UNDEAD_MODIFIER_ID = -2;
|
2016-09-19 23:36:35 +02:00
|
|
|
auto undeadModifier = getExportedBonusList().getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
|
2014-06-05 19:52:14 +03:00
|
|
|
if(hasUndead)
|
|
|
|
{
|
|
|
|
if(!undeadModifier)
|
2015-09-04 18:38:42 +02:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
undeadModifier = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]);
|
2015-09-04 18:38:42 +02:00
|
|
|
undeadModifier->description = undeadModifier->description.substr(0, undeadModifier->description.size()-2);//trim value
|
|
|
|
addNewBonus(undeadModifier);
|
2015-11-16 15:30:40 +02:00
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
else if(undeadModifier)
|
|
|
|
removeBonus(undeadModifier);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArmedInstance::armyChanged()
|
|
|
|
{
|
|
|
|
updateMoraleBonusFromArmy();
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
CBonusSystemNode & CArmedInstance::whereShouldBeAttached(CGameState * gs)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
if(tempOwner < PlayerColor::PLAYER_LIMIT)
|
2022-11-06 01:26:13 +02:00
|
|
|
if(auto * where = gs->getPlayerState(tempOwner))
|
|
|
|
return *where;
|
|
|
|
|
|
|
|
return gs->globalEffects;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
CBonusSystemNode & CArmedInstance::whatShouldBeAttached()
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
return *this;
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|