-- Makes brewing use one unit of clean water.
--
--[====[
brew-needs-water
======
Makes brewing use one unit of clean water.
Place the still adjacent to clean water, such as a river, pool or channel.
Water that is salt or stagnant will be ignored.
The water has to be accessible. A grate on top works, a floor on top doesn't.
Making mead will not require water. Honey is already a liquid.
]====]
local eventful = require 'plugins.eventful'
local waterFound = true
function intToBool(i)
return (i ~= 0)
end
function getWaterFromTile(x, y, z)
local liquidBlock = dfhack.maps.ensureTileBlock(x,y,z-1)
local blockDesignation = liquidBlock.designation[x%16][y%16]
waterFound = false
if
blockDesignation.flow_size > 0 and
blockDesignation.liquid_type == intToBool(df.tile_liquid.Water) and
not blockDesignation.water_stagnant and
not blockDesignation.water_salt
then
local tiletype = dfhack.maps.getTileType(x,y,z)
local shape = df.tiletype.attrs[tiletype].shape
if -- check if the water is accessible from above
shape == df.tiletype_shape.EMPTY or
shape == df.tiletype_shape.STAIR_DOWN or
shape == df.tiletype_shape.STAIR_UPDOWN or
shape == df.tiletype_shape.RAMP_TOP
then
blockDesignation.flow_size = blockDesignation.flow_size - 1
waterFound = true
end
end
return
end
function usewater(reaction,reaction_product,unit,input_items,input_reagents,output_items,call_native)
-- The reaction calls this function once for each product.
-- call 1: produce drink
-- call 2: produce seeds
-- We only want to check for water the first time.
if reaction_product.item_type ~= df.item_type.DRINK then
call_native.value = waterFound
return
end
local pos = unit.pos
call_native.value = true
-- check the tiles above and below the still for water
for x = pos.x - 1, pos.x + 1, 1 do
for y = pos.y - 2, pos.y + 2, 4 do
getWaterFromTile(x, y, pos.z)
if waterFound then
return
end
end
end
-- check the tiles left and right of the still for water
for x = pos.x - 2, pos.x + 2, 4 do
for y = pos.y - 1, pos.y + 1, 1 do
getWaterFromTile(x, y, pos.z)
if waterFound then
return
end
end
end
dfhack.gui.showZoomAnnouncement(df.announcement_type.CANCEL_JOB, unit.pos, dfhack.TranslateName(unit.name).." cancels "..reaction.name..": Needs clean water." , COLOR_RED, true)
call_native.value = false
return
end
local args = {...}
if args[1] == 'help' then
print(dfhack.script_help())
else
if dfhack.isMapLoaded() then
for i,reaction in ipairs(df.global.world.raws.reactions.reactions) do
if string.startswith(reaction.code,'BREW_DRINK') then
eventful.registerReaction(reaction.code,usewater)
end
end
print('Brewing (except mead) now uses one unit of clean water.')
print('Water will be taken from any source adjacent to the still.')
print('For details, run "brew-needs-water help"')
else
print('Load a map before running this script.')
end
end