git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / tank / TankState.cpp
blobcd21cbe2ababb0d4f00a5d6777b9c722d1309f4d
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>
32 static struct AllowedStateTransitions
34 TankState::State from, to;
36 allowedStateTransitions[] =
38 TankState::sLoading, TankState::sInitializing,
39 TankState::sInitializing,TankState::sPending,
40 TankState::sPending, TankState::sDead,
41 TankState::sDead, TankState::sNormal,
42 TankState::sNormal , TankState::sDead
45 TankState::TankState(ScorchedContext &context, unsigned int playerId) :
46 state_(sLoading), tank_(0),
47 readyState_(sReady),
48 context_(context), spectator_(false),
49 muted_(false),
50 skipshots_(false),
51 lives_(0), maxLives_(1), destroy_(false)
55 TankState::~TankState()
59 void TankState::newMatch()
61 setState(sDead);
62 readyState_ = sReady;
65 void TankState::newGame()
67 setState(sNormal);
68 if (!tank_->isTemp())
70 maxLives_ = context_.getOptionsGame().getPlayerLives();
73 lives_ = maxLives_;
74 tank_->getTargetState().setFalling(0);
77 void TankState::clientNewGame()
79 skipshots_ = false;
82 void TankState::setState(State s)
84 for (int i=0; i<sizeof(allowedStateTransitions) /
85 sizeof(AllowedStateTransitions); i++)
87 if (state_ == allowedStateTransitions[i].from &&
88 s == allowedStateTransitions[i].to)
90 state_ = s;
91 break;
95 if (state_ != sNormal)
97 // Make sure the target and shield physics
98 // are disabled
99 tank_->getLife().setLife(0);
100 tank_->getShield().setCurrentShield(0);
102 else
104 // Make sure target space contains tank
105 tank_->getLife().setLife(tank_->getLife().getLife());
109 const char *TankState::getStateString()
111 static char string[1024];
112 snprintf(string, 1024, "%s - %s %s(%i hp)",
113 ((readyState_==sReady)?"Rdy":"Wait"),
114 getSmallStateString(),
115 (muted_?"muted ":""),
116 (int) tank_->getLife().getLife().asInt());
117 return string;
120 const char *TankState::getSmallStateString()
122 const char *type = "";
123 switch (state_)
125 case sPending:
126 type = spectator_?"(Spec)Pending":"Pending";
127 break;
128 case sNormal:
129 type = spectator_?"(Spec)Alive":"Alive";
130 break;
131 case sInitializing:
132 type = spectator_?"(Spec)Initializing":"Initializing";
133 break;
134 case sLoading:
135 type = spectator_?"(Spec)Loading":"Loading";
136 break;
137 case sDead:
138 type = spectator_?"(Spec)Dead":"Dead";
139 break;
142 return type;
145 LangString &TankState::getSmallStateLangString()
147 LANG_RESOURCE_CONST_VAR(SPEC_PENDING, "SPEC_PENDING", "(Spec)Pending");
148 LANG_RESOURCE_CONST_VAR(PENDING, "PENDING", "Pending");
149 LANG_RESOURCE_CONST_VAR(SPEC_ALIVE, "SPEC_ALIVE", "(Spec)Alive");
150 LANG_RESOURCE_CONST_VAR(ALIVE, "ALIVE", "Alive");
151 LANG_RESOURCE_CONST_VAR(SPEC_INITIALIZING, "SPEC_INITIALIZING", "(Spec)Initializing");
152 LANG_RESOURCE_CONST_VAR(INITIALIZING, "INITIALIZING", "Initializing");
153 LANG_RESOURCE_CONST_VAR(SPEC_LOADING, "SPEC_LOADING", "(Spec)Loading");
154 LANG_RESOURCE_CONST_VAR(LOADING, "LOADING", "Loading");
155 LANG_RESOURCE_CONST_VAR(SPEC_DEAD, "SPEC_DEAD", "(Spec)Dead");
156 LANG_RESOURCE_CONST_VAR(DEAD, "DEAD", "Dead");
159 switch (state_)
161 case sPending:
162 return spectator_?SPEC_PENDING:PENDING;
163 case sNormal:
164 return spectator_?SPEC_ALIVE:ALIVE;
165 case sInitializing:
166 return spectator_?SPEC_INITIALIZING:INITIALIZING;
167 case sLoading:
168 return spectator_?SPEC_LOADING:LOADING;
169 case sDead:
170 return spectator_?SPEC_DEAD:DEAD;
171 break;
174 static LangString nullResult;
175 return nullResult;
178 bool TankState::writeMessage(NetBuffer &buffer)
180 buffer.addToBuffer((int) state_);
181 buffer.addToBuffer(spectator_);
182 buffer.addToBuffer(lives_);
183 buffer.addToBuffer(maxLives_);
184 return true;
187 bool TankState::readMessage(NetBufferReader &reader)
189 int s;
190 if (!reader.getFromBuffer(s)) return false;
191 state_ = (TankState::State) s;
192 setState((TankState::State) s);
193 if (!reader.getFromBuffer(spectator_)) return false;
194 if (!reader.getFromBuffer(lives_)) return false;
195 if (!reader.getFromBuffer(maxLives_)) return false;
196 return true;