1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
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.
10 //
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.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <common/OptionsTransient.h>
22 #include <common/OptionsScorched.h>
23 #include <common/Defines.h>
24 #include <target/TargetContainer.h>
25 #include <tank/Tank.h>
26 #include <tank/TankState.h>
27 #include <time.h>
28 #include <math.h>
29
OptionsTransient(OptionsScorched & optionsGame)30 OptionsTransient::OptionsTransient(OptionsScorched &optionsGame) :
31 optionsGame_(optionsGame), newGame_(false),
32 currentRoundNo_(options_, "CurrentRoundNo",
33 "The current number of rounds played in this game", 0, 0),
34 currentTurnNo_(options_, "CurrentTurnNo",
35 "The current number of turns played in this round", 0, 1),
36 wallType_(options_, "WallType",
37 "The current wall type", 0, 0)
38 {
39
40 }
41
~OptionsTransient()42 OptionsTransient::~OptionsTransient()
43 {
44 }
45
getLeastUsedTeam(TargetContainer & container)46 unsigned int OptionsTransient::getLeastUsedTeam(TargetContainer &container)
47 {
48 // Reset all the counts
49 std::map<unsigned int, unsigned int> counts;
50 for (int i=1; i<=optionsGame_.getTeams(); i++)
51 {
52 counts[i] = 0;
53 }
54
55 // Add all the tanks to the counts
56 std::map<unsigned int, Tank *>::iterator itor;
57 std::map<unsigned int, Tank *> &tanks =
58 container.getTanks();
59 for (itor = tanks.begin();
60 itor != tanks.end();
61 ++itor)
62 {
63 Tank *tank = (*itor).second;
64 if (tank->getTeam() > 0 &&
65 tank->getState().getTankPlaying())
66 {
67 counts[tank->getTeam()] ++;
68 }
69 }
70
71 // Find the least counted team
72 unsigned int team = 1;
73 unsigned int count = counts[1];
74 for (int i=2; i<=optionsGame_.getTeams(); i++)
75 {
76 if (counts[i] < count)
77 {
78 team = i;
79 count = counts[i];
80 }
81 }
82 return team;
83 }
84
writeToBuffer(NetBuffer & buffer)85 bool OptionsTransient::writeToBuffer(NetBuffer &buffer)
86 {
87 if (!OptionEntryHelper::writeToBuffer(options_, buffer, false)) return false;
88 return true;
89 }
90
readFromBuffer(NetBufferReader & reader)91 bool OptionsTransient::readFromBuffer(NetBufferReader &reader)
92 {
93 if (!OptionEntryHelper::readFromBuffer(options_, reader, false)) return false;
94 return true;
95 }
96
reset()97 void OptionsTransient::reset()
98 {
99 currentRoundNo_.setValue(0);
100 currentTurnNo_.setValue(1);
101 }
102
startNewGame()103 void OptionsTransient::startNewGame()
104 {
105 currentRoundNo_.setValue(optionsGame_.getNoRounds()+1);
106 }
107
newGame()108 void OptionsTransient::newGame()
109 {
110 currentTurnNo_.setValue(1);
111 currentRoundNo_.setValue(currentRoundNo_.getValue() + 1);
112 newGameWall();
113 }
114
getWallColor()115 Vector &OptionsTransient::getWallColor()
116 {
117 static Vector wallColor;
118 switch (getWallType())
119 {
120 case wallWrapAround:
121 wallColor = Vector(0.5f, 0.5f, 0.0f);
122 break;
123 case wallBouncy:
124 wallColor = Vector(0.0f, 0.0f, 0.5f);
125 break;
126 case wallConcrete:
127 wallColor = Vector(0.5f, 0.5f, 0.5f);
128 break;
129 default:
130 wallColor = Vector(0.0f, 0.0f, 0.0f);
131 break;
132 }
133
134 return wallColor;
135 }
136
newGameWall()137 void OptionsTransient::newGameWall()
138 {
139 switch (optionsGame_.getWallType())
140 {
141 case OptionsGame::WallConcrete:
142 wallType_.setValue((int) wallConcrete);
143 break;
144 case OptionsGame::WallBouncy:
145 wallType_.setValue((int) wallBouncy);
146 break;
147 case OptionsGame::WallWrapAround:
148 wallType_.setValue((int) wallWrapAround);
149 break;
150 case OptionsGame::WallNone:
151 wallType_.setValue((int) wallNone);
152 break;
153 float r;
154 case OptionsGame::WallActive: // Bouncy or Wrap
155 r = RAND * 2.0f + 1.0f;
156 wallType_.setValue((int) r);
157 break;
158 case OptionsGame::WallInactive: // Concrete or None
159 if (RAND < 0.5f) wallType_.setValue((int) wallConcrete);
160 else wallType_.setValue((int) wallNone);
161 break;
162 default:
163 r = RAND * 4.0f;
164 wallType_.setValue((int) r);
165 break;
166 }
167 }
168
getArmsLevel()169 int OptionsTransient::getArmsLevel()
170 {
171 float start = (float) optionsGame_.getStartArmsLevel();
172 float end = (float) optionsGame_.getEndArmsLevel();
173
174 float roundsPlayed = float(getCurrentRoundNo());
175 float totalRounds = float(optionsGame_.getNoRounds());
176
177 float armsLevel = start + ((end - start) * (roundsPlayed / totalRounds));
178 return (int) armsLevel;
179 }
180