Petr Mrázek 2011-03-30 14:35:41 +02:00
commit e61a907da1
11 changed files with 237 additions and 168 deletions

@ -1,5 +1,5 @@
from ctypes import * from ctypes import c_uint, byref
from dftypes import * from dftypes import libdfhack, Construction
class Constructions(object): class Constructions(object):
def __init__(self, ptr): def __init__(self, ptr):

@ -15,7 +15,6 @@ libdfhack.Context_getProcess.restype = c_void_p
libdfhack.Context_getCreatures.restype = c_void_p libdfhack.Context_getCreatures.restype = c_void_p
libdfhack.Context_getMaps.restype = c_void_p libdfhack.Context_getMaps.restype = c_void_p
libdfhack.Context_getGui.restype = c_void_p libdfhack.Context_getGui.restype = c_void_p
libdfhack.Context_getPosition.restype = c_void_p
libdfhack.Context_getMaterials.restype = c_void_p libdfhack.Context_getMaterials.restype = c_void_p
libdfhack.Context_getTranslation.restype = c_void_p libdfhack.Context_getTranslation.restype = c_void_p
libdfhack.Context_getVegetation.restype = c_void_p libdfhack.Context_getVegetation.restype = c_void_p
@ -58,7 +57,6 @@ class Context(object):
def __init__(self, ptr): def __init__(self, ptr):
self._c_ptr = ptr self._c_ptr = ptr
self._pos_obj = None
self._mat_obj = None self._mat_obj = None
self._map_obj = None self._map_obj = None
self._veg_obj = None self._veg_obj = None
@ -100,14 +98,6 @@ class Context(object):
def is_suspended(self): def is_suspended(self):
return libdfhack.Context_isSuspended(self._c_ptr) > 0 return libdfhack.Context_isSuspended(self._c_ptr) > 0
@property
def position(self):
import position
if self._pos_obj is None:
self._pos_obj = position.Position(libdfhack.Context_getPosition(self._c_ptr))
return self._pos_obj
@property @property
def materials(self): def materials(self):
import materials import materials

@ -276,7 +276,7 @@ class Construction(Structure):
("form", c_ushort), ("form", c_ushort),
("unk_8", c_ushort), ("unk_8", c_ushort),
("mat_type", c_ushort), ("mat_type", c_ushort),
("mat_idx", c_ushort), ("mat_idx", c_uint),
("unk3", c_ushort), ("unk3", c_ushort),
("unk4", c_ushort), ("unk4", c_ushort),
("unk5", c_ushort), ("unk5", c_ushort),

@ -1,22 +1,31 @@
from ctypes import * from ctypes import c_void_p, c_int, c_uint, byref
from pydfhack import libdfhack, ViewScreen from dftypes import libdfhack, ViewScreen, Hotkey
from util import check_pointer_cache
libdfhack.Gui_getViewCoords.argtypes = [ c_void_p, POINTER(c_int), POINTER(c_int), POINTER(c_int) ]
libdfhack.Gui_setViewCoords.argtypes = [ c_void_p, c_int, c_int, c_int ]
libdfhack.Gui_getCursorCoords.argtypes = [ c_void_p, POINTER(c_int), POINTER(c_int), POINTER(c_int) ]
libdfhack.Gui_setCursorCoords.argtypes = [ c_void_p, c_int, c_int, c_int ]
libdfhack.Gui_getWindowSize.argtypes = [ c_void_p, POINTER(c_int), POINTER(c_int) ]
libdfhack.Gui_ReadHotkeys.restype = c_void_p
libdfhack.Gui_ReadViewScreen.argtypes = [ c_void_p, c_void_p ] libdfhack.Gui_ReadViewScreen.argtypes = [ c_void_p, c_void_p ]
libdfhack.Gui_ReadHotkeys.restype = c_void_p
libdfhack.Gui_getScreenTiles.argtypes = [ c_void_p, c_int, c_int ]
libdfhack.Gui_getScreenTiles.restype = c_void_p
class Gui(object): class Gui(object):
def __init__(self, ptr): def __init__(self, ptr):
self._gui_ptr = ptr self._gui_ptr = ptr
self._vx, self._vy, self._vz = c_int(), c_int(), c_int()
self._cx, self._cy, self._cz = c_int(), c_int(), c_int()
self._ww, self._wh = c_int(), c_int()
def start(self): def start(self):
return libdfhack.Gui_Start(self._gui_ptr) return libdfhack.Gui_Start(self._gui_ptr) > 0
def finish(self): def finish(self):
return libdfhack.Gui_Finish(self._gui_ptr) return libdfhack.Gui_Finish(self._gui_ptr) > 0
def read_view_screen(self): def read_view_screen(self):
s = ViewScreen() s = ViewScreen()
@ -27,37 +36,38 @@ class Gui(object):
return None return None
def get_view_coords(self): def get_view_coords(self):
if libdfhack.Gui_getViewCoords(self._gui_ptr, byref(self._vx), byref(self._vy), byref(self._vz)) > 0: x, y, z = (0, 0, 0)
return (self._vx.value, self._vy.value, self._vz.value)
if libdfhack.Gui_getViewCoords(self._gui_ptr, byref(x), byref(y), byref(z)) > 0:
return (x, y, z)
else: else:
return (-1, -1, -1) return (-1, -1, -1)
def set_view_coords(self, v_coords): def set_view_coords(self, x, y, z):
self._vx.value, self._vy.value, self._vz.value = v_coords libdfhack.Gui_setViewCoords(self._gui_ptr, x, y, z)
libdfhack.Gui_setViewCoords(self._gui_ptr, self._vx, self._vy, self._vz)
view_coords = property(get_view_coords, set_view_coords)
def get_cursor_coords(self): def get_cursor_coords(self):
if libdfhack.Gui_getCursorCoords(self._gui_ptr, byref(self._cx), byref(self._cy), byref(self._cz)) > 0: x, y, z = (0, 0, 0)
return (self._cx.value, self._cy.value, self._cz.value)
if libdfhack.Gui_getCursorCoords(self._gui_ptr, byref(x), byref(y), byref(z)) > 0:
return (x, y, z)
else: else:
return (-1, -1, -1) return (-1, -1, -1)
def set_cursor_coords(self, c_coords): def set_cursor_coords(self, x, y, z):
self._cx.value, self._cy.value, self_cz.value = c_coords libdfhack.Gui_setCursorCoords(self._gui_ptr, x, y, z)
libdfhack.Gui_setCursorCoords(self._gui_ptr, self._cx, self._cy, self._cz)
cursor_coords = property(get_cursor_coords, set_cursor_coords)
def read_hotkeys(self): def read_hotkeys(self):
return check_pointer_cache(libdfhack.Gui_ReadHotkeys(self._gui_ptr)) return check_pointer_cache(libdfhack.Gui_ReadHotkeys(self._gui_ptr))
def get_screen_tiles(self, width, height):
return check_pointer_cache(libdfhack.Gui_getScreenTiles(self._gui_ptr, width, height))
@property @property
def window_size(self): def window_size(self):
if libdfhack.Gui_getWindowSize(self._gui_ptr, byref(self._ww), byref(self._wh)) > 0: width, height = (0, 0)
return (self._ww.value, self._wh.value)
if libdfhack.Gui_getWindowSize(self._gui_ptr, byref(width), byref(height)) > 0:
return (width, height)
else: else:
return (-1, -1) return (-1, -1)

