OpenTTD Source 20241224-master-gee860a5c8e
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"
21#include "texteff.hpp"
22#include "gfx_func.h"
23#include "gamelog.h"
24#include "animated_tile_func.h"
25#include "tilehighlight_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
39extern TileIndex _cur_tileloop_tile;
40extern void MakeNewgameSettingsLive();
41
42void InitializeSound();
43void InitializeMusic();
44void InitializeVehicles();
46void InitializeRoadGui();
47void InitializeAirportGui();
48void InitializeDockGui();
49void InitializeGraphGui();
51void InitializeTownGui();
52void InitializeIndustries();
54void InitializeTrees();
56void InitializeCheats();
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
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. */
97
98 Map::Allocate(size_x, size_y);
99
101 _game_speed = 100;
103 _cur_tileloop_tile = 1;
104 _thd.redsq = INVALID_TILE;
105 if (reset_settings) MakeNewgameSettingsLive();
106
107 _newgrf_profilers.clear();
108
109 if (reset_date) {
110 TimerGameCalendar::Date new_date = TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
111 TimerGameCalendar::SetDate(new_date, 0);
112
114 /* If using wallclock units, start at year 1. */
116 } else {
117 /* Otherwise, we always keep the economy date synced with the calendar date. */
118 TimerGameEconomy::SetDate(new_date.base(), 0);
119 }
121 }
122
125
126 RebuildStationKdtree();
127 RebuildTownKdtree();
128 RebuildViewportKdtree();
129
131
132 InitializeSound();
134
135 InitializeVehicles();
136
138 InitializeLandscape();
140 InitializeRoadGui();
141 InitializeAirportGui();
142 InitializeDockGui();
143 InitializeGraphGui();
145 InitializeTownGui();
147 InitializeTrees();
148 InitializeIndustries();
150
155
156 InitTextEffects();
159
161
163
164 _gamelog.Reset();
167 _gamelog.Mode();
170}
Base functions for all AIs.
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
Tile animation!
static void Initialize()
Initialize the AI system.
Definition ai_core.cpp:165
static void Initialize()
Initialize the Game system.
Definition game_core.cpp:57
void Mode()
Logs a change in game mode (scenario editor or game)
Definition gamelog.cpp:389
void GRFAddList(const GRFConfig *newg)
Logs adding of list of GRFs.
Definition gamelog.cpp:578
void Revision()
Logs a change in game revision.
Definition gamelog.cpp:378
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
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition gamelog.cpp:94
void StopAction()
Stops logging of any changes.
Definition gamelog.cpp:74
static void Clear()
Clear all link graphs and jobs from the schedule.
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static void SetDate(Date date, DateFract fract)
Set the date.
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
static void SetDate(Date date, DateFract fract)
Set the date.
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
void InitializeEconomy()
Resets economy to initial values.
Definition economy.cpp:951
Functions related to the economy.
Base functions for all Games.
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:31
Functions to be called to log fundamental changes to the game.
@ GLAT_START
Game created.
Definition gamelog.h:17
PauseMode _pause_mode
The current pause mode.
Definition gfx.cpp:50
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition gfx.cpp:40
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:51
Functions related to the gfx engine.
Functions related to OTTD's landscape.
Declaration of link graph schedule used for cargo distribution.
void InitializeObjects()
Initialize/reset the objects.
void InitializeCheats()
Reinitialise all the cheats.
Definition cheat.cpp:19
void GenerateSavegameId()
Generate an unique savegame ID.
Definition misc.cpp:87
void InitializeObjectGui()
Reset all data of the object GUI.
std::string GenerateUid(std::string_view subject)
Generate an unique ID.
Definition misc.cpp:66
void InitializeMusic()
Prepare the music system for use.
void InitializeRailGui()
Initialize rail building GUI settings.
void InitializeCompanies()
Initialize the pool of companies.
void InitializeOldNames()
Initialize the old names table memory.
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
Network functions used by other parts of OpenTTD.
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:8926
Base for the NewGRF implementation.
GRFConfig * _grfconfig
First item in list of current GRF set up.
Functions related to NewGRF houses.
Profiling of NewGRF action 2 handling.
Functions related to news.
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition news_gui.cpp:719
@ PM_UNPAUSED
A normal unpaused game.
Definition openttd.h:69
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
@ PT_NORMAL
Normal pool containing game objects.
Definition pool_type.hpp:18
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
A number of safeguards to prevent using unsafe methods.
void InitializeScriptGui()
Reset the Script windows to their initial state.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:57
Declarations for accessing the k-d tree of stations.
Definition of base types and functions in a cross-platform compatible way.
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:81
TimerGameCalendar::Year starting_year
starting date
std::string savegame_id
Unique ID of the savegame.
Definition openttd.h:57
GameCreationSettings game_creation
settings used during the creation of a game (map)
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:36
static void Clean(PoolType)
Clean all pools of given type.
Definition pool_func.cpp:30
TileIndex redsq
The tile that has to get a red selection.
Functions related to text effects.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Functions related to tile highlights.
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition of the game-calendar-timer.
Definition of the game-economy-timer.
Definition of the tick-based game-timer.
Declarations for accessing the k-d tree of towns.
void UnInitWindowSystem()
Close down the windowing system.
Definition window.cpp:1804
Window functions not directly related to making/drawing windows.