Handle OptionEntryEnum in the console
[scorched3d/parasti.git] / src / common / tank / TankState.cpp
blob18ec7eae33f76f05299fa7a915dff05d0ce17a8b
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 <stdio.h>
22 #include <tank/Tank.h>
23 #include <tank/TankState.h>
24 #include <target/TargetLife.h>
25 #include <target/TargetShield.h>
26 #include <target/TargetState.h>
27 #include <engine/ScorchedContext.h>
28 #include <lang/LangResource.h>
29 #include <common/OptionsScorched.h>
30 #include <common/Defines.h>
31 #include <common/Logger.h>
33 static struct AllowedStateTransitions
35 TankState::State from, to;
37 allowedStateTransitions[] =
39 TankState::sLoading, TankState::sInitializing,
40 TankState::sInitializing,TankState::sPending,
41 TankState::sPending, TankState::sDead,
42 TankState::sDead, TankState::sNormal,
43 TankState::sNormal , TankState::sDead
46 TankState::TankState(ScorchedContext &context, unsigned int playerId) :
47 state_(sLoading), tank_(0),
48 readyState_(sReady),
49 context_(context), spectator_(false),
50 muted_(false),
51 skipshots_(false),
52 lives_(0), maxLives_(1), destroy_(false)
56 TankState::~TankState()
60 void TankState::newMatch()
62 setState(sDead);
63 readyState_ = sReady;
66 void TankState::newGame()
68 setState(sNormal);
69 if (!tank_->isTemp())
71 maxLives_ = context_.getOptionsGame().getPlayerLives();
74 lives_ = maxLives_;
75 tank_->getTargetState().setFalling(0);
78 void TankState::clientNewGame()
80 skipshots_ = false;
83 void TankState::setState(State s)
85 for (int i=0; i<sizeof(allowedStateTransitions) /
86 sizeof(AllowedStateTransitions); i++)
88 if (state_ == allowedStateTransitions[i].from &&
89 s == allowedStateTransitions[i].to)
91 state_ = s;
92 break;
96 if (state_ != sNormal)
98 // Make sure the target and shield physics
99 // are disabled
100 tank_->getLife().setLife(0);
101 tank_->getShield().setCurrentShield(0);
103 else
105 // Make sure target space contains tank
106 tank_->getLife().setLife(tank_->getLife().getLife());
110 const char *TankState::getStateString()
112 static char string[1024];
113 snprintf(string, 1024, "%s - %s %s(%i hp)",
114 ((readyState_==sReady)?"Rdy":"Wait"),
115 getSmallStateString(),
116 (muted_?"muted ":""),
117 (int) tank_->getLife().getLife().asInt());
118 return string;
121 const char *TankState::getSmallStateString()
123 const char *type = "";
124 switch (state_)
126 case sPending:
127 type = spectator_?"(Spec)Pending":"Pending";
128 break;
129 case sNormal:
130 type = spectator_?"(Spec)Alive":"Alive";
131 break;
132 case sInitializing:
133 type = spectator_?"(Spec)Initializing":"Initializing";
134 break;
135 case sLoading:
136 type = spectator_?"(Spec)Loading":"Loading";
137 break;
138 case sDead:
139 type = spectator_?"(Spec)Dead":"Dead";
140 break;
143 return type;
146 LangString &TankState::getSmallStateLangString()
148 LANG_RESOURCE_CONST_VAR(SPEC_PENDING, "SPEC_PENDING", "(Spec)Pending");
149 LANG_RESOURCE_CONST_VAR(PENDING, "PENDING", "Pending");
150 LANG_RESOURCE_CONST_VAR(SPEC_ALIVE, "SPEC_ALIVE", "(Spec)Alive");
151 LANG_RESOURCE_CONST_VAR(ALIVE, "ALIVE", "Alive");
152 LANG_RESOURCE_CONST_VAR(SPEC_INITIALIZING, "SPEC_INITIALIZING", "(Spec)Initializing");
153 LANG_RESOURCE_CONST_VAR(INITIALIZING, "INITIALIZING", "Initializing");
154 LANG_RESOURCE_CONST_VAR(SPEC_LOADING, "SPEC_LOADING", "(Spec)Loading");
155 LANG_RESOURCE_CONST_VAR(LOADING, "LOADING", "Loading");
156 LANG_RESOURCE_CONST_VAR(SPEC_DEAD, "SPEC_DEAD", "(Spec)Dead");
157 LANG_RESOURCE_CONST_VAR(DEAD, "DEAD", "Dead");
160 switch (state_)
162 case sPending:
163 return spectator_?SPEC_PENDING:PENDING;
164 case sNormal:
165 return spectator_?SPEC_ALIVE:ALIVE;
166 case sInitializing:
167 return spectator_?SPEC_INITIALIZING:INITIALIZING;
168 case sLoading:
169 return spectator_?SPEC_LOADING:LOADING;
170 case sDead:
171 return spectator_?SPEC_DEAD:DEAD;
172 break;
175 static LangString nullResult;
176 return nullResult;
179 bool TankState::writeMessage(NetBuffer &buffer)
181 buffer.addToBuffer((int) state_);
182 buffer.addToBuffer(spectator_);
183 buffer.addToBuffer(lives_);
184 buffer.addToBuffer(maxLives_);
185 return true;
188 bool TankState::readMessage(NetBufferReader &reader)
190 int s;
191 if (!reader.getFromBuffer(s))
193 Logger::log("TankState::state_ read failed");
194 return false;
196 state_ = (TankState::State) s;
197 setState((TankState::State) s);
198 if (!reader.getFromBuffer(spectator_))
200 Logger::log("TankState::spectator_ read failed");
201 return false;
203 if (!reader.getFromBuffer(lives_))
205 Logger::log("TankState::lives_ read failed");
206 return false;
208 if (!reader.getFromBuffer(maxLives_))
210 Logger::log("TankState::maxLives_ read failed");
211 return false;
213 return true;