Tank.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 <math.h>
00022 #include <tank/Tank.h>
00023 #include <tank/TankType.h>
00024 #include <tank/TankColorGenerator.h>
00025 #include <tank/TankModelStore.h>
00026 #include <tank/TankAccessories.h>
00027 #include <tank/TankScore.h>
00028 #include <tank/TankState.h>
00029 #include <tank/TankPosition.h>
00030 #include <tank/TankModelContainer.h>
00031 #include <tank/TankMod.h>
00032 #include <tank/TankAvatar.h>
00033 #include <tank/TankCamera.h>
00034 #include <tankai/TankAI.h>
00035 #include <tankai/TankAIStore.h>
00036 #include <weapons/AccessoryStore.h>
00037 #include <target/TargetLife.h>
00038 #include <engine/ScorchedContext.h>
00039 #include <common/Defines.h>
00040 #include <common/Logger.h>
00041 
00042 Tank::Tank(ScorchedContext &context, 
00043                 unsigned int playerId, 
00044                 unsigned int destinationId,
00045                 const LangString &name, 
00046                 Vector &color, 
00047                 const char *modelName,
00048                 const char *typeName) :
00049         Target(playerId, name, context), 
00050         context_(context),
00051         destinationId_(destinationId),
00052         color_(color), 
00053         tankAI_(0),
00054         team_(0), 
00055         ipAddress_(0), 
00056         keepAlive_(0)
00057 {
00058         accessories_ = new TankAccessories(context);
00059         score_ = new TankScore(context);
00060         state_ = new TankState(context, playerId);
00061         position_ = new TankPosition(context);
00062         modelContainer_ = new TankModelContainer(modelName, typeName);
00063         mod_ = new TankMod();
00064         avatar_ = new TankAvatar();
00065         camera_ = new TankCamera(context);
00066 
00067         position_->setTank(this);
00068         score_->setTank(this);
00069         state_->setTank(this);
00070         accessories_->setTank(this);
00071         modelContainer_->setTank(this);
00072         state_->setState(TankState::sLoading);
00073 }
00074 
00075 Tank::~Tank()
00076 {
00077         state_->setState(TankState::sDead);
00078 
00079         delete tankAI_; tankAI_ = 0;
00080         delete accessories_; accessories_ = 0;
00081         delete score_; score_ = 0;
00082         delete state_; state_ = 0;
00083         delete position_; position_ = 0;
00084         delete modelContainer_; modelContainer_ = 0;
00085         delete mod_; mod_ = 0;
00086         delete avatar_; avatar_ = 0;
00087         delete camera_; camera_ = 0;
00088 }
00089 
00090 void Tank::setTankAI(TankAI *ai)
00091 {
00092         if (tankAI_) delete tankAI_;
00093         tankAI_ = ai;
00094 }
00095 
00096 void Tank::newMatch()
00097 {
00098         accessories_->newMatch();
00099         score_->newMatch();
00100         state_->newMatch();
00101         if (tankAI_) tankAI_->newMatch();
00102 }
00103 
00104 void Tank::newGame()
00105 {
00106         TankType *tankType = 
00107                 context_.getTankModels().getTypeByName(
00108                         getModelContainer().getTankTypeName());
00109         getLife().setMaxLife(tankType->getLife());
00110 
00111         Target::newGame();
00112 
00113         state_->newGame();
00114         score_->newGame();
00115         position_->newGame();
00116         if (tankAI_) tankAI_->newGame();
00117 }
00118 
00119 void Tank::rezTank()
00120 {
00121         if (tankAI_) tankAI_->newGame();
00122         getState().setState(TankState::sNormal);
00123         getLife().setLife(getLife().getMaxLife());
00124         getPosition().undo();
00125 }
00126 
00127 void Tank::clientNewGame()
00128 {
00129         position_->clientNewGame();
00130         state_->clientNewGame();
00131         score_->clientNewGame();
00132 }
00133 
00134 bool Tank::getAlive()
00135 {
00136         return (getState().getState() == TankState::sNormal &&
00137                 getState().getSpectator() == false);
00138 }
00139 
00140 Weapon *Tank::getDeathAction()
00141 {
00142         setDeathAction(context_.getAccessoryStore().getDeathAnimation());
00143         return Target::getDeathAction();
00144 }
00145 
00146 Vector &Tank::getColor()
00147 {
00148         if (team_ > 0) return TankColorGenerator::getTeamColor(team_);
00149         return color_;
00150 }
00151 
00152 bool Tank::writeMessage(NetBuffer &buffer, bool writeAccessories)
00153 {
00154         if (!Target::writeMessage(buffer)) return false;  // Base class 1st
00155         buffer.addToBuffer(destinationId_);
00156         buffer.addToBuffer(team_);
00157         buffer.addToBuffer(color_);
00158         if (!state_->writeMessage(buffer)) return false;
00159         if (!accessories_->writeMessage(buffer, writeAccessories)) return false;
00160         if (!score_->writeMessage(buffer)) return false;
00161         if (!position_->writeMessage(buffer)) return false;
00162         if (!modelContainer_->writeMessage(buffer)) return false;
00163         return true;
00164 }
00165 
00166 bool Tank::readMessage(NetBufferReader &reader)
00167 {
00168         if (!Target::readMessage(reader)) 
00169         {
00170                 Logger::log("Target::readMessage failed");
00171                 return false; // Base class 1st
00172         }
00173         if (!reader.getFromBuffer(destinationId_))
00174         {
00175                 Logger::log("Tank::destinationId_ read failed");
00176                 return false;
00177         }
00178         if (!reader.getFromBuffer(team_))
00179         {
00180                 Logger::log("Tank::team_ read failed");
00181                 return false;
00182         }
00183         if (!reader.getFromBuffer(color_))
00184         {
00185                 Logger::log("Tank::color_ read failed");
00186                 return false;
00187         }
00188         if (!state_->readMessage(reader))
00189         {
00190                 Logger::log("Tank::state_ read failed");
00191                 return false;
00192         }
00193         if (!accessories_->readMessage(reader))
00194         {
00195                 Logger::log("Tank::accessories_ read failed");
00196                 return false;
00197         }
00198         if (!score_->readMessage(reader))
00199         {
00200                 Logger::log("Tank::score_ read failed");
00201                 return false;
00202         }
00203         if (!position_->readMessage(reader))
00204         {
00205                 Logger::log("Tank::position_ read failed");
00206                 return false;
00207         }
00208         if (!modelContainer_->readMessage(reader))
00209         {
00210                 Logger::log("Tank::modelContainer_ read failed");
00211                 return false;
00212         }
00213 
00214         if (!context_.getServerMode())
00215         {
00216                 // If any humans turn into computers remove the HumanAI
00217                 if (destinationId_ == 0) setTankAI(0);
00218         }
00219         return true;
00220 }

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