2010-12-25 21:23:30 +02:00
|
|
|
/*
|
2015-12-08 08:53:14 +02:00
|
|
|
* BattleAction.cpp, part of VCMI engine
|
2010-12-25 21:23:30 +02: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
|
|
|
|
*
|
|
|
|
*/
|
2010-12-25 03:43:40 +02:00
|
|
|
|
2017-07-01 20:05:10 +02:00
|
|
|
#include "StdInc.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
#include "BattleAction.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "Unit.h"
|
|
|
|
#include "CBattleInfoCallback.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static const int32_t INVALID_UNIT_ID = -1000;
|
2013-02-04 00:05:44 +03:00
|
|
|
|
2013-11-06 16:42:58 +03:00
|
|
|
BattleAction::BattleAction():
|
|
|
|
side(-1),
|
|
|
|
stackNumber(-1),
|
2017-07-20 06:08:49 +02:00
|
|
|
actionType(EActionType::INVALID),
|
|
|
|
actionSubtype(-1)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction BattleAction::makeHeal(const battle::Unit * healer, const battle::Unit * healed)
|
2013-08-25 16:03:29 +03:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = healer->unitSide();
|
|
|
|
ba.actionType = EActionType::STACK_HEAL;
|
|
|
|
ba.stackNumber = healer->unitId();
|
|
|
|
ba.aimToUnit(healed);
|
2013-08-25 16:03:29 +03:00
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction BattleAction::makeDefend(const battle::Unit * stack)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = stack->unitSide();
|
|
|
|
ba.actionType = EActionType::DEFEND;
|
|
|
|
ba.stackNumber = stack->unitId();
|
2010-12-25 03:43:40 +02:00
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction BattleAction::makeMeleeAttack(const battle::Unit * stack, BattleHex destination, BattleHex attackFrom, bool returnAfterAttack)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = stack->unitSide(); //FIXME: will it fail if stack mind controlled?
|
|
|
|
ba.actionType = EActionType::WALK_AND_ATTACK;
|
|
|
|
ba.stackNumber = stack->unitId();
|
|
|
|
ba.aimToHex(attackFrom);
|
|
|
|
ba.aimToHex(destination);
|
2023-05-01 00:20:01 +02:00
|
|
|
if(returnAfterAttack && stack->hasBonusOfType(BonusType::RETURN_AFTER_STRIKE))
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.aimToHex(stack->getPosition());
|
2010-12-25 03:43:40 +02:00
|
|
|
return ba;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
BattleAction BattleAction::makeWait(const battle::Unit * stack)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = stack->unitSide();
|
|
|
|
ba.actionType = EActionType::WAIT;
|
|
|
|
ba.stackNumber = stack->unitId();
|
2010-12-25 03:43:40 +02:00
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction BattleAction::makeShotAttack(const battle::Unit * shooter, const battle::Unit * target)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = shooter->unitSide();
|
|
|
|
ba.actionType = EActionType::SHOOT;
|
|
|
|
ba.stackNumber = shooter->unitId();
|
|
|
|
ba.aimToUnit(target);
|
2010-12-25 03:43:40 +02:00
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2023-02-15 00:44:59 +02:00
|
|
|
BattleAction BattleAction::makeCreatureSpellcast(const battle::Unit * stack, const battle::Target & target, const SpellID & spellID)
|
2019-05-04 05:42:55 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
|
|
|
ba.actionType = EActionType::MONSTER_SPELL;
|
|
|
|
ba.actionSubtype = spellID;
|
|
|
|
ba.setTarget(target);
|
|
|
|
ba.side = stack->unitSide();
|
|
|
|
ba.stackNumber = stack->unitId();
|
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleAction BattleAction::makeMove(const battle::Unit * stack, BattleHex dest)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
BattleAction ba;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.side = stack->unitSide();
|
|
|
|
ba.actionType = EActionType::WALK;
|
|
|
|
ba.stackNumber = stack->unitId();
|
|
|
|
ba.aimToHex(dest);
|
2010-12-25 03:43:40 +02:00
|
|
|
return ba;
|
2011-02-12 18:12:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction BattleAction::makeEndOFTacticPhase(ui8 side)
|
|
|
|
{
|
|
|
|
BattleAction ba;
|
|
|
|
ba.side = side;
|
2017-07-20 06:08:49 +02:00
|
|
|
ba.actionType = EActionType::END_TACTIC_PHASE;
|
2011-02-12 18:12:48 +02:00
|
|
|
return ba;
|
|
|
|
}
|
2013-11-09 19:25:20 +03:00
|
|
|
|
2022-10-14 10:24:29 +02:00
|
|
|
BattleAction BattleAction::makeSurrender(ui8 side)
|
|
|
|
{
|
|
|
|
BattleAction ba;
|
|
|
|
ba.side = side;
|
|
|
|
ba.actionType = EActionType::SURRENDER;
|
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleAction BattleAction::makeRetreat(ui8 side)
|
|
|
|
{
|
|
|
|
BattleAction ba;
|
|
|
|
ba.side = side;
|
|
|
|
ba.actionType = EActionType::RETREAT;
|
|
|
|
return ba;
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:38:10 +02:00
|
|
|
std::string BattleAction::toString() const
|
2013-11-09 19:25:20 +03:00
|
|
|
{
|
|
|
|
std::stringstream actionTypeStream;
|
2017-08-11 13:38:10 +02:00
|
|
|
actionTypeStream << actionType;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
std::stringstream targetStream;
|
|
|
|
|
|
|
|
for(const DestinationInfo & info : target)
|
|
|
|
{
|
|
|
|
if(info.unitValue == INVALID_UNIT_ID)
|
|
|
|
{
|
|
|
|
targetStream << info.hexValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
targetStream << info.unitValue;
|
|
|
|
targetStream << "@";
|
|
|
|
targetStream << info.hexValue;
|
|
|
|
}
|
|
|
|
targetStream << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::format fmt("{BattleAction: side '%d', stackNumber '%d', actionType '%s', actionSubtype '%d', target {%s}}");
|
|
|
|
fmt % static_cast<int>(side) % stackNumber % actionTypeStream.str() % actionSubtype % targetStream.str();
|
2017-08-11 13:38:10 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
2013-11-09 19:25:20 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void BattleAction::aimToHex(const BattleHex & destination)
|
|
|
|
{
|
|
|
|
DestinationInfo info;
|
|
|
|
info.hexValue = destination;
|
|
|
|
info.unitValue = INVALID_UNIT_ID;
|
|
|
|
|
|
|
|
target.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleAction::aimToUnit(const battle::Unit * destination)
|
|
|
|
{
|
|
|
|
DestinationInfo info;
|
|
|
|
info.hexValue = destination->getPosition();
|
|
|
|
info.unitValue = destination->unitId();
|
|
|
|
|
|
|
|
target.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
battle::Target BattleAction::getTarget(const CBattleInfoCallback * cb) const
|
|
|
|
{
|
|
|
|
battle::Target ret;
|
|
|
|
|
2023-02-15 00:44:59 +02:00
|
|
|
for(const auto & destination : target)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(destination.unitValue == INVALID_UNIT_ID)
|
|
|
|
ret.emplace_back(destination.hexValue);
|
|
|
|
else
|
|
|
|
ret.emplace_back(cb->battleGetUnitByID(destination.unitValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleAction::setTarget(const battle::Target & target_)
|
|
|
|
{
|
|
|
|
target.clear();
|
2023-02-15 00:44:59 +02:00
|
|
|
for(const auto & destination : target_)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(destination.unitValue == nullptr)
|
|
|
|
aimToHex(destination.hexValue);
|
|
|
|
else
|
|
|
|
aimToUnit(destination.unitValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-11 13:38:10 +02:00
|
|
|
std::ostream & operator<<(std::ostream & os, const BattleAction & ba)
|
|
|
|
{
|
|
|
|
os << ba.toString();
|
|
|
|
return os;
|
2013-11-09 19:25:20 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|