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 ////////////////////////////////////////////////////////////////////////////////
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),
49 context_(context
), spectator_(false),
52 lives_(0), maxLives_(1), destroy_(false)
56 TankState::~TankState()
60 void TankState::newMatch()
66 void TankState::newGame()
71 maxLives_
= context_
.getOptionsGame().getPlayerLives();
75 tank_
->getTargetState().setFalling(0);
78 void TankState::clientNewGame()
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
)
96 if (state_
!= sNormal
)
98 // Make sure the target and shield physics
100 tank_
->getLife().setLife(0);
101 tank_
->getShield().setCurrentShield(0);
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());
121 const char *TankState::getSmallStateString()
123 const char *type
= "";
127 type
= spectator_
?"(Spec)Pending":"Pending";
130 type
= spectator_
?"(Spec)Alive":"Alive";
133 type
= spectator_
?"(Spec)Initializing":"Initializing";
136 type
= spectator_
?"(Spec)Loading":"Loading";
139 type
= spectator_
?"(Spec)Dead":"Dead";
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");
163 return spectator_
?SPEC_PENDING
:PENDING
;
165 return spectator_
?SPEC_ALIVE
:ALIVE
;
167 return spectator_
?SPEC_INITIALIZING
:INITIALIZING
;
169 return spectator_
?SPEC_LOADING
:LOADING
;
171 return spectator_
?SPEC_DEAD
:DEAD
;
175 static LangString nullResult
;
179 bool TankState::writeMessage(NetBuffer
&buffer
)
181 buffer
.addToBuffer((int) state_
);
182 buffer
.addToBuffer(spectator_
);
183 buffer
.addToBuffer(lives_
);
184 buffer
.addToBuffer(maxLives_
);
188 bool TankState::readMessage(NetBufferReader
&reader
)
191 if (!reader
.getFromBuffer(s
))
193 Logger::log("TankState::state_ read failed");
196 state_
= (TankState::State
) s
;
197 setState((TankState::State
) s
);
198 if (!reader
.getFromBuffer(spectator_
))
200 Logger::log("TankState::spectator_ read failed");
203 if (!reader
.getFromBuffer(lives_
))
205 Logger::log("TankState::lives_ read failed");
208 if (!reader
.getFromBuffer(maxLives_
))
210 Logger::log("TankState::maxLives_ read failed");