ServerTooFewPlayersStimulus.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/ServerTooFewPlayersStimulus.h>
00022 #include <server/ServerNewGameState.h>
00023 #include <server/ScorchedServer.h>
00024 #include <server/ServerCommon.h>
00025 #include <tank/TankContainer.h>
00026 #include <tank/TankState.h>
00027 #include <common/OptionsScorched.h>
00028 #include <common/Logger.h>
00029 
00030 ServerTooFewPlayersStimulus *ServerTooFewPlayersStimulus::instance_ = 0;
00031 
00032 ServerTooFewPlayersStimulus *ServerTooFewPlayersStimulus::instance()
00033 {
00034         if (!instance_)
00035         {
00036                 instance_ = new ServerTooFewPlayersStimulus;
00037         }
00038         return instance_;
00039 }
00040 
00041 ServerTooFewPlayersStimulus::ServerTooFewPlayersStimulus()
00042 {
00043 
00044 }
00045 
00046 ServerTooFewPlayersStimulus::~ServerTooFewPlayersStimulus()
00047 {
00048 
00049 }
00050 
00051 bool ServerTooFewPlayersStimulus::acceptStateChange(const unsigned state, 
00052                                                                                                         const unsigned nextState,
00053                                                                                                         float frameTime)
00054 {
00055         // Check if we need to add any new bots
00056         ServerNewGameState::checkBots(false);
00057 
00058         // Make sure we have enough players to play a game
00059         if (ScorchedServer::instance()->getTankContainer().getNoOfNonSpectatorTanks() <
00060                 ScorchedServer::instance()->getOptionsGame().getNoMinPlayers())
00061         {
00062                 checkExit();
00063                 return true;
00064         }
00065         
00066         // Check we have enough team players
00067         if (ScorchedServer::instance()->getOptionsGame().getTeams() > 1 &&
00068                 ScorchedServer::instance()->getOptionsGame().getTeamBallance() !=
00069                         OptionsGame::TeamBallanceAuto &&
00070                 ScorchedServer::instance()->getOptionsGame().getTeamBallance() !=
00071                         OptionsGame::TeamBallanceAutoByScore &&
00072                 ScorchedServer::instance()->getOptionsGame().getTeamBallance() !=
00073                         OptionsGame::TeamBallanceAutoByBots)
00074         {
00075                 // If it is auto ballanced, then if there are at least two players
00076                 // then we are ok.  And if there are not two players then
00077                 // the first check will catch it.
00078                 
00079                 // Move players between teams
00080                 ServerNewGameState::checkTeams();
00081                 
00082                 // Check there is at least one player in each team
00083                 int teamCount[4];
00084                 for (int i=0; i<ScorchedServer::instance()->getOptionsGame().getTeams();i++)
00085                 {
00086                         teamCount[i] = 0;
00087                 }
00088                 std::map<unsigned int, Tank *> &playingTanks = 
00089                         ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00090                 std::map<unsigned int, Tank *>::iterator mainitor;
00091                 for (mainitor = playingTanks.begin();
00092                          mainitor != playingTanks.end();
00093                          mainitor++)
00094                 {
00095                         Tank *current = (*mainitor).second;
00096                         if (!current->getState().getSpectator())
00097                         {
00098                                 if (current->getTeam() > 0)
00099                                 {
00100                                         teamCount[current->getTeam() - 1]++;
00101                                 }
00102                         }
00103                 }
00104                 for (int i=0; i<ScorchedServer::instance()->getOptionsGame().getTeams();i++)
00105                 {
00106                         if (teamCount[i] == 0)
00107                         {
00108                                 checkExit();
00109                                 return true;
00110                         }
00111                 }
00112         }
00113         
00114         return false;
00115 }
00116 
00117 void ServerTooFewPlayersStimulus::checkExit()
00118 {
00119         if (ServerCommon::getExitEmpty())
00120         {
00121                 Logger::log("Exit server when empty");
00122                 exit(0);
00123         }
00124 }

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