ShotState.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 <engine/ShotState.h>
00022 #include <engine/ActionController.h>
00023 #include <actions/CheckResurrection.h>
00024 #include <coms/ComsMessageSender.h>
00025 #include <common/Logger.h>
00026 #include <common/RandomGenerator.h>
00027 #include <common/OptionsScorched.h>
00028 #include <common/OptionsTransient.h>
00029 #include <tank/TankState.h>
00030 #include <tank/TankScore.h>
00031 #include <tank/TankAccessories.h>
00032 #include <tank/TankPosition.h>
00033 #include <target/TargetLife.h>
00034 #include <target/TargetState.h>
00035 
00036 ShotState::ShotState(ScorchedContext &context,
00037         PlayShots &playShots) :
00038         context_(context), playShots_(playShots),
00039         firstTime_(true)
00040 {
00041 }
00042 
00043 ShotState::~ShotState()
00044 {
00045 }
00046 
00047 void ShotState::setup()
00048 {
00049         // Set all player kills this turn to 0 (used for multikill)
00050         {
00051                 std::map<unsigned int, Tank *> &tanks = 
00052                         context_.getTankContainer().getPlayingTanks();
00053                 std::map<unsigned int, Tank *>::iterator itor;
00054                 for (itor = tanks.begin();
00055                         itor != tanks.end();
00056                         itor++)
00057                 {
00058                         Tank *tank = (*itor).second;
00059                         tank->getScore().setTurnKills(0);
00060                 }
00061         }
00062 
00063         // Reset the counts in the action controller
00064         context_.getActionController().setStopImmediately(true);
00065         context_.getActionController().resetTime();
00066         context_.getActionController().clear();
00067 
00068         if (context_.getOptionsGame().getActionSyncCheck())
00069         {
00070                 std::map<unsigned int, Target *> &targets =
00071                         context_.getTargetContainer().getTargets();
00072                 std::map<unsigned int, Target *>::iterator itor;
00073                 for (itor = targets.begin();
00074                         itor != targets.end();
00075                         itor++)
00076                 {
00077                         Target *target = itor->second;
00078                         if (target->getPlayerId() >= TargetID::MIN_TARGET_TRANSIENT_ID ||
00079                                 target->getTargetState().getMovement() ||
00080                                 !target->isTarget())
00081                         {
00082                                 context_.getActionController().addSyncCheck(
00083                                         S3D::formatStringBuffer("TargetDef : %u %s %i %i,%i,%i %i,%i,%i %s", 
00084                                                 target->getPlayerId(),
00085                                                 target->getCStrName().c_str(),
00086                                                 target->getLife().getLife().getInternal(),
00087                                                 target->getLife().getTargetPosition()[0].getInternal(),
00088                                                 target->getLife().getTargetPosition()[1].getInternal(),
00089                                                 target->getLife().getTargetPosition()[2].getInternal(),
00090                                                 target->getLife().getVelocity()[0].getInternal(),
00091                                                 target->getLife().getVelocity()[1].getInternal(),
00092                                                 target->getLife().getVelocity()[2].getInternal(),
00093                                                 target->getAlive()?"Alive":"Dead"));
00094                         }
00095                 }
00096         }
00097 
00098         // Add all shots that should be run at the start of the round
00099         // to the action controller
00100         playShots_.playShots(context_);
00101         context_.getActionController().addLastAction(new CheckResurrection());
00102 
00103         // Reset the amount of time taken
00104         firstTime_ = true;
00105 
00106         // Add all of the new events
00107         context_.getActionController().getEvents().initialize(context_);
00108 }
00109 
00110 bool ShotState::run(float frameTime)
00111 {
00112         if (!context_.getActionController().noReferencedActions() ||
00113                 firstTime_)
00114         {
00115                 // The action controller will now have shots to simulate
00116                 // We continue simulation until there are no actions left
00117                 // in the action controller
00118                 firstTime_ = false;
00119         }
00120         else
00121         {
00122                 // We have finished all shots
00123                 Logger::log(S3D::formatStringBuffer(
00124                         "Finished playing Shots %.2f seconds", 
00125                                 context_.getActionController().getActionTime().asFloat()));
00126                 context_.getActionController().getEvents().clear();
00127                 context_.getActionController().logProfiledActions();
00128                 context_.getActionController().setStopImmediately(false);
00129 
00130                 return true;
00131         }
00132         return false;
00133 }

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