ruby: misc tweaks

develop
jj 2012-09-18 18:13:22 +02:00
parent cc4639aad0
commit b470abed90
4 changed files with 17 additions and 8 deletions

@ -178,7 +178,17 @@ module DFHack
# set building at position, with optional width/height
def building_position(bld, pos, w=nil, h=nil)
x, y, z = (pos.respond_to?(:x) ? [pos.x, pos.y, pos.z] : pos)
if pos.respond_to?(:x1)
x, y, z = pos.x1, pos.y1, pos.z
w ||= pos.x2-pos.x1+1 if pos.respond_to?(:x2)
h ||= pos.y2-pos.y1+1 if pos.respond_to?(:y2)
elsif pos.respond_to?(:x)
x, y, z = pos.x, pos.y, pos.z
else
x, y, z = pos
end
w ||= pos.w if pos.respond_to?(:w)
h ||= pos.h if pos.respond_to?(:h)
bld.x1 = x
bld.y1 = y
bld.z = z

@ -124,8 +124,7 @@ module DFHack
case h
when Hash; h.each { |k, v| send("#{k}=", v) }
when Array; names = _field_names ; raise 'bad size' if names.length != h.length ; names.zip(h).each { |n, a| send("#{n}=", a) }
when Compound; _field_names.each { |n| send("#{n}=", h.send(n)) }
else raise 'wut?'
else _field_names.each { |n| send("#{n}=", h.send(n)) }
end
end
def _fields ; self.class._fields.to_a ; end

@ -25,11 +25,11 @@ end
module DFHack
class OnupdateCallback
attr_accessor :callback, :timelimit, :minyear, :minyeartick
def initialize(cb, tl)
def initialize(cb, tl, initdelay=0)
@callback = cb
@ticklimit = tl
@minyear = (tl ? df.cur_year : 0)
@minyeartick = (tl ? df.cur_year_tick : 0)
@minyeartick = (tl ? df.cur_year_tick+initdelay : 0)
end
# run callback if timedout
@ -61,9 +61,9 @@ module DFHack
# 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, &b)
def onupdate_register(ticklimit=nil, initialtickdelay=0, &b)
@onupdate_list ||= []
@onupdate_list << OnupdateCallback.new(b, ticklimit)
@onupdate_list << OnupdateCallback.new(b, ticklimit, initialtickdelay)
DFHack.onupdate_active = true
if onext = @onupdate_list.sort.first
DFHack.onupdate_minyear = onext.minyear

@ -49,7 +49,7 @@ module DFHack
end
def unit_iscitizen(u)
u.race == ui.race_id and u.civ_id == ui.civ_id and !u.flags1.dead and !u.flags1.merchant and
u.race == ui.race_id and u.civ_id == ui.civ_id and !u.flags1.dead and !u.flags1.merchant and !u.flags1.forest and
!u.flags1.diplomat and !u.flags2.resident and !u.flags3.ghostly and
!u.curse.add_tags1.OPPOSED_TO_LIFE and !u.curse.add_tags1.CRAZED and
u.mood != :Berserk