git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / common / OptionsTransient.cpp
blob363734a6cb580b66f60141ca8ea3a41cddff6fd7
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 <common/OptionsTransient.h>
22 #include <common/OptionsScorched.h>
23 #include <common/Defines.h>
24 #include <tank/TankContainer.h>
25 #include <tank/TankState.h>
26 #include <time.h>
27 #include <math.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)";
60 break;
61 case OptionsGame::TurnSequentialRandom:
62 gameType = "Sequential (Random)";
63 break;
64 case OptionsGame::TurnSimultaneous:
65 gameType = "Simultaneous";
66 break;
68 return gameType;
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++)
77 counts[i] = 0;
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();
85 itor != tanks.end();
86 itor++)
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)
105 team = i;
106 count = counts[i];
109 return team;
112 bool OptionsTransient::writeToBuffer(NetBuffer &buffer)
114 if (!OptionEntryHelper::writeToBuffer(options_, buffer, false)) return false;
115 return true;
118 bool OptionsTransient::readFromBuffer(NetBufferReader &reader)
120 if (!OptionEntryHelper::readFromBuffer(options_, reader, false)) return false;
121 return true;
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()
142 newGame_ = true;
143 currentRoundNo_.setValue(currentRoundNo_.getValue() + 1);
144 if (currentRoundNo_.getValue() >= optionsGame_.getBuyOnRound() &&
145 !optionsGame_.getGiveAllWeapons())
147 currentGameNo_.setValue(0);
149 else
151 currentGameNo_.setValue(1);
154 newGameWind();
155 newGameWall();
158 void OptionsTransient::nextRound()
160 if (!newGame_)
162 currentGameNo_.setValue(currentGameNo_.getValue() + 1);
164 newGame_ = false;
165 nextRoundWind();
168 void OptionsTransient::newGameWind()
170 RandomGenerator random;
171 random.seed(rand());
173 switch(optionsGame_.getWindForce())
175 case OptionsGame::WindRandom:
176 windSpeed_.setValue(
177 (random.getRandFixed() * fixed(true, 59000)).asInt()); // ie range 0->5
178 break;
179 case OptionsGame::Wind1:
180 case OptionsGame::Wind2:
181 case OptionsGame::Wind3:
182 case OptionsGame::Wind4:
183 case OptionsGame::Wind5:
184 windSpeed_.setValue(
185 fixed(int(optionsGame_.getWindForce()) - 1));
186 break;
187 case OptionsGame::WindBreezy:
188 windSpeed_.setValue(
189 (random.getRandFixed() * fixed(true, 29000)).asInt());// ie range 0->2);
190 break;
191 case OptionsGame::WindGale:
192 windSpeed_.setValue(
193 (random.getRandFixed() * fixed(true, 29000)).asInt() + 3); // ie range 3->5);
194 break;
195 case OptionsGame::WindNone:
196 default:
197 windSpeed_.setValue(0);
198 break;
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);
212 else
214 windStartAngle_.setValue(0);
215 windAngle_.setValue(0);
216 windDirection_.setValue(FixedVector::getNullVector());
220 void OptionsTransient::nextRoundWind()
222 if (optionsGame_.getWindType() != OptionsGame::WindOnMove)
224 return;
227 if (windSpeed_.getValue() > 0)
229 RandomGenerator random;
230 random.seed(rand());
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())
247 case wallWrapAround:
248 wallColor = Vector(0.5f, 0.5f, 0.0f);
249 break;
250 case wallBouncy:
251 wallColor = Vector(0.0f, 0.0f, 0.5f);
252 break;
253 default:
254 wallColor = Vector(0.5f, 0.5f, 0.5f);
255 break;
258 return wallColor;
261 void OptionsTransient::newGameWall()
263 switch (optionsGame_.getWallType())
265 case OptionsGame::WallConcrete:
266 wallType_.setValue((int) wallConcrete);
267 break;
268 case OptionsGame::WallBouncy:
269 wallType_.setValue((int) wallBouncy);
270 break;
271 case OptionsGame::WallWrapAround:
272 wallType_.setValue((int) wallWrapAround);
273 break;
274 case OptionsGame::WallNone:
275 wallType_.setValue((int) wallNone);
276 break;
277 float r;
278 case OptionsGame::WallActive: // Bouncy or Wrap
279 r = RAND * 2.0f + 1.0f;
280 wallType_.setValue((int) r);
281 break;
282 case OptionsGame::WallInactive: // Concrete or None
283 if (RAND < 0.5f) wallType_.setValue((int) wallConcrete);
284 else wallType_.setValue((int) wallNone);
285 break;
286 default:
287 r = RAND * 4.0f;
288 wallType_.setValue((int) r);
289 break;
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;