2011-12-19 20:50:54 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file game_instance.cpp Implementation of GameInstance. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
2012-01-02 22:44:28 +00:00
|
|
|
#include "../error.h"
|
2011-12-19 20:50:54 +00:00
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
|
|
|
|
|
|
|
#include "../script/script_storage.hpp"
|
2021-11-29 23:52:23 +00:00
|
|
|
#include "../script/script_cmd.h"
|
2023-02-04 03:17:55 +00:00
|
|
|
#include "../script/script_gui.h"
|
2011-12-19 20:55:56 +00:00
|
|
|
#include "game_config.hpp"
|
|
|
|
#include "game_info.hpp"
|
2011-12-19 20:50:54 +00:00
|
|
|
#include "game_instance.hpp"
|
2011-12-19 21:05:46 +00:00
|
|
|
#include "game_text.hpp"
|
2011-12-19 20:50:54 +00:00
|
|
|
#include "game.hpp"
|
|
|
|
|
2019-03-12 14:37:57 +00:00
|
|
|
/* Convert all Game related classes to Squirrel data. */
|
|
|
|
#include "../script/api/game/game_includes.hpp"
|
2011-12-19 20:50:54 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-12-19 20:50:54 +00:00
|
|
|
|
|
|
|
GameInstance::GameInstance() :
|
|
|
|
ScriptInstance("GS")
|
|
|
|
{}
|
|
|
|
|
2011-12-19 20:55:56 +00:00
|
|
|
void GameInstance::Initialize(GameInfo *info)
|
2011-12-19 20:50:54 +00:00
|
|
|
{
|
2012-08-13 19:22:26 +00:00
|
|
|
this->versionAPI = info->GetAPIVersion();
|
|
|
|
|
2011-12-19 20:50:54 +00:00
|
|
|
/* Register the GameController */
|
|
|
|
SQGSController_Register(this->engine);
|
|
|
|
|
2011-12-19 21:05:25 +00:00
|
|
|
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
|
2011-12-19 20:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameInstance::RegisterAPI()
|
|
|
|
{
|
|
|
|
ScriptInstance::RegisterAPI();
|
|
|
|
|
2019-03-12 14:37:57 +00:00
|
|
|
/* Register all classes */
|
|
|
|
SQGS_RegisterAll(this->engine);
|
2011-12-19 20:50:54 +00:00
|
|
|
|
2011-12-19 21:05:46 +00:00
|
|
|
RegisterGameTranslation(this->engine);
|
2012-08-13 19:22:26 +00:00
|
|
|
|
|
|
|
if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
|
2011-12-19 20:50:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 08:07:54 +00:00
|
|
|
int GameInstance::GetSetting(const std::string &name)
|
2011-12-19 20:50:54 +00:00
|
|
|
{
|
2011-12-19 20:55:56 +00:00
|
|
|
return GameConfig::GetConfig()->GetSetting(name);
|
2011-12-19 20:50:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:53:34 +00:00
|
|
|
ScriptInfo *GameInstance::FindLibrary(const std::string &library, int version)
|
2011-12-19 20:50:54 +00:00
|
|
|
{
|
2011-12-19 20:56:59 +00:00
|
|
|
return (ScriptInfo *)Game::FindLibrary(library, version);
|
2011-12-19 20:50:54 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 22:44:28 +00:00
|
|
|
void GameInstance::Died()
|
|
|
|
{
|
|
|
|
ScriptInstance::Died();
|
|
|
|
|
2023-02-04 03:17:55 +00:00
|
|
|
ShowScriptDebugWindow(OWNER_DEITY);
|
2012-01-02 22:44:28 +00:00
|
|
|
|
|
|
|
const GameInfo *info = Game::GetInfo();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (info != nullptr) {
|
2012-01-02 22:44:28 +00:00
|
|
|
ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
|
|
|
|
|
2023-05-06 07:44:35 +00:00
|
|
|
if (!info->GetURL().empty()) {
|
2012-01-02 22:44:28 +00:00
|
|
|
ScriptLog::Info("Please report the error to the following URL:");
|
|
|
|
ScriptLog::Info(info->GetURL());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 20:50:54 +00:00
|
|
|
/**
|
|
|
|
* DoCommand callback function for all commands executed by Game Scripts.
|
2021-10-10 15:20:27 +00:00
|
|
|
* @param cmd cmd as given to DoCommandPInternal.
|
2021-10-31 21:07:22 +00:00
|
|
|
* @param result The result of the command.
|
|
|
|
* @param data Command data as given to Command<>::Post.
|
2021-11-29 23:52:23 +00:00
|
|
|
* @param result_data Additional returned data from the command.
|
2011-12-19 20:50:54 +00:00
|
|
|
*/
|
2023-02-24 21:50:11 +00:00
|
|
|
void CcGame(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
|
2011-12-19 20:50:54 +00:00
|
|
|
{
|
2023-02-24 21:50:11 +00:00
|
|
|
if (Game::GetGameInstance()->DoCommandCallback(result, data, std::move(result_data), cmd)) {
|
2019-09-07 16:37:01 +00:00
|
|
|
Game::GetGameInstance()->Continue();
|
|
|
|
}
|
2011-12-19 20:50:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 21:43:38 +00:00
|
|
|
CommandCallbackData *GameInstance::GetDoCommandCallback()
|
2011-12-19 20:50:54 +00:00
|
|
|
{
|
|
|
|
return &CcGame;
|
|
|
|
}
|