1 ////////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2018 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See http://www.gnu.org/copyleft/gpl.html for details.
9 ////////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef __GLOBAL_PARTY_HEADER__
12 #define __GLOBAL_PARTY_HEADER__
13 
14 #include "utils/utils_common.h"
15 
16 #include <vector>
17 
18 namespace vt_global
19 {
20 
21 class GlobalCharacter;
22 
23 //! \brief The maximum number of characters that can be in the active party
24 const uint32_t GLOBAL_MAX_PARTY_SIZE = 4;
25 
26 /** ****************************************************************************
27 *** \brief Represents a party of characters
28 ***
29 *** This class is a container for a group or "party" of characters. A party is a type
30 *** of target for items and skills. The GameGlobal class also organizes characters
31 *** into parties for convienence. Note that an character may be either an enemy or
32 *** a character, but you should avoid creating parties that contain both characters
33 *** and enemies, as it can lead to conflicts. For example, a character and enemy which
34 *** enemy which have the same ID value).
35 ***
36 *** Parties may or may not allow duplicate characters (a duplicate character is defined
37 *** as an character that has the same _id member as another character in the party).
38 *** This property is determined in the GlobalParty constructor
39 ***
40 *** \note When this class is destroyed, the characters contained within the class are
41 *** <i>not</i> destroyed. Only the references to those characters through this class object
42 *** are lost.
43 ***
44 *** \note All methods which perform an operation by using an character ID are
45 *** <b>only</b> valid to use if the party does not allow duplicates.
46 *** ***************************************************************************/
47 class GlobalParty
48 {
49 public:
50     //! \param allow_duplicates Determines whether or not the party allows duplicate characters to be added (default value == false)
51     explicit GlobalParty(bool allow_duplicates = false) :
_allow_duplicates(allow_duplicates)52         _allow_duplicates(allow_duplicates)
53     {}
54 
~GlobalParty()55     ~GlobalParty()
56     {}
57 
58     // ---------- Character addition, removal, and retrieval methods
59 
60     /** \brief Adds an character to the party
61     *** \param character A pointer to the character to add to the party
62     *** \param index The index where the character should be inserted. If negative, character is added to the end
63     *** \note The character will not be added if it is already in the party and duplicates are not allowed
64     **/
65     void AddCharacter(GlobalCharacter *character, int32_t index = -1);
66 
67     /** \brief Removes an character from the party
68     *** \param index The index of the character in the party to remove
69     *** \return A pointer to the character that was removed, or nullptr if the index provided was invalid
70     **/
71     GlobalCharacter *RemoveCharacterAtIndex(uint32_t index);
72 
73     /** \brief Removes an character from the party
74     *** \param id The id value of the character to remove
75     *** \return A pointer to the character that was removed, or nullptr if the character was not found in the party
76     **/
77     GlobalCharacter *RemoveCharacterByID(uint32_t id);
78 
79     /** \brief Clears the party of all characters
80     *** \note This function does not return the character pointers, so if you wish to get the
81     *** GlobalCharacter make sure you do so prior to invoking this call.
82     **/
RemoveAllCharacters()83     void RemoveAllCharacters() {
84         _characters.clear();
85     }
86 
87     /** \brief Retrieves a poitner to the character in the party at a specified index
88     *** \param index The index where the character may be found in the party
89     *** \return A pointer to the character at the specified index, or nullptr if the index argument was invalid
90     **/
91     GlobalCharacter *GetCharacterAtIndex(uint32_t index) const;
92 
93     /** \brief Retrieves a poitner to the character in the party with the spefified id
94     *** \param id The id of the character to return
95     *** \return A pointer to the character with the requested ID, or nullptr if the character was not found
96     **/
97     GlobalCharacter *GetCharacterByID(uint32_t id) const;
98 
99     // ---------- Character swap and replacement methods
100 
101     /** \brief Swaps the location of two characters in the party by their indeces
102     *** \param first_index The index of the first character to swap
103     *** \param second_index The index of the second character to swap
104     **/
105     void SwapCharactersByIndex(uint32_t first_index, uint32_t second_index);
106 
107     /** \brief Swaps the location of two characters in the party by looking up their IDs
108     *** \param first_id The id of the first character to swap
109     *** \param second_id The id of the second character to swap
110     **/
111     void SwapCharactersByID(uint32_t first_id, uint32_t second_id);
112 
113     /** \brief Replaces an character in the party at a specified index with a new character
114     *** \param index The index of the character to be replaced
115     *** \param new_character A pointer to the character that will replace the existing character
116     *** \return A pointer to the replaced character, or nullptr if the operation did not take place
117     **/
118     GlobalCharacter *ReplaceCharacterByIndex(uint32_t index, GlobalCharacter *new_character);
119 
120     /** \brief Replaces an character in the party with the specified id with a new character
121     *** \param id The id of the character to be replaced
122     *** \param new_character A pointer to the character that will replace the existing character
123     *** \return A pointer to the replaced character, or nullptr if the operation did not take place
124     **/
125     GlobalCharacter *ReplaceCharacterByID(uint32_t id, GlobalCharacter *new_character);
126 
127     // ---------- Other methods
128 
129     /** \brief Computes the average experience level of all characters in the party
130     *** \return A float representing the average experience level (0.0f if party is empty)
131     **/
132     float AverageExperienceLevel() const;
133 
134     /** \brief Adds a certain amount of hit points to all characters in the party
135     *** \param hp The number of health points to add
136     **/
137     void AddHitPoints(uint32_t hp);
138 
139     /** \brief Adds a certain amount of skill points to all characters in the party
140     *** \param sp The number of skill points to add
141     **/
142     void AddSkillPoints(uint32_t sp);
143 
IsAllowDuplicates()144     bool IsAllowDuplicates() const {
145         return _allow_duplicates;
146     }
147 
IsPartyEmpty()148     bool IsPartyEmpty() const {
149         return _characters.empty();
150     }
151 
GetPartySize()152     uint32_t GetPartySize() const {
153         return _characters.size();
154     }
155 
GetAllCharacters()156     const std::vector<GlobalCharacter *>& GetAllCharacters() const {
157         return _characters;
158     }
159 
160     //! \brief Returns the character party position
161     uint32_t GetPartyPosition(GlobalCharacter* character);
162 
163 private:
164     /** \brief Characters are allowed to be inserted into the party multiple times when this member is true
165     *** \note The value of this member is set in the class constructor and can not be changed at a later time
166     **/
167     bool _allow_duplicates;
168 
169     /** \brief A container of characters that are in this party
170     *** The GlobalCharacter objects pointed to by the elements in this vector are not managed by this class. Therefore
171     *** one needs to be careful that if any of the GlobalCharacter objects are destroyed outside the context of this
172     *** class, the character should be removed from this container immediately to avoid a possible segmentation fault.
173     **/
174     std::vector<GlobalCharacter *> _characters;
175 }; // class GlobalParty
176 
177 } // namespace vt_global
178 
179 #endif // __GLOBAL_PARTY_HEADER__
180