ruby: add description field to onupdate_register

develop
jj 2012-11-24 16:05:03 +01:00
parent cb06c89698
commit e73274d281
7 changed files with 37 additions and 35 deletions

@ -125,9 +125,9 @@ DFHack callbacks
The plugin interfaces with dfhack 'onupdate' hook.
To register ruby code to be run every graphic frame, use:
handle = df.onupdate_register { puts 'i love flooding the console' }
handle = df.onupdate_register('log') { puts 'i love flooding the console' }
You can also rate-limit when your callback is called to a number of game ticks:
handle = df.onupdate_register(10) { puts '10 more in-game ticks elapsed' }
handle = df.onupdate_register('myname', 10) { puts '10 more in-game ticks elapsed' }
In this case, the callback is called immediately, and then every X in-game
ticks (advances only when the game is unpaused).
To stop being called, use:

@ -24,8 +24,9 @@ end
module DFHack
class OnupdateCallback
attr_accessor :callback, :timelimit, :minyear, :minyeartick
def initialize(cb, tl, initdelay=0)
attr_accessor :callback, :timelimit, :minyear, :minyeartick, :description
def initialize(descr, cb, tl, initdelay=0)
@description = descr
@callback = cb
@ticklimit = tl
@minyear = (tl ? df.cur_year : 0)
@ -34,22 +35,21 @@ module DFHack
# run callback if timedout
def check_run(year, yeartick, yearlen)
if !@ticklimit
@callback.call
else
if year > @minyear or (year == @minyear and yeartick >= @minyeartick)
@minyear = year
@minyeartick = yeartick + @ticklimit
if @minyeartick > yearlen
@minyear += 1
@minyeartick -= yearlen
end
@callback.call
if @ticklimit
return unless year > @minyear or (year == @minyear and yeartick >= @minyeartick)
@minyear = year
@minyeartick = yeartick + @ticklimit
if @minyeartick > yearlen
@minyear += 1
@minyeartick -= yearlen
end
end
# t0 = Time.now
@callback.call
# dt = Time.now - t0 ; puts "rb cb #@description took #{'%.02f' % dt}s" if dt > 0.1
rescue
df.onupdate_unregister self
puts_err "onupdate cb #$!", $!.backtrace
puts_err "onupdate #@description unregistered: #$!", $!.backtrace
end
def <=>(o)
@ -61,10 +61,11 @@ module DFHack
attr_accessor :onupdate_list, :onstatechange_list
# register a callback to be called every gframe or more
# ex: DFHack.onupdate_register { DFHack.world.units[0].counters.job_counter = 0 }
def onupdate_register(ticklimit=nil, initialtickdelay=0, &b)
# ex: DFHack.onupdate_register('fastdwarf') { DFHack.world.units[0].counters.job_counter = 0 }
def onupdate_register(descr, ticklimit=nil, initialtickdelay=0, &b)
raise ArgumentError, 'need a description as 1st arg' unless descr.kind_of?(::String)
@onupdate_list ||= []
@onupdate_list << OnupdateCallback.new(b, ticklimit, initialtickdelay)
@onupdate_list << OnupdateCallback.new(descr, b, ticklimit, initialtickdelay)
DFHack.onupdate_active = true
if onext = @onupdate_list.sort.first
DFHack.onupdate_minyear = onext.minyear
@ -73,8 +74,9 @@ module DFHack
@onupdate_list.last
end
# delete the callback for onupdate ; use the value returned by onupdate_register
# delete the callback for onupdate ; use the value returned by onupdate_register or the description
def onupdate_unregister(b)
b = @onupdate_list.find { |bb| bb.description == b } if b.kind_of?(String)
@onupdate_list.delete b
if @onupdate_list.empty?
DFHack.onupdate_active = false

@ -5,7 +5,7 @@ class AutoFarm
@lastcounts = Hash.new(0)
end
def setthreshold (id, v)
def setthreshold(id, v)
if df.world.raws.plants.all.find { |r| r.id == id }
@thresholds[id] = v.to_i
else
@ -13,11 +13,11 @@ class AutoFarm
end
end
def setdefault (v)
def setdefault(v)
@thresholds.default = v.to_i
end
def is_plantable (plant)
def is_plantable(plant)
season = df.cur_season
harvest = df.cur_season_tick + plant.growdur * 10
will_finish = harvest < 10080
@ -40,7 +40,7 @@ class AutoFarm
return plantable
end
def set_farms ( plants, farms)
def set_farms( plants, farms)
return if farms.length == 0
if plants.length == 0
plants = [-1]
@ -66,7 +66,7 @@ class AutoFarm
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.artifact1 && plantable.has_key? (i.mat_index))
!i.flags.artifact1 && plantable.has_key?(i.mat_index))
counts[i.mat_index] = counts[i.mat_index] + i.stack_size
end
}
@ -95,13 +95,13 @@ class AutoFarm
end
}
set_farms (plants_s, farms_s)
set_farms (plants_u, farms_u)
set_farms(plants_s, farms_s)
set_farms(plants_u, farms_u)
end
def start
@onupdate = df.onupdate_register (100) { process }
@onupdate = df.onupdate_register('autofarm', 100) { process }
@running = true
end

@ -26,7 +26,7 @@ class AutoUnsuspend
end
def start
@onupdate = df.onupdate_register (5) { process }
@onupdate = df.onupdate_register('autounsuspend', 5) { process }
@running = true
end
@ -36,7 +36,7 @@ class AutoUnsuspend
end
def status
stat = @running ? "Running." : "Loaded."
@running ? 'Running.' : 'Stopped.'
end
end
@ -53,6 +53,6 @@ else
if $AutoUnsuspend
puts $AutoUnsuspend.status
else
puts "AI not started"
puts 'Not loaded.'
end
end

@ -4,7 +4,7 @@ $magma_sources ||= []
case $script_args[0]
when 'here'
$magma_onupdate ||= df.onupdate_register(12) {
$magma_onupdate ||= df.onupdate_register('magmasource', 12) {
# called every 12 game ticks (100x a dwarf day)
if $magma_sources.empty?
df.onupdate_unregister($magma_onupdate)

@ -21,7 +21,7 @@ slayit = lambda { |u|
else
# it's getting hot around here
# !!WARNING!! do not call on a magma-safe creature
ouh = df.onupdate_register(1) {
ouh = df.onupdate_register("slayrace ensure #{u.id}", 1) {
if u.flags1.dead
df.onupdate_unregister(ouh)
else

@ -8,12 +8,12 @@ when 'add'
if u = df.unit_find
$superdwarf_ids |= [u.id]
if df.gamemode == :ADVENTURE
if df.gamemode == :ADVENTURE and not df.respond_to?(:cur_year_tick_advmode)
onupdate_delay = nil
else
onupdate_delay = 1
end
$superdwarf_onupdate ||= df.onupdate_register(onupdate_delay) {
$superdwarf_onupdate ||= df.onupdate_register('superdwarf', onupdate_delay) {
if $superdwarf_ids.empty?
df.onupdate_unregister($superdwarf_onupdate)
$superdwarf_onupdate = nil