1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <target/TargetContainer.h>
22 #include <target/Target.h>
23 #include <tank/Tank.h>
24 #include <tank/TankState.h>
25 #include <tanket/Tanket.h>
26
TargetContainer()27 TargetContainer::TargetContainer() :
28 playerId_(0),
29 destinationId_(0),
30 roundId_(0),
31 currentPlayer_(0)
32 {
33 }
34
~TargetContainer()35 TargetContainer::~TargetContainer()
36 {
37 std::map<unsigned int, Target *>::iterator itor;
38 for (itor = targets_.begin();
39 itor != targets_.end();
40 ++itor)
41 {
42 delete itor->second;
43 }
44 targets_.clear();
45 }
46
addTarget(Target * target)47 void TargetContainer::addTarget(Target *target)
48 {
49 std::map<unsigned int, Target *>::iterator findItor =
50 targets_.find(target->getPlayerId());
51 if (findItor != targets_.end())
52 {
53 Target *original = (*findItor).second;
54 S3D::dialogExit("Scorched3D",
55 S3D::formatStringBuffer("Duplicate target %u being added to container.\n"
56 "Original :%s, this %s",
57 target->getPlayerId(),
58 original->getCStrName().c_str(),
59 target->getCStrName().c_str()));
60 }
61
62 switch (target->getType())
63 {
64 case Target::TypeTank:
65 tanks_[target->getPlayerId()] = (Tank *) target;
66 case Target::TypeTanket:
67 tankets_[target->getPlayerId()] = (Tanket *) target;
68 case Target::TypeTarget:
69 targets_[target->getPlayerId()] = target;
70 }
71 }
72
removeTarget(unsigned int playerId)73 Target *TargetContainer::removeTarget(unsigned int playerId)
74 {
75 std::map<unsigned int, Target *>::iterator itor =
76 targets_.find(playerId);
77 if (itor == targets_.end()) return 0;
78
79 Target *target = (*itor).second;
80 targets_.erase(itor);
81
82 switch (target->getType())
83 {
84 case Target::TypeTank:
85 tanks_.erase(playerId);
86 if (currentPlayer_ == target)
87 {
88 currentPlayer_ = 0;
89 }
90 // Note: No break
91 case Target::TypeTanket:
92 tankets_.erase(playerId);
93 // Note: No break
94 case Target::TypeTarget:
95 break;
96 }
97 return target;
98 }
99
getTargetById(unsigned int id)100 Target *TargetContainer::getTargetById(unsigned int id)
101 {
102 std::map<unsigned int, Target *>::iterator mainitor =
103 targets_.find(id);
104 if (mainitor != targets_.end())
105 {
106 Target *target = (*mainitor).second;
107 DIALOG_ASSERT(target->getPlayerId() == id);
108
109 return target;
110 }
111 return 0;
112 }
113
getTanketById(unsigned int id)114 Tanket *TargetContainer::getTanketById(unsigned int id)
115 {
116 std::map<unsigned int, Tanket *>::iterator mainitor =
117 tankets_.find(id);
118 if (mainitor != tankets_.end())
119 {
120 Tanket *tanket = (*mainitor).second;
121 DIALOG_ASSERT(tanket->getPlayerId() == id);
122
123 return tanket;
124 }
125 return 0;
126 }
127
getTankById(unsigned int id)128 Tank *TargetContainer::getTankById(unsigned int id)
129 {
130 std::map<unsigned int, Tank *>::iterator mainitor =
131 tanks_.find(id);
132 if (mainitor != tanks_.end())
133 {
134 Tank *tank = (*mainitor).second;
135 DIALOG_ASSERT(tank->getPlayerId() == id);
136
137 return tank;
138 }
139 return 0;
140 }
141
getTankByName(const LangString & name)142 Tank *TargetContainer::getTankByName(const LangString &name)
143 {
144 std::map<unsigned int, Tank *>::iterator mainitor;
145 for (mainitor = tanks_.begin();
146 mainitor != tanks_.end();
147 ++mainitor)
148 {
149 Tank *tank = (*mainitor).second;
150 if (tank->getTargetName() == name) return tank;
151 }
152 return 0;
153 }
154
setCurrentPlayerId(unsigned int pid)155 void TargetContainer::setCurrentPlayerId(unsigned int pid)
156 {
157 playerId_ = pid;
158 if (playerId_)
159 {
160 currentPlayer_ = getTankById(playerId_);
161 }
162 else
163 {
164 currentPlayer_ = 0;
165 }
166 }
167
teamCount()168 int TargetContainer::teamCount()
169 {
170 int team1 = 0;
171 int team2 = 0;
172 int team3 = 0;
173 int team4 = 0;
174
175 std::map<unsigned int, Tank *>::iterator mainitor;
176 for (mainitor = tanks_.begin();
177 mainitor != tanks_.end();
178 ++mainitor)
179 {
180 Tank *current = (*mainitor).second;
181 if (current->getState().getTankPlaying() &&
182 current->getState().getLives() > 0)
183 {
184 if (current->getTeam() == 1) team1=1;
185 if (current->getTeam() == 2) team2=1;
186 if (current->getTeam() == 3) team3=1;
187 if (current->getTeam() == 4) team4=1;
188 }
189 }
190 return team1 + team2 + team3 + team4;
191 }
192
aliveCount()193 int TargetContainer::aliveCount()
194 {
195 int alive = 0;
196 std::map<unsigned int, Tank *>::iterator mainitor;
197 for (mainitor = tanks_.begin();
198 mainitor != tanks_.end();
199 ++mainitor)
200 {
201 Tank *current = (*mainitor).second;
202 if (current->getState().getTankPlaying() &&
203 current->getState().getLives() > 0)
204 {
205 alive++;
206 }
207 }
208 return alive;
209 }
210
getNoOfNonSpectatorTanks()211 int TargetContainer::getNoOfNonSpectatorTanks()
212 {
213 int count = 0;
214 std::map<unsigned int, Tank *>::iterator mainitor;
215 for (mainitor = tanks_.begin();
216 mainitor != tanks_.end();
217 ++mainitor)
218 {
219 Tank *current = (*mainitor).second;
220 if (current->getState().getTankPlaying()) count++;
221 }
222 return count;
223 }
224