mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-06 09:09:40 +02:00
* New files for lib: CBattleCallback.cpp and CBattleCallback.h
* Updated MSVC project files * Filesystem: My version of .SND archive does not have \0 after WAV extension. Fixed (though hardcoded 3-char extension length). * New bonus types: BLOCK_MAGIC_ABOVE for blocking casting spells above given level and BLOCK_ALL_MAGIC for blocking all magic. * Heavy rewrite of battle callbacks. Fixed some minor bugs. Code reusage between lib/client/server (removed proxy calls). Better access control and support for various perspectives. * Fixed #1031 * Fixed Orb of Inhibition and Recanter's Cloak (they were incorrectly implemented). Fixed #97. * Fleeing hero won't lose artifacts. Spellbook won't be captured. Fixed #980. * Fixed crash when attacking stack dies before counterattack (ie. because of Fire Shield) * Server does some basic checks if action requests during battle are valid * Minor stuff.
This commit is contained in:
@@ -34,417 +34,6 @@
|
||||
|
||||
extern boost::rand48 ran;
|
||||
|
||||
boost::shared_mutex& CCallbackBase::getGsMutex()
|
||||
{
|
||||
return *gs->mx;
|
||||
}
|
||||
|
||||
si8 CBattleInfoCallback::battleHasDistancePenalty( const CStack * stack, BattleHex destHex )
|
||||
{
|
||||
return gs->curB->hasDistancePenalty(stack, destHex);
|
||||
}
|
||||
|
||||
si8 CBattleInfoCallback::battleHasWallPenalty( const CStack * stack, BattleHex destHex )
|
||||
{
|
||||
return gs->curB->hasWallPenalty(stack, destHex);
|
||||
}
|
||||
|
||||
si8 CBattleInfoCallback::battleCanTeleportTo(const CStack * stack, BattleHex destHex, int telportLevel)
|
||||
{
|
||||
return gs->curB->canTeleportTo(stack, destHex, telportLevel);
|
||||
}
|
||||
|
||||
std::vector<int> CBattleInfoCallback::battleGetDistances(const CStack * stack, BattleHex hex /*= BattleHex::INVALID*/, BattleHex * predecessors /*= NULL*/)
|
||||
{
|
||||
// FIXME - This method is broken, hex argument is not used. However AI depends on that wrong behaviour.
|
||||
|
||||
if(!hex.isValid())
|
||||
hex = stack->position;
|
||||
|
||||
std::vector<int> ret(GameConstants::BFIELD_SIZE, -1); //fill initial ret with -1's
|
||||
|
||||
if(!hex.isValid()) //stack has bad position? probably castle turret, return initial values (they can't move)
|
||||
return ret;
|
||||
|
||||
bool ac[GameConstants::BFIELD_SIZE] = {0};
|
||||
std::set<BattleHex> occupyable;
|
||||
gs->curB->getAccessibilityMap(ac, stack->doubleWide(), stack->attackerOwned, false, occupyable, stack->hasBonusOfType(Bonus::FLYING), stack);
|
||||
BattleHex pr[GameConstants::BFIELD_SIZE];
|
||||
int dist[GameConstants::BFIELD_SIZE];
|
||||
gs->curB->makeBFS(stack->position, ac, pr, dist, stack->doubleWide(), stack->attackerOwned, stack->hasBonusOfType(Bonus::FLYING), false);
|
||||
|
||||
for(int i=0; i<GameConstants::BFIELD_SIZE; ++i)
|
||||
{
|
||||
if(pr[i] != -1)
|
||||
ret[i] = dist[i];
|
||||
}
|
||||
|
||||
if(predecessors)
|
||||
{
|
||||
memcpy(predecessors, pr, GameConstants::BFIELD_SIZE * sizeof(BattleHex));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
std::set<BattleHex> CBattleInfoCallback::battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos /*= BattleHex::INVALID*/)
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
|
||||
tlog1 << "battleGetAttackedHexes called when there is no battle!\n";
|
||||
std::set<BattleHex> set;
|
||||
return set;
|
||||
}
|
||||
|
||||
return gs->curB->getAttackedHexes(attacker, destinationTile, attackerPos);
|
||||
}
|
||||
|
||||
ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell( const CSpell * spell )
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
|
||||
tlog1 << "battleCanCastThisSpell called when there is no battle!\n";
|
||||
return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
|
||||
}
|
||||
|
||||
return gs->curB->battleCanCastThisSpell(player, spell, ECastingMode::HERO_CASTING);
|
||||
}
|
||||
|
||||
ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell(const CSpell * spell, BattleHex destination)
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
|
||||
tlog1 << "battleCanCastThisSpell called when there is no battle!\n";
|
||||
return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
|
||||
}
|
||||
|
||||
return gs->curB->battleCanCastThisSpellHere(player, spell, ECastingMode::HERO_CASTING, destination);
|
||||
}
|
||||
|
||||
ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCreatureCastThisSpell(const CSpell * spell, BattleHex destination)
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
|
||||
tlog1 << "battleCanCastThisSpell called when there is no battle!\n";
|
||||
return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
|
||||
}
|
||||
|
||||
return gs->curB->battleCanCastThisSpellHere(player, spell, ECastingMode::CREATURE_ACTIVE_CASTING, destination);
|
||||
}
|
||||
|
||||
si32 CBattleInfoCallback::battleGetRandomStackSpell(const CStack * stack, ERandomSpell mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case RANDOM_GENIE:
|
||||
return gs->curB->getRandomBeneficialSpell(stack); //target
|
||||
break;
|
||||
case RANDOM_AIMED:
|
||||
return gs->curB->getRandomCastedSpell(stack); //caster
|
||||
break;
|
||||
default:
|
||||
tlog1 << "Incorrect mode of battleGetRandomSpell (" << mode <<")\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
si8 CBattleInfoCallback::battleGetTacticDist()
|
||||
{
|
||||
if (!gs->curB)
|
||||
{
|
||||
tlog1 << "battleGetTacticDist called when no battle!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gs->curB->sides[gs->curB->tacticsSide] == player)
|
||||
{
|
||||
return gs->curB->tacticDistance;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ui8 CBattleInfoCallback::battleGetMySide()
|
||||
{
|
||||
if (!gs->curB)
|
||||
{
|
||||
tlog1 << "battleGetMySide called when no battle!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gs->curB->sides[1] == player;
|
||||
}
|
||||
|
||||
int CBattleInfoCallback::battleGetSurrenderCost()
|
||||
{
|
||||
if (!gs->curB)
|
||||
{
|
||||
tlog1 << "battleGetSurrenderCost called when no battle!\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return gs->curB->getSurrenderingCost(player);
|
||||
}
|
||||
|
||||
int CBattleInfoCallback::battleGetBattlefieldType()
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
//return gs->battleGetBattlefieldType();
|
||||
|
||||
if(!gs->curB)
|
||||
{
|
||||
tlog2<<"battleGetBattlefieldType called when there is no battle!"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
return gs->curB->battlefieldType;
|
||||
}
|
||||
|
||||
// int CBattleInfoCallback::battleGetObstaclesAtTile(BattleHex tile) //returns bitfield
|
||||
// {
|
||||
// //TODO - write
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
std::vector<shared_ptr<const CObstacleInstance> > CBattleInfoCallback::battleGetAllObstacles()
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
std::vector<shared_ptr<const CObstacleInstance> > ret;
|
||||
if(gs->curB)
|
||||
{
|
||||
BOOST_FOREACH(auto oi, gs->curB->obstacles)
|
||||
{
|
||||
if(player < 0 || gs->curB->isObstacleVisibleForSide(*oi, battleGetMySide()))
|
||||
ret.push_back(oi);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const CStack* CBattleInfoCallback::battleGetStackByID(int ID, bool onlyAlive)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
if(!gs->curB) return NULL;
|
||||
return gs->curB->getStack(ID, onlyAlive);
|
||||
}
|
||||
|
||||
const CStack* CBattleInfoCallback::battleGetStackByPos(BattleHex pos, bool onlyAlive)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
return gs->curB->battleGetStack(pos, onlyAlive);
|
||||
}
|
||||
|
||||
BattleHex CBattleInfoCallback::battleGetPos(int stack)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
if(!gs->curB)
|
||||
{
|
||||
tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
|
||||
return BattleHex::INVALID;
|
||||
}
|
||||
for(size_t g=0; g<gs->curB->stacks.size(); ++g)
|
||||
{
|
||||
if(gs->curB->stacks[g]->ID == stack)
|
||||
return gs->curB->stacks[g]->position;
|
||||
}
|
||||
return BattleHex::INVALID;
|
||||
}
|
||||
|
||||
TStacks CBattleInfoCallback::battleGetStacks(EStackOwnership whose /*= MINE_AND_ENEMY*/, bool onlyAlive /*= true*/)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
TStacks ret;
|
||||
if(!gs->curB) //there is no battle
|
||||
{
|
||||
tlog2<<"battleGetStacks called when there is no battle!"<<std::endl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
BOOST_FOREACH(const CStack *s, gs->curB->stacks)
|
||||
{
|
||||
bool ownerMatches = (whose == MINE_AND_ENEMY) || (whose == ONLY_MINE && s->owner == player) || (whose == ONLY_ENEMY && s->owner != player);
|
||||
bool alivenessMatches = s->alive() || !onlyAlive;
|
||||
if(ownerMatches && alivenessMatches)
|
||||
ret.push_back(s);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CBattleInfoCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
|
||||
return;
|
||||
}
|
||||
gs->curB->getStackQueue(out, howMany);
|
||||
}
|
||||
|
||||
void CBattleInfoCallback::battleGetStackCountOutsideHexes(bool *ac)
|
||||
{
|
||||
if(!gs->curB)
|
||||
{
|
||||
tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
|
||||
for (int i = 0; i < GameConstants::BFIELD_SIZE; ++i) ac[i] = false;
|
||||
}
|
||||
else {
|
||||
std::set<BattleHex> ignored;
|
||||
gs->curB->getAccessibilityMap(ac, false /*ignored*/, false, false, ignored, false /*ignored*/, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<BattleHex> CBattleInfoCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
if(!gs->curB)
|
||||
{
|
||||
tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
|
||||
return std::vector<BattleHex>();
|
||||
}
|
||||
return gs->curB->getAccessibility(stack, addOccupiable, attackable);
|
||||
//return gs->battleGetRange(ID);
|
||||
}
|
||||
|
||||
bool CBattleInfoCallback::battleCanShoot(const CStack * stack, BattleHex dest)
|
||||
{
|
||||
//boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
||||
|
||||
if(!gs->curB) return false;
|
||||
|
||||
return gs->curB->battleCanShoot(stack, dest);
|
||||
}
|
||||
|
||||
bool CBattleInfoCallback::battleCanCastSpell()
|
||||
{
|
||||
if(!gs->curB) //there is no battle
|
||||
return false;
|
||||
|
||||
return gs->curB->battleCanCastSpell(player, ECastingMode::HERO_CASTING) == ESpellCastProblem::OK;
|
||||
}
|
||||
|
||||
bool CBattleInfoCallback::battleCanFlee()
|
||||
{
|
||||
return gs->curB->battleCanFlee(player);
|
||||
}
|
||||
|
||||
const CGTownInstance *CBattleInfoCallback::battleGetDefendedTown()
|
||||
{
|
||||
if(!gs->curB || gs->curB->town == NULL)
|
||||
return NULL;
|
||||
|
||||
return gs->curB->town;
|
||||
}
|
||||
|
||||
ui8 CBattleInfoCallback::battleGetWallState(int partOfWall)
|
||||
{
|
||||
if(!gs->curB || gs->curB->siege == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return gs->curB->si.wallState[partOfWall];
|
||||
}
|
||||
|
||||
int CBattleInfoCallback::battleGetWallUnderHex(BattleHex hex)
|
||||
{
|
||||
if(!gs->curB || gs->curB->siege == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return gs->curB->hexToWallPart(hex);
|
||||
}
|
||||
|
||||
TDmgRange CBattleInfoCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg)
|
||||
{
|
||||
if(!gs->curB)
|
||||
return std::make_pair(0, 0);
|
||||
|
||||
const CGHeroInstance * attackerHero, * defenderHero;
|
||||
bool shooting = battleCanShoot(attacker, defender->position);
|
||||
|
||||
if(gs->curB->sides[0] == attacker->owner)
|
||||
{
|
||||
attackerHero = gs->curB->heroes[0];
|
||||
defenderHero = gs->curB->heroes[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
attackerHero = gs->curB->heroes[1];
|
||||
defenderHero = gs->curB->heroes[0];
|
||||
}
|
||||
|
||||
TDmgRange ret = gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, shooting, 0, false, false, false);
|
||||
|
||||
if(retaliationDmg)
|
||||
{
|
||||
if(shooting)
|
||||
{
|
||||
retaliationDmg->first = retaliationDmg->second = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
|
||||
for (int i=0; i<2; ++i)
|
||||
{
|
||||
BattleStackAttacked bsa;
|
||||
bsa.damageAmount = ret.*pairElems[i];
|
||||
retaliationDmg->*pairElems[!i] = gs->curB->calculateDmgRange(defender, attacker, bsa.newAmount, attacker->count, attackerHero, defenderHero, false, 0, false, false, false).*pairElems[!i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ui8 CBattleInfoCallback::battleGetSiegeLevel()
|
||||
{
|
||||
if(!gs->curB)
|
||||
return 0;
|
||||
|
||||
return gs->curB->siege;
|
||||
}
|
||||
|
||||
const CGHeroInstance * CBattleInfoCallback::battleGetFightingHero(ui8 side) const
|
||||
{
|
||||
if(!gs->curB)
|
||||
return 0;
|
||||
|
||||
//TODO: this method should not exist... you shouldn't be able to get info about enemy hero
|
||||
return gs->curB->heroes[side];
|
||||
}
|
||||
|
||||
shared_ptr<const CObstacleInstance> CBattleInfoCallback::battleGetObstacleOnPos(BattleHex tile, bool onlyBlocking /*= true*/)
|
||||
{
|
||||
if(!gs->curB)
|
||||
return shared_ptr<const CObstacleInstance>();
|
||||
|
||||
BOOST_FOREACH(auto &obs, battleGetAllObstacles())
|
||||
{
|
||||
if(vstd::contains(obs->getBlockedTiles(), tile)
|
||||
|| (!onlyBlocking && vstd::contains(obs->getAffectedTiles(), tile)))
|
||||
{
|
||||
return obs;
|
||||
}
|
||||
}
|
||||
return shared_ptr<const CObstacleInstance>();
|
||||
}
|
||||
|
||||
int CBattleInfoCallback::battleGetMoatDmg()
|
||||
{
|
||||
if(!gs->curB || !gs->curB->town)
|
||||
return 0;
|
||||
|
||||
//TODO move to config file
|
||||
static const int dmgs[] = {70, 70, -1,
|
||||
90, 70, 90,
|
||||
70, 90, 70};
|
||||
if(gs->curB->town->subID < ARRAY_COUNT(dmgs))
|
||||
return dmgs[gs->curB->town->subID];
|
||||
return 0;
|
||||
}
|
||||
|
||||
CGameState * CPrivilagedInfoCallback::gameState ()
|
||||
{
|
||||
return gs;
|
||||
@@ -729,7 +318,7 @@ int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * ca
|
||||
ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
|
||||
//if there is a battle
|
||||
if(gs->curB)
|
||||
return gs->curB->getSpellCost(sp, caster);
|
||||
return gs->curB->battleGetSpellCost(sp, caster);
|
||||
|
||||
//if there is no battle
|
||||
return caster->getSpellCost(sp);
|
||||
@@ -784,7 +373,7 @@ bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown
|
||||
//TODO vision support
|
||||
if(town->ID == GameConstants::TOWNI_TYPE)
|
||||
dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
|
||||
else if(town->ID == 33 || town->ID == 219)
|
||||
else if(town->ID == Obj::GARRISON || town->ID == Obj::GARRISON2)
|
||||
dest.initFromArmy(static_cast<const CArmedInstance *>(town), detailed);
|
||||
else
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user