Here, try this. I haven't tested it but barring typos and such it should work. It should reduce the liquid level by 1 and run the reaction normally if there is liquid underneath the workshop, otherwise, it should prevent the reagents from being used and produce no product. Also comes with a magma version.
--Lets you make reactions that require water or magma under the workshop. Reactions must start with LUA_HOOK_USEWATER or LUA_HOOK_USEMAGMA.
function usewater(reaction,unit,job,input_items,input_reagents,output_items,call_native)
local building = dfhack.buildings.findAtTile(unit.pos)
local pos = {}
pos.x = building.centerx
pos.y = building.centery
pos.z = building.z
baseBlock = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z)
liquidBlock = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z-1)
if liquidBlock.designation[pos.x%16][pos.y%16].flow_size > 0 and liquidBlock.designation[pos.x%16][pos.y%16].liquid_type == true then
liquidBlock.designation[pos.x%16][pos.y%16].flow_size = liquidBlock.designation[pos.x%16][pos.y%16].flow_size - 1
else
dfhack.gui.showAnnouncement( dfhack.TranslateName(unit.name).." cancels "..reaction.name..": Needs water." , COLOR_RED, true)
for i=0,#input_items-1,1 do
input_items[i].flags.PRESERVE_REAGENT = true
end
for i=0,#reaction.products-1,1 do
reaction.products[i].probability = 0
end
end
end
function usemagma(reaction,unit,job,input_items,input_reagents,output_items,call_native)
local building = dfhack.buildings.findAtTile(unit.pos)
local pos = {}
pos.x = building.centerx
pos.y = building.centery
pos.z = building.z
baseBlock = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z)
liquidBlock = dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z-1)
if liquidBlock.designation[pos.x%16][pos.y%16].flow_size > 0 and liquidBlock.designation[pos.x%16][pos.y%16].liquid_type == false then
liquidBlock.designation[pos.x%16][pos.y%16].flow_size = liquidBlock.designation[pos.x%16][pos.y%16].flow_size - 1
else
dfhack.gui.showAnnouncement( dfhack.TranslateName(unit.name).." cancels "..reaction.name..": Needs magma." , COLOR_RED, true)
for i=0,#input_items-1,1 do
input_items[i].flags.PRESERVE_REAGENT = true
end
for i=0,#reaction.products-1,1 do
reaction.products[i].probability = 0
end
end
end
dfhack.onStateChange.load = function(code)
local registered_reactions
if code==SC_MAP_LOADED then
--registered_reactions = {}
for i,reaction in ipairs(df.global.world.raws.reactions) do
if string.starts(reaction.code,'LUA_HOOK_USEWATER') then
eventful.registerReaction(reaction.code,usewater)
registered_reactions = true
elseif string.starts(reaction.code,'LUA_HOOK_USEMAGMA') then
eventful.registerReaction(reaction.code,usemagma)
registered_reactions = true
end
end
if registered_reactions then
print('Use Liquid Reactions: Loaded.')
end
elseif code==SC_MAP_UNLOADED then
end
end
if dfhack.isMapLoaded() then dfhack.onStateChange.load(SC_MAP_LOADED) end