From 970b30b467f7d89f3bf0799f3d98f7904a01bd1b Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Tue, 25 Feb 2014 23:15:14 -0500 Subject: [PATCH] Added entity_list::IsMobSpawnedByNpcTypeID Exported to both lua and perl --- zone/entity.cpp | 16 ++++++++++++++++ zone/entity.h | 1 + zone/lua_entity_list.cpp | 6 ++++++ zone/lua_entity_list.h | 1 + zone/perl_entity.cpp | 28 ++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+) diff --git a/zone/entity.cpp b/zone/entity.cpp index 9103ed157..64bbe53c1 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -1012,6 +1012,22 @@ Mob *EntityList::GetMobByNpcTypeID(uint32 get_id) return nullptr; } +bool EntityList::IsMobSpawnedByNpcTypeID(uint32 get_id) +{ + if (get_id == 0 || npc_list.empty()) + return false; + + auto it = npc_list.begin(); + while (it != npc_list.end()) { + // Mobs will have a 0 as their GetID() if they're dead + if (it->second->GetNPCTypeID() == get_id && it->second->GetID() != 0) + return true; + ++it; + } + + return false; +} + Object *EntityList::GetObjectByDBID(uint32 id) { if (id == 0 || object_list.empty()) diff --git a/zone/entity.h b/zone/entity.h index dbdeb5dcd..a42e63f9b 100644 --- a/zone/entity.h +++ b/zone/entity.h @@ -135,6 +135,7 @@ public: inline Mob *GetMobID(uint16 id) { return(GetMob(id)); } //for perl Mob *GetMob(const char* name); Mob *GetMobByNpcTypeID(uint32 get_id); + bool IsMobSpawnedByNpcTypeID(uint32 get_id); Mob *GetTargetForVirus(Mob* spreader); inline NPC *GetNPCByID(uint16 id) { return npc_list.count(id) ? npc_list.at(id) : nullptr; } diff --git a/zone/lua_entity_list.cpp b/zone/lua_entity_list.cpp index 5319b016a..32bd9a89d 100644 --- a/zone/lua_entity_list.cpp +++ b/zone/lua_entity_list.cpp @@ -65,6 +65,11 @@ Lua_Mob Lua_EntityList::GetMobByNpcTypeID(int npc_type) { return Lua_Mob(self->GetMobByNpcTypeID(npc_type)); } +bool Lua_EntityList::IsMobSpawnedByNpcTypeID(int npc_type) { + Lua_Safe_Call_Bool(); + return self->IsMobSpawnedByNpcTypeID(npc_type); +} + Lua_NPC Lua_EntityList::GetNPCByID(int id) { Lua_Safe_Call_Class(Lua_NPC); return Lua_NPC(self->GetNPCByID(id)); @@ -420,6 +425,7 @@ luabind::scope lua_register_entity_list() { .def("GetMob", (Lua_Mob(Lua_EntityList::*)(const char*))&Lua_EntityList::GetMob) .def("GetMob", (Lua_Mob(Lua_EntityList::*)(int))&Lua_EntityList::GetMob) .def("GetMobByNpcTypeID", (Lua_Mob(Lua_EntityList::*)(int))&Lua_EntityList::GetMobByNpcTypeID) + .def("IsMobSpawnedByNpcTypeID", (bool(Lua_EntityList::*)(int))&Lua_EntityList::IsMobSpawnedByNpcTypeID) .def("GetNPCByID", (Lua_NPC(Lua_EntityList::*)(int))&Lua_EntityList::GetNPCByID) .def("GetNPCByNPCTypeID", (Lua_NPC(Lua_EntityList::*)(int))&Lua_EntityList::GetNPCByNPCTypeID) .def("GetClientByName", (Lua_Client(Lua_EntityList::*)(const char*))&Lua_EntityList::GetClientByName) diff --git a/zone/lua_entity_list.h b/zone/lua_entity_list.h index 4fbc5f0ed..dc9c1a5ed 100644 --- a/zone/lua_entity_list.h +++ b/zone/lua_entity_list.h @@ -51,6 +51,7 @@ public: Lua_Mob GetMob(const char *name); Lua_Mob GetMob(int id); Lua_Mob GetMobByNpcTypeID(int npc_type); + bool IsMobSpawnedByNpcTypeID(int npc_type); Lua_NPC GetNPCByID(int id); Lua_NPC GetNPCByNPCTypeID(int npc_type); Lua_Client GetClientByName(const char *name); diff --git a/zone/perl_entity.cpp b/zone/perl_entity.cpp index 255e9278f..3d685e011 100644 --- a/zone/perl_entity.cpp +++ b/zone/perl_entity.cpp @@ -150,6 +150,33 @@ XS(XS_EntityList_GetMobByNpcTypeID) XSRETURN(1); } +XS(XS_EntityList_IsMobSpawnedByNpcTypeID); /* prototype pass -Wmissing-prototypes */ +XS(XS_EntityList_IsMobSpawnedByNpcTypeID) +{ + dXSARGS; + if (items != 2) + Perl_croak(aTHX_ "Usage: EntityList::ValidMobByNpcTypeID(THIS, get_id)"); + { + EntityList * THIS; + bool RETVAL; + uint32 get_id = (uint32)SvUV(ST(1)); + + if (sv_derived_from(ST(0), "EntityList")) { + IV tmp = SvIV((SV*)SvRV(ST(0))); + THIS = INT2PTR(EntityList *,tmp); + } + else + Perl_croak(aTHX_ "THIS is not of type EntityList"); + if(THIS == nullptr) + Perl_croak(aTHX_ "THIS is nullptr, avoiding crash."); + + RETVAL = THIS->IsMobSpawnedByNpcTypeID(get_id); + ST(0) = boolSV(RETVAL); + sv_2mortal(ST(0)); + } + XSRETURN(1); +} + XS(XS_EntityList_GetNPCByID); /* prototype to pass -Wmissing-prototypes */ XS(XS_EntityList_GetNPCByID) { @@ -2127,6 +2154,7 @@ XS(boot_EntityList) newXSproto(strcpy(buf, "GetMob"), XS_EntityList_GetMob, file, "$$"); newXSproto(strcpy(buf, "GetMobByID"), XS_EntityList_GetMobByID, file, "$$"); newXSproto(strcpy(buf, "GetMobByNpcTypeID"), XS_EntityList_GetMobByNpcTypeID, file, "$$"); + newXSproto(strcpy(buf, "IsMobSpawnedByNpcTypeID"), XS_EntityList_IsMobSpawnedByNpcTypeID, file, "$$"); newXSproto(strcpy(buf, "GetNPCByID"), XS_EntityList_GetNPCByID, file, "$$"); newXSproto(strcpy(buf, "GetNPCByNPCTypeID"), XS_EntityList_GetNPCByNPCTypeID, file, "$$"); newXSproto(strcpy(buf, "GetClientByName"), XS_EntityList_GetClientByName, file, "$$");