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 $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 = ();
indent_rb {
render_enum_fields($type);

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

@ -19,12 +19,14 @@ module DFHack
a.flatten.each { |l|
print_str(l.to_s.chomp + "\n")
}
nil
end
def puts_err(*a)
a.flatten.each { |l|
print_err(l.to_s.chomp + "\n")
}
nil
end
def test
@ -34,6 +36,11 @@ module DFHack
puts "cursor pos: #{cursor.x} #{cursor.y} #{cursor.z}"
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"