@ -5,9 +5,9 @@ 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
_default_argtypes = [ c_void_p, uint_ptr, uint_ptr, uint_ptr ] libdfhack.Maps_getSize.argtypes = [ c_void_p, uint_ptr, uint_ptr, uint_ptr ]
libdfhack.Maps_getPosition.argtypes = [ c_void_p, uint_ptr, uint_ptr, uint_ptr ]
libdfhack.Maps_getSize.argtypes = _default_argtypes
libdfhack.Maps_ReadTileTypes.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(TileTypes40d) ] libdfhack.Maps_ReadTileTypes.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(TileTypes40d) ]
libdfhack.Maps_WriteTileTypes.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(TileTypes40d) ] libdfhack.Maps_WriteTileTypes.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(TileTypes40d) ]
libdfhack.Maps_ReadDesignations.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Designations40d) ] libdfhack.Maps_ReadDesignations.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Designations40d) ]
@ -18,14 +18,13 @@ 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 = [ 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 = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadFrozenVeins.argtypes = _default_argtypes libdfhack.Maps_ReadFrozenVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadSpatterVeins.argtypes = _default_argtypes libdfhack.Maps_ReadSpatterVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadGrassVeins.argtypes = _default_argtypes libdfhack.Maps_ReadGrassVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadWorldConstructions.argtypes = _default_argtypes libdfhack.Maps_ReadWorldConstructions.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadStandardVeins.restype = c_void_p libdfhack.Maps_ReadStandardVeins.restype = c_void_p
libdfhack.Maps_ReadFrozenVeins.restype = c_void_p libdfhack.Maps_ReadFrozenVeins.restype = c_void_p
@ -217,16 +216,19 @@ class Maps(object):
@property @property
def size(self): def size(self):
x = c_uint() x, y, z = (0, 0, 0)
y = c_uint()
z = c_uint()
retval = libdfhack.Maps_getSize(self._map_ptr, byref(x), byref(y), byref(z)) retval = libdfhack.Maps_getSize(self._map_ptr, byref(x), byref(y), byref(z))
if retval > 0: return (x, y, z)
return (int(x.value), int(y.value), int(z.value))
else: @property
return (-1, -1, -1) def position(self):
x, y, z = (0, 0, 0)
libdfhack.Maps_getPosition(self._map_ptr, byref(x), byref(y), byref(z))
return (x, y, z)
class MapPoint(object): class MapPoint(object):
__slots__ = ["_x", "_y", "_z", "_cmp_val"] __slots__ = ["_x", "_y", "_z", "_cmp_val"]

@ -36,6 +36,18 @@ extern "C" {
DFHACK_EXPORT int Gui_Start(DFHackObject* gui); DFHACK_EXPORT int Gui_Start(DFHackObject* gui);
DFHACK_EXPORT int Gui_Finish(DFHackObject* gui); DFHACK_EXPORT int Gui_Finish(DFHackObject* gui);
DFHACK_EXPORT int Gui_getViewCoords(DFHackObject* gui, int32_t* x, int32_t* y, int32_t* z);
DFHACK_EXPORT int Gui_setViewCoords(DFHackObject* gui, int32_t x, int32_t y, int32_t z);
DFHACK_EXPORT int Gui_getCursorCoords(DFHackObject* gui, int32_t* x, int32_t* y, int32_t* z);
DFHACK_EXPORT int Gui_setCursorCoords(DFHackObject* gui, int32_t x, int32_t y, int32_t z);
DFHACK_EXPORT t_hotkey* Gui_ReadHotkeys(DFHackObject* gui);
DFHACK_EXPORT int Gui_getWindowSize(DFHackObject* gui, int32_t* width, int32_t* height);
DFHACK_EXPORT t_screen* Gui_getScreenTiles(DFHackObject* gui, int32_t width, int32_t height);
DFHACK_EXPORT int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen); DFHACK_EXPORT int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen);
DFHACK_EXPORT int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState); DFHACK_EXPORT int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState);

@ -42,6 +42,8 @@ DFHACK_EXPORT t_feature* Maps_ReadGlobalFeatures(DFHackObject* maps);
DFHACK_EXPORT c_featuremap_node* Maps_ReadLocalFeatures(DFHackObject* maps); DFHACK_EXPORT c_featuremap_node* Maps_ReadLocalFeatures(DFHackObject* maps);
DFHACK_EXPORT void Maps_getSize(DFHackObject* maps, uint32_t* x, uint32_t* y, uint32_t* z); DFHACK_EXPORT void Maps_getSize(DFHackObject* maps, uint32_t* x, uint32_t* y, uint32_t* z);
DFHACK_EXPORT void Maps_getPosition(DFHackObject* maps, int32_t* x, int32_t* y, int32_t* z);
DFHACK_EXPORT int Maps_isValidBlock(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); DFHACK_EXPORT int Maps_isValidBlock(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z);
DFHACK_EXPORT uint32_t Maps_getBlockPtr(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); DFHACK_EXPORT uint32_t Maps_getBlockPtr(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z);

