Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / misc.cpp
blobdd95950ddaa23292d1c19091b1978f37c97acfff
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 std::string _savegame_id; ///< Unique ID of the current savegame.
41 extern TileIndex _cur_tileloop_tile;
42 extern void MakeNewgameSettingsLive();
44 void InitializeSound();
45 void InitializeMusic();
46 void InitializeVehicles();
47 void InitializeRailGui();
48 void InitializeRoadGui();
49 void InitializeAirportGui();
50 void InitializeDockGui();
51 void InitializeGraphGui();
52 void InitializeObjectGui();
53 void InitializeTownGui();
54 void InitializeIndustries();
55 void InitializeObjects();
56 void InitializeTrees();
57 void InitializeCompanies();
58 void InitializeCheats();
59 void InitializeNPF();
60 void InitializeOldNames();
62 /**
63 * Generate an unique ID.
65 * It isn't as much of an unique ID but more a hashed digest of a random
66 * string and a time. It is very likely to be unique, but it does not follow
67 * any UUID standard.
69 std::string GenerateUid(std::string_view subject)
71 std::array<uint8_t, 32> random_bytes;
72 RandomBytesWithFallback(random_bytes);
74 auto current_time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
75 std::string coding_string = fmt::format("{}{}", current_time, subject);
77 std::array<uint8_t, 16> digest;
78 crypto_blake2b_ctx ctx;
79 crypto_blake2b_init(&ctx, digest.size());
80 crypto_blake2b_update(&ctx, random_bytes.data(), random_bytes.size());
81 crypto_blake2b_update(&ctx, reinterpret_cast<const uint8_t *>(coding_string.data()), coding_string.size());
82 crypto_blake2b_final(&ctx, digest.data());
84 return FormatArrayAsHex(digest);
87 /**
88 * Generate an unique savegame ID.
90 void GenerateSavegameId()
92 _savegame_id = GenerateUid("OpenTTD Savegame ID");
95 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
97 /* Make sure there isn't any window that can influence anything
98 * related to the new game we're about to start/load. */
99 UnInitWindowSystem();
101 Map::Allocate(size_x, size_y);
103 _pause_mode = PM_UNPAUSED;
104 _game_speed = 100;
105 TimerGameTick::counter = 0;
106 _cur_tileloop_tile = 1;
107 _thd.redsq = INVALID_TILE;
108 if (reset_settings) MakeNewgameSettingsLive();
110 _newgrf_profilers.clear();
112 if (reset_date) {
113 TimerGameCalendar::Date new_date = TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
114 TimerGameCalendar::SetDate(new_date, 0);
116 if (TimerGameEconomy::UsingWallclockUnits()) {
117 /* If using wallclock units, start at year 1. */
118 TimerGameEconomy::SetDate(TimerGameEconomy::ConvertYMDToDate(1, 0, 1), 0);
119 } else {
120 /* Otherwise, we always keep the economy date synced with the calendar date. */
121 TimerGameEconomy::SetDate(new_date.base(), 0);
123 InitializeOldNames();
126 LinkGraphSchedule::Clear();
127 PoolBase::Clean(PT_NORMAL);
129 RebuildStationKdtree();
130 RebuildTownKdtree();
131 RebuildViewportKdtree();
133 ResetPersistentNewGRFData();
135 InitializeSound();
136 InitializeMusic();
138 InitializeVehicles();
140 InitNewsItemStructs();
141 InitializeLandscape();
142 InitializeRailGui();
143 InitializeRoadGui();
144 InitializeAirportGui();
145 InitializeDockGui();
146 InitializeGraphGui();
147 InitializeObjectGui();
148 InitializeTownGui();
149 InitializeScriptGui();
150 InitializeTrees();
151 InitializeIndustries();
152 InitializeObjects();
153 InitializeBuildingCounts();
155 InitializeNPF();
157 InitializeCompanies();
158 AI::Initialize();
159 Game::Initialize();
160 InitializeCheats();
162 InitTextEffects();
163 NetworkInitChatMessage();
164 InitializeAnimatedTiles();
166 InitializeEconomy();
168 ResetObjectToPlace();
170 _gamelog.Reset();
171 _gamelog.StartAction(GLAT_START);
172 _gamelog.Revision();
173 _gamelog.Mode();
174 _gamelog.GRFAddList(_grfconfig);
175 _gamelog.StopAction();