1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef EP_GAME_BATTLE_H
19 #define EP_GAME_BATTLE_H
20 
21 #include <functional>
22 #include <lcf/rpg/fwd.h>
23 #include <lcf/rpg/system.h>
24 #include <lcf/rpg/troop.h>
25 #include "teleport_target.h"
26 #include "utils.h"
27 #include "point.h"
28 
29 class Game_Battler;
30 class Game_Enemy;
31 class Game_Actor;
32 class Game_Interpreter;
33 class Game_Interpreter_Battle;
34 class Spriteset_Battle;
35 
36 enum class BattleResult {
37 	Victory,
38 	Escape,
39 	Defeat,
40 	Abort
41 };
42 
43 
44 namespace Game_Battle {
45 	/**
46 	 * Initialize Game_Battle.
47 	 */
48 	void Init(int troop_id);
49 
50 	/** @return true if a battle is currently running */
51 	bool IsBattleRunning();
52 
53 	/**
54 	 * Quits (frees) Game_Battle.
55 	 */
56 	void Quit();
57 
58 	/**
59 	 * Updates the battle animation
60 	 */
61 	void UpdateAnimation();
62 
63 	/**
64 	 * Updates spriteset graphics
65 	 */
66 	void UpdateGraphics();
67 
68 	/**
69 	 * Checks if a victory condition for the player party (enemy dead) is fulfilled.
70 	 *
71 	 * @return True on victory
72 	 */
73 	bool CheckWin();
74 
75 	/**
76 	 * Check if a lose condition for the player party (party dead) is fulfilled.
77 	 *
78 	 * @return True on lose
79 	 */
80 	bool CheckLose();
81 
82 	Spriteset_Battle& GetSpriteset();
83 
84 	/**
85 	 * Plays a battle animation against several targets simultaneously.
86 	 *
87 	 * @param animation_id the animation ID
88 	 * @param targets a vector of pointer to the battlers to play against
89 	 *
90 	 * @return the number of frames of the animation.
91 	 */
92 	int ShowBattleAnimation(int animation_id, std::vector<Game_Battler*> targets, bool only_sound = false, int cutoff = -1, bool invert = false);
93 
94 	/**
95 	 * Whether or not a battle animation is currently playing.
96 	 */
97 	bool IsBattleAnimationWaiting();
98 
99 	/**
100 	 * Starts a new battle turn.
101 	 * Event pages are reset depending on the actor.
102 	 *
103 	 * @param battler Battler who did the turn.
104 	 */
105 	void NextTurn(Game_Battler* battler = nullptr);
106 
107 	/**
108 	 * Updates the gauge of all battlers based on the highest agi of all.
109 	 */
110 	void UpdateAtbGauges();
111 
112 	void ChangeBackground(const std::string& name);
113 
114 	const std::string& GetBackground();
115 
116 	int GetTurn();
117 	bool CheckTurns(int turns, int base, int multiple);
118 
119 	/**
120 	 * Gets the game interpreter.
121 	 *
122 	 * @return the game interpreter.
123 	 */
124 	Game_Interpreter& GetInterpreter();
125 
126 	/**
127 	 * Gets the battle game interpreter.
128 	 *
129 	 * @return the battle game interpreter.
130 	 */
131 	Game_Interpreter_Battle& GetInterpreterBattle();
132 
133 	void SetTerrainId(int id);
134 	int GetTerrainId();
135 
136 	void SetBattleCondition(lcf::rpg::System::BattleCondition cond);
137 	lcf::rpg::System::BattleCondition GetBattleCondition();
138 
139 	void SetBattleFormation(lcf::rpg::System::BattleFormation form);
140 	lcf::rpg::System::BattleFormation GetBattleFormation();
141 
142 	/**
143 	 * Calculates the base grid position from the parameters used for both actors and enemies.
144 	 *
145 	 * @param party_idx the index of the party member
146 	 * @param party_size the size of the party
147 	 * @param table_x Which grid table to use for X position
148 	 * @param table_y which grid table to use for Y position
149 	 * @param cond the battle condition
150 	 * @param form the battle formation
151 	 * @param terrain_id the battle terrain id
152 	 */
153 	Point CalculateBaseGridPosition(
154 			int party_idx,
155 			int party_size,
156 			int x_table,
157 			int y_table,
158 			lcf::rpg::System::BattleFormation form,
159 			int terrain_id);
160 
161 	/**
162 	 * Calculate the battler position that would be used for the enemy
163 	 *
164 	 * @param enemy the enemy to calculate.
165 	 */
166 	Point Calculate2k3BattlePosition(const Game_Enemy& enemy);
167 
168 	/**
169 	 * Calculate the battler position that would be used for the enemy
170 	 *
171 	 * @param enemy the enemy to calculate.
172 	 */
173 	Point Calculate2k3BattlePosition(const Game_Actor& actor);
174 
175 	struct BattleTest {
176 		bool enabled = false;
177 		int troop_id = 0;
178 		std::string background;
179 		int terrain_id = 0;
180 		lcf::rpg::System::BattleFormation formation = lcf::rpg::System::BattleFormation_terrain;
181 		lcf::rpg::System::BattleCondition condition = lcf::rpg::System::BattleCondition_none;
182 	};
183 
184 	extern struct BattleTest battle_test;
185 
186 	/** @return true if a death handler is installed */
187 	bool HasDeathHandler();
188 
189 	/** @return death handler common event if one is installed, otherwise 0 */
190 	int GetDeathHandlerCommonEvent();
191 
192 	/** @return death teleport handler if one is installed, otherwise an inactive target */
193 	TeleportTarget GetDeathHandlerTeleport();
194 
195 	/** @return the active troop for the currently running battle */
196 	const lcf::rpg::Troop* GetActiveTroop();
197 
198 	/** Don't reference this, use IsBattleRunning()! */
199 	extern bool battle_running;
200 }
201 
IsBattleRunning()202 inline bool Game_Battle::IsBattleRunning() {
203 	return battle_running;
204 }
205 
206 #endif
207