From f40b44a454f2bf6dbf0295ef412ce9839347841f Mon Sep 17 00:00:00 2001 From: KimLS Date: Sat, 25 Aug 2018 15:33:29 -0700 Subject: [PATCH] Starting rewrite of lua parser in earnest --- zone/CMakeLists.txt | 90 +- zone/lua/lua_client.cpp | 8 + zone/lua/lua_corpse.cpp | 8 + zone/lua/lua_door.cpp | 8 + zone/lua/lua_entity.cpp | 37 + zone/lua/lua_events.h | 88 ++ zone/lua/lua_forward.h | 10 + zone/lua/lua_general.cpp | 5 + zone/lua/lua_mob.cpp | 8 + zone/lua/lua_npc.cpp | 8 + zone/lua/lua_object.cpp | 8 + zone/lua/lua_parser.cpp | 1136 ++++++++++++++++++ zone/{ => lua}/lua_parser.h | 50 +- zone/lua/lua_structs.h | 7 + zone/{ => lua/old}/lua_bit.cpp | 0 zone/{ => lua/old}/lua_bit.h | 0 zone/{ => lua/old}/lua_client.cpp | 0 zone/{ => lua/old}/lua_client.h | 0 zone/{ => lua/old}/lua_corpse.cpp | 0 zone/{ => lua/old}/lua_corpse.h | 0 zone/{ => lua/old}/lua_door.cpp | 0 zone/{ => lua/old}/lua_door.h | 0 zone/{ => lua/old}/lua_encounter.cpp | 0 zone/{ => lua/old}/lua_encounter.h | 0 zone/{ => lua/old}/lua_entity.cpp | 0 zone/{ => lua/old}/lua_entity.h | 0 zone/{ => lua/old}/lua_entity_list.cpp | 0 zone/{ => lua/old}/lua_entity_list.h | 0 zone/{ => lua/old}/lua_general.cpp | 468 ++++---- zone/{ => lua/old}/lua_general.h | 0 zone/{ => lua/old}/lua_group.cpp | 0 zone/{ => lua/old}/lua_group.h | 0 zone/{ => lua/old}/lua_hate_entry.cpp | 0 zone/{ => lua/old}/lua_hate_list.cpp | 0 zone/{ => lua/old}/lua_hate_list.h | 0 zone/{ => lua/old}/lua_inventory.cpp | 0 zone/{ => lua/old}/lua_inventory.h | 0 zone/{ => lua/old}/lua_item.cpp | 0 zone/{ => lua/old}/lua_item.h | 0 zone/{ => lua/old}/lua_iteminst.cpp | 0 zone/{ => lua/old}/lua_iteminst.h | 0 zone/{ => lua/old}/lua_mob.cpp | 0 zone/{ => lua/old}/lua_mob.h | 0 zone/{ => lua/old}/lua_mod.cpp | 10 +- zone/{ => lua/old}/lua_mod.h | 0 zone/{ => lua/old}/lua_npc.cpp | 0 zone/{ => lua/old}/lua_npc.h | 0 zone/{ => lua/old}/lua_object.cpp | 0 zone/{ => lua/old}/lua_object.h | 0 zone/{ => lua/old}/lua_packet.cpp | 0 zone/{ => lua/old}/lua_packet.h | 0 zone/{ => lua/old}/lua_parser_events.cpp | 0 zone/{ => lua/old}/lua_parser_events.h | 0 zone/{ => lua/old}/lua_raid.cpp | 0 zone/{ => lua/old}/lua_raid.h | 0 zone/{ => lua/old}/lua_spawn.cpp | 0 zone/{ => lua/old}/lua_spawn.h | 0 zone/{ => lua/old}/lua_spell.cpp | 2 +- zone/{ => lua/old}/lua_spell.h | 0 zone/{ => lua/old}/lua_stat_bonuses.cpp | 0 zone/{ => lua/old}/lua_stat_bonuses.h | 0 zone/lua_parser.cpp | 1389 ---------------------- zone/lua_ptr.h | 55 - 63 files changed, 1625 insertions(+), 1770 deletions(-) create mode 100644 zone/lua/lua_client.cpp create mode 100644 zone/lua/lua_corpse.cpp create mode 100644 zone/lua/lua_door.cpp create mode 100644 zone/lua/lua_entity.cpp create mode 100644 zone/lua/lua_events.h create mode 100644 zone/lua/lua_forward.h create mode 100644 zone/lua/lua_general.cpp create mode 100644 zone/lua/lua_mob.cpp create mode 100644 zone/lua/lua_npc.cpp create mode 100644 zone/lua/lua_object.cpp create mode 100644 zone/lua/lua_parser.cpp rename zone/{ => lua}/lua_parser.h (82%) create mode 100644 zone/lua/lua_structs.h rename zone/{ => lua/old}/lua_bit.cpp (100%) rename zone/{ => lua/old}/lua_bit.h (100%) rename zone/{ => lua/old}/lua_client.cpp (100%) rename zone/{ => lua/old}/lua_client.h (100%) rename zone/{ => lua/old}/lua_corpse.cpp (100%) rename zone/{ => lua/old}/lua_corpse.h (100%) rename zone/{ => lua/old}/lua_door.cpp (100%) rename zone/{ => lua/old}/lua_door.h (100%) rename zone/{ => lua/old}/lua_encounter.cpp (100%) rename zone/{ => lua/old}/lua_encounter.h (100%) rename zone/{ => lua/old}/lua_entity.cpp (100%) rename zone/{ => lua/old}/lua_entity.h (100%) rename zone/{ => lua/old}/lua_entity_list.cpp (100%) rename zone/{ => lua/old}/lua_entity_list.h (100%) rename zone/{ => lua/old}/lua_general.cpp (91%) rename zone/{ => lua/old}/lua_general.h (100%) rename zone/{ => lua/old}/lua_group.cpp (100%) rename zone/{ => lua/old}/lua_group.h (100%) rename zone/{ => lua/old}/lua_hate_entry.cpp (100%) rename zone/{ => lua/old}/lua_hate_list.cpp (100%) rename zone/{ => lua/old}/lua_hate_list.h (100%) rename zone/{ => lua/old}/lua_inventory.cpp (100%) rename zone/{ => lua/old}/lua_inventory.h (100%) rename zone/{ => lua/old}/lua_item.cpp (100%) rename zone/{ => lua/old}/lua_item.h (100%) rename zone/{ => lua/old}/lua_iteminst.cpp (100%) rename zone/{ => lua/old}/lua_iteminst.h (100%) rename zone/{ => lua/old}/lua_mob.cpp (100%) rename zone/{ => lua/old}/lua_mob.h (100%) rename zone/{ => lua/old}/lua_mod.cpp (99%) rename zone/{ => lua/old}/lua_mod.h (100%) rename zone/{ => lua/old}/lua_npc.cpp (100%) rename zone/{ => lua/old}/lua_npc.h (100%) rename zone/{ => lua/old}/lua_object.cpp (100%) rename zone/{ => lua/old}/lua_object.h (100%) rename zone/{ => lua/old}/lua_packet.cpp (100%) rename zone/{ => lua/old}/lua_packet.h (100%) rename zone/{ => lua/old}/lua_parser_events.cpp (100%) rename zone/{ => lua/old}/lua_parser_events.h (100%) rename zone/{ => lua/old}/lua_raid.cpp (100%) rename zone/{ => lua/old}/lua_raid.h (100%) rename zone/{ => lua/old}/lua_spawn.cpp (100%) rename zone/{ => lua/old}/lua_spawn.h (100%) rename zone/{ => lua/old}/lua_spell.cpp (99%) rename zone/{ => lua/old}/lua_spell.h (100%) rename zone/{ => lua/old}/lua_stat_bonuses.cpp (100%) rename zone/{ => lua/old}/lua_stat_bonuses.h (100%) delete mode 100644 zone/lua_parser.cpp delete mode 100644 zone/lua_ptr.h diff --git a/zone/CMakeLists.txt b/zone/CMakeLists.txt index 0414a8d2b..2c6a0d7f7 100644 --- a/zone/CMakeLists.txt +++ b/zone/CMakeLists.txt @@ -40,30 +40,6 @@ SET(zone_sources horse.cpp inventory.cpp loottables.cpp - lua_bit.cpp - lua_corpse.cpp - lua_client.cpp - lua_door.cpp - lua_encounter.cpp - lua_entity.cpp - lua_entity_list.cpp - lua_general.cpp - lua_group.cpp - lua_hate_list.cpp - lua_inventory.cpp - lua_item.cpp - lua_iteminst.cpp - lua_mob.cpp - lua_mod.cpp - lua_npc.cpp - lua_object.cpp - lua_packet.cpp - lua_parser.cpp - lua_parser_events.cpp - lua_raid.cpp - lua_spawn.cpp - lua_spell.cpp - lua_stat_bonuses.cpp embperl.cpp embxs.cpp entity.cpp @@ -156,7 +132,7 @@ SET(zone_headers command.h common.h corpse.h - data_bucket.h + data_bucket.h doors.h embparser.h embperl.h @@ -173,31 +149,6 @@ SET(zone_headers hate_list.h heal_rotation.h horse.h - lua_bit.h - lua_client.h - lua_corpse.h - lua_door.h - lua_encounter.h - lua_entity.h - lua_entity_list.h - lua_general.h - lua_group.h - lua_hate_list.h - lua_inventory.h - lua_item.h - lua_iteminst.h - lua_mob.h - lua_mod.h - lua_npc.h - lua_object.h - lua_packet.h - lua_parser.h - lua_parser_events.h - lua_ptr.h - lua_raid.h - lua_spawn.h - lua_spell.h - lua_stat_bonuses.h map.h masterentity.h maxskill.h @@ -245,6 +196,45 @@ SET(zone_headers zonedump.h ) +IF(EQEMU_BUILD_LUA) + +SET(zone_sources ${zone_sources} + lua/lua_client.cpp + lua/lua_corpse.cpp + lua/lua_door.cpp + lua/lua_entity.cpp + lua/lua_npc.cpp + lua/lua_mob.cpp + lua/lua_object.cpp + lua/lua_general.cpp + lua/lua_parser.cpp +) + +SET(zone_headers ${zone_headers} + lua/lua_events.h + lua/lua_forward.h + lua/lua_parser.h + lua/lua_structs.h +) + +SOURCE_GROUP(lua FILES + lua/lua_client.cpp + lua/lua_corpse.cpp + lua/lua_door.cpp + lua/lua_entity.cpp + lua/lua_events.h + lua/lua_forward.h + lua/lua_npc.cpp + lua/lua_mob.cpp + lua/lua_object.cpp + lua/lua_general.cpp + lua/lua_parser.cpp + lua/lua_parser.h + lua/lua_structs.h +) + +endif() + IF(EQEMU_DEPOP_INVALIDATES_CACHE) ADD_DEFINITIONS(-DDEPOP_INVALIDATES_NPC_TYPES_CACHE) ENDIF(EQEMU_DEPOP_INVALIDATES_CACHE) diff --git a/zone/lua/lua_client.cpp b/zone/lua/lua_client.cpp new file mode 100644 index 000000000..1948dd461 --- /dev/null +++ b/zone/lua/lua_client.cpp @@ -0,0 +1,8 @@ +#include +#include "../client.h" + +void lua_register_client(sol::state *state) { + state->new_usertype("Client", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_corpse.cpp b/zone/lua/lua_corpse.cpp new file mode 100644 index 000000000..b3a4a30de --- /dev/null +++ b/zone/lua/lua_corpse.cpp @@ -0,0 +1,8 @@ +#include +#include "../corpse.h" + +void lua_register_corpse(sol::state *state) { + state->new_usertype("Corpse", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_door.cpp b/zone/lua/lua_door.cpp new file mode 100644 index 000000000..0dd021ba3 --- /dev/null +++ b/zone/lua/lua_door.cpp @@ -0,0 +1,8 @@ +#include +#include "../doors.h" + +void lua_register_doors(sol::state *state) { + state->new_usertype("Doors", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_entity.cpp b/zone/lua/lua_entity.cpp new file mode 100644 index 000000000..994394439 --- /dev/null +++ b/zone/lua/lua_entity.cpp @@ -0,0 +1,37 @@ +#include +#include "../entity.h" +#include "../client.h" +#include "../npc.h" +#include "../corpse.h" +#include "../doors.h" +#include "../object.h" + +void lua_register_entity(sol::state *state) { + state->new_usertype("Entity", + "IsClient", &Entity::IsClient, + "IsNPC", &Entity::IsNPC, + "IsMob", &Entity::IsMob, + "IsMerc", &Entity::IsMerc, + "IsCorpse", &Entity::IsCorpse, + "IsPlayerCorpse", &Entity::IsPlayerCorpse, + "IsNPCCorpse", &Entity::IsNPCCorpse, + "IsObject", &Entity::IsObject, + "IsDoor", &Entity::IsDoor, + "IsTrap", &Entity::IsTrap, + "IsBeacon", &Entity::IsBeacon, + "IsEncounter", &Entity::IsEncounter, + "IsBot", &Entity::IsBot, + "IsAura", &Entity::IsAura, + "CastToClient", (Client*(Entity::*)())&Entity::CastToClient, + "CastToMob", (Mob*(Entity::*)())&Entity::CastToMob, + //"CastToMerc", (Merc*(Entity::*)())&Entity::CastToMerc, + "CastToCorpse", (Corpse*(Entity::*)())&Entity::CastToCorpse, + "CastToObject", (Object*(Entity::*)())&Entity::CastToObject, + "CastToDoors", (Doors*(Entity::*)())&Entity::CastToDoors, + //"CastToTrap", (Trap*(Entity::*)())&Entity::CastToTrap, + //"CastToBeacon", (Beacon*(Entity::*)())&Entity::CastToBeacon, + //"CastToEncounter", (Encounter*(Entity::*)())&Entity::CastToEncounter, + "GetID", &Entity::GetID, + "GetName", &Entity::GetName + ); +} diff --git a/zone/lua/lua_events.h b/zone/lua/lua_events.h new file mode 100644 index 000000000..57b1c42c6 --- /dev/null +++ b/zone/lua/lua_events.h @@ -0,0 +1,88 @@ +#pragma once + +const char *LuaEvents[_LargestEventID] = { + "event_say", + "event_trade", + "event_death", + "event_spawn", + "event_attack", + "event_combat", + "event_aggro", + "event_slay", + "event_npc_slay", + "event_waypoint_arrive", + "event_waypoint_depart", + "event_timer", + "event_signal", + "event_hp", + "event_enter", + "event_exit", + "event_enter_zone", + "event_click_door", + "event_loot", + "event_zone", + "event_level_up", + "event_killed_merit", + "event_cast_on", + "event_task_accepted", + "event_task_stage_complete", + "event_task_update", + "event_task_complete", + "event_task_fail", + "event_aggro_say", + "event_player_pickup", + "event_popup_response", + "event_environmental_damage", + "event_proximity_say", + "event_cast", + "event_cast_begin", + "event_scale_calc", + "event_item_enter_zone", + "event_target_change", + "event_hate_list", + "event_spell_effect", + "event_spell_effect", + "event_spell_buff_tic", + "event_spell_buff_tic", + "event_spell_fade", + "event_spell_effect_translocate_complete", + "event_combine_success", + "event_combine_failure", + "event_item_click", + "event_item_click_cast", + "event_group_change", + "event_forage_success", + "event_forage_failure", + "event_fish_start", + "event_fish_success", + "event_fish_failure", + "event_click_object", + "event_discover_item", + "event_disconnect", + "event_connect", + "event_item_tick", + "event_duel_win", + "event_duel_lose", + "event_encounter_load", + "event_encounter_unload", + "event_command", + "event_drop_item", + "event_destroy_item", + "event_feign_death", + "event_weapon_proc", + "event_equip_item", + "event_unequip_item", + "event_augment_item", + "event_unaugment_item", + "event_augment_insert", + "event_augment_remove", + "event_enter_area", + "event_leave_area", + "event_respawn", + "event_death_complete", + "event_unhandled_opcode", + "event_tick", + "event_spawn_zone", + "event_death_zone", + "event_use_skill" +}; \ No newline at end of file diff --git a/zone/lua/lua_forward.h b/zone/lua/lua_forward.h new file mode 100644 index 000000000..0036d3344 --- /dev/null +++ b/zone/lua/lua_forward.h @@ -0,0 +1,10 @@ +#pragma once + +void lua_register_general(sol::state *state); +void lua_register_entity(sol::state *state); +void lua_register_mob(sol::state *state); +void lua_register_npc(sol::state *state); +void lua_register_client(sol::state *state); +void lua_register_doors(sol::state *state); +void lua_register_corpse(sol::state *state); +void lua_register_object(sol::state *state); \ No newline at end of file diff --git a/zone/lua/lua_general.cpp b/zone/lua/lua_general.cpp new file mode 100644 index 000000000..94674d5e7 --- /dev/null +++ b/zone/lua/lua_general.cpp @@ -0,0 +1,5 @@ +#include + +void lua_register_general(sol::state *state) { + auto table = state->create_named_table("eqemu"); +} diff --git a/zone/lua/lua_mob.cpp b/zone/lua/lua_mob.cpp new file mode 100644 index 000000000..c9d704e1d --- /dev/null +++ b/zone/lua/lua_mob.cpp @@ -0,0 +1,8 @@ +#include +#include "../mob.h" + +void lua_register_mob(sol::state *state) { + state->new_usertype("Mob", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_npc.cpp b/zone/lua/lua_npc.cpp new file mode 100644 index 000000000..78c4ca58f --- /dev/null +++ b/zone/lua/lua_npc.cpp @@ -0,0 +1,8 @@ +#include +#include "../npc.h" + +void lua_register_npc(sol::state *state) { + state->new_usertype("NPC", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_object.cpp b/zone/lua/lua_object.cpp new file mode 100644 index 000000000..c9995970c --- /dev/null +++ b/zone/lua/lua_object.cpp @@ -0,0 +1,8 @@ +#include +#include "../object.h" + +void lua_register_object(sol::state *state) { + state->new_usertype("Object", + sol::base_classes, sol::bases() + ); +} diff --git a/zone/lua/lua_parser.cpp b/zone/lua/lua_parser.cpp new file mode 100644 index 000000000..9f8db8573 --- /dev/null +++ b/zone/lua/lua_parser.cpp @@ -0,0 +1,1136 @@ +#include +#include +#include +#include +#include +#include + +#include "../../common/spdat.h" +#include "../masterentity.h" +#include "../questmgr.h" +#include "../zone.h" +#include "../zone_config.h" + +#include "lua_parser.h" +#include "lua_events.h" +#include "lua_structs.h" +#include "lua_forward.h" + +extern Zone *zone; +extern const ZoneConfig *Config; + +struct LuaParser::Implementation +{ + std::map vars; + std::map loaded; + std::unique_ptr state; +}; + +LuaParser::LuaParser() { + mImpl.reset(new Implementation()); +} + +LuaParser::~LuaParser() { +} + +int LuaParser::EventNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + if(!npc) { + return 0; + } + + if(!HasQuestSub(npc->GetNPCTypeID(), evt)) { + return 0; + } + + std::string package_name = "npc_" + std::to_string(npc->GetNPCTypeID()); + return _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers); +} + +int LuaParser::EventGlobalNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + if(!npc) { + return 0; + } + + if(!HasGlobalQuestSub(evt)) { + return 0; + } + + return _EventNPC("global_npc", evt, npc, init, data, extra_data, extra_pointers); +} + +int LuaParser::_EventNPC(std::string package_name, QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, + std::vector *extra_pointers, sol::function *l_func) { + int r = 0; + const char *sub_name = LuaEvents[evt]; + auto script = mImpl->loaded.find(package_name); + + sol::protected_function f; + if (l_func) { + f = *l_func; + } + else { + f = script->second.Env[sub_name]; + } + + auto table = mImpl->state->create_table(); + table["self"] = npc; + + Client *c = (init && init->IsClient()) ? init->CastToClient() : nullptr; + quest_manager.StartQuest(npc, c, nullptr); + try { + auto res = f(table); + if (res.valid()) { + r = res; + } + else { + sol::error err = res; + AddError(err.what()); + } + } + catch (sol::error &err) { + AddError(err.what()); + } + quest_manager.EndQuest(); + + return r; +} + +int LuaParser::EventPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + if(!client) { + return 0; + } + + if(!PlayerHasQuestSub(evt)) { + return 0; + } + + return _EventPlayer("player", evt, client, data, extra_data, extra_pointers); +} + +int LuaParser::EventGlobalPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + if(!client) { + return 0; + } + + if(!GlobalPlayerHasQuestSub(evt)) { + return 0; + } + + return _EventPlayer("global_player", evt, client, data, extra_data, extra_pointers); +} + +int LuaParser::_EventPlayer(std::string package_name, QuestEventID evt, Client *client, std::string data, uint32 extra_data, + std::vector *extra_pointers, sol::function *l_func) { + //const char *sub_name = LuaEvents[evt]; + //int start = lua_gettop(mImpl->state); + // + //try { + // int npop = 1; + // if(l_func != nullptr) { + // l_func->push(mImpl->state); + // } else { + // lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); + // lua_getfield(mImpl->state, -1, sub_name); + // npop = 2; + // } + // + // lua_createtable(mImpl->state, 0, 0); + // //push self + // Lua_Client l_client(client); + // luabind::adl::object l_client_o = luabind::adl::object(mImpl->state, l_client); + // l_client_o.push(mImpl->state); + // lua_setfield(mImpl->state, -2, "self"); + // + // auto arg_function = mImpl->PlayerArgumentDispatch[evt]; + // arg_function(this, mImpl->state, client, data, extra_data, extra_pointers); + // + // quest_manager.StartQuest(client, client, nullptr); + // if(lua_pcall(mImpl->state, 1, 1, 0)) { + // std::string error = lua_tostring(mImpl->state, -1); + // AddError(error); + // quest_manager.EndQuest(); + // return 0; + // } + // quest_manager.EndQuest(); + // + // if(lua_isnumber(mImpl->state, -1)) { + // int ret = static_cast(lua_tointeger(mImpl->state, -1)); + // lua_pop(mImpl->state, npop); + // return ret; + // } + // + // lua_pop(mImpl->state, npop); + //} catch(std::exception &ex) { + // std::string error = "Lua Exception: "; + // error += std::string(ex.what()); + // AddError(error); + // + // //Restore our stack to the best of our ability + // int end = lua_gettop(mImpl->state); + // int n = end - start; + // if(n > 0) { + // lua_pop(mImpl->state, n); + // } + //} + + return 0; +} + +int LuaParser::EventItem(QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + if(!item) { + return 0; + } + + if(!ItemHasQuestSub(item, evt)) { + return 0; + } + + std::string package_name = "item_"; + package_name += std::to_string(item->GetID()); + return _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers); +} + +int LuaParser::_EventItem(std::string package_name, QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, + std::string data, uint32 extra_data, std::vector *extra_pointers, sol::function *l_func) { + //const char *sub_name = LuaEvents[evt]; + // + //int start = lua_gettop(mImpl->state); + // + //try { + // int npop = 1; + // if(l_func != nullptr) { + // l_func->push(mImpl->state); + // } else { + // lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); + // lua_getfield(mImpl->state, -1, sub_name); + // } + // + // lua_createtable(mImpl->state, 0, 0); + // //always push self + // Lua_ItemInst l_item(item); + // luabind::adl::object l_item_o = luabind::adl::object(mImpl->state, l_item); + // l_item_o.push(mImpl->state); + // lua_setfield(mImpl->state, -2, "self"); + // + // Lua_Client l_client(client); + // luabind::adl::object l_client_o = luabind::adl::object(mImpl->state, l_client); + // l_client_o.push(mImpl->state); + // lua_setfield(mImpl->state, -2, "owner"); + // + // //redo this arg function + // auto arg_function = mImpl->ItemArgumentDispatch[evt]; + // arg_function(this, mImpl->state, client, item, mob, data, extra_data, extra_pointers); + // + // quest_manager.StartQuest(client, client, item); + // if(lua_pcall(mImpl->state, 1, 1, 0)) { + // std::string error = lua_tostring(mImpl->state, -1); + // AddError(error); + // quest_manager.EndQuest(); + // return 0; + // } + // quest_manager.EndQuest(); + // + // if(lua_isnumber(mImpl->state, -1)) { + // int ret = static_cast(lua_tointeger(mImpl->state, -1)); + // lua_pop(mImpl->state, npop); + // return ret; + // } + // + // lua_pop(mImpl->state, npop); + //} catch(std::exception &ex) { + // std::string error = "Lua Exception: "; + // error += std::string(ex.what()); + // AddError(error); + // + // //Restore our stack to the best of our ability + // int end = lua_gettop(mImpl->state); + // int n = end - start; + // if(n > 0) { + // lua_pop(mImpl->state, n); + // } + //} + + return 0; +} + +int LuaParser::EventSpell(QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, + std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + std::string package_name = "spell_" + std::to_string(spell_id); + + if(!SpellHasQuestSub(spell_id, evt)) { + return 0; + } + + return _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers); +} + +int LuaParser::_EventSpell(std::string package_name, QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, + std::vector *extra_pointers, sol::function *l_func) { + //const char *sub_name = LuaEvents[evt]; + // + //int start = lua_gettop(mImpl->state); + // + //try { + // int npop = 1; + // if(l_func != nullptr) { + // l_func->push(mImpl->state); + // } else { + // lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); + // lua_getfield(mImpl->state, -1, sub_name); + // npop = 2; + // } + // + // lua_createtable(mImpl->state, 0, 0); + // + // //always push self even if invalid + // if(IsValidSpell(spell_id)) { + // Lua_Spell l_spell(&spells[spell_id]); + // luabind::adl::object l_spell_o = luabind::adl::object(mImpl->state, l_spell); + // l_spell_o.push(mImpl->state); + // } else { + // Lua_Spell l_spell(nullptr); + // luabind::adl::object l_spell_o = luabind::adl::object(mImpl->state, l_spell); + // l_spell_o.push(mImpl->state); + // } + // lua_setfield(mImpl->state, -2, "self"); + // + // auto arg_function = mImpl->SpellArgumentDispatch[evt]; + // arg_function(this, mImpl->state, npc, client, spell_id, extra_data, extra_pointers); + // + // quest_manager.StartQuest(npc, client, nullptr); + // if(lua_pcall(mImpl->state, 1, 1, 0)) { + // std::string error = lua_tostring(mImpl->state, -1); + // AddError(error); + // quest_manager.EndQuest(); + // return 0; + // } + // quest_manager.EndQuest(); + // + // if(lua_isnumber(mImpl->state, -1)) { + // int ret = static_cast(lua_tointeger(mImpl->state, -1)); + // lua_pop(mImpl->state, npop); + // return ret; + // } + // + // lua_pop(mImpl->state, npop); + //} catch(std::exception &ex) { + // std::string error = "Lua Exception: "; + // error += std::string(ex.what()); + // AddError(error); + // + // //Restore our stack to the best of our ability + // int end = lua_gettop(mImpl->state); + // int n = end - start; + // if(n > 0) { + // lua_pop(mImpl->state, n); + // } + //} + + return 0; +} + +int LuaParser::EventEncounter(QuestEventID evt, std::string encounter_name, std::string data, uint32 extra_data, std::vector *extra_pointers) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return 0; + } + + std::string package_name = "encounter_" + encounter_name; + + if(!EncounterHasQuestSub(encounter_name, evt)) { + return 0; + } + + return _EventEncounter(package_name, evt, encounter_name, data, extra_data, extra_pointers); +} + +int LuaParser::_EventEncounter(std::string package_name, QuestEventID evt, std::string encounter_name, std::string data, uint32 extra_data, + std::vector *extra_pointers) { + //const char *sub_name = LuaEvents[evt]; + // + //int start = lua_gettop(mImpl->state); + // + //try { + // lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); + // lua_getfield(mImpl->state, -1, sub_name); + // + // lua_createtable(mImpl->state, 0, 0); + // lua_pushstring(mImpl->state, encounter_name.c_str()); + // lua_setfield(mImpl->state, -2, "name"); + // + // Encounter *enc = lua_encounters[encounter_name]; + // + // auto arg_function = mImpl->EncounterArgumentDispatch[evt]; + // arg_function(this, mImpl->state, enc, data, extra_data, extra_pointers); + // + // quest_manager.StartQuest(enc, nullptr, nullptr, encounter_name); + // if(lua_pcall(mImpl->state, 1, 1, 0)) { + // std::string error = lua_tostring(mImpl->state, -1); + // AddError(error); + // quest_manager.EndQuest(); + // return 0; + // } + // quest_manager.EndQuest(); + // + // if(lua_isnumber(mImpl->state, -1)) { + // int ret = static_cast(lua_tointeger(mImpl->state, -1)); + // lua_pop(mImpl->state, 2); + // return ret; + // } + // + // lua_pop(mImpl->state, 2); + //} catch(std::exception &ex) { + // std::string error = "Lua Exception: "; + // error += std::string(ex.what()); + // AddError(error); + // + // //Restore our stack to the best of our ability + // int end = lua_gettop(mImpl->state); + // int n = end - start; + // if(n > 0) { + // lua_pop(mImpl->state, n); + // } + //} + + return 0; +} + +bool LuaParser::HasQuestSub(uint32 npc_id, QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + std::string package_name = "npc_" + std::to_string(npc_id); + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, package_name); +} + +bool LuaParser::HasGlobalQuestSub(QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, "global_npc"); +} + +bool LuaParser::PlayerHasQuestSub(QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, "player"); +} + +bool LuaParser::GlobalPlayerHasQuestSub(QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, "global_player"); +} + +bool LuaParser::SpellHasQuestSub(uint32 spell_id, QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + std::string package_name = "spell_" + std::to_string(spell_id); + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, package_name); +} + +bool LuaParser::ItemHasQuestSub(EQEmu::ItemInstance *itm, QuestEventID evt) { + if (itm == nullptr) { + return false; + } + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + std::string package_name = "item_"; + package_name += std::to_string(itm->GetID()); + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, package_name); +} + +bool LuaParser::EncounterHasQuestSub(std::string encounter_name, QuestEventID evt) { + evt = ConvertLuaEvent(evt); + if(evt >= _LargestEventID) { + return false; + } + + std::string package_name = "encounter_" + encounter_name; + + const char *subname = LuaEvents[evt]; + return HasFunction(subname, package_name); +} + +void LuaParser::LoadNPCScript(std::string filename, int npc_id) { + std::string package_name = "npc_" + std::to_string(npc_id); + + LoadScript(filename, package_name); +} + +void LuaParser::LoadGlobalNPCScript(std::string filename) { + LoadScript(filename, "global_npc"); +} + +void LuaParser::LoadPlayerScript(std::string filename) { + LoadScript(filename, "player"); +} + +void LuaParser::LoadGlobalPlayerScript(std::string filename) { + LoadScript(filename, "global_player"); +} + +void LuaParser::LoadItemScript(std::string filename, EQEmu::ItemInstance *item) { + if (item == nullptr) + return; + std::string package_name = "item_"; + package_name += std::to_string(item->GetID()); + + LoadScript(filename, package_name); +} + +void LuaParser::LoadSpellScript(std::string filename, uint32 spell_id) { + std::string package_name = "spell_" + std::to_string(spell_id); + + LoadScript(filename, package_name); +} + +void LuaParser::LoadEncounterScript(std::string filename, std::string encounter_name) { + std::string package_name = "encounter_" + encounter_name; + + LoadScript(filename, package_name); +} + +void LuaParser::AddVar(std::string name, std::string val) { + mImpl->vars[name] = val; +} + +std::string LuaParser::GetVar(std::string name) { + auto iter = mImpl->vars.find(name); + if(iter != mImpl->vars.end()) { + return iter->second; + } + + return std::string(); +} + +void LuaParser::Init() { + ReloadQuests(); +} + +void LuaParser::ReloadQuests() { + mImpl->loaded.clear(); + errors_.clear(); + + mImpl->state.reset(new sol::state()); + mImpl->state->open_libraries(sol::lib::base, + sol::lib::package, + sol::lib::coroutine, + sol::lib::string, + sol::lib::os, + sol::lib::math, + sol::lib::table, + sol::lib::debug, + sol::lib::bit32, + sol::lib::io); + mImpl->state->script("math.randomseed(os.time())"); + + auto globals = mImpl->state->globals(); +#ifdef SANITIZE_LUA_LIBS + globals["io"] = sol::nil; + globals["collectgarbage"] = sol::nil; + globals["loadfile"] = sol::nil; + + auto os = globals["os"]; + os["exit"] = sol::nil; + os["execute"] = sol::nil; + os["getenv"] = sol::nil; + os["remove"] = sol::nil; + os["rename"] = sol::nil; + os["setlocale"] = sol::nil; + os["tmpname"] = sol::nil; +#endif + +#ifdef _WIN32 + const char libext[] = ".dll"; +#else + const char libext[] = ".so"; +#endif + +#if SOL_LUA_VERSION == 501 + const char lua_version[] = "5.1"; +#elif SOL_LUA_VERSION == 502 + const char lua_version[] = "5.2"; +#elif SOL_LUA_VERSION == 503 + const char lua_version[] = "5.3"; +#else +#error Unsupported Lua version +#endif + + auto package = globals["package"]; + std::string path = package["path"]; + std::string cpath = package["cpath"]; + + path += ";./" + Config->LuaModuleDir + "?.lua;./" + Config->LuaModuleDir + "?/init.lua"; + path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?.lua"; + path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?/init.lua"; + cpath += ";./" + Config->LuaModuleDir + "?" + libext; + cpath += ";./" + Config->LuaModuleDir + "lib/lua/" + lua_version + "/?" + libext; + + package["path"] = path; + package["cpath"] = cpath; + + MapFunctions(); + +// mImpl->loaded.clear(); +// errors_.clear(); +// mImpl->mods.clear(); +// lua_encounter_events_registered.clear(); +// lua_encounters_loaded.clear(); +// +// for (auto encounter : lua_encounters) { +// encounter.second->Depop(); +// } +// +// lua_encounters.clear(); +// // so the Depop function above depends on the Process being called again so ... +// // And there is situations where it wouldn't be :P +// entity_list.EncounterProcess(); +// +// if(mImpl->state) { +// lua_close(mImpl->state); +// } +// +// mImpl->state = luaL_newstate(); +// luaL_openlibs(mImpl->state); +// +// auto top = lua_gettop(mImpl->state); +// +// if(luaopen_bit(mImpl->state) != 1) { +// std::string error = lua_tostring(mImpl->state, -1); +// AddError(error); +// } +// +// if(luaL_dostring(mImpl->state, "math.randomseed(os.time())")) { +// std::string error = lua_tostring(mImpl->state, -1); +// AddError(error); +// } +// +//#ifdef SANITIZE_LUA_LIBS +// //io +// lua_pushnil(mImpl->state); +// //lua_setglobal(mImpl->state, "io"); +// +// //some os/debug are okay some are not +// lua_getglobal(mImpl->state, "os"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "exit"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "execute"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "getenv"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "remove"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "rename"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "setlocale"); +// lua_pushnil(mImpl->state); +// lua_setfield(mImpl->state, -2, "tmpname"); +// lua_pop(mImpl->state, 1); +// +// lua_pushnil(mImpl->state); +// lua_setglobal(mImpl->state, "collectgarbage"); +// +// lua_pushnil(mImpl->state); +// lua_setglobal(mImpl->state, "loadfile"); +// +//#endif +// +// // lua 5.2+ defines these +//#if defined(LUA_VERSION_MAJOR) && defined(LUA_VERSION_MINOR) +// const char lua_version[] = LUA_VERSION_MAJOR "." LUA_VERSION_MINOR; +//#elif LUA_VERSION_NUM == 501 +// const char lua_version[] = "5.1"; +//#else +//#error Incompatible lua version +//#endif +// +//#ifdef WINDOWS +// const char libext[] = ".dll"; +//#else +// // lua doesn't care OSX doesn't use sonames +// const char libext[] = ".so"; +//#endif +// +// lua_getglobal(mImpl->state, "package"); +// lua_getfield(mImpl->state, -1, "path"); +// std::string module_path = lua_tostring(mImpl->state,-1); +// module_path += ";./" + Config->LuaModuleDir + "?.lua;./" + Config->LuaModuleDir + "?/init.lua"; +// // luarock paths using lua_modules as tree +// // to path it adds foo/share/lua/5.1/?.lua and foo/share/lua/5.1/?/init.lua +// module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?.lua"; +// module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?/init.lua"; +// lua_pop(mImpl->state, 1); +// lua_pushstring(mImpl->state, module_path.c_str()); +// lua_setfield(mImpl->state, -2, "path"); +// lua_pop(mImpl->state, 1); +// +// lua_getglobal(mImpl->state, "package"); +// lua_getfield(mImpl->state, -1, "cpath"); +// module_path = lua_tostring(mImpl->state, -1); +// module_path += ";./" + Config->LuaModuleDir + "?" + libext; +// // luarock paths using lua_modules as tree +// // luarocks adds foo/lib/lua/5.1/?.so for cpath +// module_path += ";./" + Config->LuaModuleDir + "lib/lua/" + lua_version + "/?" + libext; +// lua_pop(mImpl->state, 1); +// lua_pushstring(mImpl->state, module_path.c_str()); +// lua_setfield(mImpl->state, -2, "cpath"); +// lua_pop(mImpl->state, 1); +// +// MapFunctions(mImpl->state); +// +// //load init +// std::string path = Config->QuestDir; +// path += "/"; +// path += QUEST_GLOBAL_DIRECTORY; +// path += "/script_init.lua"; +// +// FILE *f = fopen(path.c_str(), "r"); +// if(f) { +// fclose(f); +// +// if(luaL_dofile(mImpl->state, path.c_str())) { +// std::string error = lua_tostring(mImpl->state, -1); +// AddError(error); +// } +// } +// +// //zone init - always loads after global +// if(zone) { +// std::string zone_script = Config->QuestDir; +// zone_script += "/"; +// zone_script += zone->GetShortName(); +// zone_script += "/script_init_v"; +// zone_script += std::to_string(zone->GetInstanceVersion()); +// zone_script += ".lua"; +// f = fopen(zone_script.c_str(), "r"); +// if(f) { +// fclose(f); +// +// if(luaL_dofile(mImpl->state, zone_script.c_str())) { +// std::string error = lua_tostring(mImpl->state, -1); +// AddError(error); +// } +// } +// else { +// zone_script = Config->QuestDir; +// zone_script += "/"; +// zone_script += zone->GetShortName(); +// zone_script += "/script_init.lua"; +// f = fopen(zone_script.c_str(), "r"); +// if (f) { +// fclose(f); +// +// if (luaL_dofile(mImpl->state, zone_script.c_str())) { +// std::string error = lua_tostring(mImpl->state, -1); +// AddError(error); +// } +// } +// } +// } +// +// FILE *load_order = fopen("mods/load_order.txt", "r"); +// if (load_order) { +// char file_name[256] = { 0 }; +// while (fgets(file_name, 256, load_order) != nullptr) { +// for (int i = 0; i < 256; ++i) { +// auto c = file_name[i]; +// if (c == '\n' || c == '\r' || c == ' ') { +// file_name[i] = 0; +// break; +// } +// } +// +// LoadScript("mods/" + std::string(file_name), file_name); +// mImpl->mods.push_back(LuaMod(mImpl->state, this, file_name)); +// } +// +// fclose(load_order); +// } +// +// auto end = lua_gettop(mImpl->state); +// int n = end - top; +// if (n > 0) { +// lua_pop(mImpl->state, n); +// } +} + +void LuaParser::LoadScript(const std::string &filename) +{ +} + +void LuaParser::LoadScript(const std::string &filename, const std::string &package_name) { + auto iter = mImpl->loaded.find(package_name); + if(iter != mImpl->loaded.end()) { + return; + } + + Script s; + s.Env = sol::environment(*mImpl->state, sol::create, mImpl->state->globals()); + try { + auto res = mImpl->state->safe_script_file(filename); + if (res.valid()) { + s.Loaded = true; + } + else { + sol::error err = res; + AddError(err.what()); + s.Loaded = false; + } + } + catch (sol::error &err) { + AddError(err.what()); + } + + mImpl->loaded[package_name] = s; +} + +bool LuaParser::HasFunction(const std::string &subname, const std::string &package_name) { + auto iter = mImpl->loaded.find(package_name); + if(iter == mImpl->loaded.end()) { + return false; + } + + auto f = iter->second.Env[subname]; + if (f.valid() && f.get_type() == sol::type::function) { + return true; + } + + return false; +} + +void LuaParser::MapFunctions() { + lua_register_entity(mImpl->state.get()); + lua_register_mob(mImpl->state.get()); + lua_register_npc(mImpl->state.get()); + lua_register_client(mImpl->state.get()); + lua_register_doors(mImpl->state.get()); + lua_register_corpse(mImpl->state.get()); + lua_register_object(mImpl->state.get()); + lua_register_general(mImpl->state.get()); +} + +int LuaParser::DispatchEventNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, + std::vector *extra_pointers) { +// evt = ConvertLuaEvent(evt); +// if(evt >= _LargestEventID) { +// return 0; +// } +// +// if(!npc) +// return 0; +// +// std::string package_name = "npc_" + std::to_string(npc->GetNPCTypeID()); +// int ret = 0; +// +// auto iter = lua_encounter_events_registered.find(package_name); +// if(iter != lua_encounter_events_registered.end()) { +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// } +// +// iter = lua_encounter_events_registered.find("npc_-1"); +// if(iter == lua_encounter_events_registered.end()) { +// return ret; +// } +// +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// +// return ret; + return 0; +} + +int LuaParser::DispatchEventPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, + std::vector *extra_pointers) { +// evt = ConvertLuaEvent(evt); +// if(evt >= _LargestEventID) { +// return 0; +// } +// +// std::string package_name = "player"; +// +// auto iter = lua_encounter_events_registered.find(package_name); +// if(iter == lua_encounter_events_registered.end()) { +// return 0; +// } +// +// int ret = 0; +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventPlayer(package_name, evt, client, data, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// +// return ret; + return 0; +} + +int LuaParser::DispatchEventItem(QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, std::string data, uint32 extra_data, + std::vector *extra_pointers) { +// evt = ConvertLuaEvent(evt); +// if(evt >= _LargestEventID) { +// return 0; +// } +// +// if(!item) +// return 0; +// +// std::string package_name = "item_"; +// package_name += std::to_string(item->GetID()); +// int ret = 0; +// +// auto iter = lua_encounter_events_registered.find(package_name); +// if(iter != lua_encounter_events_registered.end()) { +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// } +// +// iter = lua_encounter_events_registered.find("item_-1"); +// if(iter == lua_encounter_events_registered.end()) { +// return ret; +// } +// +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// return ret; + return 0; +} + +int LuaParser::DispatchEventSpell(QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, + std::vector *extra_pointers) { +// evt = ConvertLuaEvent(evt); +// if(evt >= _LargestEventID) { +// return 0; +// } +// +// std::string package_name = "spell_" + std::to_string(spell_id); +// +// int ret = 0; +// auto iter = lua_encounter_events_registered.find(package_name); +// if(iter != lua_encounter_events_registered.end()) { +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) { +// ret = i; +// } +// } +// ++riter; +// } +// } +// +// iter = lua_encounter_events_registered.find("spell_-1"); +// if(iter == lua_encounter_events_registered.end()) { +// return ret; +// } +// +// auto riter = iter->second.begin(); +// while(riter != iter->second.end()) { +// if(riter->event_id == evt) { +// std::string package_name = "encounter_" + riter->encounter_name; +// int i = _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers, &riter->lua_reference); +// if(i != 0) +// ret = i; +// } +// ++riter; +// } +// return ret; + return 0; +} + +QuestEventID LuaParser::ConvertLuaEvent(QuestEventID evt) { + switch(evt) { + case EVENT_SLAY: + case EVENT_NPC_SLAY: + return EVENT_SLAY; + break; + case EVENT_SPELL_EFFECT_CLIENT: + case EVENT_SPELL_EFFECT_NPC: + return EVENT_SPELL_EFFECT_CLIENT; + break; + case EVENT_SPELL_BUFF_TIC_CLIENT: + case EVENT_SPELL_BUFF_TIC_NPC: + return EVENT_SPELL_BUFF_TIC_CLIENT; + break; + case EVENT_AGGRO: + case EVENT_ATTACK: + return _LargestEventID; + break; + default: + return evt; + } +} + +void LuaParser::MeleeMitigation(Mob *self, Mob *attacker, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) +{ + //for (auto &mod : mImpl->mods) { + // mod.MeleeMitigation(self, attacker, hit, opts, ignoreDefault); + //} +} + +void LuaParser::ApplyDamageTable(Mob *self, DamageHitInfo &hit, bool &ignoreDefault) +{ + //for (auto &mod : mImpl->mods) { + // mod.ApplyDamageTable(self, hit, ignoreDefault); + //} +} + +bool LuaParser::AvoidDamage(Mob *self, Mob *other, DamageHitInfo &hit, bool & ignoreDefault) +{ + bool retval = false; + //for (auto &mod : mImpl->mods) { + // mod.AvoidDamage(self, other, hit, retval, ignoreDefault); + //} + return retval; +} + +bool LuaParser::CheckHitChance(Mob *self, Mob *other, DamageHitInfo &hit, bool &ignoreDefault) +{ + bool retval = false; + //for (auto &mod : mImpl->mods) { + // mod.CheckHitChance(self, other, hit, retval, ignoreDefault); + //} + return retval; +} + +void LuaParser::TryCriticalHit(Mob *self, Mob *defender, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) +{ + //for (auto &mod : mImpl->mods) { + // mod.TryCriticalHit(self, defender, hit, opts, ignoreDefault); + //} +} + +void LuaParser::CommonOutgoingHitSuccess(Mob *self, Mob *other, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) +{ + //for (auto &mod : mImpl->mods) { + // mod.CommonOutgoingHitSuccess(self, other, hit, opts, ignoreDefault); + //} +} + +uint32 LuaParser::GetRequiredAAExperience(Client *self, bool &ignoreDefault) +{ + uint32 retval = 0; + //for (auto &mod : mImpl->mods) { + // mod.GetRequiredAAExperience(self, retval, ignoreDefault); + //} + return retval; +} + +uint32 LuaParser::GetEXPForLevel(Client *self, uint16 level, bool &ignoreDefault) +{ + uint32 retval = 0; + //for (auto &mod : mImpl->mods) { + // mod.GetEXPForLevel(self, level, retval, ignoreDefault); + //} + return retval; +} + +uint32 LuaParser::GetExperienceForKill(Client *self, Mob *against, bool &ignoreDefault) +{ + uint32 retval = 0; + //for (auto &mod : mImpl->mods) { + // mod.GetExperienceForKill(self, against, retval, ignoreDefault); + //} + return retval; +} diff --git a/zone/lua_parser.h b/zone/lua/lua_parser.h similarity index 82% rename from zone/lua_parser.h rename to zone/lua/lua_parser.h index 83969b650..a18704e87 100644 --- a/zone/lua_parser.h +++ b/zone/lua/lua_parser.h @@ -1,20 +1,9 @@ -#ifndef _EQE_LUA_PARSER_H -#define _EQE_LUA_PARSER_H -#ifdef LUA_EQEMU - -#include "quest_parser_collection.h" -#include "quest_interface.h" +#pragma once +#include "../quest_parser_collection.h" +#include "../quest_interface.h" #include -#include -#include -#include +#include -#include "zone_config.h" -#include "lua_mod.h" - -extern const ZoneConfig *Config; - -struct lua_State; class Client; class NPC; @@ -23,15 +12,6 @@ namespace EQEmu class ItemInstance; } -#include "lua_parser_events.h" - -struct lua_registered_event; -namespace luabind { - namespace adl { - class object; - } -} - class LuaParser : public QuestInterface { public: ~LuaParser(); @@ -87,7 +67,7 @@ public: return &inst; } - bool HasFunction(std::string function, std::string package_name); + bool HasFunction(const std::string &function, const std::string &package_name); //Mod Extensions void MeleeMitigation(Mob *self, Mob *attacker, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault); @@ -102,27 +82,25 @@ public: private: LuaParser(); - LuaParser(const LuaParser&); - LuaParser& operator=(const LuaParser&); + LuaParser(const LuaParser&) = delete; + LuaParser& operator=(const LuaParser&) = delete; int _EventNPC(std::string package_name, QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func = nullptr); + std::vector *extra_pointers, sol::function *l_func = nullptr); int _EventPlayer(std::string package_name, QuestEventID evt, Client *client, std::string data, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func = nullptr); + std::vector *extra_pointers, sol::function *l_func = nullptr); int _EventItem(std::string package_name, QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, std::string data, - uint32 extra_data, std::vector *extra_pointers, luabind::adl::object *l_func = nullptr); + uint32 extra_data, std::vector *extra_pointers, sol::function *l_func = nullptr); int _EventSpell(std::string package_name, QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func = nullptr); + std::vector *extra_pointers, sol::function *l_func = nullptr); int _EventEncounter(std::string package_name, QuestEventID evt, std::string encounter_name, std::string data, uint32 extra_data, std::vector *extra_pointers); - void LoadScript(std::string filename, std::string package_name); - void MapFunctions(lua_State *L); + void LoadScript(const std::string &filename); + void LoadScript(const std::string &filename, const std::string &package_name); + void MapFunctions(); QuestEventID ConvertLuaEvent(QuestEventID evt); struct Implementation; std::unique_ptr mImpl; }; - -#endif -#endif diff --git a/zone/lua/lua_structs.h b/zone/lua/lua_structs.h new file mode 100644 index 000000000..20ea48455 --- /dev/null +++ b/zone/lua/lua_structs.h @@ -0,0 +1,7 @@ +#pragma once + +struct Script +{ + bool Loaded; + sol::environment Env; +}; \ No newline at end of file diff --git a/zone/lua_bit.cpp b/zone/lua/old/lua_bit.cpp similarity index 100% rename from zone/lua_bit.cpp rename to zone/lua/old/lua_bit.cpp diff --git a/zone/lua_bit.h b/zone/lua/old/lua_bit.h similarity index 100% rename from zone/lua_bit.h rename to zone/lua/old/lua_bit.h diff --git a/zone/lua_client.cpp b/zone/lua/old/lua_client.cpp similarity index 100% rename from zone/lua_client.cpp rename to zone/lua/old/lua_client.cpp diff --git a/zone/lua_client.h b/zone/lua/old/lua_client.h similarity index 100% rename from zone/lua_client.h rename to zone/lua/old/lua_client.h diff --git a/zone/lua_corpse.cpp b/zone/lua/old/lua_corpse.cpp similarity index 100% rename from zone/lua_corpse.cpp rename to zone/lua/old/lua_corpse.cpp diff --git a/zone/lua_corpse.h b/zone/lua/old/lua_corpse.h similarity index 100% rename from zone/lua_corpse.h rename to zone/lua/old/lua_corpse.h diff --git a/zone/lua_door.cpp b/zone/lua/old/lua_door.cpp similarity index 100% rename from zone/lua_door.cpp rename to zone/lua/old/lua_door.cpp diff --git a/zone/lua_door.h b/zone/lua/old/lua_door.h similarity index 100% rename from zone/lua_door.h rename to zone/lua/old/lua_door.h diff --git a/zone/lua_encounter.cpp b/zone/lua/old/lua_encounter.cpp similarity index 100% rename from zone/lua_encounter.cpp rename to zone/lua/old/lua_encounter.cpp diff --git a/zone/lua_encounter.h b/zone/lua/old/lua_encounter.h similarity index 100% rename from zone/lua_encounter.h rename to zone/lua/old/lua_encounter.h diff --git a/zone/lua_entity.cpp b/zone/lua/old/lua_entity.cpp similarity index 100% rename from zone/lua_entity.cpp rename to zone/lua/old/lua_entity.cpp diff --git a/zone/lua_entity.h b/zone/lua/old/lua_entity.h similarity index 100% rename from zone/lua_entity.h rename to zone/lua/old/lua_entity.h diff --git a/zone/lua_entity_list.cpp b/zone/lua/old/lua_entity_list.cpp similarity index 100% rename from zone/lua_entity_list.cpp rename to zone/lua/old/lua_entity_list.cpp diff --git a/zone/lua_entity_list.h b/zone/lua/old/lua_entity_list.h similarity index 100% rename from zone/lua_entity_list.h rename to zone/lua/old/lua_entity_list.h diff --git a/zone/lua_general.cpp b/zone/lua/old/lua_general.cpp similarity index 91% rename from zone/lua_general.cpp rename to zone/lua/old/lua_general.cpp index da62dc305..9118cb212 100644 --- a/zone/lua_general.cpp +++ b/zone/lua/old/lua_general.cpp @@ -37,240 +37,240 @@ struct Filters { }; struct MessageTypes { }; struct Rule { }; -struct lua_registered_event { - std::string encounter_name; - luabind::adl::object lua_reference; - QuestEventID event_id; -}; - -extern std::map> lua_encounter_events_registered; -extern std::map lua_encounters_loaded; -extern std::map lua_encounters; - -extern void MapOpcodes(); -extern void ClearMappedOpcode(EmuOpcode op); - -void unregister_event(std::string package_name, std::string name, int evt); - -void load_encounter(std::string name) { - if(lua_encounters_loaded.count(name) > 0) - return; - auto enc = new Encounter(name.c_str()); - entity_list.AddEncounter(enc); - lua_encounters[name] = enc; - lua_encounters_loaded[name] = true; - parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, "", 0); -} - -void load_encounter_with_data(std::string name, std::string info_str) { - if(lua_encounters_loaded.count(name) > 0) - return; - auto enc = new Encounter(name.c_str()); - entity_list.AddEncounter(enc); - lua_encounters[name] = enc; - lua_encounters_loaded[name] = true; - std::vector info_ptrs; - info_ptrs.push_back(&info_str); - parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, "", 0, &info_ptrs); -} - -void unload_encounter(std::string name) { - if(lua_encounters_loaded.count(name) == 0) - return; - - auto liter = lua_encounter_events_registered.begin(); - while(liter != lua_encounter_events_registered.end()) { - std::list &elist = liter->second; - auto iter = elist.begin(); - while(iter != elist.end()) { - if((*iter).encounter_name.compare(name) == 0) { - iter = elist.erase(iter); - } else { - ++iter; - } - } - - if(elist.size() == 0) { - lua_encounter_events_registered.erase(liter++); - } else { - ++liter; - } - } - - lua_encounters[name]->Depop(); - lua_encounters.erase(name); - lua_encounters_loaded.erase(name); - parse->EventEncounter(EVENT_ENCOUNTER_UNLOAD, name, "", 0); -} - -void unload_encounter_with_data(std::string name, std::string info_str) { - if(lua_encounters_loaded.count(name) == 0) - return; - - auto liter = lua_encounter_events_registered.begin(); - while(liter != lua_encounter_events_registered.end()) { - std::list &elist = liter->second; - auto iter = elist.begin(); - while(iter != elist.end()) { - if((*iter).encounter_name.compare(name) == 0) { - iter = elist.erase(iter); - } - else { - ++iter; - } - } - - if(elist.size() == 0) { - lua_encounter_events_registered.erase(liter++); - } - else { - ++liter; - } - } - - lua_encounters[name]->Depop(); - lua_encounters.erase(name); - lua_encounters_loaded.erase(name); - std::vector info_ptrs; - info_ptrs.push_back(&info_str); - parse->EventEncounter(EVENT_ENCOUNTER_UNLOAD, name, "", 0, &info_ptrs); -} - -void register_event(std::string package_name, std::string name, int evt, luabind::adl::object func) { - if(lua_encounters_loaded.count(name) == 0) - return; - - unregister_event(package_name, name, evt); - - lua_registered_event e; - e.encounter_name = name; - e.lua_reference = func; - e.event_id = static_cast(evt); - - auto liter = lua_encounter_events_registered.find(package_name); - if(liter == lua_encounter_events_registered.end()) { - std::list elist; - elist.push_back(e); - lua_encounter_events_registered[package_name] = elist; - } else { - std::list &elist = liter->second; - elist.push_back(e); - } -} - -void unregister_event(std::string package_name, std::string name, int evt) { - auto liter = lua_encounter_events_registered.find(package_name); - if(liter != lua_encounter_events_registered.end()) { - std::list elist = liter->second; - auto iter = elist.begin(); - while(iter != elist.end()) { - if(iter->event_id == evt && iter->encounter_name.compare(name) == 0) { - iter = elist.erase(iter); - break; - } - ++iter; - } - lua_encounter_events_registered[package_name] = elist; - } -} - -void register_npc_event(std::string name, int evt, int npc_id, luabind::adl::object func) { - if(luabind::type(func) == LUA_TFUNCTION) { - std::stringstream package_name; - package_name << "npc_" << npc_id; - - register_event(package_name.str(), name, evt, func); - } -} - -void register_npc_event(int evt, int npc_id, luabind::adl::object func) { - std::string name = quest_manager.GetEncounter(); - register_npc_event(name, evt, npc_id, func); -} - -void unregister_npc_event(std::string name, int evt, int npc_id) { - std::stringstream package_name; - package_name << "npc_" << npc_id; - - unregister_event(package_name.str(), name, evt); -} - -void unregister_npc_event(int evt, int npc_id) { - std::string name = quest_manager.GetEncounter(); - unregister_npc_event(name, evt, npc_id); -} - -void register_player_event(std::string name, int evt, luabind::adl::object func) { - if(luabind::type(func) == LUA_TFUNCTION) { - register_event("player", name, evt, func); - } -} - -void register_player_event(int evt, luabind::adl::object func) { - std::string name = quest_manager.GetEncounter(); - register_player_event(name, evt, func); -} - -void unregister_player_event(std::string name, int evt) { - unregister_event("player", name, evt); -} - -void unregister_player_event(int evt) { - std::string name = quest_manager.GetEncounter(); - unregister_player_event(name, evt); -} - -void register_item_event(std::string name, int evt, int item_id, luabind::adl::object func) { - std::string package_name = "item_"; - package_name += std::to_string(item_id); - - if(luabind::type(func) == LUA_TFUNCTION) { - register_event(package_name, name, evt, func); - } -} - -void register_item_event(int evt, int item_id, luabind::adl::object func) { - std::string name = quest_manager.GetEncounter(); - register_item_event(name, evt, item_id, func); -} - -void unregister_item_event(std::string name, int evt, int item_id) { - std::string package_name = "item_"; - package_name += std::to_string(item_id); - - unregister_event(package_name, name, evt); -} - -void unregister_item_event(int evt, int item_id) { - std::string name = quest_manager.GetEncounter(); - unregister_item_event(name, evt, item_id); -} - -void register_spell_event(std::string name, int evt, int spell_id, luabind::adl::object func) { - if(luabind::type(func) == LUA_TFUNCTION) { - std::stringstream package_name; - package_name << "spell_" << spell_id; - - register_event(package_name.str(), name, evt, func); - } -} - -void register_spell_event(int evt, int spell_id, luabind::adl::object func) { - std::string name = quest_manager.GetEncounter(); - register_spell_event(name, evt, spell_id, func); -} - -void unregister_spell_event(std::string name, int evt, int spell_id) { - std::stringstream package_name; - package_name << "spell_" << spell_id; - - unregister_event(package_name.str(), name, evt); -} - -void unregister_spell_event(int evt, int spell_id) { - std::string name = quest_manager.GetEncounter(); - unregister_spell_event(name, evt, spell_id); -} +//struct lua_registered_event { +// std::string encounter_name; +// luabind::adl::object lua_reference; +// QuestEventID event_id; +//}; +// +//extern std::map> lua_encounter_events_registered; +//extern std::map lua_encounters_loaded; +//extern std::map lua_encounters; +// +//extern void MapOpcodes(); +//extern void ClearMappedOpcode(EmuOpcode op); +// +//void unregister_event(std::string package_name, std::string name, int evt); +// +//void load_encounter(std::string name) { +// if(lua_encounters_loaded.count(name) > 0) +// return; +// auto enc = new Encounter(name.c_str()); +// entity_list.AddEncounter(enc); +// lua_encounters[name] = enc; +// lua_encounters_loaded[name] = true; +// parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, "", 0); +//} +// +//void load_encounter_with_data(std::string name, std::string info_str) { +// if(lua_encounters_loaded.count(name) > 0) +// return; +// auto enc = new Encounter(name.c_str()); +// entity_list.AddEncounter(enc); +// lua_encounters[name] = enc; +// lua_encounters_loaded[name] = true; +// std::vector info_ptrs; +// info_ptrs.push_back(&info_str); +// parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, "", 0, &info_ptrs); +//} +// +//void unload_encounter(std::string name) { +// if(lua_encounters_loaded.count(name) == 0) +// return; +// +// auto liter = lua_encounter_events_registered.begin(); +// while(liter != lua_encounter_events_registered.end()) { +// std::list &elist = liter->second; +// auto iter = elist.begin(); +// while(iter != elist.end()) { +// if((*iter).encounter_name.compare(name) == 0) { +// iter = elist.erase(iter); +// } else { +// ++iter; +// } +// } +// +// if(elist.size() == 0) { +// lua_encounter_events_registered.erase(liter++); +// } else { +// ++liter; +// } +// } +// +// lua_encounters[name]->Depop(); +// lua_encounters.erase(name); +// lua_encounters_loaded.erase(name); +// parse->EventEncounter(EVENT_ENCOUNTER_UNLOAD, name, "", 0); +//} +// +//void unload_encounter_with_data(std::string name, std::string info_str) { +// if(lua_encounters_loaded.count(name) == 0) +// return; +// +// auto liter = lua_encounter_events_registered.begin(); +// while(liter != lua_encounter_events_registered.end()) { +// std::list &elist = liter->second; +// auto iter = elist.begin(); +// while(iter != elist.end()) { +// if((*iter).encounter_name.compare(name) == 0) { +// iter = elist.erase(iter); +// } +// else { +// ++iter; +// } +// } +// +// if(elist.size() == 0) { +// lua_encounter_events_registered.erase(liter++); +// } +// else { +// ++liter; +// } +// } +// +// lua_encounters[name]->Depop(); +// lua_encounters.erase(name); +// lua_encounters_loaded.erase(name); +// std::vector info_ptrs; +// info_ptrs.push_back(&info_str); +// parse->EventEncounter(EVENT_ENCOUNTER_UNLOAD, name, "", 0, &info_ptrs); +//} +// +//void register_event(std::string package_name, std::string name, int evt, luabind::adl::object func) { +// if(lua_encounters_loaded.count(name) == 0) +// return; +// +// unregister_event(package_name, name, evt); +// +// lua_registered_event e; +// e.encounter_name = name; +// e.lua_reference = func; +// e.event_id = static_cast(evt); +// +// auto liter = lua_encounter_events_registered.find(package_name); +// if(liter == lua_encounter_events_registered.end()) { +// std::list elist; +// elist.push_back(e); +// lua_encounter_events_registered[package_name] = elist; +// } else { +// std::list &elist = liter->second; +// elist.push_back(e); +// } +//} +// +//void unregister_event(std::string package_name, std::string name, int evt) { +// auto liter = lua_encounter_events_registered.find(package_name); +// if(liter != lua_encounter_events_registered.end()) { +// std::list elist = liter->second; +// auto iter = elist.begin(); +// while(iter != elist.end()) { +// if(iter->event_id == evt && iter->encounter_name.compare(name) == 0) { +// iter = elist.erase(iter); +// break; +// } +// ++iter; +// } +// lua_encounter_events_registered[package_name] = elist; +// } +//} +// +//void register_npc_event(std::string name, int evt, int npc_id, luabind::adl::object func) { +// if(luabind::type(func) == LUA_TFUNCTION) { +// std::stringstream package_name; +// package_name << "npc_" << npc_id; +// +// register_event(package_name.str(), name, evt, func); +// } +//} +// +//void register_npc_event(int evt, int npc_id, luabind::adl::object func) { +// std::string name = quest_manager.GetEncounter(); +// register_npc_event(name, evt, npc_id, func); +//} +// +//void unregister_npc_event(std::string name, int evt, int npc_id) { +// std::stringstream package_name; +// package_name << "npc_" << npc_id; +// +// unregister_event(package_name.str(), name, evt); +//} +// +//void unregister_npc_event(int evt, int npc_id) { +// std::string name = quest_manager.GetEncounter(); +// unregister_npc_event(name, evt, npc_id); +//} +// +//void register_player_event(std::string name, int evt, luabind::adl::object func) { +// if(luabind::type(func) == LUA_TFUNCTION) { +// register_event("player", name, evt, func); +// } +//} +// +//void register_player_event(int evt, luabind::adl::object func) { +// std::string name = quest_manager.GetEncounter(); +// register_player_event(name, evt, func); +//} +// +//void unregister_player_event(std::string name, int evt) { +// unregister_event("player", name, evt); +//} +// +//void unregister_player_event(int evt) { +// std::string name = quest_manager.GetEncounter(); +// unregister_player_event(name, evt); +//} +// +//void register_item_event(std::string name, int evt, int item_id, luabind::adl::object func) { +// std::string package_name = "item_"; +// package_name += std::to_string(item_id); +// +// if(luabind::type(func) == LUA_TFUNCTION) { +// register_event(package_name, name, evt, func); +// } +//} +// +//void register_item_event(int evt, int item_id, luabind::adl::object func) { +// std::string name = quest_manager.GetEncounter(); +// register_item_event(name, evt, item_id, func); +//} +// +//void unregister_item_event(std::string name, int evt, int item_id) { +// std::string package_name = "item_"; +// package_name += std::to_string(item_id); +// +// unregister_event(package_name, name, evt); +//} +// +//void unregister_item_event(int evt, int item_id) { +// std::string name = quest_manager.GetEncounter(); +// unregister_item_event(name, evt, item_id); +//} +// +//void register_spell_event(std::string name, int evt, int spell_id, luabind::adl::object func) { +// if(luabind::type(func) == LUA_TFUNCTION) { +// std::stringstream package_name; +// package_name << "spell_" << spell_id; +// +// register_event(package_name.str(), name, evt, func); +// } +//} +// +//void register_spell_event(int evt, int spell_id, luabind::adl::object func) { +// std::string name = quest_manager.GetEncounter(); +// register_spell_event(name, evt, spell_id, func); +//} +// +//void unregister_spell_event(std::string name, int evt, int spell_id) { +// std::stringstream package_name; +// package_name << "spell_" << spell_id; +// +// unregister_event(package_name.str(), name, evt); +//} +// +//void unregister_spell_event(int evt, int spell_id) { +// std::string name = quest_manager.GetEncounter(); +// unregister_spell_event(name, evt, spell_id); +//} Lua_Mob lua_spawn2(int npc_type, int grid, int unused, double x, double y, double z, double heading) { auto position = glm::vec4(x, y, z, heading); diff --git a/zone/lua_general.h b/zone/lua/old/lua_general.h similarity index 100% rename from zone/lua_general.h rename to zone/lua/old/lua_general.h diff --git a/zone/lua_group.cpp b/zone/lua/old/lua_group.cpp similarity index 100% rename from zone/lua_group.cpp rename to zone/lua/old/lua_group.cpp diff --git a/zone/lua_group.h b/zone/lua/old/lua_group.h similarity index 100% rename from zone/lua_group.h rename to zone/lua/old/lua_group.h diff --git a/zone/lua_hate_entry.cpp b/zone/lua/old/lua_hate_entry.cpp similarity index 100% rename from zone/lua_hate_entry.cpp rename to zone/lua/old/lua_hate_entry.cpp diff --git a/zone/lua_hate_list.cpp b/zone/lua/old/lua_hate_list.cpp similarity index 100% rename from zone/lua_hate_list.cpp rename to zone/lua/old/lua_hate_list.cpp diff --git a/zone/lua_hate_list.h b/zone/lua/old/lua_hate_list.h similarity index 100% rename from zone/lua_hate_list.h rename to zone/lua/old/lua_hate_list.h diff --git a/zone/lua_inventory.cpp b/zone/lua/old/lua_inventory.cpp similarity index 100% rename from zone/lua_inventory.cpp rename to zone/lua/old/lua_inventory.cpp diff --git a/zone/lua_inventory.h b/zone/lua/old/lua_inventory.h similarity index 100% rename from zone/lua_inventory.h rename to zone/lua/old/lua_inventory.h diff --git a/zone/lua_item.cpp b/zone/lua/old/lua_item.cpp similarity index 100% rename from zone/lua_item.cpp rename to zone/lua/old/lua_item.cpp diff --git a/zone/lua_item.h b/zone/lua/old/lua_item.h similarity index 100% rename from zone/lua_item.h rename to zone/lua/old/lua_item.h diff --git a/zone/lua_iteminst.cpp b/zone/lua/old/lua_iteminst.cpp similarity index 100% rename from zone/lua_iteminst.cpp rename to zone/lua/old/lua_iteminst.cpp diff --git a/zone/lua_iteminst.h b/zone/lua/old/lua_iteminst.h similarity index 100% rename from zone/lua_iteminst.h rename to zone/lua/old/lua_iteminst.h diff --git a/zone/lua_mob.cpp b/zone/lua/old/lua_mob.cpp similarity index 100% rename from zone/lua_mob.cpp rename to zone/lua/old/lua_mob.cpp diff --git a/zone/lua_mob.h b/zone/lua/old/lua_mob.h similarity index 100% rename from zone/lua_mob.h rename to zone/lua/old/lua_mob.h diff --git a/zone/lua_mod.cpp b/zone/lua/old/lua_mod.cpp similarity index 99% rename from zone/lua_mod.cpp rename to zone/lua/old/lua_mod.cpp index a891fb064..93099a702 100644 --- a/zone/lua_mod.cpp +++ b/zone/lua/old/lua_mod.cpp @@ -2,11 +2,11 @@ #include #include -#include "../common/spdat.h" -#include "masterentity.h" -#include "questmgr.h" -#include "zone.h" -#include "zone_config.h" +#include "../../common/spdat.h" +#include "../masterentity.h" +#include "../questmgr.h" +#include "../zone.h" +#include "../zone_config.h" #include "lua_parser.h" #include "lua_mod.h" diff --git a/zone/lua_mod.h b/zone/lua/old/lua_mod.h similarity index 100% rename from zone/lua_mod.h rename to zone/lua/old/lua_mod.h diff --git a/zone/lua_npc.cpp b/zone/lua/old/lua_npc.cpp similarity index 100% rename from zone/lua_npc.cpp rename to zone/lua/old/lua_npc.cpp diff --git a/zone/lua_npc.h b/zone/lua/old/lua_npc.h similarity index 100% rename from zone/lua_npc.h rename to zone/lua/old/lua_npc.h diff --git a/zone/lua_object.cpp b/zone/lua/old/lua_object.cpp similarity index 100% rename from zone/lua_object.cpp rename to zone/lua/old/lua_object.cpp diff --git a/zone/lua_object.h b/zone/lua/old/lua_object.h similarity index 100% rename from zone/lua_object.h rename to zone/lua/old/lua_object.h diff --git a/zone/lua_packet.cpp b/zone/lua/old/lua_packet.cpp similarity index 100% rename from zone/lua_packet.cpp rename to zone/lua/old/lua_packet.cpp diff --git a/zone/lua_packet.h b/zone/lua/old/lua_packet.h similarity index 100% rename from zone/lua_packet.h rename to zone/lua/old/lua_packet.h diff --git a/zone/lua_parser_events.cpp b/zone/lua/old/lua_parser_events.cpp similarity index 100% rename from zone/lua_parser_events.cpp rename to zone/lua/old/lua_parser_events.cpp diff --git a/zone/lua_parser_events.h b/zone/lua/old/lua_parser_events.h similarity index 100% rename from zone/lua_parser_events.h rename to zone/lua/old/lua_parser_events.h diff --git a/zone/lua_raid.cpp b/zone/lua/old/lua_raid.cpp similarity index 100% rename from zone/lua_raid.cpp rename to zone/lua/old/lua_raid.cpp diff --git a/zone/lua_raid.h b/zone/lua/old/lua_raid.h similarity index 100% rename from zone/lua_raid.h rename to zone/lua/old/lua_raid.h diff --git a/zone/lua_spawn.cpp b/zone/lua/old/lua_spawn.cpp similarity index 100% rename from zone/lua_spawn.cpp rename to zone/lua/old/lua_spawn.cpp diff --git a/zone/lua_spawn.h b/zone/lua/old/lua_spawn.h similarity index 100% rename from zone/lua_spawn.h rename to zone/lua/old/lua_spawn.h diff --git a/zone/lua_spell.cpp b/zone/lua/old/lua_spell.cpp similarity index 99% rename from zone/lua_spell.cpp rename to zone/lua/old/lua_spell.cpp index 18353efaa..16e3fe9ca 100644 --- a/zone/lua_spell.cpp +++ b/zone/lua/old/lua_spell.cpp @@ -3,7 +3,7 @@ #include "lua.hpp" #include -#include "../common/spdat.h" +#include "../../common/spdat.h" #include "lua_spell.h" Lua_Spell::Lua_Spell(int id) { diff --git a/zone/lua_spell.h b/zone/lua/old/lua_spell.h similarity index 100% rename from zone/lua_spell.h rename to zone/lua/old/lua_spell.h diff --git a/zone/lua_stat_bonuses.cpp b/zone/lua/old/lua_stat_bonuses.cpp similarity index 100% rename from zone/lua_stat_bonuses.cpp rename to zone/lua/old/lua_stat_bonuses.cpp diff --git a/zone/lua_stat_bonuses.h b/zone/lua/old/lua_stat_bonuses.h similarity index 100% rename from zone/lua_stat_bonuses.h rename to zone/lua/old/lua_stat_bonuses.h diff --git a/zone/lua_parser.cpp b/zone/lua_parser.cpp deleted file mode 100644 index b405de39b..000000000 --- a/zone/lua_parser.cpp +++ /dev/null @@ -1,1389 +0,0 @@ -#ifdef LUA_EQEMU - -#include "lua.hpp" -#include -#include - -#include -#include -#include -#include -#include - -#include "../common/spdat.h" -#include "masterentity.h" -#include "questmgr.h" -#include "zone.h" -#include "zone_config.h" - -#include "lua_parser.h" -#include "lua_bit.h" -#include "lua_entity.h" -#include "lua_item.h" -#include "lua_iteminst.h" -#include "lua_mob.h" -#include "lua_hate_list.h" -#include "lua_client.h" -#include "lua_inventory.h" -#include "lua_npc.h" -#include "lua_spell.h" -#include "lua_entity_list.h" -#include "lua_group.h" -#include "lua_raid.h" -#include "lua_corpse.h" -#include "lua_object.h" -#include "lua_door.h" -#include "lua_spawn.h" -#include "lua_packet.h" -#include "lua_general.h" -#include "lua_encounter.h" -#include "lua_stat_bonuses.h" - -const char *LuaEvents[_LargestEventID] = { - "event_say", - "event_trade", - "event_death", - "event_spawn", - "event_attack", - "event_combat", - "event_aggro", - "event_slay", - "event_npc_slay", - "event_waypoint_arrive", - "event_waypoint_depart", - "event_timer", - "event_signal", - "event_hp", - "event_enter", - "event_exit", - "event_enter_zone", - "event_click_door", - "event_loot", - "event_zone", - "event_level_up", - "event_killed_merit", - "event_cast_on", - "event_task_accepted", - "event_task_stage_complete", - "event_task_update", - "event_task_complete", - "event_task_fail", - "event_aggro_say", - "event_player_pickup", - "event_popup_response", - "event_environmental_damage", - "event_proximity_say", - "event_cast", - "event_cast_begin", - "event_scale_calc", - "event_item_enter_zone", - "event_target_change", - "event_hate_list", - "event_spell_effect", - "event_spell_effect", - "event_spell_buff_tic", - "event_spell_buff_tic", - "event_spell_fade", - "event_spell_effect_translocate_complete", - "event_combine_success", - "event_combine_failure", - "event_item_click", - "event_item_click_cast", - "event_group_change", - "event_forage_success", - "event_forage_failure", - "event_fish_start", - "event_fish_success", - "event_fish_failure", - "event_click_object", - "event_discover_item", - "event_disconnect", - "event_connect", - "event_item_tick", - "event_duel_win", - "event_duel_lose", - "event_encounter_load", - "event_encounter_unload", - "event_command", - "event_drop_item", - "event_destroy_item", - "event_feign_death", - "event_weapon_proc", - "event_equip_item", - "event_unequip_item", - "event_augment_item", - "event_unaugment_item", - "event_augment_insert", - "event_augment_remove", - "event_enter_area", - "event_leave_area", - "event_respawn", - "event_death_complete", - "event_unhandled_opcode", - "event_tick", - "event_spawn_zone", - "event_death_zone", - "event_use_skill" -}; - -extern Zone *zone; - -struct LuaParser::Implementation -{ - std::map vars; - std::map loaded; - std::vector mods; - lua_State *state; - NPCArgumentHandler NPCArgumentDispatch[_LargestEventID]; - PlayerArgumentHandler PlayerArgumentDispatch[_LargestEventID]; - ItemArgumentHandler ItemArgumentDispatch[_LargestEventID]; - SpellArgumentHandler SpellArgumentDispatch[_LargestEventID]; - EncounterArgumentHandler EncounterArgumentDispatch[_LargestEventID]; -}; - -struct lua_registered_event { - std::string encounter_name; - luabind::adl::object lua_reference; - QuestEventID event_id; -}; - -std::map> lua_encounter_events_registered; -std::map lua_encounters_loaded; -std::map lua_encounters; - -LuaParser::LuaParser() { - mImpl.reset(new Implementation()); - - for(int i = 0; i < _LargestEventID; ++i) { - mImpl->NPCArgumentDispatch[i] = handle_npc_null; - mImpl->PlayerArgumentDispatch[i] = handle_player_null; - mImpl->ItemArgumentDispatch[i] = handle_item_null; - mImpl->SpellArgumentDispatch[i] = handle_spell_null; - mImpl->EncounterArgumentDispatch[i] = handle_encounter_null; - } - - mImpl->NPCArgumentDispatch[EVENT_SAY] = handle_npc_event_say; - mImpl->NPCArgumentDispatch[EVENT_AGGRO_SAY] = handle_npc_event_say; - mImpl->NPCArgumentDispatch[EVENT_PROXIMITY_SAY] = handle_npc_event_say; - mImpl->NPCArgumentDispatch[EVENT_TRADE] = handle_npc_event_trade; - mImpl->NPCArgumentDispatch[EVENT_HP] = handle_npc_event_hp; - mImpl->NPCArgumentDispatch[EVENT_TARGET_CHANGE] = handle_npc_single_mob; - mImpl->NPCArgumentDispatch[EVENT_CAST_ON] = handle_npc_cast; - mImpl->NPCArgumentDispatch[EVENT_KILLED_MERIT] = handle_npc_single_client; - mImpl->NPCArgumentDispatch[EVENT_SLAY] = handle_npc_single_mob; - mImpl->NPCArgumentDispatch[EVENT_ENTER] = handle_npc_single_client; - mImpl->NPCArgumentDispatch[EVENT_EXIT] = handle_npc_single_client; - mImpl->NPCArgumentDispatch[EVENT_TASK_ACCEPTED] = handle_npc_task_accepted; - mImpl->NPCArgumentDispatch[EVENT_POPUP_RESPONSE] = handle_npc_popup; - mImpl->NPCArgumentDispatch[EVENT_WAYPOINT_ARRIVE] = handle_npc_waypoint; - mImpl->NPCArgumentDispatch[EVENT_WAYPOINT_DEPART] = handle_npc_waypoint; - mImpl->NPCArgumentDispatch[EVENT_HATE_LIST] = handle_npc_hate; - mImpl->NPCArgumentDispatch[EVENT_COMBAT] = handle_npc_hate; - mImpl->NPCArgumentDispatch[EVENT_SIGNAL] = handle_npc_signal; - mImpl->NPCArgumentDispatch[EVENT_TIMER] = handle_npc_timer; - mImpl->NPCArgumentDispatch[EVENT_DEATH] = handle_npc_death; - mImpl->NPCArgumentDispatch[EVENT_DEATH_COMPLETE] = handle_npc_death; - mImpl->NPCArgumentDispatch[EVENT_CAST] = handle_npc_cast; - mImpl->NPCArgumentDispatch[EVENT_CAST_BEGIN] = handle_npc_cast; - mImpl->NPCArgumentDispatch[EVENT_FEIGN_DEATH] = handle_npc_single_client; - mImpl->NPCArgumentDispatch[EVENT_ENTER_AREA] = handle_npc_area; - mImpl->NPCArgumentDispatch[EVENT_LEAVE_AREA] = handle_npc_area; - - mImpl->PlayerArgumentDispatch[EVENT_SAY] = handle_player_say; - mImpl->PlayerArgumentDispatch[EVENT_ENVIRONMENTAL_DAMAGE] = handle_player_environmental_damage; - mImpl->PlayerArgumentDispatch[EVENT_DEATH] = handle_player_death; - mImpl->PlayerArgumentDispatch[EVENT_DEATH_COMPLETE] = handle_player_death; - mImpl->PlayerArgumentDispatch[EVENT_TIMER] = handle_player_timer; - mImpl->PlayerArgumentDispatch[EVENT_DISCOVER_ITEM] = handle_player_discover_item; - mImpl->PlayerArgumentDispatch[EVENT_FISH_SUCCESS] = handle_player_fish_forage_success; - mImpl->PlayerArgumentDispatch[EVENT_FORAGE_SUCCESS] = handle_player_fish_forage_success; - mImpl->PlayerArgumentDispatch[EVENT_CLICK_OBJECT] = handle_player_click_object; - mImpl->PlayerArgumentDispatch[EVENT_CLICK_DOOR] = handle_player_click_door; - mImpl->PlayerArgumentDispatch[EVENT_SIGNAL] = handle_player_signal; - mImpl->PlayerArgumentDispatch[EVENT_POPUP_RESPONSE] = handle_player_popup_response; - mImpl->PlayerArgumentDispatch[EVENT_PLAYER_PICKUP] = handle_player_pick_up; - mImpl->PlayerArgumentDispatch[EVENT_CAST] = handle_player_cast; - mImpl->PlayerArgumentDispatch[EVENT_CAST_BEGIN] = handle_player_cast; - mImpl->PlayerArgumentDispatch[EVENT_TASK_FAIL] = handle_player_task_fail; - mImpl->PlayerArgumentDispatch[EVENT_ZONE] = handle_player_zone; - mImpl->PlayerArgumentDispatch[EVENT_DUEL_WIN] = handle_player_duel_win; - mImpl->PlayerArgumentDispatch[EVENT_DUEL_LOSE] = handle_player_duel_loss; - mImpl->PlayerArgumentDispatch[EVENT_LOOT] = handle_player_loot; - mImpl->PlayerArgumentDispatch[EVENT_TASK_STAGE_COMPLETE] = handle_player_task_stage_complete; - mImpl->PlayerArgumentDispatch[EVENT_TASK_COMPLETE] = handle_player_task_update; - mImpl->PlayerArgumentDispatch[EVENT_TASK_UPDATE] = handle_player_task_update; - mImpl->PlayerArgumentDispatch[EVENT_COMMAND] = handle_player_command; - mImpl->PlayerArgumentDispatch[EVENT_COMBINE_SUCCESS] = handle_player_combine; - mImpl->PlayerArgumentDispatch[EVENT_COMBINE_FAILURE] = handle_player_combine; - mImpl->PlayerArgumentDispatch[EVENT_FEIGN_DEATH] = handle_player_feign; - mImpl->PlayerArgumentDispatch[EVENT_ENTER_AREA] = handle_player_area; - mImpl->PlayerArgumentDispatch[EVENT_LEAVE_AREA] = handle_player_area; - mImpl->PlayerArgumentDispatch[EVENT_RESPAWN] = handle_player_respawn; - mImpl->PlayerArgumentDispatch[EVENT_UNHANDLED_OPCODE] = handle_player_packet; - mImpl->PlayerArgumentDispatch[EVENT_USE_SKILL] = handle_player_use_skill; - - mImpl->ItemArgumentDispatch[EVENT_ITEM_CLICK] = handle_item_click; - mImpl->ItemArgumentDispatch[EVENT_ITEM_CLICK_CAST] = handle_item_click; - mImpl->ItemArgumentDispatch[EVENT_TIMER] = handle_item_timer; - mImpl->ItemArgumentDispatch[EVENT_WEAPON_PROC] = handle_item_proc; - mImpl->ItemArgumentDispatch[EVENT_LOOT] = handle_item_loot; - mImpl->ItemArgumentDispatch[EVENT_EQUIP_ITEM] = handle_item_equip; - mImpl->ItemArgumentDispatch[EVENT_UNEQUIP_ITEM] = handle_item_equip; - mImpl->ItemArgumentDispatch[EVENT_AUGMENT_ITEM] = handle_item_augment; - mImpl->ItemArgumentDispatch[EVENT_UNAUGMENT_ITEM] = handle_item_augment; - mImpl->ItemArgumentDispatch[EVENT_AUGMENT_INSERT] = handle_item_augment_insert; - mImpl->ItemArgumentDispatch[EVENT_AUGMENT_REMOVE] = handle_item_augment_remove; - - mImpl->SpellArgumentDispatch[EVENT_SPELL_EFFECT_CLIENT] = handle_spell_effect; - mImpl->SpellArgumentDispatch[EVENT_SPELL_BUFF_TIC_CLIENT] = handle_spell_tic; - mImpl->SpellArgumentDispatch[EVENT_SPELL_FADE] = handle_spell_fade; - mImpl->SpellArgumentDispatch[EVENT_SPELL_EFFECT_TRANSLOCATE_COMPLETE] = handle_translocate_finish; - - mImpl->EncounterArgumentDispatch[EVENT_TIMER] = handle_encounter_timer; - mImpl->EncounterArgumentDispatch[EVENT_ENCOUNTER_LOAD] = handle_encounter_load; - mImpl->EncounterArgumentDispatch[EVENT_ENCOUNTER_UNLOAD] = handle_encounter_unload; - - mImpl->state = nullptr; -} - -LuaParser::~LuaParser() { - // valgrind didn't like when we didn't clean these up :P - lua_encounters.clear(); - lua_encounter_events_registered.clear(); - lua_encounters_loaded.clear(); - if(mImpl->state) { - lua_close(mImpl->state); - } -} - -int LuaParser::EventNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!npc) { - return 0; - } - - if(!HasQuestSub(npc->GetNPCTypeID(), evt)) { - return 0; - } - - std::string package_name = "npc_" + std::to_string(npc->GetNPCTypeID()); - return _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers); -} - -int LuaParser::EventGlobalNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!npc) { - return 0; - } - - if(!HasGlobalQuestSub(evt)) { - return 0; - } - - return _EventNPC("global_npc", evt, npc, init, data, extra_data, extra_pointers); -} - -int LuaParser::_EventNPC(std::string package_name, QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func) { - const char *sub_name = LuaEvents[evt]; - - int start = lua_gettop(mImpl->state); - - try { - int npop = 1; - if(l_func != nullptr) { - l_func->push(mImpl->state); - } else { - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, sub_name); - npop = 2; - } - - lua_createtable(mImpl->state, 0, 0); - //always push self - Lua_NPC l_npc(npc); - luabind::adl::object l_npc_o = luabind::adl::object(mImpl->state, l_npc); - l_npc_o.push(mImpl->state); - lua_setfield(mImpl->state, -2, "self"); - - auto arg_function = mImpl->NPCArgumentDispatch[evt]; - arg_function(this, mImpl->state, npc, init, data, extra_data, extra_pointers); - Client *c = (init && init->IsClient()) ? init->CastToClient() : nullptr; - - quest_manager.StartQuest(npc, c, nullptr); - if(lua_pcall(mImpl->state, 1, 1, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - quest_manager.EndQuest(); - return 0; - } - quest_manager.EndQuest(); - - if(lua_isnumber(mImpl->state, -1)) { - int ret = static_cast(lua_tointeger(mImpl->state, -1)); - lua_pop(mImpl->state, npop); - return ret; - } - - lua_pop(mImpl->state, npop); - } catch(std::exception &ex) { - std::string error = "Lua Exception: "; - error += std::string(ex.what()); - AddError(error); - - //Restore our stack to the best of our ability - int end = lua_gettop(mImpl->state); - int n = end - start; - if(n > 0) { - lua_pop(mImpl->state, n); - } - } - - return 0; -} - -int LuaParser::EventPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!client) { - return 0; - } - - if(!PlayerHasQuestSub(evt)) { - return 0; - } - - return _EventPlayer("player", evt, client, data, extra_data, extra_pointers); -} - -int LuaParser::EventGlobalPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!client) { - return 0; - } - - if(!GlobalPlayerHasQuestSub(evt)) { - return 0; - } - - return _EventPlayer("global_player", evt, client, data, extra_data, extra_pointers); -} - -int LuaParser::_EventPlayer(std::string package_name, QuestEventID evt, Client *client, std::string data, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func) { - const char *sub_name = LuaEvents[evt]; - int start = lua_gettop(mImpl->state); - - try { - int npop = 1; - if(l_func != nullptr) { - l_func->push(mImpl->state); - } else { - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, sub_name); - npop = 2; - } - - lua_createtable(mImpl->state, 0, 0); - //push self - Lua_Client l_client(client); - luabind::adl::object l_client_o = luabind::adl::object(mImpl->state, l_client); - l_client_o.push(mImpl->state); - lua_setfield(mImpl->state, -2, "self"); - - auto arg_function = mImpl->PlayerArgumentDispatch[evt]; - arg_function(this, mImpl->state, client, data, extra_data, extra_pointers); - - quest_manager.StartQuest(client, client, nullptr); - if(lua_pcall(mImpl->state, 1, 1, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - quest_manager.EndQuest(); - return 0; - } - quest_manager.EndQuest(); - - if(lua_isnumber(mImpl->state, -1)) { - int ret = static_cast(lua_tointeger(mImpl->state, -1)); - lua_pop(mImpl->state, npop); - return ret; - } - - lua_pop(mImpl->state, npop); - } catch(std::exception &ex) { - std::string error = "Lua Exception: "; - error += std::string(ex.what()); - AddError(error); - - //Restore our stack to the best of our ability - int end = lua_gettop(mImpl->state); - int n = end - start; - if(n > 0) { - lua_pop(mImpl->state, n); - } - } - - return 0; -} - -int LuaParser::EventItem(QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!item) { - return 0; - } - - if(!ItemHasQuestSub(item, evt)) { - return 0; - } - - std::string package_name = "item_"; - package_name += std::to_string(item->GetID()); - return _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers); -} - -int LuaParser::_EventItem(std::string package_name, QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, - std::string data, uint32 extra_data, std::vector *extra_pointers, luabind::adl::object *l_func) { - const char *sub_name = LuaEvents[evt]; - - int start = lua_gettop(mImpl->state); - - try { - int npop = 1; - if(l_func != nullptr) { - l_func->push(mImpl->state); - } else { - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, sub_name); - } - - lua_createtable(mImpl->state, 0, 0); - //always push self - Lua_ItemInst l_item(item); - luabind::adl::object l_item_o = luabind::adl::object(mImpl->state, l_item); - l_item_o.push(mImpl->state); - lua_setfield(mImpl->state, -2, "self"); - - Lua_Client l_client(client); - luabind::adl::object l_client_o = luabind::adl::object(mImpl->state, l_client); - l_client_o.push(mImpl->state); - lua_setfield(mImpl->state, -2, "owner"); - - //redo this arg function - auto arg_function = mImpl->ItemArgumentDispatch[evt]; - arg_function(this, mImpl->state, client, item, mob, data, extra_data, extra_pointers); - - quest_manager.StartQuest(client, client, item); - if(lua_pcall(mImpl->state, 1, 1, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - quest_manager.EndQuest(); - return 0; - } - quest_manager.EndQuest(); - - if(lua_isnumber(mImpl->state, -1)) { - int ret = static_cast(lua_tointeger(mImpl->state, -1)); - lua_pop(mImpl->state, npop); - return ret; - } - - lua_pop(mImpl->state, npop); - } catch(std::exception &ex) { - std::string error = "Lua Exception: "; - error += std::string(ex.what()); - AddError(error); - - //Restore our stack to the best of our ability - int end = lua_gettop(mImpl->state); - int n = end - start; - if(n > 0) { - lua_pop(mImpl->state, n); - } - } - - return 0; -} - -int LuaParser::EventSpell(QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - std::string package_name = "spell_" + std::to_string(spell_id); - - if(!SpellHasQuestSub(spell_id, evt)) { - return 0; - } - - return _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers); -} - -int LuaParser::_EventSpell(std::string package_name, QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, - std::vector *extra_pointers, luabind::adl::object *l_func) { - const char *sub_name = LuaEvents[evt]; - - int start = lua_gettop(mImpl->state); - - try { - int npop = 1; - if(l_func != nullptr) { - l_func->push(mImpl->state); - } else { - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, sub_name); - npop = 2; - } - - lua_createtable(mImpl->state, 0, 0); - - //always push self even if invalid - if(IsValidSpell(spell_id)) { - Lua_Spell l_spell(&spells[spell_id]); - luabind::adl::object l_spell_o = luabind::adl::object(mImpl->state, l_spell); - l_spell_o.push(mImpl->state); - } else { - Lua_Spell l_spell(nullptr); - luabind::adl::object l_spell_o = luabind::adl::object(mImpl->state, l_spell); - l_spell_o.push(mImpl->state); - } - lua_setfield(mImpl->state, -2, "self"); - - auto arg_function = mImpl->SpellArgumentDispatch[evt]; - arg_function(this, mImpl->state, npc, client, spell_id, extra_data, extra_pointers); - - quest_manager.StartQuest(npc, client, nullptr); - if(lua_pcall(mImpl->state, 1, 1, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - quest_manager.EndQuest(); - return 0; - } - quest_manager.EndQuest(); - - if(lua_isnumber(mImpl->state, -1)) { - int ret = static_cast(lua_tointeger(mImpl->state, -1)); - lua_pop(mImpl->state, npop); - return ret; - } - - lua_pop(mImpl->state, npop); - } catch(std::exception &ex) { - std::string error = "Lua Exception: "; - error += std::string(ex.what()); - AddError(error); - - //Restore our stack to the best of our ability - int end = lua_gettop(mImpl->state); - int n = end - start; - if(n > 0) { - lua_pop(mImpl->state, n); - } - } - - return 0; -} - -int LuaParser::EventEncounter(QuestEventID evt, std::string encounter_name, std::string data, uint32 extra_data, std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - std::string package_name = "encounter_" + encounter_name; - - if(!EncounterHasQuestSub(encounter_name, evt)) { - return 0; - } - - return _EventEncounter(package_name, evt, encounter_name, data, extra_data, extra_pointers); -} - -int LuaParser::_EventEncounter(std::string package_name, QuestEventID evt, std::string encounter_name, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - const char *sub_name = LuaEvents[evt]; - - int start = lua_gettop(mImpl->state); - - try { - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, sub_name); - - lua_createtable(mImpl->state, 0, 0); - lua_pushstring(mImpl->state, encounter_name.c_str()); - lua_setfield(mImpl->state, -2, "name"); - - Encounter *enc = lua_encounters[encounter_name]; - - auto arg_function = mImpl->EncounterArgumentDispatch[evt]; - arg_function(this, mImpl->state, enc, data, extra_data, extra_pointers); - - quest_manager.StartQuest(enc, nullptr, nullptr, encounter_name); - if(lua_pcall(mImpl->state, 1, 1, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - quest_manager.EndQuest(); - return 0; - } - quest_manager.EndQuest(); - - if(lua_isnumber(mImpl->state, -1)) { - int ret = static_cast(lua_tointeger(mImpl->state, -1)); - lua_pop(mImpl->state, 2); - return ret; - } - - lua_pop(mImpl->state, 2); - } catch(std::exception &ex) { - std::string error = "Lua Exception: "; - error += std::string(ex.what()); - AddError(error); - - //Restore our stack to the best of our ability - int end = lua_gettop(mImpl->state); - int n = end - start; - if(n > 0) { - lua_pop(mImpl->state, n); - } - } - - return 0; -} - -bool LuaParser::HasQuestSub(uint32 npc_id, QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - std::string package_name = "npc_" + std::to_string(npc_id); - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, package_name); -} - -bool LuaParser::HasGlobalQuestSub(QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, "global_npc"); -} - -bool LuaParser::PlayerHasQuestSub(QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, "player"); -} - -bool LuaParser::GlobalPlayerHasQuestSub(QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, "global_player"); -} - -bool LuaParser::SpellHasQuestSub(uint32 spell_id, QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - std::string package_name = "spell_" + std::to_string(spell_id); - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, package_name); -} - -bool LuaParser::ItemHasQuestSub(EQEmu::ItemInstance *itm, QuestEventID evt) { - if (itm == nullptr) { - return false; - } - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - std::string package_name = "item_"; - package_name += std::to_string(itm->GetID()); - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, package_name); -} - -bool LuaParser::EncounterHasQuestSub(std::string encounter_name, QuestEventID evt) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return false; - } - - std::string package_name = "encounter_" + encounter_name; - - const char *subname = LuaEvents[evt]; - return HasFunction(subname, package_name); -} - -void LuaParser::LoadNPCScript(std::string filename, int npc_id) { - std::string package_name = "npc_" + std::to_string(npc_id); - - LoadScript(filename, package_name); -} - -void LuaParser::LoadGlobalNPCScript(std::string filename) { - LoadScript(filename, "global_npc"); -} - -void LuaParser::LoadPlayerScript(std::string filename) { - LoadScript(filename, "player"); -} - -void LuaParser::LoadGlobalPlayerScript(std::string filename) { - LoadScript(filename, "global_player"); -} - -void LuaParser::LoadItemScript(std::string filename, EQEmu::ItemInstance *item) { - if (item == nullptr) - return; - std::string package_name = "item_"; - package_name += std::to_string(item->GetID()); - - LoadScript(filename, package_name); -} - -void LuaParser::LoadSpellScript(std::string filename, uint32 spell_id) { - std::string package_name = "spell_" + std::to_string(spell_id); - - LoadScript(filename, package_name); -} - -void LuaParser::LoadEncounterScript(std::string filename, std::string encounter_name) { - std::string package_name = "encounter_" + encounter_name; - - LoadScript(filename, package_name); -} - -void LuaParser::AddVar(std::string name, std::string val) { - mImpl->vars[name] = val; -} - -std::string LuaParser::GetVar(std::string name) { - auto iter = mImpl->vars.find(name); - if(iter != mImpl->vars.end()) { - return iter->second; - } - - return std::string(); -} - -void LuaParser::Init() { - ReloadQuests(); -} - -void LuaParser::ReloadQuests() { - mImpl->loaded.clear(); - errors_.clear(); - mImpl->mods.clear(); - lua_encounter_events_registered.clear(); - lua_encounters_loaded.clear(); - - for (auto encounter : lua_encounters) { - encounter.second->Depop(); - } - - lua_encounters.clear(); - // so the Depop function above depends on the Process being called again so ... - // And there is situations where it wouldn't be :P - entity_list.EncounterProcess(); - - if(mImpl->state) { - lua_close(mImpl->state); - } - - mImpl->state = luaL_newstate(); - luaL_openlibs(mImpl->state); - - auto top = lua_gettop(mImpl->state); - - if(luaopen_bit(mImpl->state) != 1) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - } - - if(luaL_dostring(mImpl->state, "math.randomseed(os.time())")) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - } - -#ifdef SANITIZE_LUA_LIBS - //io - lua_pushnil(mImpl->state); - //lua_setglobal(mImpl->state, "io"); - - //some os/debug are okay some are not - lua_getglobal(mImpl->state, "os"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "exit"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "execute"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "getenv"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "remove"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "rename"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "setlocale"); - lua_pushnil(mImpl->state); - lua_setfield(mImpl->state, -2, "tmpname"); - lua_pop(mImpl->state, 1); - - lua_pushnil(mImpl->state); - lua_setglobal(mImpl->state, "collectgarbage"); - - lua_pushnil(mImpl->state); - lua_setglobal(mImpl->state, "loadfile"); - -#endif - - // lua 5.2+ defines these -#if defined(LUA_VERSION_MAJOR) && defined(LUA_VERSION_MINOR) - const char lua_version[] = LUA_VERSION_MAJOR "." LUA_VERSION_MINOR; -#elif LUA_VERSION_NUM == 501 - const char lua_version[] = "5.1"; -#else -#error Incompatible lua version -#endif - -#ifdef WINDOWS - const char libext[] = ".dll"; -#else - // lua doesn't care OSX doesn't use sonames - const char libext[] = ".so"; -#endif - - lua_getglobal(mImpl->state, "package"); - lua_getfield(mImpl->state, -1, "path"); - std::string module_path = lua_tostring(mImpl->state,-1); - module_path += ";./" + Config->LuaModuleDir + "?.lua;./" + Config->LuaModuleDir + "?/init.lua"; - // luarock paths using lua_modules as tree - // to path it adds foo/share/lua/5.1/?.lua and foo/share/lua/5.1/?/init.lua - module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?.lua"; - module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?/init.lua"; - lua_pop(mImpl->state, 1); - lua_pushstring(mImpl->state, module_path.c_str()); - lua_setfield(mImpl->state, -2, "path"); - lua_pop(mImpl->state, 1); - - lua_getglobal(mImpl->state, "package"); - lua_getfield(mImpl->state, -1, "cpath"); - module_path = lua_tostring(mImpl->state, -1); - module_path += ";./" + Config->LuaModuleDir + "?" + libext; - // luarock paths using lua_modules as tree - // luarocks adds foo/lib/lua/5.1/?.so for cpath - module_path += ";./" + Config->LuaModuleDir + "lib/lua/" + lua_version + "/?" + libext; - lua_pop(mImpl->state, 1); - lua_pushstring(mImpl->state, module_path.c_str()); - lua_setfield(mImpl->state, -2, "cpath"); - lua_pop(mImpl->state, 1); - - MapFunctions(mImpl->state); - - //load init - std::string path = Config->QuestDir; - path += "/"; - path += QUEST_GLOBAL_DIRECTORY; - path += "/script_init.lua"; - - FILE *f = fopen(path.c_str(), "r"); - if(f) { - fclose(f); - - if(luaL_dofile(mImpl->state, path.c_str())) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - } - } - - //zone init - always loads after global - if(zone) { - std::string zone_script = Config->QuestDir; - zone_script += "/"; - zone_script += zone->GetShortName(); - zone_script += "/script_init_v"; - zone_script += std::to_string(zone->GetInstanceVersion()); - zone_script += ".lua"; - f = fopen(zone_script.c_str(), "r"); - if(f) { - fclose(f); - - if(luaL_dofile(mImpl->state, zone_script.c_str())) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - } - } - else { - zone_script = Config->QuestDir; - zone_script += "/"; - zone_script += zone->GetShortName(); - zone_script += "/script_init.lua"; - f = fopen(zone_script.c_str(), "r"); - if (f) { - fclose(f); - - if (luaL_dofile(mImpl->state, zone_script.c_str())) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - } - } - } - } - - FILE *load_order = fopen("mods/load_order.txt", "r"); - if (load_order) { - char file_name[256] = { 0 }; - while (fgets(file_name, 256, load_order) != nullptr) { - for (int i = 0; i < 256; ++i) { - auto c = file_name[i]; - if (c == '\n' || c == '\r' || c == ' ') { - file_name[i] = 0; - break; - } - } - - LoadScript("mods/" + std::string(file_name), file_name); - mImpl->mods.push_back(LuaMod(mImpl->state, this, file_name)); - } - - fclose(load_order); - } - - auto end = lua_gettop(mImpl->state); - int n = end - top; - if (n > 0) { - lua_pop(mImpl->state, n); - } -} - -void LuaParser::LoadScript(std::string filename, std::string package_name) { - auto iter = mImpl->loaded.find(package_name); - if(iter != mImpl->loaded.end()) { - return; - } - - auto top = lua_gettop(mImpl->state); - if(luaL_loadfile(mImpl->state, filename.c_str())) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - lua_pop(mImpl->state, 1); - return; - } - - //This makes an env table named: package_name - //And makes it so we can see the global table _G from it - //Then sets it so this script is called from that table as an env - - lua_createtable(mImpl->state, 0, 0); // anon table - lua_getglobal(mImpl->state, "_G"); // get _G - lua_setfield(mImpl->state, -2, "__index"); //anon table.__index = _G - - lua_pushvalue(mImpl->state, -1); //copy table to top of stack - lua_setmetatable(mImpl->state, -2); //setmetatable(anon_table, copied table) - - lua_pushvalue(mImpl->state, -1); //put the table we made into the registry - lua_setfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - - lua_setfenv(mImpl->state, -2); //set the env to the table we made - - if(lua_pcall(mImpl->state, 0, 0, 0)) { - std::string error = lua_tostring(mImpl->state, -1); - AddError(error); - lua_pop(mImpl->state, 1); - } - else { - mImpl->loaded[package_name] = true; - } - - auto end = lua_gettop(mImpl->state); - int n = end - top; - if (n > 0) { - lua_pop(mImpl->state, n); - } -} - -bool LuaParser::HasFunction(std::string subname, std::string package_name) { - //std::transform(subname.begin(), subname.end(), subname.begin(), ::tolower); - - auto iter = mImpl->loaded.find(package_name); - if(iter == mImpl->loaded.end()) { - return false; - } - - lua_getfield(mImpl->state, LUA_REGISTRYINDEX, package_name.c_str()); - lua_getfield(mImpl->state, -1, subname.c_str()); - - if(lua_isfunction(mImpl->state, -1)) { - lua_pop(mImpl->state, 2); - return true; - } - - lua_pop(mImpl->state, 2); - return false; -} - -void LuaParser::MapFunctions(lua_State *L) { - - try { - luabind::open(L); - - luabind::module(L) - [ - lua_register_general(), - lua_register_random(), - lua_register_events(), - lua_register_faction(), - lua_register_slot(), - lua_register_material(), - lua_register_client_version(), - lua_register_appearance(), - lua_register_classes(), - lua_register_skills(), - lua_register_bodytypes(), - lua_register_filters(), - lua_register_message_types(), - lua_register_entity(), - lua_register_encounter(), - lua_register_mob(), - lua_register_special_abilities(), - lua_register_npc(), - lua_register_client(), - lua_register_inventory(), - lua_register_inventory_where(), - lua_register_iteminst(), - lua_register_item(), - lua_register_spell(), - lua_register_spawn(), - lua_register_hate_entry(), - lua_register_hate_list(), - lua_register_entity_list(), - lua_register_mob_list(), - lua_register_client_list(), - lua_register_npc_list(), - lua_register_corpse_list(), - lua_register_object_list(), - lua_register_door_list(), - lua_register_spawn_list(), - lua_register_group(), - lua_register_raid(), - lua_register_corpse(), - lua_register_door(), - lua_register_object(), - lua_register_packet(), - lua_register_packet_opcodes(), - lua_register_stat_bonuses(), - lua_register_rules_const(), - lua_register_rulei(), - lua_register_ruler(), - lua_register_ruleb() - ]; - - } catch(std::exception &ex) { - std::string error = ex.what(); - AddError(error); - } -} - -int LuaParser::DispatchEventNPC(QuestEventID evt, NPC* npc, Mob *init, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!npc) - return 0; - - std::string package_name = "npc_" + std::to_string(npc->GetNPCTypeID()); - int ret = 0; - - auto iter = lua_encounter_events_registered.find(package_name); - if(iter != lua_encounter_events_registered.end()) { - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - } - - iter = lua_encounter_events_registered.find("npc_-1"); - if(iter == lua_encounter_events_registered.end()) { - return ret; - } - - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventNPC(package_name, evt, npc, init, data, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - - return ret; -} - -int LuaParser::DispatchEventPlayer(QuestEventID evt, Client *client, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - std::string package_name = "player"; - - auto iter = lua_encounter_events_registered.find(package_name); - if(iter == lua_encounter_events_registered.end()) { - return 0; - } - - int ret = 0; - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventPlayer(package_name, evt, client, data, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - - return ret; -} - -int LuaParser::DispatchEventItem(QuestEventID evt, Client *client, EQEmu::ItemInstance *item, Mob *mob, std::string data, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - if(!item) - return 0; - - std::string package_name = "item_"; - package_name += std::to_string(item->GetID()); - int ret = 0; - - auto iter = lua_encounter_events_registered.find(package_name); - if(iter != lua_encounter_events_registered.end()) { - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - } - - iter = lua_encounter_events_registered.find("item_-1"); - if(iter == lua_encounter_events_registered.end()) { - return ret; - } - - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventItem(package_name, evt, client, item, mob, data, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - return ret; -} - -int LuaParser::DispatchEventSpell(QuestEventID evt, NPC* npc, Client *client, uint32 spell_id, uint32 extra_data, - std::vector *extra_pointers) { - evt = ConvertLuaEvent(evt); - if(evt >= _LargestEventID) { - return 0; - } - - std::string package_name = "spell_" + std::to_string(spell_id); - - int ret = 0; - auto iter = lua_encounter_events_registered.find(package_name); - if(iter != lua_encounter_events_registered.end()) { - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) { - ret = i; - } - } - ++riter; - } - } - - iter = lua_encounter_events_registered.find("spell_-1"); - if(iter == lua_encounter_events_registered.end()) { - return ret; - } - - auto riter = iter->second.begin(); - while(riter != iter->second.end()) { - if(riter->event_id == evt) { - std::string package_name = "encounter_" + riter->encounter_name; - int i = _EventSpell(package_name, evt, npc, client, spell_id, extra_data, extra_pointers, &riter->lua_reference); - if(i != 0) - ret = i; - } - ++riter; - } - return ret; -} - -QuestEventID LuaParser::ConvertLuaEvent(QuestEventID evt) { - switch(evt) { - case EVENT_SLAY: - case EVENT_NPC_SLAY: - return EVENT_SLAY; - break; - case EVENT_SPELL_EFFECT_CLIENT: - case EVENT_SPELL_EFFECT_NPC: - return EVENT_SPELL_EFFECT_CLIENT; - break; - case EVENT_SPELL_BUFF_TIC_CLIENT: - case EVENT_SPELL_BUFF_TIC_NPC: - return EVENT_SPELL_BUFF_TIC_CLIENT; - break; - case EVENT_AGGRO: - case EVENT_ATTACK: - return _LargestEventID; - break; - default: - return evt; - } -} - -#endif - -void LuaParser::MeleeMitigation(Mob *self, Mob *attacker, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) -{ - for (auto &mod : mImpl->mods) { - mod.MeleeMitigation(self, attacker, hit, opts, ignoreDefault); - } -} - -void LuaParser::ApplyDamageTable(Mob *self, DamageHitInfo &hit, bool &ignoreDefault) -{ - for (auto &mod : mImpl->mods) { - mod.ApplyDamageTable(self, hit, ignoreDefault); - } -} - -bool LuaParser::AvoidDamage(Mob *self, Mob *other, DamageHitInfo &hit, bool & ignoreDefault) -{ - bool retval = false; - for (auto &mod : mImpl->mods) { - mod.AvoidDamage(self, other, hit, retval, ignoreDefault); - } - return retval; -} - -bool LuaParser::CheckHitChance(Mob *self, Mob *other, DamageHitInfo &hit, bool &ignoreDefault) -{ - bool retval = false; - for (auto &mod : mImpl->mods) { - mod.CheckHitChance(self, other, hit, retval, ignoreDefault); - } - return retval; -} - -void LuaParser::TryCriticalHit(Mob *self, Mob *defender, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) -{ - for (auto &mod : mImpl->mods) { - mod.TryCriticalHit(self, defender, hit, opts, ignoreDefault); - } -} - -void LuaParser::CommonOutgoingHitSuccess(Mob *self, Mob *other, DamageHitInfo &hit, ExtraAttackOptions *opts, bool &ignoreDefault) -{ - for (auto &mod : mImpl->mods) { - mod.CommonOutgoingHitSuccess(self, other, hit, opts, ignoreDefault); - } -} - -uint32 LuaParser::GetRequiredAAExperience(Client *self, bool &ignoreDefault) -{ - uint32 retval = 0; - for (auto &mod : mImpl->mods) { - mod.GetRequiredAAExperience(self, retval, ignoreDefault); - } - return retval; -} - -uint32 LuaParser::GetEXPForLevel(Client *self, uint16 level, bool &ignoreDefault) -{ - uint32 retval = 0; - for (auto &mod : mImpl->mods) { - mod.GetEXPForLevel(self, level, retval, ignoreDefault); - } - return retval; -} - -uint32 LuaParser::GetExperienceForKill(Client *self, Mob *against, bool &ignoreDefault) -{ - uint32 retval = 0; - for (auto &mod : mImpl->mods) { - mod.GetExperienceForKill(self, against, retval, ignoreDefault); - } - return retval; -} diff --git a/zone/lua_ptr.h b/zone/lua_ptr.h deleted file mode 100644 index d337059ae..000000000 --- a/zone/lua_ptr.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef EQEMU_LUA_PTR_H -#define EQEMU_LUA_PTR_H -#ifdef LUA_EQEMU - -#ifndef EQEMU_UNSAFE_LUA -#define Lua_Safe_Call_Void() if(!d_) { return; } NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Bool() if(!d_) { return false; } NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Int() if(!d_) { return 0; } NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Real() if(!d_) { return 0.0; } NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_String() if(!d_) { return ""; } NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Class(type) if(!d_) { return type(); } NativeType *self = reinterpret_cast(d_) -#else -#define Lua_Safe_Call_Void() NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Bool() NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Int() NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Real() NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_String() NativeType *self = reinterpret_cast(d_) -#define Lua_Safe_Call_Class(type) NativeType *self = reinterpret_cast(d_) -#endif - -template -class Lua_Ptr -{ -public: - Lua_Ptr() { - } - - Lua_Ptr(T *d) : d_(d) { - } - - ~Lua_Ptr() { - } - - T *GetLuaPtrData() { - return d_; - } - - void SetLuaPtrData(T *d) { - d_ = d; - } - - bool Null() { - return d_ == nullptr; - } - - bool Valid() { - return d_ != nullptr; - } - -protected: - T *d_; -}; - -#endif -#endif \ No newline at end of file