TankSort.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/TankSort.h>
00022 #include <tank/Tank.h>
00023 #include <tank/TankContainer.h>
00024 #include <tank/TankTeamScore.h>
00025 #include <tank/TankScore.h>
00026 #include <tank/TankState.h>
00027 #include <common/OptionsScorched.h>
00028 
00029 int TankSort::compare(ScorchedContext &context,
00030                 const LangString &nameX, TankScore &scoreX,
00031                 const LangString &nameY, TankScore &scoreY)
00032 {
00033         if (scoreX.getScore() > scoreY.getScore()) return 1;
00034         if (scoreX.getScore() == scoreY.getScore())
00035         {
00036                 return LangStringUtil::strcmp(nameX, nameY);
00037         }
00038         return -1;
00039 }
00040 
00041 bool TankSort::SortOnScore::operator()(const Tank *x, const Tank *y, ScorchedContext &context) const
00042 {
00043         Tank &tankX = *((Tank *) x);
00044         Tank &tankY = *((Tank *) y);
00045         TankScore &scoreX = tankX.getScore();
00046         TankScore &scoreY = tankY.getScore();
00047 
00048         if (tankX.getState().getSpectator() &&
00049                 tankY.getState().getSpectator())
00050         {
00051                 if (LangStringUtil::strcmp(((Tank *)x)->getTargetName(), 
00052                         ((Tank *)y)->getTargetName()) < 0) return true;
00053                 return false;
00054         }
00055         else if (tankX.getState().getSpectator())
00056         {
00057                 return false;
00058         }
00059         else if (tankY.getState().getSpectator())
00060         {
00061                 return true;
00062         }
00063 
00064         int compareResult = compare(context, 
00065                 tankX.getTargetName(), scoreX, 
00066                 tankY.getTargetName(), scoreY);
00067         return (compareResult > 0);
00068 }
00069 
00070 int TankSort::getWinningTeam(ScorchedContext &context)
00071 {
00072         for (int i=1; i<=context.getOptionsGame().getTeams(); i++)
00073         {
00074                 int scorei = context.getTankTeamScore().getScore(i);
00075 
00076                 bool top = true;
00077                 for (int j=1; j<=context.getOptionsGame().getTeams(); j++)
00078                 {
00079                         if (i == j) continue;
00080 
00081                         int scorej = context.getTankTeamScore().getScore(j);
00082                         if (scorej >= scorei)
00083                         {
00084                                 top = false;
00085                                 break;
00086                         }
00087                 }
00088                 if (top) return i;
00089         }
00090         return 0;
00091 }
00092 
00093 void TankSort::getSortedTanks(std::list<Tank *> &list, ScorchedContext &context)
00094 {
00095         std::list<Tank *> newList;
00096         while (!list.empty())
00097         {
00098                 std::list<Tank *>::iterator removeItor = list.begin();
00099                 std::list<Tank *>::iterator itor = list.begin(); itor++;
00100                 for (;itor != list.end(); itor++)
00101                 {
00102                         static TankSort::SortOnScore compare;
00103                         if (!compare(*itor, *removeItor, context)) removeItor = itor;
00104                 }
00105 
00106                 newList.push_front(*removeItor);
00107                 list.erase(removeItor);
00108         }
00109 
00110         list = newList;
00111 }
00112 
00113 void TankSort::getSortedTanksIds(ScorchedContext &context, 
00114         std::list<unsigned int> &list, bool allTanks)
00115 {
00116         std::list<Tank *> sortedTanks;
00117         std::map<unsigned int, Tank *> tanks;
00118         if (allTanks) tanks = context.getTankContainer().getAllTanks();
00119         else tanks = context.getTankContainer().getPlayingTanks();
00120         std::map<unsigned int, Tank *>::iterator itor;
00121         for (itor = tanks.begin();
00122                 itor != tanks.end();
00123                 itor++)
00124         {
00125                 Tank *tank = (*itor).second;
00126                 sortedTanks.push_back(tank);
00127         }
00128 
00129         getSortedTanks(sortedTanks, context);
00130         std::list<Tank *>::iterator resultitor;
00131         for (resultitor = sortedTanks.begin();
00132                 resultitor != sortedTanks.end();
00133                 resultitor++)
00134         {
00135                 list.push_back((*resultitor)->getPlayerId());
00136         }
00137 }

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