git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / tank / TankContainer.cpp
blob91fd64e2de5ee3b9cab5de2c13dcc24993d54c6b
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
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.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <tank/TankContainer.h>
22 #include <tank/TankState.h>
24 TankContainer::TankContainer(TargetContainer &targets) :
25 targets_(targets),
26 playerId_(0),
27 destinationId_(0)
32 TankContainer::~TankContainer()
37 void TankContainer::addTank(Tank *tank)
39 targets_.internalAddTarget(tank);
40 tanks_[tank->getPlayerId()] = tank;
43 Tank *TankContainer::removeTank(unsigned int playerId)
45 Target *target = targets_.internalRemoveTarget(playerId);
46 if (target)
48 tanks_.erase(playerId);
49 DIALOG_ASSERT(!target->isTarget());
51 return (Tank *) target;
54 Tank *TankContainer::getTankById(unsigned int id)
56 std::map<unsigned int, Tank *>::iterator findItor =
57 tanks_.find(id);
58 if (findItor != tanks_.end())
60 return (*findItor).second;
62 return 0;
65 Tank *TankContainer::getTankByName(const LangString &name)
67 std::map<unsigned int, Tank *>::iterator mainitor;
68 for (mainitor = tanks_.begin();
69 mainitor != tanks_.end();
70 mainitor++)
72 Tank *tank = (*mainitor).second;
73 if (tank->getTargetName() == name) return tank;
75 return 0;
78 Tank *TankContainer::getCurrentTank()
80 if (playerId_)
82 static Tank *currentPlayer = 0;
83 if (!currentPlayer || currentPlayer->getPlayerId() != playerId_)
85 currentPlayer = getTankById(playerId_);
87 return currentPlayer;
90 return 0;
93 void TankContainer::clientNewGame()
95 std::map<unsigned int, Tank *>::iterator mainitor;
96 for (mainitor = tanks_.begin();
97 mainitor != tanks_.end();
98 mainitor++)
100 Tank *tank = (*mainitor).second;
101 if (!tank->isTemp())
103 tank->clientNewGame();
108 void TankContainer::newMatch()
110 std::map<unsigned int, Tank *>::iterator mainitor;
111 for (mainitor = tanks_.begin();
112 mainitor != tanks_.end();
113 mainitor++)
115 Tank *tank = (*mainitor).second;
116 if (!tank->isTemp())
118 tank->newMatch();
123 int TankContainer::teamCount()
125 int team1 = 0;
126 int team2 = 0;
127 int team3 = 0;
128 int team4 = 0;
130 std::map<unsigned int, Tank *>::iterator mainitor;
131 for (mainitor = tanks_.begin();
132 mainitor != tanks_.end();
133 mainitor++)
135 Tank *current = (*mainitor).second;
136 if (!current->isTemp())
138 if (current->getState().getState() == TankState::sNormal)
140 if (current->getTeam() == 1) team1=1;
141 if (current->getTeam() == 2) team2=1;
142 if (current->getTeam() == 3) team3=1;
143 if (current->getTeam() == 4) team4=1;
147 return team1 + team2 + team3 + team4;
150 int TankContainer::aliveCount()
152 int alive = 0;
153 std::map<unsigned int, Tank *>::iterator mainitor;
154 for (mainitor = tanks_.begin();
155 mainitor != tanks_.end();
156 mainitor++)
158 Tank *current = (*mainitor).second;
159 if (!current->isTemp())
161 if (current->getState().getState() == TankState::sNormal)
163 alive++;
167 return alive;
170 void TankContainer::setAllDead()
172 std::map<unsigned int, Tank *>::iterator mainitor;
173 for (mainitor = tanks_.begin();
174 mainitor != tanks_.end();
175 mainitor++)
177 Tank *current = (*mainitor).second;
178 if (!current->isTemp())
180 if (current->getState().getState() != TankState::sPending &&
181 current->getState().getState() != TankState::sLoading &&
182 current->getState().getState() != TankState::sInitializing)
184 current->getState().setState(TankState::sDead);
185 current->getState().setLives(0);
191 bool TankContainer::allReady()
193 std::map<unsigned int, Tank *>::iterator mainitor;
194 for (mainitor = tanks_.begin();
195 mainitor != tanks_.end();
196 mainitor++)
198 Tank *current = (*mainitor).second;
199 if (!current->isTemp())
201 // current check tanks that are not pending
202 if (current->getState().getState() != TankState::sPending &&
203 current->getState().getState() != TankState::sLoading &&
204 current->getState().getState() != TankState::sInitializing)
206 if (current->getState().getReadyState() ==
207 TankState::SNotReady) return false;
211 return true;
214 void TankContainer::setAllNotReady()
216 std::map<unsigned int, Tank *>::iterator mainitor;
217 for (mainitor = tanks_.begin();
218 mainitor != tanks_.end();
219 mainitor++)
221 Tank *current = (*mainitor).second;
222 if (!current->isTemp())
224 // current check tanks that are not pending
225 if (current->getState().getState() != TankState::sPending &&
226 current->getState().getState() != TankState::sLoading &&
227 current->getState().getState() != TankState::sInitializing)
229 current->getState().setNotReady();
235 int TankContainer::getNoOfNonSpectatorTanks()
237 int count = 0;
238 std::map<unsigned int, Tank *>::iterator mainitor;
239 for (mainitor = tanks_.begin();
240 mainitor != tanks_.end();
241 mainitor++)
243 Tank *current = (*mainitor).second;
244 if (!current->isTemp())
246 if (!current->getState().getSpectator()) count++;
249 return count;
252 std::map<unsigned int, Tank *> &TankContainer::getPlayingTanks()
254 static std::map<unsigned int, Tank *> tanks;
255 tanks.clear();
257 std::map<unsigned int, Tank *>::iterator mainitor;
258 for (mainitor = tanks_.begin();
259 mainitor != tanks_.end();
260 mainitor++)
262 Tank *current = (*mainitor).second;
263 if (!current->isTemp())
265 tanks[current->getPlayerId()] = current;
269 return tanks;
272 std::map<unsigned int, Tank *> &TankContainer::getAllTanks()
274 return tanks_;
277 int TankContainer::getNoOfTanks()
279 int count = 0;
280 std::map<unsigned int, Tank *>::iterator mainitor;
281 for (mainitor = tanks_.begin();
282 mainitor != tanks_.end();
283 mainitor++)
285 Tank *current = (*mainitor).second;
286 if (!current->isTemp())
288 count++;
291 return count;