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 #include <algorithm>
19 #include <list>
20 #include "game_party_base.h"
21 #include "utils.h"
22 #include "rand.h"
23 
~Game_Party_Base()24 Game_Party_Base::~Game_Party_Base() {
25 }
26 
GetBattlers(std::vector<Game_Battler * > & out)27 void Game_Party_Base::GetBattlers(std::vector<Game_Battler*>& out) {
28 	int count = GetBattlerCount();
29 	for (int i = 0; i < count; ++i) {
30 		Game_Battler* battler = &(*this)[i];
31 		out.push_back(battler);
32 	}
33 }
34 
GetActiveBattlers(std::vector<Game_Battler * > & out)35 void Game_Party_Base::GetActiveBattlers(std::vector<Game_Battler*>& out) {
36 	int count = GetBattlerCount();
37 	for (int i = 0; i < count; ++i) {
38 		Game_Battler* battler = &(*this)[i];
39 		if (battler->Exists()) {
40 			out.push_back(battler);
41 		}
42 	}
43 }
44 
GetDeadBattlers(std::vector<Game_Battler * > & out)45 void Game_Party_Base::GetDeadBattlers(std::vector<Game_Battler*>& out) {
46 	int count = GetBattlerCount();
47 	for (int i = 0; i < count; ++i) {
48 		Game_Battler* battler = &(*this)[i];
49 		if (battler->IsDead()) {
50 			out.push_back(battler);
51 		}
52 	}
53 }
54 
GetNextActiveBattler(Game_Battler * battler)55 Game_Battler* Game_Party_Base::GetNextActiveBattler(Game_Battler* battler) {
56 	std::vector<Game_Battler*> battlers;
57 	GetBattlers(battlers);
58 
59 	std::vector<Game_Battler*>::const_iterator it;
60 	it = std::find(battlers.begin(), battlers.end(), battler);
61 
62 	if (it == battlers.end()) {
63 		return NULL;
64 	}
65 
66 	for (++it; it != battlers.end(); ++it) {
67 		Game_Battler* b = *it;
68 		if (b->Exists()) {
69 			return b;
70 		}
71 	}
72 
73 	// None found after battler, try from the beginning now
74 	for (it = battlers.begin(); *it != battler; ++it) {
75 		Game_Battler* b = *it;
76 		if (b->Exists()) {
77 			return b;
78 		}
79 	}
80 
81 	return NULL;
82 }
83 
GetRandomActiveBattler()84 Game_Battler* Game_Party_Base::GetRandomActiveBattler() {
85 	std::vector<Game_Battler*> battlers;
86 	GetActiveBattlers(battlers);
87 	if (battlers.empty()) {
88 		return NULL;
89 	}
90 	return battlers[Rand::GetRandomNumber(0, battlers.size() - 1)];
91 }
92 
GetRandomDeadBattler()93 Game_Battler* Game_Party_Base::GetRandomDeadBattler() {
94 	std::vector<Game_Battler*> battlers;
95 	GetDeadBattlers(battlers);
96 	if (battlers.empty()) {
97 		return NULL;
98 	}
99 
100 	return battlers[Rand::GetRandomNumber(0, battlers.size() - 1)];
101 }
102 
IsAnyActive()103 bool Game_Party_Base::IsAnyActive() {
104 	return GetRandomActiveBattler() != NULL;
105 }
106 
GetAverageAgility()107 int Game_Party_Base::GetAverageAgility() {
108 	std::vector<Game_Battler*> battlers;
109 	int agi = 0;
110 
111 	GetBattlers(battlers);
112 
113 	std::vector<Game_Battler*>::const_iterator it;
114 
115 	for (it = battlers.begin(); it != battlers.end(); ++it) {
116 		agi += (*it)->GetAgi();
117 	}
118 
119 	return battlers.empty() ? 1 : agi /= battlers.size();
120 }
121 
122