OpenTTD Source: src/misc.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
39extern TileIndex _cur_tileloop_tile;
40extern void MakeNewgameSettingsLive();
41
42void InitializeSound();
43void InitializeMusic();
44void InitializeVehicles();
45void InitializeRailGui();
46void InitializeRoadGui();
47void InitializeAirportGui();
48void InitializeDockGui();
49void InitializeGraphGui();
50void InitializeObjectGui();
51void InitializeTownGui();
52void InitializeIndustries();
53void InitializeObjects();
54void InitializeTrees();
55void InitializeCompanies();
56void InitializeCheats();
57void InitializeOldNames();
58
66std::string GenerateUid(std::string_view subject)
67{
68 std::array<uint8_t, 32> random_bytes;
69 RandomBytesWithFallback(random_bytes);
70
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);
73
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());
80
81 return FormatArrayAsHex(digest);
82}
83
87 void GenerateSavegameId()
88{
89 _game_session_stats.savegame_id = GenerateUid("OpenTTD Savegame ID");
90}
91
92void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
93{
94 /* Make sure there isn't any window that can influence anything
95 * related to the new game we're about to start/load. */
96 UnInitWindowSystem();
97
98 Map::Allocate(size_x, size_y);
99
100 _pause_mode = {};
101 _game_speed = 100;
102 TimerGameTick::counter = 0;
103 TimerGameEconomy::days_since_last_month = 0;
104 _cur_tileloop_tile = TileIndex{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(TimerGameEconomy::Year{1}, 0, 1), 0);
117 } else {
118 /* Otherwise, we always keep the economy date synced with the calendar date. */
119 TimerGameEconomy::SetDate(TimerGameEconomy::Date{new_date.base()}, 0);
120 }
121 InitializeOldNames();
122 }
123
124 LinkGraphSchedule::Clear();
125 PoolBase::Clean(PoolType::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
152 InitializeCompanies();
153 AI::Initialize();
154 Game::Initialize();
155 InitializeCheats();
156
157 InitTextEffects();
158 NetworkInitChatMessage();
159 InitializeAnimatedTiles();
160
161 InitializeEconomy();
162
163 ResetObjectToPlace();
164
165 _gamelog.Reset();
166 _gamelog.StartAction(GLAT_START);
167 _gamelog.Revision();
168 _gamelog.Mode();
169 _gamelog.GRFAddList(_grfconfig);
170 _gamelog.StopAction();
171}
ai.hpp
Base functions for all AIs.
InitializeAnimatedTiles
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
Definition animated_tile.cpp:106
animated_tile_func.h
Tile animation!
AI::Initialize
static void Initialize()
Initialize the AI system.
Definition ai_core.cpp:165
Game::Initialize
static void Initialize()
Initialize the Game system.
Definition game_core.cpp:57
Gamelog::Mode
void Mode()
Logs a change in game mode (scenario editor or game)
Definition gamelog.cpp:389
Gamelog::Revision
void Revision()
Logs a change in game revision.
Definition gamelog.cpp:378
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::Reset
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition gamelog.cpp:94
Gamelog::GRFAddList
void GRFAddList(const GRFConfigList &newg)
Logs adding of list of GRFs.
Definition gamelog.cpp:578
Gamelog::StopAction
void StopAction()
Stops logging of any changes.
Definition gamelog.cpp:74
LinkGraphSchedule::Clear
static void Clear()
Clear all link graphs and jobs from the schedule.
Definition linkgraphschedule.cpp:121
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
TimerGameCalendar::SetDate
static void SetDate(Date date, DateFract fract)
Set the date.
Definition timer_game_calendar.cpp:66
TimerGameEconomy::UsingWallclockUnits
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
Definition timer_game_economy.cpp:98
TimerGameEconomy::SetDate
static void SetDate(Date date, DateFract fract)
Set the date.
Definition timer_game_economy.cpp:82
TimerGameEconomy::days_since_last_month
static uint days_since_last_month
Number of days that have elapsed since the last month.
Definition timer_game_economy.h:40
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:67
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition timer_game_tick.h:60
InitializeEconomy
void InitializeEconomy()
Resets economy to initial values.
Definition economy.cpp:934
economy_func.h
Functions related to the economy.
game.hpp
Base functions for all Games.
_gamelog
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:31
gamelog.h
Functions to be called to log fundamental changes to the game.
GLAT_START
@ GLAT_START
Game created.
Definition gamelog.h:17
_game_speed
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition gfx.cpp:40
_game_session_stats
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:51
_pause_mode
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:50
gfx_func.h
Functions related to the gfx engine.
landscape.h
Functions related to OTTD's landscape.
linkgraphschedule.h
Declaration of link graph schedule used for cargo distribution.
InitializeObjects
void InitializeObjects()
Initialize/reset the objects.
Definition object_cmd.cpp:73
InitializeCheats
void InitializeCheats()
Reinitialise all the cheats.
Definition cheat.cpp:19
GenerateSavegameId
void GenerateSavegameId()
Generate an unique savegame ID.
Definition misc.cpp:87
InitializeObjectGui
void InitializeObjectGui()
Reset all data of the object GUI.
Definition object_gui.cpp:435
GenerateUid
std::string GenerateUid(std::string_view subject)
Generate an unique ID.
Definition misc.cpp:66
InitializeMusic
void InitializeMusic()
Prepare the music system for use.
Definition music_gui.cpp:472
InitializeRailGui
void InitializeRailGui()
Initialize rail building GUI settings.
Definition rail_gui.cpp:1915
InitializeCompanies
void InitializeCompanies()
Initialize the pool of companies.
Definition company_cmd.cpp:684
InitializeOldNames
void InitializeOldNames()
Initialize the old names table memory.
Definition strings_sl.cpp:108
NetworkInitChatMessage
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
Definition network_chat_gui.cpp:108
network_func.h
Network functions used by other parts of OpenTTD.
ResetPersistentNewGRFData
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:505
newgrf.h
Base for the NewGRF implementation.
_grfconfig
GRFConfigList _grfconfig
First item in list of current GRF set up.
Definition newgrf_config.cpp:154
newgrf_house.h
Functions related to NewGRF houses.
newgrf_profiling.h
Profiling of NewGRF action 2 handling.
news_func.h
Functions related to news.
InitNewsItemStructs
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition news_gui.cpp:714
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
PoolType::Normal
@ Normal
Normal pool containing game objects.
RandomBytesWithFallback
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
Definition random_func.cpp:94
safeguards.h
A number of safeguards to prevent using unsafe methods.
InitializeScriptGui
void InitializeScriptGui()
Reset the Script windows to their initial state.
Definition script_gui.cpp:1298
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
station_kdtree.h
Declarations for accessing the k-d tree of stations.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:77
GameCreationSettings::starting_year
TimerGameCalendar::Year starting_year
starting date
Definition settings_type.h:380
GameSessionStats::savegame_id
std::string savegame_id
Unique ID of the savegame.
Definition openttd.h:57
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition settings_type.h:634
Map::Allocate
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:37
PoolBase::Clean
static void Clean(PoolTypes)
Clean all pools of given type.
Definition pool_func.cpp:30
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TileHighlightData::redsq
TileIndex redsq
The tile that has to get a red selection.
Definition tilehighlight_type.h:72
texteff.hpp
Functions related to text effects.
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
tilehighlight_func.h
Functions related to tile highlights.
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition viewport.cpp:3573
timer_game_calendar.h
Definition of the game-calendar-timer.
timer_game_economy.h
Definition of the game-economy-timer.
timer_game_tick.h
Definition of the tick-based game-timer.
town_kdtree.h
Declarations for accessing the k-d tree of towns.
UnInitWindowSystem
void UnInitWindowSystem()
Close down the windowing system.
Definition window.cpp:1881
window_func.h
Window functions not directly related to making/drawing windows.