1 ////////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software and
7 // you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ////////////////////////////////////////////////////////////////////////////////
10 
11 /** ****************************************************************************
12 *** \file    battle.h
13 *** \author  Tyler Olsen, roots@allacrost.org
14 *** \author  Viljami Korhonen, mindflayer@allacrost.org
15 *** \author  Corey Hoffstein, visage@allacrost.org
16 *** \author  Andy Gardner, chopperdave@allacrost.org
17 *** \author  Yohann Ferreira, yohann ferreira orange fr
18 *** \brief   Header file for battle mode interface.
19 ***
20 *** This code handles event processing, game state updates, and video frame
21 *** drawing when the user is fighting a battle.
22 *** ***************************************************************************/
23 
24 #ifndef __BATTLE_HEADER__
25 #define __BATTLE_HEADER__
26 
27 #include "battle_menu.h"
28 
29 #include "engine/mode_manager.h"
30 
31 #include "modes/battle/battle_enemy_info.h"
32 
33 #include <list>
34 
35 namespace vt_common
36 {
37 class DialogueSupervisor;
38 }
39 
40 namespace vt_battle
41 {
42 
43 //! \brief Determines whether the code in the vt_battle namespace should print debug statements or not.
44 extern bool BATTLE_DEBUG;
45 
46 //! \brief An internal namespace to be used only within the battle code. Don't use this namespace anywhere else!
47 namespace private_battle
48 {
49 
50 class BattleActor;
51 class BattleCharacter;
52 class BattleEnemy;
53 class BattleObject;
54 class BattleParticleEffect;
55 class BattleAnimation;
56 class BattleFinish;
57 class CommandSupervisor;
58 class SequenceSupervisor;
59 
60 //! \brief Used to indicate what state the overall battle is currently operating in
61 enum BATTLE_STATE {
62     BATTLE_STATE_INVALID   = -1,
63     BATTLE_STATE_INITIAL   =  0, //!< Character sprites are running in from off-screen to their battle positions
64     BATTLE_STATE_NORMAL    =  1, //!< Normal state where player is watching actions play out and waiting for a turn
65     BATTLE_STATE_COMMAND   =  2, //!< Player is choosing a command for a character
66     BATTLE_STATE_VICTORY   =  3, //!< Battle has ended with the characters victorious
67     BATTLE_STATE_DEFEAT    =  4, //!< Battle has ended with the characters defeated
68     BATTLE_STATE_EXITING   =  5, //!< Player has closed battle windows and battle mode is fading out
69     BATTLE_STATE_TOTAL     =  6
70 };
71 
72 } // namespace private_battle
73 
74 /** ****************************************************************************
75 *** \brief Manages all objects, events, and scenes that occur in a battle
76 ***
77 *** To create a battle, first you must create an instance of this class. Next,
78 *** the battle must be populated with enemies by using the AddEnemy() methods
79 *** prescribed below. You must then call the InitializeEnemies() method so that
80 *** the added enemies are ready for the battle to come. This should all be done
81 *** prior the Reset() method being called. If you fail to add any enemies,
82 *** an error will occur and the battle will self-terminate itself.
83 *** ***************************************************************************/
84 class BattleMode : public vt_mode_manager::GameMode
85 {
86     friend class private_battle::SequenceSupervisor;
87 
88 public:
89     BattleMode();
90 
91     ~BattleMode();
92 
93     //! \brief Returns a pointer to the currently active instance of battle mode
CurrentInstance()94     static BattleMode *CurrentInstance() {
95         return _current_instance;
96     }
97 
98     //! \name Inherited methods for the GameMode class
99     //@{
100     //! \brief Resets appropriate class members. Called whenever BattleMode is made the active game mode.
101     void Reset();
102 
103     //! \brief This method calls different update functions depending on the battle state.
104     void Update();
105 
106     //! \brief This method calls different draw functions depending on the battle state.
107     void Draw();
108 
109     //! \brief This method calls different draw functions depending on the battle state.
110     void DrawPostEffects();
111     //@}
112 
113     /** \brief Adds a new active enemy to the battle field
114     *** \param new_enemy_id The id number of the new enemy to add to the battle
115     *** \param position_x, position_y The enemy sprite position on the battle ground in pixels
116     *** If both are equal to 0.0f, the position is automatically computed.
117     *** This method works precisely the same was as the method which takes a GlobalEnemy argument,
118     *** only this version will construct the global enemy just using its id (meaning that it has
119     *** to open up the Lua file which defines the enemy). If the GlobalEnemy has already been
120     *** defined somewhere else, it is better to pass it in to the alternative definition of this
121     *** function.
122     **/
123     void AddEnemy(uint32_t new_enemy_id, float position_x, float position_y);
AddEnemy(uint32_t new_enemy_id)124     void AddEnemy(uint32_t new_enemy_id) {
125         AddEnemy(new_enemy_id, 0.0f, 0.0f);
126     }
127 
128     /** \brief Restores the battle to its initial state, allowing the player another attempt to achieve victory
129     *** This function is permitted only when the battle state isn't invalid, as this value is reserved
130     *** for battles that haven't started yet.
131     **/
132     void RestartBattle();
133 
134     //! \brief Tells the battle actor class whether
135     //! it should update the state timer.
AreActorStatesPaused()136     bool AreActorStatesPaused() const {
137         return _actor_state_paused;
138     }
139 
SetActorStatePaused(bool state)140     void SetActorStatePaused(bool state) {
141         _actor_state_paused = state;
142     }
143 
GetState()144     private_battle::BATTLE_STATE GetState() const {
145         return _state;
146     }
147 
148     //! \brief Sets the battle in scene mode, pausing the actors actions and states.
SetSceneMode(bool scene_mode)149     void SetSceneMode(bool scene_mode) {
150         _scene_mode = scene_mode;
151     }
152 
153     //! \brief Tells whether the battle is paused and playing a scene
IsInSceneMode()154     bool IsInSceneMode() const {
155         return _scene_mode;
156     }
157 
158     //! \brief Tells whether user input is accepted in dialogues.
159     //! Used by the common dialogue supervisor.
160     //! In the battle mode, dialogues can handle input only when in scene mode.
AcceptUserInputInDialogues()161     bool AcceptUserInputInDialogues() const {
162         return _scene_mode;
163     }
164 
165     /** \brief Changes the state of the battle and performs any initializations and updates needed
166     *** \param new_state The new state to change the battle to
167     **/
168     void ChangeState(private_battle::BATTLE_STATE new_state);
169 
170     /** \brief Requests battle mode to enter the command state and to open the command menu for a specific character
171     *** \param character A pointer to the character to enter commands for
172     *** \return True only if the requested operation was accepted
173     ***
174     *** This method does not guarantee that any change will take place. If the command menu is already open for a
175     *** different character, it will reject the request.
176     **/
177     bool OpenCommandMenu(private_battle::BattleCharacter *character);
178 
179     //! \brief Returns true if the battle has finished and entered either the victory or defeat state
IsBattleFinished()180     bool IsBattleFinished() const {
181         return ((_state == private_battle::BATTLE_STATE_VICTORY) || (_state == private_battle::BATTLE_STATE_DEFEAT));
182     }
183 
184     //! \brief Returns the number of character actors in the battle, both living and dead
GetNumberOfCharacters()185     uint32_t GetNumberOfCharacters() const {
186         return _character_actors.size();
187     }
188 
189     //! \brief Returns the number of enemy actors in the battle, both living and dead
GetNumberOfEnemies()190     uint32_t GetNumberOfEnemies() const {
191         return _enemy_actors.size();
192     }
193 
194     //! \brief Computes whether at least one battle character is dead.
195     bool isOneCharacterDead() const;
196 
197     /** \name Battle notification methods
198     *** These methods are called by other battle classes to indicate events such as when an actor
199     *** changes its state. Often BattleMode will respond by updating the state of one or more of its
200     *** members and calling other battle classes to notify them of the event as well.
201     **/
202     //@{
203     //! \brief Called whenever the player is in the command menu and exits it without selecting an action
204     void NotifyCommandCancel();
205 
206     /** \brief Called whenever the player has finished selecting a command for a character
207     *** \param character A pointer to the character that just had its command completed.
208     **/
209     void NotifyCharacterCommandComplete(private_battle::BattleCharacter *character);
210 
211     /** \brief Called to notify BattleMode when an actor is ready to execute an action
212     *** \param actor A pointer to the actor who has entered the state ACTOR_STATE_READY
213     **/
214     void NotifyActorReady(private_battle::BattleActor *actor);
215 
216     /** \brief Performs any necessary changes in response to an actor's death
217     *** \param actor A pointer to the actor who is now deceased
218     **/
219     void NotifyActorDeath(private_battle::BattleActor *actor);
220     //@}
221 
222     //! \name Class member accessor methods
223     //@{
GetCharacterActors()224     std::deque<private_battle::BattleCharacter *>& GetCharacterActors() {
225         return _character_actors;
226     }
227 
GetCharacterActor(uint32_t index)228     private_battle::BattleCharacter* GetCharacterActor(uint32_t index) {
229         if (index >= _character_actors.size())
230             return nullptr;
231         return _character_actors[index];
232     }
233 
GetEnemyActors()234     std::deque<private_battle::BattleEnemy *>& GetEnemyActors() {
235         return _enemy_actors;
236     }
237 
GetEnemyActor(uint32_t index)238     private_battle::BattleEnemy* GetEnemyActor(uint32_t index) {
239         if (index >= _enemy_actors.size())
240             return nullptr;
241         return _enemy_actors[index];
242     }
243 
GetCharacterParty()244     std::deque<private_battle::BattleActor *>& GetCharacterParty() {
245         return _character_party;
246     }
247 
GetEnemyParty()248     std::deque<private_battle::BattleActor *>& GetEnemyParty() {
249         return _enemy_party;
250     }
251 
GetCommandSupervisor()252     private_battle::CommandSupervisor* GetCommandSupervisor() {
253         return _command_supervisor;
254     }
255 
GetDialogueSupervisor()256     vt_common::DialogueSupervisor* GetDialogueSupervisor() {
257         return _dialogue_supervisor;
258     }
259 
260     //! \brief Sets or updates the battle actor idle state time to reflect its current stamina.
261     //! \note the _highest_stamina and _battle_type_time_factor members must be set before calling
262     //! this method.
263     void SetActorIdleStateTime(private_battle::BattleActor *actor);
264 
265     //! \brief Triggers a battle particle effect at the given location.
266     //! We do not use the particle manager here as we're considering the particle effect
267     //! as a battle object which has to be drawn along other battle others sorted by the Y coordinate.
268     //!
269     //! \param The effect filename is the particle effect definition file.
270     //! \param x the x coordinates of the particle effect in pixels.
271     //! \param y the y coordinates of the particle effect in pixels.
272     void TriggerBattleParticleEffect(const std::string& effect_filename, float x, float y);
273 
274     //! \brief Creates a battle animation object.
275     //! Those objects are also drawn sorted by their Y coordinate value.
276     //! Note that at the animation is created invisible at coordinate (0,0)
277     //! and that you must call SetVisible(true) and move it somewhere visible
278     //! for it to be shown.
279     //! Once you don't need it anymore, you can throw it by calling Remove()
280     //! and the animation will be freed from memory on the next Battle update.
281     //!
282     //! \param The animation filename is the animation definition file.
283     //! \return the animation object for scripted manipulation purpose.
284     private_battle::BattleAnimation* CreateBattleAnimation(const std::string& animation_filename);
285 
286     //! \brief Sets whether the current fight is a fight including a boss.
287     //! N.B.: Certain items shouldn't work in a boss fight, for instance.
288     void SetBossBattle(bool is_boss = true) {
289         _is_boss_battle = is_boss;
290     }
291 
292     //! \brief Tells whether the current fight is a fight including a boss.
IsBossBattle()293     bool IsBossBattle() const {
294         return _is_boss_battle;
295     }
296 
297     //! \brief Tells the battle mode Heroes will receive an aguility boost at battle start.
BoostHeroPartyInitiative()298     void BoostHeroPartyInitiative() {
299         _hero_init_boost = true;
300     }
301 
302     //! \brief Tells the battle mode Enemies will receive an aguility boost at battle start.
BoostEnemyPartyInitiative()303     void BoostEnemyPartyInitiative() {
304         _enemy_init_boost = true;
305     }
306     //@}
307 
308 private:
309 
310     //! \brief A static pointer to the currently active instance of battle mode
311     static BattleMode* _current_instance;
312 
313     //! \brief Retains the current state of the battle
314     private_battle::BATTLE_STATE _state;
315 
316     //! \name Battle supervisor classes
317     //@{
318     //! \brief Manages update and draw calls during special battle sequences
319     private_battle::SequenceSupervisor* _sequence_supervisor;
320 
321     //! \brief Manages state and visuals when the player is selecting a command for a character
322     private_battle::CommandSupervisor* _command_supervisor;
323 
324     //! \brief Stores and processes any dialogue that is to occur on the battle
325     vt_common::DialogueSupervisor* _dialogue_supervisor;
326 
327     //! \brief Presents player with information and options after a battle has concluded
328     private_battle::BattleFinish* _battle_finish;
329     //@}
330 
331     //! \name Battle Actor Containers
332     //@{
333     /** \brief Characters that are presently fighting in the battle
334     *** No more than four characters may be fighting at any given time, thus this structure will never
335     *** contain more than four BattleCharacter objects. This structure does not include any characters
336     *** that are in the party, but not actively fighting in the battle. This structure includes
337     *** characters that have zero hit points.
338     **/
339     std::deque<private_battle::BattleCharacter *> _character_actors;
340 
341     /** \brief Identical to the _character_actors container except that the elements are BattleActor pointers
342     *** \note This container is necessary for the GlobalTarget class, which needs a common data type so that
343     *** it may point to either the character or enemy party.
344     **/
345     std::deque<private_battle::BattleActor *> _character_party;
346 
347     /** \brief Enemies that are presently fighting in the battle
348     *** There is a theoretical limit on how many enemies may fight in one battle, but that is dependent upon
349     *** the sprite size of all active enemies and this limit will be detected by the BattleMode class.
350     *** This structure includes enemies that have zero hit points.
351     **/
352     std::deque<private_battle::BattleEnemy *> _enemy_actors;
353 
354     /** \brief Identical to the _enemy_actors container except that the elements are BattleActor pointers
355     *** \note This container is necessary for the GlobalTarget class, which needs a common data type so that
356     *** it may point to either the character or enemy party.
357     **/
358     std::deque<private_battle::BattleActor *> _enemy_party;
359 
360     //! \brief A copy of the enemy actors id at the beginning of the battle. Useful when restarting the battle,
361     //! as the number of enemies might have changed.
362     std::deque<BattleEnemyInfo> _initial_enemy_actors_info;
363 
364     /** \brief The effects container.
365     *** It will permit to draw particle effects and animations in the right order,
366     *** and will get rid of the "dead" useless effects at update time.
367     **/
368     std::vector<private_battle::BattleObject *> _battle_effects;
369 
370     /** \brief A FIFO queue of all actors that are ready to perform an action
371     *** When an actor has completed the wait time for their warm-up state, they enter the ready state and are
372     *** placed in this queue. The actor at the front of the queue is in the acting state, meaning that they are
373     *** executing their action. All other actors in the queue are waiting for the acting actor to finish and
374     *** be removed from the queue before they can take their turn.
375     **/
376     std::list<private_battle::BattleActor *> _ready_queue;
377     //@}
378 
379     /** \brief Vector used to draw all battle objects based on their y coordinate.
380     *** Sorted in the update() method.
381     **/
382     std::vector<private_battle::BattleObject *> _battle_objects;
383 
384     /** \brief The number of character swaps that the player may currently perform
385     *** The maximum number of swaps ever allowed is four, thus the value of this class member will always have the range [0, 4].
386     *** This member is also used to determine how many swap cards to draw on the battle screen.
387     **/
388     uint8_t _current_number_swaps;
389 
390     /** \brief Tells whether the last enemy is dying.
391     *** In that case, the battle character action must be canceled, and the command made unavailable
392     *** until the last die animation is done.
393     **/
394     bool _last_enemy_dying;
395 
396     //! \brief the Stamina Icon general transluency. Used to make the characters's stamina icon disappear on wins.
397     float _stamina_icon_alpha;
398 
399     //! \brief Tells whether the state of battle actors should be paused. Used in wait battle types.
400     bool _actor_state_paused;
401 
402     //! Tells whether the battle is in scene mode
403     //! The actor states should then be paused, the dialogues played if there are some,
404     //! But the actors animations and indicators should still updates.
405     //! The effects shouldn't update though.
406     bool _scene_mode;
407 
408     //! \brief Setup at battle start, and used to normalize the battle actors speed in battle.
409     uint32_t _highest_stamina;
410 
411     //! \brief Tells whether the battle is a boss fight.
412     bool _is_boss_battle;
413 
414     //! \brief The battle menu
415     vt_battle::private_battle::BattleMenu _battle_menu;
416 
417     //! \brief The Auto Battle text displayed when auto battle is active
418     vt_video::TextImage _auto_battle_text;
419 
420     //! \brief Whether the hero party should get an initiative boost at battle start.
421     bool _hero_init_boost;
422 
423     //! \brief Whether the enemy party should get an initiative boost at battle start.
424     bool _enemy_init_boost;
425 
426 
427     ////////////////////////////// PRIVATE METHODS ///////////////////////////////
428 
429     //! \brief Initializes all data necessary for the battle to begin
430     void _Initialize();
431 
432     //! \brief Set the battle music state
433     void _ResetMusicState();
434 
435     /** \brief Applies a battle command to a given character automatically.
436     *** \param character The character which will receive the command.
437     **/
438     void _AutoCharacterCommand(private_battle::BattleCharacter* character);
439 
440     /** \brief Sets the origin location of all character and enemy actors
441     *** The location of the actors in both parties is dependent upon the number and physical size of the actor
442     *** (the size of its sprite image). This function implements the algorithm that determines those locations.
443     **/
444     void _DetermineActorLocations();
445 
446     //! \brief Returns the number of enemies that are still alive in the battle
447     uint32_t _NumberEnemiesAlive() const;
448 
449     /** \brief Returns the number of enemies that are still capable to fight in the battle.
450     *** Which isn't the number of alive enemies, since that function can tell whether an enemy
451     *** is currently dying.
452     **/
453     uint32_t _NumberValidEnemies() const;
454 
455     /** \brief Returns the number of characters that are still alive in the battle
456     *** \note This function only counts the characters on the screen, not characters in the party reserves
457     **/
458     uint32_t _NumberCharactersAlive() const;
459 
460 
461     //! \name Draw assistant functions
462     //@{
463     /** \brief Draws all background images and animations
464     *** The images and effects drawn by this function will never be drawn over anything else in the battle
465     *** (battle sprites, menus, etc.).
466     **/
467     void _DrawBackgroundGraphics();
468 
469     /** \brief Draws all characters, enemy sprites as well as any sprite visuals
470     *** In addition to the sprites themselves, this function draws special effects and indicators for the sprites.
471     *** For example, the actor selector image and any visible action effects like magic.
472     **/
473     void _DrawSprites();
474 
475     /** \brief Draws all foreground images and animations
476     *** The images and effects drawn by this function will be drawn over sprites,
477     *** but not over the post effects and the gui.
478     **/
479     void _DrawForegroundGraphics();
480 
481     //! \brief Draws all GUI graphics on the screen
482     void _DrawGUI();
483 
484     /** \brief Draws the bottom menu visuals and information
485     *** The bottom menu contains a wide array of information, including swap cards, character portraits, character names,
486     *** and both character and enemy status. This menu is perpetually drawn on the battle screen.
487     **/
488     void _DrawBottomMenu();
489 
490     //! \brief Draws the stamina bar and the icons of the actors of both parties
491     void _DrawStaminaBar();
492     //@}
493 }; // class BattleMode : public vt_mode_manager::GameMode
494 
495 } // namespace vt_battle
496 
497 #endif // __BATTLE_HEADER__
498