From 923ea3f4b0a2c435399dd967d4736e2ec626344f Mon Sep 17 00:00:00 2001 From: Warmist Date: Sun, 7 Oct 2012 20:44:18 +0300 Subject: [PATCH] Reactionhooks more usefull and gm-editor minor tweaks (e.g. search in containers) --- library/include/LuaTools.h | 15 +++++++++++++++ plugins/reactionhooks.cpp | 9 ++++++--- scripts/gui/gm-editor.lua | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/library/include/LuaTools.h b/library/include/LuaTools.h index 3330e23e7..e956a65d5 100644 --- a/library/include/LuaTools.h +++ b/library/include/LuaTools.h @@ -472,3 +472,18 @@ namespace DFHack {namespace Lua { name##_event.invoke(out, 5); \ } \ } + +#define DEFINE_LUA_EVENT_6(name, handler, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5,arg_type6) \ + static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true)); \ + void name(color_ostream &out, arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4,arg_type5 arg5, arg_type6 arg6) { \ + handler(out, arg1, arg2, arg3, arg4, arg5, arg6); \ + if (auto state = name##_event.state_if_count()) { \ + DFHack::Lua::Push(state, arg1); \ + DFHack::Lua::Push(state, arg2); \ + DFHack::Lua::Push(state, arg3); \ + DFHack::Lua::Push(state, arg4); \ + DFHack::Lua::Push(state, arg5); \ + DFHack::Lua::Push(state, arg6); \ + name##_event.invoke(out, 6); \ + } \ +} \ No newline at end of file diff --git a/plugins/reactionhooks.cpp b/plugins/reactionhooks.cpp index 4041b99a5..d70fb9ea1 100644 --- a/plugins/reactionhooks.cpp +++ b/plugins/reactionhooks.cpp @@ -184,9 +184,10 @@ df::item* find_item( return NULL; } -static void handle_reaction_done(color_ostream &out, df::unit *unit, std::vector *in_items, std::vector *out_items,bool *call_native){}; +static void handle_reaction_done(color_ostream &out,df::reaction*, df::unit *unit, std::vector *in_items,std::vector *in_reag + , std::vector *out_items,bool *call_native){}; -DEFINE_LUA_EVENT_4(onReactionComplete, handle_reaction_done, df::unit *, std::vector *,std::vector *,bool *); +DEFINE_LUA_EVENT_6(onReactionComplete, handle_reaction_done,df::reaction*, df::unit *, std::vector *,std::vector *,std::vector *,bool *); DFHACK_PLUGIN_LUA_EVENTS { @@ -207,9 +208,11 @@ struct product_hook : item_product { ) { if (auto product = products[this]) { + df::reaction* this_reaction=product->react; + CoreSuspendClaimer suspend; color_ostream_proxy out(Core::getInstance().getConsole()); bool call_native=true; - onReactionComplete(out,unit,in_items,out_items,&call_native); + onReactionComplete(out,this_reaction,unit,in_items,in_reag,out_items,&call_native); if(!call_native) return; } diff --git a/scripts/gui/gm-editor.lua b/scripts/gui/gm-editor.lua index 309663bdf..6999f1d8c 100644 --- a/scripts/gui/gm-editor.lua +++ b/scripts/gui/gm-editor.lua @@ -45,6 +45,25 @@ function GmEditorUi:init(args) return self end +function GmEditorUi:find(test) + local trg=self:currentTarget() + if trg.target and trg.target._kind and trg.target._kind=="container" then + if test== nil then + dialog.showInputPrompt("Test function","Input function that tests(k,v as argument):",COLOR_WHITE,"",dfhack.curry(self.find,self)) + return + end + local e,what=load("return function(k,v) return "..test.." end") + if e==nil then + dialog.showMessage("Error!","function failed to compile\n"..what,COLOR_RED) + end + for k,v in pairs(trg.target) do + if e()(k,v)==true then + self:pushTarget(v) + return + end + end + end +end function GmEditorUi:insertNew(typename) local tp=typename if typename== nil then @@ -165,6 +184,8 @@ end self:changeSelected(10) elseif keys.SELECT then self:editSelected() + elseif keys.CUSTOM_ALT_F then + self:find() elseif keys.CUSTOM_ALT_E then --self:specialEditor() elseif keys.CUSTOM_ALT_I then --insert