/** * 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 = NULL; _gw.lc = _local_company; _gw.quit_thread = false; _gw.threaded = true; /* This disables some commands and stuff */ SetLocalCompany(COMPANY_SPECTATOR); InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings); PrepareGenerateWorldProgress(); /* 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); if (_gw.thread != NULL) { _gw.thread->Join(); delete _gw.thread; _gw.thread = NULL; } if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread, "ottd:genworld")) { DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode"); _gw.threaded = false; _modal_progress_work_mutex->EndCritical(); _GenerateWorld(NULL); _modal_progress_work_mutex->BeginCritical(); return; } UnshowCriticalError(); /* Remove any open window */ DeleteAllNonVitalWindows(); /* Hide vital windows, because we don't allow to use them */ HideVitalWindows(); /* Don't show the dialog if we don't have a thread */ ShowGenerateWorldProgress(); /* Centre the view on the map */ if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) { ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true); } }
/** * This code is shared for the majority of the pushbuttons. * Handles e.g. the pressing of a button (to build things), playing of click sound and sets certain parameters * * @param w Window which called the function * @param widget ID of the widget (=button) that called this function * @param cursor How should the cursor image change? E.g. cursor with depot image in it * @param mode Tile highlighting mode, e.g. drawing a rectangle or a dot on the ground * @return true if the button is clicked, false if it's unclicked */ bool HandlePlacePushButton(Window *w, int widget, CursorID cursor, HighLightStyle mode) { if (w->IsWidgetDisabled(widget)) return false; if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP); w->SetDirty(); if (w->IsWidgetLowered(widget)) { ResetObjectToPlace(); return false; } SetObjectToPlace(cursor, PAL_NONE, mode, w->window_class, w->window_number); w->LowerWidget(widget); return true; }
/** * The internal, real, generate function. */ static void _GenerateWorld(void *) { /* Make sure everything is done via OWNER_NONE. */ Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE); try { _generating_world = true; _modal_progress_work_mutex->BeginCritical(); if (_network_dedicated) DEBUG(net, 1, "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 row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row)); for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0)); } /* 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); } 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)) { _cur_company.Restore(); 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() != NULL) { 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 != NULL) _gw.proc(); IncreaseGeneratingWorldProgress(GWP_GAME_START); CleanupGeneration(); _modal_progress_work_mutex->EndCritical(); ShowNewGRFError(); if (_network_dedicated) DEBUG(net, 1, "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 (...) { BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true); if (_cur_company.IsValid()) _cur_company.Restore(); _generating_world = false; _modal_progress_work_mutex->EndCritical(); throw; } }
SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64) { static const StringID saveload_captions[] = { STR_SAVELOAD_LOAD_CAPTION, STR_SAVELOAD_LOAD_SCENARIO, STR_SAVELOAD_SAVE_CAPTION, STR_SAVELOAD_SAVE_SCENARIO, STR_SAVELOAD_LOAD_HEIGHTMAP, }; assert((uint)mode < lengthof(saveload_captions)); /* Use an array to define what will be the current file type being handled * by current file mode */ switch (mode) { case SLD_SAVE_GAME: this->GenerateFileName(); break; case SLD_SAVE_SCENARIO: strecpy(this->edit_str_buf, "UNNAMED", &this->edit_str_buf[edit_str_size - 1]); break; default: break; } this->afilter = CS_ALPHANUMERAL; InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 240); this->CreateNestedTree(desc); if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(SLWW_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); this->GetWidget<NWidgetCore>(SLWW_WINDOWTITLE)->widget_data = saveload_captions[mode]; this->FinishInitNested(desc, 0); this->LowerWidget(SLWW_DRIVES_DIRECTORIES_LIST); /* pause is only used in single-player, non-editor mode, non-menu mode. It * will be unpaused in the WE_DESTROY event handler. */ if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) { DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE); } SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); this->OnInvalidateData(0); ResetObjectToPlace(); o_dir.type = FIOS_TYPE_DIRECT; switch (_saveload_mode) { case SLD_SAVE_GAME: case SLD_LOAD_GAME: FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR); break; case SLD_SAVE_SCENARIO: case SLD_LOAD_SCENARIO: FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR); break; case SLD_LOAD_HEIGHTMAP: FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR); break; default: strecpy(o_dir.name, _personal_dir, lastof(o_dir.name)); } /* Focus the edit box by default in the save windows */ if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { this->SetFocusedWidget(SLWW_SAVE_OSK_TITLE); } }
SaveLoadWindow(WindowDesc *desc, AbstractFileType abstract_filetype, SaveLoadOperation fop) : Window(desc), filename_editbox(64), abstract_filetype(abstract_filetype), fop(fop), filter_editbox(EDITBOX_MAX_SIZE) { assert(this->fop == SLO_SAVE || this->fop == SLO_LOAD); /* For saving, construct an initial file name. */ if (this->fop == SLO_SAVE) { switch (this->abstract_filetype) { case FT_SAVEGAME: this->GenerateFileName(); break; case FT_SCENARIO: case FT_HEIGHTMAP: this->filename_editbox.text.Assign("UNNAMED"); break; default: NOT_REACHED(); } } this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox; this->filename_editbox.ok_button = WID_SL_SAVE_GAME; this->CreateNestedTree(true); if (this->fop == SLO_LOAD && this->abstract_filetype == FT_SAVEGAME) { this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); } /* Select caption string of the window. */ StringID caption_string; switch (this->abstract_filetype) { case FT_SAVEGAME: caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION; break; case FT_SCENARIO: caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO; break; case FT_HEIGHTMAP: caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP; break; default: NOT_REACHED(); } this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = caption_string; this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR); this->FinishInitNested(0); this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST); this->querystrings[WID_SL_FILTER] = &this->filter_editbox; this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR; /* pause is only used in single-player, non-editor mode, non-menu mode. It * will be unpaused in the WE_DESTROY event handler. */ if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) { DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE); } SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); this->OnInvalidateData(SLIWD_RESCAN_FILES); ResetObjectToPlace(); /* Select the initial directory. */ o_dir.type = FIOS_TYPE_DIRECT; switch (this->abstract_filetype) { case FT_SAVEGAME: FioGetDirectory(o_dir.name, lastof(o_dir.name), SAVE_DIR); break; case FT_SCENARIO: FioGetDirectory(o_dir.name, lastof(o_dir.name), SCENARIO_DIR); break; case FT_HEIGHTMAP: FioGetDirectory(o_dir.name, lastof(o_dir.name), HEIGHTMAP_DIR); break; default: strecpy(o_dir.name, _personal_dir, lastof(o_dir.name)); } switch (this->fop) { case SLO_SAVE: /* Focus the edit box by default in the save window */ this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE); break; default: this->SetFocusedWidget(WID_SL_FILTER); } }