Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Advanced crematorium - ash production based on burned item weight  (Read 1646 times)

Raidau

  • Bay Watcher
    • View Profile

This script allows you to burn anything you wish and get various amount of product depending on burned items weight.

How to use:

-create a lua file in your hack/scripts folder, copy the text there and add the script name to dfhack.init
-create custom reaction(s) starting with "[REACTION:LUA_HOOK_NB_CREMAT" and give it to your civ

I recommend usage of clever input stockpiles or dfhack workshop-job tool to make sure you burn only things you really want to burn.

Dwarf corpses are currenly ignored by reactions, this is hardcoded. So if you want to cremate your dwarves (as well as intruders dwarf necromancers) you need to manually set flag for the corpse item dead_dwarf = false

Code: [Select]
--NB item burning script. Produces additional ash bars depending on burned items total weight.
--By Raidau, based on Roses scripts
--[[
example reaction:
[REACTION:LUA_HOOK_NB_CREMAT_ANY_5]
[NAME:burn 5 organic items to ash]
[BUILDING:KILN:CUSTOM_B]
[REAGENT:FACTOR35:1:NONE:NONE:NONE:NONE][REACTION_CLASS:ORGANIC_BURNABLE][EMPTY] - a trick to avoid improper behavior with thread/globs and everything that has dimensions, containers and cages are very complicated, so its advisable not ot allow them to avoid exploits
[REAGENT:organic item:1:NONE:NONE:NONE:NONE][REACTION_CLASS:ORGANIC_BURNABLE][EMPTY]
[REAGENT:organic item:1:NONE:NONE:NONE:NONE][REACTION_CLASS:ORGANIC_BURNABLE][EMPTY]
[REAGENT:organic item:1:NONE:NONE:NONE:NONE][REACTION_CLASS:ORGANIC_BURNABLE][EMPTY]
[REAGENT:organic item:1:NONE:NONE:NONE:NONE][REACTION_CLASS:ORGANIC_BURNABLE][EMPTY]
[PRODUCT:0:1:BAR:NO_SUBTYPE:ASH:NO_MATGLOSS][PRODUCT_DIMENSION:150] - 0 probability still makes reaction take time and require skill, but dont produce anything
[SKILL:WOOD_BURNING]
[FUEL]

The above reaction requires [REACTION_CLASS:ORGANIC_BURNABLE] added to all desirable materials (or templates) to prevent burning e.g. bronze colossus or inorganic titans' severed body parts
--]]

function cremate(reaction,unit,input_items,input_reagents,output_items,call_native)
--print("cremat debug: ")
--print("input items: ")
--printall(input_items)

--print("maker unit: "..tostring(unit))
local factor = tonumber(string.match(reaction.reagents[0].code,"FACTOR(%d+)"))
print("custom efficiency factor found: "..tostring(factor))
if not factor then factor = 35 end
local totalweight = 0
--local x = output_items[0]

for i=0,#input_items-1 do
local v = input_items[i]
--print ("adding weight of item "..tostring(v).." to total w")
totalweight = totalweight + v.weight + (v.weight_fraction/1000000)
end

--print ("total weight calculated="..totalweight)

local barcount = totalweight^(factor/100)

--print ("barcount calculated="..barcount)

if math.random()<(barcount-math.floor(barcount)) then
barcount = barcount + 1
end

barcount = math.floor(barcount)
--print ("final bar count calculated="..barcount)

if barcount > 0 then
for i = 1, barcount do
local item=df['item_barst']:new() --incredible
item.id=df.global.item_next_id
df.global.world.items.all:insert('#',item)
df.global.item_next_id=df.global.item_next_id+1
item:setMaterial(9)
item:setMaterialIndex(-1)
item.dimension = 150
item:categorize(true)
item.flags.removed=true

if dfhack.buildings.findAtTile(unit.pos) then
dfhack.items.moveToBuilding(item,dfhack.buildings.findAtTile(unit.pos),0)
item.flags.in_building = false
else
dfhack.items.moveToGround(item,{x=unit.pos.x,y=unit.pos.y,z=unit.pos.z})
end

--print ("extra ash bar created "..tostring(item))
end
end
end

-- START Taken from hire-guard.lua
local eventful = require 'plugins.eventful'
local utils = require 'utils'

function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

dfhack.onStateChange.loadnb_cremat = function(code)
local registered_reactions
if code==SC_MAP_LOADED then
--registered_reactions = {}
for i,reaction in ipairs(df.global.world.raws.reactions) do
-- register each applicable reaction (to avoid doing string check
-- for every lua hook reaction (not just ours), this way uses identity check
if string.starts(reaction.code,'LUA_HOOK_NB_CREMAT') then
-- register reaction.code
eventful.registerReaction(reaction.code,cremate)
-- save reaction.code
--table.insert(registered_reactions,reaction.code)
registered_reactions = true
end
end
--if #registered_reactions > 0 then print('HireGuard: Loaded') end
if registered_reactions then print('NB crematorium loaded, reaction found') end
elseif code==SC_MAP_UNLOADED then
--[[ doesn't seem to be working, and probably not needed
registered_reactions = registered_reactions or {}
if #registered_reactions > 0 then print('HireGuard: Unloaded') end
for i,reaction in ipairs(registered_reactions) do
-- un register each registered reaction (to prevent persistance between
-- differing worlds (probably irrelavant, but doesn't hurt)
-- un register reaction.code
eventful.registerReaction(reaction.code,nil)
end
registered_reactions = nil -- clear registered_reactions
--]]
end
end

print("nb_cremat loaded")
-- if dfhack.init has already been run, force it to think SC_WORLD_LOADED to that reactions get refreshed
if dfhack.isMapLoaded() then dfhack.onStateChange.loadnb_cremat(SC_MAP_LOADED) end
-- END Taken from hire-guard.lua

This is part of my upcoming (i hope) mod :)
« Last Edit: December 28, 2014, 08:09:21 am by Raidau »
Logged
Marital status manipulator
Custom item descriprions
Natural Balance Mod (2013-2015) development suspended...