TankState.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <stdio.h>
00022 #include <tank/Tank.h>
00023 #include <tank/TankState.h>
00024 #include <target/TargetLife.h>
00025 #include <target/TargetShield.h>
00026 #include <target/TargetState.h>
00027 #include <engine/ScorchedContext.h>
00028 #include <lang/LangResource.h>
00029 #include <common/OptionsScorched.h>
00030 #include <common/Defines.h>
00031 #include <common/Logger.h>
00032 
00033 static struct AllowedStateTransitions
00034 {
00035         TankState::State from, to;
00036 }
00037 allowedStateTransitions[] =
00038 {
00039         TankState::sLoading, TankState::sInitializing,
00040         TankState::sInitializing,TankState::sPending,
00041         TankState::sPending, TankState::sDead,
00042         TankState::sDead, TankState::sNormal,
00043         TankState::sNormal , TankState::sDead
00044 };
00045 
00046 TankState::TankState(ScorchedContext &context, unsigned int playerId) : 
00047         state_(sLoading), tank_(0),
00048         readyState_(sReady),
00049         context_(context), spectator_(false), 
00050         muted_(false),
00051         skipshots_(false),
00052         lives_(0), maxLives_(1), destroy_(false)
00053 {
00054 }
00055 
00056 TankState::~TankState()
00057 {
00058 }
00059 
00060 void TankState::newMatch()
00061 {
00062         setState(sDead);
00063         readyState_ = sReady;
00064 }
00065 
00066 void TankState::newGame()
00067 {
00068         setState(sNormal);
00069         if (!tank_->isTemp())
00070         {
00071                 maxLives_ = context_.getOptionsGame().getPlayerLives();
00072         }
00073 
00074         lives_ = maxLives_;
00075         tank_->getTargetState().setFalling(0);
00076 }
00077 
00078 void TankState::clientNewGame()
00079 {
00080         skipshots_ = false;
00081 }
00082 
00083 void TankState::setState(State s)
00084 {
00085         for (int i=0; i<sizeof(allowedStateTransitions) / 
00086                         sizeof(AllowedStateTransitions); i++)
00087         {
00088                 if (state_ == allowedStateTransitions[i].from &&
00089                         s == allowedStateTransitions[i].to)
00090                 {
00091                         state_ = s;
00092                         break;
00093                 }
00094         }
00095 
00096         if (state_ != sNormal)
00097         {
00098                 // Make sure the target and shield physics
00099                 // are disabled
00100                 tank_->getLife().setLife(0);
00101                 tank_->getShield().setCurrentShield(0);
00102         }
00103         else
00104         {
00105                 // Make sure target space contains tank
00106                 tank_->getLife().setLife(tank_->getLife().getLife());
00107         }
00108 }
00109 
00110 const char *TankState::getStateString()
00111 {
00112         static char string[1024];
00113         snprintf(string, 1024, "%s - %s %s(%i hp)",
00114                 ((readyState_==sReady)?"Rdy":"Wait"),
00115                 getSmallStateString(),
00116                 (muted_?"muted ":""),
00117                 (int) tank_->getLife().getLife().asInt());
00118         return string;
00119 }
00120 
00121 const char *TankState::getSmallStateString()
00122 {
00123         const char *type = "";
00124         switch (state_)
00125         {
00126         case sPending:
00127                 type = spectator_?"(Spec)Pending":"Pending";
00128                 break;
00129         case sNormal:
00130                 type = spectator_?"(Spec)Alive":"Alive";
00131                 break;
00132         case sInitializing:
00133                 type = spectator_?"(Spec)Initializing":"Initializing";
00134                 break;
00135         case sLoading:
00136                 type = spectator_?"(Spec)Loading":"Loading";
00137                 break;
00138         case sDead:
00139                 type = spectator_?"(Spec)Dead":"Dead";
00140                 break;
00141         }
00142 
00143         return type;
00144 }
00145 
00146 LangString &TankState::getSmallStateLangString()
00147 {
00148         LANG_RESOURCE_CONST_VAR(SPEC_PENDING, "SPEC_PENDING", "(Spec)Pending");
00149         LANG_RESOURCE_CONST_VAR(PENDING, "PENDING", "Pending");
00150         LANG_RESOURCE_CONST_VAR(SPEC_ALIVE, "SPEC_ALIVE", "(Spec)Alive");
00151         LANG_RESOURCE_CONST_VAR(ALIVE, "ALIVE", "Alive");
00152         LANG_RESOURCE_CONST_VAR(SPEC_INITIALIZING, "SPEC_INITIALIZING", "(Spec)Initializing");
00153         LANG_RESOURCE_CONST_VAR(INITIALIZING, "INITIALIZING", "Initializing");
00154         LANG_RESOURCE_CONST_VAR(SPEC_LOADING, "SPEC_LOADING", "(Spec)Loading");
00155         LANG_RESOURCE_CONST_VAR(LOADING, "LOADING", "Loading");
00156         LANG_RESOURCE_CONST_VAR(SPEC_DEAD, "SPEC_DEAD", "(Spec)Dead");
00157         LANG_RESOURCE_CONST_VAR(DEAD, "DEAD", "Dead");
00158 
00159 
00160         switch (state_)
00161         {
00162         case sPending:
00163                 return spectator_?SPEC_PENDING:PENDING;
00164         case sNormal:
00165                 return spectator_?SPEC_ALIVE:ALIVE;
00166         case sInitializing:
00167                 return spectator_?SPEC_INITIALIZING:INITIALIZING;
00168         case sLoading:
00169                 return spectator_?SPEC_LOADING:LOADING;
00170         case sDead:
00171                 return spectator_?SPEC_DEAD:DEAD;
00172                 break;
00173         }
00174 
00175         static LangString nullResult;
00176         return nullResult;
00177 }
00178 
00179 bool TankState::writeMessage(NetBuffer &buffer)
00180 {
00181         buffer.addToBuffer((int) state_);
00182         buffer.addToBuffer(spectator_);
00183         buffer.addToBuffer(lives_);
00184         buffer.addToBuffer(maxLives_);
00185         return true;
00186 }
00187 
00188 bool TankState::readMessage(NetBufferReader &reader)
00189 {
00190         int s;
00191         if (!reader.getFromBuffer(s))
00192         {
00193                 Logger::log("TankState::state_ read failed");
00194                 return false;
00195         }
00196         state_ = (TankState::State) s;
00197         setState((TankState::State) s);
00198         if (!reader.getFromBuffer(spectator_))
00199         {
00200                 Logger::log("TankState::spectator_ read failed");
00201                 return false;
00202         }
00203         if (!reader.getFromBuffer(lives_))
00204         {
00205                 Logger::log("TankState::lives_ read failed");
00206                 return false;
00207         }
00208         if (!reader.getFromBuffer(maxLives_))
00209         {
00210                 Logger::log("TankState::maxLives_ read failed");
00211                 return false;
00212         }
00213         return true;
00214 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3