ServerPlayingState.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/ServerPlayingState.h>
00022 #include <server/ServerShotHolder.h>
00023 #include <server/ServerState.h>
00024 #include <server/ScorchedServer.h>
00025 #include <server/ServerChannelManager.h>
00026 #include <server/TurnController.h>
00027 #include <server/ServerCommon.h>
00028 #include <tank/TankContainer.h>
00029 #include <tank/TankState.h>
00030 #include <tank/TankScore.h>
00031 #include <lua/LUAScriptHook.h>
00032 #include <common/OptionsScorched.h>
00033 #include <common/Logger.h>
00034 
00035 ServerPlayingState::ServerPlayingState() : 
00036         GameStateI("ServerPlayingState"),
00037         time_(0.0f)
00038 {
00039         ScorchedServer::instance()->getLUAScriptHook().addHookProvider("server_playing");
00040 }
00041 
00042 ServerPlayingState::~ServerPlayingState()
00043 {
00044 }
00045 
00046 void ServerPlayingState::enterState(const unsigned state)
00047 {
00048         // Set the wait timer to the current time
00049         time_ = 0.0f;
00050 
00051         // Notify scripts of a new game starting
00052         ScorchedServer::instance()->getLUAScriptHook().callHook("server_playing");
00053 }
00054 
00055 bool ServerPlayingState::acceptStateChange(const unsigned state, 
00056                 const unsigned nextState,
00057                 float frameTime)
00058 {
00059         // Check how long we are allowed to wait
00060         time_ += frameTime;
00061         int shotTime = 0;
00062         if (state == ServerState::ServerStateBuying)
00063         {
00064                 shotTime = ScorchedServer::instance()->getOptionsGame().getBuyingTime();
00065         }
00066         else
00067         {
00068                 shotTime = ScorchedServer::instance()->getOptionsGame().getShotTime();
00069         }
00070 
00071         // Check if the time to make the shots has expired
00072         if (shotTime > 0)
00073         {
00074                 if (time_ > shotTime + 5)
00075                 {
00076                         // For each alive tank that should have made a move
00077                         // Check if the tank has missed its go
00078                         // If so increment the missed counter
00079                         // Once missed counter exceeds it threshold then kick the player
00080                         std::list<unsigned int> &tanks = 
00081                                 TurnController::instance()->getPlayersThisTurn();
00082                         std::list<unsigned int>::iterator itor;
00083                         for (itor = tanks.begin();
00084                                 itor != tanks.end();
00085                                 itor++)
00086                         {
00087                                 Tank *tank = ScorchedServer::instance()->getTankContainer().getTankById(*itor);
00088                                 if (tank && tank->getState().getState() == TankState::sNormal)
00089                                 {
00090                                         if (!ServerShotHolder::instance()->haveShot(
00091                                                 tank->getPlayerId()))
00092                                         {
00093                                                 int movesMissed = tank->getScore().getMissedMoves() + 1;
00094                                                 tank->getScore().setMissedMoves(movesMissed);
00095 
00096 #ifdef S3D_SERVER
00097                                                 // If the allowed missed moves has been specified
00098                                                 if (ScorchedServer::instance()->getOptionsGame().getAllowedMissedMoves() > 0)
00099                                                 {
00100                                                         if (state == ServerState::ServerStateBuying)
00101                                                         {
00102                                                                 ServerChannelManager::instance()->sendText(
00103                                                                         ChannelText("info",
00104                                                                                 "PLAYER_MISSED_BUY",
00105                                                                                 "Player \"{0}\" failed to buy, allowed {1} more missed move(s)",
00106                                                                                 tank->getTargetName(),
00107                                                                                 ScorchedServer::instance()->getOptionsGame().getAllowedMissedMoves() - movesMissed),
00108                                                                         true);
00109                                                         }
00110                                                         else
00111                                                         {
00112                                                                 ServerChannelManager::instance()->sendText(
00113                                                                         ChannelText("info",
00114                                                                                 "PLAYER_MISSED_SHOOT",
00115                                                                                 "Player \"{0}\" failed to shoot, allowed {1} more missed move(s)",
00116                                                                                 tank->getTargetName(),
00117                                                                                 ScorchedServer::instance()->getOptionsGame().getAllowedMissedMoves() - movesMissed),
00118                                                                         true);
00119                                                         }
00120 
00121                                                         // And this player has exceeded them
00122                                                         if (movesMissed >= ScorchedServer::instance()->getOptionsGame().getAllowedMissedMoves())
00123                                                         {
00124                                                                 // Then kick this player
00125                                                                 ServerCommon::kickDestination(tank->getDestinationId());
00126                                                         }
00127                                                 }
00128 #endif
00129                                         }
00130                                 }
00131                         }
00132 
00133                         return true;
00134                 }
00135         }
00136 
00137         // Or we already have all shots
00138         if (ServerShotHolder::instance()->haveAllTurnShots())
00139         {
00140                 return true;
00141         }
00142 
00143         return false;
00144 }

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