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/ServerNextTurnState.h> 00022 #include <server/ServerState.h> 00023 #include <server/ScorchedServer.h> 00024 #include <server/TurnController.h> 00025 #include <server/ServerChannelManager.h> 00026 #include <server/ServerCommon.h> 00027 #include <tank/TankContainer.h> 00028 #include <tank/TankState.h> 00029 #include <tank/TankAccessories.h> 00030 #include <tankai/TankAI.h> 00031 #include <landscapemap/LandscapeMaps.h> 00032 #include <coms/ComsStartGameMessage.h> 00033 #include <coms/ComsTimerStartMessage.h> 00034 #include <coms/ComsMessageSender.h> 00035 #include <coms/ComsPlayerStatusMessage.h> 00036 #include <common/Logger.h> 00037 #include <common/OptionsTransient.h> 00038 #include <common/OptionsScorched.h> 00039 00040 ServerNextTurnState::ServerNextTurnState() : 00041 GameStateI("ServerNextTurnState") 00042 { 00043 } 00044 00045 ServerNextTurnState::~ServerNextTurnState() 00046 { 00047 } 00048 00049 void ServerNextTurnState::enterState(const unsigned state) 00050 { 00051 TurnController::instance()->nextTurn(); 00052 if (TurnController::instance()->getPlayersThisTurn().empty()) 00053 { 00054 // Tell each client to end the shot timer 00055 ComsTimerStartMessage timerMessage(0); 00056 ComsMessageSender::sendToAllPlayingClients(timerMessage); 00057 00058 // No more turns left, play the shot 00059 ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusShot); 00060 } 00061 else 00062 { 00063 bool weaponBuy = 00064 (ScorchedServer::instance()->getOptionsTransient().getCurrentGameNo() == 0); 00065 00066 // Tell each client to start the shot timer 00067 int time = ScorchedServer::instance()->getOptionsGame().getShotTime(); 00068 if (weaponBuy) time = ScorchedServer::instance()->getOptionsGame().getBuyingTime(); 00069 ComsTimerStartMessage timerMessage(time); 00070 ComsMessageSender::sendToAllPlayingClients(timerMessage); 00071 00072 // Tell the client who we are currently waiting on 00073 ComsPlayerStatusMessage statusMessage; 00074 statusMessage.getWaitingPlayers() = TurnController::instance()->getPlayersThisTurn(); 00075 ComsMessageSender::sendToAllPlayingClients(statusMessage, NetInterfaceFlags::fAsync); 00076 00077 // Tell the players to play the turn 00078 std::list<unsigned int>::iterator itor; 00079 for (itor = TurnController::instance()->getPlayersThisTurn().begin(); 00080 itor != TurnController::instance()->getPlayersThisTurn().end(); 00081 itor++) 00082 { 00083 unsigned int playerId = (*itor); 00084 00085 Tank *tank = 00086 ScorchedServer::instance()->getTankContainer().getTankById(playerId); 00087 DIALOG_ASSERT(tank); 00088 DIALOG_ASSERT(tank->getState().getState() == TankState::sNormal); 00089 00090 if (tank->getDestinationId() == 0) // This is a local tank 00091 { 00092 if (weaponBuy) 00093 { 00094 // Tell the computer ai to buy weapons 00095 tank->getTankAI()->buyAccessories(); 00096 00097 // Tell the computer ai to use its autodefense (if it has one) 00098 if (tank->getAccessories().getAutoDefense().haveDefense()) 00099 { 00100 tank->getTankAI()->autoDefense(); 00101 } 00102 } 00103 else 00104 { 00105 // Tell the computer ai to move 00106 tank->getTankAI()->playMove(); 00107 } 00108 } 00109 else 00110 { 00111 ServerChannelManager::instance()->sendText( 00112 ChannelText("banner", 00113 "TANK'S MOVE", 00114 "{0}'s Move", 00115 tank->getTargetName()), 00116 false); 00117 00118 // Tell the clients to start the game 00119 ComsStartGameMessage startMessage(tank->getPlayerId(), weaponBuy); 00120 ComsMessageSender::sendToSingleClient(startMessage, tank->getDestinationId()); 00121 } 00122 } 00123 00124 if (weaponBuy) 00125 { 00126 ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusBuying); 00127 } 00128 else 00129 { 00130 ScorchedServer::instance()->getGameState().stimulate(ServerState::ServerStimulusPlaying); 00131 } 00132 } 00133 }
1.5.3