dfhack/scripts/autofarm.rb

177 lines
4.1 KiB
Ruby

2012-11-20 07:40:42 -07:00
class AutoFarm
def initialize
@thresholds = Hash.new(50)
@lastcounts = Hash.new(0)
end
def setthreshold(id, v)
2013-03-15 19:58:21 -06:00
list = df.world.raws.plants.all.find_all { |plt| plt.flags[:SEED] }.map { |plt| plt.id }
if tok = df.match_rawname(id, list)
@thresholds[tok] = v.to_i
2012-11-20 07:40:42 -07:00
else
2013-03-15 19:58:21 -06:00
puts "No plant with id #{id}, try one of " +
list.map { |w| w =~ /[^\w]/ ? w.inspect : w }.sort.join(' ')
2012-11-20 07:40:42 -07:00
end
end
def setdefault(v)
2012-11-20 07:40:42 -07:00
@thresholds.default = v.to_i
end
2013-03-15 19:58:21 -06:00
def is_plantable(plant)
has_seed = plant.flags[:SEED]
2012-11-20 07:40:42 -07:00
season = df.cur_season
harvest = df.cur_season_tick + plant.growdur * 10
2013-03-15 19:58:21 -06:00
will_finish = harvest < 10080
can_plant = has_seed && plant.flags[season]
2012-11-20 07:40:42 -07:00
can_plant = can_plant && (will_finish || plant.flags[(season+1)%4])
can_plant
end
def find_plantable_plants
plantable = {}
counts = Hash.new(0)
df.world.items.other[:SEEDS].each { |i|
if (!i.flags.dump && !i.flags.forbid && !i.flags.garbage_collect &&
!i.flags.hostile && !i.flags.on_fire && !i.flags.rotten &&
!i.flags.trader && !i.flags.in_building && !i.flags.construction &&
!i.flags.artifact)
2013-03-15 19:58:21 -06:00
counts[i.mat_index] += i.stack_size
end
}
counts.keys.each { |i|
2012-11-20 07:40:42 -07:00
if df.ui.tasks.known_plants[i]
plant = df.world.raws.plants.all[i]
if is_plantable(plant)
plantable[i] = :Surface if (plant.underground_depth_min == 0 || plant.underground_depth_max == 0)
plantable[i] = :Underground if (plant.underground_depth_min > 0 || plant.underground_depth_max > 0)
end
end
}
2012-11-20 07:40:42 -07:00
return plantable
end
2013-03-15 19:58:21 -06:00
def set_farms(plants, farms)
2012-11-20 07:40:42 -07:00
return if farms.length == 0
if plants.length == 0
plants = [-1]
end
season = df.cur_season
2013-03-15 19:58:21 -06:00
farms.each_with_index { |f, idx|
f.plant_id[season] = plants[idx % plants.length]
2012-11-20 07:40:42 -07:00
}
end
def process
plantable = find_plantable_plants
2013-03-15 19:58:21 -06:00
@lastcounts = Hash.new(0)
2012-11-20 07:40:42 -07:00
df.world.items.other[:PLANT].each { |i|
if (!i.flags.dump && !i.flags.forbid && !i.flags.garbage_collect &&
!i.flags.hostile && !i.flags.on_fire && !i.flags.rotten &&
!i.flags.trader && !i.flags.in_building && !i.flags.construction &&
!i.flags.artifact && plantable.has_key?(i.mat_index))
2013-03-15 19:58:21 -06:00
id = df.world.raws.plants.all[i.mat_index].id
@lastcounts[id] += i.stack_size
2012-11-20 07:40:42 -07:00
end
}
2013-03-15 19:58:21 -06:00
return unless @running
2012-11-20 07:40:42 -07:00
plants_s = []
plants_u = []
plantable.each_key { |k|
plant = df.world.raws.plants.all[k]
2013-03-15 19:58:21 -06:00
if (@lastcounts[plant.id] < @thresholds[plant.id])
2012-11-20 07:40:42 -07:00
plants_s.push(k) if plantable[k] == :Surface
plants_u.push(k) if plantable[k] == :Underground
end
}
farms_s = []
farms_u = []
df.world.buildings.other[:FARM_PLOT].each { |f|
if (f.flags.exists)
underground = df.map_designation_at(f.centerx,f.centery,f.z).subterranean
farms_s.push(f) unless underground
farms_u.push(f) if underground
2012-11-20 07:40:42 -07:00
end
}
set_farms(plants_s, farms_s)
set_farms(plants_u, farms_u)
2012-11-20 07:40:42 -07:00
end
def start
2013-03-15 19:58:21 -06:00
return if @running
@onupdate = df.onupdate_register('autofarm', 1200) { process }
2012-11-20 07:40:42 -07:00
@running = true
end
def stop
df.onupdate_unregister(@onupdate)
@running = false
end
def status
stat = @running ? "Running." : "Stopped."
2013-03-15 19:58:21 -06:00
@lastcounts.each { |k,v|
stat << "\n#{k} limit #{@thresholds.fetch(k, 'default')} current #{v}"
}
2012-11-20 07:40:42 -07:00
@thresholds.each { |k,v|
2013-03-15 19:58:21 -06:00
stat << "\n#{k} limit #{v} current 0" unless @lastcounts.has_key?(k)
2012-11-20 07:40:42 -07:00
}
2013-03-15 19:58:21 -06:00
stat << "\nDefault: #{@thresholds.default}"
2012-11-20 07:40:42 -07:00
stat
end
end
2013-03-15 19:58:21 -06:00
$AutoFarm ||= AutoFarm.new
2012-11-20 07:40:42 -07:00
case $script_args[0]
when 'start'
2013-03-15 19:58:21 -06:00
$AutoFarm.start
puts $AutoFarm.status
2012-11-20 07:40:42 -07:00
2013-03-15 19:58:21 -06:00
when 'end', 'stop', 'disable'
$AutoFarm.stop
puts 'Stopped.'
2012-11-20 07:40:42 -07:00
when 'default'
$AutoFarm.setdefault($script_args[1])
when 'threshold'
t = $script_args[1]
$script_args[2..-1].each {|i|
$AutoFarm.setthreshold(i, t)
}
when 'delete'
$AutoFarm.stop
$AutoFarm = nil
2013-03-15 19:58:21 -06:00
when 'help', '?'
puts <<EOS
Automatically handle crop selection in farm plots based on current plant stocks.
Selects a crop for planting if current stock is below a threshold.
Selected crops are dispatched on all farmplots.
Usage:
autofarm start
autofarm default 30
autofarm threshold 150 helmet_plump tail_pig
EOS
2012-11-20 07:40:42 -07:00
else
2013-03-15 19:58:21 -06:00
$AutoFarm.process
puts $AutoFarm.status
2012-11-20 07:40:42 -07:00
end