OpenTTD Source: src/misc.cpp Source File

OpenTTD Source 14.1

misc.cpp
Go to the documentation of this file.
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 */
7
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"
36
37 #include "safeguards.h"
38
39 extern TileIndex _cur_tileloop_tile;
40 extern void MakeNewgameSettingsLive();
41
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();
59
67 std::string GenerateUid(std::string_view subject)
68 {
69 std::array<uint8_t, 32> random_bytes;
70 RandomBytesWithFallback(random_bytes);
71
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);
74
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());
81
82 return FormatArrayAsHex(digest);
83 }
84
88 void GenerateSavegameId()
89 {
90 _game_session_stats.savegame_id = GenerateUid("OpenTTD Savegame ID");
91 }
92
93 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
94 {
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();
98
99 Map::Allocate(size_x, size_y);
100
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();
107
108 _newgrf_profilers.clear();
109
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);
113
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);
120 }
121 InitializeOldNames();
122 }
123
124 LinkGraphSchedule::Clear();
125 PoolBase::Clean(PT_NORMAL);
126
127 RebuildStationKdtree();
128 RebuildTownKdtree();
129 RebuildViewportKdtree();
130
131 ResetPersistentNewGRFData();
132
133 InitializeSound();
134 InitializeMusic();
135
136 InitializeVehicles();
137
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();
152
153 InitializeNPF();
154
155 InitializeCompanies();
156 AI::Initialize();
157 Game::Initialize();
158 InitializeCheats();
159
160 InitTextEffects();
161 NetworkInitChatMessage();
162 InitializeAnimatedTiles();
163
164 InitializeEconomy();
165
166 ResetObjectToPlace();
167
168 _gamelog.Reset();
169 _gamelog.StartAction(GLAT_START);
170 _gamelog.Revision();
171 _gamelog.Mode();
172 _gamelog.GRFAddList(_grfconfig);
173 _gamelog.StopAction();
174 }
game.hpp
InitializeCompanies
void InitializeCompanies()
Initialize the pool of companies.
Definition: company_cmd.cpp:663
LinkGraphSchedule::Clear
static void Clear()
Clear all link graphs and jobs from the schedule.
Definition: linkgraphschedule.cpp:121
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const byte > data)
Format a byte array into a continuous hex string.
Definition: string.cpp:88
newgrf_house.h
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition: timer_game_tick.h:60
station_kdtree.h
TimerGameEconomy::ConvertYMDToDate
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: timer_game_economy.cpp:66
timer_game_calendar.h
_gamelog
Gamelog _gamelog
Gamelog instance.
Definition: gamelog.cpp:31
Map::Allocate
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition: map.cpp:41
NetworkInitChatMessage
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
Definition: network_chat_gui.cpp:109
ResetPersistentNewGRFData
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition: newgrf.cpp:8877
InitNewsItemStructs
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition: news_gui.cpp:634
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
UnInitWindowSystem
void UnInitWindowSystem()
Close down the windowing system.
Definition: window.cpp:1805
TimerGameEconomy::UsingWallclockUnits
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
Definition: timer_game_economy.cpp:97
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Gamelog::Reset
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:94
economy_func.h
gamelog.h
InitializeScriptGui
void InitializeScriptGui()
Reset the Script windows to their initial state.
Definition: script_gui.cpp:1337
Gamelog::Revision
void Revision()
Logs a change in game revision.
Definition: gamelog.cpp:378
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:618
ai.hpp
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:69
gfx_func.h
InitializeOldNames
void InitializeOldNames()
Initialize the old names table memory.
Definition: strings_sl.cpp:111
InitializeEconomy
void InitializeEconomy()
Resets economy to initial values.
Definition: economy.cpp:950
GenerateUid
std::string GenerateUid(std::string_view subject)
Generate an unique ID.
Definition: misc.cpp:67
tilehighlight_func.h
GameSessionStats::savegame_id
std::string savegame_id
Unique ID of the savegame.
Definition: openttd.h:57
TimerGameCalendar::ConvertYMDToDate
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: timer_game_calendar.cpp:55
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:49
AI::Initialize
static void Initialize()
Initialize the AI system.
Definition: ai_core.cpp:166
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
timer_game_tick.h
safeguards.h
GameCreationSettings::starting_year
TimerGameCalendar::Year starting_year
starting date
Definition: settings_type.h:340
PoolBase::Clean
static void Clean(PoolType)
Clean all pools of given type.
Definition: pool_func.cpp:30
TimerGameCalendar::SetDate
static void SetDate(Date date, DateFract fract)
Set the date.
Definition: timer_game_calendar.cpp:66
linkgraphschedule.h
_game_speed
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:39
stdafx.h
InitializeCheats
void InitializeCheats()
Reinitialise all the cheats.
Definition: cheat.cpp:19
landscape.h
_game_session_stats
GameSessionStats _game_session_stats
Statistics about the current session.
Definition: gfx.cpp:50
GLAT_START
@ GLAT_START
Game created.
Definition: gamelog.h:17
animated_tile_func.h
RandomBytesWithFallback
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
Definition: random_func.cpp:111
texteff.hpp
Gamelog::StartAction
void StartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:65
Gamelog::StopAction
void StopAction()
Stops logging of any changes.
Definition: gamelog.cpp:74
PT_NORMAL
@ PT_NORMAL
Normal pool containing game objects.
Definition: pool_type.hpp:18
InitializeMusic
void InitializeMusic()
Prepare the music system for use.
Definition: music_gui.cpp:473
GenerateSavegameId
void GenerateSavegameId()
Generate an unique savegame ID.
Definition: misc.cpp:88
newgrf.h
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:164
TimerGameEconomy::SetDate
static void SetDate(Date date, DateFract fract)
Set the date.
Definition: timer_game_economy.cpp:81
window_func.h
Gamelog::Mode
void Mode()
Logs a change in game mode (scenario editor or game)
Definition: gamelog.cpp:389
InitializeObjectGui
void InitializeObjectGui()
Reset all data of the object GUI.
Definition: object_gui.cpp:733
InitializeObjects
void InitializeObjects()
Initialize/reset the objects.
Definition: object_cmd.cpp:73
Gamelog::GRFAddList
void GRFAddList(const GRFConfig *newg)
Logs adding of list of GRFs.
Definition: gamelog.cpp:578
InitializeRailGui
void InitializeRailGui()
Initialize rail building GUI settings.
Definition: rail_gui.cpp:2256
pool_type.hpp
newgrf_profiling.h
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition: viewport.cpp:3483
InitializeAnimatedTiles
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
Definition: animated_tile.cpp:73
town_kdtree.h
network_func.h
TileHighlightData::redsq
TileIndex redsq
The tile that has to get a red selection.
Definition: tilehighlight_type.h:72
timer_game_economy.h
news_func.h
Game::Initialize
static void Initialize()
Initialize the Game system.
Definition: game_core.cpp:57