|
|
@ -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
|
|
|
|
@minyear = year
|
|
|
|
if year > @minyear or (year == @minyear and yeartick >= @minyeartick)
|
|
|
|
@minyeartick = yeartick + @ticklimit
|
|
|
|
@minyear = year
|
|
|
|
if @minyeartick > yearlen
|
|
|
|
@minyeartick = yeartick + @ticklimit
|
|
|
|
@minyear += 1
|
|
|
|
if @minyeartick > yearlen
|
|
|
|
@minyeartick -= yearlen
|
|
|
|
@minyear += 1
|
|
|
|
|
|
|
|
@minyeartick -= yearlen
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
@callback.call
|
|
|
|
|
|
|
|
end
|
|
|
|
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
|
|
|
|