ruby: move enum constants up one level, allow pointer arithmetic (world.map.block_index), fix StaticArray, add map test

develop
jj 2012-04-13 19:31:12 +02:00
parent bf4bd1f034
commit f85810c1e2
3 changed files with 22 additions and 13 deletions

@ -51,7 +51,8 @@ sub render_global_enum {
my ($name, $type) = @_; my ($name, $type) = @_;
my $rbname = rb_ucase($name); my $rbname = rb_ucase($name);
push @lines_rb, "class $rbname"; # store constants in DFHack::EnumName and not in DFHack::MemHack::EnumName
push @lines_rb, "class ::DFHack::$rbname";
%seen_enum_name = (); %seen_enum_name = ();
indent_rb { indent_rb {
render_enum_fields($type); render_enum_fields($type);

@ -152,28 +152,29 @@ class Pointer < MemStruct
@_tg = tg @_tg = tg
end end
def _getp def _getp(i=0)
DFHack.memory_read_int32(@_memaddr) & 0xffffffff delta = (i != 0 ? i*@_tglen : 0)
(DFHack.memory_read_int32(@_memaddr) & 0xffffffff) + delta
end end
def _setp(v) def _setp(v)
DFHack.memory_write_int32(@_memaddr, v) DFHack.memory_write_int32(@_memaddr, v)
end end
def _get ; self ; end
# _getp/_setp defined in ruby.cpp, access the pointer value def [](i)
def _get addr = _getp(i)
addr = _getp return if addr == 0
return if addr == 0 # XXX are there pointers with -1 as 'empty' value ?
@_tg._at(addr)._get @_tg._at(addr)._get
end end
def _set(v) def []=(i, v)
addr = _getp addr = _getp(i)
raise 'null pointer' if addr == 0 # TODO malloc ? raise 'null pointer' if addr == 0
@_tg._at(addr)._set(v) @_tg._at(addr)._set(v)
end end
# the pointer is invisible, forward all methods to the pointed object # the pointer is invisible, forward all methods to the pointed object
def method_missing(*a) def method_missing(*a)
_get.send(*a) self[0].send(*a)
end end
end end
class StaticArray < MemStruct class StaticArray < MemStruct
@ -193,11 +194,11 @@ class StaticArray < MemStruct
end end
def [](i) def [](i)
i += @_length if i < 0 i += @_length if i < 0
tgat(i)._get _tgat(i)._get
end end
def []=(i, v) def []=(i, v)
i += @_length if i < 0 i += @_length if i < 0
tgat(i)._set(v) _tgat(i)._set(v)
end end
include Enumerable include Enumerable

@ -19,12 +19,14 @@ module DFHack
a.flatten.each { |l| a.flatten.each { |l|
print_str(l.to_s.chomp + "\n") print_str(l.to_s.chomp + "\n")
} }
nil
end end
def puts_err(*a) def puts_err(*a)
a.flatten.each { |l| a.flatten.each { |l|
print_err(l.to_s.chomp + "\n") print_err(l.to_s.chomp + "\n")
} }
nil
end end
def test def test
@ -34,6 +36,11 @@ module DFHack
puts "cursor pos: #{cursor.x} #{cursor.y} #{cursor.z}" puts "cursor pos: #{cursor.x} #{cursor.y} #{cursor.z}"
puts "unit[0] id: #{world.units.all[0].id}" puts "unit[0] id: #{world.units.all[0].id}"
if cursor.x >= 0
world.map.block_index[cursor.x/16][cursor.y/16][cursor.z].designation[cursor.x%16][cursor.y%16].dig = TileDigDesignation::Default
puts "dug cursor tile"
end
} }
puts "done" puts "done"