1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Nelson 3a8cd6aa27
Merge 13f24c25ca 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
Peter Nelson 13f24c25ca
Add: Variable to test for a badge on a nearby tile. 2025-05-21 22:10:13 +01:00
13 changed files with 218 additions and 28 deletions

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

@ -193,6 +193,7 @@ static uint32_t GetAirportTileIDAtOffset(TileIndex tile, const Station *st, uint
/* Get airport tile ID at offset */
case 0x62: return GetAirportTileIDAtOffset(GetNearbyTile(parameter, this->tile), this->st, this->ro.grffile->grfid);
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->ats->badges, parameter);
}

View File

@ -8,13 +8,25 @@
/** @file newgrf_badge.cpp Functionality for NewGRF badges. */
#include "stdafx.h"
#include "house.h"
#include "industry_map.h"
#include "newgrf.h"
#include "newgrf_airporttiles.h"
#include "newgrf_badge.h"
#include "newgrf_badge_type.h"
#include "newgrf_object.h"
#include "newgrf_roadstop.h"
#include "newgrf_station.h"
#include "newgrf_spritegroup.h"
#include "rail.h"
#include "rail_map.h"
#include "station_map.h"
#include "stringfilter_type.h"
#include "strings_func.h"
#include "tile_map.h"
#include "timer/timer_game_calendar.h"
#include "town_map.h"
#include "tunnelbridge_map.h"
#include "table/strings.h"
@ -216,7 +228,173 @@ BadgeResolverObject::BadgeResolverObject(const Badge &badge, GrfSpecFeature feat
}
/**
* Test for a matching badge in a list of badges, returning the number of matching bits.
* Test if a list of badges contains a badge.
* @param badges List of badges.
* @param badge Badge to find.
* @returns true iff the badge appears in the list.
*/
static bool BadgesContains(std::span<const BadgeID> badges, BadgeID badge)
{
return std::ranges::find(badges, badge) != std::end(badges);
}
/**
* Test if a rail type has a badge.
* @param rt Rail type to test.
* @param badge Badge to find.
* @returns true iff the rail type has the badge.
*/
static bool RailTypeHasBadge(RailType rt, BadgeID badge)
{
return rt != INVALID_RAILTYPE && BadgesContains(GetRailTypeInfo(rt)->badges, badge);
}
/**
* Test if a road type has a badge.
* @param rt Road type to test.
* @param badge Badge to find.
* @returns true iff the road type has the badge.
*/
static bool RoadTypeHasBadge(RoadType rt, BadgeID badge)
{
return rt != INVALID_ROADTYPE && BadgesContains(GetRoadTypeInfo(rt)->badges, badge);
}
using TileHasBadgeProc = bool(*)(TileIndex tile, BadgeID badge, GrfSpecFeatures features);
static bool TileHasBadge_Rail(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
if (features.Test(GrfSpecFeature::GSF_RAILTYPES) && RailTypeHasBadge(GetRailType(tile), badge)) return true;
return false;
}
static bool TileHasBadge_Road(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
if (features.Test(GrfSpecFeature::GSF_ROADTYPES) && RoadTypeHasBadge(GetRoadTypeRoad(tile), badge)) return true;
if (features.Test(GrfSpecFeature::GSF_TRAMTYPES) && RoadTypeHasBadge(GetRoadTypeTram(tile), badge)) return true;
if (features.Test(GrfSpecFeature::GSF_RAILTYPES) && IsLevelCrossing(tile) && RailTypeHasBadge(GetRailType(tile), badge)) return true;
return false;
}
static bool TileHasBadge_Town(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
if (features.Test(GrfSpecFeature::GSF_HOUSES) && BadgesContains(HouseSpec::Get(GetHouseType(tile))->badges, badge)) return true;
return false;
}
static bool TileHasBadge_Station(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
switch (GetStationType(tile)) {
case StationType::Rail:
case StationType::RailWaypoint:
if (features.Test(GrfSpecFeature::GSF_STATIONS)) {
if (const StationSpec *spec = GetStationSpec(tile); spec != nullptr && BadgesContains(spec->badges, badge)) return true;
}
if (features.Test(GrfSpecFeature::GSF_RAILTYPES) && RailTypeHasBadge(GetRailType(tile), badge)) return true;
return false;
case StationType::Bus:
case StationType::Truck:
case StationType::RoadWaypoint:
if (features.Test(GrfSpecFeature::GSF_ROADSTOPS)) {
if (const RoadStopSpec *spec = GetRoadStopSpec(tile); spec != nullptr && BadgesContains(spec->badges, badge)) return true;
}
if (features.Test(GrfSpecFeature::GSF_ROADTYPES) && RoadTypeHasBadge(GetRoadTypeRoad(tile), badge)) return true;
if (features.Test(GrfSpecFeature::GSF_TRAMTYPES) && RoadTypeHasBadge(GetRoadTypeTram(tile), badge)) return true;
return false;
case StationType::Airport:
if (features.Test(GrfSpecFeature::GSF_AIRPORTTILES) && BadgesContains(AirportTileSpec::GetByTile(tile)->badges, badge)) return true;
return false;
default:
return false;
}
}
static bool TileHasBadge_Industry(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
if (features.Test(GrfSpecFeature::GSF_INDUSTRYTILES) && BadgesContains(GetIndustryTileSpec(GetIndustryGfx(tile))->badges, badge)) return true;
if (features.Test(GrfSpecFeature::GSF_INDUSTRIES) && BadgesContains(GetIndustrySpec(GetIndustryType(tile))->badges, badge)) return true;
return false;
}
static bool TileHasBadge_TunnelBridge(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
switch (GetTunnelBridgeTransportType(tile)) {
case TransportType::TRANSPORT_RAIL:
if (features.Test(GrfSpecFeature::GSF_RAILTYPES) && RailTypeHasBadge(GetRailType(tile), badge)) return true;
return false;
case TransportType::TRANSPORT_ROAD:
if (features.Test(GrfSpecFeature::GSF_ROADTYPES) && RoadTypeHasBadge(GetRoadTypeRoad(tile), badge)) return true;
if (features.Test(GrfSpecFeature::GSF_TRAMTYPES) && RoadTypeHasBadge(GetRoadTypeTram(tile), badge)) return true;
return false;
default:
return false;
}
}
static bool TileHasBadge_Object(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
if (features.Test(GrfSpecFeature::GSF_OBJECTS) && BadgesContains(ObjectSpec::GetByTile(tile)->badges, badge)) return true;
return false;
}
/**
* Test if a tile has an item containing the specified badge.
* @param tile Tile to query.
* @param badge Badge to search for.
* @param features GRF features to include in the test.
* @returns true iff the badge 'is on' the tile.
*/
static bool TileHasBadge(TileIndex tile, BadgeID badge, GrfSpecFeatures features)
{
/* Per-tiletype functions for badge testing. Like _tile_type_procs, this is 16 entries as GetTileType() reads 4 bits. */
static constexpr std::array<TileHasBadgeProc, 16> tile_procs = {
nullptr,
TileHasBadge_Rail,
TileHasBadge_Road,
TileHasBadge_Town,
nullptr,
TileHasBadge_Station,
nullptr,
nullptr,
TileHasBadge_Industry,
TileHasBadge_TunnelBridge,
TileHasBadge_Object,
};
TileHasBadgeProc proc = tile_procs[GetTileType(tile)];
return proc != nullptr && proc(tile, badge, features);
}
/**
* Test for a matching badge 'on' a specific map tile.
* @param grffile GRF file of the current varaction.
* @param tile Tile to test.
* @param parameter GRF-local badge index.
* @param features GRF features to include in the test.
* @returns true iff the badge is present.
*/
uint32_t GetNearbyBadgeVariableResult(const GRFFile &grffile, TileIndex tile, const ResolverObject &object)
{
GrfSpecFeatures features = static_cast<GrfSpecFeatures>(object.GetRegister(0x101));
if (features.None()) return 0;
uint32_t parameter = object.GetRegister(0x100);
if (parameter >= std::size(grffile.badge_list)) return UINT_MAX;
/* NewGRF cannot be expected to know the bounds of the map. If the tile is invalid it doesn't have the queried badge. */
if (!IsValidTile(tile)) return 0;
BadgeID index = grffile.badge_list[parameter];
return TileHasBadge(tile, index, features);
}
/**
* Test for a matching badge in a list of badges.
* @param grffile GRF file of the current varaction.
* @param badges List of badges to test.
* @param parameter GRF-local badge index.
@ -227,7 +405,7 @@ uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span<const BadgeID>
if (parameter >= std::size(grffile.badge_list)) return UINT_MAX;
BadgeID index = grffile.badge_list[parameter];
return std::ranges::find(badges, index) != std::end(badges);
return BadgesContains(badges, index);
}
/**

View File

@ -64,6 +64,7 @@ Badge *GetBadgeByLabel(std::string_view label);
Badge *GetClassBadge(BadgeClassID class_index);
std::span<const BadgeID> GetClassBadges();
uint32_t GetNearbyBadgeVariableResult(const GRFFile &grffile, TileIndex tile, const ResolverObject &object);
uint32_t GetBadgeVariableResult(const struct GRFFile &grffile, std::span<const BadgeID> badges, uint32_t parameter);
PalSpriteID GetBadgeSprite(const Badge &badge, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, PaletteID remap);

View File

@ -444,6 +444,7 @@ static uint32_t GetDistanceFromNearbyHouse(uint8_t parameter, TileIndex start_ti
return _house_mngr.GetGRFID(house_id);
}
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, HouseSpec::Get(this->house_id)->badges, parameter);
}

View File

@ -168,6 +168,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(const ResolverObject &objec
/* Variables available during construction check. */
switch (variable) {
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, GetIndustrySpec(this->type)->badges, parameter);
case 0x80: return this->tile.base();
@ -354,6 +355,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(const ResolverObject &objec
NOT_REACHED();
}
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, GetIndustrySpec(this->type)->badges, parameter);
/* Get a variable from the persistent storage */

