From 437321ac8f6c1b6bb116de2c099cba862ccc821a Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Tue, 10 Aug 2021 15:05:50 +0300 Subject: [PATCH] Move ScMap code into cpp --- OpenRCT2.xcodeproj/project.pbxproj | 4 + src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/scripting/ScriptEngine.cpp | 1 + .../scripting/bindings/world/ScMap.cpp | 273 ++++++++++++++++++ .../scripting/bindings/world/ScMap.hpp | 251 +--------------- 5 files changed, 293 insertions(+), 237 deletions(-) create mode 100644 src/openrct2/scripting/bindings/world/ScMap.cpp diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 4c50996ea1..c9a57f561b 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -789,6 +789,7 @@ CDC72A71A28542F4AD73A91C /* ScRideStation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 522FE124ED4F4E18BCF79042 /* ScRideStation.cpp */; }; 63858E295E3F451283987982 /* ScRideStation.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1260386710BF4EA4B4FC31D3 /* ScRideStation.hpp */; }; 705A4B342C7D4F38B26BAFA4 /* ScRide.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F3B00C9900ED42AFA72764ED /* ScRide.cpp */; }; + 2B8F76B32C944D3C97F0FFAF /* ScMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4BE60702D7549488A32CC0B /* ScMap.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -1880,6 +1881,7 @@ 522FE124ED4F4E18BCF79042 /* ScRideStation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScRideStation.cpp; path = src/openrct2/scripting/bindings/ride/ScRideStation.cpp; sourceTree = SOURCE_ROOT; }; 1260386710BF4EA4B4FC31D3 /* ScRideStation.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScRideStation.hpp; path = src/openrct2/scripting/bindings/ride/ScRideStation.hpp; sourceTree = SOURCE_ROOT; }; F3B00C9900ED42AFA72764ED /* ScRide.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScRide.cpp; path = src/openrct2/scripting/bindings/ride/ScRide.cpp; sourceTree = SOURCE_ROOT; }; + E4BE60702D7549488A32CC0B /* ScMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScMap.cpp; path = src/openrct2/scripting/bindings/world/ScMap.cpp; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -3460,6 +3462,7 @@ 9AB8D336AB654D0280549A89 /* ScPark.hpp */, A9F55F2291284217BA0E4E51 /* ScScenario.hpp */, 22D7DD798F054A068273739B /* ScTile.hpp */, + E4BE60702D7549488A32CC0B /* ScMap.cpp */, ); name = world; sourceTree = ""; @@ -4429,6 +4432,7 @@ F08623EA69E7456DB79F3E06 /* ScNetwork.cpp in Sources */, CDC72A71A28542F4AD73A91C /* ScRideStation.cpp in Sources */, 705A4B342C7D4F38B26BAFA4 /* ScRide.cpp in Sources */, + 2B8F76B32C944D3C97F0FFAF /* ScMap.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 50f78fc5f2..4624771168 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -866,6 +866,7 @@ + diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 04e11e54c8..5d3c8309ba 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -29,6 +29,7 @@ # include "bindings/entity/ScLitter.hpp" # include "bindings/entity/ScPeep.hpp" # include "bindings/entity/ScStaff.hpp" +# include "bindings/entity/ScVehicle.hpp" # include "bindings/game/ScCheats.hpp" # include "bindings/game/ScConsole.hpp" # include "bindings/game/ScContext.hpp" diff --git a/src/openrct2/scripting/bindings/world/ScMap.cpp b/src/openrct2/scripting/bindings/world/ScMap.cpp new file mode 100644 index 0000000000..c3e95f20fa --- /dev/null +++ b/src/openrct2/scripting/bindings/world/ScMap.cpp @@ -0,0 +1,273 @@ +/***************************************************************************** + * Copyright (c) 2014-2021 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#ifdef ENABLE_SCRIPTING + +# include "ScMap.hpp" + +# include "../../../common.h" +# include "../../../ride/Ride.h" +# include "../../../ride/TrainManager.h" +# include "../../../world/Balloon.h" +# include "../../../world/Duck.h" +# include "../../../world/EntityList.h" +# include "../../../world/Fountain.h" +# include "../../../world/Litter.h" +# include "../../../world/Map.h" +# include "../../../world/MoneyEffect.h" +# include "../../../world/Particle.h" +# include "../../Duktape.hpp" +# include "../entity/ScEntity.hpp" +# include "../entity/ScGuest.hpp" +# include "../entity/ScLitter.hpp" +# include "../entity/ScStaff.hpp" +# include "../entity/ScVehicle.hpp" +# include "../ride/ScRide.hpp" +# include "../world/ScTile.hpp" + +namespace OpenRCT2::Scripting +{ + ScMap::ScMap(duk_context* ctx) + : _context(ctx) + { + } + + DukValue ScMap::size_get() const + { + return ToDuk(_context, CoordsXY{ gMapSize, gMapSize }); + } + + int32_t ScMap::numRides_get() const + { + return static_cast(GetRideManager().size()); + } + + int32_t ScMap::numEntities_get() const + { + return MAX_ENTITIES; + } + + std::vector> ScMap::rides_get() const + { + std::vector> result; + auto rideManager = GetRideManager(); + for (const auto& ride : rideManager) + { + result.push_back(std::make_shared(ride.id)); + } + return result; + } + + std::shared_ptr ScMap::getRide(int32_t id) const + { + auto rideManager = GetRideManager(); + auto ride = rideManager[static_cast(id)]; + if (ride != nullptr) + { + return std::make_shared(ride->id); + } + return {}; + } + + std::shared_ptr ScMap::getTile(int32_t x, int32_t y) const + { + auto coords = TileCoordsXY(x, y).ToCoordsXY(); + return std::make_shared(coords); + } + + DukValue ScMap::getEntity(int32_t id) const + { + if (id >= 0 && id < MAX_ENTITIES) + { + auto spriteId = static_cast(id); + auto sprite = GetEntity(spriteId); + if (sprite != nullptr && sprite->Type != EntityType::Null) + { + return GetEntityAsDukValue(sprite); + } + } + duk_push_null(_context); + return DukValue::take_from_stack(_context); + } + + std::vector ScMap::getAllEntities(const std::string& type) const + { + std::vector result; + if (type == "balloon") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "car") + { + for (auto trainHead : TrainManager::View()) + { + for (auto carId = trainHead->sprite_index; carId != SPRITE_INDEX_NULL;) + { + auto car = GetEntity(carId); + result.push_back(GetObjectAsDukValue(_context, std::make_shared(carId))); + carId = car->next_vehicle_on_train; + } + } + } + else if (type == "litter") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "duck") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "peep") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "guest") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "staff") + { + for (auto sprite : EntityList()) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else + { + duk_error(_context, DUK_ERR_ERROR, "Invalid entity type."); + } + + return result; + } + + template + DukValue createEntityType(duk_context* ctx, const DukValue& initializer) + { + TEntityType* entity = CreateEntity(); + + auto entityPos = CoordsXYZ{ AsOrDefault(initializer["x"], 0), AsOrDefault(initializer["y"], 0), + AsOrDefault(initializer["z"], 0) }; + entity->MoveTo(entityPos); + + return GetObjectAsDukValue(ctx, std::make_shared(entity->sprite_index)); + } + + DukValue ScMap::createEntity(const std::string& type, const DukValue& initializer) + { + if (type == "car") + { + return createEntityType(_context, initializer); + } + else if (type == "staff") + { + return createEntityType(_context, initializer); + } + else if (type == "guest") + { + return createEntityType(_context, initializer); + } + else if (type == "steam_particle") + { + return createEntityType(_context, initializer); + } + else if (type == "money_effect") + { + return createEntityType(_context, initializer); + } + else if (type == "crashed_vehicle_particle") + { + return createEntityType(_context, initializer); + } + else if (type == "explosion_cloud") + { + return createEntityType(_context, initializer); + } + else if (type == "crash_splash") + { + return createEntityType(_context, initializer); + } + else if (type == "explosion_flare") + { + return createEntityType(_context, initializer); + } + else if (type == "balloon") + { + return createEntityType(_context, initializer); + } + else if (type == "duck") + { + return createEntityType(_context, initializer); + } + else if (type == "jumping_fountain") + { + return createEntityType(_context, initializer); + } + else if (type == "litter") + { + return createEntityType(_context, initializer); + } + else + { + duk_error(_context, DUK_ERR_ERROR, "Invalid entity type."); + } + } + + void ScMap::Register(duk_context* ctx) + { + dukglue_register_property(ctx, &ScMap::size_get, nullptr, "size"); + dukglue_register_property(ctx, &ScMap::numRides_get, nullptr, "numRides"); + dukglue_register_property(ctx, &ScMap::numEntities_get, nullptr, "numEntities"); + dukglue_register_property(ctx, &ScMap::rides_get, nullptr, "rides"); + dukglue_register_method(ctx, &ScMap::getRide, "getRide"); + dukglue_register_method(ctx, &ScMap::getTile, "getTile"); + dukglue_register_method(ctx, &ScMap::getEntity, "getEntity"); + dukglue_register_method(ctx, &ScMap::getAllEntities, "getAllEntities"); + dukglue_register_method(ctx, &ScMap::createEntity, "createEntity"); + } + + DukValue ScMap::GetEntityAsDukValue(const SpriteBase* sprite) const + { + auto spriteId = sprite->sprite_index; + switch (sprite->Type) + { + case EntityType::Vehicle: + return GetObjectAsDukValue(_context, std::make_shared(spriteId)); + case EntityType::Staff: + return GetObjectAsDukValue(_context, std::make_shared(spriteId)); + case EntityType::Guest: + return GetObjectAsDukValue(_context, std::make_shared(spriteId)); + case EntityType::Litter: + return GetObjectAsDukValue(_context, std::make_shared(spriteId)); + default: + return GetObjectAsDukValue(_context, std::make_shared(spriteId)); + } + } + +} // namespace OpenRCT2::Scripting + +#endif diff --git a/src/openrct2/scripting/bindings/world/ScMap.hpp b/src/openrct2/scripting/bindings/world/ScMap.hpp index 461ec53c56..a2994fd04b 100644 --- a/src/openrct2/scripting/bindings/world/ScMap.hpp +++ b/src/openrct2/scripting/bindings/world/ScMap.hpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -12,19 +12,7 @@ #ifdef ENABLE_SCRIPTING # include "../../../common.h" -# include "../../../ride/Ride.h" -# include "../../../ride/TrainManager.h" -# include "../../../world/Balloon.h" -# include "../../../world/Duck.h" -# include "../../../world/EntityList.h" -# include "../../../world/Fountain.h" -# include "../../../world/Litter.h" -# include "../../../world/Map.h" -# include "../../../world/MoneyEffect.h" -# include "../../../world/Particle.h" # include "../../Duktape.hpp" -# include "../entity/ScEntity.hpp" -# include "../entity/ScVehicle.hpp" # include "../ride/ScRide.hpp" # include "../world/ScTile.hpp" @@ -36,243 +24,32 @@ namespace OpenRCT2::Scripting duk_context* _context; public: - ScMap(duk_context* ctx) - : _context(ctx) - { - } + ScMap(duk_context* ctx); - DukValue size_get() const - { - return ToDuk(_context, CoordsXY{ gMapSize, gMapSize }); - } + DukValue size_get() const; - int32_t numRides_get() const - { - return static_cast(GetRideManager().size()); - } + int32_t numRides_get() const; - int32_t numEntities_get() const - { - return MAX_ENTITIES; - } + int32_t numEntities_get() const; - std::vector> rides_get() const - { - std::vector> result; - auto rideManager = GetRideManager(); - for (const auto& ride : rideManager) - { - result.push_back(std::make_shared(ride.id)); - } - return result; - } + std::vector> rides_get() const; - std::shared_ptr getRide(int32_t id) const - { - auto rideManager = GetRideManager(); - auto ride = rideManager[static_cast(id)]; - if (ride != nullptr) - { - return std::make_shared(ride->id); - } - return {}; - } + std::shared_ptr getRide(int32_t id) const; - std::shared_ptr getTile(int32_t x, int32_t y) const - { - auto coords = TileCoordsXY(x, y).ToCoordsXY(); - return std::make_shared(coords); - } + std::shared_ptr getTile(int32_t x, int32_t y) const; - DukValue getEntity(int32_t id) const - { - if (id >= 0 && id < MAX_ENTITIES) - { - auto spriteId = static_cast(id); - auto sprite = GetEntity(spriteId); - if (sprite != nullptr && sprite->Type != EntityType::Null) - { - return GetEntityAsDukValue(sprite); - } - } - duk_push_null(_context); - return DukValue::take_from_stack(_context); - } + DukValue getEntity(int32_t id) const; - std::vector getAllEntities(const std::string& type) const - { - std::vector result; - if (type == "balloon") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else if (type == "car") - { - for (auto trainHead : TrainManager::View()) - { - for (auto carId = trainHead->sprite_index; carId != SPRITE_INDEX_NULL;) - { - auto car = GetEntity(carId); - result.push_back(GetObjectAsDukValue(_context, std::make_shared(carId))); - carId = car->next_vehicle_on_train; - } - } - } - else if (type == "litter") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else if (type == "duck") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else if (type == "peep") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else if (type == "guest") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else if (type == "staff") - { - for (auto sprite : EntityList()) - { - result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); - } - } - else - { - duk_error(_context, DUK_ERR_ERROR, "Invalid entity type."); - } + std::vector getAllEntities(const std::string& type) const; - return result; - } + DukValue createEntity(const std::string& type, const DukValue& initializer); - template DukValue createEntityType(const DukValue& initializer) - { - TEntityType* entity = CreateEntity(); - - auto entityPos = CoordsXYZ{ AsOrDefault(initializer["x"], 0), AsOrDefault(initializer["y"], 0), - AsOrDefault(initializer["z"], 0) }; - entity->MoveTo(entityPos); - - return GetObjectAsDukValue(_context, std::make_shared(entity->sprite_index)); - } - - DukValue createEntity(const std::string& type, const DukValue& initializer) - { - if (type == "car") - { - return createEntityType(initializer); - } - else if (type == "staff") - { - return createEntityType(initializer); - } - else if (type == "guest") - { - return createEntityType(initializer); - } - else if (type == "steam_particle") - { - return createEntityType(initializer); - } - else if (type == "money_effect") - { - return createEntityType(initializer); - } - else if (type == "crashed_vehicle_particle") - { - return createEntityType(initializer); - } - else if (type == "explosion_cloud") - { - return createEntityType(initializer); - } - else if (type == "crash_splash") - { - return createEntityType(initializer); - } - else if (type == "explosion_flare") - { - return createEntityType(initializer); - } - else if (type == "balloon") - { - return createEntityType(initializer); - } - else if (type == "duck") - { - return createEntityType(initializer); - } - else if (type == "jumping_fountain") - { - return createEntityType(initializer); - } - else if (type == "litter") - { - return createEntityType(initializer); - } - else - { - duk_error(_context, DUK_ERR_ERROR, "Invalid entity type."); - } - - return DukValue(); - } - - static void Register(duk_context* ctx) - { - dukglue_register_property(ctx, &ScMap::size_get, nullptr, "size"); - dukglue_register_property(ctx, &ScMap::numRides_get, nullptr, "numRides"); - dukglue_register_property(ctx, &ScMap::numEntities_get, nullptr, "numEntities"); - dukglue_register_property(ctx, &ScMap::rides_get, nullptr, "rides"); - dukglue_register_method(ctx, &ScMap::getRide, "getRide"); - dukglue_register_method(ctx, &ScMap::getTile, "getTile"); - dukglue_register_method(ctx, &ScMap::getEntity, "getEntity"); - dukglue_register_method(ctx, &ScMap::getAllEntities, "getAllEntities"); - dukglue_register_method(ctx, &ScMap::createEntity, "createEntity"); - } + static void Register(duk_context* ctx); private: - DukValue GetEntityAsDukValue(const SpriteBase* sprite) const - { - auto spriteId = sprite->sprite_index; - switch (sprite->Type) - { - case EntityType::Vehicle: - return GetObjectAsDukValue(_context, std::make_shared(spriteId)); - case EntityType::Staff: - return GetObjectAsDukValue(_context, std::make_shared(spriteId)); - case EntityType::Guest: - return GetObjectAsDukValue(_context, std::make_shared(spriteId)); - case EntityType::Litter: - return GetObjectAsDukValue(_context, std::make_shared(spriteId)); - default: - return GetObjectAsDukValue(_context, std::make_shared(spriteId)); - } - } + DukValue GetEntityAsDukValue(const SpriteBase* sprite) const; }; + } // namespace OpenRCT2::Scripting #endif