ServerShotState.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 <server/ServerShotState.h>
00022 #include <server/ScorchedServer.h>
00023 #include <server/ServerShotHolder.h>
00024 #include <server/ServerCommon.h>
00025 #include <server/ServerChannelManager.h>
00026 #include <coms/ComsMessageSender.h>
00027 #include <common/OptionsScorched.h>
00028 #include <common/OptionsTransient.h>
00029 #include <tank/TankState.h>
00030 #include <engine/ActionController.h>
00031 
00032 ServerShotState::ServerShotState() :
00033         GameStateI("ServerShotState"),
00034         shotState_(ScorchedServer::instance()->getContext(), *ServerShotHolder::instance())
00035 {
00036 }
00037 
00038 ServerShotState::~ServerShotState()
00039 {
00040 }
00041 
00042 void ServerShotState::enterState(const unsigned state)
00043 {
00044         // Record the players that are actualy playing shots
00045         playing_.clear();
00046         std::map<unsigned int, Tank *>::iterator itor;
00047         std::map<unsigned int, Tank *> tanks = 
00048                 ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00049         std::set<unsigned int>::iterator findItor;
00050         for (itor = tanks.begin();
00051                 itor != tanks.end();
00052                 itor++)
00053         {
00054                 Tank *tank = (*itor).second;
00055                 if (tank->getState().getState() != TankState::sPending &&
00056                         tank->getState().getState() != TankState::sLoading &&
00057                         tank->getState().getState() != TankState::sInitializing &&
00058                         tank->getDestinationId() != 0)
00059                 {
00060                         unsigned int destination = tank->getDestinationId();
00061                         playing_.insert(destination);
00062                 }
00063         }
00064 
00065         // Check if all the players have skipped, and only
00066         // computer AIs are playing
00067         if (ServerShotHolder::instance()->allSkipped() &&
00068                 ScorchedServer::instance()->getOptionsGame().getTurnType().getValue() == OptionsGame::TurnSimultaneous)
00069         {
00070                 bool allAis = true;
00071                 bool someAlive = false;
00072                 std::map<unsigned int, Tank *> &tanks = 
00073                         ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00074                 std::map<unsigned int, Tank *>::iterator itor;
00075                 for (itor = tanks.begin();
00076                         itor != tanks.end();
00077                         itor++)
00078                 {
00079                         Tank *tank = (*itor).second;
00080                         if (tank->getState().getState() == TankState::sNormal)
00081                         {
00082                                 someAlive = true;
00083                                 if (tank->getDestinationId() != 0) 
00084                                 {
00085                                         allAis = false;
00086                                 }
00087                         }
00088                 }
00089 
00090                 if (allAis && someAlive)
00091                 {
00092                         ServerChannelManager::instance()->sendText(
00093                                 ChannelText("info", 
00094                                         "TURN_SKIPPING_STALEMATE", 
00095                                         "Skipping all turns due to stalemate..."),
00096                                 true);
00097                         ScorchedServer::instance()->getOptionsTransient().startNewRound();
00098                 }
00099         }
00100 
00101         // Send the player state to all players to ensure that the playing field
00102         // is consistent before the shots start
00103         // This should be done before the actual shots are fired or
00104         // any play is made also
00105         // Send all of the shots to the client
00106         unsigned int seed = rand();
00107         ComsPlayMovesMessage playMovesMessage;
00108         playMovesMessage.getSeed() = seed;
00109         ServerShotHolder::instance()->createMessage(playMovesMessage);
00110         ComsMessageSender::sendToAllPlayingClients(playMovesMessage);
00111 
00112         // Play the shots
00113         ScorchedServer::instance()->getTankContainer().setAllNotReady();
00114         ScorchedServer::instance()->getActionController().getRandom().seed(seed);
00115         shotState_.setup();
00116 }
00117 
00118 bool ServerShotState::acceptStateChange(const unsigned state, 
00119                 const unsigned nextState,
00120                 float frameTime)
00121 {
00122         return shotState_.run(frameTime);
00123 }

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