mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-13 02:52:35 +01:00
Add tile element support
This commit is contained in:
@@ -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<ScTile> getTile(sint32 x, sint32 y)
|
||||
{
|
||||
auto firstElement = map_get_first_element_at(x, y);
|
||||
return std::make_shared<ScTile>(firstElement);
|
||||
}
|
||||
|
||||
std::shared_ptr<ScThing> 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");
|
||||
}
|
||||
};
|
||||
|
||||
99
src/openrct2/scripting/ScTile.hpp
Normal file
99
src/openrct2/scripting/ScTile.hpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include <dukglue/dukglue.h>
|
||||
#include "../common.h"
|
||||
#include "../world/Sprite.h"
|
||||
#include <cstdio>
|
||||
|
||||
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<ScTileElement> getElement(size_t index)
|
||||
{
|
||||
if (index >= _count) return nullptr;
|
||||
return std::make_shared<ScTileElement>(&_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<std::shared_ptr<ScTileElement>> elements_get()
|
||||
// {
|
||||
// std::vector<std::shared_ptr<ScTileElement>> elements;
|
||||
// for (size_t i = 0; i < _count; i++)
|
||||
// {
|
||||
// elements.push_back(std::make_shared<ScTileElement>(&_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");
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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<ScConsole>(_console), "console");
|
||||
|
||||
Reference in New Issue
Block a user