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

OpenTTD Source 1.11.0-RC1

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 "../ai/ai_gui.hpp"
17 #include "game_config.hpp"
18 #include "game_info.hpp"
19 #include "game_instance.hpp"
20 #include "game_text.hpp"
21 #include "game.hpp"
22
23 /* Convert all Game related classes to Squirrel data. */
24 #include "../script/api/game/game_includes.hpp"
25
26 #include "../safeguards.h"
27
28
29 GameInstance::GameInstance() :
30 ScriptInstance("GS")
31 {}
32
33 void GameInstance::Initialize(GameInfo *info)
34 {
35 this->versionAPI = info->GetAPIVersion();
36
37 /* Register the GameController */
38 SQGSController_Register(this->engine);
39
40 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
41 }
42
43 void GameInstance::RegisterAPI()
44 {
45 ScriptInstance::RegisterAPI();
46
47 /* Register all classes */
48 SQGS_RegisterAll(this->engine);
49
50 RegisterGameTranslation(this->engine);
51
52 if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
53 }
54
55 int GameInstance::GetSetting(const char *name)
56 {
57 return GameConfig::GetConfig()->GetSetting(name);
58 }
59
60 ScriptInfo *GameInstance::FindLibrary(const char *library, int version)
61 {
62 return (ScriptInfo *)Game::FindLibrary(library, version);
63 }
64
65 void GameInstance::Died()
66 {
67 ScriptInstance::Died();
68
69 ShowAIDebugWindow(OWNER_DEITY);
70
71 const GameInfo *info = Game::GetInfo();
72 if (info != nullptr) {
73 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
74
75 if (info->GetURL() != nullptr) {
76 ScriptLog::Info("Please report the error to the following URL:");
77 ScriptLog::Info(info->GetURL());
78 }
79 }
80 }
81
90 void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
91 {
92 if (Game::GetGameInstance()->DoCommandCallback(result, tile, p1, p2, cmd)) {
93 Game::GetGameInstance()->Continue();
94 }
95 }
96
97 CommandCallback *GameInstance::GetDoCommandCallback()
98 {
99 return &CcGame;
100 }
game.hpp
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:215
game_instance.hpp
Game::GetGameInstance
static class GameInstance * GetGameInstance()
Get the current GameScript instance.
Definition: game.hpp:75
ScriptInfo::GetMainScript
const char * GetMainScript() const
Get the filename of the main.nut script.
Definition: script_info.hpp:90
game_info.hpp
GameInfo::GetAPIVersion
const char * GetAPIVersion() const
Get the API version this Game is written for.
Definition: game_info.hpp:39
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:23
Game::GetInfo
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition: game.hpp:80
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition: script_instance.hpp:23
GameInstance::FindLibrary
ScriptInfo * FindLibrary(const char *library, int version) override
Find a library.
Definition: game_instance.cpp:60
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:474
ScriptInfo::GetURL
const char * GetURL() const
Get the website for this script.
Definition: script_info.hpp:85
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:372
CommandCost
Common return value for all commands.
Definition: command_type.h:23
GameInstance::Initialize
void Initialize(class GameInfo *info)
Initialize the script and prepare it for its first run.
Definition: game_instance.cpp:33
RegisterGameTranslation
void RegisterGameTranslation(Squirrel *engine)
Register the current translation to the Squirrel engine.
Definition: game_text.cpp:319
GameInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition: game_instance.cpp:43
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition: fileio_type.h:121
CcGame
void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by Game Scripts.
Definition: game_instance.cpp:90
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:109
GameInstance::Died
void Died() override
Tell the script it died.
Definition: game_instance.cpp:65
ScriptInstance::Initialize
void Initialize(const char *main_script, const char *instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition: script_instance.cpp:69
ScriptInfo::GetInstanceName
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:80
GameInstance::GetDoCommandCallback
CommandCallback * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition: game_instance.cpp:97
game_text.hpp
ScriptConfig::GetSetting
virtual int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: script_config.cpp:97
GameConfig::GetConfig
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:18
ShowAIDebugWindow
Window * ShowAIDebugWindow(CompanyID show_company)
Open the AI debug window and select the given company.
Definition: ai_gui.cpp:1535
GameInfo
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
GameInstance::GetSetting
int GetSetting(const char *name) override
Get the value of a setting of the current instance.
Definition: game_instance.cpp:55
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:154
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle his DoCommand.
Definition: script_instance.cpp:148
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:115
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
ScriptInstance::versionAPI
const char * versionAPI
Current API used by this script.
Definition: script_instance.hpp:216
game_config.hpp
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
Game::FindLibrary
static class GameLibrary * FindLibrary(const char *library, int version)
Wrapper function for GameScanner::FindLibrary.
Definition: game_core.cpp:253