| 
									
										
										
										
											2010-12-25 19:23:30 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2015-12-08 09:53:14 +03:00
										 |  |  |  * BattleAction.cpp, part of VCMI engine | 
					
						
							| 
									
										
										
										
											2010-12-25 19:23:30 +00: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 01:43:40 +00: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 07:08:49 +03:00
										 |  |  | #include "Unit.h"
 | 
					
						
							|  |  |  | #include "CBattleInfoCallback.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-24 16:42:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 16:07:42 +03:00
										 |  |  | VCMI_LIB_NAMESPACE_BEGIN | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | static const int32_t INVALID_UNIT_ID = -1000; | 
					
						
							| 
									
										
										
										
											2013-02-03 21:05:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-06 13:42:58 +00:00
										 |  |  | BattleAction::BattleAction(): | 
					
						
							|  |  |  | 	side(-1), | 
					
						
							|  |  |  | 	stackNumber(-1), | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	actionType(EActionType::INVALID), | 
					
						
							|  |  |  | 	actionSubtype(-1) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | BattleAction BattleAction::makeHeal(const battle::Unit * healer, const battle::Unit * healed) | 
					
						
							| 
									
										
										
										
											2013-08-25 13:03:29 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.side = healer->unitSide(); | 
					
						
							|  |  |  | 	ba.actionType = EActionType::STACK_HEAL; | 
					
						
							|  |  |  | 	ba.stackNumber = healer->unitId(); | 
					
						
							|  |  |  | 	ba.aimToUnit(healed); | 
					
						
							| 
									
										
										
										
											2013-08-25 13:03:29 +00:00
										 |  |  | 	return ba; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | BattleAction BattleAction::makeDefend(const battle::Unit * stack) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.side = stack->unitSide(); | 
					
						
							|  |  |  | 	ba.actionType = EActionType::DEFEND; | 
					
						
							|  |  |  | 	ba.stackNumber = stack->unitId(); | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | 	return ba; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | BattleAction BattleAction::makeMeleeAttack(const battle::Unit * stack, BattleHex destination, BattleHex attackFrom, bool returnAfterAttack) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03: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); | 
					
						
							|  |  |  | 	if(returnAfterAttack && stack->hasBonusOfType(Bonus::RETURN_AFTER_STRIKE)) | 
					
						
							|  |  |  | 		ba.aimToHex(stack->getPosition()); | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | 	return ba; | 
					
						
							| 
									
										
										
										
											2011-01-08 18:33:40 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | BattleAction BattleAction::makeWait(const battle::Unit * stack) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.side = stack->unitSide(); | 
					
						
							|  |  |  | 	ba.actionType = EActionType::WAIT; | 
					
						
							|  |  |  | 	ba.stackNumber = stack->unitId(); | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | 	return ba; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | BattleAction BattleAction::makeShotAttack(const battle::Unit * shooter, const battle::Unit * target) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.side = shooter->unitSide(); | 
					
						
							|  |  |  | 	ba.actionType = EActionType::SHOOT; | 
					
						
							|  |  |  | 	ba.stackNumber = shooter->unitId(); | 
					
						
							|  |  |  | 	ba.aimToUnit(target); | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | 	return ba; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 05:42:55 +02:00
										 |  |  | BattleAction BattleAction::makeCreatureSpellcast(const battle::Unit * stack, const battle::Target & target, SpellID spellID) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	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 07:08:49 +03:00
										 |  |  | BattleAction BattleAction::makeMove(const battle::Unit * stack, BattleHex dest) | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.side = stack->unitSide(); | 
					
						
							|  |  |  | 	ba.actionType = EActionType::WALK; | 
					
						
							|  |  |  | 	ba.stackNumber = stack->unitId(); | 
					
						
							|  |  |  | 	ba.aimToHex(dest); | 
					
						
							| 
									
										
										
										
											2010-12-25 01:43:40 +00:00
										 |  |  | 	return ba; | 
					
						
							| 
									
										
										
										
											2011-02-12 16:12:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BattleAction BattleAction::makeEndOFTacticPhase(ui8 side) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BattleAction ba; | 
					
						
							|  |  |  | 	ba.side = side; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	ba.actionType = EActionType::END_TACTIC_PHASE; | 
					
						
							| 
									
										
										
										
											2011-02-12 16:12:48 +00:00
										 |  |  | 	return ba; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-09 16:25:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 11:24:29 +03: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 14:38:10 +03:00
										 |  |  | std::string BattleAction::toString() const | 
					
						
							| 
									
										
										
										
											2013-11-09 16:25:20 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	std::stringstream actionTypeStream; | 
					
						
							| 
									
										
										
										
											2017-08-11 14:38:10 +03:00
										 |  |  | 	actionTypeStream << actionType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03: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 14:38:10 +03:00
										 |  |  | 	return fmt.str(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-09 16:25:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03: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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(auto & destination : target) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		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(); | 
					
						
							|  |  |  | 	for(auto & destination : target_) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(destination.unitValue == nullptr) | 
					
						
							|  |  |  | 			aimToHex(destination.hexValue); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			aimToUnit(destination.unitValue); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 14:38:10 +03:00
										 |  |  | std::ostream & operator<<(std::ostream & os, const BattleAction & ba) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	os << ba.toString(); | 
					
						
							|  |  |  | 	return os; | 
					
						
							| 
									
										
										
										
											2013-11-09 16:25:20 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-26 16:07:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | VCMI_LIB_NAMESPACE_END |