1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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/TankSort.h>
22 #include <tank/Tank.h>
23 #include <tank/TankContainer.h>
24 #include <tank/TankTeamScore.h>
25 #include <tank/TankScore.h>
26 #include <tank/TankState.h>
27 #include <common/OptionsScorched.h>
29 int TankSort::compare(ScorchedContext
&context
,
30 const LangString
&nameX
, TankScore
&scoreX
,
31 const LangString
&nameY
, TankScore
&scoreY
)
33 if (scoreX
.getScore() > scoreY
.getScore()) return 1;
34 if (scoreX
.getScore() == scoreY
.getScore())
36 return LangStringUtil::strcmp(nameX
, nameY
);
41 bool TankSort::SortOnScore::operator()(const Tank
*x
, const Tank
*y
, ScorchedContext
&context
) const
43 Tank
&tankX
= *((Tank
*) x
);
44 Tank
&tankY
= *((Tank
*) y
);
45 TankScore
&scoreX
= tankX
.getScore();
46 TankScore
&scoreY
= tankY
.getScore();
48 if (tankX
.getState().getSpectator() &&
49 tankY
.getState().getSpectator())
51 if (LangStringUtil::strcmp(((Tank
*)x
)->getTargetName(),
52 ((Tank
*)y
)->getTargetName()) < 0) return true;
55 else if (tankX
.getState().getSpectator())
59 else if (tankY
.getState().getSpectator())
64 int compareResult
= compare(context
,
65 tankX
.getTargetName(), scoreX
,
66 tankY
.getTargetName(), scoreY
);
67 return (compareResult
> 0);
70 int TankSort::getWinningTeam(ScorchedContext
&context
)
72 for (int i
=1; i
<=context
.getOptionsGame().getTeams(); i
++)
74 int scorei
= context
.getTankTeamScore().getScore(i
);
77 for (int j
=1; j
<=context
.getOptionsGame().getTeams(); j
++)
81 int scorej
= context
.getTankTeamScore().getScore(j
);
93 void TankSort::getSortedTanks(std::list
<Tank
*> &list
, ScorchedContext
&context
)
95 std::list
<Tank
*> newList
;
98 std::list
<Tank
*>::iterator removeItor
= list
.begin();
99 std::list
<Tank
*>::iterator itor
= list
.begin(); itor
++;
100 for (;itor
!= list
.end(); itor
++)
102 static TankSort::SortOnScore compare
;
103 if (!compare(*itor
, *removeItor
, context
)) removeItor
= itor
;
106 newList
.push_front(*removeItor
);
107 list
.erase(removeItor
);
113 void TankSort::getSortedTanksIds(ScorchedContext
&context
,
114 std::list
<unsigned int> &list
, bool allTanks
)
116 std::list
<Tank
*> sortedTanks
;
117 std::map
<unsigned int, Tank
*> tanks
;
118 if (allTanks
) tanks
= context
.getTankContainer().getAllTanks();
119 else tanks
= context
.getTankContainer().getPlayingTanks();
120 std::map
<unsigned int, Tank
*>::iterator itor
;
121 for (itor
= tanks
.begin();
125 Tank
*tank
= (*itor
).second
;
126 sortedTanks
.push_back(tank
);
129 getSortedTanks(sortedTanks
, context
);
130 std::list
<Tank
*>::iterator resultitor
;
131 for (resultitor
= sortedTanks
.begin();
132 resultitor
!= sortedTanks
.end();
135 list
.push_back((*resultitor
)->getPlayerId());