| 487 | } |
| 488 | |
| 489 | int LuaParser::_EventNPC(std::string package_name, QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, |
| 490 | std::vector<std::any> *extra_pointers, luabind::adl::object *l_func) { |
| 491 | const char *sub_name = LuaEvents[evt]; |
| 492 | |
| 493 | int start = lua_gettop(L); |
| 494 | |
| 495 | try { |
| 496 | int npop = 2; |
| 497 | PushErrorHandler(L); |
| 498 | if(l_func != nullptr) { |
| 499 | l_func->push(L); |
| 500 | } else { |
| 501 | lua_getfield(L, LUA_REGISTRYINDEX, package_name.c_str()); |
| 502 | lua_getfield(L, -1, sub_name); |
| 503 | npop = 3; |
| 504 | } |
| 505 | |
| 506 | lua_createtable(L, 0, 0); |
| 507 | //always push self |
| 508 | Lua_NPC l_npc(npc); |
| 509 | luabind::adl::object l_npc_o = luabind::adl::object(L, l_npc); |
| 510 | l_npc_o.push(L); |
| 511 | lua_setfield(L, -2, "self"); |
| 512 | |
| 513 | auto arg_function = NPCArgumentDispatch[evt]; |
| 514 | arg_function(this, L, npc, init, data, extra_data, extra_pointers); |
| 515 | Client *c = (init && init->IsClient()) ? init->CastToClient() : nullptr; |
| 516 | |
| 517 | RunningQuest q; |
| 518 | |
| 519 | q.owner = npc; |
| 520 | q.initiator = c; |
| 521 | |
| 522 | quest_manager.StartQuest(q); |
| 523 | |
| 524 | if(lua_pcall(L, 1, 1, start + 1)) { |
| 525 | std::string error = lua_tostring(L, -1); |
| 526 | AddError(error); |
| 527 | quest_manager.EndQuest(); |
| 528 | lua_pop(L, npop); |
| 529 | return 0; |
| 530 | } |
| 531 | quest_manager.EndQuest(); |
| 532 | |
| 533 | if(lua_isnumber(L, -1)) { |
| 534 | int ret = static_cast<int>(lua_tointeger(L, -1)); |
| 535 | lua_pop(L, npop); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | lua_pop(L, npop); |
| 540 | } catch(std::exception &ex) { |
| 541 | AddError(fmt::format("Lua Exception | [{}] for NPC [{}] in [{}]: {}", sub_name, npc->GetNPCTypeID(), package_name, ex.what())); |
| 542 | |
| 543 | //Restore our stack to the best of our ability |
| 544 | int end = lua_gettop(L); |
| 545 | int n = end - start; |
| 546 | if(n > 0) { |
nothing calls this directly
no test coverage detected