autofarm: minor tweaks

develop
jj 2013-03-16 02:58:21 +01:00
parent dad485e5e1
commit 96e7b02127
1 changed files with 44 additions and 33 deletions

@ -6,10 +6,12 @@ class AutoFarm
end end
def setthreshold(id, v) def setthreshold(id, v)
if df.world.raws.plants.all.find { |r| r.id == id } list = df.world.raws.plants.all.find_all { |plt| plt.flags[:SEED] }.map { |plt| plt.id }
@thresholds[id] = v.to_i if tok = df.match_rawname(id, list)
@thresholds[tok] = v.to_i
else else
puts "No plant with id #{id}" puts "No plant with id #{id}, try one of " +
list.map { |w| w =~ /[^\w]/ ? w.inspect : w }.sort.join(' ')
end end
end end
@ -17,11 +19,11 @@ class AutoFarm
@thresholds.default = v.to_i @thresholds.default = v.to_i
end end
def is_plantable (plant) def is_plantable(plant)
has_seed = plant.flags[:SEED] has_seed = plant.flags[:SEED]
season = df.cur_season season = df.cur_season
harvest = df.cur_season_tick + plant.growdur * 10 harvest = df.cur_season_tick + plant.growdur * 10
will_finish = harvest < 10080 will_finish = harvest < 10080
can_plant = has_seed && plant.flags[season] can_plant = has_seed && plant.flags[season]
can_plant = can_plant && (will_finish || plant.flags[(season+1)%4]) can_plant = can_plant && (will_finish || plant.flags[(season+1)%4])
can_plant can_plant
@ -36,7 +38,7 @@ class AutoFarm
!i.flags.hostile && !i.flags.on_fire && !i.flags.rotten && !i.flags.hostile && !i.flags.on_fire && !i.flags.rotten &&
!i.flags.trader && !i.flags.in_building && !i.flags.construction && !i.flags.trader && !i.flags.in_building && !i.flags.construction &&
!i.flags.artifact) !i.flags.artifact)
counts[i.mat_index] = counts[i.mat_index] + i.stack_size counts[i.mat_index] += i.stack_size
end end
} }
@ -53,7 +55,7 @@ class AutoFarm
return plantable return plantable
end end
def set_farms( plants, farms) def set_farms(plants, farms)
return if farms.length == 0 return if farms.length == 0
if plants.length == 0 if plants.length == 0
plants = [-1] plants = [-1]
@ -61,41 +63,36 @@ class AutoFarm
season = df.cur_season season = df.cur_season
idx = 0 farms.each_with_index { |f, idx|
f.plant_id[season] = plants[idx % plants.length]
farms.each { |f|
f.plant_id[season] = plants[idx]
idx = (idx + 1) % plants.length
} }
end end
def process def process
return false unless @running
plantable = find_plantable_plants plantable = find_plantable_plants
counts = Hash.new(0) @lastcounts = Hash.new(0)
df.world.items.other[:PLANT].each { |i| df.world.items.other[:PLANT].each { |i|
if (!i.flags.dump && !i.flags.forbid && !i.flags.garbage_collect && if (!i.flags.dump && !i.flags.forbid && !i.flags.garbage_collect &&
!i.flags.hostile && !i.flags.on_fire && !i.flags.rotten && !i.flags.hostile && !i.flags.on_fire && !i.flags.rotten &&
!i.flags.trader && !i.flags.in_building && !i.flags.construction && !i.flags.trader && !i.flags.in_building && !i.flags.construction &&
!i.flags.artifact && plantable.has_key?(i.mat_index)) !i.flags.artifact && plantable.has_key?(i.mat_index))
counts[i.mat_index] = counts[i.mat_index] + i.stack_size id = df.world.raws.plants.all[i.mat_index].id
@lastcounts[id] += i.stack_size
end end
} }
return unless @running
plants_s = [] plants_s = []
plants_u = [] plants_u = []
@lastcounts.clear
plantable.each_key { |k| plantable.each_key { |k|
plant = df.world.raws.plants.all[k] plant = df.world.raws.plants.all[k]
if (counts[k] < @thresholds[plant.id]) if (@lastcounts[plant.id] < @thresholds[plant.id])
plants_s.push(k) if plantable[k] == :Surface plants_s.push(k) if plantable[k] == :Surface
plants_u.push(k) if plantable[k] == :Underground plants_u.push(k) if plantable[k] == :Underground
end end
@lastcounts[plant.id] = counts[k]
} }
farms_s = [] farms_s = []
@ -110,11 +107,11 @@ class AutoFarm
set_farms(plants_s, farms_s) set_farms(plants_s, farms_s)
set_farms(plants_u, farms_u) set_farms(plants_u, farms_u)
end end
def start def start
@onupdate = df.onupdate_register('autofarm', 100) { process } return if @running
@onupdate = df.onupdate_register('autofarm', 1200) { process }
@running = true @running = true
end end
@ -125,23 +122,28 @@ class AutoFarm
def status def status
stat = @running ? "Running." : "Stopped." stat = @running ? "Running." : "Stopped."
@lastcounts.each { |k,v|
stat << "\n#{k} limit #{@thresholds.fetch(k, 'default')} current #{v}"
}
@thresholds.each { |k,v| @thresholds.each { |k,v|
stat += "\n#{k} limit #{v} current #{@lastcounts[k]}" stat << "\n#{k} limit #{v} current 0" unless @lastcounts.has_key?(k)
} }
stat += "\nDefault: #{@thresholds.default}" stat << "\nDefault: #{@thresholds.default}"
stat stat
end end
end end
$AutoFarm = AutoFarm.new unless $AutoFarm $AutoFarm ||= AutoFarm.new
case $script_args[0] case $script_args[0]
when 'start' when 'start'
$AutoFarm.start $AutoFarm.start
puts $AutoFarm.status
when 'end', 'stop' when 'end', 'stop', 'disable'
$AutoFarm.stop $AutoFarm.stop
puts 'Stopped.'
when 'default' when 'default'
$AutoFarm.setdefault($script_args[1]) $AutoFarm.setdefault($script_args[1])
@ -156,10 +158,19 @@ when 'delete'
$AutoFarm.stop $AutoFarm.stop
$AutoFarm = nil $AutoFarm = nil
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
else else
if $AutoFarm $AutoFarm.process
puts $AutoFarm.status puts $AutoFarm.status
else
puts "AI not started"
end
end end