ruby: allow coords arguments to *_find, add building_find

develop
jj 2012-07-04 18:34:41 +02:00
parent 7c4f60df1d
commit f80ca239d0
3 changed files with 52 additions and 4 deletions

@ -1,5 +1,45 @@
module DFHack
class << self
def building_find(what=:selected, y=nil, z=nil)
if what == :selected
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.building if k.type == :Building
when :BuildingItems, :QueryBuilding
world.selected_building
end
elsif what.kind_of?(Integer)
# search by building.id
return world.buildings.all.binsearch(what) if not z
# search by coordinates
x = what
world.buildings.all.find { |b|
b.z == z and
if b.room.extents
dx = x - b.room.x
dy = y - b.room.y
dx >= 0 and dx <= b.room.width and
dy >= 0 and dy <= b.room.height and
b.room.extents[ dy*b.room.width + dx ] > 0
else
b.x1 <= x and b.x2 >= x and
b.y1 <= y and b.y2 >= y
end
}
elsif what.respond_to?(:x) or what.respond_to?(:pos)
# find the building at the same position
what = what.pos if what.respond_to?(:pos)
building_find(what.x, what.y, what.z)
else
raise "what what?"
end
end
# allocate a new building object
def building_alloc(type, subtype=-1, custom=-1)
cls = rtti_n2c[BuildingType::Classname[type].to_sym]

@ -2,7 +2,7 @@ module DFHack
class << self
# return an Item
# arg similar to unit.rb/unit_find; no arg = 'k' menu
def item_find(what=:selected)
def item_find(what=:selected, y=nil, z=nil)
if what == :selected
if curview._rtti_classname == :viewscreen_itemst
ref = curview.entry_ref[curview.cursor_pos]
@ -29,7 +29,11 @@ module DFHack
end
end
elsif what.kind_of?(Integer)
world.items.all.binsearch(what)
# search by id
return world.items.all.binsearch(what) if not z
# search by position
x = what
world.items.all.find { |i| i.pos.x == x and i.pos.y == y and i.pos.z == z }
elsif what.respond_to?(:x) or what.respond_to?(:pos)
world.items.all.find { |i| same_pos?(what, i) }
else

@ -4,7 +4,7 @@ module DFHack
# with no arg, return currently selected unit in df UI ('v' or 'k' menu)
# with numeric arg, search unit by unit.id
# with an argument that respond to x/y/z (eg cursor), find first unit at this position
def unit_find(what=:selected)
def unit_find(what=:selected, y=nil, z=nil)
if what == :selected
if curview._rtti_classname == :viewscreen_itemst
ref = curview.entry_ref[curview.cursor_pos]
@ -21,7 +21,11 @@ module DFHack
end
end
elsif what.kind_of?(Integer)
world.units.all.binsearch(what)
# search by id
return world.units.all.binsearch(what) if not z
# search by coords
x = what
world.units.all.find { |u| u.pos.x == x and u.pos.y == y and u.pos.z == z }
elsif what.respond_to?(:x) or what.respond_to?(:pos)
world.units.all.find { |u| same_pos?(what, u) }
else