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/>.
8 /** @file misc.cpp Misc functions that shouldn't be here. */
11 #include "landscape.h"
12 #include "news_func.h"
14 #include "script/script_gui.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"
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 InitializeOldNames();
60 * Generate an unique ID.
62 * It isn't as much of an unique ID but more a hashed digest of a random
63 * string and a time. It is very likely to be unique, but it does not follow
66 std::string
GenerateUid(std::string_view subject
)
68 std::array
<uint8_t, 32> random_bytes
;
69 RandomBytesWithFallback(random_bytes
);
71 auto current_time
= std::chrono::duration_cast
<std::chrono::nanoseconds
>(std::chrono::steady_clock::now().time_since_epoch()).count();
72 std::string coding_string
= fmt::format("{}{}", current_time
, subject
);
74 std::array
<uint8_t, 16> digest
;
75 crypto_blake2b_ctx ctx
;
76 crypto_blake2b_init(&ctx
, digest
.size());
77 crypto_blake2b_update(&ctx
, random_bytes
.data(), random_bytes
.size());
78 crypto_blake2b_update(&ctx
, reinterpret_cast<const uint8_t *>(coding_string
.data()), coding_string
.size());
79 crypto_blake2b_final(&ctx
, digest
.data());
81 return FormatArrayAsHex(digest
);
85 * Generate an unique savegame ID.
87 void GenerateSavegameId()
89 _game_session_stats
.savegame_id
= GenerateUid("OpenTTD Savegame ID");
92 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
)
94 /* Make sure there isn't any window that can influence anything
95 * related to the new game we're about to start/load. */
98 Map::Allocate(size_x
, size_y
);
100 _pause_mode
= PM_UNPAUSED
;
102 TimerGameTick::counter
= 0;
103 _cur_tileloop_tile
= 1;
104 _thd
.redsq
= INVALID_TILE
;
105 if (reset_settings
) MakeNewgameSettingsLive();
107 _newgrf_profilers
.clear();
110 TimerGameCalendar::Date new_date
= TimerGameCalendar::ConvertYMDToDate(_settings_game
.game_creation
.starting_year
, 0, 1);
111 TimerGameCalendar::SetDate(new_date
, 0);
113 if (TimerGameEconomy::UsingWallclockUnits()) {
114 /* If using wallclock units, start at year 1. */
115 TimerGameEconomy::SetDate(TimerGameEconomy::ConvertYMDToDate(1, 0, 1), 0);
117 /* Otherwise, we always keep the economy date synced with the calendar date. */
118 TimerGameEconomy::SetDate(new_date
.base(), 0);
120 InitializeOldNames();
123 LinkGraphSchedule::Clear();
124 PoolBase::Clean(PT_NORMAL
);
126 RebuildStationKdtree();
128 RebuildViewportKdtree();
130 ResetPersistentNewGRFData();
135 InitializeVehicles();
137 InitNewsItemStructs();
138 InitializeLandscape();
141 InitializeAirportGui();
143 InitializeGraphGui();
144 InitializeObjectGui();
146 InitializeScriptGui();
148 InitializeIndustries();
151 InitializeCompanies();
157 NetworkInitChatMessage();
158 InitializeAnimatedTiles();
162 ResetObjectToPlace();
165 _gamelog
.StartAction(GLAT_START
);
168 _gamelog
.GRFAddList(_grfconfig
);
169 _gamelog
.StopAction();