Doc: Prepare for 14.0-RC2 release (#12248)
[openttd-github.git] / src / misc.cpp
blobd6467a18121dc32dfd3953204f1611663aea833d
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 */
8 /** @file misc.cpp Misc functions that shouldn't be here. */
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "news_func.h"
13 #include "ai/ai.hpp"
14 #include "script/script_gui.h"
15 #include "newgrf.h"
16 #include "newgrf_house.h"
17 #include "economy_func.h"
18 #include "timer/timer_game_calendar.h"
19 #include "timer/timer_game_economy.h"
20 #include "timer/timer_game_tick.h"
21 #include "texteff.hpp"
22 #include "gfx_func.h"
23 #include "gamelog.h"
24 #include "animated_tile_func.h"
25 #include "tilehighlight_func.h"
26 #include "network/network_func.h"
27 #include "window_func.h"
28 #include "core/pool_type.hpp"
29 #include "game/game.hpp"
30 #include "linkgraph/linkgraphschedule.h"
31 #include "station_kdtree.h"
32 #include "town_kdtree.h"
33 #include "viewport_kdtree.h"
34 #include "newgrf_profiling.h"
35 #include "3rdparty/monocypher/monocypher.h"
37 #include "safeguards.h"
39 extern TileIndex _cur_tileloop_tile;
40 extern void MakeNewgameSettingsLive();
42 void InitializeSound();
43 void InitializeMusic();
44 void InitializeVehicles();
45 void InitializeRailGui();
46 void InitializeRoadGui();
47 void InitializeAirportGui();
48 void InitializeDockGui();
49 void InitializeGraphGui();
50 void InitializeObjectGui();
51 void InitializeTownGui();
52 void InitializeIndustries();
53 void InitializeObjects();
54 void InitializeTrees();
55 void InitializeCompanies();
56 void InitializeCheats();
57 void InitializeNPF();
58 void InitializeOldNames();
60 /**
61 * Generate an unique ID.
63 * It isn't as much of an unique ID but more a hashed digest of a random
64 * string and a time. It is very likely to be unique, but it does not follow
65 * any UUID standard.
67 std::string GenerateUid(std::string_view subject)
69 std::array<uint8_t, 32> random_bytes;
70 RandomBytesWithFallback(random_bytes);
72 auto current_time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
73 std::string coding_string = fmt::format("{}{}", current_time, subject);
75 std::array<uint8_t, 16> digest;
76 crypto_blake2b_ctx ctx;
77 crypto_blake2b_init(&ctx, digest.size());
78 crypto_blake2b_update(&ctx, random_bytes.data(), random_bytes.size());
79 crypto_blake2b_update(&ctx, reinterpret_cast<const uint8_t *>(coding_string.data()), coding_string.size());
80 crypto_blake2b_final(&ctx, digest.data());
82 return FormatArrayAsHex(digest);
85 /**
86 * Generate an unique savegame ID.
88 void GenerateSavegameId()
90 _game_session_stats.savegame_id = GenerateUid("OpenTTD Savegame ID");
93 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
95 /* Make sure there isn't any window that can influence anything
96 * related to the new game we're about to start/load. */
97 UnInitWindowSystem();
99 Map::Allocate(size_x, size_y);
101 _pause_mode = PM_UNPAUSED;
102 _game_speed = 100;
103 TimerGameTick::counter = 0;
104 _cur_tileloop_tile = 1;
105 _thd.redsq = INVALID_TILE;
106 if (reset_settings) MakeNewgameSettingsLive();
108 _newgrf_profilers.clear();
110 if (reset_date) {
111 TimerGameCalendar::Date new_date = TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
112 TimerGameCalendar::SetDate(new_date, 0);
114 if (TimerGameEconomy::UsingWallclockUnits()) {
115 /* If using wallclock units, start at year 1. */
116 TimerGameEconomy::SetDate(TimerGameEconomy::ConvertYMDToDate(1, 0, 1), 0);
117 } else {
118 /* Otherwise, we always keep the economy date synced with the calendar date. */
119 TimerGameEconomy::SetDate(new_date.base(), 0);
121 InitializeOldNames();
124 LinkGraphSchedule::Clear();
125 PoolBase::Clean(PT_NORMAL);
127 RebuildStationKdtree();
128 RebuildTownKdtree();
129 RebuildViewportKdtree();
131 ResetPersistentNewGRFData();
133 InitializeSound();
134 InitializeMusic();
136 InitializeVehicles();
138 InitNewsItemStructs();
139 InitializeLandscape();
140 InitializeRailGui();
141 InitializeRoadGui();
142 InitializeAirportGui();
143 InitializeDockGui();
144 InitializeGraphGui();
145 InitializeObjectGui();
146 InitializeTownGui();
147 InitializeScriptGui();
148 InitializeTrees();
149 InitializeIndustries();
150 InitializeObjects();
151 InitializeBuildingCounts();
153 InitializeNPF();
155 InitializeCompanies();
156 AI::Initialize();
157 Game::Initialize();
158 InitializeCheats();
160 InitTextEffects();
161 NetworkInitChatMessage();
162 InitializeAnimatedTiles();
164 InitializeEconomy();
166 ResetObjectToPlace();
168 _gamelog.Reset();
169 _gamelog.StartAction(GLAT_START);
170 _gamelog.Revision();
171 _gamelog.Mode();
172 _gamelog.GRFAddList(_grfconfig);
173 _gamelog.StopAction();