TankContainer.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <tank/TankContainer.h>
00022 #include <tank/TankState.h>
00023 
00024 TankContainer::TankContainer(TargetContainer &targets) : 
00025         targets_(targets),
00026         playerId_(0), 
00027         destinationId_(0)
00028 {
00029 
00030 }
00031 
00032 TankContainer::~TankContainer()
00033 {
00034 
00035 }
00036 
00037 void TankContainer::addTank(Tank *tank)
00038 {
00039         targets_.internalAddTarget(tank);
00040         tanks_[tank->getPlayerId()] = tank;
00041 }
00042 
00043 Tank *TankContainer::removeTank(unsigned int playerId)
00044 {
00045         Target *target = targets_.internalRemoveTarget(playerId);
00046         if (target)
00047         {
00048                 tanks_.erase(playerId);
00049                 DIALOG_ASSERT(!target->isTarget());
00050         }
00051         return (Tank *) target;
00052 }
00053 
00054 Tank *TankContainer::getTankById(unsigned int id)
00055 {
00056         std::map<unsigned int, Tank *>::iterator findItor =
00057                 tanks_.find(id);
00058         if (findItor != tanks_.end())
00059         {
00060                 return (*findItor).second;
00061         }
00062         return 0;
00063 }
00064 
00065 Tank *TankContainer::getTankByName(const LangString &name)
00066 {
00067         std::map<unsigned int, Tank *>::iterator mainitor;
00068         for (mainitor = tanks_.begin();
00069                 mainitor != tanks_.end();
00070                 mainitor++)
00071         {
00072                 Tank *tank = (*mainitor).second;
00073                 if (tank->getTargetName() == name) return tank;
00074         }
00075         return 0;
00076 }
00077 
00078 Tank *TankContainer::getCurrentTank()
00079 {
00080         if (playerId_)
00081         {
00082                 static Tank *currentPlayer = 0;
00083                 if (!currentPlayer || currentPlayer->getPlayerId() != playerId_)
00084                 {
00085                         currentPlayer = getTankById(playerId_);
00086                 }
00087                 return currentPlayer;
00088         }
00089 
00090         return 0;
00091 }
00092 
00093 void TankContainer::clientNewGame()
00094 {
00095         std::map<unsigned int, Tank *>::iterator mainitor;
00096         for (mainitor = tanks_.begin();
00097                 mainitor != tanks_.end();
00098                 mainitor++)
00099         {
00100                 Tank *tank = (*mainitor).second;
00101                 if (!tank->isTemp())
00102                 {
00103                         tank->clientNewGame();
00104                 }
00105         }
00106 }
00107 
00108 void TankContainer::newMatch()
00109 {
00110         std::map<unsigned int, Tank *>::iterator mainitor;
00111         for (mainitor = tanks_.begin();
00112                 mainitor != tanks_.end();
00113                 mainitor++)
00114         {
00115                 Tank *tank = (*mainitor).second;
00116                 if (!tank->isTemp())
00117                 {
00118                         tank->newMatch();
00119                 }
00120         }
00121 }
00122 
00123 int TankContainer::teamCount()
00124 {
00125         int team1 = 0;
00126         int team2 = 0;
00127         int team3 = 0;
00128         int team4 = 0;
00129 
00130         std::map<unsigned int, Tank *>::iterator mainitor;
00131         for (mainitor = tanks_.begin();
00132                 mainitor != tanks_.end();
00133                 mainitor++)
00134         {
00135                 Tank *current = (*mainitor).second;
00136                 if (!current->isTemp())
00137                 {
00138                         if (current->getState().getState() == TankState::sNormal)
00139                         {
00140                                 if (current->getTeam() == 1) team1=1;
00141                                 if (current->getTeam() == 2) team2=1;
00142                                 if (current->getTeam() == 3) team3=1;
00143                                 if (current->getTeam() == 4) team4=1;
00144                         }
00145                 }
00146         }
00147         return team1 + team2 + team3 + team4;
00148 }
00149 
00150 int TankContainer::aliveCount()
00151 {
00152         int alive = 0;
00153         std::map<unsigned int, Tank *>::iterator mainitor;
00154         for (mainitor = tanks_.begin();
00155                 mainitor != tanks_.end();
00156                 mainitor++)
00157         {
00158                 Tank *current = (*mainitor).second;
00159                 if (!current->isTemp())
00160                 {
00161                         if (current->getState().getState() == TankState::sNormal)
00162                         {
00163                                 alive++;
00164                         }
00165                 }
00166         }
00167         return alive;
00168 }
00169 
00170 void TankContainer::setAllDead()
00171 {
00172         std::map<unsigned int, Tank *>::iterator mainitor;
00173         for (mainitor = tanks_.begin();
00174                 mainitor != tanks_.end();
00175                 mainitor++)
00176         {
00177                 Tank *current = (*mainitor).second;
00178                 if (!current->isTemp())
00179                 {
00180                         if (current->getState().getState() != TankState::sPending &&
00181                                 current->getState().getState() != TankState::sLoading &&
00182                                 current->getState().getState() != TankState::sInitializing)
00183                         {
00184                                 current->getState().setState(TankState::sDead);
00185                                 current->getState().setLives(0);
00186                         }
00187                 }
00188         }
00189 }
00190 
00191 bool TankContainer::allReady()
00192 {
00193         std::map<unsigned int, Tank *>::iterator mainitor;
00194         for (mainitor = tanks_.begin();
00195                 mainitor != tanks_.end();
00196                 mainitor++)
00197         {
00198                 Tank *current = (*mainitor).second;
00199                 if (!current->isTemp())
00200                 {
00201                         // current check tanks that are not pending
00202                         if (current->getState().getState() != TankState::sPending &&
00203                                 current->getState().getState() != TankState::sLoading &&
00204                                 current->getState().getState() != TankState::sInitializing)
00205                         {
00206                                 if (current->getState().getReadyState() == 
00207                                         TankState::SNotReady) return false;
00208                         }
00209                 }
00210         }
00211         return true;
00212 }
00213 
00214 void TankContainer::setAllNotReady()
00215 {
00216         std::map<unsigned int, Tank *>::iterator mainitor;
00217         for (mainitor = tanks_.begin();
00218                 mainitor != tanks_.end();
00219                 mainitor++)
00220         {
00221                 Tank *current = (*mainitor).second;
00222                 if (!current->isTemp())
00223                 {
00224                         // current check tanks that are not pending
00225                         if (current->getState().getState() != TankState::sPending &&
00226                                 current->getState().getState() != TankState::sLoading &&
00227                                 current->getState().getState() != TankState::sInitializing)
00228                         {
00229                                 current->getState().setNotReady();
00230                         }
00231                 }
00232         }
00233 }
00234 
00235 int TankContainer::getNoOfNonSpectatorTanks()
00236 {
00237         int count = 0;
00238         std::map<unsigned int, Tank *>::iterator mainitor;
00239         for (mainitor = tanks_.begin();
00240                 mainitor != tanks_.end();
00241                 mainitor++)
00242         {
00243                 Tank *current = (*mainitor).second;
00244                 if (!current->isTemp())
00245                 {
00246                         if (!current->getState().getSpectator()) count++;
00247                 }
00248         }
00249         return count;
00250 }
00251 
00252 std::map<unsigned int, Tank *> &TankContainer::getPlayingTanks()
00253 {
00254         static std::map<unsigned int, Tank *> tanks;
00255         tanks.clear();
00256 
00257         std::map<unsigned int, Tank *>::iterator mainitor;
00258         for (mainitor = tanks_.begin();
00259                 mainitor != tanks_.end();
00260                 mainitor++)
00261         {
00262                 Tank *current = (*mainitor).second;
00263                 if (!current->isTemp())
00264                 {
00265                         tanks[current->getPlayerId()] = current;
00266                 }
00267         }       
00268 
00269         return tanks;
00270 }
00271 
00272 std::map<unsigned int, Tank *> &TankContainer::getAllTanks()
00273 {
00274         return tanks_;
00275 }
00276 
00277 int TankContainer::getNoOfTanks()
00278 {
00279         int count = 0;
00280         std::map<unsigned int, Tank *>::iterator mainitor;
00281         for (mainitor = tanks_.begin();
00282                 mainitor != tanks_.end();
00283                 mainitor++)
00284         {
00285                 Tank *current = (*mainitor).second;
00286                 if (!current->isTemp())
00287                 {
00288                         count++;
00289                 }
00290         }       
00291         return count;
00292 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3