diff --git a/zone/entity.cpp b/zone/entity.cpp index 23ff268fc..c80f83ec5 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -1097,6 +1097,22 @@ NPC *EntityList::GetNPCByNPCTypeID(uint32 npc_id) return nullptr; } +NPC *EntityList::GetNPCBySpawnID(uint32 spawn_id) +{ + if (spawn_id == 0 || npc_list.empty()) { + return nullptr; + } + + auto it = npc_list.begin(); + while (it != npc_list.end()) { + if (it->second->GetSpawnGroupId() == spawn_id) { + return it->second; + } + ++it; + } + return nullptr; +} + Mob *EntityList::GetMob(uint16 get_id) { Entity *ent = nullptr; diff --git a/zone/entity.h b/zone/entity.h index 3c182be29..8c705897a 100644 --- a/zone/entity.h +++ b/zone/entity.h @@ -166,6 +166,7 @@ public: return nullptr; } NPC *GetNPCByNPCTypeID(uint32 npc_id); + NPC *GetNPCBySpawnID(uint32 spawn_id); inline Merc *GetMercByID(uint16 id) { auto it = merc_list.find(id); diff --git a/zone/lua_entity_list.cpp b/zone/lua_entity_list.cpp index abfc020d6..e4f2b207d 100644 --- a/zone/lua_entity_list.cpp +++ b/zone/lua_entity_list.cpp @@ -80,6 +80,11 @@ Lua_NPC Lua_EntityList::GetNPCByNPCTypeID(int npc_type) { return Lua_NPC(self->GetNPCByNPCTypeID(npc_type)); } +Lua_NPC Lua_EntityList::GetNPCBySpawnID(uint32 spawn_id) { + Lua_Safe_Call_Class(Lua_NPC); + return Lua_NPC(self->GetNPCBySpawnID(spawn_id)); +} + Lua_Client Lua_EntityList::GetClientByName(const char *name) { Lua_Safe_Call_Class(Lua_Client); return Lua_Client(self->GetClientByName(name)); @@ -449,6 +454,7 @@ luabind::scope lua_register_entity_list() { .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("GetNPCBySpawnID", (Lua_NPC(Lua_EntityList::*)(int))&Lua_EntityList::GetNPCBySpawnID) .def("GetClientByName", (Lua_Client(Lua_EntityList::*)(const char*))&Lua_EntityList::GetClientByName) .def("GetClientByAccID", (Lua_Client(Lua_EntityList::*)(uint32))&Lua_EntityList::GetClientByAccID) .def("GetClientByID", (Lua_Client(Lua_EntityList::*)(int))&Lua_EntityList::GetClientByID) diff --git a/zone/lua_entity_list.h b/zone/lua_entity_list.h index 6b0074674..04030e58e 100644 --- a/zone/lua_entity_list.h +++ b/zone/lua_entity_list.h @@ -54,6 +54,7 @@ public: bool IsMobSpawnedByNpcTypeID(int npc_type); Lua_NPC GetNPCByID(int id); Lua_NPC GetNPCByNPCTypeID(int npc_type); + Lua_NPC GetNPCBySpawnID(uint32 spawn_id); Lua_Client GetClientByName(const char *name); Lua_Client GetClientByAccID(uint32 acct_id); Lua_Client GetClientByID(int id); diff --git a/zone/perl_entity.cpp b/zone/perl_entity.cpp index 0f7fe21c7..3280d0a56 100644 --- a/zone/perl_entity.cpp +++ b/zone/perl_entity.cpp @@ -219,6 +219,34 @@ XS(XS_EntityList_GetNPCByNPCTypeID) { XSRETURN(1); } +XS(XS_EntityList_GetNPCBySpawnID); /* prototype to pass -Wmissing-prototypes */ +XS(XS_EntityList_GetNPCBySpawnID) { + dXSARGS; + if (items != 2) + Perl_croak(aTHX_ "Usage: EntityList::GetNPCBySpawnID(THIS, spawn_id)"); + { + EntityList *THIS; + NPC *RETVAL; + uint32 spawn_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->GetNPCBySpawnID(spawn_id); + ST(0) = sv_newmortal(); + sv_setref_pv(ST(0), "NPC", (void *) RETVAL); + } + XSRETURN(1); +} + XS(XS_EntityList_GetClientByName); /* prototype to pass -Wmissing-prototypes */ XS(XS_EntityList_GetClientByName) { dXSARGS; @@ -1998,6 +2026,7 @@ XS(boot_EntityList) { 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, "GetNPCBySpawnID"), XS_EntityList_GetNPCBySpawnID, file, "$$"); newXSproto(strcpy(buf, "GetClientByName"), XS_EntityList_GetClientByName, file, "$$"); newXSproto(strcpy(buf, "GetClientByAccID"), XS_EntityList_GetClientByAccID, file, "$$"); newXSproto(strcpy(buf, "GetClientByID"), XS_EntityList_GetClientByID, file, "$$");