2012-06-24 09:30:26 -06:00
|
|
|
# redefine standard i/o methods to use the dfhack console
|
2012-06-24 08:09:31 -06:00
|
|
|
module Kernel
|
|
|
|
def puts(*a)
|
|
|
|
a.flatten.each { |l|
|
2016-10-21 09:00:02 -06:00
|
|
|
# XXX looks like print_str crashes with strings longer than 4096... maybe not nullterminated ?
|
|
|
|
# this workaround fixes it
|
|
|
|
s = l.to_s.chomp + "\n"
|
|
|
|
while s.length > 0
|
|
|
|
DFHack.print_str(s[0, 4000])
|
|
|
|
s[0, 4000] = ''
|
|
|
|
end
|
2012-06-24 08:09:31 -06:00
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts_err(*a)
|
|
|
|
a.flatten.each { |l|
|
2016-10-21 09:00:02 -06:00
|
|
|
s = l.to_s.chomp + "\n"
|
|
|
|
while s.length > 0
|
|
|
|
DFHack.print_err(s[0, 4000])
|
|
|
|
s[0, 4000] = ''
|
|
|
|
end
|
2012-06-24 08:09:31 -06:00
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def p(*a)
|
|
|
|
a.each { |e|
|
|
|
|
puts_err e.inspect
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
module DFHack
|
2012-11-28 05:33:07 -07:00
|
|
|
VERSION = version
|
|
|
|
|
2012-07-05 07:35:37 -06:00
|
|
|
class OnupdateCallback
|
2012-11-24 08:05:03 -07:00
|
|
|
attr_accessor :callback, :timelimit, :minyear, :minyeartick, :description
|
|
|
|
def initialize(descr, cb, tl, initdelay=0)
|
|
|
|
@description = descr
|
2012-07-05 07:35:37 -06:00
|
|
|
@callback = cb
|
|
|
|
@ticklimit = tl
|
|
|
|
@minyear = (tl ? df.cur_year : 0)
|
2012-09-18 10:13:22 -06:00
|
|
|
@minyeartick = (tl ? df.cur_year_tick+initdelay : 0)
|
2012-07-05 07:35:37 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# run callback if timedout
|
|
|
|
def check_run(year, yeartick, yearlen)
|
2012-11-24 08:05:03 -07:00
|
|
|
if @ticklimit
|
|
|
|
return unless year > @minyear or (year == @minyear and yeartick >= @minyeartick)
|
|
|
|
@minyear = year
|
|
|
|
@minyeartick = yeartick + @ticklimit
|
|
|
|
if @minyeartick > yearlen
|
|
|
|
@minyear += 1
|
|
|
|
@minyeartick -= yearlen
|
2012-07-05 07:35:37 -06:00
|
|
|
end
|
|
|
|
end
|
2012-11-24 08:05:03 -07:00
|
|
|
# t0 = Time.now
|
|
|
|
@callback.call
|
|
|
|
# dt = Time.now - t0 ; puts "rb cb #@description took #{'%.02f' % dt}s" if dt > 0.1
|
2012-11-25 09:29:03 -07:00
|
|
|
rescue Exception
|
2012-10-17 06:40:28 -06:00
|
|
|
df.onupdate_unregister self
|
2012-11-24 08:05:03 -07:00
|
|
|
puts_err "onupdate #@description unregistered: #$!", $!.backtrace
|
2012-07-05 07:35:37 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def <=>(o)
|
|
|
|
[@minyear, @minyeartick] <=> [o.minyear, o.minyeartick]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-21 12:30:31 -06:00
|
|
|
class << self
|
2012-07-05 07:35:37 -06:00
|
|
|
attr_accessor :onupdate_list, :onstatechange_list
|
|
|
|
|
2012-04-20 09:33:48 -06:00
|
|
|
# register a callback to be called every gframe or more
|
2012-11-24 08:05:03 -07:00
|
|
|
# ex: DFHack.onupdate_register('fastdwarf') { DFHack.world.units[0].counters.job_counter = 0 }
|
2012-12-11 09:25:51 -07:00
|
|
|
# if ticklimit is given, do not call unless this much game ticks have passed. Handles advmode time stretching.
|
2012-11-24 08:05:03 -07:00
|
|
|
def onupdate_register(descr, ticklimit=nil, initialtickdelay=0, &b)
|
|
|
|
raise ArgumentError, 'need a description as 1st arg' unless descr.kind_of?(::String)
|
2012-04-20 09:33:48 -06:00
|
|
|
@onupdate_list ||= []
|
2012-11-24 08:05:03 -07:00
|
|
|
@onupdate_list << OnupdateCallback.new(descr, b, ticklimit, initialtickdelay)
|
2012-04-20 09:33:48 -06:00
|
|
|
DFHack.onupdate_active = true
|
2018-07-01 09:25:53 -06:00
|
|
|
if onext = @onupdate_list.min
|
2012-07-05 07:35:37 -06:00
|
|
|
DFHack.onupdate_minyear = onext.minyear
|
|
|
|
DFHack.onupdate_minyeartick = onext.minyeartick
|
2012-07-05 08:17:41 -06:00
|
|
|
end
|
2012-04-20 09:33:48 -06:00
|
|
|
@onupdate_list.last
|
|
|
|
end
|
|
|
|
|
2012-11-24 08:05:03 -07:00
|
|
|
# delete the callback for onupdate ; use the value returned by onupdate_register or the description
|
2012-04-20 09:33:48 -06:00
|
|
|
def onupdate_unregister(b)
|
2012-11-24 08:05:03 -07:00
|
|
|
b = @onupdate_list.find { |bb| bb.description == b } if b.kind_of?(String)
|
2012-04-20 09:33:48 -06:00
|
|
|
@onupdate_list.delete b
|
2012-07-05 07:35:37 -06:00
|
|
|
if @onupdate_list.empty?
|
|
|
|
DFHack.onupdate_active = false
|
2012-12-11 09:25:51 -07:00
|
|
|
DFHack.onupdate_minyear = DFHack.onupdate_minyeartick = DFHack.onupdate_minyeartickadv = -1
|
2012-07-05 07:35:37 -06:00
|
|
|
end
|
2012-04-20 09:33:48 -06:00
|
|
|
end
|
|
|
|
|
2012-09-22 03:57:33 -06:00
|
|
|
# same as onupdate_register, but remove the callback once it returns true
|
|
|
|
def onupdate_register_once(*a)
|
|
|
|
handle = onupdate_register(*a) {
|
|
|
|
onupdate_unregister(handle) if yield
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-07-05 07:35:37 -06:00
|
|
|
TICKS_PER_YEAR = 1200*28*12
|
2012-12-11 09:25:51 -07:00
|
|
|
# this method is called by ruby.cpp if df.onupdate_active is true
|
2012-04-20 09:33:48 -06:00
|
|
|
def onupdate
|
2012-04-25 12:21:09 -06:00
|
|
|
@onupdate_list ||= []
|
2012-07-05 07:35:37 -06:00
|
|
|
|
2016-10-28 08:40:14 -06:00
|
|
|
y = yt = 0
|
|
|
|
y = cur_year rescue 0
|
2012-12-11 09:25:51 -07:00
|
|
|
ytmax = TICKS_PER_YEAR
|
|
|
|
if df.gamemode == :ADVENTURE and df.respond_to?(:cur_year_tick_advmode)
|
|
|
|
yt = cur_year_tick_advmode
|
|
|
|
ytmax *= 144
|
|
|
|
else
|
2016-10-28 08:40:14 -06:00
|
|
|
yt = cur_year_tick rescue 0
|
2012-12-11 09:25:51 -07:00
|
|
|
end
|
2012-07-05 07:35:37 -06:00
|
|
|
|
|
|
|
@onupdate_list.each { |o|
|
2012-12-11 09:25:51 -07:00
|
|
|
o.check_run(y, yt, ytmax)
|
2012-07-05 07:35:37 -06:00
|
|
|
}
|
|
|
|
|
2018-07-01 09:25:53 -06:00
|
|
|
if onext = @onupdate_list.min
|
2012-07-05 07:35:37 -06:00
|
|
|
DFHack.onupdate_minyear = onext.minyear
|
2012-12-11 09:25:51 -07:00
|
|
|
if ytmax > TICKS_PER_YEAR
|
|
|
|
DFHack.onupdate_minyeartick = -1
|
|
|
|
DFHack.onupdate_minyeartickadv = onext.minyeartick
|
|
|
|
else
|
|
|
|
DFHack.onupdate_minyeartick = onext.minyeartick
|
|
|
|
DFHack.onupdate_minyeartickadv = -1
|
|
|
|
end
|
2012-07-05 07:35:37 -06:00
|
|
|
end
|
2012-03-21 12:30:31 -06:00
|
|
|
end
|
|
|
|
|
2012-04-25 12:21:09 -06:00
|
|
|
# register a callback to be called every gframe or more
|
|
|
|
# ex: DFHack.onstatechange_register { |newstate| puts "state changed to #{newstate}" }
|
|
|
|
def onstatechange_register(&b)
|
|
|
|
@onstatechange_list ||= []
|
|
|
|
@onstatechange_list << b
|
|
|
|
@onstatechange_list.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# delete the callback for onstatechange ; use the value returned by onstatechange_register
|
|
|
|
def onstatechange_unregister(b)
|
|
|
|
@onstatechange_list.delete b
|
|
|
|
end
|
|
|
|
|
2012-09-26 07:08:54 -06:00
|
|
|
# same as onstatechange_register, but auto-unregisters if the block returns true
|
|
|
|
def onstatechange_register_once
|
|
|
|
handle = onstatechange_register { |st|
|
|
|
|
onstatechange_unregister(handle) if yield(st)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-04-25 12:21:09 -06:00
|
|
|
# this method is called by dfhack every 'onstatechange'
|
|
|
|
def onstatechange(newstate)
|
|
|
|
@onstatechange_list ||= []
|
|
|
|
@onstatechange_list.each { |cb| cb.call(newstate) }
|
|
|
|
end
|
|
|
|
|
2012-04-25 11:22:04 -06:00
|
|
|
# return true if the argument is under the cursor
|
|
|
|
def at_cursor?(obj)
|
|
|
|
same_pos?(obj, cursor)
|
|
|
|
end
|
2012-04-23 16:47:10 -06:00
|
|
|
|
2012-04-25 11:22:04 -06:00
|
|
|
# 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
|
2012-04-23 16:47:10 -06:00
|
|
|
|
2012-04-25 11:22:04 -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-07-04 07:18:36 -06:00
|
|
|
|
|
|
|
def translate_name(name, english=true, onlylastpart=false)
|
|
|
|
out = []
|
|
|
|
|
|
|
|
if not onlylastpart
|
|
|
|
out << name.first_name if name.first_name != ''
|
|
|
|
if name.nickname != ''
|
2014-09-25 14:02:31 -06:00
|
|
|
case respond_to?(:d_init) && d_init.nickname[gametype]
|
2012-07-04 07:18:36 -06:00
|
|
|
when :REPLACE_ALL; return "`#{name.nickname}'"
|
|
|
|
when :REPLACE_FIRST; out.pop
|
|
|
|
end
|
|
|
|
out << "`#{name.nickname}'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return out.join(' ') unless name.words.find { |w| w >= 0 }
|
|
|
|
|
|
|
|
if not english
|
|
|
|
tsl = world.raws.language.translations[name.language]
|
|
|
|
if name.words[0] >= 0 or name.words[1] >= 0
|
|
|
|
out << ''
|
|
|
|
out.last << tsl.words[name.words[0]] if name.words[0] >= 0
|
|
|
|
out.last << tsl.words[name.words[1]] if name.words[1] >= 0
|
|
|
|
end
|
|
|
|
if name.words[5] >= 0
|
|
|
|
out << ''
|
|
|
|
(2..5).each { |i| out.last << tsl.words[name.words[i]] if name.words[i] >= 0 }
|
|
|
|
end
|
|
|
|
if name.words[6] >= 0
|
|
|
|
out << tsl.words[name.words[6]]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
wl = world.raws.language
|
|
|
|
if name.words[0] >= 0 or name.words[1] >= 0
|
|
|
|
out << ''
|
|
|
|
out.last << wl.words[name.words[0]].forms[name.parts_of_speech[0]] if name.words[0] >= 0
|
|
|
|
out.last << wl.words[name.words[1]].forms[name.parts_of_speech[1]] if name.words[1] >= 0
|
|
|
|
end
|
|
|
|
if name.words[5] >= 0
|
2014-05-17 17:40:15 -06:00
|
|
|
out << 'the'
|
2012-07-04 07:18:36 -06:00
|
|
|
out.last.capitalize! if out.length == 1
|
2014-05-17 17:40:15 -06:00
|
|
|
out << wl.words[name.words[2]].forms[name.parts_of_speech[2]] if name.words[2] >= 0
|
|
|
|
out << wl.words[name.words[3]].forms[name.parts_of_speech[3]] if name.words[3] >= 0
|
|
|
|
if name.words[4] >= 0
|
|
|
|
out << wl.words[name.words[4]].forms[name.parts_of_speech[4]]
|
2014-05-18 12:01:07 -06:00
|
|
|
out.last << '-'
|
2014-05-17 17:40:15 -06:00
|
|
|
else
|
|
|
|
out << ''
|
|
|
|
end
|
|
|
|
out.last << wl.words[name.words[5]].forms[name.parts_of_speech[5]]
|
2012-07-04 07:18:36 -06:00
|
|
|
end
|
|
|
|
if name.words[6] >= 0
|
|
|
|
out << 'of'
|
|
|
|
out.last.capitalize! if out.length == 1
|
|
|
|
out << wl.words[name.words[6]].forms[name.parts_of_speech[6]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
out.join(' ')
|
|
|
|
end
|
2012-03-21 12:30:31 -06:00
|
|
|
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-06-24 08:09:31 -06:00
|
|
|
# load autogenned file
|
2012-06-24 09:30:26 -06:00
|
|
|
require './hack/ruby/ruby-autogen-defs'
|
2014-11-16 05:00:38 -07:00
|
|
|
require(RUBY_PLATFORM =~ /mswin|mingw|cygwin/i ? './hack/ruby/ruby-autogen-win' : './hack/ruby/ruby-autogen-gcc')
|
2012-06-24 09:30:26 -06:00
|
|
|
|
2012-06-24 08:09:31 -06:00
|
|
|
# load all modules
|
2014-11-14 04:49:08 -07:00
|
|
|
Dir['./hack/ruby/*.rb'].each { |m| require m.chomp('.rb') if m !~ /ruby-autogen/ }
|