OpenTTD Source  1.11.0-RC1
genworld.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "company_func.h"
13 #include "genworld.h"
14 #include "gfxinit.h"
15 #include "window_func.h"
16 #include "network/network.h"
17 #include "heightmap.h"
18 #include "viewport_func.h"
19 #include "date_func.h"
20 #include "engine_func.h"
21 #include "water.h"
22 #include "video/video_driver.hpp"
23 #include "tilehighlight_func.h"
24 #include "saveload/saveload.h"
25 #include "void_map.h"
26 #include "town.h"
27 #include "newgrf.h"
28 #include "core/random_func.hpp"
29 #include "core/backup_type.hpp"
30 #include "progress.h"
31 #include "error.h"
32 #include "game/game.hpp"
33 #include "game/game_instance.hpp"
34 #include "string_func.h"
35 #include "thread.h"
36 
37 #include "safeguards.h"
38 
39 
40 void GenerateClearTile();
41 void GenerateIndustries();
42 void GenerateObjects();
43 void GenerateTrees();
44 
45 void StartupEconomy();
46 void StartupCompanies();
47 void StartupDisasters();
48 
49 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
50 
58 
61 
63 
67 static void CleanupGeneration()
68 {
69  _generating_world = false;
70 
71  SetMouseCursorBusy(false);
72  /* Show all vital windows again, because we have hidden them */
73  if (_game_mode != GM_MENU) ShowVitalWindows();
74  SetModalProgress(false);
75  _gw.proc = nullptr;
76  _gw.abortp = nullptr;
77 
81 }
82 
86 static void _GenerateWorld()
87 {
88  /* Make sure everything is done via OWNER_NONE. */
89  Backup<CompanyID> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
90 
91  try {
92  _generating_world = true;
93  if (_network_dedicated) DEBUG(net, 1, "Generating map, please wait...");
94  /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
98  SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
99 
101 
103  /* Must start economy early because of the costs. */
104  StartupEconomy();
105 
106  /* Don't generate landscape items when in the scenario editor. */
107  if (_gw.mode == GWM_EMPTY) {
109 
110  /* Make sure the tiles at the north border are void tiles if needed. */
112  for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
113  for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
114  }
115 
116  /* Make the map the height of the setting */
118 
119  ConvertGroundTilesIntoWaterTiles();
121  } else {
123  GenerateClearTile();
124 
125  /* Only generate towns, tree and industries in newgame mode. */
126  if (_game_mode != GM_EDITOR) {
129  return;
130  }
132  GenerateObjects();
133  GenerateTrees();
134  }
135  }
136 
137  /* These are probably pointless when inside the scenario editor. */
141  StartupEngines();
143  StartupDisasters();
144  _generating_world = false;
145 
146  /* No need to run the tile loop in the scenario editor. */
147  if (_gw.mode != GWM_EMPTY) {
148  uint i;
149 
151  for (i = 0; i < 0x500; i++) {
152  RunTileLoop();
153  _tick_counter++;
155  }
156 
157  if (_game_mode != GM_EDITOR) {
158  Game::StartNew();
159 
160  if (Game::GetInstance() != nullptr) {
162  _generating_world = true;
163  for (i = 0; i < 2500; i++) {
164  Game::GameLoop();
166  if (Game::GetInstance()->IsSleeping()) break;
167  }
168  _generating_world = false;
169  }
170  }
171  }
172 
174 
176  _cur_company.Trash();
178 
180  /* Call any callback */
181  if (_gw.proc != nullptr) _gw.proc();
183 
185 
186  ShowNewGRFError();
187 
188  if (_network_dedicated) DEBUG(net, 1, "Map generated, starting game");
189  DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
190 
191  if (_debug_desync_level > 0) {
192  char name[MAX_PATH];
193  seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
195  }
196  } catch (AbortGenerateWorldSignal&) {
198 
200  if (_cur_company.IsValid()) _cur_company.Restore();
201 
202  if (_network_dedicated) {
203  /* Exit the game to prevent a return to main menu. */
204  DEBUG(net, 0, "Generating map failed, aborting");
205  _exit_game = true;
206  } else {
207  SwitchToMode(_switch_mode);
208  }
209  }
210 }
211 
218 {
219  _gw.proc = proc;
220 }
221 
228 {
229  _gw.abortp = proc;
230 }
231 
236 {
237  _gw.abort = true;
238 }
239 
245 {
246  return _gw.abort || _exit_game;
247 }
248 
253 {
254  /* Clean up - in SE create an empty map, otherwise, go to intro menu */
255  _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
256 
257  if (_gw.abortp != nullptr) _gw.abortp();
258 
259  throw AbortGenerateWorldSignal();
260 }
261 
269 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
270 {
271  if (HasModalProgress()) return;
272  _gw.mode = mode;
273  _gw.size_x = size_x;
274  _gw.size_y = size_y;
275  SetModalProgress(true);
276  _gw.abort = false;
277  _gw.abortp = nullptr;
279 
280  /* This disables some commands and stuff */
282 
283  InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
285 
286  /* Load the right landscape stuff, and the NewGRFs! */
287  GfxLoadSprites();
289 
290  /* Re-init the windowing system */
292 
293  /* Create toolbars */
295  SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
296 
300 
302 
303  /* Centre the view on the map */
304  if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) {
305  ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
306  }
307 
308  _GenerateWorld();
309 }
GenWorldInfo::size_y
uint size_y
Y-size of the map.
Definition: genworld.h:58
game.hpp
LoadStringWidthTable
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1276
ShowFirstError
void ShowFirstError()
Show the first error of the queue.
Definition: error_gui.cpp:337
GWP_GAME_INIT
@ GWP_GAME_INIT
Initialize the game.
Definition: genworld.h:73
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1738
ShowGenerateWorldProgress
void ShowGenerateWorldProgress()
Show the window where a user can follow the process of the map generation.
Definition: genworld_gui.cpp:1304
HideVitalWindows
void HideVitalWindows()
Delete all always on-top windows to get an empty screen.
Definition: window.cpp:3449
GameCreationSettings::generation_seed
uint32 generation_seed
noise seed for world generation
Definition: settings_type.h:292
GenerateLandscape
void GenerateLandscape(byte mode)
Definition: landscape.cpp:1297
game_instance.hpp
GWP_RUNTILELOOP
@ GWP_RUNTILELOOP
Runs the tile loop 1280 times to make snow etc.
Definition: genworld.h:74
ScrollMainWindowToTile
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2443
_GenerateWorld
static void _GenerateWorld()
The internal, real, generate function.
Definition: genworld.cpp:86
GenWorldInfo::abort
bool abort
Whether to abort the thread ASAP.
Definition: genworld.h:54
water.h
GenWorldInfo::lc
CompanyID lc
The local_company before generating.
Definition: genworld.h:56
_gw
GenWorldInfo _gw
Please only use this variable in genworld.h and genworld.cpp and nowhere else.
Definition: genworld.cpp:57
AbortGeneratingWorld
void AbortGeneratingWorld()
Initializes the abortion process.
Definition: genworld.cpp:235
EconomySettings::town_layout
TownLayout town_layout
select town layout,
Definition: settings_type.h:499
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
GenWorldInfo::abortp
GWAbortProc * abortp
Proc that is called when aborting (can be nullptr)
Definition: genworld.h:60
SaveOrLoad
SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
Definition: saveload.cpp:2764
SM_EDITOR
@ SM_EDITOR
Switch to scenario editor.
Definition: openttd.h:29
AbortGenerateWorldSignal
Definition: genworld.cpp:62
GenWorldInfo::mode
GenWorldMode mode
What mode are we making a world in.
Definition: genworld.h:55
GenerateWorldSetCallback
void GenerateWorldSetCallback(GWDoneProc *proc)
Set here the function, if any, that you want to be called when landscape generation is done.
Definition: genworld.cpp:217
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1133
void_map.h
SetLocalCompany
void SetLocalCompany(CompanyID new_company)
Sets the local company and updates the settings that are set on a per-company basis to reflect the co...
Definition: company_cmd.cpp:102
DFT_GAME_FILE
@ DFT_GAME_FILE
Save game or scenario file.
Definition: fileio_type.h:31
saveload.h
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
GWAbortProc
void GWAbortProc()
Called when genworld is aborted.
Definition: genworld.h:50
DeleteWindowByClass
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1178
town.h
StartupEngines
void StartupEngines()
Start/initialise all our engines.
Definition: engine.cpp:693
GENERATE_NEW_SEED
static const uint32 GENERATE_NEW_SEED
Create a new random seed.
Definition: genworld.h:24
genworld.h
IncreaseGeneratingWorldProgress
void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
Increases the current stage of the world generation with one.
Definition: genworld_gui.cpp:1380
ShowNewGRFError
void ShowNewGRFError()
Show the first NewGRF error we can find.
Definition: newgrf_gui.cpp:44
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:560
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
GenWorldInfo::proc
GWDoneProc * proc
Proc that is called when done (can be nullptr)
Definition: genworld.h:59
WC_MODAL_PROGRESS
@ WC_MODAL_PROGRESS
Progress report of landscape generation; Window numbers:
Definition: window_type.h:456
heightmap.h
ShowVitalWindows
void ShowVitalWindows()
Show the vital in-game windows.
Definition: main_gui.cpp:547
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:50
tilehighlight_func.h
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
GenerateWorldSetAbortCallback
void GenerateWorldSetAbortCallback(GWAbortProc *proc)
Set here the function, if any, that you want to be called when landscape generation is aborted.
Definition: genworld.cpp:227
MakeVoid
static void MakeVoid(TileIndex t)
Make a nice void tile ;)
Definition: void_map.h:19
GenerateTrees
void GenerateTrees()
Place new trees.
Definition: tree_cmd.cpp:345
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
PSM_ENTER_GAMELOOP
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
Definition: newgrf_storage.h:20
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition: settings_type.h:569
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
_tick_counter
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:30
safeguards.h
GWP_GAME_START
@ GWP_GAME_START
Really prepare to start the game.
Definition: genworld.h:76
GWP_OBJECT
@ GWP_OBJECT
Generate objects (radio tower, light houses)
Definition: genworld.h:71
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:329
GWP_MAP_INIT
@ GWP_MAP_INIT
Initialize/allocate the map, start economy.
Definition: genworld.h:65
GameCreationSettings::se_flat_world_height
byte se_flat_world_height
land height a flat world gets in SE
Definition: settings_type.h:303
HT_NONE
@ HT_NONE
default
Definition: tilehighlight_type.h:20
gfxinit.h
error.h
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:55
date_func.h
stdafx.h
landscape.h
viewport_func.h
FlatEmptyWorld
void FlatEmptyWorld(byte tile_height)
Make an empty world where all tiles are of height 'tile_height'.
Definition: heightmap.cpp:510
Backup::IsValid
bool IsValid() const
Checks whether the variable was already restored.
Definition: backup_type.hpp:63
GenWorldInfo
Properties of current genworld process.
Definition: genworld.h:53
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:60
string_func.h
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
Game::GameLoop
static void GameLoop()
Called every game-tick to let Game do something.
Definition: game_core.cpp:31
Backup::Trash
void Trash()
Trash the backup.
Definition: backup_type.hpp:103
GenerateTowns
bool GenerateTowns(TownLayout layout)
This function will generate a certain amount of towns, with a certain layout It can be called from th...
Definition: town_cmd.cpp:2201
PrepareGenerateWorldProgress
void PrepareGenerateWorldProgress()
Initializes the progress counters to the starting point.
Definition: genworld_gui.cpp:1292
HandleGeneratingWorldAbortion
void HandleGeneratingWorldAbortion()
Really handle the abortion, i.e.
Definition: genworld.cpp:252
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
Backup::Restore
void Restore()
Restore the variable.
Definition: backup_type.hpp:112
UnshowCriticalError
void UnshowCriticalError()
Unshow the critical error.
Definition: error_gui.cpp:351
DeleteAllNonVitalWindows
void DeleteAllNonVitalWindows()
It is possible that a stickied window gets to a position where the 'close' button is outside the gami...
Definition: window.cpp:3396
video_driver.hpp
COMPANY_SPECTATOR
@ COMPANY_SPECTATOR
The client is spectating.
Definition: company_type.h:35
SetGeneratingWorldProgress
void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
Set the total of a stage of the world generation.
Definition: genworld_gui.cpp:1366
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
GWM_EMPTY
@ GWM_EMPTY
Generate an empty map (sea-level)
Definition: genworld.h:29
newgrf.h
progress.h
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:44
GfxLoadSprites
void GfxLoadSprites()
Initialise and load all the sprites.
Definition: gfxinit.cpp:330
company_func.h
SM_MENU
@ SM_MENU
Switch to game intro menu.
Definition: openttd.h:31
GenerateIndustries
void GenerateIndustries()
This function will create random industries during game creation.
Definition: industry_cmd.cpp:2303
network.h
window_func.h
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1597
StartupCompanies
void StartupCompanies()
Start the next competitor now.
Definition: company_cmd.cpp:589
random_func.hpp
GenWorldInfo::size_x
uint size_x
X-size of the map.
Definition: genworld.h:57
IsGeneratingWorldAborted
bool IsGeneratingWorldAborted()
Is the generation being aborted?
Definition: genworld.cpp:244
GenerateWorld
void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
Generate a world.
Definition: genworld.cpp:269
PSM_LEAVE_GAMELOOP
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
Definition: newgrf_storage.h:21
BasePersistentStorageArray::SwitchMode
static void SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode=false)
Clear temporary changes made since the last call to SwitchMode, and set whether subsequent changes sh...
Definition: newgrf_storage.cpp:55
CleanupGeneration
static void CleanupGeneration()
Generation is done; show windows again and delete the progress window.
Definition: genworld.cpp:67
ResetWindowSystem
void ResetWindowSystem()
Reset the windowing system, by means of shutting it down followed by re-initialization.
Definition: window.cpp:1938
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:561
SetObjectToPlace
void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
Change the cursor and mouse click/drag handling to a mode for performing special operations like tile...
Definition: viewport.cpp:3373
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:17
Randomizer::SetSeed
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:55
GenWorldMode
GenWorldMode
Modes for GenerateWorld.
Definition: genworld.h:27
GWDoneProc
void GWDoneProc()
Procedure called when the genworld process finishes.
Definition: genworld.h:49
SetModalProgress
void SetModalProgress(bool state)
Set the modal progress state.
Definition: progress.cpp:22
Game::GetInstance
static class GameInstance * GetInstance()
Get the current active instance.
Definition: game.hpp:111
thread.h
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition: viewport.cpp:3421
Game::StartNew
static void StartNew()
Start up a new GameScript.
Definition: game_core.cpp:72
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:81
SetupColoursAndInitialWindow
void SetupColoursAndInitialWindow()
Initialise the default colours (remaps and the likes), and load the main windows.
Definition: main_gui.cpp:519
engine_func.h
GWP_RUNSCRIPT
@ GWP_RUNSCRIPT
Runs the game script at most 2500 times, or when ever the script sleeps.
Definition: genworld.h:75
RunTileLoop
void RunTileLoop()
Gradually iterate over all tiles on the map, calling their TileLoopProcs once every 256 ticks.
Definition: landscape.cpp:801
backup_type.hpp