1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file genworld.cpp Functions to generate a map. */
#include "stdafx.h"
#include "landscape.h"
#include "company_func.h"
#include "genworld.h"
#include "gfxinit.h"
#include "window_func.h"
#include "network/network.h"
#include "heightmap.h"
#include "viewport_func.h"
#include "date_func.h"
#include "engine_func.h"
#include "water.h"
#include "video/video_driver.hpp"
#include "tilehighlight_func.h"
#include "saveload/saveload.h"
#include "void_map.h"
#include "town.h"
#include "newgrf.h"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "progress.h"
#include "error.h"
#include "game/game.hpp"
#include "game/game_instance.hpp"
#include "string_func.h"
#include "thread.h"
#include "tgp.h"
#include "safeguards.h"
void GenerateClearTile();
void GenerateIndustries();
void GenerateObjects();
void GenerateTrees();
void StartupEconomy();
void StartupCompanies();
void StartupDisasters();
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
/**
* Please only use this variable in genworld.h and genworld.cpp and
* nowhere else. For speed improvements we need it to be global, but
* in no way the meaning of it is to use it anywhere else besides
* in the genworld.h and genworld.cpp!
*/
GenWorldInfo _gw;
/** Whether we are generating the map or not. */
bool _generating_world;
class AbortGenerateWorldSignal { };
/**
* Generation is done; show windows again and delete the progress window.
*/
static void CleanupGeneration()
{
_generating_world = false;
SetMouseCursorBusy(false);
/* Show all vital windows again, because we have hidden them */
if (_game_mode != GM_MENU) ShowVitalWindows();
SetModalProgress(false);
_gw.proc = nullptr;
_gw.abortp = nullptr;
DeleteWindowByClass(WC_MODAL_PROGRESS);
ShowFirstError();
MarkWholeScreenDirty();
}
/**
* The internal, real, generate function.
*/
static void _GenerateWorld()
{
/* Make sure everything is done via OWNER_NONE. */
Backup<CompanyID> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
try {
_generating_world = true;
if (_network_dedicated) DEBUG(net, 3, "Generating map, please wait...");
/* Set the Random() seed to generation_seed so we produce the same map with the same seed */
if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
_random.SetSeed(_settings_game.game_creation.generation_seed);
SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP);
IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
/* Must start economy early because of the costs. */
StartupEconomy();
/* Don't generate landscape items when in the scenario editor. */
if (_gw.mode == GWM_EMPTY) {
SetGeneratingWorldProgress(GWP_OBJECT, 1);
/* Make sure the tiles at the north border are void tiles if needed. */
if (_settings_game.construction.freeform_edges) {
for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
}
/* Make the map the height of the setting */
if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
ConvertGroundTilesIntoWaterTiles();
IncreaseGeneratingWorldProgress(GWP_OBJECT);
_settings_game.game_creation.snow_line_height = DEF_SNOWLINE_HEIGHT;
} else {
GenerateLandscape(_gw.mode);
GenerateClearTile();
/* Only generate towns, tree and industries in newgame mode. */
if (_game_mode != GM_EDITOR) {
if (!GenerateTowns(_settings_game.economy.town_layout)) {
HandleGeneratingWorldAbortion();
return;
}
GenerateIndustries();
GenerateObjects();
GenerateTrees();
}
}
/* These are probably pointless when inside the scenario editor. */
SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
StartupCompanies();
IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
StartupEngines();
IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
StartupDisasters();
_generating_world = false;
/* No need to run the tile loop in the scenario editor. */
if (_gw.mode != GWM_EMPTY) {
uint i;
SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
for (i = 0; i < 0x500; i++) {
RunTileLoop();
_tick_counter++;
IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
}
if (_game_mode != GM_EDITOR) {
Game::StartNew();
if (Game::GetInstance() != nullptr) {
SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
_generating_world = true;
for (i = 0; i < 2500; i++) {
Game::GameLoop();
IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
if (Game::GetInstance()->IsSleeping()) break;
}
_generating_world = false;
}
}
}
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
ResetObjectToPlace();
_cur_company.Trash();
_current_company = _local_company = _gw.lc;
SetGeneratingWorldProgress(GWP_GAME_START, 1);
/* Call any callback */
if (_gw.proc != nullptr) _gw.proc();
IncreaseGeneratingWorldProgress(GWP_GAME_START);
CleanupGeneration();
ShowNewGRFError();
if (_network_dedicated) DEBUG(net, 3, "Map generated, starting game");
DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
if (_debug_desync_level > 0) {
char name[MAX_PATH];
seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
SaveOrLoad(name, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false);
}
} catch (AbortGenerateWorldSignal&) {
CleanupGeneration();
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true);
if (_cur_company.IsValid()) _cur_company.Restore();
if (_network_dedicated) {
/* Exit the game to prevent a return to main menu. */
DEBUG(net, 0, "Generating map failed; closing server");
_exit_game = true;
} else {
SwitchToMode(_switch_mode);
}
}
}
/**
* Set here the function, if any, that you want to be called when landscape
* generation is done.
* @param proc callback procedure
*/
void GenerateWorldSetCallback(GWDoneProc *proc)
{
_gw.proc = proc;
}
/**
* Set here the function, if any, that you want to be called when landscape
* generation is aborted.
* @param proc callback procedure
*/
void GenerateWorldSetAbortCallback(GWAbortProc *proc)
{
_gw.abortp = proc;
}
/**
* Initializes the abortion process
*/
void AbortGeneratingWorld()
{
_gw.abort = true;
}
/**
* Is the generation being aborted?
* @return the 'aborted' status
*/
bool IsGeneratingWorldAborted()
{
return _gw.abort || _exit_game;
}
/**
* Really handle the abortion, i.e. clean up some of the mess
*/
void HandleGeneratingWorldAbortion()
{
/* Clean up - in SE create an empty map, otherwise, go to intro menu */
_switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
if (_gw.abortp != nullptr) _gw.abortp();
throw AbortGenerateWorldSignal();
}
/**
* Generate a world.
* @param mode The mode of world generation (see GenWorldMode).
* @param size_x The X-size of the map.
* @param size_y The Y-size of the map.
* @param reset_settings Whether to reset the game configuration (used for restart)
*/
void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
{
if (HasModalProgress()) return;
_gw.mode = mode;
_gw.size_x = size_x;
_gw.size_y = size_y;
SetModalProgress(true);
_gw.abort = false;
_gw.abortp = nullptr;
_gw.lc = _local_company;
/* This disables some commands and stuff */
SetLocalCompany(COMPANY_SPECTATOR);
InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
PrepareGenerateWorldProgress();
if (_settings_game.construction.map_height_limit == 0) {
uint estimated_height = 0;
if (_gw.mode == GWM_EMPTY && _game_mode != GM_MENU) {
estimated_height = _settings_game.game_creation.se_flat_world_height;
} else if (_gw.mode == GWM_HEIGHTMAP) {
estimated_height = _settings_game.game_creation.heightmap_height;
} else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) {
estimated_height = GetEstimationTGPMapHeight();
} else {
estimated_height = 0;
}
_settings_game.construction.map_height_limit = std::max(MAP_HEIGHT_LIMIT_AUTO_MINIMUM, std::min(MAX_MAP_HEIGHT_LIMIT, estimated_height + MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM));
}
/* Load the right landscape stuff, and the NewGRFs! */
GfxLoadSprites();
LoadStringWidthTable();
/* Re-init the windowing system */
ResetWindowSystem();
/* Create toolbars */
SetupColoursAndInitialWindow();
SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
UnshowCriticalError();
DeleteAllNonVitalWindows();
HideVitalWindows();
ShowGenerateWorldProgress();
/* Centre the view on the map */
if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) {
ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
}
_GenerateWorld();
}
|