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. The plugin interfaces with dfhack 'onupdate' hook.
To register ruby code to be run every graphic frame, use: 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: 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 In this case, the callback is called immediately, and then every X in-game
ticks (advances only when the game is unpaused). ticks (advances only when the game is unpaused).
To stop being called, use: To stop being called, use:

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

@ -5,7 +5,7 @@ class AutoFarm
@lastcounts = Hash.new(0) @lastcounts = Hash.new(0)
end end
def setthreshold (id, v) def setthreshold(id, v)
if df.world.raws.plants.all.find { |r| r.id == id } if df.world.raws.plants.all.find { |r| r.id == id }
@thresholds[id] = v.to_i @thresholds[id] = v.to_i
else else
@ -13,11 +13,11 @@ class AutoFarm
end end
end end
def setdefault (v) def setdefault(v)
@thresholds.default = v.to_i @thresholds.default = v.to_i
end end
def is_plantable (plant) def is_plantable(plant)
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
@ -40,7 +40,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]
@ -66,7 +66,7 @@ class AutoFarm
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.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 counts[i.mat_index] = counts[i.mat_index] + i.stack_size
end end
} }
@ -95,13 +95,13 @@ class AutoFarm
end end
} }
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 (100) { process } @onupdate = df.onupdate_register('autofarm', 100) { process }
@running = true @running = true
end end

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

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

@ -21,7 +21,7 @@ slayit = lambda { |u|
else else
# it's getting hot around here # it's getting hot around here
# !!WARNING!! do not call on a magma-safe creature # !!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 if u.flags1.dead
df.onupdate_unregister(ouh) df.onupdate_unregister(ouh)
else else

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