From 2ed348f23daa9e68871ae3f531bf242d518eebbb Mon Sep 17 00:00:00 2001 From: KimLS Date: Sat, 9 Jan 2016 01:39:23 -0800 Subject: [PATCH] Adding crazy behavior interface. --- zone/CMakeLists.txt | 2 + zone/client_process.cpp | 4 +- zone/lua_behavior_interface.cpp | 166 ++++++++++++++++++++++++++++++++ zone/lua_behavior_interface.h | 25 +++++ zone/lua_parser.h | 1 - zone/merc.cpp | 2 +- zone/npc.cpp | 2 +- 7 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 zone/lua_behavior_interface.cpp create mode 100644 zone/lua_behavior_interface.h diff --git a/zone/CMakeLists.txt b/zone/CMakeLists.txt index ba9aa8620..525d4726c 100644 --- a/zone/CMakeLists.txt +++ b/zone/CMakeLists.txt @@ -33,6 +33,7 @@ SET(zone_sources horse.cpp inventory.cpp loottables.cpp + lua_behavior_interface.cpp lua_bit.cpp lua_corpse.cpp lua_client.cpp @@ -150,6 +151,7 @@ SET(zone_headers guild_mgr.h hate_list.h horse.h + lua_behavior_interface.h lua_bit.h lua_client.h lua_corpse.h diff --git a/zone/client_process.cpp b/zone/client_process.cpp index 3b973e7b7..5bd61d344 100644 --- a/zone/client_process.cpp +++ b/zone/client_process.cpp @@ -236,8 +236,8 @@ bool Client::Process() { CheckMercSuspendTimer(); } - if(IsAIControlled()) - AI_Process(); + //if(IsAIControlled()) + //AI_Process(); // Don't reset the bindwound timer so we can check it in BindWound as well. if (bindwound_timer.Check(false) && bindwound_target != 0) { diff --git a/zone/lua_behavior_interface.cpp b/zone/lua_behavior_interface.cpp new file mode 100644 index 000000000..83cc14999 --- /dev/null +++ b/zone/lua_behavior_interface.cpp @@ -0,0 +1,166 @@ +#ifdef LUA_EQEMU +#include "lua_behavior_interface.h" + +#include +#include +#include + +#include "masterentity.h" +#include "../common/spdat.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" + +LuaBehaviorInterface::LuaBehaviorInterface() +{ + L = nullptr; +} + +LuaBehaviorInterface::~LuaBehaviorInterface() +{ + if (L) { + lua_close(L); + } +} + +bool LuaBehaviorInterface::Init() +{ + if (L) { + return false; + } + + L = luaL_newstate(); + luaL_openlibs(L); + + if (luaopen_bit(L) != 1) { + return false; + } + + if (luaL_dostring(L, "math.randomseed(os.time())")) { + return false; + } + +#ifdef SANITIZE_LUA_LIBS + lua_pushnil(L); + lua_setglobal(L, "io"); + + lua_getglobal(L, "os"); + lua_pushnil(L); + lua_setfield(L, -2, "exit"); + lua_pushnil(L); + lua_setfield(L, -2, "execute"); + lua_pushnil(L); + lua_setfield(L, -2, "getenv"); + lua_pushnil(L); + lua_setfield(L, -2, "remove"); + lua_pushnil(L); + lua_setfield(L, -2, "rename"); + lua_pushnil(L); + lua_setfield(L, -2, "setlocale"); + lua_pushnil(L); + lua_setfield(L, -2, "tmpname"); + lua_pop(L, 1); + + lua_pushnil(L); + lua_setglobal(L, "collectgarbage"); + + lua_pushnil(L); + lua_setglobal(L, "loadfile"); +#endif + + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + std::string module_path = lua_tostring(L, -1); + module_path += ";./lua_modules/?.lua"; + lua_pop(L, 1); + lua_pushstring(L, module_path.c_str()); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); + + if (!MapFunctions()) { + return false; + } + + std::string path = "ai/script_init.lua"; + + FILE *f = fopen(path.c_str(), "r"); + if (f) { + fclose(f); + + if (luaL_dofile(L, path.c_str())) { + return false; + } + } + + return true; +} + +bool LuaBehaviorInterface::MapFunctions() +{ + try { + luabind::open(L); + + luabind::module(L) + [ + lua_register_general(), + lua_register_events(), + lua_register_faction(), + lua_register_slot(), + lua_register_material(), + lua_register_client_version(), + lua_register_appearance(), + lua_register_entity(), + 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() + ]; + + } + catch (std::exception &ex) { + return false; + } + + return true; +} + +#endif diff --git a/zone/lua_behavior_interface.h b/zone/lua_behavior_interface.h new file mode 100644 index 000000000..73aec920a --- /dev/null +++ b/zone/lua_behavior_interface.h @@ -0,0 +1,25 @@ +#ifndef EQEMU_LUA_BEHAVIOR_INTERFACE_H +#define EQEMU_LUA_BEHAVIOR_INTERFACE_H +#ifdef LUA_EQEMU + +namespace luabind { + namespace adl { + class object; + } +} + +struct lua_State; + +class LuaBehaviorInterface +{ +public: + LuaBehaviorInterface(); + ~LuaBehaviorInterface(); + bool Init(); +private: + bool MapFunctions(); + lua_State *L; +}; + +#endif +#endif diff --git a/zone/lua_parser.h b/zone/lua_parser.h index 63d9facfe..754554c41 100644 --- a/zone/lua_parser.h +++ b/zone/lua_parser.h @@ -87,7 +87,6 @@ private: void LoadScript(std::string filename, std::string package_name); bool HasFunction(std::string function, std::string package_name); - void ClearStates(); void MapFunctions(lua_State *L); QuestEventID ConvertLuaEvent(QuestEventID evt); diff --git a/zone/merc.cpp b/zone/merc.cpp index b4125735e..30bac4703 100644 --- a/zone/merc.cpp +++ b/zone/merc.cpp @@ -1304,7 +1304,7 @@ bool Merc::Process() return true; // Merc AI - AI_Process(); + //AI_Process(); return true; } diff --git a/zone/npc.cpp b/zone/npc.cpp index fd7f6880c..381b494e2 100644 --- a/zone/npc.cpp +++ b/zone/npc.cpp @@ -734,7 +734,7 @@ bool NPC::Process() } } - AI_Process(); + //AI_Process(); return true; }