From f31dbd03c6a163dabe5e078da2088b0c447e2cf2 Mon Sep 17 00:00:00 2001 From: mostlynick3 Date: Wed, 18 Mar 2026 16:19:06 +0100 Subject: [PATCH] feat(TransportMethods): Add new Transports class, expose basic transport getters and passenger handling (#371) --- src/LuaEngine/ALEIncludes.h | 1 + src/LuaEngine/LuaFunctions.cpp | 25 ++++++ src/LuaEngine/methods/MapMethods.h | 19 +++++ src/LuaEngine/methods/TransportMethods.h | 93 ++++++++++++++++++++++ src/LuaEngine/methods/WorldObjectMethods.h | 11 +++ 5 files changed, 149 insertions(+) create mode 100644 src/LuaEngine/methods/TransportMethods.h diff --git a/src/LuaEngine/ALEIncludes.h b/src/LuaEngine/ALEIncludes.h index 7b305cd..bc38f08 100644 --- a/src/LuaEngine/ALEIncludes.h +++ b/src/LuaEngine/ALEIncludes.h @@ -36,6 +36,7 @@ #include "SpellInfo.h" #include "SpellMgr.h" #include "TemporarySummon.h" +#include "Transport.h" #include "WorldPacket.h" #include "WorldSession.h" #include "MapMgr.h" diff --git a/src/LuaEngine/LuaFunctions.cpp b/src/LuaEngine/LuaFunctions.cpp index 7da5c9e..9c0ee65 100644 --- a/src/LuaEngine/LuaFunctions.cpp +++ b/src/LuaEngine/LuaFunctions.cpp @@ -44,6 +44,7 @@ extern "C" #include "SpellInfoMethods.h" #include "PetMethods.h" #include "LootMethods.h" +#include "TransportMethods.h" // DBCStores includes #include "GemPropertiesEntryMethods.h" @@ -258,6 +259,7 @@ ALERegister WorldObjectMethods[] = { "GetExactDistance2d", &LuaWorldObject::GetExactDistance2d }, { "GetRelativePoint", &LuaWorldObject::GetRelativePoint }, { "GetAngle", &LuaWorldObject::GetAngle }, + { "GetTransport", &LuaWorldObject::GetTransport }, // Boolean { "IsWithinLoS", &LuaWorldObject::IsWithinLoS }, @@ -1315,6 +1317,7 @@ ALERegister MapMethods[] = { "GetWorldObject", &LuaMap::GetWorldObject }, { "GetCreatures", &LuaMap::GetCreatures }, { "GetCreaturesByAreaId", &LuaMap::GetCreaturesByAreaId }, + { "GetTransports", &LuaMap::GetTransports }, // Setters @@ -1800,6 +1803,22 @@ ALERegister LootMethods[] = { NULL, NULL } }; +ALERegister TransportMethods[] = +{ + // Getters + { "GetPassengers", &LuaTransport::GetPassengers }, + + // Boolean + { "IsMotionTransport", &LuaTransport::IsMotionTransport }, + + // Other + { "AddPassenger", &LuaTransport::AddPassenger }, + { "RemovePassenger", &LuaTransport::RemovePassenger }, + { "EnableMovement", &LuaTransport::EnableMovement }, + + { NULL, NULL } +}; + // fix compile error about accessing vehicle destructor template<> int ALETemplate::CollectGarbage(lua_State* L) { @@ -1891,6 +1910,12 @@ void RegisterFunctions(ALE* E) ALETemplate::SetMethods(E, WorldObjectMethods); ALETemplate::SetMethods(E, GameObjectMethods); + ALETemplate::Register(E, "Transport"); + ALETemplate::SetMethods(E, ObjectMethods); + ALETemplate::SetMethods(E, WorldObjectMethods); + ALETemplate::SetMethods(E, GameObjectMethods); + ALETemplate::SetMethods(E, TransportMethods); + ALETemplate::Register(E, "Corpse"); ALETemplate::SetMethods(E, ObjectMethods); ALETemplate::SetMethods(E, WorldObjectMethods); diff --git a/src/LuaEngine/methods/MapMethods.h b/src/LuaEngine/methods/MapMethods.h index 04eba56..79a1a08 100644 --- a/src/LuaEngine/methods/MapMethods.h +++ b/src/LuaEngine/methods/MapMethods.h @@ -379,5 +379,24 @@ namespace LuaMap lua_settop(L, tbl); return 1; } + + + /** + * Returns a table of all [Transport]s on the [Map] + * + * @return table transports + */ + int GetTransports(lua_State* L, Map* map) + { + TransportsContainer const& transports = map->GetAllTransports(); + lua_createtable(L, transports.size(), 0); + int i = 1; + for (Transport* transport : transports) + { + ALE::Push(L, transport); + lua_rawseti(L, -2, i++); + } + return 1; + } }; #endif diff --git a/src/LuaEngine/methods/TransportMethods.h b/src/LuaEngine/methods/TransportMethods.h new file mode 100644 index 0000000..941ce67 --- /dev/null +++ b/src/LuaEngine/methods/TransportMethods.h @@ -0,0 +1,93 @@ +/* +* Copyright (C) 2010 - 2025 Eluna Lua Engine +* This program is free software licensed under GPL version 3 +* Please see the included DOCS/LICENSE.md for more information +*/ + +#ifndef TRANSPORTMETHODS_H +#define TRANSPORTMETHODS_H + +#include "Transport.h" + +/*** + * Represents a transport object in the world, such as boats and zeppelins. + * + * Inherits all methods from: [Object], [WorldObject], [GameObject] + */ +namespace LuaTransport +{ + /** + * Returns a table of all passengers on the [Transport] + * + * @return table passengers + */ + int GetPassengers(lua_State* L, Transport* transport) + { + Transport::PassengerSet const& passengers = transport->GetPassengers(); + lua_createtable(L, static_cast(passengers.size()), 0); + int i = 1; + for (WorldObject* passenger : passengers) + { + ALE::Push(L, passenger); + lua_rawseti(L, -2, i++); + } + return 1; + } + + /** + * Returns 'true' if the [Transport] is a MotionTransport (moving transport such as a boat or zeppelin) + * + * @return bool isMotionTransport + */ + int IsMotionTransport(lua_State* L, Transport* transport) + { + ALE::Push(L, dynamic_cast(transport) != nullptr); + return 1; + } + + /** + * Adds a [WorldObject] as a passenger to the [Transport] + * + * @param [WorldObject] passenger : the object to add as a passenger + * @param bool withAll = true : if true, also sets transport movement info on the passenger + */ + int AddPassenger(lua_State* L, Transport* transport) + { + WorldObject* passenger = ALE::CHECKOBJ(L, 2); + bool withAll = ALE::CHECKVAL(L, 3, true); + transport->AddPassenger(passenger, withAll); + return 0; + } + + /** + * Removes a [WorldObject] passenger from the [Transport] + * + * @param [WorldObject] passenger : the object to remove + * @param bool withAll = true : if true, also clears transport movement info from the passenger + */ + int RemovePassenger(lua_State* L, Transport* transport) + { + WorldObject* passenger = ALE::CHECKOBJ(L, 2); + bool withAll = ALE::CHECKVAL(L, 3, true); + transport->RemovePassenger(passenger, withAll); + return 0; + } + + /** + * Enables or disables movement on the [Transport] + * + * Only works on MotionTransports where canBeStopped is set. + * + * @param bool enabled : true to enable movement, false to stop + */ + int EnableMovement(lua_State* L, Transport* transport) + { + bool enabled = ALE::CHECKVAL(L, 2); + MotionTransport* mt = dynamic_cast(transport); + if (mt) + mt->EnableMovement(enabled); + return 0; + } +} + +#endif diff --git a/src/LuaEngine/methods/WorldObjectMethods.h b/src/LuaEngine/methods/WorldObjectMethods.h index 1fd0377..a92a435 100644 --- a/src/LuaEngine/methods/WorldObjectMethods.h +++ b/src/LuaEngine/methods/WorldObjectMethods.h @@ -612,6 +612,17 @@ namespace LuaWorldObject return 1; } + /** + * Returns the transport the [WorldObject] is on, or nil if not on a transport + * + * @return [Transport] transport + */ + int GetTransport(lua_State* L, WorldObject* obj) + { + ALE::Push(L, static_cast(obj->GetTransport())); + return 1; + } + /** * Sends a [WorldPacket] to [Player]s in sight of the [WorldObject]. *