1 /*
2  * Copyright (C) 2002-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 #ifndef WL_LOGIC_MAP_OBJECTS_TRIBES_BATTLE_H
20 #define WL_LOGIC_MAP_OBJECTS_TRIBES_BATTLE_H
21 
22 #include "logic/map_objects/map_object.h"
23 
24 namespace Widelands {
25 class Soldier;
26 
27 class BattleDescr : public MapObjectDescr {
28 public:
BattleDescr(char const * const init_name,char const * const init_descname)29 	BattleDescr(char const* const init_name, char const* const init_descname)
30 	   : MapObjectDescr(MapObjectType::BATTLE, init_name, init_descname, "") {
31 	}
~BattleDescr()32 	~BattleDescr() override {
33 	}
34 
35 private:
36 	DISALLOW_COPY_AND_ASSIGN(BattleDescr);
37 };
38 
39 /**
40  * Manages the battle between two opposing soldiers.
41  *
42  * A \ref Battle object is created using the \ref create() function as soon as
43  * a soldier decides he wants to attack someone else. A battle always has both
44  * Soldiers defined, the battle object must be destroyed as soon as there is no
45  * other Soldier to battle anymore.
46  */
47 class Battle : public MapObject {
48 public:
49 	const BattleDescr& descr() const;
50 
51 	Battle();                           //  for loading an existing battle from a savegame
52 	Battle(Game&, Soldier*, Soldier*);  //  to create a new battle in the game
53 
54 	// Implements MapObject.
55 	bool init(EditorGameBase&) override;
56 	void cleanup(EditorGameBase&) override;
has_new_save_support()57 	bool has_new_save_support() override {
58 		return true;
59 	}
60 	void save(EditorGameBase&, MapObjectSaver&, FileWrite&) override;
61 	static MapObject::Loader* load(EditorGameBase&, MapObjectLoader&, FileRead&);
62 
63 	// Cancel this battle immediately and schedule destruction.
64 	void cancel(Game&, Soldier&);
65 
66 	// Returns true if the battle should not be interrupted.
67 	bool locked(Game&);
68 
69 	// The two soldiers involved in this fight.
first()70 	Soldier* first() {
71 		return first_;
72 	}
second()73 	Soldier* second() {
74 		return second_;
75 	}
76 
77 	uint32_t get_pending_damage(const Soldier* for_whom) const;
78 
79 	// Returns the other soldier involved in this battle. CHECKs that the given
80 	// soldier is participating in this battle. Can return nullptr, probably when the
81 	// opponent has died.
82 	Soldier* opponent(const Soldier&) const;
83 
84 	// Called by the battling soldiers once they've met on a common node and are
85 	// idle.
86 	void get_battle_work(Game&, Soldier&);
87 
88 private:
89 	struct Loader : public MapObject::Loader {
90 		virtual void load(FileRead&);
91 		void load_pointers() override;
92 
93 		Serial first_;
94 		Serial second_;
95 	};
96 
97 	void calculate_round(Game&);
98 
99 	Soldier* first_;
100 	Soldier* second_;
101 
102 	/**
103 	 * Gametime when the battle was created.
104 	 */
105 	int32_t creationtime_;
106 
107 	/**
108 	 * 1 if only the first soldier is ready, 2 if only the second soldier
109 	 * is ready, 3 if both are ready.
110 	 */
111 	uint8_t readyflags_;
112 
113 	/**
114 	 * Damage pending to apply. Damage is applied at end of round so animations
115 	 * can show current action.
116 	 */
117 	uint32_t damage_;
118 
119 	/**
120 	 * \c true if the first soldier is the next to strike.
121 	 */
122 	bool first_strikes_;
123 
124 	/**
125 	 * \c true if the last turn attacker damaged his opponent
126 	 */
127 	bool last_attack_hits_;
128 };
129 }  // namespace Widelands
130 
131 #endif  // end of include guard: WL_LOGIC_MAP_OBJECTS_TRIBES_BATTLE_H
132