~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/genworld.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: genworld.cpp 12004 2008-01-29 00:27:25Z rubidium $ */
 
1
/* $Id: genworld.cpp 15689 2009-03-12 15:14:22Z rubidium $ */
2
2
 
3
 
/** @file genworld.cpp */
 
3
/** @file genworld.cpp Functions to generate a map. */
4
4
 
5
5
#include "stdafx.h"
6
6
#include "openttd.h"
7
7
#include "landscape.h"
8
 
#include "player_func.h"
 
8
#include "company_func.h"
9
9
#include "variables.h"
10
10
#include "thread.h"
11
11
#include "command_func.h"
13
13
#include "gfxinit.h"
14
14
#include "window_func.h"
15
15
#include "network/network.h"
16
 
#include "debug.h"
17
 
#include "settings_func.h"
18
16
#include "heightmap.h"
19
17
#include "viewport_func.h"
20
18
#include "gfx_func.h"
21
 
#include "map_func.h"
22
19
#include "date_func.h"
23
20
#include "core/random_func.hpp"
24
 
#include "engine.h"
25
 
#include "settings_type.h"
 
21
#include "engine_func.h"
26
22
#include "newgrf_storage.h"
27
23
#include "water.h"
 
24
#include "blitter/factory.hpp"
 
25
#include "tilehighlight_func.h"
 
26
#include "saveload/saveload.h"
 
27
#include "void_map.h"
 
28
#include "settings_type.h"
 
29
#include "town.h"
28
30
 
29
31
#include "table/sprites.h"
30
32
 
31
33
void GenerateClearTile();
32
34
void GenerateIndustries();
33
35
void GenerateUnmovables();
34
 
bool GenerateTowns();
35
36
void GenerateTrees();
36
37
 
37
38
void StartupEconomy();
38
 
void StartupPlayers();
 
39
void StartupCompanies();
39
40
void StartupDisasters();
40
41
 
41
 
void InitializeGame(int mode, uint size_x, uint size_y);
 
42
void InitializeGame(uint size_x, uint size_y, bool reset_date);
42
43
 
43
44
/* Please only use this variable in genworld.h and genworld.c and
44
45
 *  nowhere else. For speed improvements we need it to be global, but
82
83
}
83
84
 
84
85
/**
 
86
 * Clean up the 'mess' of generation. That is show windows again, reset
 
87
 * thread variables and delete the progress window.
 
88
 */
 
89
static void CleanupGeneration()
 
90
{
 
91
        _generating_world = false;
 
92
 
 
93
        if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
 
94
        /* Show all vital windows again, because we have hidden them */
 
95
        if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
 
96
        _gw.active   = false;
 
97
        _gw.proc     = NULL;
 
98
        _gw.abortp   = NULL;
 
99
        _gw.threaded = false;
 
100
 
 
101
        DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
 
102
        MarkWholeScreenDirty();
 
103
}
 
104
 
 
105
/**
85
106
 * The internal, real, generate function.
86
107
 */
87
 
static void *_GenerateWorld(void *arg)
 
