| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * CStack.h, part of VCMI engine | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: listed in file AUTHORS in main folder | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * License: GNU General Public License v2.0 or later | 
					
						
							|  |  |  |  * Full text of license available in license.txt file, in main folder | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-07-09 22:15:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | #include "JsonNode.h"
 | 
					
						
							| 
									
										
										
										
											2023-05-01 20:29:53 +03:00
										 |  |  | #include "bonuses/Bonus.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-30 17:35:15 +03:00
										 |  |  | #include "bonuses/CBonusSystemNode.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | #include "CCreatureHandler.h" //todo: remove
 | 
					
						
							| 
									
										
										
										
											2017-06-24 16:42:05 +02:00
										 |  |  | #include "battle/BattleHex.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | #include "mapObjects/CGHeroInstance.h" // for commander serialization
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | #include "battle/CUnitState.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 16:07:42 +03:00
										 |  |  | VCMI_LIB_NAMESPACE_BEGIN | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | struct BattleStackAttacked; | 
					
						
							|  |  |  | class BattleInfo; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 14:30:54 +03:00
										 |  |  | //Represents STACK_BATTLE nodes
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | class DLL_LINKAGE CStack : public CBonusSystemNode, public battle::CUnitState, public battle::IUnitEnvironment | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-27 20:43:20 +03:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-03-14 00:26:44 +03:00
										 |  |  | 	ui32 ID = -1; //unique ID of stack
 | 
					
						
							|  |  |  | 	const CCreature * type = nullptr; | 
					
						
							| 
									
										
										
										
											2022-09-29 11:44:46 +02:00
										 |  |  | 	TerrainId nativeTerrain; //tmp variable to save native terrain value on battle init
 | 
					
						
							| 
									
										
										
										
											2023-03-14 00:26:44 +03:00
										 |  |  | 	ui32 baseAmount = -1; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PlayerColor owner; //owner - player color (255 for neutrals)
 | 
					
						
							| 
									
										
										
										
											2023-03-14 00:26:44 +03:00
										 |  |  | 	ui8 side = 1; | 
					
						
							| 
									
										
										
										
											2023-04-27 20:43:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SlotID slot;  //slot - position in garrison (may be 255 for neutrals/called creatures)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	const CStackInstance * base = nullptr; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
 | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	BattleHex initialPosition; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-14 00:26:44 +03:00
										 |  |  | 	CStack(const CStackInstance * base, const PlayerColor & O, int I, ui8 Side, const SlotID & S); | 
					
						
							|  |  |  | 	CStack(const CStackBasicDescriptor * stack, const PlayerColor & O, int I, ui8 Side, const SlotID & S = SlotID(255)); | 
					
						
							| 
									
										
										
										
											2017-07-18 00:04:00 +03:00
										 |  |  | 	CStack(); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	~CStack(); | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	std::string nodeName() const override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 	void localInit(BattleInfo * battleInfo); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	std::string getName() const; //plural or singular
 | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
 | 
					
						
							| 
									
										
										
										
											2018-04-17 14:59:30 +02:00
										 |  |  | 	bool isOnNativeTerrain() const; | 
					
						
							| 
									
										
										
										
											2022-09-29 11:44:46 +02:00
										 |  |  | 	bool isOnTerrain(TerrainId terrain) const; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	ui32 level() const; | 
					
						
							|  |  |  | 	si32 magicResistance() const override; //include aura of resistance
 | 
					
						
							|  |  |  | 	std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
 | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 	const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-28 21:05:25 +03:00
										 |  |  | 	static std::vector<BattleHex> meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID); | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 11:34:00 +03:00
										 |  |  | 	BattleHex::EDir destShiftDir() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const; //requires bsa.damageAmout filled
 | 
					
						
							| 
									
										
										
										
											2023-03-14 00:26:44 +03:00
										 |  |  | 	static void prepareAttacked(BattleStackAttacked & bsa, | 
					
						
							|  |  |  | 								vstd::RNG & rand, | 
					
						
							|  |  |  | 								const std::shared_ptr<battle::CUnitState> & customState); //requires bsa.damageAmout filled
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	const CCreature * unitType() const override; | 
					
						
							| 
									
										
										
										
											2017-07-09 19:49:52 +03:00
										 |  |  | 	int32_t unitBaseAmount() const override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	uint32_t unitId() const override; | 
					
						
							|  |  |  | 	ui8 unitSide() const override; | 
					
						
							|  |  |  | 	PlayerColor unitOwner() const override; | 
					
						
							|  |  |  | 	SlotID unitSlot() const override; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	std::string getDescription() const override; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 	bool unitHasAmmoCart(const battle::Unit * unit) const override; | 
					
						
							|  |  |  | 	PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override; | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +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 17:58:30 +03:00
										 |  |  | 	void spendMana(ServerCallback * server, const int spellCost) const override; | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 18:56:28 +03:00
										 |  |  | 	const IBonusBearer* getBonusBearer() const override; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2021-09-12 14:30:54 +03:00
										 |  |  | 	PlayerColor getOwner() const override | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return this->owner; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 	template <typename Handler> void serialize(Handler & h, const int version) | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 		//this assumes that stack objects is newly created
 | 
					
						
							|  |  |  | 		//stackState is not serialized here
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 		assert(isIndependentNode()); | 
					
						
							| 
									
										
										
										
											2017-07-31 16:35:42 +03:00
										 |  |  | 		h & static_cast<CBonusSystemNode&>(*this); | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 		h & type; | 
					
						
							| 
									
										
										
										
											2017-07-31 16:35:42 +03:00
										 |  |  | 		h & ID; | 
					
						
							|  |  |  | 		h & baseAmount; | 
					
						
							|  |  |  | 		h & owner; | 
					
						
							|  |  |  | 		h & slot; | 
					
						
							|  |  |  | 		h & side; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 		h & initialPosition; | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-09 15:49:37 +03:00
										 |  |  | 		const CArmedInstance * army = (base ? base->armyObj : nullptr); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 		SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(h.saving) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-07-31 16:35:42 +03:00
										 |  |  | 			h & army; | 
					
						
							|  |  |  | 			h & extSlot; | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-07-31 16:35:42 +03:00
										 |  |  | 			h & army; | 
					
						
							|  |  |  | 			h & extSlot; | 
					
						
							| 
									
										
										
										
											2017-07-20 07:08:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 			if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2023-04-18 00:11:16 +03:00
										 |  |  | 				const auto * hero = dynamic_cast<const CGHeroInstance *>(army); | 
					
						
							| 
									
										
										
										
											2017-07-09 15:49:37 +03:00
										 |  |  | 				assert(hero); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 				base = hero->commander; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				//no external slot possible, so no base stack
 | 
					
						
							|  |  |  | 				base = nullptr; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				base = nullptr; | 
					
						
							| 
									
										
										
										
											2023-01-02 18:00:51 +02:00
										 |  |  | 				logGlobal->warn("%s doesn't have a base stack!", type->getNameSingularTranslated()); | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				base = &army->getStack(extSlot); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-04 14:24:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	const BattleInfo * battle; //do not serialize
 | 
					
						
							| 
									
										
										
										
											2017-03-17 16:48:44 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2022-07-26 16:07:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | VCMI_LIB_NAMESPACE_END |