From b96fdee531cac76968d851ecd015e24e5442cf02 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 10 May 2020 18:07:27 +0100 Subject: [PATCH] Add plugin API to remove entities (#11702) --- distribution/openrct2.d.ts | 6 +++++ src/openrct2/scripting/ScEntity.hpp | 37 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 5350003619..02cf0b4c9c 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -698,6 +698,12 @@ declare global { * The z-coordinate of the entity in game units. */ z: number; + + /** + * Removes the entity from the map. + * Note: removing vehicles and peeps that are on rides is currently unsupported. + */ + remove(): void; } /** diff --git a/src/openrct2/scripting/ScEntity.hpp b/src/openrct2/scripting/ScEntity.hpp index 645fd95d05..69dcfbb8d6 100644 --- a/src/openrct2/scripting/ScEntity.hpp +++ b/src/openrct2/scripting/ScEntity.hpp @@ -11,6 +11,7 @@ #ifdef ENABLE_SCRIPTING +# include "../Context.h" # include "../common.h" # include "../world/Sprite.h" # include "Duktape.hpp" @@ -119,6 +120,41 @@ namespace OpenRCT2::Scripting } } + void remove() + { + auto ctx = GetContext()->GetScriptEngine().GetContext(); + auto entity = GetEntity(); + if (entity != nullptr) + { + invalidate_sprite_2(entity); + switch (entity->sprite_identifier) + { + case SPRITE_IDENTIFIER_VEHICLE: + duk_error(ctx, DUK_ERR_ERROR, "Removing a vehicle is currently unsupported."); + break; + case SPRITE_IDENTIFIER_PEEP: + { + auto peep = static_cast(entity); + // We can't remove a single peep from a ride at the moment as this can cause complications with the + // vehicle car having an unsupported peep capacity. + if (peep->state == PEEP_STATE_ON_RIDE || peep->state == PEEP_STATE_ENTERING_RIDE) + { + duk_error(ctx, DUK_ERR_ERROR, "Removing a peep that is on a ride is currently unsupported."); + } + else + { + peep->Remove(); + } + break; + } + case SPRITE_IDENTIFIER_MISC: + case SPRITE_IDENTIFIER_LITTER: + sprite_remove(entity); + break; + } + } + } + SpriteBase* GetEntity() const { return &get_sprite(_id)->generic; @@ -132,6 +168,7 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScEntity::x_get, &ScEntity::x_set, "x"); dukglue_register_property(ctx, &ScEntity::y_get, &ScEntity::y_set, "y"); dukglue_register_property(ctx, &ScEntity::z_get, &ScEntity::z_set, "z"); + dukglue_register_method(ctx, &ScEntity::remove, "remove"); } };