2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file misc.cpp Misc functions that shouldn't be here. */
|
2007-03-03 05:04:22 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2007-04-12 15:07:15 +02:00
|
|
|
#include "landscape.h"
|
2008-03-28 09:53:36 +01:00
|
|
|
#include "news_func.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "ai/ai.hpp"
|
2023-02-04 04:17:55 +01:00
|
|
|
#include "script/script_gui.h"
|
2011-07-30 00:23:17 +02:00
|
|
|
#include "newgrf.h"
|
2007-03-19 12:27:30 +01:00
|
|
|
#include "newgrf_house.h"
|
2007-12-21 23:50:51 +01:00
|
|
|
#include "economy_func.h"
|
2023-05-04 15:14:12 +02:00
|
|
|
#include "timer/timer_game_calendar.h"
|
2024-01-22 15:04:34 +01:00
|
|
|
#include "timer/timer_game_economy.h"
|
2023-04-24 18:55:40 +02:00
|
|
|
#include "timer/timer_game_tick.h"
|
2007-12-27 14:35:39 +01:00
|
|
|
#include "texteff.hpp"
|
2008-01-09 10:45:45 +01:00
|
|
|
#include "gfx_func.h"
|
2008-06-03 20:35:58 +02:00
|
|
|
#include "gamelog.h"
|
2008-04-20 10:22:59 +02:00
|
|
|
#include "animated_tile_func.h"
|
2008-05-07 15:10:15 +02:00
|
|
|
#include "tilehighlight_func.h"
|
2008-08-12 00:45:11 +02:00
|
|
|
#include "network/network_func.h"
|
2009-01-24 00:40:12 +01:00
|
|
|
#include "window_func.h"
|
2011-02-20 00:05:47 +01:00
|
|
|
#include "core/pool_type.hpp"
|
2011-12-19 21:50:54 +01:00
|
|
|
#include "game/game.hpp"
|
2013-06-09 14:58:37 +02:00
|
|
|
#include "linkgraph/linkgraphschedule.h"
|
2019-02-18 21:14:52 +01:00
|
|
|
#include "station_kdtree.h"
|
2019-02-18 17:32:53 +01:00
|
|
|
#include "town_kdtree.h"
|
2019-02-22 17:57:28 +01:00
|
|
|
#include "viewport_kdtree.h"
|
2020-01-26 13:45:51 +01:00
|
|
|
#include "newgrf_profiling.h"
|
2024-01-18 23:52:53 +01:00
|
|
|
#include "3rdparty/monocypher/monocypher.h"
|
2008-01-09 10:45:45 +01:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2008-04-18 12:16:51 +02:00
|
|
|
extern TileIndex _cur_tileloop_tile;
|
2009-01-12 18:11:45 +01:00
|
|
|
extern void MakeNewgameSettingsLive();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-08-09 21:50:44 +02:00
|
|
|
void InitializeSound();
|
2009-12-22 22:40:29 +01:00
|
|
|
void InitializeMusic();
|
2007-03-07 12:47:46 +01:00
|
|
|
void InitializeVehicles();
|
|
|
|
void InitializeRailGui();
|
|
|
|
void InitializeRoadGui();
|
|
|
|
void InitializeAirportGui();
|
|
|
|
void InitializeDockGui();
|
2019-02-07 21:17:32 +01:00
|
|
|
void InitializeGraphGui();
|
2010-08-28 21:43:41 +02:00
|
|
|
void InitializeObjectGui();
|
2019-01-05 14:22:07 +01:00
|
|
|
void InitializeTownGui();
|
2007-03-07 12:47:46 +01:00
|
|
|
void InitializeIndustries();
|
2010-08-13 14:45:26 +02:00
|
|
|
void InitializeObjects();
|
2007-03-07 12:47:46 +01:00
|
|
|
void InitializeTrees();
|
2008-09-30 22:39:50 +02:00
|
|
|
void InitializeCompanies();
|
2008-04-17 23:21:01 +02:00
|
|
|
void InitializeCheats();
|
2008-04-19 10:21:55 +02:00
|
|
|
void InitializeOldNames();
|
2005-01-22 21:23:18 +01:00
|
|
|
|
2023-04-25 19:43:45 +02:00
|
|
|
/**
|
|
|
|
* Generate an unique ID.
|
|
|
|
*
|
2024-01-18 23:52:53 +01:00
|
|
|
* It isn't as much of an unique ID but more a hashed digest of a random
|
|
|
|
* string and a time. It is very likely to be unique, but it does not follow
|
|
|
|
* any UUID standard.
|
2023-04-25 19:43:45 +02:00
|
|
|
*/
|
|
|
|
std::string GenerateUid(std::string_view subject)
|
|
|
|
{
|
2024-01-18 23:52:53 +01:00
|
|
|
std::array<uint8_t, 32> random_bytes;
|
|
|
|
RandomBytesWithFallback(random_bytes);
|
|
|
|
|
2023-04-25 19:43:45 +02:00
|
|
|
auto current_time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
2024-01-18 23:52:53 +01:00
|
|
|
std::string coding_string = fmt::format("{}{}", current_time, subject);
|
2023-04-25 19:43:45 +02:00
|
|
|
|
2024-01-18 23:52:53 +01:00
|
|
|
std::array<uint8_t, 16> digest;
|
|
|
|
crypto_blake2b_ctx ctx;
|
|
|
|
crypto_blake2b_init(&ctx, digest.size());
|
|
|
|
crypto_blake2b_update(&ctx, random_bytes.data(), random_bytes.size());
|
|
|
|
crypto_blake2b_update(&ctx, reinterpret_cast<const uint8_t *>(coding_string.data()), coding_string.size());
|
|
|
|
crypto_blake2b_final(&ctx, digest.data());
|
2023-04-25 19:43:45 +02:00
|
|
|
|
2023-05-18 22:41:42 +02:00
|
|
|
return FormatArrayAsHex(digest);
|
2023-04-25 19:43:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an unique savegame ID.
|
|
|
|
*/
|
|
|
|
void GenerateSavegameId()
|
|
|
|
{
|
2024-03-16 08:58:56 +01:00
|
|
|
_game_session_stats.savegame_id = GenerateUid("OpenTTD Savegame ID");
|
2023-04-25 19:43:45 +02:00
|
|
|
}
|
|
|
|
|
2009-08-07 00:00:32 +02:00
|
|
|
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2009-01-24 00:40:12 +01:00
|
|
|
/* Make sure there isn't any window that can influence anything
|
|
|
|
* related to the new game we're about to start/load. */
|
|
|
|
UnInitWindowSystem();
|
|
|
|
|
2023-01-21 12:14:28 +01:00
|
|
|
Map::Allocate(size_x, size_y);
|
2005-07-13 21:51:31 +02:00
|
|
|
|
2009-05-06 17:06:57 +02:00
|
|
|
_pause_mode = PM_UNPAUSED;
|
2021-02-28 15:41:03 +01:00
|
|
|
_game_speed = 100;
|
2023-04-24 18:55:40 +02:00
|
|
|
TimerGameTick::counter = 0;
|
2012-04-17 21:43:43 +02:00
|
|
|
_cur_tileloop_tile = 1;
|
2009-01-19 16:06:11 +01:00
|
|
|
_thd.redsq = INVALID_TILE;
|
2009-08-07 00:00:32 +02:00
|
|
|
if (reset_settings) MakeNewgameSettingsLive();
|
2004-09-11 11:55:19 +02:00
|
|
|
|
2020-01-26 13:45:51 +01:00
|
|
|
_newgrf_profilers.clear();
|
|
|
|
|
2008-05-29 11:54:47 +02:00
|
|
|
if (reset_date) {
|
2024-01-22 15:04:34 +01:00
|
|
|
TimerGameCalendar::Date new_date = TimerGameCalendar::ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
|
|
|
|
TimerGameCalendar::SetDate(new_date, 0);
|
2024-01-23 17:36:09 +01:00
|
|
|
|
|
|
|
if (TimerGameEconomy::UsingWallclockUnits()) {
|
|
|
|
/* If using wallclock units, start at year 1. */
|
|
|
|
TimerGameEconomy::SetDate(TimerGameEconomy::ConvertYMDToDate(1, 0, 1), 0);
|
|
|
|
} else {
|
|
|
|
/* Otherwise, we always keep the economy date synced with the calendar date. */
|
|
|
|
TimerGameEconomy::SetDate(new_date.base(), 0);
|
|
|
|
}
|
2008-05-05 00:19:44 +02:00
|
|
|
InitializeOldNames();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2013-06-09 14:58:37 +02:00
|
|
|
LinkGraphSchedule::Clear();
|
2011-02-20 00:05:47 +01:00
|
|
|
PoolBase::Clean(PT_NORMAL);
|
|
|
|
|
2019-02-18 21:14:52 +01:00
|
|
|
RebuildStationKdtree();
|
2019-02-18 17:32:53 +01:00
|
|
|
RebuildTownKdtree();
|
2019-02-22 17:57:28 +01:00
|
|
|
RebuildViewportKdtree();
|
2019-02-18 17:32:53 +01:00
|
|
|
|
2011-07-30 00:23:17 +02:00
|
|
|
ResetPersistentNewGRFData();
|
|
|
|
|
2010-08-22 11:18:01 +02:00
|
|
|
InitializeSound();
|
|
|
|
InitializeMusic();
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
InitializeVehicles();
|
|
|
|
|
|
|
|
InitNewsItemStructs();
|
2005-07-13 21:51:31 +02:00
|
|
|
InitializeLandscape();
|
2004-08-09 19:04:08 +02:00
|
|
|
InitializeRailGui();
|
|
|
|
InitializeRoadGui();
|
|
|
|
InitializeAirportGui();
|
|
|
|
InitializeDockGui();
|
2019-02-07 21:17:32 +01:00
|
|
|
InitializeGraphGui();
|
2010-08-28 21:43:41 +02:00
|
|
|
InitializeObjectGui();
|
2019-01-05 14:22:07 +01:00
|
|
|
InitializeTownGui();
|
2023-02-04 04:17:55 +01:00
|
|
|
InitializeScriptGui();
|
2004-08-09 19:04:08 +02:00
|
|
|
InitializeTrees();
|
|
|
|
InitializeIndustries();
|
2010-08-13 14:45:26 +02:00
|
|
|
InitializeObjects();
|
2004-09-11 11:55:19 +02:00
|
|
|
|
2008-09-30 22:39:50 +02:00
|
|
|
InitializeCompanies();
|
2009-01-12 18:11:45 +01:00
|
|
|
AI::Initialize();
|
2011-12-19 21:50:54 +01:00
|
|
|
Game::Initialize();
|
2004-08-09 19:04:08 +02:00
|
|
|
InitializeCheats();
|
|
|
|
|
|
|
|
InitTextEffects();
|
2008-08-12 00:45:11 +02:00
|
|
|
NetworkInitChatMessage();
|
2004-08-09 19:04:08 +02:00
|
|
|
InitializeAnimatedTiles();
|
|
|
|
|
2009-09-06 20:53:57 +02:00
|
|
|
InitializeEconomy();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
ResetObjectToPlace();
|
2008-06-03 20:35:58 +02:00
|
|
|
|
2023-05-01 19:14:31 +02:00
|
|
|
_gamelog.Reset();
|
|
|
|
_gamelog.StartAction(GLAT_START);
|
|
|
|
_gamelog.Revision();
|
|
|
|
_gamelog.Mode();
|
|
|
|
_gamelog.GRFAddList(_grfconfig);
|
|
|
|
_gamelog.StopAction();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|