1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <server/ServerStateNewGame.h>
22 #include <server/ScorchedServer.h>
23 #include <server/ServerState.h>
24 #include <server/ServerCommon.h>
25 #include <server/ServerSimulator.h>
26 #include <server/ServerConsoleProgressCounter.h>
27 #include <server/ServerChannelManager.h>
28 #include <server/ServerLoadLevel.h>
29 #include <weapons/EconomyStore.h>
30 #include <target/TargetContainer.h>
31 #include <tank/TankState.h>
32 #include <landscapedef/LandscapeDefinitions.h>
33 #include <landscapemap/LandscapeMaps.h>
34 #include <common/OptionsTransient.h>
35 #include <common/StatsLogger.h>
36
ServerStateNewGame()37 ServerStateNewGame::ServerStateNewGame()
38 {
39 }
40
~ServerStateNewGame()41 ServerStateNewGame::~ServerStateNewGame()
42 {
43 }
44
newGame()45 void ServerStateNewGame::newGame()
46 {
47 newGameState();
48
49 // Make sure any remaining actions have been processed before starting a new game
50 ScorchedServer::instance()->getServerSimulator().processRemaining();
51
52 // Make sure tanks are in correct state
53 std::list<Tank *> playingTanks;
54 std::set<unsigned int> loadingDestinations;
55 std::map<unsigned int, Tank *> &tanks =
56 ScorchedServer::instance()->getTargetContainer().getTanks();
57 std::map<unsigned int, Tank *>::iterator tankItor;
58 for (tankItor = tanks.begin();
59 tankItor != tanks.end();
60 ++tankItor)
61 {
62 Tank *tank = tankItor->second;
63 if (tank->getState().getState() != TankState::sLoading)
64 {
65 tank->getState().setState(TankState::sLoading);
66 if (loadingDestinations.find(tank->getDestinationId()) == loadingDestinations.end())
67 {
68 loadingDestinations.insert(tank->getDestinationId());
69 playingTanks.push_back(tank);
70 }
71 }
72 }
73
74 // Inform the stats logger
75 StatsLogger::instance()->gameStart(playingTanks);
76
77 // Store this as the current level
78 // Do after setting the state so the state of the tanks is consistent when
79 // the level is saved
80 ScorchedServer::instance()->getServerSimulator().newLevel();
81
82 // Tell all destinations to load
83 std::set<unsigned int>::iterator destItor;
84 for (destItor = loadingDestinations.begin();
85 destItor != loadingDestinations.end();
86 ++destItor)
87 {
88 ServerLoadLevel::destinationLoadLevel(*destItor);
89 }
90 }
91
newGameState()92 void ServerStateNewGame::newGameState()
93 {
94 // Make sure options are up to date
95 if (ScorchedServer::instance()->getOptionsGame().commitChanges())
96 {
97 ScorchedServer::instance()->getServerChannelManager().sendText(
98 ChannelText("info",
99 "GAME_OPTIONS_CHANGED",
100 "Game options have been changed!"),
101 true);
102 }
103
104 // Make any enconomic changes
105 ScorchedServer::instance()->getEconomyStore().getEconomy()->calculatePrices();
106 ScorchedServer::instance()->getEconomyStore().getEconomy()->savePrices();
107
108 // Get a landscape definition to use
109 ServerCommon::serverLog("Generating landscape");
110 LandscapeDefinition defn;
111 if (ScorchedServer::instance()->getLandscapeMaps().getDefinitions().
112 getDefinition().getDefinitionNumber() == 0)
113 {
114 defn = ScorchedServer::instance()->getLandscapes().getBlankLandscapeDefn();
115 }
116 else
117 {
118 defn = ScorchedServer::instance()->getLandscapes().getRandomLandscapeDefn(
119 ScorchedServer::instance()->getContext().getOptionsGame(),
120 ScorchedServer::instance()->getContext().getTargetContainer());
121 }
122
123 // Load the per level options
124 ScorchedServer::instance()->getOptionsGame().updateLevelOptions(
125 ScorchedServer::instance()->getContext(), defn);
126
127 // Set all options (wind etc..)
128 if (ScorchedServer::instance()->getServerState().getState() == ServerState::ServerStartupState)
129 {
130 ScorchedServer::instance()->getContext().getOptionsTransient().reset();
131 }
132 ScorchedServer::instance()->getContext().getOptionsTransient().newGame();
133
134 // Generate the new level
135 ProgressCounter *progressCounter = 0;
136 #ifdef S3D_SERVER
137 progressCounter = ServerConsoleProgressCounter::instance()->getProgressCounter();
138 #endif
139
140 ScorchedServer::instance()->getLandscapeMaps().generateMaps(
141 ScorchedServer::instance()->getContext(), defn,
142 progressCounter);
143 ServerCommon::serverLog("Finished generating landscape");
144 }
145