diff --git a/src/openrct2/scripting/ScMap.hpp b/src/openrct2/scripting/ScMap.hpp index c4f69e16ae..86a355535d 100644 --- a/src/openrct2/scripting/ScMap.hpp +++ b/src/openrct2/scripting/ScMap.hpp @@ -4,6 +4,7 @@ #include "../common.h" #include "../ride/Ride.h" #include "../world/Map.h" +#include "ScTile.hpp" #include "ScThing.hpp" namespace OpenRCT2::Scripting @@ -40,6 +41,12 @@ namespace OpenRCT2::Scripting return MAX_SPRITES; } + std::shared_ptr getTile(sint32 x, sint32 y) + { + auto firstElement = map_get_first_element_at(x, y); + return std::make_shared(firstElement); + } + std::shared_ptr getThing(sint32 id) { if (id >= 0 && id < MAX_SPRITES) @@ -58,6 +65,7 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScMap::size_get, nullptr, "size"); dukglue_register_property(ctx, &ScMap::rides_get, nullptr, "rides"); dukglue_register_property(ctx, &ScMap::things_get, nullptr, "things"); + dukglue_register_method(ctx, &ScMap::getTile, "getTile"); dukglue_register_method(ctx, &ScMap::getThing, "getThing"); } }; diff --git a/src/openrct2/scripting/ScTile.hpp b/src/openrct2/scripting/ScTile.hpp new file mode 100644 index 0000000000..a3d96a5fdc --- /dev/null +++ b/src/openrct2/scripting/ScTile.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include +#include "../common.h" +#include "../world/Sprite.h" +#include + +namespace OpenRCT2::Scripting +{ + class ScTileElement + { + private: + rct_tile_element * _element; + + public: + ScTileElement(rct_tile_element * element) + : _element(element) + { + } + + std::string type_get() + { + if (tile_element_get_type(_element) == TILE_ELEMENT_TYPE_PATH) + { + return "footpath"; + } + return "unknown"; + } + + bool broken_get() { return _element->flags & TILE_ELEMENT_FLAG_BROKEN; } + void broken_set(bool value) + { + if (value) + { + _element->flags |= TILE_ELEMENT_FLAG_BROKEN; + } + else + { + _element->flags &= ~TILE_ELEMENT_FLAG_BROKEN; + } + } + + static void Register(duk_context * ctx) + { + dukglue_register_property(ctx, &ScTileElement::type_get, nullptr, "type"); + dukglue_register_property(ctx, &ScTileElement::broken_get, &ScTileElement::broken_set, "broken"); + } + }; + + class ScTile + { + private: + rct_tile_element * _first; + size_t _count = 0; + + public: + ScTile(rct_tile_element * first) + : _first(first) + { + _count = 0; + if (first != nullptr) + { + auto element = first; + do + { + _count++; + } + while (!tile_element_is_last_for_tile(element++)); + } + } + + size_t numElements_get() { return _count; } + + std::shared_ptr getElement(size_t index) + { + if (index >= _count) return nullptr; + return std::make_shared(&_first[index]); + } + + // This is not a good idea as the array is generated each time elements is accessed and + // chances are scripts will do tile.elements[0], tile.elements[1]... + // std::vector> elements_get() + // { + // std::vector> elements; + // for (size_t i = 0; i < _count; i++) + // { + // elements.push_back(std::make_shared(&_first[i])); + // } + // return elements; + // } + + static void Register(duk_context * ctx) + { + // dukglue_register_property(ctx, &ScTile::elements_get, nullptr, "elements"); + dukglue_register_property(ctx, &ScTile::numElements_get, nullptr, "numElements"); + dukglue_register_method(ctx, &ScTile::getElement, "getElement"); + } + }; +} diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 2d641a63fd..67a2d3d957 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -23,6 +23,7 @@ #include "ScDisposable.hpp" #include "ScMap.hpp" #include "ScPark.hpp" +#include "ScTile.hpp" #include "ScThing.hpp" using namespace OpenRCT2; @@ -56,6 +57,8 @@ void ScriptEngine::Initialise() ScDisposable::Register(ctx); ScMap::Register(ctx); ScPark::Register(ctx); + ScTile::Register(ctx); + ScTileElement::Register(ctx); ScThing::Register(ctx); dukglue_register_global(ctx, std::make_shared(_console), "console");