2012-04-13 08:17:56 -06:00
|
|
|
require 'hack/ruby-autogen'
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
module DFHack
|
2012-03-21 12:30:31 -06:00
|
|
|
class << self
|
2012-04-20 09:33:48 -06:00
|
|
|
# update the ruby.cpp version to accept a block
|
2012-03-21 12:30:31 -06:00
|
|
|
def suspend
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
do_suspend
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
resume
|
|
|
|
end
|
|
|
|
else
|
|
|
|
do_suspend
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-20 09:33:48 -06:00
|
|
|
module ::Kernel
|
|
|
|
def puts(*a)
|
|
|
|
a.flatten.each { |l|
|
|
|
|
DFHack.print_str(l.to_s.chomp + "\n")
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts_err(*a)
|
|
|
|
a.flatten.each { |l|
|
|
|
|
DFHack.print_err(l.to_s.chomp + "\n")
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
2012-04-23 13:12:24 -06:00
|
|
|
|
|
|
|
def p(*a)
|
|
|
|
a.each { |e|
|
|
|
|
puts e.inspect
|
|
|
|
}
|
|
|
|
end
|
2012-03-21 12:30:31 -06:00
|
|
|
end
|
|
|
|
|
2012-04-20 09:33:48 -06:00
|
|
|
# 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(&b)
|
|
|
|
@onupdate_list ||= []
|
|
|
|
@onupdate_list << b
|
|
|
|
DFHack.onupdate_active = true
|
|
|
|
@onupdate_list.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# delete the callback for onupdate ; use the value returned by onupdate_register
|
|
|
|
def onupdate_unregister(b)
|
|
|
|
@onupdate_list.delete b
|
|
|
|
DFHack.onupdate_active = false if @onupdate_list.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
# this method is called by dfhack every 'onupdate' if onupdate_active is true
|
|
|
|
def onupdate
|
|
|
|
@onupdate_list.each { |cb| cb.call }
|
2012-03-21 12:30:31 -06:00
|
|
|
end
|
|
|
|
|
2012-04-17 14:42:23 -06:00
|
|
|
# return an Unit
|
2012-04-20 09:33:48 -06:00
|
|
|
# with no arg, return currently selected unit in df UI ('v' or 'k' menu)
|
2012-04-17 14:42:23 -06:00
|
|
|
# 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
|
2012-04-20 09:33:48 -06:00
|
|
|
def find_unit(what=:selected)
|
|
|
|
if what == :selected
|
2012-04-17 14:42:23 -06:00
|
|
|
case ui.main.mode
|
|
|
|
when UiSidebarMode::ViewUnits
|
|
|
|
# nobody selected => idx == 0
|
|
|
|
v = world.units.other[0][ui_selected_unit]
|
2012-04-20 07:30:15 -06:00
|
|
|
v if v and v.pos.z == cursor.z
|
2012-04-17 14:42:23 -06:00
|
|
|
when UiSidebarMode::LookAround
|
|
|
|
k = ui_look_list.items[ui_look_cursor]
|
2012-04-20 07:30:15 -06:00
|
|
|
k.unit if k.type == UiLookList::Unit
|
2012-04-17 14:42:23 -06:00
|
|
|
end
|
|
|
|
elsif what.kind_of?(Integer)
|
|
|
|
world.units.all.find { |u| u.id == what }
|
|
|
|
elsif what.respond_to?(:x) or what.respond_to?(:pos)
|
|
|
|
what = what.pos if what.respond_to?(:pos)
|
|
|
|
x, y, z = what.x, what.y, what.z
|
|
|
|
world.units.all.find { |u| u.pos.x == x and u.pos.y == y and u.pos.z == z }
|
|
|
|
else
|
|
|
|
raise "what what?"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# return an Item
|
2012-04-20 09:33:48 -06:00
|
|
|
# arg similar to find_unit; no arg = 'k' menu
|
|
|
|
def find_item(what=:selected)
|
|
|
|
if what == :selected
|
2012-04-17 14:42:23 -06:00
|
|
|
case ui.main.mode
|
|
|
|
when UiSidebarMode::LookAround
|
|
|
|
k = ui_look_list.items[ui_look_cursor]
|
2012-04-20 07:30:15 -06:00
|
|
|
k.item if k.type == UiLookList::Item
|
2012-04-17 14:42:23 -06:00
|
|
|
end
|
|
|
|
elsif what.kind_of?(Integer)
|
|
|
|
world.items.all.find { |i| i.id == what }
|
|
|
|
elsif what.respond_to?(:x) or what.respond_to?(:pos)
|
|
|
|
what = what.pos if what.respond_to?(:pos)
|
|
|
|
x, y, z = what.x, what.y, what.z
|
|
|
|
world.items.all.find { |i| i.pos.x == x and i.pos.y == y and i.pos.z == z }
|
|
|
|
else
|
|
|
|
raise "what what?"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-20 09:33:48 -06:00
|
|
|
# return a map block by tile coordinates
|
|
|
|
# you can also use find_map_block(cursor) or anything that respond to x/y/z
|
2012-04-17 14:42:23 -06:00
|
|
|
def find_map_block(x=cursor, 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 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
|
|
|
|
world.map.block_index[x/16][y/16][z]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-23 13:12:24 -06:00
|
|
|
# yields every map block
|
|
|
|
def each_map_block
|
|
|
|
(0...world.map.x_count_block).each { |xb|
|
|
|
|
xl = world.map.block_index[xb]
|
|
|
|
(0...world.map.y_count_block).each { |yb|
|
|
|
|
yl = xl[yb]
|
|
|
|
(0...world.map.z_count_block).each { |z|
|
|
|
|
p = yl[z]
|
|
|
|
yield p._getv if p._getp != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# yields every map block for a given z level
|
|
|
|
def each_map_block_z(z)
|
|
|
|
(0...world.map.x_count_block).each { |xb|
|
|
|
|
xl = world.map.block_index[xb]
|
|
|
|
(0...world.map.y_count_block).each { |yb|
|
|
|
|
p = xl[yb][z]
|
|
|
|
yield p._getv if p._getp != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# center the DF screen on something
|
|
|
|
# updates the cursor position if visible
|
2012-04-20 09:33:48 -06:00
|
|
|
def center_viewscreen(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)
|
|
|
|
|
|
|
|
# compute screen 'map' size (tiles)
|
|
|
|
menuwidth = ui_menu_width
|
|
|
|
# ui_menu_width shows only the 'tab' status
|
|
|
|
menuwidth = 1 if menuwidth == 2 and ui_area_map_width == 2 and cursor.x != -30000
|
|
|
|
menuwidth = 2 if menuwidth == 3 and cursor.x != -30000
|
|
|
|
w_w = gps.dimx - 2
|
|
|
|
w_h = gps.dimy - 2
|
|
|
|
case menuwidth
|
|
|
|
when 1; w_w -= 55
|
|
|
|
when 2; w_w -= (ui_area_map_width == 2 ? 24 : 31)
|
|
|
|
end
|
|
|
|
|
|
|
|
# center view
|
|
|
|
w_x = x - w_w/2
|
|
|
|
w_y = y - w_h/2
|
|
|
|
w_z = z
|
|
|
|
# round view coordinates (optional)
|
|
|
|
#w_x -= w_x % 10
|
|
|
|
#w_y -= w_y % 10
|
|
|
|
# crop to map limits
|
|
|
|
w_x = [[w_x, world.map.x_count - w_w].min, 0].max
|
|
|
|
w_y = [[w_y, world.map.y_count - w_h].min, 0].max
|
|
|
|
|
|
|
|
self.window_x = w_x
|
|
|
|
self.window_y = w_y
|
|
|
|
self.window_z = w_z
|
|
|
|
|
|
|
|
if cursor.x != -30000
|
|
|
|
cursor.x, cursor.y, cursor.z = x, y, z
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# add an announcement
|
|
|
|
# color = integer, bright = bool
|
|
|
|
def add_announcement(str, color=0, bright=false)
|
|
|
|
cont = false
|
|
|
|
while str.length > 0
|
|
|
|
rep = Report.cpp_alloc
|
|
|
|
rep.color = color
|
|
|
|
rep.bright = ((bright && bright != 0) ? 1 : 0)
|
|
|
|
rep.year = cur_year
|
|
|
|
rep.time = cur_year_tick
|
|
|
|
rep.flags.continuation = cont
|
|
|
|
cont = true
|
|
|
|
rep.flags.announcement = true
|
|
|
|
rep.text = str[0, 73]
|
|
|
|
str = str[73..-1].to_s
|
|
|
|
rep.id = world.status.next_report_id
|
|
|
|
world.status.next_report_id += 1
|
|
|
|
world.status.reports << rep
|
|
|
|
world.status.announcements << rep
|
|
|
|
world.status.display_timer = 2000
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-23 13:12:24 -06:00
|
|
|
# try to match a user-specified name to one from the raws
|
|
|
|
# uses case-switching and substring matching
|
|
|
|
# eg match_rawname('coal', ['COAL_BITUMINOUS', 'BAUXITE']) => 'COAL_BITUMINOUS'
|
|
|
|
def match_rawname(name, rawlist)
|
|
|
|
rawlist.each { |r| return r if name == r }
|
|
|
|
rawlist.each { |r| return r if name.downcase == r.downcase }
|
|
|
|
may = rawlist.find_all { |r| r.downcase.index(name.downcase) }
|
|
|
|
may.first if may.length == 1
|
|
|
|
end
|
|
|
|
|
2012-03-21 12:30:31 -06:00
|
|
|
def test
|
|
|
|
puts "starting"
|
|
|
|
|
|
|
|
suspend {
|
2012-04-13 08:17:56 -06:00
|
|
|
puts "cursor pos: #{cursor.x} #{cursor.y} #{cursor.z}"
|
2012-03-21 12:30:31 -06:00
|
|
|
|
2012-04-17 14:42:23 -06:00
|
|
|
if u = find_unit
|
|
|
|
puts "selected unit id: #{u.id}"
|
|
|
|
end
|
2012-04-13 11:31:12 -06:00
|
|
|
|
2012-04-17 14:42:23 -06:00
|
|
|
if b = find_map_block
|
|
|
|
b.designation[cursor.x%16][cursor.y%16].dig = TileDigDesignation::Default
|
|
|
|
b.flags.designated = true
|
2012-04-13 11:31:12 -06:00
|
|
|
puts "dug cursor tile"
|
|
|
|
end
|
2012-03-21 12:30:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
puts "done"
|
|
|
|
end
|
|
|
|
end
|
2012-03-21 11:49:50 -06:00
|
|
|
end
|
|
|
|
|
2012-04-20 09:33:48 -06:00
|
|
|
# global alias so we can write 'df.world.units.all[0]'
|
2012-04-19 11:35:55 -06:00
|
|
|
def df
|
|
|
|
DFHack
|
|
|
|
end
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
# load user-specified startup file
|
|
|
|
load 'ruby_custom.rb' if File.exist?('ruby_custom.rb')
|