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 <common/OptionsTransient.h>
22 #include <common/OptionsScorched.h>
23 #include <common/Defines.h>
24 #include <tank/TankContainer.h>
25 #include <tank/TankState.h>
29 OptionsTransient::OptionsTransient(OptionsScorched
&optionsGame
) :
30 optionsGame_(optionsGame
), newGame_(false),
31 currentRoundNo_(options_
, "CurrentRoundNo",
32 "The current number of rounds played in this game", 0, 0),
33 currentGameNo_(options_
, "CurrentGameNo",
34 "The current game", 0, 0),
35 windAngle_(options_
, "WindAngle",
36 "The current wind angle (direction)", 0, 0),
37 windStartAngle_(options_
, "WindStartAngle",
38 "The angle (direction) the wind started the round on", 0, 0),
39 windSpeed_(options_
, "WindSpeed",
40 "The current speed of the wind", 0, 0),
41 windDirection_(options_
, "WindDirection",
42 "The current wind direction vector", 0, FixedVector::getNullVector()),
43 wallType_(options_
, "WallType",
44 "The current wall type", 0, 0)
49 OptionsTransient::~OptionsTransient()
53 const char *OptionsTransient::getGameType()
55 const char *gameType
= "Unknown";
56 switch (optionsGame_
.getTurnType())
58 case OptionsGame::TurnSequentialLooserFirst
:
59 gameType
= "Sequential (Loser)";
61 case OptionsGame::TurnSequentialRandom
:
62 gameType
= "Sequential (Random)";
64 case OptionsGame::TurnSimultaneous
:
65 gameType
= "Simultaneous";
71 unsigned int OptionsTransient::getLeastUsedTeam(TankContainer
&container
)
73 // Reset all the counts
74 std::map
<unsigned int, unsigned int> counts
;
75 for (int i
=1; i
<=optionsGame_
.getTeams(); i
++)
80 // Add all the tanks to the counts
81 std::map
<unsigned int, Tank
*>::iterator itor
;
82 std::map
<unsigned int, Tank
*> &tanks
=
83 container
.getPlayingTanks();
84 for (itor
= tanks
.begin();
88 Tank
*tank
= (*itor
).second
;
89 if (!tank
->getState().getSpectator())
91 if (tank
->getTeam() > 0)
93 counts
[tank
->getTeam()] ++;
98 // Find the least counted team
99 unsigned int team
= 1;
100 unsigned int count
= counts
[1];
101 for (int i
=2; i
<=optionsGame_
.getTeams(); i
++)
103 if (counts
[i
] < count
)
112 bool OptionsTransient::writeToBuffer(NetBuffer
&buffer
)
114 if (!OptionEntryHelper::writeToBuffer(options_
, buffer
, false)) return false;
118 bool OptionsTransient::readFromBuffer(NetBufferReader
&reader
)
120 if (!OptionEntryHelper::readFromBuffer(options_
, reader
, false)) return false;
124 void OptionsTransient::reset()
126 currentGameNo_
.setValue(0);
127 currentRoundNo_
.setValue(0);
130 void OptionsTransient::startNewGame()
132 currentRoundNo_
.setValue(optionsGame_
.getNoRounds()+1);
135 void OptionsTransient::startNewRound()
137 currentGameNo_
.setValue(optionsGame_
.getNoMaxRoundTurns() + 1);
140 void OptionsTransient::newGame()
143 currentRoundNo_
.setValue(currentRoundNo_
.getValue() + 1);
144 if (currentRoundNo_
.getValue() >= optionsGame_
.getBuyOnRound() &&
145 !optionsGame_
.getGiveAllWeapons())
147 currentGameNo_
.setValue(0);
151 currentGameNo_
.setValue(1);
158 void OptionsTransient::nextRound()
162 currentGameNo_
.setValue(currentGameNo_
.getValue() + 1);
168 void OptionsTransient::newGameWind()
170 RandomGenerator random
;
173 switch(optionsGame_
.getWindForce())
175 case OptionsGame::WindRandom
:
177 (random
.getRandFixed() * fixed(true, 59000)).asInt()); // ie range 0->5
179 case OptionsGame::Wind1
:
180 case OptionsGame::Wind2
:
181 case OptionsGame::Wind3
:
182 case OptionsGame::Wind4
:
183 case OptionsGame::Wind5
:
185 fixed(int(optionsGame_
.getWindForce()) - 1));
187 case OptionsGame::WindBreezy
:
189 (random
.getRandFixed() * fixed(true, 29000)).asInt());// ie range 0->2);
191 case OptionsGame::WindGale
:
193 (random
.getRandFixed() * fixed(true, 29000)).asInt() + 3); // ie range 3->5);
195 case OptionsGame::WindNone
:
197 windSpeed_
.setValue(0);
201 if (windSpeed_
.getValue() > 0)
203 fixed winAngle
= random
.getRandFixed() * 360;
204 windStartAngle_
.setValue(winAngle
);
205 windAngle_
.setValue(winAngle
);
207 fixed windDirX
= (winAngle
/ fixed(180) * fixed::XPI
).sin();
208 fixed windDirY
= (winAngle
/ fixed(180) * fixed::XPI
).cos();
209 FixedVector
windDir(windDirX
, windDirY
, 0);
210 windDirection_
.setValue(windDir
);
214 windStartAngle_
.setValue(0);
215 windAngle_
.setValue(0);
216 windDirection_
.setValue(FixedVector::getNullVector());
220 void OptionsTransient::nextRoundWind()
222 if (optionsGame_
.getWindType() != OptionsGame::WindOnMove
)
227 if (windSpeed_
.getValue() > 0)
229 RandomGenerator random
;
232 fixed winAngle
= windStartAngle_
.getValue() + ((random
.getRandFixed() * 40) - 20);
233 windAngle_
.setValue(winAngle
);
235 fixed windDirX
= (winAngle
/ fixed(180) * fixed::XPI
).sin();
236 fixed windDirY
= (winAngle
/ fixed(180) * fixed::XPI
).cos();
237 FixedVector
windDir(windDirX
, windDirY
, 0);
238 windDirection_
.setValue(windDir
);
242 Vector
&OptionsTransient::getWallColor()
244 static Vector wallColor
;
245 switch (getWallType())
248 wallColor
= Vector(0.5f
, 0.5f
, 0.0f
);
251 wallColor
= Vector(0.0f
, 0.0f
, 0.5f
);
254 wallColor
= Vector(0.5f
, 0.5f
, 0.5f
);
261 void OptionsTransient::newGameWall()
263 switch (optionsGame_
.getWallType())
265 case OptionsGame::WallConcrete
:
266 wallType_
.setValue((int) wallConcrete
);
268 case OptionsGame::WallBouncy
:
269 wallType_
.setValue((int) wallBouncy
);
271 case OptionsGame::WallWrapAround
:
272 wallType_
.setValue((int) wallWrapAround
);
274 case OptionsGame::WallNone
:
275 wallType_
.setValue((int) wallNone
);
278 case OptionsGame::WallActive
: // Bouncy or Wrap
279 r
= RAND
* 2.0f
+ 1.0f
;
280 wallType_
.setValue((int) r
);
282 case OptionsGame::WallInactive
: // Concrete or None
283 if (RAND
< 0.5f
) wallType_
.setValue((int) wallConcrete
);
284 else wallType_
.setValue((int) wallNone
);
288 wallType_
.setValue((int) r
);
293 int OptionsTransient::getArmsLevel()
295 float start
= (float) optionsGame_
.getStartArmsLevel();
296 float end
= (float) optionsGame_
.getEndArmsLevel();
298 float roundsPlayed
= float(getCurrentRoundNo());
299 float totalRounds
= float(optionsGame_
.getNoRounds());
301 float armsLevel
= start
+ ((end
- start
) * (roundsPlayed
/ totalRounds
));
302 return (int) armsLevel
;