ruby: rename find_map_block to map_block_at, add other helper methods

develop
jj 2012-04-24 00:47:10 +02:00
parent 8a05db7a1b
commit c1baebd083
1 changed files with 31 additions and 3 deletions

@ -106,7 +106,7 @@ module DFHack
# return a map block by tile coordinates # return a map block by tile coordinates
# you can also use find_map_block(cursor) or anything that respond to x/y/z # you can also use find_map_block(cursor) or anything that respond to x/y/z
def find_map_block(x=cursor, y=nil, z=nil) def map_block_at(x, y=nil, z=nil)
x = x.pos if x.respond_to?(:pos) x = x.pos if x.respond_to?(:pos)
x, y, z = x.x, x.y, x.z if x.respond_to?(:x) x, y, z = x.x, x.y, x.z if x.respond_to?(:x)
if x >= 0 and x < world.map.x_count and y >= 0 and y < world.map.y_count and z >= 0 and z < world.map.z_count if x >= 0 and x < world.map.x_count and y >= 0 and y < world.map.y_count and z >= 0 and z < world.map.z_count
@ -114,6 +114,22 @@ module DFHack
end end
end end
def map_designation_at(x, y=nil, z=nil)
x = x.pos if x.respond_to?(:pos)
x, y, z = x.x, x.y, x.z if x.respond_to?(:x)
if b = map_block_at(x, y, z)
b.designation[x%16][y%16]
end
end
def map_occupancy_at(x, y=nil, z=nil)
x = x.pos if x.respond_to?(:pos)
x, y, z = x.x, x.y, x.z if x.respond_to?(:x)
if b = map_block_at(x, y, z)
b.occupancy[x%16][y%16]
end
end
# yields every map block # yields every map block
def each_map_block def each_map_block
(0...world.map.x_count_block).each { |xb| (0...world.map.x_count_block).each { |xb|
@ -139,6 +155,18 @@ module DFHack
} }
end end
# return true if the argument is under the cursor
def at_cursor?(obj)
same_pos?(obj, cursor)
end
# returns true if both arguments are at the same x/y/z
def same_pos?(pos1, pos2)
pos1 = pos1.pos if pos1.respond_to?(:pos)
pos2 = pos2.pos if pos2.respond_to?(:pos)
pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z
end
# center the DF screen on something # center the DF screen on something
# updates the cursor position if visible # updates the cursor position if visible
def center_viewscreen(x, y=nil, z=nil) def center_viewscreen(x, y=nil, z=nil)
@ -220,8 +248,8 @@ module DFHack
puts "selected unit id: #{u.id}" puts "selected unit id: #{u.id}"
end end
if b = find_map_block if b = map_block_at(cursor)
b.designation[cursor.x%16][cursor.y%16].dig = TileDigDesignation::Default map_designation_at(cursor).dig = TileDigDesignation::Default
b.flags.designated = true b.flags.designated = true
puts "dug cursor tile" puts "dug cursor tile"
end end