From af59e0dc90e0ab2fe82d1cf6086b872244eb74ad Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Sat, 4 Jun 2022 19:36:55 +0200 Subject: [PATCH 1/7] expose core functions Exposed/added functions/methods: - IsPlayer() - GameObject:AddLoot(entry,amount) --- src/LuaEngine/GameObjectMethods.h | 60 +++++++++++++++++++++++++++++++ src/LuaEngine/LuaFunctions.cpp | 2 ++ src/LuaEngine/ObjectMethods.h | 23 ++++++++---- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index b96305f2ec..0c9b2f1e81 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -235,6 +235,66 @@ namespace LuaGameObject return 0; } + + /** + * Adds an [Item] to the loot of a [GameObject] + * + * @param uint32 entry : The entry of the [Item] + * @param uint32 amount = 1 : amount of the [Item] to add to the loot + * @return uint32 itemGUIDlow : low GUID of the [Item] + */ + + int AddLoot(lua_State* L, GameObject* go) + { + int i = 0; + uint32 entry = Eluna::CHECKVAL(L, ++i); + uint32 amount = Eluna::CHECKVAL(L, ++i, 1); + int argAmount = lua_gettop(L); + +#if defined TRINITY || defined AZEROTHCORE + CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction(); +#endif + uint8 addedItems = 0; + while (i + 2 <= argAmount) + { + uint32 entry = Eluna::CHECKVAL(L, ++i); + uint32 amount = Eluna::CHECKVAL(L, ++i); + +#if defined TRINITY || AZEROTHCORE + ItemTemplate const* item_proto = eObjectMgr->GetItemTemplate(entry); +#else + ItemTemplate const* item_proto = ObjectMgr::GetItemPrototype(entry); +#endif + if (!item_proto) + { + luaL_error(L, "Item entry %d does not exist", entry); + continue; + } + if (amount < 1 || (item_proto->MaxCount > 0 && amount > uint32(item_proto->MaxCount))) + { + luaL_error(L, "Item entry %d has invalid amount %d", entry, amount); + continue; + } + if (Item* item = Item::CreateItem(entry, amount)) + { +#if defined TRINITY || AZEROTHCORE + item->SaveToDB(trans); +#else + item->SaveToDB(); +#endif + LootStoreItem storeItem = LootStoreItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, 1, 1); + go->loot.AddItem(storeItem); +#if defined TRINITY || AZEROTHCORE + Eluna::Push(L, item->GetGUID().GetCounter()); +#else + Eluna::Push(L, item->GetGUIDLow()); +#endif + ++addedItems; + } + } + return addedItems; + } + /** * Saves [GameObject] to the database * diff --git a/src/LuaEngine/LuaFunctions.cpp b/src/LuaEngine/LuaFunctions.cpp index 55ce5860ed..3194ac3af1 100644 --- a/src/LuaEngine/LuaFunctions.cpp +++ b/src/LuaEngine/LuaFunctions.cpp @@ -180,6 +180,7 @@ ElunaRegister ObjectMethods[] = // Boolean { "IsInWorld", &LuaObject::IsInWorld }, + { "IsPlayer", &LuaObject::IsPlayer }, { "HasFlag", &LuaObject::HasFlag }, // Other @@ -914,6 +915,7 @@ ElunaRegister GameObjectMethods[] = { "Despawn", &LuaGameObject::Despawn }, { "Respawn", &LuaGameObject::Respawn }, { "SaveToDB", &LuaGameObject::SaveToDB }, + { "AddLoot", &LuaGameObject::AddLoot }, { NULL, NULL } }; diff --git a/src/LuaEngine/ObjectMethods.h b/src/LuaEngine/ObjectMethods.h index 156d63fe17..768710d67e 100644 --- a/src/LuaEngine/ObjectMethods.h +++ b/src/LuaEngine/ObjectMethods.h @@ -51,6 +51,17 @@ namespace LuaObject return 1; } + /** + * Returns 'true' if the [Object] is a player, 'false' otherwise. + * + * @return bool IsPlayer + */ + int IsPlayer(lua_State* L, Player* player) + { + Eluna::Push(L, player->IsPlayer()); + return 1; + } + /** * Returns the data at the specified index, casted to a signed 32-bit integer. * @@ -156,14 +167,14 @@ namespace LuaObject /** * Returns the GUID of the [Object]. - * + * * GUID is an unique identifier for the object. - * + * * However on MaNGOS and cMangos creatures and gameobjects inside different maps can share * the same GUID but not on the same map. - * + * * On TrinityCore this value is unique across all maps - * + * * @return ObjectGuid guid */ int GetGUID(lua_State* L, Object* obj) @@ -174,10 +185,10 @@ namespace LuaObject /** * Returns the low-part of the [Object]'s GUID. - * + * * On TrinityCore all low GUIDs are different for all objects of the same type. * For example creatures in instances are assigned new GUIDs when the Map is created. - * + * * On MaNGOS and cMaNGOS low GUIDs are unique only on the same map. * For example creatures in instances use the same low GUID assigned for that spawn in the database. * This is why to identify a creature you have to know the instanceId and low GUID. See [Map:GetIntstanceId] From c616143557a39a98c214dcc5ed7213bb96a871f4 Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Sat, 4 Jun 2022 20:11:06 +0200 Subject: [PATCH 2/7] fix missing stuff --- src/LuaEngine/GameObjectMethods.h | 1 - src/LuaEngine/ObjectMethods.h | 16 +++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index 0c9b2f1e81..0ffbf2cd56 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -235,7 +235,6 @@ namespace LuaGameObject return 0; } - /** * Adds an [Item] to the loot of a [GameObject] * diff --git a/src/LuaEngine/ObjectMethods.h b/src/LuaEngine/ObjectMethods.h index 768710d67e..d96fbdfd8b 100644 --- a/src/LuaEngine/ObjectMethods.h +++ b/src/LuaEngine/ObjectMethods.h @@ -52,16 +52,18 @@ namespace LuaObject } /** - * Returns 'true' if the [Object] is a player, 'false' otherwise. - * - * @return bool IsPlayer - */ - int IsPlayer(lua_State* L, Player* player) + * Returns 'true' if the [Object] is a player, 'false' otherwise. + * + * @return bool IsPlayer + */ + int IsPlayer(lua_State* L, Object* obj) { - Eluna::Push(L, player->IsPlayer()); +#ifdef AZEROTHCORE //AC-only + Eluna::Push(L, obj->IsPlayer()); +#endif return 1; } - + /** * Returns the data at the specified index, casted to a signed 32-bit integer. * From 2920ddd67526f809439bf6ee07b4eb527a23ad4b Mon Sep 17 00:00:00 2001 From: Axel Cocat Date: Sat, 4 Jun 2022 21:13:55 +0200 Subject: [PATCH 3/7] fix AddLoot --- src/LuaEngine/GameObjectMethods.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index 0ffbf2cd56..f51e26a363 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -245,9 +245,7 @@ namespace LuaGameObject int AddLoot(lua_State* L, GameObject* go) { - int i = 0; - uint32 entry = Eluna::CHECKVAL(L, ++i); - uint32 amount = Eluna::CHECKVAL(L, ++i, 1); + int i = 1; int argAmount = lua_gettop(L); #if defined TRINITY || defined AZEROTHCORE @@ -281,7 +279,7 @@ namespace LuaGameObject #else item->SaveToDB(); #endif - LootStoreItem storeItem = LootStoreItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, 1, 1); + LootStoreItem storeItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, item->GetCount(), item->GetCount()); go->loot.AddItem(storeItem); #if defined TRINITY || AZEROTHCORE Eluna::Push(L, item->GetGUID().GetCounter()); @@ -291,6 +289,11 @@ namespace LuaGameObject ++addedItems; } } + +#if defined TRINITY || AZEROTHCORE + CharacterDatabase.CommitTransaction(trans); +#endif + return addedItems; } From 78984d8a959648ae378a1cd1b2b7769414ba5ac9 Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Sun, 5 Jun 2022 20:53:07 +0200 Subject: [PATCH 4/7] remove extra code --- src/LuaEngine/GameObjectMethods.h | 43 +++---------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index f51e26a363..3e63747d76 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -240,7 +240,6 @@ namespace LuaGameObject * * @param uint32 entry : The entry of the [Item] * @param uint32 amount = 1 : amount of the [Item] to add to the loot - * @return uint32 itemGUIDlow : low GUID of the [Item] */ int AddLoot(lua_State* L, GameObject* go) @@ -248,52 +247,16 @@ namespace LuaGameObject int i = 1; int argAmount = lua_gettop(L); -#if defined TRINITY || defined AZEROTHCORE - CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction(); -#endif uint8 addedItems = 0; while (i + 2 <= argAmount) { uint32 entry = Eluna::CHECKVAL(L, ++i); uint32 amount = Eluna::CHECKVAL(L, ++i); -#if defined TRINITY || AZEROTHCORE - ItemTemplate const* item_proto = eObjectMgr->GetItemTemplate(entry); -#else - ItemTemplate const* item_proto = ObjectMgr::GetItemPrototype(entry); -#endif - if (!item_proto) - { - luaL_error(L, "Item entry %d does not exist", entry); - continue; - } - if (amount < 1 || (item_proto->MaxCount > 0 && amount > uint32(item_proto->MaxCount))) - { - luaL_error(L, "Item entry %d has invalid amount %d", entry, amount); - continue; - } - if (Item* item = Item::CreateItem(entry, amount)) - { -#if defined TRINITY || AZEROTHCORE - item->SaveToDB(trans); -#else - item->SaveToDB(); -#endif - LootStoreItem storeItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, item->GetCount(), item->GetCount()); - go->loot.AddItem(storeItem); -#if defined TRINITY || AZEROTHCORE - Eluna::Push(L, item->GetGUID().GetCounter()); -#else - Eluna::Push(L, item->GetGUIDLow()); -#endif - ++addedItems; - } + LootStoreItem storeItem(entry, 0, 100, 0, LOOT_MODE_DEFAULT, 0, amount, amount); + go->loot.AddItem(storeItem); + ++addedItems; } - -#if defined TRINITY || AZEROTHCORE - CharacterDatabase.CommitTransaction(trans); -#endif - return addedItems; } From 22b58552c4cf9c8314bd81a936566c8721f49a16 Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Sun, 5 Jun 2022 23:06:48 +0200 Subject: [PATCH 5/7] remove more extra code --- src/LuaEngine/GameObjectMethods.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index 3e63747d76..e416102534 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -247,7 +247,6 @@ namespace LuaGameObject int i = 1; int argAmount = lua_gettop(L); - uint8 addedItems = 0; while (i + 2 <= argAmount) { uint32 entry = Eluna::CHECKVAL(L, ++i); @@ -255,9 +254,8 @@ namespace LuaGameObject LootStoreItem storeItem(entry, 0, 100, 0, LOOT_MODE_DEFAULT, 0, amount, amount); go->loot.AddItem(storeItem); - ++addedItems; } - return addedItems; + return 0; } /** From 0b5ca428d33cc27673bb53b0dbc913041fc4be52 Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Tue, 7 Jun 2022 11:30:18 +0200 Subject: [PATCH 6/7] remove AddLoot() --- src/LuaEngine/GameObjectMethods.h | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/LuaEngine/GameObjectMethods.h b/src/LuaEngine/GameObjectMethods.h index e416102534..b96305f2ec 100644 --- a/src/LuaEngine/GameObjectMethods.h +++ b/src/LuaEngine/GameObjectMethods.h @@ -235,29 +235,6 @@ namespace LuaGameObject return 0; } - /** - * Adds an [Item] to the loot of a [GameObject] - * - * @param uint32 entry : The entry of the [Item] - * @param uint32 amount = 1 : amount of the [Item] to add to the loot - */ - - int AddLoot(lua_State* L, GameObject* go) - { - int i = 1; - int argAmount = lua_gettop(L); - - while (i + 2 <= argAmount) - { - uint32 entry = Eluna::CHECKVAL(L, ++i); - uint32 amount = Eluna::CHECKVAL(L, ++i); - - LootStoreItem storeItem(entry, 0, 100, 0, LOOT_MODE_DEFAULT, 0, amount, amount); - go->loot.AddItem(storeItem); - } - return 0; - } - /** * Saves [GameObject] to the database * From b389961ebc27ccd47b42679ff7c69c04285120ac Mon Sep 17 00:00:00 2001 From: 55Honey <71938210+55Honey@users.noreply.github.com> Date: Tue, 7 Jun 2022 11:31:27 +0200 Subject: [PATCH 7/7] remove AddLoot() 2 --- src/LuaEngine/LuaFunctions.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/LuaEngine/LuaFunctions.cpp b/src/LuaEngine/LuaFunctions.cpp index 3194ac3af1..bc2c865f1b 100644 --- a/src/LuaEngine/LuaFunctions.cpp +++ b/src/LuaEngine/LuaFunctions.cpp @@ -915,7 +915,6 @@ ElunaRegister GameObjectMethods[] = { "Despawn", &LuaGameObject::Despawn }, { "Respawn", &LuaGameObject::Respawn }, { "SaveToDB", &LuaGameObject::SaveToDB }, - { "AddLoot", &LuaGameObject::AddLoot }, { NULL, NULL } };