123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257 |
- #include "StdInc.h"
- #include "BattleState.h"
- #include <numeric>
- #include <boost/random/linear_congruential.hpp>
- #include "VCMI_Lib.h"
- #include "CObjectHandler.h"
- #include "CHeroHandler.h"
- #include "CCreatureHandler.h"
- #include "CSpellHandler.h"
- #include "CTownHandler.h"
- #include "NetPacks.h"
- #include "JsonNode.h"
- #include "Filesystem/CResourceLoader.h"
- /*
- * BattleState.h, 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
- *
- */
- extern boost::rand48 ran;
- const CStack * BattleInfo::getNextStack() const
- {
- std::vector<const CStack *> hlp;
- battleGetStackQueue(hlp, 1, -1);
- if(hlp.size())
- return hlp[0];
- else
- return NULL;
- }
- int BattleInfo::getAvaliableHex(TCreature creID, bool attackerOwned, int initialPos) const
- {
- bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
- //bool flying = VLC->creh->creatures[creID]->isFlying();
- int pos;
- if (initialPos > -1)
- pos = initialPos;
- else //summon elementals depending on player side
- {
- if (attackerOwned)
- pos = 0; //top left
- else
- pos = GameConstants::BFIELD_WIDTH - 1; //top right
- }
- auto accessibility = getAccesibility();
- std::set<BattleHex> occupyable;
- for(int i = 0; i < accessibility.size(); i++)
- if(accessibility.accessible(i, twoHex, attackerOwned))
- occupyable.insert(i);
- if (!occupyable.size())
- {
- return BattleHex::INVALID; //all tiles are covered
- }
- return BattleHex::getClosestTile(attackerOwned, pos, occupyable);
- }
- std::pair< std::vector<BattleHex>, int > BattleInfo::getPath(BattleHex start, BattleHex dest, const CStack *stack)
- {
- auto reachability = getReachability(stack);
- if(reachability.predecessors[dest] == -1) //cannot reach destination
- {
- return std::make_pair(std::vector<BattleHex>(), 0);
- }
- //making the Path
- std::vector<BattleHex> path;
- BattleHex curElem = dest;
- while(curElem != start)
- {
- path.push_back(curElem);
- curElem = reachability.predecessors[curElem];
- }
- return std::make_pair(path, reachability.distances[dest]);
- }
- ui32 BattleInfo::calculateDmg( const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero,
- bool shooting, ui8 charge, bool lucky, bool deathBlow, bool ballistaDoubleDmg )
- {
- TDmgRange range = calculateDmgRange(attacker, defender, shooting, charge, lucky, deathBlow, ballistaDoubleDmg);
- if(range.first != range.second)
- {
- int valuesToAverage[10];
- int howManyToAv = std::min<ui32>(10, attacker->count);
- for (int g=0; g<howManyToAv; ++g)
- {
- valuesToAverage[g] = range.first + rand() % (range.second - range.first + 1);
- }
- return std::accumulate(valuesToAverage, valuesToAverage + howManyToAv, 0) / howManyToAv;
- }
- else
- return range.first;
- }
- void BattleInfo::calculateCasualties( std::map<ui32,si32> *casualties ) const
- {
- for(ui32 i=0; i<stacks.size();i++)//setting casualties
- {
- const CStack * const st = stacks[i];
- si32 killed = (st->alive() ? st->baseAmount - st->count : st->baseAmount);
- vstd::amax(killed, 0);
- if(killed)
- casualties[!st->attackerOwned][st->getCreature()->idNumber] += killed;
- }
- }
- int BattleInfo::calculateSpellDuration( const CSpell * spell, const CGHeroInstance * caster, int usedSpellPower)
- {
- if(!caster)
- {
- if (!usedSpellPower)
- return 3; //default duration of all creature spells
- else
- return usedSpellPower; //use creature spell power
- }
- switch(spell->id)
- {
- case Spells::FRENZY:
- return 1;
- default: //other spells
- return caster->getPrimSkillLevel(PrimarySkill::SPELL_POWER) + caster->valOfBonuses(Bonus::SPELL_DURATION);
- }
- }
- CStack * BattleInfo::generateNewStack(const CStackInstance &base, bool attackerOwned, int slot, BattleHex position) const
- {
- int stackID = getIdForNewStack();
- int owner = attackerOwned ? sides[0] : sides[1];
- assert((owner >= GameConstants::PLAYER_LIMIT) ||
- (base.armyObj && base.armyObj->tempOwner == owner));
- CStack * ret = new CStack(&base, owner, stackID, attackerOwned, slot);
- ret->position = getAvaliableHex (base.getCreatureID(), attackerOwned, position); //TODO: what if no free tile on battlefield was found?
- ret->state.insert(EBattleStackState::ALIVE); //alive state indication
- return ret;
- }
- CStack * BattleInfo::generateNewStack(const CStackBasicDescriptor &base, bool attackerOwned, int slot, BattleHex position) const
- {
- int stackID = getIdForNewStack();
- int owner = attackerOwned ? sides[0] : sides[1];
- CStack * ret = new CStack(&base, owner, stackID, attackerOwned, slot);
- ret->position = position;
- ret->state.insert(EBattleStackState::ALIVE); //alive state indication
- return ret;
- }
- ui32 BattleInfo::calculateHealedHP(const CGHeroInstance * caster, const CSpell * spell, const CStack * stack, const CStack * sacrificedStack) const
- {
- bool resurrect = resurrects(spell->id);
- int healedHealth;
- if (spell->id == Spells::SACRIFICE && sacrificedStack)
- healedHealth = (caster->getPrimSkillLevel(PrimarySkill::SPELL_POWER) + sacrificedStack->MaxHealth() + spell->powers[caster->getSpellSchoolLevel(spell)]) * sacrificedStack->count;
- else
- healedHealth = caster->getPrimSkillLevel(PrimarySkill::SPELL_POWER) * spell->power + spell->powers[caster->getSpellSchoolLevel(spell)];
- healedHealth = calculateSpellBonus(healedHealth, spell, caster, stack);
- return std::min<ui32>(healedHealth, stack->MaxHealth() - stack->firstHPleft + (resurrect ? stack->baseAmount * stack->MaxHealth() : 0));
- }
- ui32 BattleInfo::calculateHealedHP(int healedHealth, const CSpell * spell, const CStack * stack) const
- {
- bool resurrect = resurrects(spell->id);
- return std::min<ui32>(healedHealth, stack->MaxHealth() - stack->firstHPleft + (resurrect ? stack->baseAmount * stack->MaxHealth() : 0));
- }
- ui32 BattleInfo::calculateHealedHP(const CSpell * spell, int usedSpellPower, int spellSchoolLevel, const CStack * stack) const
- {
- bool resurrect = resurrects(spell->id);
- int healedHealth = usedSpellPower * spell->power + spell->powers[spellSchoolLevel];
- return std::min<ui32>(healedHealth, stack->MaxHealth() - stack->firstHPleft + (resurrect ? stack->baseAmount * stack->MaxHealth() : 0));
- }
- bool BattleInfo::resurrects(TSpell spellid) const
- {
- return VLC->spellh->spells[spellid]->isRisingSpell();
- }
- const CStack * BattleInfo::battleGetStack(BattleHex pos, bool onlyAlive)
- {
- CStack * stack = NULL;
- for(ui32 g=0; g<stacks.size(); ++g)
- {
- if(stacks[g]->position == pos
- || (stacks[g]->doubleWide()
- &&( (stacks[g]->attackerOwned && stacks[g]->position-1 == pos)
- || (!stacks[g]->attackerOwned && stacks[g]->position+1 == pos) )
- ) )
- {
- if (stacks[g]->alive())
- return stacks[g]; //we prefer living stacks - there cna be only one stack on te tile, so return it imediately
- else if (!onlyAlive)
- stack = stacks[g]; //dead stacks are only accessible when there's no alive stack on this tile
- }
- }
- return stack;
- }
- const CGHeroInstance * BattleInfo::battleGetOwner(const CStack * stack) const
- {
- return heroes[!stack->attackerOwned];
- }
- void BattleInfo::localInit()
- {
- belligerents[0]->battle = belligerents[1]->battle = this;
- BOOST_FOREACH(CArmedInstance *b, belligerents)
- b->attachTo(this);
- BOOST_FOREACH(CStack *s, stacks)
- localInitStack(s);
- exportBonuses();
- }
- void BattleInfo::localInitStack(CStack * s)
- {
- s->exportBonuses();
- if(s->base) //stack originating from "real" stack in garrison -> attach to it
- {
- s->attachTo(const_cast<CStackInstance*>(s->base));
- }
- else //attach directly to obj to which stack belongs and creature type
- {
- CArmedInstance *army = belligerents[!s->attackerOwned];
- s->attachTo(army);
- assert(s->type);
- s->attachTo(const_cast<CCreature*>(s->type));
- }
- s->postInit();
- }
- namespace CGH
- {
- using namespace std;
- static void readBattlePositions(const JsonNode &node, vector< vector<int> > & dest)
- {
- BOOST_FOREACH(const JsonNode &level, node.Vector())
- {
- std::vector<int> pom;
- BOOST_FOREACH(const JsonNode &value, level.Vector())
- {
- pom.push_back(value.Float());
- }
- dest.push_back(pom);
- }
- }
- }
- //RNG that works like H3 one
- struct RandGen
- {
- int seed;
- void srand(int s)
- {
- seed = s;
- }
- void srand(int3 pos)
- {
- srand(110291 * pos.x + 167801 * pos.y + 81569);
- }
- int rand()
- {
- seed = 214013 * seed + 2531011;
- return (seed >> 16) & 0x7FFF;
- }
- int rand(int min, int max)
- {
- if(min == max)
- return min;
- if(min > max)
- return min;
- return min + rand() % (max - min + 1);
- }
- };
- struct RangeGenerator
- {
- class ExhaustedPossibilities : public std::exception
- {
- };
- RangeGenerator(int _min, int _max, boost::function<int()> _myRand)
- {
- myRand = _myRand;
- min = _min;
- remainingCount = _max - _min + 1;
- remaining.resize(remainingCount, true);
- }
- int generateNumber()
- {
- if(!remainingCount)
- throw ExhaustedPossibilities();
- if(remainingCount == 1)
- return 0;
- return myRand() % remainingCount;
- }
- //get number fulfilling predicate. Never gives the same number twice.
- int getSuchNumber(boost::function<bool(int)> goodNumberPred = 0)
- {
- int ret = -1;
- do
- {
- int n = generateNumber();
- int i = 0;
- for(;;i++)
- {
- assert(i < (int)remaining.size());
- if(!remaining[i])
- continue;
- if(!n)
- break;
- n--;
- }
- remainingCount--;
- remaining[i] = false;
- ret = i + min;
- } while(goodNumberPred && !goodNumberPred(ret));
- return ret;
- }
- int min, remainingCount;
- std::vector<bool> remaining;
- boost::function<int()> myRand;
- };
- BattleInfo * BattleInfo::setupBattle( int3 tile, ETerrainType::ETerrainType terrain, BFieldType::BFieldType battlefieldType, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town )
- {
- CMP_stack cmpst;
- BattleInfo *curB = new BattleInfo;
- curB->castSpells[0] = curB->castSpells[1] = 0;
- curB->sides[0] = armies[0]->tempOwner;
- curB->sides[1] = armies[1]->tempOwner;
- if(curB->sides[1] == 254)
- curB->sides[1] = 255;
- std::vector<CStack*> & stacks = (curB->stacks);
- curB->tile = tile;
- curB->battlefieldType = battlefieldType;
- curB->belligerents[0] = const_cast<CArmedInstance*>(armies[0]);
- curB->belligerents[1] = const_cast<CArmedInstance*>(armies[1]);
- curB->heroes[0] = const_cast<CGHeroInstance*>(heroes[0]);
- curB->heroes[1] = const_cast<CGHeroInstance*>(heroes[1]);
- curB->round = -2;
- curB->activeStack = -1;
- curB->enchanterCounter[0] = curB->enchanterCounter[1] = 0; //ready to cast
- if(town)
- {
- curB->town = town;
- curB->siege = town->fortLevel();
- curB->terrainType = VLC->townh->factions[town->town->typeID].nativeTerrain;
- }
- else
- {
- curB->town = NULL;
- curB->siege = CGTownInstance::NONE;
- curB->terrainType = terrain;
- }
- //setting up siege obstacles
- if (town && town->hasFort())
- {
- for (int b = 0; b < ARRAY_COUNT (curB->si.wallState); ++b)
- {
- curB->si.wallState[b] = 1;
- }
- }
- //randomize obstacles
- if (town == NULL && !creatureBank) //do it only when it's not siege and not creature bank
- {
- const int ABSOLUTE_OBSTACLES_COUNT = 34, USUAL_OBSTACLES_COUNT = 91; //shouldn't be changes if we want H3-like obstacle placement
- RandGen r;
- auto ourRand = [&]{ return r.rand(); };
- r.srand(tile);
- r.rand(1,8); //battle sound ID to play... can't do anything with it here
- int tilesToBlock = r.rand(5,12);
- const int specialBattlefield = battlefieldTypeToBI(battlefieldType);
- std::vector<BattleHex> blockedTiles;
- auto appropriateAbsoluteObstacle = [&](int id)
- {
- return VLC->heroh->absoluteObstacles[id].isAppropriate(curB->terrainType, specialBattlefield);
- };
- auto appropriateUsualObstacle = [&](int id) -> bool
- {
- return VLC->heroh->obstacles[id].isAppropriate(curB->terrainType, specialBattlefield);
- };
- if(r.rand(1,100) <= 40) //put cliff-like obstacle
- {
- RangeGenerator obidgen(0, ABSOLUTE_OBSTACLES_COUNT-1, ourRand);
- try
- {
- auto obstPtr = make_shared<CObstacleInstance>();
- obstPtr->obstacleType = CObstacleInstance::ABSOLUTE_OBSTACLE;
- obstPtr->ID = obidgen.getSuchNumber(appropriateAbsoluteObstacle);
- obstPtr->uniqueID = curB->obstacles.size();
- curB->obstacles.push_back(obstPtr);
- BOOST_FOREACH(BattleHex blocked, obstPtr->getBlockedTiles())
- blockedTiles.push_back(blocked);
- tilesToBlock -= VLC->heroh->absoluteObstacles[obstPtr->ID].blockedTiles.size() / 2;
- }
- catch(RangeGenerator::ExhaustedPossibilities &)
- {
- //silently ignore, if we can't place absolute obstacle, we'll go with the usual ones
- }
- }
- RangeGenerator obidgen(0, USUAL_OBSTACLES_COUNT-1, ourRand);
- try
- {
- while(tilesToBlock > 0)
- {
- const int obid = obidgen.getSuchNumber(appropriateUsualObstacle);
- const CObstacleInfo &obi = VLC->heroh->obstacles[obid];
- auto validPosition = [&](BattleHex pos) -> bool
- {
- if(obi.height >= pos.getY())
- return false;
- if(pos.getX() == 0)
- return false;
- if(pos.getX() + obi.width > 15)
- return false;
- if(vstd::contains(blockedTiles, pos))
- return false;
- BOOST_FOREACH(BattleHex blocked, obi.getBlocked(pos))
- {
- if(vstd::contains(blockedTiles, blocked))
- return false;
- int x = blocked.getX();
- if(x <= 2 || x >= 14)
- return false;
- }
- return true;
- };
- RangeGenerator posgenerator(18, 168, ourRand);
- auto obstPtr = make_shared<CObstacleInstance>();
- obstPtr->ID = obid;
- obstPtr->pos = posgenerator.getSuchNumber(validPosition);
- obstPtr->uniqueID = curB->obstacles.size();
- curB->obstacles.push_back(obstPtr);
- BOOST_FOREACH(BattleHex blocked, obstPtr->getBlockedTiles())
- blockedTiles.push_back(blocked);
- tilesToBlock -= obi.blockedTiles.size();
- }
- }
- catch(RangeGenerator::ExhaustedPossibilities &)
- {
- }
- }
- //reading battleStartpos - add creatures AFTER random obstacles are generated
- //TODO: parse once to some structure
- std::vector< std::vector<int> > looseFormations[2], tightFormations[2], creBankFormations[2];
- std::vector <int> commanderField, commanderBank;
- const JsonNode config(ResourceID("config/battleStartpos.json"));
- const JsonVector &positions = config["battle_positions"].Vector();
- CGH::readBattlePositions(positions[0]["levels"], looseFormations[0]);
- CGH::readBattlePositions(positions[1]["levels"], looseFormations[1]);
- CGH::readBattlePositions(positions[2]["levels"], tightFormations[0]);
- CGH::readBattlePositions(positions[3]["levels"], tightFormations[1]);
- CGH::readBattlePositions(positions[4]["levels"], creBankFormations[0]);
- CGH::readBattlePositions(positions[5]["levels"], creBankFormations[1]);
- BOOST_FOREACH (auto position, config["commanderPositions"]["field"].Vector())
- {
- commanderField.push_back (position.Float());
- }
- BOOST_FOREACH (auto position, config["commanderPositions"]["creBank"].Vector())
- {
- commanderBank.push_back (position.Float());
- }
- //adding war machines
- if(!creatureBank)
- {
- //Checks if hero has artifact and create appropriate stack
- auto handleWarMachine= [&](int side, int artslot, int cretype, int hex)
- {
- if(heroes[side] && heroes[side]->getArt(artslot))
- stacks.push_back(curB->generateNewStack(CStackBasicDescriptor(cretype, 1), !side, 255, hex));
- };
- handleWarMachine(0, 13, 146, 52); //ballista
- handleWarMachine(0, 14, 148, 18); //ammo cart
- handleWarMachine(0, 15, 147, 154);//first aid tent
- if(town && town->hasFort())
- handleWarMachine(0, 16, 145, 120);//catapult
- if(!town) //defending hero shouldn't receive ballista (bug #551)
- handleWarMachine(1, 13, 146, 66); //ballista
- handleWarMachine(1, 14, 148, 32); //ammo cart
- handleWarMachine(1, 15, 147, 168); //first aid tent
- }
- //war machines added
- //battleStartpos read
- for(int side = 0; side < 2; side++)
- {
- int formationNo = armies[side]->stacksCount() - 1;
- vstd::abetween(formationNo, 0, GameConstants::ARMY_SIZE - 1);
- int k = 0; //stack serial
- for(TSlots::const_iterator i = armies[side]->Slots().begin(); i != armies[side]->Slots().end(); i++, k++)
- {
- std::vector<int> *formationVector = nullptr;
- if(creatureBank)
- formationVector = &creBankFormations[side][formationNo];
- else if(armies[side]->formation)
- formationVector = &tightFormations[side][formationNo];
- else
- formationVector = &looseFormations[side][formationNo];
- BattleHex pos = (k < formationVector->size() ? formationVector->at(k) : 0);
- if(creatureBank && i->second->type->isDoubleWide())
- pos += side ? BattleHex::LEFT : BattleHex::RIGHT;
- CStack * stack = curB->generateNewStack(*i->second, !side, i->first, pos);
- stacks.push_back(stack);
- }
- }
- //adding commanders
- for (int i = 0; i < 2; ++i)
- {
- if (heroes[i] && heroes[i]->commander)
- {
- CStack * stack = curB->generateNewStack (*heroes[i]->commander, !i, -2, //TODO: use COMMANDER_SLOT_PLACEHOLDER
- creatureBank ? commanderBank[i] : commanderField[i]);
- stacks.push_back(stack);
- }
- }
- if (curB->siege == CGTownInstance::CITADEL || curB->siege == CGTownInstance::CASTLE)
- {
- // keep tower
- CStack * stack = curB->generateNewStack(CStackBasicDescriptor(149, 1), false, 255, -2);
- stacks.push_back(stack);
- if (curB->siege == CGTownInstance::CASTLE)
- {
- // lower tower + upper tower
- CStack * stack = curB->generateNewStack(CStackBasicDescriptor(149, 1), false, 255, -4);
- stacks.push_back(stack);
- stack = curB->generateNewStack(CStackBasicDescriptor(149, 1), false, 255, -3);
- stacks.push_back(stack);
- }
- //moat
- auto moat = make_shared<MoatObstacle>();
- moat->ID = curB->town->subID;
- moat->obstacleType = CObstacleInstance::MOAT;
- moat->uniqueID = curB->obstacles.size();
- curB->obstacles.push_back(moat);
- }
- std::stable_sort(stacks.begin(),stacks.end(),cmpst);
- //spell level limiting bonus
- curB->addNewBonus(new Bonus(Bonus::ONE_BATTLE, Bonus::LEVEL_SPELL_IMMUNITY, Bonus::OTHER,
- 0, -1, -1, Bonus::INDEPENDENT_MAX));
- //giving terrain overalay premies
- int bonusSubtype = -1;
- switch(battlefieldType)
- {
- case BFieldType::MAGIC_PLAINS:
- {
- bonusSubtype = 0;
- }
- case BFieldType::FIERY_FIELDS:
- {
- if(bonusSubtype == -1) bonusSubtype = 1;
- }
- case BFieldType::ROCKLANDS:
- {
- if(bonusSubtype == -1) bonusSubtype = 8;
- }
- case BFieldType::MAGIC_CLOUDS:
- {
- if(bonusSubtype == -1) bonusSubtype = 2;
- }
- case BFieldType::LUCID_POOLS:
- {
- if(bonusSubtype == -1) bonusSubtype = 4;
- }
- { //common part for cases 9, 14, 15, 16, 17
- curB->addNewBonus(new Bonus(Bonus::ONE_BATTLE, Bonus::MAGIC_SCHOOL_SKILL, Bonus::TERRAIN_OVERLAY, 3, -1, "", bonusSubtype));
- break;
- }
- case BFieldType::HOLY_GROUND:
- {
- curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, +1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD)));
- curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, -1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL)));
- break;
- }
- case BFieldType::CLOVER_FIELD:
- { //+2 luck bonus for neutral creatures
- curB->addNewBonus(makeFeature(Bonus::LUCK, Bonus::ONE_BATTLE, 0, +2, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureFactionLimiter>(-1)));
- break;
- }
- case BFieldType::EVIL_FOG:
- {
- curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, -1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::GOOD)));
- curB->addNewBonus(makeFeature(Bonus::MORALE, Bonus::ONE_BATTLE, 0, +1, Bonus::TERRAIN_OVERLAY)->addLimiter(make_shared<CreatureAlignmentLimiter>(EAlignment::EVIL)));
- break;
- }
- case BFieldType::CURSED_GROUND:
- {
- curB->addNewBonus(makeFeature(Bonus::NO_MORALE, Bonus::ONE_BATTLE, 0, 0, Bonus::TERRAIN_OVERLAY));
- curB->addNewBonus(makeFeature(Bonus::NO_LUCK, Bonus::ONE_BATTLE, 0, 0, Bonus::TERRAIN_OVERLAY));
- Bonus * b = makeFeature(Bonus::LEVEL_SPELL_IMMUNITY, Bonus::ONE_BATTLE, GameConstants::SPELL_LEVELS, 1, Bonus::TERRAIN_OVERLAY);
- b->valType = Bonus::INDEPENDENT_MAX;
- curB->addNewBonus(b);
- break;
- }
- }
- //overlay premies given
- //native terrain bonuses
- auto nativeTerrain = make_shared<CreatureNativeTerrainLimiter>(curB->terrainType);
- curB->addNewBonus(makeFeature(Bonus::STACKS_SPEED, Bonus::ONE_BATTLE, 0, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
- curB->addNewBonus(makeFeature(Bonus::PRIMARY_SKILL, Bonus::ONE_BATTLE, PrimarySkill::ATTACK, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
- curB->addNewBonus(makeFeature(Bonus::PRIMARY_SKILL, Bonus::ONE_BATTLE, PrimarySkill::DEFENSE, 1, Bonus::TERRAIN_NATIVE)->addLimiter(nativeTerrain));
- //////////////////////////////////////////////////////////////////////////
- //tactics
- bool isTacticsAllowed = !creatureBank; //no tactics in crebanks
- int tacticLvls[2] = {0};
- for(int i = 0; i < ARRAY_COUNT(tacticLvls); i++)
- {
- if(heroes[i])
- tacticLvls[i] += heroes[i]->getSecSkillLevel(CGHeroInstance::TACTICS);
- }
- int tacticsSkillDiff = tacticLvls[0] - tacticLvls[1];
- if(tacticsSkillDiff && isTacticsAllowed)
- {
- curB->tacticsSide = tacticsSkillDiff < 0;
- curB->tacticDistance = std::abs(tacticsSkillDiff)*2 + 1;
- }
- else
- curB->tacticDistance = 0;
- // workaround bonuses affecting only enemy
- for(int i = 0; i < 2; i++)
- {
- TNodes nodes;
- curB->belligerents[i]->getRedAncestors(nodes);
- BOOST_FOREACH(CBonusSystemNode *n, nodes)
- {
- BOOST_FOREACH(Bonus *b, n->getExportedBonusList())
- {
- if(b->effectRange == Bonus::ONLY_ENEMY_ARMY/* && b->propagator && b->propagator->shouldBeAttached(curB)*/)
- {
- Bonus *bCopy = new Bonus(*b);
- bCopy->effectRange = Bonus::NO_LIMIT;
- bCopy->propagator.reset();
- bCopy->limiter.reset(new StackOwnerLimiter(curB->sides[!i]));
- curB->addNewBonus(bCopy);
- }
- }
- }
- }
- return curB;
- }
- const CGHeroInstance * BattleInfo::getHero( TPlayerColor player ) const
- {
- assert(sides[0] == player || sides[1] == player);
- if(heroes[0] && heroes[0]->getOwner() == player)
- return heroes[0];
- return heroes[1];
- }
- std::vector<ui32> BattleInfo::calculateResistedStacks(const CSpell * sp, const CGHeroInstance * caster, const CGHeroInstance * hero2, const std::set<const CStack*> affectedCreatures, int casterSideOwner, ECastingMode::ECastingMode mode, int usedSpellPower, int spellLevel) const
- {
- std::vector<ui32> ret;
- for(auto it = affectedCreatures.begin(); it != affectedCreatures.end(); ++it)
- {
- if(battleIsImmune(caster, sp, mode, (*it)->position) != ESpellCastProblem::OK) //FIXME: immune stacks should not display resisted animation
- {
- ret.push_back((*it)->ID);
- continue;
- }
- //non-negative spells should always succeed, unless immune
- if(!sp->isNegative())// && (*it)->owner == casterSideOwner)
- continue;
- /*
- const CGHeroInstance * bonusHero; //hero we should take bonuses from
- if((*it)->owner == casterSideOwner)
- bonusHero = caster;
- else
- bonusHero = hero2;*/
- int prob = (*it)->magicResistance(); //probability of resistance in %
- if(prob > 100) prob = 100;
- if(rand()%100 < prob) //immunity from resistance
- ret.push_back((*it)->ID);
- }
- if(sp->id == Spells::HYPNOTIZE) //hypnotize
- {
- for(auto it = affectedCreatures.begin(); it != affectedCreatures.end(); ++it)
- {
- if( (*it)->hasBonusOfType(Bonus::SPELL_IMMUNITY, sp->id) //100% sure spell immunity
- || ( (*it)->count - 1 ) * (*it)->MaxHealth() + (*it)->firstHPleft
- >
- usedSpellPower * 25 + sp->powers[spellLevel] //TODO: allow 'damage' bonus for hypnotize
- )
- {
- ret.push_back((*it)->ID);
- }
- }
- }
- return ret;
- }
- TPlayerColor BattleInfo::theOtherPlayer(TPlayerColor player) const
- {
- return sides[!whatSide(player)];
- }
- ui8 BattleInfo::whatSide(TPlayerColor player) const
- {
- for(int i = 0; i < ARRAY_COUNT(sides); i++)
- if(sides[i] == player)
- return i;
- tlog1 << "BattleInfo::whatSide: Player " << player << " is not in battle!\n";
- return -1;
- }
- int BattleInfo::getIdForNewStack() const
- {
- if(stacks.size())
- {
- //stacks vector may be sorted not by ID and they may be not contiguous -> find stack with max ID
- auto highestIDStack = *std::max_element(stacks.begin(), stacks.end(),
- [](const CStack *a, const CStack *b) { return a->ID < b->ID; });
- return highestIDStack->ID + 1;
- }
- return 0;
- }
- shared_ptr<CObstacleInstance> BattleInfo::getObstacleOnTile(BattleHex tile) const
- {
- BOOST_FOREACH(auto &obs, obstacles)
- if(vstd::contains(obs->getAffectedTiles(), tile))
- return obs;
- return shared_ptr<CObstacleInstance>();
- }
- BattlefieldBI::BattlefieldBI BattleInfo::battlefieldTypeToBI(BFieldType::BFieldType bfieldType)
- {
- static const std::map<BFieldType::BFieldType, BattlefieldBI::BattlefieldBI> theMap = boost::assign::map_list_of
- (BFieldType::CLOVER_FIELD, BattlefieldBI::CLOVER_FIELD)
- (BFieldType::CURSED_GROUND, BattlefieldBI::CURSED_GROUND)
- (BFieldType::EVIL_FOG, BattlefieldBI::EVIL_FOG)
- (BFieldType::FAVOURABLE_WINDS, BattlefieldBI::NONE)
- (BFieldType::FIERY_FIELDS, BattlefieldBI::FIERY_FIELDS)
- (BFieldType::HOLY_GROUND, BattlefieldBI::HOLY_GROUND)
- (BFieldType::LUCID_POOLS, BattlefieldBI::LUCID_POOLS)
- (BFieldType::MAGIC_CLOUDS, BattlefieldBI::MAGIC_CLOUDS)
- (BFieldType::MAGIC_PLAINS, BattlefieldBI::MAGIC_PLAINS)
- (BFieldType::ROCKLANDS, BattlefieldBI::ROCKLANDS)
- (BFieldType::SAND_SHORE, BattlefieldBI::COASTAL);
- auto itr = theMap.find(bfieldType);
- if(itr != theMap.end())
- return itr->second;
- return BattlefieldBI::NONE;
- }
- CStack * BattleInfo::getStack(int stackID, bool onlyAlive /*= true*/)
- {
- return const_cast<CStack *>(battleGetStackByID(stackID, onlyAlive));
- }
- CStack * BattleInfo::getStackT(BattleHex tileID, bool onlyAlive /*= true*/)
- {
- return const_cast<CStack *>(battleGetStackByPos(tileID, onlyAlive));
- }
- BattleInfo::BattleInfo()
- {
- setBattle(this);
- setNodeType(BATTLE);
- }
- CStack::CStack(const CStackInstance *Base, int O, int I, bool AO, int S)
- : base(Base), ID(I), owner(O), slot(S), attackerOwned(AO),
- counterAttacks(1)
- {
- assert(base);
- type = base->type;
- count = baseAmount = base->count;
- setNodeType(STACK_BATTLE);
- }
- CStack::CStack()
- {
- init();
- setNodeType(STACK_BATTLE);
- }
- CStack::CStack(const CStackBasicDescriptor *stack, int O, int I, bool AO, int S)
- : base(NULL), ID(I), owner(O), slot(S), attackerOwned(AO), counterAttacks(1)
- {
- type = stack->type;
- count = baseAmount = stack->count;
- setNodeType(STACK_BATTLE);
- }
- void CStack::init()
- {
- base = NULL;
- type = NULL;
- ID = -1;
- count = baseAmount = -1;
- firstHPleft = -1;
- owner = 255;
- slot = 255;
- attackerOwned = false;
- position = BattleHex();
- counterAttacks = -1;
- }
- void CStack::postInit()
- {
- assert(type);
- assert(getParentNodes().size());
- firstHPleft = MaxHealth();
- shots = getCreature()->valOfBonuses(Bonus::SHOTS);
- counterAttacks = 1 + valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
- casts = valOfBonuses(Bonus::CASTS);
- }
- ui32 CStack::Speed( int turn /*= 0*/ , bool useBind /* = false*/) const
- {
- if(hasBonus(Selector::type(Bonus::SIEGE_WEAPON) && Selector::turns(turn))) //war machines cannot move
- return 0;
- int speed = valOfBonuses(Selector::type(Bonus::STACKS_SPEED) && Selector::turns(turn));
- int percentBonus = 0;
- BOOST_FOREACH(const Bonus *b, getBonusList())
- {
- if(b->type == Bonus::STACKS_SPEED)
- {
- percentBonus += b->additionalInfo;
- }
- }
- speed = ((100 + percentBonus) * speed)/100;
- //bind effect check - doesn't influence stack initiative
- if (useBind && getEffect (Spells::BIND))
- {
- return 0;
- }
- return speed;
- }
- si32 CStack::magicResistance() const
- {
- si32 magicResistance;
- if (base) //TODO: make war machines receive aura of magic resistance
- {
- magicResistance = base->magicResistance();
- int auraBonus = 0;
- BOOST_FOREACH (const CStack * stack, base->armyObj->battle-> batteAdjacentCreatures(this))
- {
- if (stack->owner == owner)
- {
- vstd::amax(auraBonus, stack->valOfBonuses(Bonus::SPELL_RESISTANCE_AURA)); //max value
- }
- }
- magicResistance += auraBonus;
- vstd::amin (magicResistance, 100);
- }
- else
- magicResistance = type->magicResistance();
- return magicResistance;
- }
- void CStack::stackEffectToFeature(std::vector<Bonus> & sf, const Bonus & sse)
- {
- const CSpell * sp = VLC->spellh->spells[sse.sid];
- std::vector<Bonus> tmp;
- sp->getEffects(tmp, sse.val);
- BOOST_FOREACH(Bonus& b, tmp)
- {
- b.turnsRemain = sse.turnsRemain;
- sf.push_back(b);
- }
- }
- bool CStack::willMove(int turn /*= 0*/) const
- {
- return ( turn ? true : !vstd::contains(state, EBattleStackState::DEFENDING) )
- && !moved(turn)
- && canMove(turn);
- }
- bool CStack::canMove( int turn /*= 0*/ ) const
- {
- return alive()
- && !hasBonus(Selector::type(Bonus::NOT_ACTIVE) && Selector::turns(turn)); //eg. Ammo Cart or blinded creature
- }
- bool CStack::moved( int turn /*= 0*/ ) const
- {
- if(!turn)
- return vstd::contains(state, EBattleStackState::MOVED);
- else
- return false;
- }
- bool CStack::waited(int turn /*= 0*/) const
- {
- if(!turn)
- return vstd::contains(state, EBattleStackState::WAITING);
- else
- return false;
- }
- bool CStack::doubleWide() const
- {
- return getCreature()->doubleWide;
- }
- BattleHex CStack::occupiedHex() const
- {
- return occupiedHex(position);
- }
- BattleHex CStack::occupiedHex(BattleHex assumedPos) const
- {
- if (doubleWide())
- {
- if (attackerOwned)
- return assumedPos - 1;
- else
- return assumedPos + 1;
- }
- else
- {
- return BattleHex::INVALID;
- }
- }
- std::vector<BattleHex> CStack::getHexes() const
- {
- return getHexes(position);
- }
- std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos) const
- {
- return getHexes(assumedPos, doubleWide(), attackerOwned);
- }
- std::vector<BattleHex> CStack::getHexes(BattleHex assumedPos, bool twoHex, bool AttackerOwned)
- {
- std::vector<BattleHex> hexes;
- hexes.push_back(assumedPos);
- if (twoHex)
- {
- if (AttackerOwned)
- hexes.push_back(assumedPos - 1);
- else
- hexes.push_back(assumedPos + 1);
- }
- return hexes;
- }
- bool CStack::coversPos(BattleHex pos) const
- {
- return vstd::contains(getHexes(), pos);
- }
- std::vector<BattleHex> CStack::getSurroundingHexes(BattleHex attackerPos) const
- {
- BattleHex hex = (attackerPos != BattleHex::INVALID) ? attackerPos : position; //use hypothetical position
- std::vector<BattleHex> hexes;
- if (doubleWide())
- {
- const int WN = GameConstants::BFIELD_WIDTH;
- if(attackerOwned)
- { //position is equal to front hex
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN+2 : WN+1 ), hexes);
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), hexes);
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), hexes);
- BattleHex::checkAndPush(hex - 2, hexes);
- BattleHex::checkAndPush(hex + 1, hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN-2 : WN-1 ), hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), hexes);
- }
- else
- {
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), hexes);
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), hexes);
- BattleHex::checkAndPush(hex - ( (hex/WN)%2 ? WN-1 : WN-2 ), hexes);
- BattleHex::checkAndPush(hex + 2, hexes);
- BattleHex::checkAndPush(hex - 1, hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), hexes);
- BattleHex::checkAndPush(hex + ( (hex/WN)%2 ? WN+1 : WN+2 ), hexes);
- }
- return hexes;
- }
- else
- {
- return hex.neighbouringTiles();
- }
- }
- std::vector<si32> CStack::activeSpells() const
- {
- std::vector<si32> ret;
- TBonusListPtr spellEffects = getSpellBonuses();
- BOOST_FOREACH(const Bonus *it, *spellEffects)
- {
- if (!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects
- ret.push_back(it->sid);
- }
- return ret;
- }
- CStack::~CStack()
- {
- detachFromAll();
- }
- const CGHeroInstance * CStack::getMyHero() const
- {
- if(base)
- return dynamic_cast<const CGHeroInstance *>(base->armyObj);
- else //we are attached directly?
- BOOST_FOREACH(const CBonusSystemNode *n, getParentNodes())
- if(n->getNodeType() == HERO)
- return dynamic_cast<const CGHeroInstance *>(n);
- return NULL;
- }
- std::string CStack::nodeName() const
- {
- std::ostringstream oss;
- oss << "Battle stack [" << ID << "]: " << count << " creatures of ";
- if(type)
- oss << type->namePl;
- else
- oss << "[UNDEFINED TYPE]";
- oss << " from slot " << (int)slot;
- if(base && base->armyObj)
- oss << " of armyobj=" << base->armyObj->id;
- return oss.str();
- }
- void CStack::prepareAttacked(BattleStackAttacked &bsa) const
- {
- bsa.killedAmount = bsa.damageAmount / MaxHealth();
- unsigned damageFirst = bsa.damageAmount % MaxHealth();
- if (bsa.damageAmount && vstd::contains(state, EBattleStackState::CLONED)) // block ability should not kill clone (0 damage)
- {
- bsa.killedAmount = count;
- bsa.flags |= BattleStackAttacked::CLONE_KILLED;
- return; // no rebirth I believe
- }
- if( firstHPleft <= damageFirst )
- {
- bsa.killedAmount++;
- bsa.newHP = firstHPleft + MaxHealth() - damageFirst;
- }
- else
- {
- bsa.newHP = firstHPleft - damageFirst;
- }
- if(count <= bsa.killedAmount) //stack killed
- {
- bsa.newAmount = 0;
- bsa.flags |= BattleStackAttacked::KILLED;
- bsa.killedAmount = count; //we cannot kill more creatures than we have
- int resurrectFactor = valOfBonuses(Bonus::REBIRTH);
- if (resurrectFactor > 0 && casts) //there must be casts left
- {
- int resurrectedCount = base->count * resurrectFactor / 100;
- if (resurrectedCount)
- resurrectedCount += ((base->count * resurrectFactor / 100.0 - resurrectedCount) > ran()%100 / 100.0) ? 1 : 0; //last stack has proportional chance to rebirth
- else //only one unit
- resurrectedCount += ((base->count * resurrectFactor / 100.0) > ran()%100 / 100.0) ? 1 : 0;
- if (hasBonusOfType(Bonus::REBIRTH, 1))
- vstd::amax (resurrectedCount, 1); //resurrect at least one Sacred Phoenix
- if (resurrectedCount)
- {
- bsa.flags |= BattleStackAttacked::REBIRTH;
- bsa.newAmount = resurrectedCount; //risky?
- bsa.newHP = MaxHealth(); //resore full health
- }
- }
- }
- else
- {
- bsa.newAmount = count - bsa.killedAmount;
- }
- }
- bool CStack::isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos /*= BattleHex::INVALID*/, BattleHex defenderPos /*= BattleHex::INVALID*/)
- {
- if (!attackerPos.isValid())
- {
- attackerPos = attacker->position;
- }
- if (!defenderPos.isValid())
- {
- defenderPos = defender->position;
- }
- return
- (BattleHex::mutualPosition(attackerPos, defenderPos) >= 0) //front <=> front
- || (attacker->doubleWide() //back <=> front
- && BattleHex::mutualPosition(attackerPos + (attacker->attackerOwned ? -1 : 1), defenderPos) >= 0)
- || (defender->doubleWide() //front <=> back
- && BattleHex::mutualPosition(attackerPos, defenderPos + (defender->attackerOwned ? -1 : 1)) >= 0)
- || (defender->doubleWide() && attacker->doubleWide()//back <=> back
- && BattleHex::mutualPosition(attackerPos + (attacker->attackerOwned ? -1 : 1), defenderPos + (defender->attackerOwned ? -1 : 1)) >= 0);
- }
- bool CStack::ableToRetaliate() const
- {
- return alive()
- && (counterAttacks > 0 || hasBonusOfType(Bonus::UNLIMITED_RETALIATIONS))
- && !hasBonusOfType(Bonus::SIEGE_WEAPON)
- && !hasBonusOfType(Bonus::HYPNOTIZED)
- && !hasBonusOfType(Bonus::NO_RETALIATION);
- }
- std::string CStack::getName() const
- {
- return (count > 1) ? type->namePl : type->nameSing; //War machines can't use base
- }
- bool CStack::isValidTarget(bool allowDead/* = false*/) const /*alive non-turret stacks (can be attacked or be object of magic effect) */
- {
- return (alive() || allowDead) && position.isValid();
- }
- bool CStack::canBeHealed() const
- {
- return firstHPleft < MaxHealth()
- && isValidTarget()
- && !hasBonusOfType(Bonus::SIEGE_WEAPON);
- }
- bool CMP_stack::operator()( const CStack* a, const CStack* b )
- {
- switch(phase)
- {
- case 0: //catapult moves after turrets
- return a->getCreature()->idNumber > b->getCreature()->idNumber; //catapult is 145 and turrets are 149
- case 1: //fastest first, upper slot first
- {
- int as = a->Speed(turn), bs = b->Speed(turn);
- if(as != bs)
- return as > bs;
- else
- return a->slot < b->slot;
- }
- case 2: //fastest last, upper slot first
- //TODO: should be replaced with order of receiving morale!
- case 3: //fastest last, upper slot first
- {
- int as = a->Speed(turn), bs = b->Speed(turn);
- if(as != bs)
- return as < bs;
- else
- return a->slot < b->slot;
- }
- default:
- assert(0);
- return false;
- }
- }
- CMP_stack::CMP_stack( int Phase /*= 1*/, int Turn )
- {
- phase = Phase;
- turn = Turn;
- }
|