108
static void _GenerateWorld(void *arg)
88
109
{
89
 
        _generating_world = true;
90
 
        if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
91
 
        /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
92
 
        if (_patches.generation_seed == GENERATE_NEW_SEED) _patches.generation_seed = _patches_newgame.generation_seed = InteractiveRandom();
93
 
        _random.SetSeed(_patches.generation_seed);
94
 
        SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
95
 
        SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
96
 
 
97
 
        IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
98
 
        /* Must start economy early because of the costs. */
99
 
        StartupEconomy();
100
 
 
101
 
        /* Don't generate landscape items when in the scenario editor. */
102
 
        if (_gw.mode == GW_EMPTY) {
103
 
                SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
104
 
 
105
 
                /* Make the map the height of the patch setting */
106
 
                if (_game_mode != GM_MENU) FlatEmptyWorld(_patches.se_flat_world_height);
107
 
 
108
 
                ConvertGroundTilesIntoWaterTiles();
109
 
                IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
110
 
        } else {
111
 
                GenerateLandscape(_gw.mode);
112
 
                GenerateClearTile();
113
 
 
114
 
                /* only generate towns, tree and industries in newgame mode. */
115
 
                if (_game_mode != GM_EDITOR) {
116
 
                        GenerateTowns();
117
 
                        GenerateIndustries();
118
 
                        GenerateUnmovables();
119
 
                        GenerateTrees();
120
 
                }
121
 
        }
122
 
 
123
 
        ClearStorageChanges(true);
124
 
 
125
 
        /* These are probably pointless when inside the scenario editor. */
126
 
        SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
127
 
        StartupPlayers();
128
 
        IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
129
 
        StartupEngines();
130
 
        IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
131
 
        StartupDisasters();
132
 
        _generating_world = false;
133
 
 
134
 
        /* No need to run the tile loop in the scenario editor. */
135
 
        if (_gw.mode != GW_EMPTY) {
136
 
                uint i;
137
 
 
138
 
                SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
139
 
                for (i = 0; i < 0x500; i++) {
140
 
                        RunTileLoop();
141
 
                        IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
142
 
                }
143
 
        }
144
 
 
145
 
        ResetObjectToPlace();
146
 
        SetLocalPlayer(_gw.lp);
147
 
 
148
 
        SetGeneratingWorldProgress(GWP_GAME_START, 1);
149
 
        /* Call any callback */
150
 
        if (_gw.proc != NULL) _gw.proc();
151
 
        IncreaseGeneratingWorldProgress(GWP_GAME_START);
152
 
 
153
 
        if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
154
 
        /* Show all vital windows again, because we have hidden them */
155
 
        if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
156
 
        _gw.active   = false;
157
 
        _gw.thread   = NULL;
158
 
        _gw.proc     = NULL;
159
 
        _gw.threaded = false;
160
 
 
161
 
        DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
162
 
        MarkWholeScreenDirty();
163
 
 
164
 
        if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
165
 
 
166
 
        if (_patches.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
167
 
 
168
 
        return NULL;
 
110
        try {
 
111
                _generating_world = true;
 
112
                if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
 
113
                /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
 
114
                if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
 
115
                _random.SetSeed(_settings_game.game_creation.generation_seed);
 
116
                SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
 
117
                SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
 
118
 
 
119
                IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
 
120
                /* Must start economy early because of the costs. */
 
121
                StartupEconomy();
 
122
 
 
123
                /* Don't generate landscape items when in the scenario editor. */
 
124
                if (_gw.mode == GW_EMPTY) {
 
125
                        SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
 
126
 
 
127
                        /* Make sure the tiles at the north border are void tiles if needed. */
 
128
                        if (_settings_game.construction.freeform_edges) {
 
129
                                for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
 
130
                                for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
 
131
                        }
 
132
 
 
133
                        /* Make the map the height of the setting */
 
134
                        if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
 
135
 
 
136
                        ConvertGroundTilesIntoWaterTiles();
 
137
                        IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
 
138
                } else {
 
139
                        GenerateLandscape(_gw.mode);
 
140
                        GenerateClearTile();
 
141
 
 
142
                        /* only generate towns, tree and industries in newgame mode. */
 
143
                        if (_game_mode != GM_EDITOR) {
 
144
                                if (!GenerateTowns(_settings_game.economy.town_layout)) {
 
145
                                        HandleGeneratingWorldAbortion();
 
146
                                        return;
 
147
                                }
 
148
                                GenerateIndustries();
 
149
                                GenerateUnmovables();
 
150
                                GenerateTrees();
 
151
                        }
 
152
                }
 
153
 
 
154
                ClearStorageChanges(true);
 
155
 
 
156
                /* These are probably pointless when inside the scenario editor. */
 
157
                SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
 
158
                StartupCompanies();
 
159
                IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
 
160
                StartupEngines();
 
161
                IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
 
162
                StartupDisasters();
 
163
                _generating_world = false;
 
164
 
 
165
                /* No need to run the tile loop in the scenario editor. */
 
166
                if (_gw.mode != GW_EMPTY) {
 
167
                        uint i;
 
168
 
 
169
                        SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
 
170
                        for (i = 0; i < 0x500; i++) {
 
171
                                RunTileLoop();
 
172
                                IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
 
173
                        }
 
174
                }
 
175
 
 
176
                ResetObjectToPlace();
 
177
                _local_company = _gw.lc;
 
178
 
 
179
                SetGeneratingWorldProgress(GWP_GAME_START, 1);
 
180
                /* Call any callback */
 
181
                if (_gw.proc != NULL) _gw.proc();
 
182
                IncreaseGeneratingWorldProgress(GWP_GAME_START);
 
183
 
 
184
                CleanupGeneration();
 
185
 
 
186
                if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
 
187
                DEBUG(desync, 1, "new_map: %i\n", _settings_game.game_creation.generation_seed);
 
188
 
 
189
                if (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, CMD_PAUSE);
 
190
                if (_debug_desync_level > 0) {
 
191
                        char name[MAX_PATH];
 
192
                        snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
 
193
                        SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
 
194
                }
 
195
        } catch (...) {
 
196
                _generating_world = false;
 
197
                throw;
 
198
        }
169
199
}
170
200
 
171
201
/**
194
224
{
195
225
        if (_gw.thread == NULL) return;
196
226
        _gw.quit_thread = true;
197
 
        OTTDJoinThread((OTTDThread*)_gw.thread);
 
227
        _gw.thread->Join();
 
228
        delete _gw.thread;
198
229
        _gw.thread   = NULL;
199
230
        _gw.threaded = false;
200
231
}
225
256
 
226
257
        if (_gw.abortp != NULL) _gw.abortp();
227
258
 
228
 
        if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
229
 
        /* Show all vital windows again, because we have hidden them */
230
 
        if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
231
 
        _gw.active   = false;
232
 
        _gw.thread   = NULL;
233
 
        _gw.proc     = NULL;
234
 
        _gw.abortp   = NULL;
235
 
        _gw.threaded = false;
236
 
 
237
 
        DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
238
 
        MarkWholeScreenDirty();
239
 
 
240
 
        OTTDExitThread();
 
259
        CleanupGeneration();
 
260
 
 
261
        if (_gw.thread != NULL) _gw.thread->Exit();
 
262
 
 
263
        extern void SwitchToMode(SwitchMode new_mode);
 
264
        SwitchToMode(_switch_mode);
241
265
}
242
266
 
