diff --git a/src/openrct2-ui/scripting/ScUi.hpp b/src/openrct2-ui/scripting/ScUi.hpp index 1d58081e4c..5484286a6f 100644 --- a/src/openrct2-ui/scripting/ScUi.hpp +++ b/src/openrct2-ui/scripting/ScUi.hpp @@ -9,12 +9,12 @@ #pragma once -#include "../Context.h" -#include "../common.h" #include "ScWindow.hpp" -#include #include +#include +#include +#include #include #include @@ -93,12 +93,12 @@ namespace OpenRCT2::Scripting std::shared_ptr getWindow(int32_t index) { - for (int32_t i = 0; i < g_window_list.size(); i++) + int32_t i = 0; + for (auto w : g_window_list) { if (i == index) { - auto w = g_window_list[i].get(); - return std::make_shared(w); + return std::make_shared(w.get()); } i++; } diff --git a/src/openrct2-ui/scripting/ScWindow.hpp b/src/openrct2-ui/scripting/ScWindow.hpp index 1925e7bf2e..413b260894 100644 --- a/src/openrct2-ui/scripting/ScWindow.hpp +++ b/src/openrct2-ui/scripting/ScWindow.hpp @@ -9,11 +9,10 @@ #pragma once -#include "../common.h" -#include "../interface/Window.h" -#include "../interface/Window_internal.h" - -#include +#include +#include +#include +#include namespace OpenRCT2::Scripting { diff --git a/src/openrct2/interface/StdInOutConsole.cpp b/src/openrct2/interface/StdInOutConsole.cpp index a52d9dc346..9491b00cd7 100644 --- a/src/openrct2/interface/StdInOutConsole.cpp +++ b/src/openrct2/interface/StdInOutConsole.cpp @@ -11,10 +11,9 @@ #include "../OpenRCT2.h" #include "../platform/Platform2.h" #include "../scripting/ScriptEngine.h" +#include "../thirdparty/linenoise.hpp" #include "InteractiveConsole.h" -#include - using namespace OpenRCT2; void StdInOutConsole::Start() diff --git a/src/openrct2/scripting/Duktape.hpp b/src/openrct2/scripting/Duktape.hpp new file mode 100644 index 0000000000..22b096e3f6 --- /dev/null +++ b/src/openrct2/scripting/Duktape.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "../thirdparty/dukglue/dukglue.h" +#include diff --git a/src/openrct2/scripting/HookEngine.h b/src/openrct2/scripting/HookEngine.h index ed426a42c6..4753d3ce64 100644 --- a/src/openrct2/scripting/HookEngine.h +++ b/src/openrct2/scripting/HookEngine.h @@ -10,8 +10,8 @@ #pragma once #include "../common.h" +#include "Duktape.hpp" -#include #include #include #include diff --git a/src/openrct2/scripting/Plugin.cpp b/src/openrct2/scripting/Plugin.cpp index 234c8a3e7f..f96b75272b 100644 --- a/src/openrct2/scripting/Plugin.cpp +++ b/src/openrct2/scripting/Plugin.cpp @@ -9,9 +9,9 @@ #include "Plugin.h" +#include "Duktape.hpp" + #include -#include -#include #include #include diff --git a/src/openrct2/scripting/Plugin.h b/src/openrct2/scripting/Plugin.h index c95ca31315..4ae6b8a8d3 100644 --- a/src/openrct2/scripting/Plugin.h +++ b/src/openrct2/scripting/Plugin.h @@ -9,7 +9,8 @@ #pragma once -#include +#include "Duktape.hpp" + #include #include #include diff --git a/src/openrct2/scripting/ScConsole.hpp b/src/openrct2/scripting/ScConsole.hpp index c6fb8038e0..a53a9b8b82 100644 --- a/src/openrct2/scripting/ScConsole.hpp +++ b/src/openrct2/scripting/ScConsole.hpp @@ -9,10 +9,8 @@ #pragma once -#include "../core/Math.hpp" #include "../interface/InteractiveConsole.h" - -#include +#include "Duktape.hpp" namespace OpenRCT2::Scripting { @@ -50,7 +48,7 @@ namespace OpenRCT2::Scripting { const auto d = val.as_double(); const duk_int_t i = val.as_int(); - if (Math::AlmostEqual(d, i)) + if (AlmostEqual(d, i)) { str = std::to_string(i); } @@ -84,5 +82,18 @@ namespace OpenRCT2::Scripting dukglue_register_method(ctx, &ScConsole::clear, "clear"); dukglue_register_method(ctx, &ScConsole::log, "log"); } + + private: + // Taken from http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon + template + static typename std::enable_if::is_integer, bool>::type AlmostEqual(T x, T y, int32_t ulp = 20) + { + // the machine epsilon has to be scaled to the magnitude of the values used + // and multiplied by the desired precision in ULPs (units in the last place) + return std::abs(x - y) <= std::numeric_limits::epsilon() * std::abs(x + y) * ulp + // unless the result is subnormal + || std::abs(x - y) + < (std::numeric_limits::min)(); // TODO: Remove parentheses around min once the macro is removed + } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/ScContext.hpp b/src/openrct2/scripting/ScContext.hpp index 85b8de1956..b53770a9fe 100644 --- a/src/openrct2/scripting/ScContext.hpp +++ b/src/openrct2/scripting/ScContext.hpp @@ -9,12 +9,12 @@ #pragma once +#include "Duktape.hpp" #include "HookEngine.h" #include "ScDisposable.hpp" #include "ScriptEngine.h" #include -#include #include namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/ScDisposable.hpp b/src/openrct2/scripting/ScDisposable.hpp index 7e2840710b..13219b0ed4 100644 --- a/src/openrct2/scripting/ScDisposable.hpp +++ b/src/openrct2/scripting/ScDisposable.hpp @@ -9,7 +9,8 @@ #pragma once -#include +#include "Duktape.hpp" + #include namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/ScMap.hpp b/src/openrct2/scripting/ScMap.hpp index 84866ac2f6..410b894610 100644 --- a/src/openrct2/scripting/ScMap.hpp +++ b/src/openrct2/scripting/ScMap.hpp @@ -12,11 +12,10 @@ #include "../common.h" #include "../ride/Ride.h" #include "../world/Map.h" +#include "Duktape.hpp" #include "ScThing.hpp" #include "ScTile.hpp" -#include - namespace OpenRCT2::Scripting { class ScMap @@ -62,7 +61,7 @@ namespace OpenRCT2::Scripting if (id >= 0 && id < MAX_SPRITES) { auto sprite = get_sprite(id); - if (sprite != nullptr && sprite->unknown.sprite_identifier != SPRITE_IDENTIFIER_NULL) + if (sprite != nullptr && sprite->generic.sprite_identifier != SPRITE_IDENTIFIER_NULL) { return std::make_shared(sprite); } diff --git a/src/openrct2/scripting/ScPark.hpp b/src/openrct2/scripting/ScPark.hpp index bc966afb7f..57d133f57e 100644 --- a/src/openrct2/scripting/ScPark.hpp +++ b/src/openrct2/scripting/ScPark.hpp @@ -15,9 +15,9 @@ #include "../management/NewsItem.h" #include "../windows/Intent.h" #include "../world/Park.h" +#include "Duktape.hpp" #include -#include namespace OpenRCT2::Scripting { diff --git a/src/openrct2/scripting/ScThing.hpp b/src/openrct2/scripting/ScThing.hpp index 05fee9bf89..5d303454f3 100644 --- a/src/openrct2/scripting/ScThing.hpp +++ b/src/openrct2/scripting/ScThing.hpp @@ -11,8 +11,7 @@ #include "../common.h" #include "../world/Sprite.h" - -#include +#include "Duktape.hpp" namespace OpenRCT2::Scripting { @@ -29,7 +28,7 @@ namespace OpenRCT2::Scripting std::string type_get() { - if (_sprite->unknown.sprite_identifier == SPRITE_IDENTIFIER_PEEP) + if (_sprite->generic.sprite_identifier == SPRITE_IDENTIFIER_PEEP) { return "peep"; } @@ -39,31 +38,31 @@ namespace OpenRCT2::Scripting // x getter and setter int32_t x_get() { - return _sprite->unknown.x; + return _sprite->generic.x; } void x_set(int32_t value) { - _sprite->unknown.x = value; + _sprite->generic.x = value; } // y getter and setter int32_t y_get() { - return _sprite->unknown.y; + return _sprite->generic.y; } void y_set(int32_t value) { - _sprite->unknown.y = value; + _sprite->generic.y = value; } // z getter and setter int16_t z_get() { - return _sprite->unknown.z; + return _sprite->generic.z; } void z_set(int16_t value) { - _sprite->unknown.z = value; + _sprite->generic.z = value; } uint8_t tshirtColour_get() diff --git a/src/openrct2/scripting/ScTile.hpp b/src/openrct2/scripting/ScTile.hpp index 89ccaafd33..69c605106b 100644 --- a/src/openrct2/scripting/ScTile.hpp +++ b/src/openrct2/scripting/ScTile.hpp @@ -14,19 +14,19 @@ #include "../world/Scenery.h" #include "../world/Sprite.h" #include "../world/Surface.h" +#include "Duktape.hpp" #include -#include namespace OpenRCT2::Scripting { class ScTileElement { protected: - rct_tile_element* _element; + TileElement* _element; public: - ScTileElement(rct_tile_element* element) + ScTileElement(TileElement* element) : _element(element) { } @@ -105,63 +105,59 @@ namespace OpenRCT2::Scripting { uint8_t slope_get() { - return _element->properties.surface.slope & TILE_ELEMENT_SURFACE_SLOPE_MASK; + return _element->AsSurface()->GetSlope(); } void slope_set(uint8_t value) { // TODO: Give warning when value > TILE_ELEMENT_SURFACE_SLOPE_MASK - _element->properties.surface.slope &= ~TILE_ELEMENT_SURFACE_SLOPE_MASK; - _element->properties.surface.slope |= value & TILE_ELEMENT_SURFACE_SLOPE_MASK; + _element->AsSurface()->SetSlope(value); } uint8_t terrain_get() { - return surface_get_terrain(_element); + return _element->AsSurface()->GetSurfaceStyle(); } void terrain_set(uint8_t value) { - surface_set_terrain(_element, value); + _element->AsSurface()->SetSurfaceStyle(value); } uint8_t waterHeight_get() { - return surface_get_water_height(_element); + return _element->AsSurface()->GetWaterHeight(); } void waterHeight_set(uint8_t value) { // TODO: Give warning when value > TILE_ELEMENT_SURFACE_WATER_HEIGHT_MASK - _element->properties.surface.terrain &= ~TILE_ELEMENT_SURFACE_WATER_HEIGHT_MASK; - _element->properties.surface.terrain |= value & TILE_ELEMENT_SURFACE_WATER_HEIGHT_MASK; + _element->AsSurface()->SetWaterHeight(value); } uint8_t grassLength_get() { - return _element->properties.surface.grass_length; + return _element->AsSurface()->GetGrassLength(); } void grassLength_set(uint8_t value) { // TODO: Give warning when value > GRASS_LENGTH_CLUMPS_2 - _element->properties.surface.grass_length = value; + return _element->AsSurface()->SetGrassLength(value); } uint8_t ownership_get() { - return _element->properties.surface.ownership & 0xF0; + return _element->AsSurface()->GetOwnership(); } void ownership_set(uint8_t flags) { - _element->properties.surface.ownership &= ~0xF0; - _element->properties.surface.ownership |= flags << 4; + _element->AsSurface()->SetOwnership(flags); } uint8_t parkFences_get() { - return _element->properties.surface.ownership & 0xF0; + return _element->AsSurface()->GetParkFences(); } void parkFences_set(uint8_t flags) { - _element->properties.surface.ownership &= ~0x0F; - _element->properties.surface.ownership |= flags & 0x0F; + return _element->AsSurface()->SetParkFences(flags); } public: @@ -184,52 +180,60 @@ namespace OpenRCT2::Scripting class ScFootpathAdditionStatus { protected: - rct_tile_element* _element; + TileElement* _element; public: - ScFootpathAdditionStatus(rct_tile_element* element) + ScFootpathAdditionStatus(TileElement* element) : _element(element) { } uint8_t north_get() { - return _element->properties.path.addition_status & 3; + return _element->AsPath()->GetAdditionStatus() & 3; } void north_set(uint8_t value) { - _element->properties.path.addition_status &= ~3; - _element->properties.path.addition_status |= value & 3; + auto addition = _element->AsPath()->GetAdditionStatus(); + addition &= ~3; + addition |= value & 3; + _element->AsPath()->SetAdditionStatus(addition); } uint8_t east_get() { - return _element->properties.path.addition_status & 3; + return _element->AsPath()->GetAdditionStatus() & 3; } void east_set(uint8_t value) { - _element->properties.path.addition_status &= ~(3 << 2); - _element->properties.path.addition_status |= (value & 3) << 2; + auto addition = _element->AsPath()->GetAdditionStatus(); + addition &= ~(3 << 2); + addition |= (value & 3) << 2; + _element->AsPath()->SetAdditionStatus(addition); } uint8_t south_get() { - return _element->properties.path.addition_status & 3; + return _element->AsPath()->GetAdditionStatus() & 3; } void south_set(uint8_t value) { - _element->properties.path.addition_status &= ~(3 << 4); - _element->properties.path.addition_status |= (value & 3) << 4; + auto addition = _element->AsPath()->GetAdditionStatus(); + addition &= ~(3 << 4); + addition |= (value & 3) << 4; + _element->AsPath()->SetAdditionStatus(addition); } uint8_t west_get() { - return _element->properties.path.addition_status & 3; + return _element->AsPath()->GetAdditionStatus() & 3; } void west_set(uint8_t value) { - _element->properties.path.addition_status &= ~(3 << 6); - _element->properties.path.addition_status |= (value & 3) << 6; + auto addition = _element->AsPath()->GetAdditionStatus(); + addition &= ~(3 << 6); + addition |= (value & 3) << 6; + _element->AsPath()->SetAdditionStatus(addition); } static void Register(duk_context* ctx) @@ -242,10 +246,10 @@ namespace OpenRCT2::Scripting }; protected: - rct_tile_element* _element; + TileElement* _element; bool VerifyPathAdditionExists() { - if (!footpath_element_has_path_scenery(_element)) + if (!_element->AsPath()->HasAddition()) { // TODO: Show warning in console that the path addition has been removed return false; @@ -254,25 +258,25 @@ namespace OpenRCT2::Scripting } public: - ScFootpathAddition(rct_tile_element* element) + ScFootpathAddition(TileElement* element) : _element(element) { } void Remove() { - footpath_element_set_path_scenery(_element, 0); + _element->AsPath()->SetAddition(0); } uint8_t type_get() { if (!VerifyPathAdditionExists()) return 0; - return footpath_element_get_path_scenery(_element); + return _element->AsPath()->GetAddition(); } void type_set(uint8_t value) { - footpath_element_set_path_scenery(_element, value); + _element->AsPath()->SetAddition(value); } bool isBin_get() @@ -280,8 +284,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_IS_BIN; } bool isBench_get() @@ -289,8 +292,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_IS_BENCH; } bool isLamp_get() @@ -298,8 +300,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_LAMP; } bool isBreakable_get() @@ -307,8 +308,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_BREAKABLE; } bool isJumpingFountainWater_get() @@ -316,8 +316,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_JUMPING_FOUNTAIN_WATER; } bool isJumpingFountainSnow_get() @@ -325,8 +324,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_JUMPING_FOUNTAIN_SNOW; } bool allowedOnQueue_get() @@ -334,8 +332,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_DONT_ALLOW_ON_QUEUE; } bool allowedOnSlope_get() @@ -343,8 +340,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_DONT_ALLOW_ON_SLOPE; } bool isQueueScreen_get() @@ -352,8 +348,7 @@ namespace OpenRCT2::Scripting if (!VerifyPathAdditionExists()) return false; - auto index = footpath_element_get_path_scenery_index(_element); - auto sceneryEntry = get_footpath_item_entry(index); + auto sceneryEntry = _element->AsPath()->GetAdditionEntry(); return sceneryEntry->path_bit.flags & PATH_BIT_FLAG_IS_QUEUE_SCREEN; } @@ -387,36 +382,36 @@ namespace OpenRCT2::Scripting uint8_t footpathType_get() { - return footpath_element_get_type(_element); + return _element->AsPath()->GetPathEntryIndex(); } void type_set(uint8_t index) { - auto entry = get_footpath_entry(index); + auto entry = get_path_surface_entry(index); if (entry == nullptr) { // TODO: Give warning (most likely only happens when index > MAX_PATH_OBJECTS) return; } - footpath_element_set_type(_element, index); + _element->AsPath()->SetPathEntryIndex(index); } bool isSloped_get() { - return footpath_element_is_sloped(_element); + return _element->AsPath()->IsSloped(); } void isSlope_set(bool value) { - footpath_element_set_sloped(_element, value); + _element->AsPath()->SetSloped(value); } bool isQueue_get() { - return footpath_element_is_queue(_element); + return _element->AsPath()->IsQueue(); } std::shared_ptr addition_get() { - if (!footpath_element_has_path_scenery(_element)) + if (!_element->AsPath()->HasAddition()) return nullptr; return std::make_shared(_element); @@ -424,29 +419,29 @@ namespace OpenRCT2::Scripting uint8_t edges_get() { - return footpath_element_get_edges(_element); + return _element->AsPath()->GetEdges(); } void edges_set(uint8_t value) { - footpath_element_set_edges(_element, value); + _element->AsPath()->SetEdges(value); } uint8_t corners_get() { - return footpath_element_get_corners(_element); + return _element->AsPath()->GetCorners(); } void corners_set(uint8_t value) { - footpath_element_set_corners(_element, value); + _element->AsPath()->SetCorners(value); } uint8_t rideIndex_get() { - if (!footpath_element_is_queue(_element)) + if (!_element->AsPath()->IsQueue()) { // TODO: Show warning that "path is not a queue" } - return _element->properties.path.ride_index; + return _element->AsPath()->GetRideIndex(); } public: @@ -473,7 +468,7 @@ namespace OpenRCT2::Scripting { auto track_ride_index_get() const { - return _element->properties.track.ride_index; + return _element->AsTrack()->GetRideIndex(); } public: @@ -575,11 +570,11 @@ namespace OpenRCT2::Scripting class ScTile { private: - rct_tile_element* _first; + TileElement* _first; size_t _count = 0; public: - ScTile(rct_tile_element* first) + ScTile(TileElement* first) : _first(first) { _count = 0; diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index b413227f8a..7d8012e3e8 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -14,6 +14,7 @@ #include "../core/Path.hpp" #include "../interface/InteractiveConsole.h" #include "../platform/Platform2.h" +#include "Duktape.hpp" #include "ScConsole.hpp" #include "ScContext.hpp" #include "ScDisposable.hpp" @@ -22,8 +23,6 @@ #include "ScThing.hpp" #include "ScTile.hpp" -#include -#include #include #include