2012-12-19 17:54:10 +03:00
/*
2017-07-13 10:26:03 +02:00
* CBattleInfoCallback . cpp , part of VCMI engine
2012-12-19 17:54:10 +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
*
*/
2017-07-01 20:05:10 +02:00
# include "StdInc.h"
2017-06-24 15:51:07 +02:00
# include "CBattleInfoCallback.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
# include <vcmi/scripting/Service.h>
2017-06-29 01:02:05 +02:00
# include "../CStack.h"
2017-06-24 15:51:07 +02:00
# include "BattleInfo.h"
2017-06-29 01:02:05 +02:00
# include "../NetPacks.h"
# include "../spells/CSpellHandler.h"
# include "../mapObjects/CGTownInstance.h"
2022-06-28 10:05:30 +02:00
# include "../BattleFieldHandler.h"
2022-12-31 17:25:40 +02:00
# include "../CModHandler.h"
2012-12-19 17:54:10 +03:00
2022-07-26 15:07:42 +02:00
VCMI_LIB_NAMESPACE_BEGIN
2017-06-26 18:50:35 +02:00
namespace SiegeStuffThatShouldBeMovedToHandlers // <=== TODO
2012-08-26 12:07:48 +03:00
{
2023-01-12 23:52:03 +02:00
2018-02-10 20:52:23 +02:00
static void retrieveTurretDamageRange ( const CGTownInstance * town , const battle : : Unit * turret , double & outMinDmg , double & outMaxDmg ) //does not match OH3 yet, but damage is somewhat close
2014-05-17 12:37:12 +03:00
{
2023-01-12 23:52:03 +02:00
// http://heroes.thelazy.net/wiki/Arrow_tower
2017-07-20 06:08:49 +02:00
assert ( turret - > creatureIndex ( ) = = CreatureID : : ARROW_TOWERS ) ;
2017-06-24 15:51:07 +02:00
assert ( town ) ;
2017-07-20 06:08:49 +02:00
assert ( turret - > getPosition ( ) > = - 4 & & turret - > getPosition ( ) < = - 2 ) ;
2016-02-01 19:03:57 +02:00
2023-01-12 23:52:03 +02:00
// base damage, irregardless of town level
static const int baseDamageKeep = 10 ;
static const int baseDamageTower = 6 ;
// extra damage, for each building in town
static const int extraDamage = 2 ;
const int townLevel = town - > getTownLevel ( ) ;
2017-11-18 11:41:07 +02:00
2023-01-12 23:52:03 +02:00
int minDamage ;
2012-08-26 12:07:48 +03:00
2023-01-12 23:52:03 +02:00
if ( turret - > getPosition ( ) = = BattleHex : : CASTLE_CENTRAL_TOWER )
minDamage = baseDamageKeep + townLevel * extraDamage ;
else
minDamage = baseDamageTower + townLevel / 2 * extraDamage ;
2012-08-26 12:07:48 +03:00
2023-01-12 23:52:03 +02:00
outMinDmg = minDamage ;
outMaxDmg = minDamage * 2 ;
2012-08-26 12:07:48 +03:00
}
2017-06-24 15:51:07 +02:00
static BattleHex lineToWallHex ( int line ) //returns hex with wall in given line (y coordinate)
2012-08-26 12:07:48 +03:00
{
2023-01-13 01:59:09 +02:00
static const BattleHex lineToHex [ ] = { 12 , 29 , 45 , 62 , 78 , 96 , 112 , 130 , 147 , 165 , 182 } ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
return lineToHex [ line ] ;
2012-08-26 12:07:48 +03:00
}
2017-06-24 15:51:07 +02:00
static bool sameSideOfWall ( BattleHex pos1 , BattleHex pos2 )
2012-08-26 12:07:48 +03:00
{
2017-06-24 15:51:07 +02:00
const int wallInStackLine = lineToWallHex ( pos1 . getY ( ) ) ;
const int wallInDestLine = lineToWallHex ( pos2 . getY ( ) ) ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
const bool stackLeft = pos1 < wallInStackLine ;
const bool destLeft = pos2 < wallInDestLine ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
return stackLeft = = destLeft ;
2012-08-26 12:07:48 +03:00
}
2017-06-24 15:51:07 +02:00
// parts of wall
2023-01-13 00:35:58 +02:00
static const std : : pair < int , EWallPart > wallParts [ ] =
2012-08-26 12:07:48 +03:00
{
2017-06-26 18:50:35 +02:00
std : : make_pair ( 50 , EWallPart : : KEEP ) ,
2017-06-24 15:51:07 +02:00
std : : make_pair ( 183 , EWallPart : : BOTTOM_TOWER ) ,
std : : make_pair ( 182 , EWallPart : : BOTTOM_WALL ) ,
std : : make_pair ( 130 , EWallPart : : BELOW_GATE ) ,
2017-06-26 18:50:35 +02:00
std : : make_pair ( 78 , EWallPart : : OVER_GATE ) ,
std : : make_pair ( 29 , EWallPart : : UPPER_WALL ) ,
std : : make_pair ( 12 , EWallPart : : UPPER_TOWER ) ,
std : : make_pair ( 95 , EWallPart : : INDESTRUCTIBLE_PART_OF_GATE ) ,
std : : make_pair ( 96 , EWallPart : : GATE ) ,
std : : make_pair ( 45 , EWallPart : : INDESTRUCTIBLE_PART ) ,
std : : make_pair ( 62 , EWallPart : : INDESTRUCTIBLE_PART ) ,
2017-06-24 15:51:07 +02:00
std : : make_pair ( 112 , EWallPart : : INDESTRUCTIBLE_PART ) ,
std : : make_pair ( 147 , EWallPart : : INDESTRUCTIBLE_PART ) ,
std : : make_pair ( 165 , EWallPart : : INDESTRUCTIBLE_PART )
} ;
2012-08-26 12:07:48 +03:00
2023-01-13 00:35:58 +02:00
static EWallPart hexToWallPart ( BattleHex hex )
2013-07-22 01:01:29 +03:00
{
2017-06-24 15:51:07 +02:00
for ( auto & elem : wallParts )
2013-07-22 01:01:29 +03:00
{
2017-06-24 15:51:07 +02:00
if ( elem . first = = hex )
return elem . second ;
2013-07-22 01:01:29 +03:00
}
2017-06-24 15:51:07 +02:00
return EWallPart : : INVALID ; //not found!
2012-08-26 12:07:48 +03:00
}
2023-01-13 00:35:58 +02:00
static BattleHex WallPartToHex ( EWallPart part )
2013-01-20 23:29:35 +03:00
{
2017-06-24 15:51:07 +02:00
for ( auto & elem : wallParts )
2013-06-23 14:25:48 +03:00
{
2017-06-24 15:51:07 +02:00
if ( elem . second = = part )
return elem . first ;
2013-06-23 14:25:48 +03:00
}
2013-01-20 23:29:35 +03:00
2017-06-24 15:51:07 +02:00
return BattleHex : : INVALID ; //not found!
2013-01-20 23:29:35 +03:00
}
2012-08-26 12:07:48 +03:00
}
2017-06-24 15:51:07 +02:00
using namespace SiegeStuffThatShouldBeMovedToHandlers ;
2015-09-16 17:28:14 +02:00
2017-07-20 06:08:49 +02:00
ESpellCastProblem : : ESpellCastProblem CBattleInfoCallback : : battleCanCastSpell ( const spells : : Caster * caster , spells : : Mode mode ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( ESpellCastProblem : : INVALID ) ;
2016-09-18 17:12:07 +02:00
if ( caster = = nullptr )
{
2017-06-26 18:50:35 +02:00
logGlobal - > error ( " CBattleInfoCallback::battleCanCastSpell: no spellcaster. " ) ;
2016-09-18 17:12:07 +02:00
return ESpellCastProblem : : INVALID ;
}
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 PlayerColor player = caster - > getCasterOwner ( ) ;
2017-07-01 10:34:00 +02:00
const auto side = playerToSide ( player ) ;
if ( ! side )
2016-11-26 20:14:21 +02:00
return ESpellCastProblem : : INVALID ;
2017-07-01 10:34:00 +02:00
if ( ! battleDoWeKnowAbout ( side . get ( ) ) )
2012-08-26 12:07:48 +03:00
{
2017-08-10 18:39:27 +02:00
logGlobal - > warn ( " You can't check if enemy can cast given spell! " ) ;
2012-08-26 12:07:48 +03:00
return ESpellCastProblem : : INVALID ;
}
2016-09-18 17:12:07 +02:00
if ( battleTacticDist ( ) )
return ESpellCastProblem : : ONGOING_TACTIC_PHASE ;
2017-07-20 06:08:49 +02:00
switch ( mode )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
case spells : : Mode : : HERO :
2017-06-24 15:51:07 +02:00
{
2017-07-01 10:34:00 +02:00
if ( battleCastSpells ( side . get ( ) ) > 0 )
2017-07-20 06:08:49 +02:00
return ESpellCastProblem : : CASTS_PER_TURN_LIMIT ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
auto hero = dynamic_cast < const CGHeroInstance * > ( caster ) ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
if ( ! hero )
return ESpellCastProblem : : NO_HERO_TO_CAST_SPELL ;
if ( hero - > hasBonusOfType ( Bonus : : BLOCK_ALL_MAGIC ) )
return ESpellCastProblem : : MAGIC_IS_BLOCKED ;
}
2012-08-26 12:07:48 +03:00
break ;
default :
break ;
}
return ESpellCastProblem : : OK ;
}
2023-01-13 01:59:09 +02:00
struct Point
{
int x , y ;
} ;
/// Algorithm to test whether line segment between points line1-line2 will intersect with
2023-01-14 23:01:33 +02:00
/// rectangle specified by top-left and bottom-right points
2023-01-13 01:59:09 +02:00
/// Note that in order to avoid floating point rounding errors algorithm uses integers with no divisions
2023-01-14 23:01:33 +02:00
static bool intersectionSegmentRect ( Point line1 , Point line2 , Point rectTL , Point rectBR )
2023-01-13 01:59:09 +02:00
{
2023-01-14 23:01:33 +02:00
assert ( rectTL . x < rectBR . x ) ;
assert ( rectTL . y < rectBR . y ) ;
2023-01-13 01:59:09 +02:00
// check whether segment is located to the left of our AABB
2023-01-14 23:01:33 +02:00
if ( line1 . x < rectTL . x & & line2 . x < rectTL . x )
2023-01-13 01:59:09 +02:00
return false ;
// check whether segment is located to the right of our AABB
2023-01-14 23:01:33 +02:00
if ( line1 . x > rectBR . x & & line2 . x > rectBR . x )
2023-01-13 01:59:09 +02:00
return false ;
// check whether segment is located on top of our AABB
2023-01-14 23:01:33 +02:00
if ( line1 . y < rectTL . y & & line2 . y < rectTL . y )
2023-01-13 01:59:09 +02:00
return false ;
// check whether segment is located below of our AABB
2023-01-14 23:01:33 +02:00
if ( line1 . y > rectBR . y & & line2 . y > rectBR . y )
2023-01-13 01:59:09 +02:00
return false ;
Point vector { line2 . x - line1 . x , line2 . y - line1 . y } ;
// compute position of AABB corners relative to our line
2023-01-14 23:01:33 +02:00
int tlTest = vector . y * rectTL . x - vector . x * rectTL . y + ( line2 . x * line1 . y - line1 . x * line2 . y ) ;
int trTest = vector . y * rectBR . x - vector . x * rectTL . y + ( line2 . x * line1 . y - line1 . x * line2 . y ) ;
int blTest = vector . y * rectTL . x - vector . x * rectBR . y + ( line2 . x * line1 . y - line1 . x * line2 . y ) ;
int brTest = vector . y * rectBR . x - vector . x * rectBR . y + ( line2 . x * line1 . y - line1 . x * line2 . y ) ;
2023-01-13 01:59:09 +02:00
// if all points are on the left of our line then there is no intersection
if ( tlTest > 0 & & trTest > 0 & & blTest > 0 & & brTest > 0 )
return false ;
// if all points are on the right of our line then there is no intersection
if ( tlTest < 0 & & trTest < 0 & & blTest < 0 & & brTest < 0 )
return false ;
// if all previous checks failed, this means that there is an intersection between line and AABB
return true ;
}
2017-07-20 06:08:49 +02:00
bool CBattleInfoCallback : : battleHasWallPenalty ( const IBonusBearer * shooter , BattleHex shooterPosition , BattleHex destHex ) const
2012-08-26 12:07:48 +03:00
{
2023-01-13 01:59:09 +02:00
auto isTileBlocked = [ & ] ( BattleHex tile )
{
EWallPart wallPart = battleHexToWallPart ( tile ) ;
if ( wallPart = = EWallPart : : INDESTRUCTIBLE_PART_OF_GATE )
return false ; // does not blocks ranged attacks
if ( wallPart = = EWallPart : : INDESTRUCTIBLE_PART )
return true ; // always blocks ranged attacks
assert ( isWallPartPotentiallyAttackable ( wallPart ) ) ;
EWallState state = battleGetWallState ( wallPart ) ;
return state ! = EWallState : : DESTROYED ;
} ;
auto needWallPenalty = [ & ] ( BattleHex from , BattleHex dest )
{
2023-01-14 23:01:33 +02:00
// arbitrary selected cell size for virtual grid
// any even number can be selected (for division by two)
static const int cellSize = 10 ;
// create line that goes from center of shooter cell to center of target cell
Point line1 { from . getX ( ) * cellSize + cellSize / 2 , from . getY ( ) * cellSize + cellSize / 2 } ;
Point line2 { dest . getX ( ) * cellSize + cellSize / 2 , dest . getY ( ) * cellSize + cellSize / 2 } ;
2023-01-13 01:59:09 +02:00
for ( int y = 0 ; y < GameConstants : : BFIELD_HEIGHT ; + + y )
{
BattleHex obstacle = lineToWallHex ( y ) ;
if ( ! isTileBlocked ( obstacle ) )
continue ;
2023-01-14 23:01:33 +02:00
// create rect around cell with an obstacle
Point rectTL { obstacle . getX ( ) * cellSize , obstacle . getY ( ) * cellSize } ;
Point recrBR { obstacle . getX ( ) * ( cellSize + 1 ) , obstacle . getY ( ) * ( cellSize + 1 ) } ;
2023-01-13 01:59:09 +02:00
2023-01-14 23:01:33 +02:00
if ( intersectionSegmentRect ( line1 , line2 , rectTL , recrBR ) )
2023-01-13 01:59:09 +02:00
return true ;
}
return false ;
} ;
2012-08-26 12:07:48 +03:00
RETURN_IF_NOT_BATTLE ( false ) ;
2017-07-20 06:08:49 +02:00
if ( ! battleGetSiegeLevel ( ) )
return false ;
const std : : string cachingStrNoWallPenalty = " type_NO_WALL_PENALTY " ;
2020-11-11 21:43:40 +02:00
static const auto selectorNoWallPenalty = Selector : : type ( ) ( Bonus : : NO_WALL_PENALTY ) ;
2017-07-20 06:08:49 +02:00
if ( shooter - > hasBonus ( selectorNoWallPenalty , cachingStrNoWallPenalty ) )
2012-08-26 12:07:48 +03:00
return false ;
2012-09-20 19:55:21 +03:00
const int wallInStackLine = lineToWallHex ( shooterPosition . getY ( ) ) ;
2023-01-13 01:59:09 +02:00
const bool shooterOutsideWalls = shooterPosition < wallInStackLine ;
2012-08-26 12:07:48 +03:00
2023-01-13 01:59:09 +02:00
return shooterOutsideWalls & & needWallPenalty ( shooterPosition , destHex ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
si8 CBattleInfoCallback : : battleCanTeleportTo ( const battle : : Unit * stack , BattleHex destHex , int telportLevel ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( false ) ;
2013-07-25 14:53:36 +03:00
if ( ! getAccesibility ( stack ) . accessible ( destHex , stack ) )
2012-08-26 12:07:48 +03:00
return false ;
2016-09-24 08:27:58 +02:00
const ui8 siegeLevel = battleGetSiegeLevel ( ) ;
//check for wall
//advanced teleport can pass wall of fort|citadel, expert - of castle
if ( ( siegeLevel > CGTownInstance : : NONE & & telportLevel < 2 ) | | ( siegeLevel > = CGTownInstance : : CASTLE & & telportLevel < 3 ) )
2017-07-20 06:08:49 +02:00
return sameSideOfWall ( stack - > getPosition ( ) , destHex ) ;
2012-08-26 12:07:48 +03:00
return true ;
}
2019-05-04 05:42:55 +02:00
std : : vector < PossiblePlayerBattleAction > CBattleInfoCallback : : getClientActionsForStack ( const CStack * stack , const BattleClientInterfaceData & data )
{
RETURN_IF_NOT_BATTLE ( std : : vector < PossiblePlayerBattleAction > ( ) ) ;
std : : vector < PossiblePlayerBattleAction > allowedActionList ;
if ( data . tacticsMode ) //would "if(battleGetTacticDist() > 0)" work?
{
allowedActionList . push_back ( PossiblePlayerBattleAction : : MOVE_TACTICS ) ;
allowedActionList . push_back ( PossiblePlayerBattleAction : : CHOOSE_TACTICS_STACK ) ;
}
else
{
if ( stack - > canCast ( ) ) //TODO: check for battlefield effects that prevent casting?
{
if ( stack - > hasBonusOfType ( Bonus : : SPELLCASTER ) & & data . creatureSpellToCast ! = - 1 )
{
const CSpell * spell = SpellID ( data . creatureSpellToCast ) . toSpell ( ) ;
PossiblePlayerBattleAction act = getCasterAction ( spell , stack , spells : : Mode : : CREATURE_ACTIVE ) ;
allowedActionList . push_back ( act ) ;
}
if ( stack - > hasBonusOfType ( Bonus : : RANDOM_SPELLCASTER ) )
allowedActionList . push_back ( PossiblePlayerBattleAction : : RANDOM_GENIE_SPELL ) ;
}
if ( stack - > canShoot ( ) )
allowedActionList . push_back ( PossiblePlayerBattleAction : : SHOOT ) ;
if ( stack - > hasBonusOfType ( Bonus : : RETURN_AFTER_STRIKE ) )
allowedActionList . push_back ( PossiblePlayerBattleAction : : ATTACK_AND_RETURN ) ;
allowedActionList . push_back ( PossiblePlayerBattleAction : : ATTACK ) ; //all active stacks can attack
allowedActionList . push_back ( PossiblePlayerBattleAction : : WALK_AND_ATTACK ) ; //not all stacks can always walk, but we will check this elsewhere
if ( stack - > canMove ( ) & & stack - > Speed ( 0 , true ) ) //probably no reason to try move war machines or bound stacks
allowedActionList . push_back ( PossiblePlayerBattleAction : : MOVE_STACK ) ;
auto siegedTown = battleGetDefendedTown ( ) ;
if ( siegedTown & & siegedTown - > hasFort ( ) & & stack - > hasBonusOfType ( Bonus : : CATAPULT ) ) //TODO: check shots
allowedActionList . push_back ( PossiblePlayerBattleAction : : CATAPULT ) ;
if ( stack - > hasBonusOfType ( Bonus : : HEALER ) )
allowedActionList . push_back ( PossiblePlayerBattleAction : : HEAL ) ;
}
return allowedActionList ;
}
PossiblePlayerBattleAction CBattleInfoCallback : : getCasterAction ( const CSpell * spell , const spells : : Caster * caster , spells : : Mode mode ) const
{
RETURN_IF_NOT_BATTLE ( PossiblePlayerBattleAction : : INVALID ) ;
PossiblePlayerBattleAction spellSelMode = PossiblePlayerBattleAction : : ANY_LOCATION ;
const CSpell : : TargetInfo ti ( spell , caster - > getSpellSchoolLevel ( spell ) , mode ) ;
if ( ti . massive | | ti . type = = spells : : AimType : : NO_TARGET )
spellSelMode = PossiblePlayerBattleAction : : NO_LOCATION ;
else if ( ti . type = = spells : : AimType : : LOCATION & & ti . clearAffected )
spellSelMode = PossiblePlayerBattleAction : : FREE_LOCATION ;
else if ( ti . type = = spells : : AimType : : CREATURE )
spellSelMode = PossiblePlayerBattleAction : : AIMED_SPELL_CREATURE ;
else if ( ti . type = = spells : : AimType : : OBSTACLE )
spellSelMode = PossiblePlayerBattleAction : : OBSTACLE ;
return spellSelMode ;
}
2017-07-15 13:08:20 +02:00
std : : set < BattleHex > CBattleInfoCallback : : battleGetAttackedHexes ( const CStack * attacker , BattleHex destinationTile , BattleHex attackerPos ) const
2012-08-26 12:07:48 +03:00
{
std : : set < BattleHex > attackedHexes ;
RETURN_IF_NOT_BATTLE ( attackedHexes ) ;
AttackableTiles at = getPotentiallyAttackableHexes ( attacker , destinationTile , attackerPos ) ;
2013-06-29 16:05:48 +03:00
for ( BattleHex tile : at . hostileCreaturePositions )
2012-08-26 12:07:48 +03:00
{
const CStack * st = battleGetStackByPos ( tile , true ) ;
if ( st & & st - > owner ! = attacker - > owner ) //only hostile stacks - does it work well with Berserk?
{
attackedHexes . insert ( tile ) ;
}
}
2013-06-29 16:05:48 +03:00
for ( BattleHex tile : at . friendlyCreaturePositions )
2012-08-26 12:07:48 +03:00
{
2012-08-26 12:59:07 +03:00
if ( battleGetStackByPos ( tile , true ) ) //friendly stacks can also be damaged by Dragon Breath
2012-08-26 12:07:48 +03:00
{
attackedHexes . insert ( tile ) ;
}
}
return attackedHexes ;
}
2016-09-09 19:30:36 +02:00
SpellID CBattleInfoCallback : : battleGetRandomStackSpell ( CRandomGenerator & rand , const CStack * stack , ERandomSpell mode ) const
2012-08-26 12:07:48 +03:00
{
switch ( mode )
{
case RANDOM_GENIE :
2016-09-09 19:30:36 +02:00
return getRandomBeneficialSpell ( rand , stack ) ; //target
2012-08-26 12:07:48 +03:00
break ;
case RANDOM_AIMED :
2016-09-09 19:30:36 +02:00
return getRandomCastedSpell ( rand , stack ) ; //caster
2012-08-26 12:07:48 +03:00
break ;
default :
2017-08-12 14:43:41 +02:00
logGlobal - > error ( " Incorrect mode of battleGetRandomSpell (%d) " , static_cast < int > ( mode ) ) ;
2013-02-14 02:55:42 +03:00
return SpellID : : NONE ;
2012-08-26 12:07:48 +03:00
}
}
const CStack * CBattleInfoCallback : : battleGetStackByPos ( BattleHex pos , bool onlyAlive ) const
{
RETURN_IF_NOT_BATTLE ( nullptr ) ;
2013-09-27 22:42:17 +03:00
for ( auto s : battleGetAllStacks ( true ) )
2013-07-27 22:01:31 +03:00
if ( vstd : : contains ( s - > getHexes ( ) , pos ) & & ( ! onlyAlive | | s - > alive ( ) ) )
2017-06-24 15:51:07 +02:00
return s ;
2012-08-26 12:07:48 +03:00
return nullptr ;
}
2017-07-20 06:08:49 +02:00
const battle : : Unit * CBattleInfoCallback : : battleGetUnitByPos ( BattleHex pos , bool onlyAlive ) const
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
RETURN_IF_NOT_BATTLE ( nullptr ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
auto ret = battleGetUnitsIf ( [ = ] ( const battle : : Unit * unit )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
return ! unit - > isGhost ( )
& & vstd : : contains ( battle : : Unit : : getHexes ( unit - > getPosition ( ) , unit - > doubleWide ( ) , unit - > unitSide ( ) ) , pos )
& & ( ! onlyAlive | | unit - > alive ( ) ) ;
} ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
if ( ! ret . empty ( ) )
return ret . front ( ) ;
else
return nullptr ;
}
battle : : Units CBattleInfoCallback : : battleAliveUnits ( ) const
{
return battleGetUnitsIf ( [ ] ( const battle : : Unit * unit )
{
return unit - > isValidTarget ( false ) ;
} ) ;
}
battle : : Units CBattleInfoCallback : : battleAliveUnits ( ui8 side ) const
{
return battleGetUnitsIf ( [ = ] ( const battle : : Unit * unit )
{
return unit - > isValidTarget ( false ) & & unit - > unitSide ( ) = = side ;
} ) ;
}
//T is battle::Unit descendant
template < typename T >
2019-05-16 15:41:02 +02:00
const T * takeOneUnit ( std : : vector < const T * > & all , const int turn , int8_t & lastMoved , int phase )
2017-07-20 06:08:49 +02:00
{
2019-05-17 10:34:59 +02:00
const T * returnedUnit = nullptr ;
size_t currentUnitIndex = 0 ;
2012-08-26 12:07:48 +03:00
2019-05-17 10:34:59 +02:00
for ( size_t i = 0 ; i < all . size ( ) ; i + + )
2017-07-20 06:08:49 +02:00
{
2019-05-17 10:34:59 +02:00
int32_t currentUnitSpeed = - 1 ;
int32_t returnedUnitSpeed = - 1 ;
if ( returnedUnit )
returnedUnitSpeed = returnedUnit - > getInitiative ( turn ) ;
2019-05-16 15:41:02 +02:00
if ( all [ i ] )
2012-08-26 12:07:48 +03:00
{
2019-05-17 10:34:59 +02:00
currentUnitSpeed = all [ i ] - > getInitiative ( turn ) ;
2019-05-18 18:37:02 +02:00
switch ( phase )
2019-05-16 15:41:02 +02:00
{
2019-05-17 10:34:59 +02:00
case 1 : // Faster first, attacker priority, higher slot first
if ( returnedUnit = = nullptr | | currentUnitSpeed > returnedUnitSpeed )
2019-05-16 15:41:02 +02:00
{
2019-05-17 10:34:59 +02:00
returnedUnit = all [ i ] ;
currentUnitIndex = i ;
2019-05-16 15:41:02 +02:00
}
2019-05-17 10:34:59 +02:00
else if ( currentUnitSpeed = = returnedUnitSpeed )
2019-05-16 15:41:02 +02:00
{
2019-05-17 10:34:59 +02:00
if ( lastMoved = = - 1 & & turn < = 0 & & all [ i ] - > unitSide ( ) = = BattleSide : : ATTACKER
& & ! ( returnedUnit - > unitSide ( ) = = all [ i ] - > unitSide ( ) & & returnedUnit - > unitSlot ( ) < all [ i ] - > unitSlot ( ) ) ) // Turn 0 attacker priority
{
returnedUnit = all [ i ] ;
currentUnitIndex = i ;
}
else if ( lastMoved ! = - 1 & & all [ i ] - > unitSide ( ) ! = lastMoved
& & ! ( returnedUnit - > unitSide ( ) = = all [ i ] - > unitSide ( ) & & returnedUnit - > unitSlot ( ) < all [ i ] - > unitSlot ( ) ) ) // Alternate equal speeds units
{
returnedUnit = all [ i ] ;
currentUnitIndex = i ;
}
2019-05-16 15:41:02 +02:00
}
2019-05-17 10:34:59 +02:00
break ;
case 2 : // Slower first, higher slot first
case 3 :
if ( returnedUnit = = nullptr | | currentUnitSpeed < returnedUnitSpeed )
{
returnedUnit = all [ i ] ;
currentUnitIndex = i ;
}
else if ( currentUnitSpeed = = returnedUnitSpeed & & lastMoved ! = - 1 & & all [ i ] - > unitSide ( ) ! = lastMoved
& & ! ( returnedUnit - > unitSide ( ) = = all [ i ] - > unitSide ( ) & & returnedUnit - > unitSlot ( ) < all [ i ] - > unitSlot ( ) ) ) // Alternate equal speeds units
{
returnedUnit = all [ i ] ;
currentUnitIndex = i ;
}
break ;
default :
break ;
2019-05-16 15:41:02 +02:00
}
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
}
2012-08-26 12:07:48 +03:00
2019-05-17 10:34:59 +02:00
if ( ! returnedUnit )
2019-05-16 15:41:02 +02:00
return nullptr ;
2019-05-17 10:34:59 +02:00
all [ currentUnitIndex ] = nullptr ;
return returnedUnit ;
2017-07-20 06:08:49 +02:00
}
void CBattleInfoCallback : : battleGetTurnOrder ( std : : vector < battle : : Units > & out , const size_t maxUnits , const int maxTurns , const int turn , int8_t lastMoved ) const
{
RETURN_IF_NOT_BATTLE ( ) ;
if ( maxUnits = = 0 & & maxTurns = = 0 )
{
logGlobal - > error ( " Attempt to get infinite battle queue " ) ;
return ;
}
auto actualTurn = turn > 0 ? turn : 0 ;
auto outputFull = [ & ] ( ) - > bool
{
if ( maxUnits = = 0 )
return false ; //no limit
size_t outSize = 0 ;
for ( const auto & oneTurn : out )
outSize + = oneTurn . size ( ) ;
return outSize > = maxUnits ;
2012-08-26 12:07:48 +03:00
} ;
2017-07-20 06:08:49 +02:00
out . emplace_back ( ) ;
2012-08-26 12:07:48 +03:00
//We'll split creatures with remaining movement to 4 buckets
2012-08-26 12:59:07 +03:00
// [0] - turrets/catapult,
// [1] - normal (unmoved) creatures, other war machines,
// [2] - waited cres that had morale,
2012-08-26 12:07:48 +03:00
// [3] - rest of waited cres
2017-07-20 06:08:49 +02:00
std : : array < battle : : Units , 4 > phase ;
const battle : : Unit * active = battleActiveUnit ( ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
if ( active )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
//its first turn and active unit hasn't taken any action yet - must be placed at the beginning of queue, no matter what
if ( turn = = 0 & & active - > willMove ( ) & & ! active - > waited ( ) )
{
out . back ( ) . push_back ( active ) ;
if ( outputFull ( ) )
return ;
}
//its first or current turn, turn priority for active stack side
//TODO: what if active stack mind-controlled?
if ( turn < = 0 & & lastMoved < 0 )
lastMoved = active - > unitSide ( ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
auto all = battleGetUnitsIf ( [ ] ( const battle : : Unit * unit )
2012-08-31 19:33:30 +03:00
{
2017-07-20 06:08:49 +02:00
return ! unit - > isGhost ( ) ;
} ) ;
if ( ! vstd : : contains_if ( all , [ ] ( const battle : : Unit * unit ) { return unit - > willMove ( 100000 ) ; } ) ) //little evil, but 100000 should be enough for all effects to disappear
{
//No unit will be able to move, battle is over.
2012-08-31 19:33:30 +03:00
out . clear ( ) ;
return ;
}
2017-07-20 06:08:49 +02:00
for ( auto one : all )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
if ( ( actualTurn = = 0 & & ! one - > willMove ( ) ) //we are considering current round and unit won't move
| | ( actualTurn > 0 & & ! one - > canMove ( turn ) ) //unit won't be able to move in later rounds
| | ( actualTurn = = 0 & & one = = active & & ! out . at ( 0 ) . empty ( ) & & one = = out . front ( ) . front ( ) ) ) //it's active unit already added at the beginning of queue
2012-08-26 12:07:48 +03:00
{
continue ;
}
2017-07-20 06:08:49 +02:00
int p = one - > battleQueuePhase ( turn ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
phase [ p ] . push_back ( one ) ;
2012-08-26 12:07:48 +03:00
}
2019-05-16 15:41:02 +02:00
boost : : sort ( phase [ 0 ] , CMP_stack ( 0 , actualTurn , lastMoved ) ) ;
2017-07-20 06:08:49 +02:00
std : : copy ( phase [ 0 ] . begin ( ) , phase [ 0 ] . end ( ) , std : : back_inserter ( out . back ( ) ) ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
if ( outputFull ( ) )
2012-08-26 12:07:48 +03:00
return ;
2017-07-20 06:08:49 +02:00
for ( int i = 1 ; i < 4 ; i + + )
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
boost : : sort ( phase [ i ] , CMP_stack ( i , actualTurn , lastMoved ) ) ;
2012-08-26 12:07:48 +03:00
int pi = 1 ;
2017-07-20 06:08:49 +02:00
while ( ! outputFull ( ) & & pi < 4 )
2012-08-26 12:07:48 +03:00
{
2019-05-16 15:41:02 +02:00
const battle : : Unit * current = nullptr ;
if ( phase [ pi ] . empty ( ) )
2012-08-26 12:07:48 +03:00
pi + + ;
else
2019-05-16 15:41:02 +02:00
{
current = takeOneUnit ( phase [ pi ] , actualTurn , lastMoved , pi ) ;
if ( ! current )
2019-05-18 18:37:02 +02:00
{
2019-05-16 15:41:02 +02:00
pi + + ;
2019-05-18 18:37:02 +02:00
}
2019-05-16 15:41:02 +02:00
else
{
out . back ( ) . push_back ( current ) ;
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
lastMoved = current - > unitSide ( ) ;
2019-05-16 15:41:02 +02:00
}
}
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
2019-05-18 18:37:02 +02:00
if ( lastMoved < 0 )
2019-05-16 15:41:02 +02:00
lastMoved = BattleSide : : ATTACKER ;
2017-07-20 06:08:49 +02:00
if ( ! outputFull ( ) & & ( maxTurns = = 0 | | out . size ( ) < maxTurns ) )
battleGetTurnOrder ( out , maxUnits , maxTurns , actualTurn + 1 , lastMoved ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
std : : vector < BattleHex > CBattleInfoCallback : : battleGetAvailableHexes ( const battle : : Unit * unit ) const
{
RETURN_IF_NOT_BATTLE ( std : : vector < BattleHex > ( ) ) ;
if ( ! unit - > getPosition ( ) . isValid ( ) ) //turrets
return std : : vector < BattleHex > ( ) ;
auto reachability = getReachability ( unit ) ;
return battleGetAvailableHexes ( reachability , unit ) ;
}
std : : vector < BattleHex > CBattleInfoCallback : : battleGetAvailableHexes ( const ReachabilityInfo & cache , const battle : : Unit * unit ) const
2012-08-26 12:07:48 +03:00
{
std : : vector < BattleHex > ret ;
RETURN_IF_NOT_BATTLE ( ret ) ;
2017-07-20 06:08:49 +02:00
if ( ! unit - > getPosition ( ) . isValid ( ) ) //turrets
2012-08-26 12:07:48 +03:00
return ret ;
2017-07-20 06:08:49 +02:00
auto unitSpeed = unit - > Speed ( 0 , true ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
const bool tacticPhase = battleTacticDist ( ) & & battleGetTacticsSide ( ) = = unit - > unitSide ( ) ;
for ( int i = 0 ; i < GameConstants : : BFIELD_SIZE ; + + i )
2012-08-26 12:07:48 +03:00
{
// If obstacles or other stacks makes movement impossible, it can't be helped.
2017-07-20 06:08:49 +02:00
if ( ! cache . isReachable ( i ) )
2012-08-26 12:07:48 +03:00
continue ;
2017-07-20 06:08:49 +02:00
if ( tacticPhase )
2012-08-26 12:07:48 +03:00
{
//Stack has to perform tactic-phase movement -> can enter any reachable tile within given range
if ( ! isInTacticRange ( i ) )
continue ;
}
else
{
2017-07-20 06:08:49 +02:00
//Not tactics phase -> destination must be reachable and within unit range.
2020-10-01 10:38:06 +02:00
if ( cache . distances [ i ] > ( int ) unitSpeed )
2012-08-26 12:07:48 +03:00
continue ;
}
ret . push_back ( i ) ;
2017-07-20 06:08:49 +02:00
}
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
return ret ;
}
std : : vector < BattleHex > CBattleInfoCallback : : battleGetAvailableHexes ( const battle : : Unit * unit , bool addOccupiable , std : : vector < BattleHex > * attackable ) const
{
std : : vector < BattleHex > ret = battleGetAvailableHexes ( unit ) ;
if ( ret . empty ( ) )
return ret ;
if ( addOccupiable & & unit - > doubleWide ( ) )
{
std : : vector < BattleHex > occupiable ;
for ( auto hex : ret )
occupiable . push_back ( unit - > occupiedHex ( hex ) ) ;
vstd : : concatenate ( ret , occupiable ) ;
2012-08-26 12:07:48 +03:00
}
if ( attackable )
{
auto meleeAttackable = [ & ] ( BattleHex hex ) - > bool
{
// Return true if given hex has at least one available neighbour.
// Available hexes are already present in ret vector.
auto availableNeighbor = boost : : find_if ( ret , [ = ] ( BattleHex availableHex )
2017-06-26 18:50:35 +02:00
{
return BattleHex : : mutualPosition ( hex , availableHex ) > = 0 ;
} ) ;
2012-08-26 12:07:48 +03:00
return availableNeighbor ! = ret . end ( ) ;
} ;
2017-07-20 06:08:49 +02:00
for ( auto otherSt : battleAliveUnits ( otherSide ( unit - > unitSide ( ) ) ) )
2012-08-26 12:07:48 +03:00
{
if ( ! otherSt - > isValidTarget ( false ) )
continue ;
std : : vector < BattleHex > occupied = otherSt - > getHexes ( ) ;
2017-07-20 06:08:49 +02:00
if ( battleCanShoot ( unit , otherSt - > getPosition ( ) ) )
2012-08-26 12:07:48 +03:00
{
attackable - > insert ( attackable - > end ( ) , occupied . begin ( ) , occupied . end ( ) ) ;
continue ;
}
2013-06-29 16:05:48 +03:00
for ( BattleHex he : occupied )
2012-08-26 12:07:48 +03:00
{
if ( meleeAttackable ( he ) )
attackable - > push_back ( he ) ;
}
}
}
//adding occupiable likely adds duplicates to ret -> clean it up
boost : : sort ( ret ) ;
ret . erase ( boost : : unique ( ret ) . end ( ) , ret . end ( ) ) ;
return ret ;
}
2015-04-09 21:49:11 +02:00
bool CBattleInfoCallback : : battleCanAttack ( const CStack * stack , const CStack * target , BattleHex dest ) const
{
RETURN_IF_NOT_BATTLE ( false ) ;
2016-02-01 19:03:57 +02:00
2015-04-09 21:49:11 +02:00
if ( battleTacticDist ( ) )
return false ;
2016-02-01 19:03:57 +02:00
2015-04-09 21:49:11 +02:00
if ( ! stack | | ! target )
return false ;
2016-02-01 19:03:57 +02:00
2016-09-29 22:14:22 +02:00
if ( ! battleMatchOwner ( stack , target ) )
2015-04-09 21:49:11 +02:00
return false ;
2016-02-01 19:03:57 +02:00
2015-04-09 21:49:11 +02:00
auto & id = stack - > getCreature ( ) - > idNumber ;
if ( id = = CreatureID : : FIRST_AID_TENT | | id = = CreatureID : : CATAPULT )
return false ;
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
2019-03-20 21:58:15 +02:00
return target - > alive ( ) ;
2015-04-09 21:49:11 +02:00
}
2019-06-28 20:05:25 +02:00
bool CBattleInfoCallback : : battleCanShoot ( const battle : : Unit * attacker ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( false ) ;
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 ( battleTacticDist ( ) ) //no shooting during tactics
2012-08-26 12:59:07 +03:00
return false ;
2012-08-26 12:07:48 +03:00
2019-06-28 20:05:25 +02:00
if ( ! attacker )
return false ;
if ( attacker - > creatureIndex ( ) = = CreatureID : : CATAPULT ) //catapult cannot attack creatures
2012-08-26 12:07:48 +03:00
return false ;
2016-11-18 11:56:13 +02:00
//forgetfulness
2020-11-11 21:43:40 +02:00
TConstBonusListPtr forgetfulList = attacker - > getBonuses ( Selector : : type ( ) ( Bonus : : FORGETFULL ) ) ;
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 ( ! forgetfulList - > empty ( ) )
2016-11-18 11:56:13 +02:00
{
2020-11-11 21:43:40 +02:00
int forgetful = forgetfulList - > valOfBonuses ( Selector : : type ( ) ( Bonus : : FORGETFULL ) ) ;
2016-11-18 11:56:13 +02:00
//advanced+ level
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 ( forgetful > 1 )
2016-11-18 11:56:13 +02:00
return false ;
}
2012-08-26 12:07:48 +03:00
2019-06-28 20:05:25 +02:00
return attacker - > canShoot ( ) & & ( ! battleIsUnitBlocked ( attacker )
| | attacker - > hasBonusOfType ( Bonus : : FREE_SHOOTING ) ) ;
}
bool CBattleInfoCallback : : battleCanShoot ( const battle : : Unit * attacker , BattleHex dest ) const
{
RETURN_IF_NOT_BATTLE ( false ) ;
const battle : : Unit * defender = battleGetUnitByPos ( dest ) ;
if ( ! attacker | | ! defender )
2012-08-26 12:07:48 +03:00
return false ;
2019-06-28 20:05:25 +02:00
if ( battleMatchOwner ( attacker , defender ) & & defender - > alive ( ) )
2023-01-12 16:25:12 +02:00
{
if ( battleCanShoot ( attacker ) )
{
2023-01-12 17:27:21 +02:00
auto limitedRangeBonus = attacker - > getBonus ( Selector : : type ( ) ( Bonus : : LIMITED_SHOOTING_RANGE ) ) ;
if ( limitedRangeBonus = = nullptr )
2023-01-12 16:25:12 +02:00
{
return true ;
}
2023-01-12 17:27:21 +02:00
int shootingRange = limitedRangeBonus - > val ;
2023-01-12 18:07:40 +02:00
return isEnemyUnitWithinSpecifiedRange ( attacker - > getPosition ( ) , defender , shootingRange ) ;
2023-01-12 16:25:12 +02:00
}
}
2019-06-28 20:05:25 +02:00
return false ;
2012-08-26 12:07:48 +03:00
}
2017-06-26 18:50:35 +02:00
TDmgRange CBattleInfoCallback : : calculateDmgRange ( const BattleAttackInfo & info ) const
2012-08-26 12:07:48 +03:00
{
2013-02-06 11:02:46 +03:00
auto battleBonusValue = [ & ] ( const IBonusBearer * bearer , CSelector selector ) - > int
{
2020-11-11 21:43:40 +02:00
auto noLimit = Selector : : effectRange ( ) ( Bonus : : NO_LIMIT ) ;
2014-03-07 16:21:09 +03:00
auto limitMatches = info . shooting
2020-11-11 21:43:40 +02:00
? Selector : : effectRange ( ) ( Bonus : : ONLY_DISTANCE_FIGHT )
: Selector : : effectRange ( ) ( Bonus : : ONLY_MELEE_FIGHT ) ;
2013-07-02 15:08:30 +03:00
//any regular bonuses or just ones for melee/ranged
return bearer - > getBonuses ( selector , noLimit . Or ( limitMatches ) ) - > totalValue ( ) ;
2013-02-06 11:02:46 +03:00
} ;
2017-07-20 06:08:49 +02:00
const IBonusBearer * attackerBonuses = info . attacker ;
const IBonusBearer * defenderBonuses = info . defender ;
double additiveBonus = 1.0 + info . additiveBonus ;
double multBonus = 1.0 * info . multBonus ;
double minDmg = 0.0 ;
double maxDmg = 0.0 ;
minDmg = info . attacker - > getMinDamage ( info . shooting ) ;
maxDmg = info . attacker - > getMaxDamage ( info . shooting ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
minDmg * = info . attacker - > getCount ( ) ,
maxDmg * = info . attacker - > getCount ( ) ;
2012-09-20 19:55:21 +03:00
2017-07-20 06:08:49 +02:00
if ( info . attacker - > creatureIndex ( ) = = CreatureID : : ARROW_TOWERS )
2012-08-26 12:07:48 +03:00
{
2018-02-10 20:52:23 +02:00
SiegeStuffThatShouldBeMovedToHandlers : : retrieveTurretDamageRange ( battleGetDefendedTown ( ) , info . attacker , minDmg , maxDmg ) ;
2017-07-20 06:08:49 +02:00
TDmgRange unmodifiableTowerDamage = std : : make_pair ( int64_t ( minDmg ) , int64_t ( maxDmg ) ) ;
2017-11-18 11:41:07 +02:00
return unmodifiableTowerDamage ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
const std : : string cachingStrSiedgeWeapon = " type_SIEGE_WEAPON " ;
2020-11-11 21:43:40 +02:00
static const auto selectorSiedgeWeapon = Selector : : type ( ) ( Bonus : : SIEGE_WEAPON ) ;
2017-07-20 06:08:49 +02:00
if ( attackerBonuses - > hasBonus ( selectorSiedgeWeapon , cachingStrSiedgeWeapon ) & & info . attacker - > creatureIndex ( ) ! = CreatureID : : ARROW_TOWERS ) //any siege weapon, but only ballista can attack (second condition - not arrow turret)
2012-08-26 12:07:48 +03:00
{ //minDmg and maxDmg are multiplied by hero attack + 1
2018-02-10 20:52:23 +02:00
auto retrieveHeroPrimSkill = [ & ] ( int skill ) - > int
2012-08-26 12:07:48 +03:00
{
2020-10-01 07:55:41 +02:00
std : : shared_ptr < const Bonus > b = attackerBonuses - > getBonus ( Selector : : sourceTypeSel ( Bonus : : HERO_BASE_SKILL ) . And ( Selector : : typeSubtype ( Bonus : : PRIMARY_SKILL , skill ) ) ) ;
2012-08-26 12:07:48 +03:00
return b ? b - > val : 0 ; //if there is no hero or no info on his primary skill, return 0
} ;
2018-02-10 20:52:23 +02:00
minDmg * = retrieveHeroPrimSkill ( PrimarySkill : : ATTACK ) + 1 ;
maxDmg * = retrieveHeroPrimSkill ( PrimarySkill : : ATTACK ) + 1 ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
double attackDefenceDifference = 0.0 ;
2012-08-26 12:07:48 +03:00
2020-11-11 21:43:40 +02:00
double multAttackReduction = 1.0 - battleBonusValue ( attackerBonuses , Selector : : type ( ) ( Bonus : : GENERAL_ATTACK_REDUCTION ) ) / 100.0 ;
2017-07-20 06:08:49 +02:00
attackDefenceDifference + = info . attacker - > getAttack ( info . shooting ) * multAttackReduction ;
2012-08-26 12:07:48 +03:00
2020-11-11 21:43:40 +02:00
double multDefenceReduction = 1.0 - battleBonusValue ( attackerBonuses , Selector : : type ( ) ( Bonus : : ENEMY_DEFENCE_REDUCTION ) ) / 100.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
attackDefenceDifference - = info . defender - > getDefense ( info . shooting ) * multDefenceReduction ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
const std : : string cachingStrSlayer = " type_SLAYER " ;
2020-11-11 21:43:40 +02:00
static const auto selectorSlayer = Selector : : type ( ) ( Bonus : : SLAYER ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
//slayer handling //TODO: apply only ONLY_MELEE_FIGHT / DISTANCE_FIGHT?
auto slayerEffects = attackerBonuses - > getBonuses ( selectorSlayer , cachingStrSlayer ) ;
2020-10-01 07:55:41 +02:00
if ( std : : shared_ptr < const Bonus > slayerEffect = slayerEffects - > getFirst ( Selector : : all ) )
2017-07-20 06:08:49 +02:00
{
std : : vector < int32_t > affectedIds ;
const auto spLevel = slayerEffect - > val ;
const CCreature * defenderType = info . defender - > unitType ( ) ;
bool isAffected = false ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
for ( const auto & b : defenderType - > getBonusList ( ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
if ( ( b - > type = = Bonus : : KING3 & & spLevel > = 3 ) | | //expert
( b - > type = = Bonus : : KING2 & & spLevel > = 2 ) | | //adv +
( b - > type = = Bonus : : KING1 & & spLevel > = 0 ) ) //none or basic +
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
isAffected = true ;
2012-08-26 12:07:48 +03:00
break ;
}
}
2017-07-20 06:08:49 +02:00
if ( isAffected )
2021-02-20 03:57:50 +02:00
{
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
attackDefenceDifference + = SpellID ( SpellID : : SLAYER ) . toSpell ( ) - > getLevelPower ( spLevel ) ;
2021-02-09 00:39:52 +02:00
if ( info . attacker - > hasBonusOfType ( Bonus : : SPECIAL_PECULIAR_ENCHANT , SpellID : : SLAYER ) )
{
ui8 attackerTier = info . attacker - > unitType ( ) - > level ;
ui8 specialtyBonus = std : : max ( 5 - attackerTier , 0 ) ;
attackDefenceDifference + = specialtyBonus ;
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
}
2021-02-09 00:39:52 +02:00
}
2012-08-26 12:07:48 +03:00
}
//bonus from attack/defense skills
if ( attackDefenceDifference < 0 ) //decreasing dmg
{
2023-01-05 00:28:14 +02:00
const double defenseMultiplier = VLC - > modh - > settings . DEFENSE_POINT_DMG_MULTIPLIER ;
const double defenseMultiplierCap = VLC - > modh - > settings . DEFENSE_POINTS_DMG_MULTIPLIER_CAP ;
2022-12-31 17:25:40 +02:00
const double dec = std : : min ( defenseMultiplier * ( - attackDefenceDifference ) , defenseMultiplierCap ) ;
2012-08-26 12:07:48 +03:00
multBonus * = 1.0 - dec ;
}
else //increasing dmg
{
2023-01-05 00:28:14 +02:00
const double attackMultiplier = VLC - > modh - > settings . ATTACK_POINT_DMG_MULTIPLIER ;
const double attackMultiplierCap = VLC - > modh - > settings . ATTACK_POINTS_DMG_MULTIPLIER_CAP ;
2022-12-31 17:25:40 +02:00
const double inc = std : : min ( attackMultiplier * attackDefenceDifference , attackMultiplierCap ) ;
2012-08-26 12:07:48 +03:00
additiveBonus + = inc ;
}
2017-07-20 06:08:49 +02:00
const std : : string cachingStrJousting = " type_JOUSTING " ;
2020-11-11 21:43:40 +02:00
static const auto selectorJousting = Selector : : type ( ) ( Bonus : : JOUSTING ) ;
2017-07-20 06:08:49 +02:00
const std : : string cachingStrChargeImmunity = " type_CHARGE_IMMUNITY " ;
2020-11-11 21:43:40 +02:00
static const auto selectorChargeImmunity = Selector : : type ( ) ( Bonus : : CHARGE_IMMUNITY ) ;
2017-07-20 06:08:49 +02:00
2012-08-26 12:07:48 +03:00
//applying jousting bonus
2017-07-20 06:08:49 +02:00
if ( info . chargedFields > 0 & & attackerBonuses - > hasBonus ( selectorJousting , cachingStrJousting ) & & ! defenderBonuses - > hasBonus ( selectorChargeImmunity , cachingStrChargeImmunity ) )
2012-09-20 19:55:21 +03:00
additiveBonus + = info . chargedFields * 0.05 ;
2012-08-26 12:07:48 +03:00
//handling secondary abilities and artifacts giving premies to them
2017-07-20 06:08:49 +02:00
const std : : string cachingStrArchery = " type_SECONDARY_SKILL_PREMYs_ARCHERY " ;
static const auto selectorArchery = Selector : : typeSubtype ( Bonus : : SECONDARY_SKILL_PREMY , SecondarySkill : : ARCHERY ) ;
const std : : string cachingStrOffence = " type_SECONDARY_SKILL_PREMYs_OFFENCE " ;
static const auto selectorOffence = Selector : : typeSubtype ( Bonus : : SECONDARY_SKILL_PREMY , SecondarySkill : : OFFENCE ) ;
const std : : string cachingStrArmorer = " type_SECONDARY_SKILL_PREMYs_ARMORER " ;
static const auto selectorArmorer = Selector : : typeSubtype ( Bonus : : SECONDARY_SKILL_PREMY , SecondarySkill : : ARMORER ) ;
2012-09-20 19:55:21 +03:00
if ( info . shooting )
2017-07-20 06:08:49 +02:00
additiveBonus + = attackerBonuses - > valOfBonuses ( selectorArchery , cachingStrArchery ) / 100.0 ;
2012-08-26 12:07:48 +03:00
else
2017-07-20 06:08:49 +02:00
additiveBonus + = attackerBonuses - > valOfBonuses ( selectorOffence , cachingStrOffence ) / 100.0 ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
multBonus * = ( std : : max ( 0 , 100 - defenderBonuses - > valOfBonuses ( selectorArmorer , cachingStrArmorer ) ) ) / 100.0 ;
2012-08-26 12:07:48 +03:00
//handling hate effect
2017-07-20 06:08:49 +02:00
//assume that unit have only few HATE features and cache them all
const std : : string cachingStrHate = " type_HATE " ;
2020-11-11 21:43:40 +02:00
static const auto selectorHate = Selector : : type ( ) ( Bonus : : HATE ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
auto allHateEffects = attackerBonuses - > getBonuses ( selectorHate , cachingStrHate ) ;
2012-08-26 12:07:48 +03:00
2020-11-11 21:43:40 +02:00
additiveBonus + = allHateEffects - > valOfBonuses ( Selector : : subtype ( ) ( info . defender - > creatureIndex ( ) ) ) / 100.0 ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
const std : : string cachingStrMeleeReduction = " type_GENERAL_DAMAGE_REDUCTIONs_0 " ;
static const auto selectorMeleeReduction = Selector : : typeSubtype ( Bonus : : GENERAL_DAMAGE_REDUCTION , 0 ) ;
const std : : string cachingStrRangedReduction = " type_GENERAL_DAMAGE_REDUCTIONs_1 " ;
static const auto selectorRangedReduction = Selector : : typeSubtype ( Bonus : : GENERAL_DAMAGE_REDUCTION , 1 ) ;
2012-08-26 12:07:48 +03:00
//handling spell effects
2013-02-06 11:02:46 +03:00
if ( ! info . shooting ) //eg. shield
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
multBonus * = ( 100 - defenderBonuses - > valOfBonuses ( selectorMeleeReduction , cachingStrMeleeReduction ) ) / 100.0 ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
else //eg. air shield
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
multBonus * = ( 100 - defenderBonuses - > valOfBonuses ( selectorRangedReduction , cachingStrRangedReduction ) ) / 100.0 ;
2012-08-26 12:07:48 +03:00
}
2016-11-18 11:56:13 +02:00
if ( info . shooting )
{
//todo: set actual percentage in spell bonus configuration instead of just level; requires non trivial backward compatibility handling
//get list first, total value of 0 also counts
2020-11-11 21:43:40 +02:00
TConstBonusListPtr forgetfulList = attackerBonuses - > getBonuses ( Selector : : type ( ) ( Bonus : : FORGETFULL ) , " type_FORGETFULL " ) ;
2016-11-18 11:56:13 +02:00
if ( ! forgetfulList - > empty ( ) )
{
2017-07-20 06:08:49 +02:00
int forgetful = forgetfulList - > valOfBonuses ( Selector : : all ) ;
2016-11-18 11:56:13 +02:00
//none of basic level
if ( forgetful = = 0 | | forgetful = = 1 )
multBonus * = 0.5 ;
else
logGlobal - > warn ( " Attempt to calculate shooting damage with adv+ FORGETFULL effect " ) ;
}
}
2017-07-20 06:08:49 +02:00
const std : : string cachingStrForcedMinDamage = " type_ALWAYS_MINIMUM_DAMAGE " ;
2020-11-11 21:43:40 +02:00
static const auto selectorForcedMinDamage = Selector : : type ( ) ( Bonus : : ALWAYS_MINIMUM_DAMAGE ) ;
2017-07-20 06:08:49 +02:00
const std : : string cachingStrForcedMaxDamage = " type_ALWAYS_MAXIMUM_DAMAGE " ;
2020-11-11 21:43:40 +02:00
static const auto selectorForcedMaxDamage = Selector : : type ( ) ( Bonus : : ALWAYS_MAXIMUM_DAMAGE ) ;
2017-07-20 06:08:49 +02:00
2020-10-01 07:55:41 +02:00
TConstBonusListPtr curseEffects = attackerBonuses - > getBonuses ( selectorForcedMinDamage , cachingStrForcedMinDamage ) ;
TConstBonusListPtr blessEffects = attackerBonuses - > getBonuses ( selectorForcedMaxDamage , cachingStrForcedMaxDamage ) ;
2017-07-20 06:08:49 +02:00
2012-08-26 12:07:48 +03:00
int curseBlessAdditiveModifier = blessEffects - > totalValue ( ) - curseEffects - > totalValue ( ) ;
2018-03-12 07:20:18 +02:00
double curseMultiplicativePenalty = curseEffects - > size ( ) ? ( * std : : max_element ( curseEffects - > begin ( ) , curseEffects - > end ( ) , & Bonus : : compareByAdditionalInfo < std : : shared_ptr < Bonus > > ) ) - > additionalInfo [ 0 ] : 0 ;
2012-08-26 12:07:48 +03:00
if ( curseMultiplicativePenalty ) //curse handling (partial, the rest is below)
{
multBonus * = 1.0 - curseMultiplicativePenalty / 100 ;
}
2017-07-20 06:08:49 +02:00
const std : : string cachingStrAdvAirShield = " isAdvancedAirShield " ;
2016-09-19 23:36:35 +02:00
auto isAdvancedAirShield = [ ] ( const Bonus * bonus )
2012-08-26 12:07:48 +03:00
{
2012-08-26 12:59:07 +03:00
return bonus - > source = = Bonus : : SPELL_EFFECT
2017-06-24 15:51:07 +02:00
& & bonus - > sid = = SpellID : : AIR_SHIELD
& & bonus - > val > = SecSkillLevel : : ADVANCED ;
2012-08-26 12:07:48 +03:00
} ;
2016-02-01 19:03:57 +02:00
if ( info . shooting )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
//wall / distance penalty + advanced air shield
2019-06-28 20:05:25 +02:00
BattleHex attackerPos = info . attackerPos . isValid ( ) ? info . attackerPos : info . attacker - > getPosition ( ) ;
BattleHex defenderPos = info . defenderPos . isValid ( ) ? info . defenderPos : info . defender - > getPosition ( ) ;
const bool distPenalty = battleHasDistancePenalty ( attackerBonuses , attackerPos , defenderPos ) ;
const bool obstaclePenalty = battleHasWallPenalty ( attackerBonuses , attackerPos , defenderPos ) ;
2017-07-20 06:08:49 +02:00
if ( distPenalty | | defenderBonuses - > hasBonus ( isAdvancedAirShield , cachingStrAdvAirShield ) )
2012-08-26 12:07:48 +03:00
multBonus * = 0.5 ;
2017-07-20 06:08:49 +02:00
if ( obstaclePenalty )
2012-08-26 12:07:48 +03:00
multBonus * = 0.5 ; //cumulative
}
2017-07-20 06:08:49 +02:00
else
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
const std : : string cachingStrNoMeleePenalty = " type_NO_MELEE_PENALTY " ;
2020-11-11 21:43:40 +02:00
static const auto selectorNoMeleePenalty = Selector : : type ( ) ( Bonus : : NO_MELEE_PENALTY ) ;
2017-07-20 06:08:49 +02:00
if ( info . attacker - > isShooter ( ) & & ! attackerBonuses - > hasBonus ( selectorNoMeleePenalty , cachingStrNoMeleePenalty ) )
multBonus * = 0.5 ;
2012-08-26 12:07:48 +03:00
}
2016-02-01 19:03:57 +02:00
// psychic elementals versus mind immune units 50%
2017-07-20 06:08:49 +02:00
if ( info . attacker - > creatureIndex ( ) = = CreatureID : : PSYCHIC_ELEMENTAL )
2016-02-01 19:03:57 +02:00
{
2017-07-20 06:08:49 +02:00
const std : : string cachingStrMindImmunity = " type_MIND_IMMUNITY " ;
2020-11-11 21:43:40 +02:00
static const auto selectorMindImmunity = Selector : : type ( ) ( Bonus : : MIND_IMMUNITY ) ;
2017-07-20 06:08:49 +02:00
if ( defenderBonuses - > hasBonus ( selectorMindImmunity , cachingStrMindImmunity ) )
multBonus * = 0.5 ;
2016-02-01 19:03:57 +02:00
}
2013-06-17 18:45:55 +03:00
// TODO attack on petrified unit 50%
// blinded unit retaliates
2012-08-26 12:07:48 +03:00
minDmg * = additiveBonus * multBonus ;
maxDmg * = additiveBonus * multBonus ;
if ( curseEffects - > size ( ) ) //curse handling (rest)
{
minDmg + = curseBlessAdditiveModifier ;
2017-07-20 06:08:49 +02:00
maxDmg = minDmg ;
2012-08-26 12:07:48 +03:00
}
else if ( blessEffects - > size ( ) ) //bless handling
{
maxDmg + = curseBlessAdditiveModifier ;
2017-07-20 06:08:49 +02:00
minDmg = maxDmg ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
TDmgRange returnedVal = std : : make_pair ( int64_t ( minDmg ) , int64_t ( maxDmg ) ) ;
2012-08-26 12:07:48 +03:00
//damage cannot be less than 1
vstd : : amax ( returnedVal . first , 1 ) ;
vstd : : amax ( returnedVal . second , 1 ) ;
return returnedVal ;
}
2017-07-20 06:08:49 +02:00
TDmgRange CBattleInfoCallback : : battleEstimateDamage ( const CStack * attacker , const CStack * defender , TDmgRange * retaliationDmg ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( std : : make_pair ( 0 , 0 ) ) ;
2017-07-20 06:08:49 +02:00
const bool shooting = battleCanShoot ( attacker , defender - > getPosition ( ) ) ;
2012-09-20 19:55:21 +03:00
const BattleAttackInfo bai ( attacker , defender , shooting ) ;
2017-07-20 06:08:49 +02:00
return battleEstimateDamage ( bai , retaliationDmg ) ;
2012-09-20 19:55:21 +03:00
}
2017-07-20 06:08:49 +02:00
TDmgRange CBattleInfoCallback : : battleEstimateDamage ( const BattleAttackInfo & bai , TDmgRange * retaliationDmg ) const
2012-09-20 19:55:21 +03:00
{
RETURN_IF_NOT_BATTLE ( std : : make_pair ( 0 , 0 ) ) ;
TDmgRange ret = calculateDmgRange ( bai ) ;
2012-08-26 12:07:48 +03:00
if ( retaliationDmg )
{
2012-09-20 19:55:21 +03:00
if ( bai . shooting )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
//FIXME: handle RANGED_RETALIATION
2012-08-26 12:07:48 +03:00
retaliationDmg - > first = retaliationDmg - > second = 0 ;
}
else
{
2017-07-20 06:08:49 +02:00
//TODO: rewrite using boost::numeric::interval
//TODO: rewire once more using interval-based fuzzy arithmetic
int64_t TDmgRange : : * pairElems [ ] = { & TDmgRange : : first , & TDmgRange : : second } ;
2012-08-26 12:07:48 +03:00
for ( int i = 0 ; i < 2 ; + + i )
{
2012-09-20 19:55:21 +03:00
auto retaliationAttack = bai . reverse ( ) ;
2017-07-20 06:08:49 +02:00
int64_t dmg = ret . * pairElems [ i ] ;
auto state = retaliationAttack . attacker - > acquireState ( ) ;
state - > damage ( dmg ) ;
retaliationAttack . attacker = state . get ( ) ;
2012-09-20 19:55:21 +03:00
retaliationDmg - > * pairElems [ ! i ] = calculateDmgRange ( retaliationAttack ) . * pairElems [ ! i ] ;
2012-08-26 12:07:48 +03:00
}
}
}
return ret ;
}
2017-07-01 17:59:53 +02:00
std : : vector < std : : shared_ptr < const CObstacleInstance > > CBattleInfoCallback : : battleGetAllObstaclesOnPos ( BattleHex tile , bool onlyBlocking ) const
2012-08-26 12:07:48 +03:00
{
2017-07-01 17:59:53 +02:00
std : : vector < std : : shared_ptr < const CObstacleInstance > > obstacles = std : : vector < std : : shared_ptr < const CObstacleInstance > > ( ) ;
RETURN_IF_NOT_BATTLE ( obstacles ) ;
for ( auto & obs : battleGetAllObstacles ( ) )
2012-08-26 12:07:48 +03:00
{
if ( vstd : : contains ( obs - > getBlockedTiles ( ) , tile )
2017-07-01 17:59:53 +02:00
| | ( ! onlyBlocking & & vstd : : contains ( obs - > getAffectedTiles ( ) , tile ) ) )
2012-08-26 12:07:48 +03:00
{
2017-07-01 17:59:53 +02:00
obstacles . push_back ( obs ) ;
2012-08-26 12:07:48 +03:00
}
}
2017-07-01 17:59:53 +02:00
return obstacles ;
2012-08-26 12:07:48 +03:00
}
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
std : : vector < std : : shared_ptr < const CObstacleInstance > > CBattleInfoCallback : : getAllAffectedObstaclesByStack ( const battle : : Unit * unit ) const
2017-07-09 10:18:46 +02:00
{
std : : vector < std : : shared_ptr < const CObstacleInstance > > affectedObstacles = std : : vector < std : : shared_ptr < const CObstacleInstance > > ( ) ;
RETURN_IF_NOT_BATTLE ( affectedObstacles ) ;
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 ( unit - > alive ( ) )
2017-07-09 10:18:46 +02:00
{
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
affectedObstacles = battleGetAllObstaclesOnPos ( unit - > getPosition ( ) , false ) ;
if ( unit - > doubleWide ( ) )
2017-07-09 10:18:46 +02:00
{
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
BattleHex otherHex = unit - > occupiedHex ( unit - > getPosition ( ) ) ;
2017-07-09 10:18:46 +02:00
if ( otherHex . isValid ( ) )
for ( auto & i : battleGetAllObstaclesOnPos ( otherHex , false ) )
affectedObstacles . push_back ( i ) ;
}
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
for ( auto hex : unit - > getHexes ( ) )
2017-07-20 17:15:47 +02:00
if ( hex = = ESiegeHex : : GATE_BRIDGE )
if ( battleGetGateState ( ) = = EGateState : : OPENED | | battleGetGateState ( ) = = EGateState : : DESTROYED )
for ( int i = 0 ; i < affectedObstacles . size ( ) ; i + + )
if ( affectedObstacles . at ( i ) - > obstacleType = = CObstacleInstance : : MOAT )
affectedObstacles . erase ( affectedObstacles . begin ( ) + i ) ;
2017-07-09 10:18:46 +02:00
}
return affectedObstacles ;
}
2012-08-26 12:07:48 +03:00
AccessibilityInfo CBattleInfoCallback : : getAccesibility ( ) const
{
AccessibilityInfo ret ;
ret . fill ( EAccessibility : : ACCESSIBLE ) ;
//removing accessibility for side columns of hexes
for ( int y = 0 ; y < GameConstants : : BFIELD_HEIGHT ; y + + )
{
ret [ BattleHex ( GameConstants : : BFIELD_WIDTH - 1 , y ) ] = EAccessibility : : SIDE_COLUMN ;
ret [ BattleHex ( 0 , y ) ] = EAccessibility : : SIDE_COLUMN ;
}
2017-07-13 16:47:28 +02:00
//special battlefields with logically unavailable tiles
2022-06-28 10:05:30 +02:00
auto bFieldType = battleGetBattlefieldType ( ) ;
if ( bFieldType ! = BattleField : : NONE )
2017-07-15 23:01:33 +02:00
{
2022-06-28 10:05:30 +02:00
std : : vector < BattleHex > impassableHexes = bFieldType . getInfo ( ) - > impassableHexes ;
for ( auto hex : impassableHexes )
ret [ hex ] = EAccessibility : : UNAVAILABLE ;
2017-07-15 23:01:33 +02:00
}
2017-07-13 16:47:28 +02:00
2012-09-29 23:25:54 +03:00
//gate -> should be before stacks
2016-01-29 21:43:35 +02:00
if ( battleGetSiegeLevel ( ) > 0 )
2012-09-29 23:25:54 +03:00
{
2017-06-26 18:50:35 +02:00
EAccessibility accessability = EAccessibility : : ACCESSIBLE ;
2016-02-13 16:40:31 +02:00
switch ( battleGetGateState ( ) )
2016-01-29 21:43:35 +02:00
{
2016-02-13 16:40:31 +02:00
case EGateState : : CLOSED :
2016-01-29 21:43:35 +02:00
accessability = EAccessibility : : GATE ;
break ;
2016-02-13 16:40:31 +02:00
case EGateState : : BLOCKED :
2016-01-29 21:43:35 +02:00
accessability = EAccessibility : : UNAVAILABLE ;
break ;
}
2016-02-09 16:38:59 +02:00
ret [ ESiegeHex : : GATE_OUTER ] = ret [ ESiegeHex : : GATE_INNER ] = accessability ;
2012-09-29 23:25:54 +03:00
}
2012-08-26 12:07:48 +03:00
//tiles occupied by standing stacks
2017-07-20 06:08:49 +02:00
for ( auto unit : battleAliveUnits ( ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
for ( auto hex : unit - > getHexes ( ) )
2012-08-26 12:07:48 +03:00
if ( hex . isAvailable ( ) ) //towers can have <0 pos; we don't also want to overwrite side columns
ret [ hex ] = EAccessibility : : ALIVE_STACK ;
}
//obstacles
2013-06-29 16:05:48 +03:00
for ( const auto & obst : battleGetAllObstacles ( ) )
2012-08-26 12:07:48 +03:00
{
2013-06-29 16:05:48 +03:00
for ( auto hex : obst - > getBlockedTiles ( ) )
2012-08-26 12:07:48 +03:00
ret [ hex ] = EAccessibility : : OBSTACLE ;
}
//walls
if ( battleGetSiegeLevel ( ) > 0 )
{
2016-02-09 16:38:59 +02:00
static const int permanentlyLocked [ ] = { 12 , 45 , 62 , 112 , 147 , 165 } ;
2013-06-29 16:05:48 +03:00
for ( auto hex : permanentlyLocked )
2012-08-26 12:07:48 +03:00
ret [ hex ] = EAccessibility : : UNAVAILABLE ;
//TODO likely duplicated logic
2023-01-13 00:35:58 +02:00
static const std : : pair < EWallPart , BattleHex > lockedIfNotDestroyed [ ] =
2016-02-09 16:38:59 +02:00
{
//which part of wall, which hex is blocked if this part of wall is not destroyed
2023-01-13 00:35:58 +02:00
std : : make_pair ( EWallPart : : BOTTOM_WALL , BattleHex ( ESiegeHex : : DESTRUCTIBLE_WALL_4 ) ) ,
std : : make_pair ( EWallPart : : BELOW_GATE , BattleHex ( ESiegeHex : : DESTRUCTIBLE_WALL_3 ) ) ,
std : : make_pair ( EWallPart : : OVER_GATE , BattleHex ( ESiegeHex : : DESTRUCTIBLE_WALL_2 ) ) ,
std : : make_pair ( EWallPart : : UPPER_WALL , BattleHex ( ESiegeHex : : DESTRUCTIBLE_WALL_1 ) )
2016-02-09 16:38:59 +02:00
} ;
2012-08-26 12:07:48 +03:00
2013-06-29 16:05:48 +03:00
for ( auto & elem : lockedIfNotDestroyed )
2012-08-26 12:07:48 +03:00
{
2013-08-06 14:20:28 +03:00
if ( battleGetWallState ( elem . first ) ! = EWallState : : DESTROYED )
2013-06-29 16:05:48 +03:00
ret [ elem . second ] = EAccessibility : : DESTRUCTIBLE_WALL ;
2012-08-26 12:07:48 +03:00
}
}
return ret ;
}
2017-07-20 06:08:49 +02:00
AccessibilityInfo CBattleInfoCallback : : getAccesibility ( const battle : : Unit * stack ) const
2012-08-28 15:28:13 +03:00
{
2017-07-20 06:08:49 +02:00
return getAccesibility ( battle : : Unit : : getHexes ( stack - > getPosition ( ) , stack - > doubleWide ( ) , stack - > unitSide ( ) ) ) ;
2012-08-28 15:28:13 +03:00
}
2017-06-26 18:50:35 +02:00
AccessibilityInfo CBattleInfoCallback : : getAccesibility ( const std : : vector < BattleHex > & accessibleHexes ) const
2012-08-28 15:28:13 +03:00
{
auto ret = getAccesibility ( ) ;
2013-06-29 16:05:48 +03:00
for ( auto hex : accessibleHexes )
2012-09-30 14:33:19 +03:00
if ( hex . isValid ( ) )
ret [ hex ] = EAccessibility : : ACCESSIBLE ;
2012-08-28 15:28:13 +03:00
return ret ;
}
2017-06-26 18:50:35 +02:00
ReachabilityInfo CBattleInfoCallback : : makeBFS ( const AccessibilityInfo & accessibility , const ReachabilityInfo : : Parameters & params ) const
2012-08-26 12:07:48 +03:00
{
ReachabilityInfo ret ;
ret . accessibility = accessibility ;
ret . params = params ;
ret . predecessors . fill ( BattleHex : : INVALID ) ;
2012-08-26 22:13:57 +03:00
ret . distances . fill ( ReachabilityInfo : : INFINITE_DIST ) ;
2012-08-26 12:07:48 +03:00
2012-08-27 15:34:43 +03:00
if ( ! params . startPosition . isValid ( ) ) //if got call for arrow turrets
return ret ;
2020-11-21 19:01:42 +02:00
const std : : set < BattleHex > obstacles = getStoppers ( params . perspective ) ;
2012-08-26 12:07:48 +03:00
std : : queue < BattleHex > hexq ; //bfs queue
//first element
hexq . push ( params . startPosition ) ;
ret . distances [ params . startPosition ] = 0 ;
2017-07-20 06:08:49 +02:00
std : : array < bool , GameConstants : : BFIELD_SIZE > accessibleCache ;
for ( int hex = 0 ; hex < GameConstants : : BFIELD_SIZE ; hex + + )
accessibleCache [ hex ] = accessibility . accessible ( hex , params . doubleWide , params . side ) ;
2012-08-26 12:07:48 +03:00
while ( ! hexq . empty ( ) ) //bfs loop
{
const BattleHex curHex = hexq . front ( ) ;
hexq . pop ( ) ;
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
2020-11-21 19:01:42 +02:00
//walking stack can't step past the obstacles
2020-11-21 22:57:01 +02:00
if ( curHex ! = params . startPosition & & isInObstacle ( curHex , obstacles , params ) )
continue ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
const int costToNeighbour = ret . distances [ curHex . hex ] + 1 ;
for ( BattleHex neighbour : BattleHex : : neighbouringTilesCache [ curHex . hex ] )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
if ( neighbour . isValid ( ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
const int costFoundSoFar = ret . distances [ neighbour . hex ] ;
if ( accessibleCache [ neighbour . hex ] & & costToNeighbour < costFoundSoFar )
{
hexq . push ( neighbour ) ;
ret . distances [ neighbour . hex ] = costToNeighbour ;
ret . predecessors [ neighbour . hex ] = curHex ;
}
2012-08-26 12:07:48 +03:00
}
}
}
return ret ;
}
2020-11-21 22:57:01 +02:00
bool CBattleInfoCallback : : isInObstacle (
BattleHex hex ,
const std : : set < BattleHex > & obstacles ,
const ReachabilityInfo : : Parameters & params ) const
{
auto occupiedHexes = battle : : Unit : : getHexes ( hex , params . doubleWide , params . side ) ;
for ( auto occupiedHex : occupiedHexes )
{
if ( vstd : : contains ( obstacles , occupiedHex ) )
{
if ( occupiedHex = = ESiegeHex : : GATE_BRIDGE )
{
if ( battleGetGateState ( ) ! = EGateState : : DESTROYED & & params . side = = BattleSide : : ATTACKER )
return true ;
}
else
return true ;
}
}
return false ;
}
2012-08-26 12:07:48 +03:00
std : : set < BattleHex > CBattleInfoCallback : : getStoppers ( BattlePerspective : : BattlePerspective whichSidePerspective ) const
{
std : : set < BattleHex > ret ;
RETURN_IF_NOT_BATTLE ( ret ) ;
2013-06-29 16:05:48 +03:00
for ( auto & oi : battleGetAllObstacles ( whichSidePerspective ) )
2012-08-26 12:07:48 +03:00
{
if ( battleIsObstacleVisibleForSide ( * oi , whichSidePerspective ) )
{
range : : copy ( oi - > getStoppingTile ( ) , vstd : : set_inserter ( ret ) ) ;
}
}
return ret ;
}
2017-07-20 06:08:49 +02:00
std : : pair < const battle : : Unit * , BattleHex > CBattleInfoCallback : : getNearestStack ( const battle : : Unit * closest ) const
2012-08-26 12:07:48 +03:00
{
auto reachability = getReachability ( closest ) ;
2017-07-20 06:08:49 +02:00
auto avHexes = battleGetAvailableHexes ( reachability , closest ) ;
2012-08-26 12:07:48 +03:00
// I hate std::pairs with their undescriptive member names first / second
struct DistStack
{
2022-12-08 23:20:42 +02:00
uint32_t distanceToPred ;
2016-02-01 19:03:57 +02:00
BattleHex destination ;
2017-07-20 06:08:49 +02:00
const battle : : Unit * stack ;
2012-08-26 12:07:48 +03:00
} ;
2015-04-03 04:08:13 +02:00
std : : vector < DistStack > stackPairs ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
std : : vector < const battle : : Unit * > possible = battleGetUnitsIf ( [ = ] ( const battle : : Unit * unit )
2015-04-03 04:08:13 +02:00
{
2017-07-20 06:08:49 +02:00
return unit - > isValidTarget ( false ) & & unit ! = closest ;
2016-02-28 04:10:20 +02:00
} ) ;
2016-02-01 19:03:57 +02:00
2017-07-20 06:08:49 +02:00
for ( const battle : : Unit * st : possible )
{
2015-04-03 04:08:13 +02:00
for ( BattleHex hex : avHexes )
if ( CStack : : isMeleeAttackPossible ( closest , st , hex ) )
2013-05-25 10:06:00 +03:00
{
2016-10-07 08:45:03 +02:00
DistStack hlp = { reachability . distances [ hex ] , hex , st } ;
2013-05-25 10:06:00 +03:00
stackPairs . push_back ( hlp ) ;
}
2017-07-20 06:08:49 +02:00
}
2012-08-26 12:07:48 +03:00
2013-05-25 10:06:00 +03:00
if ( stackPairs . size ( ) )
2012-08-26 12:07:48 +03:00
{
auto comparator = [ ] ( DistStack lhs , DistStack rhs ) { return lhs . distanceToPred < rhs . distanceToPred ; } ;
auto minimal = boost : : min_element ( stackPairs , comparator ) ;
2015-04-03 04:08:13 +02:00
return std : : make_pair ( minimal - > stack , minimal - > destination ) ;
2012-08-26 12:07:48 +03:00
}
2013-05-25 10:06:00 +03:00
else
2017-07-20 06:08:49 +02:00
return std : : make_pair < const battle : : Unit * , BattleHex > ( nullptr , BattleHex : : INVALID ) ;
}
BattleHex CBattleInfoCallback : : getAvaliableHex ( CreatureID creID , ui8 side , int initialPos ) 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
bool twoHex = VLC - > creh - > objects [ creID ] - > isDoubleWide ( ) ;
2017-07-20 06:08:49 +02:00
int pos ;
if ( initialPos > - 1 )
pos = initialPos ;
else //summon elementals depending on player side
{
if ( side = = BattleSide : : ATTACKER )
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 , side ) )
occupyable . insert ( i ) ;
if ( occupyable . empty ( ) )
{
return BattleHex : : INVALID ; //all tiles are covered
}
return BattleHex : : getClosestTile ( side , pos , occupyable ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
2012-08-26 12:07:48 +03:00
si8 CBattleInfoCallback : : battleGetTacticDist ( ) const
{
RETURN_IF_NOT_BATTLE ( 0 ) ;
//TODO get rid of this method
if ( battleDoWeKnowAbout ( battleGetTacticsSide ( ) ) )
return battleTacticDist ( ) ;
return 0 ;
}
bool CBattleInfoCallback : : isInTacticRange ( BattleHex dest ) const
{
RETURN_IF_NOT_BATTLE ( false ) ;
auto side = battleGetTacticsSide ( ) ;
auto dist = battleGetTacticDist ( ) ;
return ( ( ! side & & dest . getX ( ) > 0 & & dest . getX ( ) < = dist )
2017-06-24 15:51:07 +02:00
| | ( side & & dest . getX ( ) < GameConstants : : BFIELD_WIDTH - 1 & & dest . getX ( ) > = GameConstants : : BFIELD_WIDTH - dist - 1 ) ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
ReachabilityInfo CBattleInfoCallback : : getReachability ( const battle : : Unit * unit ) const
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
ReachabilityInfo : : Parameters params ( unit , unit - > getPosition ( ) ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
if ( ! battleDoWeKnowAbout ( unit - > unitSide ( ) ) )
2012-08-26 12:07:48 +03:00
{
//Stack is held by enemy, we can't use his perspective to check for reachability.
2012-08-28 15:28:13 +03:00
// Happens ie. when hovering enemy stack for its range. The arg could be set properly, but it's easier to fix it here.
2012-08-26 12:07:48 +03:00
params . perspective = battleGetMySide ( ) ;
}
return getReachability ( params ) ;
}
ReachabilityInfo CBattleInfoCallback : : getReachability ( const ReachabilityInfo : : Parameters & params ) const
{
if ( params . flying )
return getFlyingReachability ( params ) ;
else
2012-08-28 15:28:13 +03:00
return makeBFS ( getAccesibility ( params . knownAccessible ) , params ) ;
2012-08-26 12:07:48 +03:00
}
2013-11-07 15:48:41 +03:00
ReachabilityInfo CBattleInfoCallback : : getFlyingReachability ( const ReachabilityInfo : : Parameters & params ) const
2012-08-26 12:07:48 +03:00
{
ReachabilityInfo ret ;
2012-08-28 15:28:13 +03:00
ret . accessibility = getAccesibility ( params . knownAccessible ) ;
2012-08-26 12:07:48 +03:00
for ( int i = 0 ; i < GameConstants : : BFIELD_SIZE ; i + + )
{
2017-07-01 10:34:00 +02:00
if ( ret . accessibility . accessible ( i , params . doubleWide , params . side ) )
2012-08-26 12:07:48 +03:00
{
ret . predecessors [ i ] = params . startPosition ;
ret . distances [ i ] = BattleHex : : getDistance ( params . startPosition , i ) ;
}
}
return ret ;
}
2019-06-28 20:05:25 +02:00
AttackableTiles CBattleInfoCallback : : getPotentiallyAttackableHexes ( const battle : : Unit * attacker , BattleHex destinationTile , BattleHex attackerPos ) const
2012-08-26 12:07:48 +03:00
{
2012-11-30 17:06:22 +03:00
//does not return hex attacked directly
2012-08-26 12:07:48 +03:00
AttackableTiles at ;
RETURN_IF_NOT_BATTLE ( at ) ;
2017-07-20 06:08:49 +02:00
BattleHex hex = ( attackerPos ! = BattleHex : : INVALID ) ? attackerPos : attacker - > getPosition ( ) ; //real or hypothetical (cursor) position
2012-11-29 18:02:55 +03:00
2022-12-21 18:04:54 +02:00
auto defender = battleGetUnitByPos ( destinationTile , true ) ;
2022-12-18 11:42:02 +02:00
if ( ! defender )
return at ; // can't attack thin air
2012-11-21 12:13:33 +03:00
//FIXME: dragons or cerbers can rotate before attack, making their base hex different (#1124)
2022-12-18 11:42:02 +02:00
bool reverse = isToReverse ( destinationTile , attacker , defender ) ;
2017-09-05 22:29:31 +02:00
if ( reverse & & attacker - > doubleWide ( ) )
2012-11-29 18:02:55 +03:00
{
2012-11-30 17:06:22 +03:00
hex = attacker - > occupiedHex ( hex ) ; //the other hex stack stands on
2012-11-29 18:02:55 +03:00
}
2017-09-05 22:29:31 +02:00
if ( attacker - > hasBonusOfType ( Bonus : : ATTACKS_ALL_ADJACENT ) )
2012-08-26 12:07:48 +03:00
{
2017-09-05 22:29:31 +02:00
boost : : copy ( attacker - > getSurroundingHexes ( attackerPos ) , vstd : : set_inserter ( at . hostileCreaturePositions ) ) ;
2012-08-26 12:07:48 +03:00
}
2017-09-05 22:29:31 +02:00
if ( attacker - > hasBonusOfType ( Bonus : : THREE_HEADED_ATTACK ) )
2012-08-26 12:07:48 +03:00
{
std : : vector < BattleHex > hexes = attacker - > getSurroundingHexes ( attackerPos ) ;
2017-09-05 22:29:31 +02:00
for ( BattleHex tile : hexes )
2012-08-26 12:07:48 +03:00
{
2017-09-05 22:29:31 +02:00
if ( ( BattleHex : : mutualPosition ( tile , destinationTile ) > - 1 & & BattleHex : : mutualPosition ( tile , hex ) > - 1 ) ) //adjacent both to attacker's head and attacked tile
2012-08-26 12:07:48 +03:00
{
2019-06-28 20:05:25 +02:00
auto st = battleGetUnitByPos ( tile , true ) ;
if ( st & & battleMatchOwner ( st , attacker ) ) //only hostile stacks - does it work well with Berserk?
2012-08-26 12:07:48 +03:00
at . hostileCreaturePositions . insert ( tile ) ;
}
}
}
2017-09-05 22:29:31 +02:00
if ( attacker - > hasBonusOfType ( Bonus : : WIDE_BREATH ) )
{
std : : vector < BattleHex > hexes = destinationTile . neighbouringTiles ( ) ;
for ( int i = 0 ; i < hexes . size ( ) ; i + + )
{
2017-11-13 15:13:55 +02:00
if ( hexes . at ( i ) = = hex )
{
2017-09-05 22:29:31 +02:00
hexes . erase ( hexes . begin ( ) + i ) ;
i = 0 ;
2017-01-28 13:28:30 +02:00
}
2017-09-05 22:29:31 +02:00
}
for ( BattleHex tile : hexes )
{
//friendly stacks can also be damaged by Dragon Breath
2019-06-28 20:05:25 +02:00
auto st = battleGetUnitByPos ( tile , true ) ;
if ( st & & st ! = attacker )
2021-02-20 03:57:50 +02:00
at . friendlyCreaturePositions . insert ( tile ) ;
2017-01-28 13:28:30 +02:00
}
2017-09-05 22:29:31 +02:00
}
2019-06-28 20:05:25 +02:00
else if ( attacker - > hasBonusOfType ( Bonus : : TWO_HEX_ATTACK_BREATH ) )
2012-08-26 12:07:48 +03:00
{
2022-12-21 18:04:54 +02:00
auto direction = BattleHex : : mutualPosition ( hex , destinationTile ) ;
if ( direction ! = BattleHex : : NONE ) //only adjacent hexes are subject of dragon breath calculation
2012-08-26 12:07:48 +03:00
{
2022-12-21 18:04:54 +02:00
BattleHex nextHex = destinationTile . cloneInDirection ( direction , false ) ;
if ( nextHex . isValid ( ) )
2021-02-20 03:57:50 +02:00
{
//friendly stacks can also be damaged by Dragon Breath
2022-12-21 18:04:54 +02:00
auto st = battleGetUnitByPos ( nextHex , true ) ;
2021-02-20 03:57:50 +02:00
if ( st ! = nullptr )
2022-12-21 18:04:54 +02:00
at . friendlyCreaturePositions . insert ( nextHex ) ;
2021-02-20 03:57:50 +02:00
}
2012-08-26 12:07:48 +03:00
}
}
return at ;
}
2019-06-28 20:05:25 +02:00
AttackableTiles CBattleInfoCallback : : getPotentiallyShootableHexes ( const battle : : Unit * attacker , BattleHex destinationTile , BattleHex attackerPos ) const
2017-09-05 23:45:31 +02:00
{
//does not return hex attacked directly
AttackableTiles at ;
RETURN_IF_NOT_BATTLE ( at ) ;
if ( attacker - > hasBonusOfType ( Bonus : : SHOOTS_ALL_ADJACENT ) & & ! vstd : : contains ( attackerPos . neighbouringTiles ( ) , destinationTile ) )
{
std : : vector < BattleHex > targetHexes = destinationTile . neighbouringTiles ( ) ;
targetHexes . push_back ( destinationTile ) ;
boost : : copy ( targetHexes , vstd : : set_inserter ( at . hostileCreaturePositions ) ) ;
}
2012-08-26 12:07:48 +03:00
return at ;
}
2019-06-28 20:05:25 +02:00
std : : vector < const battle : : Unit * > CBattleInfoCallback : : getAttackedBattleUnits ( const battle : : Unit * attacker , BattleHex destinationTile , bool rangedAttack , BattleHex attackerPos ) const
{
std : : vector < const battle : : Unit * > units ;
RETURN_IF_NOT_BATTLE ( units ) ;
AttackableTiles at ;
if ( rangedAttack )
at = getPotentiallyShootableHexes ( attacker , destinationTile , attackerPos ) ;
else
at = getPotentiallyAttackableHexes ( attacker , destinationTile , attackerPos ) ;
units = battleGetUnitsIf ( [ = ] ( const battle : : Unit * unit )
{
2020-05-16 15:14:58 +02:00
if ( unit - > isGhost ( ) | | ! unit - > alive ( ) )
2019-06-28 20:05:25 +02:00
return false ;
2020-05-16 15:14:58 +02:00
for ( BattleHex hex : battle : : Unit : : getHexes ( unit - > getPosition ( ) , unit - > doubleWide ( ) , unit - > unitSide ( ) ) )
{
if ( vstd : : contains ( at . hostileCreaturePositions , hex ) )
2019-06-28 20:05:25 +02:00
return true ;
2020-05-16 15:14:58 +02:00
if ( vstd : : contains ( at . friendlyCreaturePositions , hex ) )
2019-06-28 20:05:25 +02:00
return true ;
}
return false ;
} ) ;
return units ;
}
2017-09-04 23:32:24 +02:00
std : : set < const CStack * > CBattleInfoCallback : : getAttackedCreatures ( const CStack * attacker , BattleHex destinationTile , bool rangedAttack , BattleHex attackerPos ) const
2012-08-26 12:07:48 +03:00
{
std : : set < const CStack * > attackedCres ;
RETURN_IF_NOT_BATTLE ( attackedCres ) ;
2017-09-05 23:45:31 +02:00
AttackableTiles at ;
if ( rangedAttack )
at = getPotentiallyShootableHexes ( attacker , destinationTile , attackerPos ) ;
else
at = getPotentiallyAttackableHexes ( attacker , destinationTile , attackerPos ) ;
2013-06-29 16:05:48 +03:00
for ( BattleHex tile : at . hostileCreaturePositions ) //all around & three-headed attack
2012-08-26 12:07:48 +03:00
{
const CStack * st = battleGetStackByPos ( tile , true ) ;
if ( st & & st - > owner ! = attacker - > owner ) //only hostile stacks - does it work well with Berserk?
{
attackedCres . insert ( st ) ;
}
}
2013-06-29 16:05:48 +03:00
for ( BattleHex tile : at . friendlyCreaturePositions )
2012-08-26 12:07:48 +03:00
{
const CStack * st = battleGetStackByPos ( tile , true ) ;
if ( st ) //friendly stacks can also be damaged by Dragon Breath
{
attackedCres . insert ( st ) ;
}
}
return attackedCres ;
}
2022-12-18 11:42:02 +02:00
static bool isHexInFront ( BattleHex hex , BattleHex testHex , BattleSide : : Type side )
2012-11-29 18:02:55 +03:00
{
2022-12-18 11:42:02 +02:00
static const std : : set < BattleHex : : EDir > rightDirs { BattleHex : : BOTTOM_RIGHT , BattleHex : : TOP_RIGHT , BattleHex : : RIGHT } ;
static const std : : set < BattleHex : : EDir > leftDirs { BattleHex : : BOTTOM_LEFT , BattleHex : : TOP_LEFT , BattleHex : : LEFT } ;
2012-11-29 18:02:55 +03:00
2022-12-18 11:42:02 +02:00
auto mutualPos = BattleHex : : mutualPosition ( hex , testHex ) ;
2013-08-06 18:25:51 +03:00
2022-12-18 11:42:02 +02:00
if ( side = = BattleSide : : ATTACKER )
return rightDirs . count ( mutualPos ) ;
else
return leftDirs . count ( mutualPos ) ;
2012-11-29 18:02:55 +03:00
}
2013-08-06 18:25:51 +03:00
//TODO: this should apply also to mechanics and cursor interface
2022-12-18 11:42:02 +02:00
bool CBattleInfoCallback : : isToReverse ( BattleHex attackerHex , const battle : : Unit * attacker , const battle : : Unit * defender ) const
2012-11-29 18:02:55 +03:00
{
2022-12-18 11:42:02 +02:00
if ( attackerHex < 0 ) //turret
2012-11-29 18:02:55 +03:00
return false ;
2022-12-18 11:42:02 +02:00
BattleHex defenderHex = defender - > getPosition ( ) ;
if ( isHexInFront ( attackerHex , defenderHex , BattleSide : : Type ( attacker - > unitSide ( ) ) ) )
2014-03-23 19:36:16 +03:00
return false ;
2022-12-18 11:42:02 +02:00
if ( defender - > doubleWide ( ) )
{
if ( isHexInFront ( attackerHex , defender - > occupiedHex ( ) , BattleSide : : Type ( attacker - > unitSide ( ) ) ) )
return false ;
2012-11-29 18:02:55 +03:00
}
2022-12-18 11:42:02 +02:00
if ( attacker - > doubleWide ( ) )
2012-11-29 18:02:55 +03:00
{
2022-12-18 11:42:02 +02:00
if ( isHexInFront ( attacker - > occupiedHex ( ) , defenderHex , BattleSide : : Type ( attacker - > unitSide ( ) ) ) )
return false ;
2012-11-29 18:02:55 +03:00
}
2022-12-18 11:42:02 +02:00
// a bit weird case since here defender is slightly behind attacker, so reversing seems preferable,
// but this is how H3 handles it which is important, e.g. for direction of dragon breath attacks
if ( attacker - > doubleWide ( ) & & defender - > doubleWide ( ) )
{
if ( isHexInFront ( attacker - > occupiedHex ( ) , defender - > occupiedHex ( ) , BattleSide : : Type ( attacker - > unitSide ( ) ) ) )
return false ;
}
return true ;
2012-11-29 18:02:55 +03:00
}
2017-07-20 06:08:49 +02:00
ReachabilityInfo : : TDistances CBattleInfoCallback : : battleGetDistances ( const battle : : Unit * unit , BattleHex assumedPosition ) const
2012-08-26 12:07:48 +03:00
{
ReachabilityInfo : : TDistances ret ;
ret . fill ( - 1 ) ;
RETURN_IF_NOT_BATTLE ( ret ) ;
2017-07-20 06:08:49 +02:00
auto reachability = getReachability ( unit ) ;
2012-08-26 12:07:48 +03:00
boost : : copy ( reachability . distances , ret . begin ( ) ) ;
return ret ;
}
2017-07-20 06:08:49 +02:00
bool CBattleInfoCallback : : battleHasDistancePenalty ( const IBonusBearer * shooter , BattleHex shooterPosition , BattleHex destHex ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( false ) ;
2017-07-20 06:08:49 +02:00
const std : : string cachingStrNoDistancePenalty = " type_NO_DISTANCE_PENALTY " ;
2020-11-11 21:43:40 +02:00
static const auto selectorNoDistancePenalty = Selector : : type ( ) ( Bonus : : NO_DISTANCE_PENALTY ) ;
2017-07-20 06:08:49 +02:00
if ( shooter - > hasBonus ( selectorNoDistancePenalty , cachingStrNoDistancePenalty ) )
2012-08-26 12:07:48 +03:00
return false ;
2017-07-20 06:08:49 +02:00
if ( auto target = battleGetUnitByPos ( destHex , true ) )
2012-08-26 12:07:48 +03:00
{
2012-11-29 18:02:55 +03:00
//If any hex of target creature is within range, there is no penalty
2023-01-15 15:52:41 +02:00
int range = GameConstants : : BATTLE_PENALTY_DISTANCE ;
auto bonus = shooter - > getBonus ( Selector : : type ( ) ( Bonus : : LIMITED_SHOOTING_RANGE ) ) ;
if ( bonus ! = nullptr & & bonus - > additionalInfo ! = CAddInfo : : NONE )
range = bonus - > additionalInfo [ 0 ] ;
if ( isEnemyUnitWithinSpecifiedRange ( shooterPosition , target , range ) )
2023-01-12 18:07:40 +02:00
return false ;
2012-08-26 12:07:48 +03:00
}
2012-11-29 18:02:55 +03:00
else
{
2017-07-20 06:08:49 +02:00
if ( BattleHex : : getDistance ( shooterPosition , destHex ) < = GameConstants : : BATTLE_PENALTY_DISTANCE )
2012-11-29 18:02:55 +03:00
return false ;
}
2012-08-26 12:07:48 +03:00
return true ;
}
2023-01-12 18:07:40 +02:00
bool CBattleInfoCallback : : isEnemyUnitWithinSpecifiedRange ( BattleHex attackerPosition , const battle : : Unit * defenderUnit , unsigned int range ) const
{
for ( auto hex : defenderUnit - > getHexes ( ) )
if ( BattleHex : : getDistance ( attackerPosition , hex ) < = range )
return true ;
2023-01-15 21:16:14 +02:00
2023-01-12 18:07:40 +02:00
return false ;
}
2023-01-13 00:35:58 +02:00
BattleHex CBattleInfoCallback : : wallPartToBattleHex ( EWallPart part ) const
2013-08-06 14:20:28 +03:00
{
RETURN_IF_NOT_BATTLE ( BattleHex : : INVALID ) ;
return WallPartToHex ( part ) ;
}
2023-01-13 00:35:58 +02:00
EWallPart CBattleInfoCallback : : battleHexToWallPart ( BattleHex hex ) const
2012-08-26 12:07:48 +03:00
{
2013-12-08 20:54:13 +03:00
RETURN_IF_NOT_BATTLE ( EWallPart : : INVALID ) ;
2012-08-26 12:07:48 +03:00
return hexToWallPart ( hex ) ;
}
2023-01-13 00:35:58 +02:00
bool CBattleInfoCallback : : isWallPartPotentiallyAttackable ( EWallPart wallPart ) const
2013-12-08 20:54:13 +03:00
{
RETURN_IF_NOT_BATTLE ( false ) ;
return wallPart ! = EWallPart : : INDESTRUCTIBLE_PART & & wallPart ! = EWallPart : : INDESTRUCTIBLE_PART_OF_GATE & &
2017-06-26 18:50:35 +02:00
wallPart ! = EWallPart : : INVALID ;
2013-12-08 20:54:13 +03:00
}
std : : vector < BattleHex > CBattleInfoCallback : : getAttackableBattleHexes ( ) const
{
std : : vector < BattleHex > attackableBattleHexes ;
RETURN_IF_NOT_BATTLE ( attackableBattleHexes ) ;
for ( auto & wallPartPair : wallParts )
{
if ( isWallPartPotentiallyAttackable ( wallPartPair . second ) )
{
2023-01-13 01:09:24 +02:00
auto wallState = battleGetWallState ( wallPartPair . second ) ;
if ( wallState = = EWallState : : REINFORCED | | wallState = = EWallState : : INTACT | | wallState = = EWallState : : DAMAGED )
2013-12-08 20:54:13 +03:00
{
attackableBattleHexes . push_back ( BattleHex ( wallPartPair . first ) ) ;
}
}
}
return attackableBattleHexes ;
}
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
int32_t CBattleInfoCallback : : battleGetSpellCost ( const spells : : Spell * sp , const CGHeroInstance * caster ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( - 1 ) ;
//TODO should be replaced using bonus system facilities (propagation onto battle node)
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
int32_t ret = caster - > getSpellCost ( sp ) ;
2012-08-26 12:07:48 +03:00
//checking for friendly stacks reducing cost of the spell and
//enemy stacks increasing it
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
int32_t manaReduction = 0 ;
int32_t manaIncrease = 0 ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
for ( auto unit : battleAliveUnits ( ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
if ( unit - > unitOwner ( ) = = caster - > tempOwner & & unit - > hasBonusOfType ( Bonus : : CHANGES_SPELL_COST_FOR_ALLY ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
vstd : : amax ( manaReduction , unit - > valOfBonuses ( Bonus : : CHANGES_SPELL_COST_FOR_ALLY ) ) ;
2012-08-26 12:07:48 +03:00
}
2017-07-20 06:08:49 +02:00
if ( unit - > unitOwner ( ) ! = caster - > tempOwner & & unit - > hasBonusOfType ( Bonus : : CHANGES_SPELL_COST_FOR_ENEMY ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
vstd : : amax ( manaIncrease , unit - > valOfBonuses ( Bonus : : CHANGES_SPELL_COST_FOR_ENEMY ) ) ;
2012-08-26 12:07:48 +03:00
}
}
return ret - manaReduction + manaIncrease ;
}
2017-07-20 06:08:49 +02:00
bool CBattleInfoCallback : : battleHasShootingPenalty ( const battle : : Unit * shooter , BattleHex destHex ) const
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
return battleHasDistancePenalty ( shooter , shooter - > getPosition ( ) , destHex ) | | battleHasWallPenalty ( shooter , shooter - > getPosition ( ) , destHex ) ;
2017-06-24 15:51:07 +02:00
}
2017-07-20 06:08:49 +02:00
bool CBattleInfoCallback : : battleIsUnitBlocked ( const battle : : Unit * unit ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( false ) ;
2017-07-20 06:08:49 +02:00
if ( unit - > hasBonusOfType ( Bonus : : SIEGE_WEAPON ) ) //siege weapons cannot be blocked
2012-08-26 12:07:48 +03:00
return false ;
2017-07-20 06:08:49 +02:00
for ( auto adjacent : battleAdjacentUnits ( unit ) )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
if ( adjacent - > unitOwner ( ) ! = unit - > unitOwner ( ) ) //blocked by enemy stack
2012-08-26 12:07:48 +03:00
return true ;
}
return false ;
}
2017-07-20 06:08:49 +02:00
std : : set < const battle : : Unit * > CBattleInfoCallback : : battleAdjacentUnits ( const battle : : Unit * unit ) const
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
std : : set < const battle : : Unit * > ret ;
RETURN_IF_NOT_BATTLE ( ret ) ;
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
for ( auto hex : unit - > getSurroundingHexes ( ) )
{
if ( auto neighbour = battleGetUnitByPos ( hex , true ) )
ret . insert ( neighbour ) ;
}
2012-08-26 12:07:48 +03:00
2017-07-20 06:08:49 +02:00
return ret ;
2012-08-26 12:07:48 +03:00
}
2016-09-09 19:30:36 +02:00
SpellID CBattleInfoCallback : : getRandomBeneficialSpell ( CRandomGenerator & rand , const CStack * subject ) const
2012-08-26 12:07:48 +03:00
{
2013-02-11 02:24:57 +03:00
RETURN_IF_NOT_BATTLE ( SpellID : : NONE ) ;
2014-12-01 23:24:36 +02:00
//This is complete list. No spells from mods.
//todo: this should be Spellbook of caster Stack
2016-02-01 19:03:57 +02:00
static const std : : set < SpellID > allPossibleSpells =
2014-12-01 23:24:36 +02:00
{
SpellID : : AIR_SHIELD ,
SpellID : : ANTI_MAGIC ,
2016-02-01 19:03:57 +02:00
SpellID : : BLESS ,
2014-12-01 23:24:36 +02:00
SpellID : : BLOODLUST ,
SpellID : : COUNTERSTRIKE ,
SpellID : : CURE ,
2016-02-01 19:03:57 +02:00
SpellID : : FIRE_SHIELD ,
2014-12-01 23:24:36 +02:00
SpellID : : FORTUNE ,
SpellID : : HASTE ,
SpellID : : MAGIC_MIRROR ,
2016-02-01 19:03:57 +02:00
SpellID : : MIRTH ,
2014-12-01 23:24:36 +02:00
SpellID : : PRAYER ,
SpellID : : PRECISION ,
SpellID : : PROTECTION_FROM_AIR ,
SpellID : : PROTECTION_FROM_EARTH ,
SpellID : : PROTECTION_FROM_FIRE ,
SpellID : : PROTECTION_FROM_WATER ,
SpellID : : SHIELD ,
SpellID : : SLAYER ,
SpellID : : STONE_SKIN
} ;
std : : vector < SpellID > beneficialSpells ;
2016-02-01 19:03:57 +02:00
2017-07-20 06:08:49 +02:00
auto getAliveEnemy = [ = ] ( const std : : function < bool ( const CStack * ) > & pred ) - > const CStack *
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
auto stacks = battleGetStacksIf ( [ = ] ( const CStack * stack )
2012-08-26 12:07:48 +03:00
{
2017-07-20 06:08:49 +02:00
return pred ( stack ) & & stack - > owner ! = subject - > owner & & stack - > isValidTarget ( false ) ;
2014-12-01 23:24:36 +02:00
} ) ;
2017-07-20 06:08:49 +02:00
if ( stacks . empty ( ) )
return nullptr ;
else
return stacks . front ( ) ;
2014-12-01 23:24:36 +02:00
} ;
2012-08-26 12:07:48 +03:00
2022-05-19 14:14:50 +02:00
for ( const SpellID & spellID : allPossibleSpells )
2014-12-01 23:24:36 +02:00
{
2016-10-01 06:28:03 +02:00
std : : stringstream cachingStr ;
cachingStr < < " source_ " < < Bonus : : SPELL_EFFECT < < " id_ " < < spellID . num ;
if ( subject - > hasBonus ( Selector : : source ( Bonus : : SPELL_EFFECT , spellID ) , Selector : : all , cachingStr . str ( ) )
2017-06-26 18:50:35 +02:00
//TODO: this ability has special limitations
2017-07-20 06:08:49 +02:00
| | ! ( spellID . toSpell ( ) - > canBeCast ( this , spells : : Mode : : CREATURE_ACTIVE , subject ) ) )
2014-12-01 23:24:36 +02:00
continue ;
2012-08-26 12:07:48 +03:00
2014-12-01 23:24:36 +02:00
switch ( spellID )
{
case SpellID : : SHIELD :
case SpellID : : FIRE_SHIELD : // not if all enemy units are shooters
2017-06-24 15:51:07 +02:00
{
auto walker = getAliveEnemy ( [ & ] ( const CStack * stack ) //look for enemy, non-shooting stack
2016-02-01 19:03:57 +02:00
{
2017-07-04 13:24:46 +02:00
return ! stack - > canShoot ( ) ;
2017-06-24 15:51:07 +02:00
} ) ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
if ( ! walker )
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : AIR_SHIELD : //only against active shooters
2017-06-24 15:51:07 +02:00
{
auto shooter = getAliveEnemy ( [ & ] ( const CStack * stack ) //look for enemy, non-shooting stack
2014-12-01 23:24:36 +02:00
{
2017-07-04 13:24:46 +02:00
return stack - > canShoot ( ) ;
2017-06-24 15:51:07 +02:00
} ) ;
if ( ! shooter )
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : ANTI_MAGIC :
case SpellID : : MAGIC_MIRROR :
case SpellID : : PROTECTION_FROM_AIR :
case SpellID : : PROTECTION_FROM_EARTH :
case SpellID : : PROTECTION_FROM_FIRE :
2016-02-01 19:03:57 +02:00
case SpellID : : PROTECTION_FROM_WATER :
2017-06-24 15:51:07 +02:00
{
2017-07-01 10:34:00 +02:00
const ui8 enemySide = 1 - subject - > side ;
2017-06-24 15:51:07 +02:00
//todo: only if enemy has spellbook
if ( ! battleHasHero ( enemySide ) ) //only if there is enemy hero
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : CURE : //only damaged units
2017-06-24 15:51:07 +02:00
{
//do not cast on affected by debuffs
2017-07-04 13:24:46 +02:00
if ( ! subject - > canBeHealed ( ) )
2017-06-24 15:51:07 +02:00
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : BLOODLUST :
2017-06-24 15:51:07 +02:00
{
2017-07-04 13:24:46 +02:00
if ( subject - > canShoot ( ) ) //TODO: if can shoot - only if enemy units are adjacent
2017-06-24 15:51:07 +02:00
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : PRECISION :
2017-06-24 15:51:07 +02:00
{
2017-07-04 13:24:46 +02:00
if ( ! subject - > canShoot ( ) )
2017-06-24 15:51:07 +02:00
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
case SpellID : : SLAYER : //only if monsters are present
2017-06-24 15:51:07 +02:00
{
2017-06-26 18:50:35 +02:00
auto kingMonster = getAliveEnemy ( [ & ] ( const CStack * stack ) - > bool //look for enemy, non-shooting stack
2014-12-01 23:24:36 +02:00
{
2020-11-11 21:43:40 +02:00
const auto isKing = Selector : : type ( ) ( Bonus : : KING1 )
. Or ( Selector : : type ( ) ( Bonus : : KING2 ) )
. Or ( Selector : : type ( ) ( Bonus : : KING3 ) ) ;
2013-07-02 15:08:30 +03:00
2017-06-24 15:51:07 +02:00
return stack - > hasBonus ( isKing ) ;
} ) ;
2012-08-26 12:07:48 +03:00
2017-06-24 15:51:07 +02:00
if ( ! kingMonster )
continue ;
}
2014-12-01 23:24:36 +02:00
break ;
2012-08-26 12:07:48 +03:00
}
2016-02-01 19:03:57 +02:00
beneficialSpells . push_back ( spellID ) ;
2012-08-26 12:07:48 +03:00
}
2014-12-01 23:24:36 +02:00
if ( ! beneficialSpells . empty ( ) )
2014-04-10 20:11:09 +03:00
{
2016-09-09 19:30:36 +02:00
return * RandomGeneratorUtil : : nextItem ( beneficialSpells , rand ) ;
2014-04-10 20:11:09 +03:00
}
2012-08-26 12:07:48 +03:00
else
2014-04-10 20:11:09 +03:00
{
2013-02-11 02:24:57 +03:00
return SpellID : : NONE ;
2014-04-10 20:11:09 +03:00
}
2012-08-26 12:07:48 +03:00
}
2016-09-09 19:30:36 +02:00
SpellID CBattleInfoCallback : : getRandomCastedSpell ( CRandomGenerator & rand , const CStack * caster ) const
2012-08-26 12:07:48 +03:00
{
2013-02-11 02:24:57 +03:00
RETURN_IF_NOT_BATTLE ( SpellID : : NONE ) ;
2012-08-26 12:07:48 +03:00
2020-11-11 21:43:40 +02:00
TConstBonusListPtr bl = caster - > getBonuses ( Selector : : type ( ) ( Bonus : : SPELLCASTER ) ) ;
2012-08-26 12:07:48 +03:00
if ( ! bl - > size ( ) )
2013-02-11 02:24:57 +03:00
return SpellID : : NONE ;
2012-08-26 12:07:48 +03:00
int totalWeight = 0 ;
2016-09-19 23:36:35 +02:00
for ( auto b : * bl )
2012-08-26 12:07:48 +03:00
{
2018-03-12 07:20:18 +02:00
totalWeight + = std : : max ( b - > additionalInfo [ 0 ] , 1 ) ; //minimal chance to cast is 1
2012-08-26 12:07:48 +03:00
}
2016-09-09 19:30:36 +02:00
int randomPos = rand . nextInt ( totalWeight - 1 ) ;
2016-09-19 23:36:35 +02:00
for ( auto b : * bl )
2012-08-26 12:07:48 +03:00
{
2018-03-12 07:20:18 +02:00
randomPos - = std : : max ( b - > additionalInfo [ 0 ] , 1 ) ;
2012-08-26 12:07:48 +03:00
if ( randomPos < 0 )
{
2013-02-11 02:24:57 +03:00
return SpellID ( b - > subtype ) ;
2012-08-26 12:07:48 +03:00
}
}
2013-02-11 02:24:57 +03:00
return SpellID : : NONE ;
2012-08-26 12:07:48 +03:00
}
2013-03-03 20:06:03 +03:00
int CBattleInfoCallback : : battleGetSurrenderCost ( PlayerColor Player ) const
2012-08-26 12:07:48 +03:00
{
RETURN_IF_NOT_BATTLE ( - 3 ) ;
if ( ! battleCanSurrender ( Player ) )
return - 1 ;
2017-07-20 06:08:49 +02:00
const auto sideOpt = playerToSide ( Player ) ;
if ( ! sideOpt )
2016-11-28 02:33:39 +02:00
return - 1 ;
2017-07-20 06:08:49 +02:00
const auto side = sideOpt . get ( ) ;
2016-11-28 02:33:39 +02:00
2012-08-26 12:07:48 +03:00
int ret = 0 ;
double discount = 0 ;
2017-07-20 06:08:49 +02:00
for ( auto unit : battleAliveUnits ( side ) )
ret + = unit - > getRawSurrenderCost ( ) ;
if ( const CGHeroInstance * h = battleGetFightingHero ( side ) )
2012-08-26 12:07:48 +03:00
discount + = h - > valOfBonuses ( Bonus : : SURRENDER_DISCOUNT ) ;
2020-10-01 10:38:06 +02:00
ret = static_cast < int > ( ret * ( 100.0 - discount ) / 100.0 ) ;
2012-08-26 12:07:48 +03:00
vstd : : amax ( ret , 0 ) ; //no negative costs for >100% discounts (impossible in original H3 mechanics, but some day...)
return ret ;
}
2017-09-06 00:03:32 +02:00
si8 CBattleInfoCallback : : battleMinSpellLevel ( ui8 side ) const
{
2017-11-13 15:13:55 +02:00
const IBonusBearer * node = nullptr ;
2017-09-06 00:03:32 +02:00
if ( const CGHeroInstance * h = battleGetFightingHero ( side ) )
node = h ;
else
node = getBattleNode ( ) ;
if ( ! node )
2017-09-09 21:01:12 +02:00
return 0 ;
2017-09-06 00:03:32 +02:00
2020-11-11 21:43:40 +02:00
auto b = node - > getBonuses ( Selector : : type ( ) ( Bonus : : BLOCK_MAGIC_BELOW ) ) ;
2017-09-06 00:03:32 +02:00
if ( b - > size ( ) )
return b - > totalValue ( ) ;
2017-09-09 21:01:12 +02:00
return 0 ;
2017-09-06 00:03:32 +02:00
}
2015-09-16 17:28:14 +02:00
si8 CBattleInfoCallback : : battleMaxSpellLevel ( ui8 side ) const
2012-08-26 12:07:48 +03:00
{
2015-09-16 17:28:14 +02:00
const IBonusBearer * node = nullptr ;
if ( const CGHeroInstance * h = battleGetFightingHero ( side ) )
2012-08-26 12:07:48 +03:00
node = h ;
2015-09-16 17:28:14 +02:00
else
node = getBattleNode ( ) ;
2012-08-26 12:07:48 +03:00
if ( ! node )
2012-08-26 12:59:07 +03:00
return GameConstants : : SPELL_LEVELS ;
2012-08-26 12:07:48 +03:00
//We can't "just get value" - it'd be 0 if there are bonuses (and all would be blocked)
2020-11-11 21:43:40 +02:00
auto b = node - > getBonuses ( Selector : : type ( ) ( Bonus : : BLOCK_MAGIC_ABOVE ) ) ;
2012-08-26 12:07:48 +03:00
if ( b - > size ( ) )
return b - > totalValue ( ) ;
return GameConstants : : SPELL_LEVELS ;
}
2012-09-20 19:55:21 +03:00
2013-09-29 23:54:29 +03:00
boost : : optional < int > CBattleInfoCallback : : battleIsFinished ( ) const
{
2017-07-20 06:08:49 +02:00
auto units = battleGetUnitsIf ( [ = ] ( const battle : : Unit * unit )
{
2022-04-16 12:18:41 +02:00
return unit - > alive ( ) & & ! unit - > isTurret ( ) & & ! unit - > hasBonusOfType ( Bonus : : SIEGE_WEAPON ) ;
2017-07-20 06:08:49 +02:00
} ) ;
2013-09-29 23:54:29 +03:00
2017-07-20 06:08:49 +02:00
std : : array < bool , 2 > hasUnit = { false , false } ; //index is BattleSide
2013-09-29 23:54:29 +03:00
2017-07-20 06:08:49 +02:00
for ( auto & unit : units )
2013-09-29 23:54:29 +03:00
{
2017-07-20 06:08:49 +02:00
//todo: move SIEGE_WEAPON check to Unit state
2022-04-16 12:18:41 +02:00
hasUnit . at ( unit - > unitSide ( ) ) = true ;
if ( hasUnit [ 0 ] & & hasUnit [ 1 ] )
return boost : : none ;
}
hasUnit = { false , false } ;
for ( auto & unit : units )
{
if ( ! unit - > isClone ( ) & & ! unit - > acquireState ( ) - > summoned & & ! dynamic_cast < const CCommanderInstance * > ( unit ) )
2013-09-29 23:54:29 +03:00
{
2017-07-20 06:08:49 +02:00
hasUnit . at ( unit - > unitSide ( ) ) = true ;
2013-09-29 23:54:29 +03:00
}
}
2017-07-20 06:08:49 +02:00
if ( ! hasUnit [ 0 ] & & ! hasUnit [ 1 ] )
2013-09-29 23:54:29 +03:00
return 2 ;
2017-07-20 06:08:49 +02:00
if ( ! hasUnit [ 1 ] )
2013-09-29 23:54:29 +03:00
return 0 ;
2022-04-16 12:18:41 +02:00
else
2013-09-30 00:31:14 +03:00
return 1 ;
2013-09-29 23:54:29 +03:00
}
2022-07-26 15:07:42 +02:00
VCMI_LIB_NAMESPACE_END