243
267
/**
246
270
 * @param size_x The X-size of the map.
247
271
 * @param size_y The Y-size of the map.
248
272
 */
249
 
void GenerateWorld(int mode, uint size_x, uint size_y)
 
273
void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y)
250
274
{
251
275
        if (_gw.active) return;
252
276
        _gw.mode   = mode;
255
279
        _gw.active = true;
256
280
        _gw.abort  = false;
257
281
        _gw.abortp = NULL;
258
 
        _gw.lp     = _local_player;
 
282
        _gw.lc     = _local_company;
259
283
        _gw.wait_for_draw = false;
260
284
        _gw.quit_thread   = false;
261
285
        _gw.threaded      = true;
262
286
 
263
287
        /* This disables some commands and stuff */
264
 
        SetLocalPlayer(PLAYER_SPECTATOR);
 
288
        SetLocalCompany(COMPANY_SPECTATOR);
265
289
        /* Make sure everything is done via OWNER_NONE */
266
 
        _current_player = OWNER_NONE;
 
290
        _current_company = OWNER_NONE;
267
291
 
268
292
        /* Set the date before loading sprites as some newgrfs check it */
269
 
        SetDate(ConvertYMDToDate(_patches.starting_year, 0, 1));
 
293
        SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
270
294
 
271
295
        /* Load the right landscape stuff */
272
296
        GfxLoadSprites();
273
297
        LoadStringWidthTable();
274
298
 
275
 
        InitializeGame(IG_NONE, _gw.size_x, _gw.size_y);
 
299
        InitializeGame(_gw.size_x, _gw.size_y, false);
276
300
        PrepareGenerateWorldProgress();
277
301
 
278
302
        /* Re-init the windowing system */
279
303
        ResetWindowSystem();
280
304
 
281
305
        /* Create toolbars */
282
 
        SetupColorsAndInitialWindow();
283
 
 
284
 
        if (_network_dedicated ||
285
 
            (_gw.thread = OTTDCreateThread(&_GenerateWorld, NULL)) == NULL) {
 
306
        SetupColoursAndInitialWindow();
 
307
 
 
308
        if (_gw.thread != NULL) {
 
309
                _gw.thread->Join();
 
310
                delete _gw.thread;
 
311
                _gw.thread = NULL;
 
312
        }
 
313
 
 
314
        if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
 
315
            !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
286
316
                DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
287
317
                _gw.threaded = false;
288
318
                _GenerateWorld(NULL);