Created
January 4, 2014 18:30
-
-
Save ab9rf/8258717 to your computer and use it in GitHub Desktop.
Automelt script for Dwarf Fortress. Requires dfhack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutoMelt | |
def initialize | |
end | |
def is_metal? (item) | |
item.respond_to?(:mat_type) && item.mat_type == 0 && df.world.raws.inorganics[item.mat_index].material.flags[:IS_METAL] | |
end | |
def is_meltable?(item) | |
if (item.flags.dump) || (item.flags.forbid) || (item.flags.garbage_collect) || | |
(item.flags.hostile) || (item.flags.on_fire) || (item.flags.rotten) || | |
(item.flags.trader) || (item.flags.in_building) || (item.flags.construction) || | |
(item.flags.artifact) || (item.flags.melt) | |
return false | |
end | |
if ! is_metal?(item) | |
return false | |
end | |
if item.general_refs.any? {|r| r.instance_of? DFHack::GeneralRefContainsItemst} | |
return false | |
end | |
if item.general_refs.any? {|r| r.instance_of? DFHack::GeneralRefUnitHolderst} | |
return false | |
end | |
if (item.instance_of? DFHack::ItemBoxst) || (item.instance_of? DFHack::ItemBarst) | |
return false | |
end | |
cr = item.general_refs.find {|r| r.instance_of? DFHack::GeneralRefContainedInItemst} | |
if (cr) | |
c = DFHack::item_find(cr.item_id) | |
if c.general_refs.any? {|r| r.instance_of? DFHack::GeneralRefUnitHolderst} | |
return false | |
end | |
end | |
if item.respond_to?(:quality) && (item.quality == :Masterful || item.quality == :Artifact) | |
return false | |
end | |
true | |
end | |
def melt(item) | |
item.flags.melt = true | |
if ! (df.world.items.other[:ANY_MELT_DESIGNATED].include?(item)) | |
df.world.items.other[:ANY_MELT_DESIGNATED].push(item) | |
end | |
end | |
def process | |
count = 0 | |
df.world.items.other[:IN_PLAY].each { |i| | |
m = is_meltable?(i) | |
if (is_meltable?(i)) | |
melt(i) | |
count = count + 1 | |
end | |
} | |
puts "#{count} items marked for melting." | |
end | |
def clear | |
count = 0 | |
while (df.world.items.other[:ANY_MELT_DESIGNATED].count > 0) | |
i = df.world.items.other[:ANY_MELT_DESIGNATED].pop | |
i.flags.melt = false | |
count = count + 1 | |
end | |
puts "#{count} items unmarked for melting." | |
end | |
def start | |
return if @running | |
@onupdate = df.onupdate_register('AutoMelt', 1200) { process } | |
@running = true | |
end | |
def stop | |
df.onupdate_unregister(@onupdate) | |
@running = false | |
end | |
def status | |
stat = @running ? "Running." : "Stopped." | |
stat | |
end | |
end | |
$AutoMelt ||= AutoMelt.new | |
case $script_args[0] | |
when 'clear' | |
$AutoMelt.clear | |
when 'once' | |
$AutoMelt.process | |
when 'start', 'enable' | |
$AutoMelt.start | |
puts $AutoMelt.status | |
when 'end', 'stop', 'disable' | |
$AutoMelt.stop | |
puts 'Stopped.' | |
when 'delete' | |
$AutoMelt.stop | |
$AutoMelt = nil | |
when 'help', '?' | |
puts <<EOS | |
Automatically mark metal objects (other than chests) of quality lower than masterful for melting. | |
Usage: | |
automelt process - run once | |
automelt clear - unmark everything marked for melting | |
automelt start - run every 1200 ticks | |
automelt stop - shut down | |
EOS | |
else | |
$AutoMelt.process | |
puts $AutoMelt.status | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment