Files
OpenRCT2-Web/src/openrct2/scripting/bindings/world/ScMap.cpp
T

358 lines
12 KiB
C++
Raw Normal View History

2021-08-10 15:05:50 +03:00
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2021-08-10 15:05:50 +03:00
*
* 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"
2024-02-12 21:32:08 +00:00
# include "../../../GameState.h"
2021-08-10 15:05:50 +03:00
# include "../../../common.h"
2021-11-24 16:48:33 +02:00
# include "../../../entity/Balloon.h"
# include "../../../entity/Duck.h"
# include "../../../entity/EntityList.h"
# include "../../../entity/Fountain.h"
2021-11-25 23:47:24 +02:00
# include "../../../entity/Guest.h"
2021-11-24 16:48:33 +02:00
# include "../../../entity/Litter.h"
# include "../../../entity/MoneyEffect.h"
# include "../../../entity/Particle.h"
2021-11-25 23:47:24 +02:00
# include "../../../entity/Staff.h"
2021-08-10 15:05:50 +03:00
# include "../../../ride/Ride.h"
# include "../../../ride/TrainManager.h"
# include "../../../world/Map.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"
2022-03-13 03:02:55 +00:00
# include "../ride/ScTrackIterator.h"
2021-08-10 15:05:50 +03:00
# include "../world/ScTile.hpp"
namespace OpenRCT2::Scripting
{
ScMap::ScMap(duk_context* ctx)
: _context(ctx)
{
}
DukValue ScMap::size_get() const
{
2024-02-12 21:32:08 +00:00
return ToDuk(_context, GetGameState().MapSize);
2021-08-10 15:05:50 +03:00
}
int32_t ScMap::numRides_get() const
{
return static_cast<int32_t>(GetRideManager().size());
}
int32_t ScMap::numEntities_get() const
{
return MAX_ENTITIES;
}
std::vector<std::shared_ptr<ScRide>> ScMap::rides_get() const
{
std::vector<std::shared_ptr<ScRide>> result;
auto rideManager = GetRideManager();
for (const auto& ride : rideManager)
{
result.push_back(std::make_shared<ScRide>(ride.id));
}
return result;
}
std::shared_ptr<ScRide> ScMap::getRide(int32_t id) const
{
auto rideManager = GetRideManager();
2022-01-19 16:54:57 +02:00
auto ride = rideManager[RideId::FromUnderlying(id)];
2021-08-10 15:05:50 +03:00
if (ride != nullptr)
{
return std::make_shared<ScRide>(ride->id);
}
return {};
}
std::shared_ptr<ScTile> ScMap::getTile(int32_t x, int32_t y) const
{
auto coords = TileCoordsXY(x, y).ToCoordsXY();
return std::make_shared<ScTile>(coords);
}
DukValue ScMap::getEntity(int32_t id) const
{
if (id >= 0 && id < MAX_ENTITIES)
{
2022-02-12 23:31:06 +02:00
auto spriteId = EntityId::FromUnderlying(id);
2021-08-10 15:05:50 +03:00
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<DukValue> ScMap::getAllEntities(const std::string& type) const
{
std::vector<DukValue> result;
if (type == "balloon")
{
for (auto sprite : EntityList<Balloon>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else if (type == "car")
{
for (auto trainHead : TrainManager::View())
{
2023-01-27 19:18:44 +02:00
for (auto carId = trainHead->Id; !carId.IsNull();)
2021-08-10 15:05:50 +03:00
{
auto car = GetEntity<Vehicle>(carId);
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(carId)));
carId = car->next_vehicle_on_train;
}
}
}
else if (type == "litter")
{
for (auto sprite : EntityList<Litter>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else if (type == "duck")
{
for (auto sprite : EntityList<Duck>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else if (type == "peep")
{
for (auto sprite : EntityList<Guest>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
for (auto sprite : EntityList<Staff>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else if (type == "guest")
{
for (auto sprite : EntityList<Guest>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else if (type == "staff")
{
for (auto sprite : EntityList<Staff>())
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->Id)));
2021-08-10 15:05:50 +03:00
}
}
else
{
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type.");
}
return result;
}
std::vector<DukValue> OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile(
const std::string& type, const DukValue& tilePos) const
{
// Get the tile position
const auto pos = FromDuk<CoordsXY>(tilePos);
// Declare a vector that will hold the result to return
std::vector<DukValue> result;
// Use EntityTileList to iterate all entities of the given type on the tile, and push them to result
if (type == "balloon")
{
for (auto sprite : EntityTileList<Balloon>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->Id)));
}
}
else if (type == "car")
{
for (auto sprite : EntityTileList<Vehicle>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(sprite->Id)));
}
}
else if (type == "litter")
{
for (auto sprite : EntityTileList<Litter>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->Id)));
}
}
else if (type == "duck")
{
for (auto sprite : EntityTileList<Duck>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->Id)));
}
}
else if (type == "guest")
{
for (auto sprite : EntityTileList<Guest>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->Id)));
}
}
else if (type == "staff")
{
for (auto sprite : EntityTileList<Staff>(pos))
{
2023-01-27 19:18:44 +02:00
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->Id)));
}
}
else
{
// If the given type isn't valid, throw an error
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type: %s", type.c_str());
}
return result;
}
2021-08-10 15:05:50 +03:00
template<typename TEntityType, typename TScriptType>
DukValue createEntityType(duk_context* ctx, const DukValue& initializer)
{
TEntityType* entity = CreateEntity<TEntityType>();
if (entity == nullptr)
{
// Probably no more space for entities for this specified entity type.
return ToDuk(ctx, undefined);
}
2021-08-10 15:05:50 +03:00
auto entityPos = CoordsXYZ{ AsOrDefault(initializer["x"], 0), AsOrDefault(initializer["y"], 0),
AsOrDefault(initializer["z"], 0) };
entity->MoveTo(entityPos);
2023-01-27 19:18:44 +02:00
return GetObjectAsDukValue(ctx, std::make_shared<TScriptType>(entity->Id));
2021-08-10 15:05:50 +03:00
}
DukValue ScMap::createEntity(const std::string& type, const DukValue& initializer)
{
2021-08-16 23:36:58 +02:00
DukValue res;
2021-08-10 15:05:50 +03:00
if (type == "car")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Vehicle, ScVehicle>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "staff")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Staff, ScStaff>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "guest")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Guest, ScGuest>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "steam_particle")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<SteamParticle, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "money_effect")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<MoneyEffect, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "crashed_vehicle_particle")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<VehicleCrashParticle, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "explosion_cloud")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<ExplosionCloud, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "crash_splash")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<CrashSplashParticle, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "explosion_flare")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<ExplosionFlare, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "balloon")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Balloon, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "duck")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Duck, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "jumping_fountain")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<JumpingFountain, ScEntity>(_context, initializer);
2021-08-10 15:05:50 +03:00
}
else if (type == "litter")
{
2021-08-16 23:36:58 +02:00
res = createEntityType<Litter, ScLitter>(_context, initializer);
}
else
{
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type.");
2021-08-10 15:05:50 +03:00
}
2021-08-11 22:11:25 +03:00
2021-08-16 23:36:58 +02:00
return res;
2021-08-10 15:05:50 +03:00
}
2022-03-13 03:02:55 +00:00
DukValue ScMap::getTrackIterator(const DukValue& dukPosition, int32_t elementIndex) const
{
auto position = FromDuk<CoordsXY>(dukPosition);
auto trackIterator = ScTrackIterator::FromElement(position, elementIndex);
if (trackIterator == nullptr)
return ToDuk(_context, undefined);
return GetObjectAsDukValue(_context, trackIterator);
}
2021-08-10 15:05:50 +03:00
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::getAllEntitiesOnTile, "getAllEntitiesOnTile");
2021-08-10 15:05:50 +03:00
dukglue_register_method(ctx, &ScMap::createEntity, "createEntity");
2022-03-13 03:02:55 +00:00
dukglue_register_method(ctx, &ScMap::getTrackIterator, "getTrackIterator");
2021-08-10 15:05:50 +03:00
}
2021-08-25 17:42:09 +03:00
DukValue ScMap::GetEntityAsDukValue(const EntityBase* sprite) const
2021-08-10 15:05:50 +03:00
{
2023-01-27 19:18:44 +02:00
auto spriteId = sprite->Id;
2021-08-10 15:05:50 +03:00
switch (sprite->Type)
{
case EntityType::Vehicle:
return GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(spriteId));
case EntityType::Staff:
return GetObjectAsDukValue(_context, std::make_shared<ScStaff>(spriteId));
case EntityType::Guest:
return GetObjectAsDukValue(_context, std::make_shared<ScGuest>(spriteId));
case EntityType::Litter:
return GetObjectAsDukValue(_context, std::make_shared<ScLitter>(spriteId));
default:
return GetObjectAsDukValue(_context, std::make_shared<ScEntity>(spriteId));
}
}
} // namespace OpenRCT2::Scripting
#endif