1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Norbert 4971b51781
Merge f02b13d52b into 7bb4940ebd 2025-07-19 08:29:39 +00:00
Peter Nelson 7bb4940ebd
Codechange: Use unique_ptr for all pointers in script instance. (#14339)
Removes manual memory management with new/delete.
2025-07-19 09:29:30 +01:00
npabisz f02b13d52b Add: [Script] Text effect 2025-04-27 13:30:23 -04:00
13 changed files with 270 additions and 26 deletions

View File

@ -500,6 +500,8 @@ add_files(
textbuf_type.h
texteff.cpp
texteff.hpp
texteff_cmd.cpp
texteff_cmd.h
textfile_gui.cpp
textfile_gui.h
textfile_type.h

View File

@ -83,7 +83,7 @@ void AIInstance::Died()
void AIInstance::LoadDummyScript()
{
ScriptAllocatorScope alloc_scope(this->engine);
ScriptAllocatorScope alloc_scope(this->engine.get());
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
}

View File

@ -46,6 +46,7 @@
#include "station_cmd.h"
#include "story_cmd.h"
#include "subsidy_cmd.h"
#include "texteff_cmd.h"
#include "terraform_cmd.h"
#include "timetable_cmd.h"
#include "town_cmd.h"

View File

@ -372,6 +372,10 @@ enum Commands : uint8_t {
CMD_UPDATE_LEAGUE_TABLE_ELEMENT_SCORE, ///< update the score of a league table element
CMD_REMOVE_LEAGUE_TABLE_ELEMENT, ///< remove a league table element
CMD_CREATE_TEXT_EFFECT, ///< create a new text effect
CMD_UPDATE_TEXT_EFFECT, ///< update text effect
CMD_REMOVE_TEXT_EFFECT, ///< remove text effect
CMD_END, ///< Must ALWAYS be on the end of this list!! (period)
};

View File

@ -40,6 +40,7 @@
#include "../station_cmd.h"
#include "../story_cmd.h"
#include "../subsidy_cmd.h"
#include "../texteff_cmd.h"
#include "../terraform_cmd.h"
#include "../timetable_cmd.h"
#include "../town_cmd.h"

View File

@ -206,6 +206,7 @@ add_files(
script_subsidylist.hpp
script_testmode.hpp
script_text.hpp
script_text_effect.hpp
script_tile.hpp
script_tilelist.hpp
script_town.hpp
@ -278,6 +279,7 @@ add_files(
script_subsidylist.cpp
script_testmode.cpp
script_text.cpp
script_text_effect.cpp
script_tile.cpp
script_tilelist.cpp
script_town.cpp

View File

@ -53,7 +53,7 @@ static ScriptStorage &GetStorage()
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine)
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine.get())
{
this->last_active = ScriptObject::ActiveInstance::active;
ScriptObject::ActiveInstance::active = &instance;
@ -230,8 +230,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ bool ScriptObject::CanSuspend()
{
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine;
return GetStorage().allow_do_command && squirrel->CanSuspend();
Squirrel &squirrel = *ScriptObject::GetActiveInstance().engine;
return GetStorage().allow_do_command && squirrel.CanSuspend();
}
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue()

View File

@ -0,0 +1,63 @@
/*
* 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 script_text_effect.cpp Implementation of ScriptTextEffect with multiplayer support. */
#include "../../stdafx.h"
#include "script_text_effect.hpp"
#include "script_error.hpp"
#include "script_map.hpp"
#include "../script_instance.hpp"
#include "../../texteff.hpp"
#include "../../strings_func.h"
#include "../../tile_map.h"
#include "../../command_func.h"
#include "../../texteff_cmd.h"
#include "../../safeguards.h"
/* static */ TextEffectID ScriptTextEffect::CreateAtPosition(SQInteger x, SQInteger y, Text *text, ScriptTextEffectMode mode)
{
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, text != nullptr);
EnforcePrecondition(false, !text->GetEncodedText().empty());
EnforcePrecondition(false, mode == TE_RISING || mode == TE_STATIC);
return ScriptObject::Command<CMD_CREATE_TEXT_EFFECT>::Do(&ScriptInstance::DoCommandReturnTextEffectID, x, y, (TextEffectMode)mode, text->GetEncodedText());
}
/* static */ TextEffectID ScriptTextEffect::Create(TileIndex tile, Text *text, ScriptTextEffectMode mode)
{
EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
int y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
return CreateAtPosition(x, y, text, mode);
}
/* static */ bool ScriptTextEffect::Update(TextEffectID te_id, Text *text)
{
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, te_id != INVALID_TE_ID);
EnforcePrecondition(false, text != nullptr);
EnforcePrecondition(false, !text->GetEncodedText().empty());
return ScriptObject::Command<CMD_UPDATE_TEXT_EFFECT>::Do(te_id, text->GetEncodedText());
}
/* static */ bool ScriptTextEffect::Remove(TextEffectID te_id)
{
EnforceDeityMode(false);
EnforcePrecondition(false, te_id != INVALID_TE_ID);
return ScriptObject::Command<CMD_REMOVE_TEXT_EFFECT>::Do(te_id);
}

View File

@ -0,0 +1,66 @@
/*
* 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 script_text_effect.hpp Everything to display animated text in the game world. */
#ifndef SCRIPT_TEXT_EFFECT_HPP
#define SCRIPT_TEXT_EFFECT_HPP
#include "script_object.hpp"
#include "script_text.hpp"
#include "../../texteff.hpp"
/**
* Class that handles text effect display in the game world.
* @api game
*/
class ScriptTextEffect : public ScriptObject {
public:
/**
* Text effect animation modes.
*/
enum ScriptTextEffectMode {
TE_RISING = ::TE_RISING, ///< Text slowly rises upwards
TE_STATIC = ::TE_STATIC, ///< Text stays in place
};
/**
* Create animated text at a tile location.
* @param tile The tile where to show the text.
* @param text The text to display.
* @param mode The animation mode to use.
* @return True if the text effect was created successfully.
*/
static TextEffectID Create(TileIndex tile, Text *text, ScriptTextEffectMode mode);
/**
* Create animated text at the specified location.
* @param x X coordinate in the game world.
* @param y Y coordinate in the game world.
* @param text The text to display.
* @param mode The animation mode to use.
* @return True if the text effect was created successfully.
*/
static TextEffectID CreateAtPosition(SQInteger x, SQInteger y, Text *text, ScriptTextEffectMode mode);
/**
* Update animated text
* @param te_id Text effect ID.
* @param text The text to update
* @return True if the text effect was updated successfully
*/
static bool Update(TextEffectID te_id, Text *text);
/**
* Update animated text
* @param te_id Text effect ID.
* @return True if the text effect was removed successfully
*/
static bool Remove(TextEffectID te_id);
};
#endif /* SCRIPT_TEXT_EFFECT_HPP */

View File

@ -31,6 +31,7 @@
#include "../signs_type.h"
#include "../story_type.h"
#include "../misc/endian_buffer.hpp"
#include "../texteff.hpp"
#include "../safeguards.h"
@ -50,8 +51,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
ScriptInstance::ScriptInstance(std::string_view api_name)
{
this->storage = new ScriptStorage();
this->engine = new Squirrel(api_name);
this->storage = std::make_unique<ScriptStorage>();
this->engine = std::make_unique<Squirrel>(api_name);
this->engine->SetPrintFunction(&PrintFunc);
}
@ -59,10 +60,10 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
{
ScriptObject::ActiveInstance active(*this);
this->controller = new ScriptController(company);
this->controller = std::make_unique<ScriptController>(company);
/* Register the API functions and classes */
this->engine->SetGlobalPointer(this->engine);
this->engine->SetGlobalPointer(this->engine.get());
this->RegisterAPI();
if (this->IsDead()) {
/* Failed to register API; a message has already been logged. */
@ -81,12 +82,11 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
}
/* Create the main-class */
this->instance = new SQObject();
if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) {
this->instance = std::make_unique<SQObject>();
if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
/* If CreateClassInstance has returned false instance has not been
* registered with squirrel, so avoid trying to Release it by clearing it now */
delete this->instance;
this->instance = nullptr;
this->instance.reset();
this->Died();
return;
}
@ -158,11 +158,10 @@ ScriptInstance::~ScriptInstance()
ScriptObject::ActiveInstance active(*this);
this->in_shutdown = true;
if (instance != nullptr) this->engine->ReleaseObject(this->instance);
if (engine != nullptr) delete this->engine;
delete this->storage;
delete this->controller;
delete this->instance;
if (instance != nullptr) this->engine->ReleaseObject(this->instance.get());
/* Engine must be reset explicitly in scope of the active instance. */
this->engine.reset();
}
void ScriptInstance::Continue()
@ -179,11 +178,9 @@ void ScriptInstance::Died()
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance);
delete this->instance;
delete this->engine;
this->instance = nullptr;
this->engine = nullptr;
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance.get());
this->engine.reset();
this->instance.reset();
}
void ScriptInstance::GameLoop()
@ -329,6 +326,10 @@ void ScriptInstance::CollectGarbage()
instance.engine->InsertResult(EndianBufferReader::ToValue<LeagueTableID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnTextEffectID(ScriptInstance *instance)
{
instance->engine->InsertResult(EndianBufferReader::ToValue<TextEffectID>(ScriptObject::GetLastCommandResData()));
}
ScriptStorage &ScriptInstance::GetStorage()
{

View File

@ -143,6 +143,11 @@ public:
*/
static void DoCommandReturnLeagueTableElementID(ScriptInstance &instance);
/**
* Return a TextEffectID reply for a DoCommand.
*/
static void DoCommandReturnTextEffectID(ScriptInstance *instance);
/**
* Get the controller attached to the instance.
*/
@ -256,7 +261,7 @@ public:
void ReleaseSQObject(HSQOBJECT *obj);
protected:
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm.
std::unique_ptr<class Squirrel> engine; ///< A wrapper around the squirrel vm.
std::string api_version{}; ///< Current API used by this script.
/**
@ -288,9 +293,9 @@ protected:
virtual void LoadDummyScript() = 0;
private:
class ScriptController *controller = nullptr; ///< The script main class.
class ScriptStorage *storage = nullptr; ///< Some global information for each running script.
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class.
std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
std::unique_ptr<class ScriptController> controller; ///< The script main class.
std::unique_ptr<SQObject> instance; ///< Squirrel-pointer to the script main class.
bool is_started = false; ///< Is the scripts constructor executed?
bool is_dead = false; ///< True if the script has been stopped.

View File

@ -0,0 +1,75 @@
/*
* 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 texteff_cmd.cpp Command handling for text effects */
#include "stdafx.h"
#include "command_func.h"
#include "texteff.hpp"
#include "strings_func.h"
#include "tile_map.h"
#include "texteff_cmd.h"
#include "table/strings.h"
#include "safeguards.h"
#include "landscape.h"
/**
* Show a text effect at the specified location.
* @param flags operation to perform
* @param x X coordinate in the game
* @param y Y coordinate in the game
* @param mode The animation mode to use
* @param text The text to display
* @return the cost of this operation or an error
*/
std::tuple<CommandCost, TextEffectID> CmdCreateTextEffect(DoCommandFlags flags, int32_t x, int32_t y, TextEffectMode mode, const EncodedString &text)
{
if (text.empty()) return { CMD_ERROR, INVALID_TE_ID };
if (mode != TE_RISING && mode != TE_STATIC) return { CMD_ERROR, INVALID_TE_ID };
if (flags.Test(DoCommandFlag::Execute)) {
Point pt = RemapCoords2(x, y);
EncodedString encoded_text = text;
TextEffectID te_id;
if (mode == TE_RISING) {
te_id = AddTextEffect(std::move(encoded_text), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
} else {
te_id = AddTextEffect(std::move(encoded_text), pt.x, pt.y, 0, TE_STATIC);
}
return { CommandCost(), te_id };
}
return { CommandCost(), INVALID_TE_ID };
}
CommandCost CmdUpdateTextEffect(DoCommandFlags flags, TextEffectID te_id, const EncodedString &text)
{
if (te_id == INVALID_TE_ID) return CMD_ERROR;
if (text.empty()) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) {
EncodedString encoded_text = text;
UpdateTextEffect(te_id, std::move(encoded_text));
}
return CommandCost();
}
CommandCost CmdRemoveTextEffect(DoCommandFlags flags, TextEffectID te_id)
{
if (te_id == INVALID_TE_ID) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) {
RemoveTextEffect(te_id);
}
return CommandCost();
}

24
src/texteff_cmd.h 100644
View File

@ -0,0 +1,24 @@
/*
* 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 texteff_cmd.h Command declarations for text effects */
#ifndef TEXTEFF_CMD_H
#define TEXTEFF_CMD_H
#include "command_type.h"
#include "texteff.hpp"
std::tuple<CommandCost, TextEffectID> CmdCreateTextEffect(DoCommandFlags flags, int32_t x, int32_t y, TextEffectMode mode, const EncodedString &text);
CommandCost CmdUpdateTextEffect(DoCommandFlags flags, TextEffectID te_id, const EncodedString &text);
CommandCost CmdRemoveTextEffect(DoCommandFlags flags, TextEffectID te_id);
DEF_CMD_TRAIT(CMD_CREATE_TEXT_EFFECT, CmdCreateTextEffect, CommandFlags({CommandFlag::Deity, CommandFlag::StrCtrl}), CMDT_OTHER_MANAGEMENT)
DEF_CMD_TRAIT(CMD_UPDATE_TEXT_EFFECT, CmdUpdateTextEffect, CommandFlags({CommandFlag::Deity, CommandFlag::StrCtrl}), CMDT_OTHER_MANAGEMENT)
DEF_CMD_TRAIT(CMD_REMOVE_TEXT_EFFECT, CmdRemoveTextEffect, CommandFlag::Deity, CMDT_OTHER_MANAGEMENT)
#endif /* TEXTEFF_CMD_H */