View File

@ -93,6 +93,7 @@ uint32_t GetRelativePosition(TileIndex tile, TileIndex ind_tile)
/* Get industry tile ID at offset */
case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->tile), this->industry, this->ro.grffile->grfid);
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, GetIndustryTileSpec(GetIndustryGfx(this->tile))->badges, parameter);
}

View File

@ -291,6 +291,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(const ResolverObject &objec
/* Object view */
case 0x48: return this->view;
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->spec->badges, parameter);
/*
@ -364,6 +365,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(const ResolverObject &objec
/* Count of object, distance of closest instance */
case 0x64: return GetCountAndDistanceOfClosestInstance(this->ro, parameter, this->ro.grffile->grfid, this->tile, this->obj);
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->spec->badges, parameter);
}

View File

@ -205,6 +205,7 @@ uint32_t RoadStopScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] u
return 0xFFFE;
}
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->roadstopspec->badges, parameter);
case 0xF0: return this->st == nullptr ? 0 : this->st->facilities.base(); // facilities

View File

@ -295,6 +295,12 @@ TownScopeResolver *StationResolverObject::GetTown()
}
break;
case 0x79:
if (this->axis != INVALID_AXIS && this->tile != INVALID_TILE) {
return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile, true, this->axis), this->ro);
}
break;
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->statspec->badges, parameter);
case 0xFA: return ClampTo<uint16_t>(TimerGameCalendar::date - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR); // Build date, clamped to a 16 bit value
@ -398,6 +404,7 @@ TownScopeResolver *StationResolverObject::GetTown()
return 0xFFFE;
}
case 0x79: return GetNearbyBadgeVariableResult(*this->ro.grffile, GetNearbyTile(parameter, this->tile), this->ro);
case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->statspec->badges, parameter);
/* General station variables */

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

@ -50,8 +50,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 +59,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 +81,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 +157,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 +177,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()

View File

@ -256,7 +256,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 +288,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.