374 lines
10 KiB
C++
374 lines
10 KiB
C++
/*
|
|
* Copyright (C) 2010 - 2025 Eluna Lua Engine <https://elunaluaengine.github.io/>
|
|
* This program is free software licensed under GPL version 3
|
|
* Please see the included DOCS/LICENSE.md for more information
|
|
*/
|
|
|
|
#ifndef GAMEOBJECTMETHODS_H
|
|
#define GAMEOBJECTMETHODS_H
|
|
|
|
/***
|
|
* Represents a game object in the world, such as doors, chests, and other interactive objects.
|
|
*
|
|
* Inherits all methods from: [Object], [WorldObject]
|
|
*/
|
|
namespace LuaGameObject
|
|
{
|
|
/**
|
|
* Returns 'true' if the [GameObject] can give the specified [Quest]
|
|
*
|
|
* @param uint32 questId : quest entry Id to check
|
|
* @return bool hasQuest
|
|
*/
|
|
int HasQuest(lua_State* L, GameObject* go)
|
|
{
|
|
uint32 questId = ALE::CHECKVAL<uint32>(L, 2);
|
|
|
|
ALE::Push(L, go->hasQuest(questId));
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns 'true' if the [GameObject] is spawned
|
|
*
|
|
* @return bool isSpawned
|
|
*/
|
|
int IsSpawned(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->isSpawned());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns 'true' if the [GameObject] is a transport
|
|
*
|
|
* @return bool isTransport
|
|
*/
|
|
int IsTransport(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->IsTransport());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns 'true' if the [GameObject] is active
|
|
*
|
|
* @return bool isActive
|
|
*/
|
|
int IsActive(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->isActiveObject());
|
|
return 1;
|
|
}
|
|
|
|
/*int IsDestructible(lua_State* L, GameObject* go) // TODO: Implementation core side
|
|
{
|
|
ALE::Push(L, go->IsDestructibleBuilding());
|
|
return 1;
|
|
}*/
|
|
|
|
/**
|
|
* Returns display ID of the [GameObject]
|
|
*
|
|
* @return uint32 displayId
|
|
*/
|
|
int GetDisplayId(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->GetDisplayId());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the state of a [GameObject]
|
|
* Below are client side [GOState]s off of 3.3.5a
|
|
*
|
|
* <pre>
|
|
* enum GOState
|
|
* {
|
|
* GO_STATE_ACTIVE = 0, // show in world as used and not reset (closed door open)
|
|
* GO_STATE_READY = 1, // show in world as ready (closed door close)
|
|
* GO_STATE_ACTIVE_ALTERNATIVE = 2 // show in world as used in alt way and not reset (closed door open by cannon fire)
|
|
* };
|
|
* </pre>
|
|
*
|
|
* @return [GOState] goState
|
|
*/
|
|
int GetGoState(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->GetGoState());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the [LootState] of a [GameObject]
|
|
* Below are [LootState]s off of 3.3.5a
|
|
*
|
|
* <pre>
|
|
* enum LootState
|
|
* {
|
|
* GO_NOT_READY = 0,
|
|
* GO_READY, // can be ready but despawned, and then not possible activate until spawn
|
|
* GO_ACTIVATED,
|
|
* GO_JUST_DEACTIVATED
|
|
* };
|
|
* </pre>
|
|
*
|
|
* @return [LootState] lootState
|
|
*/
|
|
int GetLootState(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->getLootState());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the [Player] that can loot the [GameObject]
|
|
*
|
|
* Not the original looter and may be nil.
|
|
*
|
|
* @return [Player] player
|
|
*/
|
|
int GetLootRecipient(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->GetLootRecipient());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the [Group] that can loot the [GameObject]
|
|
*
|
|
* Not the original looter and may be nil.
|
|
*
|
|
* @return [Group] group
|
|
*/
|
|
int GetLootRecipientGroup(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->GetLootRecipientGroup());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Returns the spawn ID for this [GameObject].
|
|
*
|
|
* @return uint32 spawnId
|
|
*/
|
|
int GetSpawnId(lua_State* L, GameObject* go)
|
|
{
|
|
ALE::Push(L, go->GetSpawnId());
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Sets the state of a [GameObject]
|
|
*
|
|
* <pre>
|
|
* enum GOState
|
|
* {
|
|
* GO_STATE_ACTIVE = 0, // show in world as used and not reset (closed door open)
|
|
* GO_STATE_READY = 1, // show in world as ready (closed door close)
|
|
* GO_STATE_ACTIVE_ALTERNATIVE = 2 // show in world as used in alt way and not reset (closed door open by cannon fire)
|
|
* };
|
|
* </pre>
|
|
*
|
|
* @param [GOState] state : all available go states can be seen above
|
|
*/
|
|
int SetGoState(lua_State* L, GameObject* go)
|
|
{
|
|
uint32 state = ALE::CHECKVAL<uint32>(L, 2, 0);
|
|
|
|
if (state == 0)
|
|
go->SetGoState(GO_STATE_ACTIVE);
|
|
else if (state == 1)
|
|
go->SetGoState(GO_STATE_READY);
|
|
else if (state == 2)
|
|
go->SetGoState(GO_STATE_ACTIVE_ALTERNATIVE);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Sets the [LootState] of a [GameObject]
|
|
* Below are [LootState]s off of 3.3.5a
|
|
*
|
|
* <pre>
|
|
* enum LootState
|
|
* {
|
|
* GO_NOT_READY = 0,
|
|
* GO_READY, // can be ready but despawned, and then not possible activate until spawn
|
|
* GO_ACTIVATED,
|
|
* GO_JUST_DEACTIVATED
|
|
* };
|
|
* </pre>
|
|
*
|
|
* @param [LootState] state : all available loot states can be seen above
|
|
*/
|
|
int SetLootState(lua_State* L, GameObject* go)
|
|
{
|
|
uint32 state = ALE::CHECKVAL<uint32>(L, 2, 0);
|
|
|
|
if (state == 0)
|
|
go->SetLootState(GO_NOT_READY);
|
|
else if (state == 1)
|
|
go->SetLootState(GO_READY);
|
|
else if (state == 2)
|
|
go->SetLootState(GO_ACTIVATED);
|
|
else if (state == 3)
|
|
go->SetLootState(GO_JUST_DEACTIVATED);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Adds an [Item] to the loot of a [GameObject]
|
|
* Requires an gameobject with loot_template set to 0.
|
|
*
|
|
* @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 = 1;
|
|
int argAmount = lua_gettop(L);
|
|
|
|
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
|
|
|
|
uint8 addedItems = 0;
|
|
while (i + 2 <= argAmount)
|
|
{
|
|
uint32 entry = ALE::CHECKVAL<uint32>(L, ++i);
|
|
uint32 amount = ALE::CHECKVAL<uint32>(L, ++i);
|
|
|
|
ItemTemplate const* item_proto = eObjectMgr->GetItemTemplate(entry);
|
|
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))
|
|
{
|
|
item->SaveToDB(trans);
|
|
LootStoreItem storeItem(item->GetEntry(), 0, 100, 0, LOOT_MODE_DEFAULT, 0, item->GetCount(), item->GetCount());
|
|
go->loot.AddItem(storeItem);
|
|
ALE::Push(L, item->GetGUID().GetCounter());
|
|
++addedItems;
|
|
}
|
|
}
|
|
|
|
CharacterDatabase.CommitTransaction(trans);
|
|
|
|
return addedItems;
|
|
}
|
|
|
|
/**
|
|
* Saves [GameObject] to the database
|
|
*
|
|
*/
|
|
int SaveToDB(lua_State* /*L*/, GameObject* go)
|
|
{
|
|
go->SaveToDB();
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Removes [GameObject] from the world
|
|
*
|
|
* The object is no longer reachable after this and it is not respawned.
|
|
*
|
|
* @param bool deleteFromDB : if true, it will delete the [GameObject] from the database
|
|
*/
|
|
int RemoveFromWorld(lua_State* L, GameObject* go)
|
|
{
|
|
bool deldb = ALE::CHECKVAL<bool>(L, 2, false);
|
|
|
|
// cs_gobject.cpp copy paste
|
|
ObjectGuid ownerGuid = go->GetOwnerGUID();
|
|
if (ownerGuid)
|
|
{
|
|
Unit* owner = eObjectAccessor()GetUnit(*go, ownerGuid);
|
|
if (!owner || !ownerGuid.IsPlayer())
|
|
return 0;
|
|
|
|
owner->RemoveGameObject(go, false);
|
|
}
|
|
|
|
if (deldb)
|
|
go->DeleteFromDB();
|
|
|
|
go->SetRespawnTime(0);
|
|
go->Delete();
|
|
|
|
ALE::CHECKOBJ<ALEObject>(L, 1)->Invalidate();
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Activates a door or a button/lever
|
|
*
|
|
* @param uint32 delay = 0 : cooldown time in seconds to restore the [GameObject] back to normal. 0 for infinite duration
|
|
*/
|
|
int UseDoorOrButton(lua_State* L, GameObject* go)
|
|
{
|
|
uint32 delay = ALE::CHECKVAL<uint32>(L, 2, 0);
|
|
|
|
go->UseDoorOrButton(delay);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Despawns a [GameObject]
|
|
*
|
|
* The gameobject may be automatically respawned by the core
|
|
*/
|
|
int Despawn(lua_State* /*L*/, GameObject* go)
|
|
{
|
|
go->SetLootState(GO_JUST_DEACTIVATED);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Respawns a [GameObject]
|
|
*/
|
|
int Respawn(lua_State* /*L*/, GameObject* go)
|
|
{
|
|
go->Respawn();
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Sets the respawn or despawn time for the gameobject.
|
|
*
|
|
* Respawn time is also used as despawn time depending on gameobject settings
|
|
*
|
|
* @param int32 delay = 0 : cooldown time in seconds to respawn or despawn the object. 0 means never
|
|
*/
|
|
int SetRespawnTime(lua_State* L, GameObject* go)
|
|
{
|
|
int32 respawn = ALE::CHECKVAL<int32>(L, 2);
|
|
|
|
go->SetRespawnTime(respawn);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Sets the respawn or despawn time for the gameobject.
|
|
*
|
|
* Respawn time is also used as despawn time depending on gameobject settings
|
|
*
|
|
* @param int32 delay = 0 : cooldown time in seconds to respawn or despawn the object. 0 means never
|
|
*/
|
|
int SetRespawnDelay(lua_State* L, GameObject* go)
|
|
{
|
|
int32 respawn = ALE::CHECKVAL<int32>(L, 2);
|
|
|
|
go->SetRespawnDelay(respawn);
|
|
return 0;
|
|
}
|
|
};
|
|
#endif
|