OpenTTD Source  14.0-beta3
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"
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"
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 std::string _savegame_id;
40 
41 extern TileIndex _cur_tileloop_tile;
42 extern void MakeNewgameSettingsLive();
43 
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();
61 
69 std::string GenerateUid(std::string_view subject)
70 {
71  std::array<uint8_t, 32> random_bytes;
72  RandomBytesWithFallback(random_bytes);
73 
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);
76 
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());
83 
84  return FormatArrayAsHex(digest);
85 }
86 
91 {
92  _savegame_id = GenerateUid("OpenTTD Savegame ID");
93 }
94 
95 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
96 {
97  /* Make sure there isn't any window that can influence anything
98  * related to the new game we're about to start/load. */
100 
101  Map::Allocate(size_x, size_y);
102 
104  _game_speed = 100;
106  _cur_tileloop_tile = 1;
107  _thd.redsq = INVALID_TILE;
108  if (reset_settings) MakeNewgameSettingsLive();
109 
110  _newgrf_profilers.clear();
111 
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);
115 
117  /* If using wallclock units, start at year 1. */
119  } else {
120  /* Otherwise, we always keep the economy date synced with the calendar date. */
121  TimerGameEconomy::SetDate(new_date.base(), 0);
122  }
124  }
125 
128 
129  RebuildStationKdtree();
130  RebuildTownKdtree();
131  RebuildViewportKdtree();
132 
134 
135  InitializeSound();
136  InitializeMusic();
137 
138  InitializeVehicles();
139 
141  InitializeLandscape();
143  InitializeRoadGui();
144  InitializeAirportGui();
145  InitializeDockGui();
146  InitializeGraphGui();
148  InitializeTownGui();
150  InitializeTrees();
151  InitializeIndustries();
153  InitializeBuildingCounts();
154 
155  InitializeNPF();
156 
158  AI::Initialize();
161 
162  InitTextEffects();
165 
167 
169 
170  _gamelog.Reset();
172  _gamelog.Revision();
173  _gamelog.Mode();
176 }
game.hpp
InitializeCompanies
void InitializeCompanies()
Initialize the pool of companies.
Definition: company_cmd.cpp:662
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:33
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:8871
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:1366
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:619
ai.hpp
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:63
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:955
GenerateUid
std::string GenerateUid(std::string_view subject)
Generate an unique ID.
Definition: misc.cpp:69
tilehighlight_func.h
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:50
AI::Initialize
static void Initialize()
Initialize the AI system.
Definition: ai_core.cpp:162
_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:341
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
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:90
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:731
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:2259
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
_savegame_id
std::string _savegame_id
Unique ID of the current savegame.
Definition: misc.cpp:39
timer_game_economy.h
news_func.h
Game::Initialize
static void Initialize()
Initialize the Game system.
Definition: game_core.cpp:57