develop
Petr Mrázek 2011-03-18 06:12:16 +01:00
commit 001b8e059d
10 changed files with 231 additions and 95 deletions

@ -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()

@ -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()

@ -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()

@ -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()

@ -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()

@ -49,7 +49,7 @@ class Position2D(Structure):
_fields_ = [("x", c_ushort), _fields_ = [("x", c_ushort),
("y", c_ushort)] ("y", c_ushort)]
class Position3d(Structure): class Position3D(Structure):
_fields_ = [("x", c_ushort), _fields_ = [("x", c_ushort),
("y", c_ushort), ("y", c_ushort),
("z", c_uint)] ("z", c_uint)]
@ -580,3 +580,18 @@ _register_callback("alloc_creaturetype_buffer_callback", _alloc_creaturetype_buf
class GameModes(Structure): class GameModes(Structure):
_fields_ = [("control_mode", c_ubyte), _fields_ = [("control_mode", c_ubyte),
("game_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)

@ -1,6 +1,6 @@
from ctypes import * from ctypes import *
from dftypes import * from dftypes import *
from util import _uintify, uint_ptr from util import _uintify, uint_ptr, check_pointer_cache
_MAX_DIM = 0x300 _MAX_DIM = 0x300
_MAX_DIM_SQR = _MAX_DIM * _MAX_DIM _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_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_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_ReadVegetation.restype = c_void_p
libdfhack.Maps_ReadStandardVeins.argtypes = _default_argtypes 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_ReadGrassVeins.restype = c_void_p
libdfhack.Maps_ReadWorldConstructions.restype = c_void_p libdfhack.Maps_ReadWorldConstructions.restype = c_void_p
libdfhack.Maps_ReadLocalFeatures.restype = c_void_p
class Maps(object): class Maps(object):
def __init__(self, ptr): def __init__(self, ptr):
self._map_ptr = ptr self._map_ptr = ptr
@ -172,81 +174,37 @@ class Maps(object):
def read_veins(self, x, y, z): def read_veins(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veins_ptr = libdfhack.Maps_ReadStandardVeins(self._map_ptr, ux, uy, uz) return check_pointer_cache(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
def read_frozen_veins(self, x, y, z): def read_frozen_veins(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veins_ptr = libdfhack.Maps_ReadFrozenVeins(self._map_ptr, ux, uy, uz) return check_pointer_cache(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
def read_spatter_veins(self, x, y, z): def read_spatter_veins(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veins_ptr = libdfhack.Maps_ReadSpatterVeins(self._map_ptr, ux, uy, uz) return check_pointer_cache(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
def read_grass_veins(self, x, y, z): def read_grass_veins(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veins_ptr = libdfhack.Maps_ReadGrassVeins(self._map_ptr, ux, uy, uz) return check_pointer_cache(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
def read_world_constructions(self, x, y, z): def read_world_constructions(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veins_ptr = libdfhack.Maps_ReadWorldConstructions(self._map_ptr, ux, uy, uz) return check_pointer_cache(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
def read_vegetation(self, x, y, z): def read_vegetation(self, x, y, z):
ux, uy, uz = _uintify(x, y, z) ux, uy, uz = _uintify(x, y, z)
veg_ptr = libdfhack.Maps_ReadVegetation(self._map_ptr, ux, uy, uz) return check_pointer_cache(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]
def read_local_features(self): def read_local_features(self):
f = libdfhack.Maps_ReadLocalFeatures(self._map_ptr) f = libdfhack.Maps_ReadLocalFeatures(self._map_ptr)
feature_dict = {} feature_dict = {}
f_arr = None f_arr = check_pointer_cache(f, False)
if f in dftypes.pointer_dict:
f_arr = dftypes.pointer_dict[f][1]
del dftypes.pointer_dict[f]
if f_arr is not None: if f_arr is not None:
for node in f_arr: for node in f_arr:

@ -1,6 +1,7 @@
from ctypes import * from ctypes import *
import dftypes import dftypes
from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther from dftypes import libdfhack, Matgloss, CreatureType, DescriptorColor, MatglossOther
from util import check_pointer_cache
libdfhack.Materials_getInorganic.restype = c_void_p libdfhack.Materials_getInorganic.restype = c_void_p
libdfhack.Materials_getOrganic.restype = c_void_p libdfhack.Materials_getOrganic.restype = c_void_p
@ -26,60 +27,28 @@ class Materials(object):
self.other = None self.other = None
def _get_inorganic(self): def _get_inorganic(self):
inorganic = libdfhack.Materials_getInorganic(self._mat_ptr) self.inorganic = check_pointer_cache(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]
def _get_organic(self): def _get_organic(self):
organic = libdfhack.Materials_getOrganic(self._mat_ptr) self.organic = check_pointer_cache(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]
def _get_tree(self): def _get_tree(self):
tree = libdfhack.Materials_getTree(self._mat_ptr) self.tree = check_pointer_cache(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]
def _get_plant(self): def _get_plant(self):
plant = libdfhack.Materials_getPlant(self._mat_ptr) self.plant = check_pointer_cache(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]
def _get_race(self): def _get_race(self):
race = libdfhack.Materials_getRace(self._mat_ptr) self.race = check_pointer_cache(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]
def _get_race_ex(self): def _get_race_ex(self):
race_ex = libdfhack.Materials_getRaceEx(self._mat_ptr) self.race_ex = check_pointer_cache(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]
def _get_color(self): def _get_color(self):
color = libdfhack.Materials_getColor(self._mat_ptr) self.color = check_pointer_cache(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]
def _get_other(self): def _get_other(self):
other = libdfhack.Materials_getOther(self._mat_ptr) self.other = check_pointer_cache(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]
def _get_all(self): def _get_all(self):
self._get_inorganic() self._get_inorganic()

@ -1,5 +1,8 @@
from ctypes import * from ctypes import *
from dftypes import libdfhack from dftypes import libdfhack
from util import check_pointer_cache
libdfhack.Position_ReadHotkeys.restype = c_void_p
class Position(object): class Position(object):
def __init__(self, ptr): def __init__(self, ptr):
@ -35,6 +38,9 @@ class Position(object):
cursor_coords = property(get_cursor_coords, set_cursor_coords) cursor_coords = property(get_cursor_coords, set_cursor_coords)
def read_hotkeys(self):
return check_pointer_cache(libdfhack.Position_ReadHotkeys(self._pos_ptr))
@property @property
def window_size(self): def window_size(self):
if libdfhack.Position_getWindowSize(self._pos_ptr, byref(self._ww), byref(self._wh)) > 0: if libdfhack.Position_getWindowSize(self._pos_ptr, byref(self._ww), byref(self._wh)) > 0:

@ -11,6 +11,18 @@ ubyte_ptr = POINTER(c_ubyte)
pointer_dict = {} 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): def _uintify(x, y, z):
return (c_uint(x), c_uint(y), c_uint(z)) return (c_uint(x), c_uint(y), c_uint(z))