From c22c4d009c94e9187faa2f685d7e57ca929585b7 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 3 Jan 2013 20:30:51 +0200 Subject: [PATCH] Added convenience functions to eventful (see Lua API.rst) --- Lua API.rst | 12 +++++ plugins/lua/eventful.lua | 109 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/Lua API.rst b/Lua API.rst index 1cc730b8a..a6bdca4ef 100644 --- a/Lua API.rst +++ b/Lua API.rst @@ -2976,6 +2976,13 @@ List of events 7. onWorkshopFillSidebarMenu(workshop,callnative) - is called when viewing a workshop in 'q' mode, to populate reactions, usefull for custom viewscreens for shops 8. postWorkshopFillSidebarMenu(workshop) - is called after calling (or not) native fillSidebarMenu(). Usefull for job button tweaking (e.g. adding custom reactions) +Functions +--------- + +1. registerReaction(reaction_name,callback) - simplified way of using onReactionComplete, the callback is function (same params as event) +2. removeNative(shop_name) - removes native choice list from the building +3. addReactionToShop(reaction_name,shop_name) - add a custom reaction to the building + Examples -------- Spawn dragon breath on each item attempt to contaminate wound: @@ -3003,6 +3010,11 @@ Granade example: dfhack.maps.spawnFlow(projectile.cur_pos,6,0,0,50000) end +Integrated tannery: +:: + b=require "plugins.eventful" + b.addReactionToShop("TAN_A_HIDE","LEATHERWORKS") + ======= Scripts ======= diff --git a/plugins/lua/eventful.lua b/plugins/lua/eventful.lua index 18d9fd7a0..c610fa87c 100644 --- a/plugins/lua/eventful.lua +++ b/plugins/lua/eventful.lua @@ -1,7 +1,112 @@ local _ENV = mkmodule('plugins.eventful') --[[ - Native events: -TODO + + --]] +local function getShopName(btype,bsubtype,bcustom) + local typenames_shop={[df.workshop_type.Carpenters]="CARPENTERS",[df.workshop_type.Farmers]="FARMERS", + [df.workshop_type.Masons]="MASONS",[df.workshop_type.Craftsdwarfs]="CRAFTSDWARFS", + [df.workshop_type.Jewelers]="JEWELERS",[df.workshop_type.MetalsmithsForge]="METALSMITHSFORGE", + [df.workshop_type.MagmaForge]="MAGMAFORGE",[df.workshop_type.Bowyers]="BOWYERS", + [df.workshop_type.Mechanics]="MECHANICS",[df.workshop_type.Siege]="SIEGE", + [df.workshop_type.Butchers]="BUTCHERS",[df.workshop_type.Leatherworks]="LEATHERWORKS", + [df.workshop_type.Tanners]="TANNERS",[df.workshop_type.Clothiers]="CLOTHIERS", + [df.workshop_type.Fishery]="FISHERY",[df.workshop_type.Still]="STILL", + [df.workshop_type.Loom]="LOOM",[df.workshop_type.Quern]="QUERN", + [df.workshop_type.Kennels]="KENNELS",[df.workshop_type.Ashery]="ASHERY", + [df.workshop_type.Kitchen]="KITCHEN",[df.workshop_type.Dyers]="DYERS", + [df.workshop_type.Tool]="TOOL",[df.workshop_type.Millstone]="MILLSTONE", + } + local typenames_furnace={[df.furnace_type.WoodFurnace]="WOOD_FURNACE",[df.furnace_type.Smelter]="SMELTER", + [df.furnace_type.GlassFurnace]="GLASS_FURNACE",[df.furnace_type.MagmaSmelter]="MAGMA_SMELTER", + [df.furnace_type.MagmaGlassFurnace]="MAGMA_GLASS_FURNACE",[df.furnace_type.MagmaKiln]="MAGMA_KILN", + [df.furnace_type.Kiln]="KILN"} + if btype==df.building_type.Workshop then + if typenames_shop[bsubtype]~=nil then + return typenames_shop[bsubtype] + else + return nil --todo add custom (not very useful) + end + elseif btype==df.building_type.Furnace then + if typenames_furnace[bsubtype]~=nil then + return typenames_furnace[bsubtype] + else + return nil --todo add custom (not very useful) + end + end +end +_registeredStuff={} +local function unregall(state) + if state==SC_WORLD_UNLOADED then + onReactionComplete._library=nil + postWorkshopFillSidebarMenu._library=nil + dfhack.onStateChange.eventful= nil + _registeredStuff={} + end +end +local function onReact(reaction,unit,input_items,input_reagents,output_items,call_native) + if _registeredStuff.reactionCallbacks and _registeredStuff.reactionCallbacks[reaction.code] then + _registeredStuff.reactionCallbacks[reaction.code](reaction,unit,input_items,input_reagents,output_items,call_native) + end +end +local function onPostSidebar(workshop) + local shop_id=getShopName(workshop:getType(),workshop:getSubtype(),workshop:getCustomType()) + if shop_id then + if _registeredStuff.shopNonNative and _registeredStuff.shopNonNative[shop_id] then + if _registeredStuff.shopNonNative[shop_id].all then + --[[for _,button in ipairs(df.global.ui_sidebar_menus.workshop_job.choices_all) do + button.is_hidden=true + end]] + df.global.ui_sidebar_menus.workshop_job.choices_visible:resize(0) + else + --todo by name + end + end + + if _registeredStuff.reactionToShop and _registeredStuff.reactionToShop[shop_id] then + for _,reaction_name in ipairs(_registeredStuff.reactionToShop[shop_id]) do + local new_button=df.interface_button_building_new_jobst:new() + --new_button.hotkey_id=--todo get hotkey + new_button.is_hidden=false + new_button.building=workshop + new_button.job_type=df.job_type.CustomReaction --could be used for other stuff too i guess... + new_button.reaction_name=reaction_name + new_button.is_custom=true + local wjob=df.global.ui_sidebar_menus.workshop_job + wjob.choices_all:insert("#",new_button) + wjob.choices_visible:insert("#",new_button) + end + end + end +end + +function registerReaction(reaction_name,callback) + _registeredStuff.reactionCallbacks=_registeredStuff.reactionCallbacks or {} + _registeredStuff.reactionCallbacks[reaction_name]=callback + onReactionComplete._library=onReact + dfhack.onStateChange.eventful=unregall +end + +function removeNative(shop_name,name) + _registeredStuff.shopNonNative=_registeredStuff.shopNonNative or {} + local shops=_registeredStuff.shopNonNative + shops[shop_name]=shops[shop_name] or {} + if name~=nil then + table.insert(shops[shop_name],name) + else + shops[shop_name].all=true + end + postWorkshopFillSidebarMenu._library=onPostSidebar + dfhack.onStateChange.eventful=unregall +end + +function addReactionToShop(reaction_name,shop_name) + _registeredStuff.reactionToShop=_registeredStuff.reactionToShop or {} + local shops=_registeredStuff.reactionToShop + shops[shop_name]=shops[shop_name] or {} + table.insert(shops[shop_name],reaction_name) + postWorkshopFillSidebarMenu._library=onPostSidebar + dfhack.onStateChange.eventful=unregall +end return _ENV \ No newline at end of file