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

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
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/* Convert all Game related classes to Squirrel data. */
25#include "../script/api/game/game_includes.hpp"
26
27#include "../safeguards.h"
28
29
30GameInstance::GameInstance() :
31 ScriptInstance("GS")
32{}
33
34 void GameInstance::Initialize(GameInfo *info)
35{
36 this->versionAPI = info->GetAPIVersion();
37
38 /* Register the GameController */
39 SQGSController_Register(this->engine);
40
41 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
42}
43
44 void GameInstance::RegisterAPI()
45{
46 ScriptInstance::RegisterAPI();
47
48 /* Register all classes */
49 SQGS_RegisterAll(this->engine);
50
51 RegisterGameTranslation(this->engine);
52
53 if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
54}
55
56 int GameInstance::GetSetting(const std::string &name)
57{
58 return GameConfig::GetConfig()->GetSetting(name);
59}
60
61 ScriptInfo *GameInstance::FindLibrary(const std::string &library, int version)
62{
63 return (ScriptInfo *)Game::FindLibrary(library, version);
64}
65
66 void GameInstance::Died()
67{
68 ScriptInstance::Died();
69
70 /* Don't show errors while loading savegame. They will be shown at end of loading anyway. */
71 if (_switch_mode != SM_NONE) return;
72
73 ShowScriptDebugWindow(OWNER_DEITY);
74
75 const GameInfo *info = Game::GetInfo();
76 if (info != nullptr) {
77 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
78
79 if (!info->GetURL().empty()) {
80 ScriptLog::Info("Please report the error to the following URL:");
81 ScriptLog::Info(info->GetURL());
82 }
83 }
84}
85
93 void CcGame(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
94{
95 if (Game::GetGameInstance()->DoCommandCallback(result, data, std::move(result_data), cmd)) {
96 Game::GetGameInstance()->Continue();
97 }
98}
99
100 CommandCallbackData *GameInstance::GetDoCommandCallback()
101{
102 return &CcGame;
103}
CommandCost
Common return value for all commands.
Definition command_type.h:23
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:38
GameInstance::GetSetting
int GetSetting(const std::string &name) override
Get the value of a setting of the current instance.
Definition game_instance.cpp:56
GameInstance::Died
void Died() override
Tell the script it died.
Definition game_instance.cpp:66
GameInstance::FindLibrary
ScriptInfo * FindLibrary(const std::string &library, int version) override
Find a library.
Definition game_instance.cpp:61
GameInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition game_instance.cpp:44
GameInstance::Initialize
void Initialize(class GameInfo *info)
Initialize the script and prepare it for its first run.
Definition game_instance.cpp:34
GameInstance::GetDoCommandCallback
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition game_instance.cpp:100
Game::GetInfo
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition game.hpp:75
Game::FindLibrary
static class GameLibrary * FindLibrary(const std::string &library, int version)
Wrapper function for GameScanner::FindLibrary.
Definition game_core.cpp:242
Game::GetGameInstance
static class GameInstance * GetGameInstance()
Get the current GameScript instance.
Definition game.hpp:70
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition script_config.cpp:82
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:71
ScriptInfo::GetURL
const std::string & GetURL() const
Get the website for this script.
Definition script_info.hpp:76
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition script_info.hpp:81
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition script_instance.hpp:25
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition script_instance.cpp:114
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition script_instance.cpp:156
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition script_instance.hpp:254
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const std::string &api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition script_instance.cpp:119
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:70
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition script_instance.cpp:150
ScriptInstance::versionAPI
std::string versionAPI
Current API used by this script.
Definition script_instance.hpp:255
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:500
CommandDataBuffer
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Definition command_type.h:470
Commands
Commands
List of commands.
Definition command_type.h:187
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:27
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition error_gui.cpp:367
WL_WARNING
@ WL_WARNING
Other information.
Definition error.h:25
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:128
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:356
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:1303
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition strings_type.h:17