From d28279894fb1548f3bf18dbed2bb4235e7cb9631 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:33:39 -0500 Subject: [PATCH 1/6] first commit --- library/python/examples/attachtest.py | 53 +++++++++++++++++++++++ library/python/examples/hotkeynotedump.py | 20 +++++++++ library/python/examples/position.py | 24 ++++++++++ library/python/examples/reveal.py | 27 ++++++++++++ library/python/examples/treedump.py | 52 ++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 library/python/examples/attachtest.py create mode 100644 library/python/examples/hotkeynotedump.py create mode 100644 library/python/examples/position.py create mode 100644 library/python/examples/reveal.py create mode 100644 library/python/examples/treedump.py diff --git a/library/python/examples/attachtest.py b/library/python/examples/attachtest.py new file mode 100644 index 000000000..e734e1ba1 --- /dev/null +++ b/library/python/examples/attachtest.py @@ -0,0 +1,53 @@ +import time +from pydfhack import ContextManager + +df_cm = ContextManager("Memory.xml") +df = None + +def test_attach(): + global df + + if not df: + df = df_cm.get_single_context() + + if not df.attach(): + print "Unable to attach!" + return False + elif not df.detach(): + print "Unabled to detach!" + return False + else: + return True + +def suspend_test(): + global df + + if not df: + df = df_cm.get_single_context() + + print "Testing suspend/resume" + + df.attach() + + t1 = time.time() + + for i in xrange(1000): + df.suspend() + + if i % 10 == 0: + print "%i%%" % (i / 10.0,) + + df.resume() + + t2 = time.time() + + df.detach() + + print "suspend test done in $0.9f seconds" % (t2 - t1) + +if __name__ == "__main__": + if test_attach(): + suspend_test() + + print "Done. Press any key to continue" + raw_input() \ No newline at end of file diff --git a/library/python/examples/hotkeynotedump.py b/library/python/examples/hotkeynotedump.py new file mode 100644 index 000000000..edfdfb2f9 --- /dev/null +++ b/library/python/examples/hotkeynotedump.py @@ -0,0 +1,20 @@ +from context import Context, ContextManager + +cm = ContextManager("Memory.xml") +df = cm.get_single_context() + +df.attach() + +pos = df.position + +print "Hotkeys" + +hotkeys = pos.read_hotkeys() + +for key in hotkeys: + print "x: %d\ny: %d\tz: %d\ttext: %s" % (key.x, key.y, key.z, key.name) + +df.detach() + +print "Done. Press any key to continue" +raw_input() \ No newline at end of file diff --git a/library/python/examples/position.py b/library/python/examples/position.py new file mode 100644 index 000000000..32454335e --- /dev/null +++ b/library/python/examples/position.py @@ -0,0 +1,24 @@ +import sys +from pydfhack import ContextManager + +df_cm = ContextManager("Memory.xml") +df = df_cm.get_single_context() + +if not df.attach(): + print "Unable to attach!" + print "Press any key to continue" + + raw_input() + sys.exit(1) + +pos = df.position + +print "view coords: %s" % (pos.view_coords,) +print "cursor coords: %s" % (pos.cursor_coords,) +print "window size: %s" % (pos.window_size,) + +if not df.detach(): + print "Unable to detach!" + +print "Done. Press any key to continue" +raw_input() \ No newline at end of file diff --git a/library/python/examples/reveal.py b/library/python/examples/reveal.py new file mode 100644 index 000000000..bfa066d8a --- /dev/null +++ b/library/python/examples/reveal.py @@ -0,0 +1,27 @@ +from pydfhack import ContextManager, Maps + +df_cm = ContextManager("Memory.xml") +df = df_cm.get_single_context() + +df.attach() + +m = df.maps() + +m.start() + +m_x, m_y, m_z = m.size + +for x in xrange(m_x): + for y in xrange(m_y): + for z in xrange(m_z): + if m.is_valid_block(x, y, z): + d = m.read_designations(x, y, z) + + for i in d: + for j in i: + j.bits.hidden = 0 + + m.write_designations(x, y, z, d) + +m.finish() +df.detach() \ No newline at end of file diff --git a/library/python/examples/treedump.py b/library/python/examples/treedump.py new file mode 100644 index 000000000..766e3abb6 --- /dev/null +++ b/library/python/examples/treedump.py @@ -0,0 +1,52 @@ +from context import Context, ContextManager + +cm = ContextManager("Memory.xml") +df = cm.get_single_context() + +df.attach() + +pos = df.position +veg = df.vegetation +mps = df.maps +mat = df.materials + +x, y, z = pos.get_cursor_coords() + +num_veg = veg.start() + +if x == -30000: + print "----==== Trees ====----" + + for i in xrange(num_veg): + t = veg.read(i) + + print "%d/%d/%d, %d:%d" % (t.x, t.y, t.z, t.type, t.material) +else: + #new method, gets the list of trees in a block. can show farm plants + if mps.start(): + pos_tuple = (x, y, z) + trees = mps.read_vegetation(x / 16, y / 16, z) + + if trees is not None: + for t in trees: + if (t.x, t.y, t.z) == pos_tuple: + print "----==== Tree at %d/%d/%d ====----" % pos_tuple + print str(t) + break + mps.finish() + + #old method, get the tree from the global vegetation vector. can't show farm plants + for i in xrange(num_veg): + t = veg.read(i) + + if (t.x, t.y, t.z) == pos_tuple: + print "----==== Tree at %d/%d/%d ====----" % pos_tuple + print str(t) + break + +veg.finish() + +df.detach() + +print "Done. Press any key to continue" +raw_input() \ No newline at end of file From 6b467a36b5bab2ded048f91a18ed077da47b0aa5 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:34:39 -0500 Subject: [PATCH 2/6] centralized getting an array/list from the pointer_cache --- library/python/pydfhack/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/library/python/pydfhack/util.py b/library/python/pydfhack/util.py index e9485052d..40fd990a1 100644 --- a/library/python/pydfhack/util.py +++ b/library/python/pydfhack/util.py @@ -11,6 +11,18 @@ ubyte_ptr = POINTER(c_ubyte) pointer_dict = {} +def check_pointer_cache(address, return_as_list = True): + arr = None + + if address in pointer_dict: + arr = pointer_dict[address][1] + del pointer_dict[address] + + if return_as_list == True: + arr = [i for i in arr] + + return arr + def _uintify(x, y, z): return (c_uint(x), c_uint(y), c_uint(z)) From 2c28d8f3bff9cae31095e76e02a9d6a0706001bd Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:35:00 -0500 Subject: [PATCH 3/6] added read_hotkeys --- library/python/pydfhack/position.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/python/pydfhack/position.py b/library/python/pydfhack/position.py index d302d4a6a..c70e85bd6 100644 --- a/library/python/pydfhack/position.py +++ b/library/python/pydfhack/position.py @@ -1,5 +1,8 @@ from ctypes import * from dftypes import libdfhack +from util import check_pointer_cache + +libdfhack.Position_ReadHotkeys.restype = c_void_p class Position(object): def __init__(self, ptr): @@ -34,6 +37,9 @@ class Position(object): libdfhack.Position_setCursorCoords(self._pos_ptr, self._cx, self._cy, self._cz) cursor_coords = property(get_cursor_coords, set_cursor_coords) + + def read_hotkeys(self): + return check_pointer_cache(libdfhack.Position_ReadHotkeys(self._pos_ptr)) @property def window_size(self): From 290368321ce8381ad47ddb9d109add38a4334d2c Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:35:29 -0500 Subject: [PATCH 4/6] updated to use the centralized pointer_cache retrieval --- library/python/pydfhack/materials.py | 49 +++++----------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/library/python/pydfhack/materials.py b/library/python/pydfhack/materials.py index d0bfc3ddd..37bfcf2ff 100644 --- a/library/python/pydfhack/materials.py +++ b/library/python/pydfhack/materials.py @@ -1,6 +1,7 @@ from ctypes import * import dftypes from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther +from util import check_pointer_cache libdfhack.Materials_getInorganic.restype = c_void_p libdfhack.Materials_getOrganic.restype = c_void_p @@ -26,60 +27,28 @@ class Materials(object): self.other = None def _get_inorganic(self): - inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) - - if inorganic in dftypes.pointer_dict: - self.inorganic = [i for i in dftypes.pointer_dict[inorganic][1]] - del dftypes.pointer_dict[inorganic] + self.inorganic = check_pointer_cache(libdfhack.Materials_getInorganic(self._mat_ptr)) def _get_organic(self): - organic = libdfhack.Materials_getOrganic(self._mat_ptr) - - if organic in dftypes.pointer_dict: - self.organic = [i for i in dftypes.pointer_dict[organic][1]] - del dftypes.pointer_dict[organic] + self.organic = check_pointer_cache(libdfhack.Materials_getOrganic(self._mat_ptr)) def _get_tree(self): - tree = libdfhack.Materials_getTree(self._mat_ptr) - - if tree in dftypes.pointer_dict: - self.tree = [i for i in dftypes.pointer_dict[tree][1]] - del dftypes.pointer_dict[tree] + self.tree = check_pointer_cache(libdfhack.Materials_getTree(self._mat_ptr)) def _get_plant(self): - plant = libdfhack.Materials_getPlant(self._mat_ptr) - - if plant in dftypes.pointer_dict: - self.plant = [i for i in dftypes.pointer_dict[plant][1]] - del dftypes.pointer_dict[plant] + self.plant = check_pointer_cache(libdfhack.Materials_getPlant(self._mat_ptr)) def _get_race(self): - race = libdfhack.Materials_getRace(self._mat_ptr) - - if race in dftypes.pointer_dict: - self.race = [i for i in dftypes.pointer_dict[race][1]] - del dftypes.pointer_dict[race] + self.race = check_pointer_cache(libdfhack.Materials_getRace(self._mat_ptr)) def _get_race_ex(self): - race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) - - if race_ex in dftypes.pointer_dict: - self.race_ex = [i for i in dftypes.pointer_dict[race_ex][1]] - del dftypes.pointer_dict[race_ex] + self.race_ex = check_pointer_cache(libdfhack.Materials_getRaceEx(self._mat_ptr)) def _get_color(self): - color = libdfhack.Materials_getColor(self._mat_ptr) - - if color in dftypes.pointer_dict: - self.color = [i for i in dftypes.pointer_dict[color][1]] - del dftypes.pointer_dict[color] + self.color = check_pointer_cache(libdfhack.Materials_getColor(self._mat_ptr)) def _get_other(self): - other = libdfhack.Materials_getOther(self._mat_ptr) - - if other in dftypes.pointer_dict: - self.other = [i for i in dftypes.pointer_dict[other][1]] - del dftypes.pointer_dict[other] + self.other = check_pointer_cache(libdfhack.Materials_getOther(self._mat_ptr)) def _get_all(self): self._get_inorganic() From 35438b82e36f11688f38270f209fc8fd1b1ac721 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:35:50 -0500 Subject: [PATCH 5/6] added Hotkey structure --- library/python/pydfhack/dftypes.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/library/python/pydfhack/dftypes.py b/library/python/pydfhack/dftypes.py index 5514d9213..20b035478 100644 --- a/library/python/pydfhack/dftypes.py +++ b/library/python/pydfhack/dftypes.py @@ -49,7 +49,7 @@ class Position2D(Structure): _fields_ = [("x", c_ushort), ("y", c_ushort)] -class Position3d(Structure): +class Position3D(Structure): _fields_ = [("x", c_ushort), ("y", c_ushort), ("z", c_uint)] @@ -580,3 +580,18 @@ _register_callback("alloc_creaturetype_buffer_callback", _alloc_creaturetype_buf class GameModes(Structure): _fields_ = [("control_mode", c_ubyte), ("game_mode", c_ubyte)] + +class Hotkey(Structure): + _fields_ = [("name", (c_char * 10)), + ("mode", c_short), + ("x", c_int), + ("y", c_int), + ("z", c_int)] + +def _alloc_hotkey_buffer_callback(ptr, count): + print "hotkey alloc: %d" % count + return util._allocate_array(ptr, Hotkey, count) + +_hotkey_functype = CFUNCTYPE(c_int, POINTER(POINTER(Hotkey)), c_uint) +_hotkey_func = _hotkey_functype(_alloc_hotkey_buffer_callback) +_register_callback("alloc_hotkey_buffer_callback", _hotkey_func) \ No newline at end of file From 901cebc683f87fca6db0d9c8d9dcdfc20782b555 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 15 Mar 2011 15:36:48 -0500 Subject: [PATCH 6/6] updated to use centralized pointer_dict retrieval fixed an argtypes bug in read_vegetation --- library/python/pydfhack/maps.py | 66 ++++++--------------------------- 1 file changed, 12 insertions(+), 54 deletions(-) diff --git a/library/python/pydfhack/maps.py b/library/python/pydfhack/maps.py index 5ee913892..f7ed0c1f0 100644 --- a/library/python/pydfhack/maps.py +++ b/library/python/pydfhack/maps.py @@ -1,6 +1,6 @@ from ctypes import * from dftypes import * -from util import _uintify, uint_ptr +from util import _uintify, uint_ptr, check_pointer_cache _MAX_DIM = 0x300 _MAX_DIM_SQR = _MAX_DIM * _MAX_DIM @@ -18,7 +18,7 @@ libdfhack.Maps_ReadOccupancy.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POIN libdfhack.Maps_WriteOccupancy.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Occupancies40d) ] libdfhack.Maps_ReadRegionOffsets.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(BiomeIndices40d) ] -libdfhack.Maps_ReadVegetation.argtypes = _default_argtypes +#libdfhack.Maps_ReadVegetation.argtypes = [ c_void_p, c_uint, c_uint, c_uint ] libdfhack.Maps_ReadVegetation.restype = c_void_p libdfhack.Maps_ReadStandardVeins.argtypes = _default_argtypes @@ -33,6 +33,8 @@ libdfhack.Maps_ReadSpatterVeins.restype = c_void_p libdfhack.Maps_ReadGrassVeins.restype = c_void_p libdfhack.Maps_ReadWorldConstructions.restype = c_void_p +libdfhack.Maps_ReadLocalFeatures.restype = c_void_p + class Maps(object): def __init__(self, ptr): self._map_ptr = ptr @@ -172,81 +174,37 @@ class Maps(object): def read_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veins_ptr = libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) - veins = None - - if veins_ptr in dftypes.pointer_dict: - veins = [i for i in dftypes.pointer_dict[veins_ptr][1]] - del dftypes.pointer_dict[veins_ptr] - - return veins + return check_pointer_cache(libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz)) def read_frozen_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veins_ptr = libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) - veins = None - - if veins_ptr in dftypes.pointer_dict: - veins = [i for i in dftypes.pointer_dict[veins_ptr][1]] - del dftypes.pointer_dict[veins_ptr] - - return veins + return check_pointer_cache(libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz)) def read_spatter_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veins_ptr = libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) - veins = None - - if veins_ptr in dftypes.pointer_dict: - veins = [i for i in dftypes.pointer_dict[veins_ptr][1]] - del dftypes.pointer_dict[veins_ptr] - - return veins + return check_pointer_cache(libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz)) def read_grass_veins(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veins_ptr = libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) - veins = None - - if veins_ptr in dftypes.pointer_dict: - veins = [i for i in dftypes.pointer_dict[veins_ptr][1]] - del dftypes.pointer_dict[veins_ptr] - - return veins + return check_pointer_cache(libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz)) def read_world_constructions(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veins_ptr = libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) - veins = None - - if veins_ptr in dftypes.pointer_dict: - veins = [i for i in dftypes.pointer_dict[veins_ptr][1]] - del dftypes.pointer_dict[veins_ptr] - - return veins + return check_pointer_cache(libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz)) def read_vegetation(self, x, y, z): ux, uy, uz = _uintify(x, y, z) - veg_ptr = libdfhack.Maps_ReadVegetation(self._map_ptr, ux, uy, uz) - veg = None - - if veg_ptr in dftypes.pointer_dict: - veg = [i for i in dftypes.pointer_dict[veg_ptr][1]] - del dftypes.pointer_dict[veg_ptr] + return check_pointer_cache(libdfhack.Maps_ReadVegetation(self._map_ptr, ux, uy, uz)) def read_local_features(self): f = libdfhack.Maps_ReadLocalFeatures(self._map_ptr) - feature_dict = {} - f_arr = None - - if f in dftypes.pointer_dict: - f_arr = dftypes.pointer_dict[f][1] - del dftypes.pointer_dict[f] + feature_dict = {} + f_arr = check_pointer_cache(f, False) if f_arr is not None: for node in f_arr: