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>
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),
48 context_(context
), spectator_(false),
51 lives_(0), maxLives_(1), destroy_(false)
55 TankState::~TankState()
59 void TankState::newMatch()
65 void TankState::newGame()
70 maxLives_
= context_
.getOptionsGame().getPlayerLives();
74 tank_
->getTargetState().setFalling(0);
77 void TankState::clientNewGame()
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
)
95 if (state_
!= sNormal
)
97 // Make sure the target and shield physics
99 tank_
->getLife().setLife(0);
100 tank_
->getShield().setCurrentShield(0);
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());
120 const char *TankState::getSmallStateString()
122 const char *type
= "";
126 type
= spectator_
?"(Spec)Pending":"Pending";
129 type
= spectator_
?"(Spec)Alive":"Alive";
132 type
= spectator_
?"(Spec)Initializing":"Initializing";
135 type
= spectator_
?"(Spec)Loading":"Loading";
138 type
= spectator_
?"(Spec)Dead":"Dead";
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");
162 return spectator_
?SPEC_PENDING
:PENDING
;
164 return spectator_
?SPEC_ALIVE
:ALIVE
;
166 return spectator_
?SPEC_INITIALIZING
:INITIALIZING
;
168 return spectator_
?SPEC_LOADING
:LOADING
;
170 return spectator_
?SPEC_DEAD
:DEAD
;
174 static LangString nullResult
;
178 bool TankState::writeMessage(NetBuffer
&buffer
)
180 buffer
.addToBuffer((int) state_
);
181 buffer
.addToBuffer(spectator_
);
182 buffer
.addToBuffer(lives_
);
183 buffer
.addToBuffer(maxLives_
);
187 bool TankState::readMessage(NetBufferReader
&reader
)
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;