@ -48,126 +48,103 @@ int Gui_Finish(DFHackObject* gui)
return -1; return -1;
} }
int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen) int Gui_getViewCoords(DFHackObject* gui, int32_t* x, int32_t* y, int32_t* z)
{ {
if(gui != NULL) if(gui != NULL)
{ {
return ((DFHack::Gui*)gui)->ReadViewScreen(*viewscreen); int32_t tx, ty, tz;
if(((DFHack::Gui*)gui)->getViewCoords(tx, ty, tz))
{
*x = tx;
*y = ty;
*z = tz;
return 1;
}
else
return 0;
} }
return -1; return -1;
} }
int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState) int Gui_setViewCoords(DFHackObject* gui, int32_t x, int32_t y, int32_t z)
{ {
if(gui != NULL) if(gui != NULL)
{ {
*menuState = ((DFHack::Gui*)gui)->ReadMenuState(); if(((DFHack::Gui*)gui)->setViewCoords(x, y, z))
return 1;
return 1; else
return 0;
} }
return -1; return -1;
} }
int Gui_getViewCoords(DFHackObject* pos, int32_t* x, int32_t* y, int32_t* z) int Gui_getCursorCoords(DFHackObject* gui, int32_t* x, int32_t* y, int32_t* z)
{
if(pos != NULL)
{
int32_t tx, ty, tz;
if(((DFHack::Gui*)pos)->getViewCoords(tx, ty, tz))
{
*x = tx;
*y = ty;
*z = tz;
return 1;
}
else
return 0;
}
return -1;
}
int Gui_setViewCoords(DFHackObject* pos, int32_t x, int32_t y, int32_t z)
{
if(pos != NULL)
{
if(((DFHack::Gui*)pos)->setViewCoords(x, y, z))
return 1;
else
return 0;
}
return -1;
}
int Gui_getCursorCoords(DFHackObject* pos, int32_t* x, int32_t* y, int32_t* z)
{ {
if(pos != NULL) if(gui != NULL)
{ {
int32_t tx, ty, tz; int32_t tx, ty, tz;
if(((DFHack::Gui*)pos)->getCursorCoords(tx, ty, tz)) if(((DFHack::Gui*)gui)->getCursorCoords(tx, ty, tz))
{ {
*x = tx; *x = tx;
*y = ty; *y = ty;
*z = tz; *z = tz;
return 1; return 1;
} }
else else
return 0; return 0;
} }
return -1; return -1;
} }
int Gui_setCursorCoords(DFHackObject* pos, int32_t x, int32_t y, int32_t z) int Gui_setCursorCoords(DFHackObject* gui, int32_t x, int32_t y, int32_t z)
{ {
if(pos != NULL) if(gui != NULL)
{ {
if(((DFHack::Gui*)pos)->setCursorCoords(x, y, z)) if(((DFHack::Gui*)gui)->setCursorCoords(x, y, z))
return 1; return 1;
else else
return 0; return 0;
} }
return -1; return -1;
} }
t_hotkey* Gui_ReadHotkeys(DFHackObject* pos) t_hotkey* Gui_ReadHotkeys(DFHackObject* gui)
{ {
if(pos != NULL) if(gui != NULL)
{ {
t_hotkey* buf = NULL; t_hotkey* buf;
(*alloc_hotkey_buffer_callback)(&buf, NUM_HOTKEYS); (*alloc_hotkey_buffer_callback)(&buf, NUM_HOTKEYS);
if(buf != NULL) if(buf != NULL)
{ {
if(((DFHack::Gui*)pos)->ReadHotkeys(buf)) if(((DFHack::Gui*)gui)->ReadHotkeys(buf))
return buf; return buf;
else else
return NULL; return NULL;
} }
else else
return NULL; return NULL;
} }
return NULL; return NULL;
} }
int Gui_getWindowSize(DFHackObject* pos, int32_t* width, int32_t* height) int Gui_getWindowSize(DFHackObject* gui, int32_t* width, int32_t* height)
{ {
if(pos != NULL) if(gui != NULL)
{ {
int32_t tw, th; int32_t tw, th;
if(((DFHack::Gui*)pos)->getWindowSize(tw, th)) if(((DFHack::Gui*)gui)->getWindowSize(tw, th))
{ {
*width = tw; *width = tw;
*height = th; *height = th;
@ -181,26 +158,47 @@ int Gui_getWindowSize(DFHackObject* pos, int32_t* width, int32_t* height)
return -1; return -1;
} }
t_screen* Gui_getScreenTiles(DFHackObject* pos, int32_t width, int32_t height) t_screen* Gui_getScreenTiles(DFHackObject* gui, int32_t width, int32_t height)
{ {
if(pos != NULL) if(gui != NULL)
{ {
t_screen* buf = NULL; t_screen* buf;
(*alloc_screen_buffer_callback)(&buf, width * height); (*alloc_screen_buffer_callback)(&buf, width * height);
if(buf == NULL) if(buf == NULL)
return NULL; return NULL;
if(((DFHack::Gui*)pos)->getScreenTiles(width, height, buf)) if(((DFHack::Gui*)gui)->getScreenTiles(width, height, buf))
return buf; return buf;
else else
return NULL; return NULL;
} }
return NULL; return NULL;
} }
int Gui_ReadViewScreen(DFHackObject* gui, t_viewscreen* viewscreen)
{
if(gui != NULL)
{
return ((DFHack::Gui*)gui)->ReadViewScreen(*viewscreen);
}
return -1;
}
int Gui_ReadMenuState(DFHackObject* gui, uint32_t* menuState)
{
if(gui != NULL)
{
*menuState = ((DFHack::Gui*)gui)->ReadMenuState();
return 1;
}
return -1;
}
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -185,6 +185,14 @@ void Maps_getSize(DFHackObject* maps, uint32_t* x, uint32_t* y, uint32_t* z)
} }
} }
void Maps_getPosition(DFHackObject* maps, int32_t* x, int32_t* y, int32_t* z)
{
if(maps != NULL)
{
((DFHack::Maps*)maps)->getPosition(*x, *y, *z);
}
}
int Maps_isValidBlock(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z) int Maps_isValidBlock(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z)
{ {
if(maps != NULL) if(maps != NULL)

@ -5,11 +5,11 @@ df = cm.get_single_context()
df.attach() df.attach()
pos = df.position gui = df.gui
print "Hotkeys" print "Hotkeys"
hotkeys = pos.read_hotkeys() hotkeys = gui.read_hotkeys()
for key in hotkeys: for key in hotkeys:
print "x: %d\ny: %d\tz: %d\ttext: %s" % (key.x, key.y, key.z, key.name) print "x: %d\ny: %d\tz: %d\ttext: %s" % (key.x, key.y, key.z, key.name)

@ -13,9 +13,56 @@ if not df.attach():
gui = df.gui gui = df.gui
print "view coords: %s" % (gui.view_coords,) if gui is not None:
print "cursor coords: %s" % (gui.cursor_coords,) maps = df.maps
print "window size: %s" % (gui.window_size,) world = df.world
have_maps = maps.start()
world.start()
gm = world.read_game_mode()
if gm is not None:
print gm
date_tuple = (world.read_current_year(), world.read_current_month(), world.read_current_day(), world.read_current_tick())
print "Year: %d Month: %d Day: %d Tick: %d" % date_tuple
v_coords = gui.get_view_coords()
c_coords = gui.get_cursor_coords()
w_coords = (-1, -1, -1)
world_pos_string = ""
if have_maps is True:
w_coords = maps.getPosition()
x = (v_coords[0] + w_coords[0]) * 48
y = (v_coords[1] + w_coords[1]) * 48
z = (v_coords[2] + w_coords[2])
world_pos_string = " world: %d/%d/%d" % (x, y, z)
print "Map world offset: %d/%d/%d embark squares" % w_coords
if v_coords != (-1, -1, -1):
print "view coords: %d/%d/%d" % v_coords
if have_maps is True:
print world_pos_string
if c_coords != (-1, -1, -1):
print "cursor coords: %d/%d/%d" % c_coords
if have_maps is True:
print world_pos_string
window_size = gui.get_window_size()
if window_size != (-1, -1):
print "window size : %d %d" % window_size
else:
print "cursor and window parameters are unsupported on your version of DF"
if not df.detach(): if not df.detach():
print "Unable to detach!" print "Unable to detach!"