ruby: refix codegen for refers-to without key-field, add translate_name

develop
jj 2012-07-04 15:18:36 +02:00
parent 877b879e57
commit cb17bde8f4
5 changed files with 76 additions and 10 deletions

@ -298,7 +298,7 @@ sub render_field_reftarget {
return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec);
my $idx = $tg->getAttribute('key-field') || 'id';
my $idx = $tg->getAttribute('key-field');
$tgvec =~ s/^\$global/df/;
return if $tgvec !~ /^[\w\.]+$/;
@ -310,9 +310,14 @@ sub render_field_reftarget {
$tgname .= '_' if ($othername and $tgname eq $othername);
}
if ($idx) {
my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; ${tgvec}.binsearch($name$fidx) ; end";
} else {
push @lines_rb, "def $tgname ; ${tgvec}[$name] ; end";
}
}
sub render_field_refto {
@ -342,7 +347,7 @@ sub render_container_reftarget {
return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec);
my $idx = $tg->getAttribute('key-field') || 'id';
my $idx = $tg->getAttribute('key-field');
$tgvec =~ s/^\$global/df/;
return if $tgvec !~ /^[\w\.]+$/;
@ -354,9 +359,13 @@ sub render_container_reftarget {
$tgname .= '_' if ($othername and $tgname eq $othername);
}
if ($idx) {
my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; $name.map { |i| $tgvec.binsearch(i$fidx) } ; end";
} else {
push @lines_rb, "def $tgname ; $name.map { |i| ${tgvec}[i] } ; end";
}
}
sub render_class_vmethods {

@ -18,7 +18,7 @@ module DFHack
when :ViewUnits
u = world.units.active[ui_selected_unit]
u.inventory[ui_look_cursor].item if u and u.pos.z == cursor.z and
ui_unit_view_mode == :Inventory and u.inventory[ui_look_cursor]
ui_unit_view_mode.value == :Inventory and u.inventory[ui_look_cursor]
end
end
elsif what.kind_of?(Integer)

@ -87,7 +87,7 @@ module DFHack
def compound(name=nil, &b)
m = Class.new(Compound)
DFHack.const_set(name, m) if name
m.instance_eval(&b)
m.class_eval(&b)
m.new
end
def rtti_classname(n)

@ -85,6 +85,57 @@ module DFHack
may = rawlist.find_all { |r| r.downcase.index(name.downcase) }
may.first if may.length == 1
end
def translate_name(name, english=true, onlylastpart=false)
out = []
if not onlylastpart
out << name.first_name if name.first_name != ''
if name.nickname != ''
case respond_to?(:d_init) && d_init.nickname_dwarf
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
out << 'the '
out.last.capitalize! if out.length == 1
(2..5).each { |i| out.last << wl.words[name.words[i]].forms[name.parts_of_speech[i]] if name.words[i] >= 0 }
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
end
end

@ -80,4 +80,10 @@ module DFHack
list
end
end
class LanguageName
def to_s(english=true)
df.translate_name(self, english)
end
end
end