ServerNextShotState.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/ServerNextShotState.h>
00022 #include <server/TurnController.h>
00023 #include <server/ScorchedServer.h>
00024 #include <server/ServerState.h>
00025 #include <server/ServerCommon.h>
00026 #include <server/ServerShotHolder.h>
00027 #include <server/ServerChannelManager.h>
00028 #include <tank/TankContainer.h>
00029 #include <tank/TankTeamScore.h>
00030 #include <tank/TankScore.h>
00031 #include <common/OptionsScorched.h>
00032 #include <common/OptionsTransient.h>
00033 #include <common/Logger.h>
00034 
00035 ServerNextShotState::ServerNextShotState() :
00036         GameStateI("ServerNextShotState")
00037 {
00038 }
00039 
00040 ServerNextShotState::~ServerNextShotState()
00041 {
00042 }
00043 
00044 bool ServerNextShotState::getRoundFinished()
00045 {
00046         // Check why this round has finished
00047         int teamWonGame = 
00048                 ScorchedServer::instance()->getContext().getTankTeamScore().getWonGame();
00049         if (teamWonGame > 0)
00050         {
00051                 // A team has won
00052                 return true;
00053         }
00054 
00055         std::map<unsigned int, Tank *> &tanks =
00056                 ScorchedServer::instance()->getTankContainer().getAllTanks();
00057         std::map<unsigned int, Tank *>::iterator itor;
00058         for (itor = tanks.begin();
00059                 itor != tanks.end();
00060                 itor++)
00061         {
00062                 Tank *tank = (*itor).second;
00063                 if (tank->getScore().getWonGame())
00064                 {
00065                         return true;
00066                 }
00067         }
00068         
00069         if (ScorchedServer::instance()->getOptionsGame().getTeams() > 1 &&
00070                 ScorchedServer::instance()->getTankContainer().teamCount() == 1)
00071         {
00072                 // Only one team left
00073                 return true;
00074         }
00075         else if (ScorchedServer::instance()->getTankContainer().aliveCount() < 2)
00076         {
00077                 // Only one person left
00078                 return true;
00079         }
00080         return false;
00081 }
00082 
00083 void ServerNextShotState::enterState(const unsigned state)
00084 {
00085         // Check if this round has finished
00086         if (getRoundFinished())
00087         {
00088                 // We have finished with this round
00089                 // check for all rounds completely finished
00090                 if (ScorchedServer::instance()->getOptionsTransient().getCurrentRoundNo() >=
00091                         ScorchedServer::instance()->getOptionsGame().getNoRounds())
00092                 {
00093                         // We have finished with all rounds restart
00094                         ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusStarting);
00095                 }
00096                 else
00097                 {
00098                         // We have finished with this round, go onto the next round
00099                         ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusNewGame);
00100                 }
00101         }
00102         else
00103         {
00104                 // Check if turn limit has been exceeded
00105                 if (ScorchedServer::instance()->getOptionsTransient().getCurrentGameNo() >
00106                         ScorchedServer::instance()->getOptionsGame().getNoMaxRoundTurns() &&
00107                         ScorchedServer::instance()->getOptionsGame().getNoMaxRoundTurns() > 0)
00108                 {
00109                         ServerChannelManager::instance()->sendText(
00110                                 ChannelText("info", 
00111                                         "ROUND_SKIP_TURN_LIMIT", 
00112                                         "Skipping round due to turn limit"),
00113                                 true);
00114 
00115                         // Clear any shots that may be waiting
00116                         ServerShotHolder::instance()->clearShots();
00117                         ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusNextTurn);
00118                 }
00119                 else
00120                 {
00121                         TurnController::instance()->nextShot();
00122                         if (TurnController::instance()->getPlayersThisShot().empty())
00123                         {
00124                                 // There are no players still to have a shot
00125                                 // The round must have finished
00126                                 ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusNextRound);
00127                         }
00128                         else
00129                         {
00130                                 // We have shots to make, lets make them
00131                                 ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusNextTurn);
00132                         }
00133                 }
00134         }
00135 }
00136 

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