From dbc83dae37dff1338e1ee6358a0a8d10103278b0 Mon Sep 17 00:00:00 2001 From: David Athay Date: Wed, 22 Oct 2008 13:30:46 +0000 Subject: Added enabling and disabling NPCs. --- src/game-server/npc.cpp | 13 +++++++++---- src/game-server/npc.hpp | 6 ++++++ src/scripting/lua.cpp | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp index fd462c50..ce344a52 100644 --- a/src/game-server/npc.cpp +++ b/src/game-server/npc.cpp @@ -25,14 +25,19 @@ #include "scripting/script.hpp" NPC::NPC(const std::string &name, int id, Script *s): - Being(OBJECT_NPC, 65535), mScript(s), mID(id) + Being(OBJECT_NPC, 65535), mScript(s), mID(id), mEnabled(true) { setName(name); } +void NPC::enable(bool enabled) +{ + mEnabled = enabled; +} + void NPC::update() { - if (!mScript) return; + if (!mScript || !mEnabled) return; mScript->prepare("npc_update"); mScript->push(this); mScript->execute(); @@ -40,7 +45,7 @@ void NPC::update() void NPC::prompt(Character *ch, bool restart) { - if (!mScript) return; + if (!mScript || !mEnabled) return; mScript->prepare(restart ? "npc_start" : "npc_next"); mScript->push(this); mScript->push(ch); @@ -49,7 +54,7 @@ void NPC::prompt(Character *ch, bool restart) void NPC::select(Character *ch, int v) { - if (!mScript) return; + if (!mScript || !mEnabled) return; mScript->prepare("npc_choose"); mScript->push(this); mScript->push(ch); diff --git a/src/game-server/npc.hpp b/src/game-server/npc.hpp index c4d8b384..cfcfa54a 100644 --- a/src/game-server/npc.hpp +++ b/src/game-server/npc.hpp @@ -38,6 +38,11 @@ class NPC : public Being void update(); + /** + * Enables the NPC + */ + void enable(bool enabled); + /** * Prompts NPC. */ @@ -71,6 +76,7 @@ class NPC : public Being private: Script *mScript; /**< Script describing NPC behavior. */ unsigned short mID; /**< ID of the NPC. */ + bool mEnabled; /**< Whether NPC is enabled */ }; #endif diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 09ac134e..846a43a1 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -216,6 +216,37 @@ static int LuaNpc_Create(lua_State *s) return 1; } +/** + * Enable a NPC if it has previously disabled + */ +static int LuaNPC_Enable(lua_State *s) +{ + NPC *p = getNPC(s, 1); + if (p) + { + p->enable(true); + bool b = GameState::insert(p); + assert(b); (void)b; + } + + return 0; +} + +/** + * Disable a NPC + */ +static int LuaNPC_Disable(lua_State *s) +{ + NPC *p = getNPC(s, 1); + if (p) + { + p->enable(false); + GameState::remove(p); + } + + return 0; +} + /** * Callback for warping a player to another place. * tmw.chr_warp(character, nil/int map, int x, int y) @@ -468,7 +499,6 @@ static int LuaBeing_Damage(lua_State *s) return 0; } - /** * Function for getting the x-coordinate of the position of a being */ @@ -781,6 +811,8 @@ LuaScript::LuaScript(): { "npc_message", &LuaNpc_Message }, { "npc_choice", &LuaNpc_Choice }, { "npc_trade", &LuaNpc_Trade }, + { "npc_enable", &LuaNPC_Enable }, + { "npc_disable", &LuaNPC_Disable }, { "chr_warp", &LuaChr_Warp }, { "chr_inv_change", &LuaChr_InvChange }, { "chr_inv_count", &LuaChr_InvCount }, -- cgit v1.2.3-60-g2f50