OpenTTD Source: src/game/game_instance.cpp Source File

OpenTTD Source 15.0-RC2

game_instance.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 "../error.h"
12
13#include "../script/squirrel_class.hpp"
14
15#include "../script/script_storage.hpp"
16#include "../script/script_cmd.h"
17#include "../script/script_gui.h"
18#include "game_config.hpp"
19#include "game_info.hpp"
20#include "game_instance.hpp"
21#include "game_text.hpp"
22#include "game.hpp"
23
24#include "table/strings.h"
25
26/* Convert all Game related classes to Squirrel data. */
27#include "../script/api/game/game_includes.hpp"
28
29#include "../safeguards.h"
30
31
32GameInstance::GameInstance() :
33 ScriptInstance("GS")
34{}
35
36 void GameInstance::Initialize(GameInfo *info)
37{
38 this->api_version = info->GetAPIVersion();
39
40 /* Register the GameController */
41 SQGSController_Register(*this->engine);
42
43 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
44}
45
46 void GameInstance::RegisterAPI()
47{
48 ScriptInstance::RegisterAPI();
49
50 /* Register all classes */
51 SQGS_RegisterAll(*this->engine);
52
53 if (!this->LoadCompatibilityScripts(GAME_DIR, GameInfo::ApiVersions)) this->Died();
54
55 if (this->IsAlive()) RegisterGameTranslation(*this->engine);
56}
57
58 int GameInstance::GetSetting(const std::string &name)
59{
60 return GameConfig::GetConfig()->GetSetting(name);
61}
62
63 ScriptInfo *GameInstance::FindLibrary(const std::string &library, int version)
64{
65 return (ScriptInfo *)Game::FindLibrary(library, version);
66}
67
68 void GameInstance::Died()
69{
70 ScriptInstance::Died();
71
72 /* Don't show errors while loading savegame. They will be shown at end of loading anyway. */
73 if (_switch_mode != SM_NONE) return;
74
75 ShowScriptDebugWindow(OWNER_DEITY);
76
77 const GameInfo *info = Game::GetInfo();
78 if (info != nullptr) {
79 ShowErrorMessage(GetEncodedString(STR_ERROR_AI_PLEASE_REPORT_CRASH), {}, WL_WARNING);
80
81 if (!info->GetURL().empty()) {
82 ScriptLog::Info("Please report the error to the following URL:");
83 ScriptLog::Info(info->GetURL());
84 }
85 }
86}
87
95 void CcGame(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
96{
97 if (Game::GetInstance()->DoCommandCallback(result, data, std::move(result_data), cmd)) {
98 Game::GetInstance()->Continue();
99 }
100}
101
102 CommandCallbackData *GameInstance::GetDoCommandCallback()
103{
104 return &CcGame;
105}
CommandCost
Common return value for all commands.
Definition command_type.h:24
GameConfig::GetConfig
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition game_config.cpp:18
GameInfo
All static information from an Game like name, version, etc.
Definition game_info.hpp:16
GameInfo::GetAPIVersion
const std::string & GetAPIVersion() const
Get the API version this Game is written for.
Definition game_info.hpp:41
GameInstance::GetSetting
int GetSetting(const std::string &name) override
Get the value of a setting of the current instance.
Definition game_instance.cpp:58
GameInstance::Died
void Died() override
Tell the script it died.
Definition game_instance.cpp:68
GameInstance::FindLibrary
ScriptInfo * FindLibrary(const std::string &library, int version) override
Find a library.
Definition game_instance.cpp:63
GameInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition game_instance.cpp:46
GameInstance::Initialize
void Initialize(class GameInfo *info)
Initialize the script and prepare it for its first run.
Definition game_instance.cpp:36
GameInstance::GetDoCommandCallback
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition game_instance.cpp:102
Game::GetInfo
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition game.hpp:70
Game::FindLibrary
static class GameLibrary * FindLibrary(const std::string &library, int version)
Wrapper function for GameScanner::FindLibrary.
Definition game_core.cpp:228
Game::GetInstance
static class GameInstance * GetInstance()
Get the current active instance.
Definition game.hpp:96
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition script_config.cpp:83
ScriptInfo
All static information from an Script like name, version, etc.
Definition script_info.hpp:30
ScriptInfo::GetInstanceName
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
Definition script_info.hpp:65
ScriptInfo::GetURL
const std::string & GetURL() const
Get the website for this script.
Definition script_info.hpp:70
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition script_info.hpp:75
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition script_instance.hpp:25
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(Subdirectory dir, std::span< const std::string_view > api_versions)
Load squirrel scripts to emulate an older API.
Definition script_instance.cpp:125
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition script_instance.cpp:100
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition script_instance.cpp:172
ScriptInstance::engine
std::unique_ptr< class Squirrel > engine
A wrapper around the squirrel vm.
Definition script_instance.hpp:259
ScriptInstance::api_version
std::string api_version
Current API used by this script.
Definition script_instance.hpp:260
ScriptInstance::Initialize
void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition script_instance.cpp:58
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition script_instance.cpp:166
ScriptInstance::IsAlive
bool IsAlive() const
Return whether the script is alive.
Definition script_instance.hpp:163
CommandCallbackData
void CommandCallbackData(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
Define a callback function for the client, after the command is finished.
Definition command_type.h:511
CommandDataBuffer
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Definition command_type.h:481
Commands
Commands
List of commands.
Definition command_type.h:196
OWNER_DEITY
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:28
WL_WARNING
@ WL_WARNING
Other information.
Definition error.h:25
ShowErrorMessage
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
Definition error_gui.cpp:290
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:101
game.hpp
Base functions for all Games.
game_config.hpp
GameConfig stores the configuration settings of every Game.
game_info.hpp
GameInfo keeps track of all information of an Game, like Author, Description, ...
game_instance.hpp
The GameInstance tracks games.
RegisterGameTranslation
void RegisterGameTranslation(Squirrel &engine)
Register the current translation to the Squirrel engine.
Definition game_text.cpp:353
game_text.hpp
Base functions regarding game texts.
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:49
ShowScriptDebugWindow
Window * ShowScriptDebugWindow(CompanyID show_company, bool new_window)
Open the Script debug window and select the given company.
Definition script_gui.cpp:1264
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90