From ea2045580ee37608f32bc3572ebb73823e7e2f89 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 11 May 2010 16:34:59 -0500 Subject: [PATCH] first commits --- dfhack/python/c api/buildings.py | 47 ++++++++ dfhack/python/c api/constructions.py | 25 ++++ dfhack/python/c api/creatures.py | 75 ++++++++++++ dfhack/python/c api/gui.py | 25 ++++ dfhack/python/c api/items.py | 33 ++++++ dfhack/python/c api/maps.py | 163 +++++++++++++++++++++++++++ dfhack/python/c api/materials.py | 141 +++++++++++++++++++++++ dfhack/python/c api/position.py | 43 +++++++ dfhack/python/c api/util.py | 14 +++ dfhack/python/c api/vegetation.py | 25 ++++ 10 files changed, 591 insertions(+) create mode 100644 dfhack/python/c api/buildings.py create mode 100644 dfhack/python/c api/constructions.py create mode 100644 dfhack/python/c api/creatures.py create mode 100644 dfhack/python/c api/gui.py create mode 100644 dfhack/python/c api/items.py create mode 100644 dfhack/python/c api/maps.py create mode 100644 dfhack/python/c api/materials.py create mode 100644 dfhack/python/c api/position.py create mode 100644 dfhack/python/c api/util.py create mode 100644 dfhack/python/c api/vegetation.py diff --git a/dfhack/python/c api/buildings.py b/dfhack/python/c api/buildings.py new file mode 100644 index 000000000..c61fd9f2b --- /dev/null +++ b/dfhack/python/c api/buildings.py @@ -0,0 +1,47 @@ +from ctypes import * +from pydftypes import * +import util + +libdfhack.Buildings_GetCustomWorkshopType.argtypes = [ c_void_p, POINTER(CustomWorkshop) ] + +def Buildings(object): + def __init__(self, ptr): + self._b_ptr = ptr + + def start(self): + num = c_uint() + + if libdfhack.Buildings_Start(self._b_ptr, byref(num)) > 0: + return int(num.value) + else: + return -1 + + def finish(self): + return libdfhack.Buildings_Finish(self._b_ptr) > 0 + + def read(self, index): + b = Building() + + if libdfhack.Buildings_Read(self._b_ptr, c_uint(index), byref(b)) > 0: + return b + else: + return None + + def read_custom_workshop_types(self): + def read_callback(count): + allocated = util._allocate_array(CustomWorkshop, count) + + workshop_types = allocated[0] + + return allocated[1] + + workshop_types = None + callback = _arr_create_func(read_callback) + + if libdfhack.Buildings_ReadCustomWorkshopTypes(self._b_ptr, callback) > 0: + return workshop_types + else: + return None + + def get_custom_workshop_type(self, custom_workshop): + return libdfhack.Buildings_GetCustomWorkshopType(self._b_ptr, byref(custom_workshop)) diff --git a/dfhack/python/c api/constructions.py b/dfhack/python/c api/constructions.py new file mode 100644 index 000000000..e525d564e --- /dev/null +++ b/dfhack/python/c api/constructions.py @@ -0,0 +1,25 @@ +from ctypes import * +from pydftypes import * + +class Constructions(object): + def __init__(self, ptr): + self._c_ptr = ptr + + def start(self): + num = c_uint() + + if libdfhack.Constructions_Start(self._c_ptr, byref(num)) > 0: + return int(num.value) + else: + return -1 + + def finish(self): + return libdfhack.Constructions_Finish(self._c_ptr) > 0 + + def read(self, index): + c = Construction() + + if libdfhack.Constructions_Read(self._c_ptr, c_uint(index), byref(c)) > 0: + return c + else: + return None diff --git a/dfhack/python/c api/creatures.py b/dfhack/python/c api/creatures.py new file mode 100644 index 000000000..d07cf2bdf --- /dev/null +++ b/dfhack/python/c api/creatures.py @@ -0,0 +1,75 @@ +from ctypes import * +from pydftypes import libdfhack, Creature, Material +import util + +libdfhack.Creatures_WriteLabors.argtypes = [ c_void_p, c_uint, POINTER(c_ubyte) ] + +class Creatures(object): + def __init__(self, ptr): + print ptr + self._c_ptr = ptr + + self._d_race_index = None + self._d_civ_id = None + + def start(self): + n = c_uint(0) + + if libdfhack.Creatures_Start(self._c_ptr, byref(n)) > 0: + return int(n.value) + else: + return -1 + + def finish(self): + return libdfhack.Creatures_Finish(self._c_ptr) > 0 + + def read_creature(self, index): + c = Creature() + + if libdfhack.Creatures_ReadCreature(self._c_ptr, c_int(index), byref(c)) > 0: + return c + else: + return None + + def read_creature_in_box(self, index, pos1, pos2): + c = Creature() + + x1, y1, z1 = c_uint(pos1[0]), c_uint(pos1[1]), c_uint(pos1[2]) + x2, y2, z2 = c_uint(pos2[0]), c_uint(pos2[1]), c_uint(pos2[2]) + + retval = libdfhack.Creatures_ReadCreatureInBox(self._c_ptr, byref(c), x1, y1, z1, x2, y2, z2) + + return (retval, c) + + def write_labors(self, index, labors): + return libdfhack.Creatures_WriteLabors(self._c_ptr, c_uint(index), labors) > 0 + + def read_job(self, creature): + def read_callback(count): + allocated = util._allocate_array(Material, count) + + jobs = allocated[0] + + return allocated[1] + + jobs = None + callback = _arr_create_func(read_callback) + + if libdfhack.Creatures_ReadJob(self._c_ptr, byref(creature), callback) > 0: + return jobs + else: + return None + + @property + def dwarf_race_index(self): + if self._d_race_index is None: + self._d_race_index = int(libdfhack.Creatures_GetDwarfRaceIndex(self._c_ptr).value) + + return self._d_race_index + + @property + def dwarf_civ_id(self): + if self._d_civ_id is None: + self._d_civ_id = int(libdfhack.Creatures_GetDwarfCivId(self._c_ptr).value) + + return self._d_civ_id diff --git a/dfhack/python/c api/gui.py b/dfhack/python/c api/gui.py new file mode 100644 index 000000000..09a7c5c94 --- /dev/null +++ b/dfhack/python/c api/gui.py @@ -0,0 +1,25 @@ +from ctypes import * +from pydfhack import libdfhack, ViewScreen + +libdfhack.Gui_ReadViewScreen.argtypes = [ c_void_p, c_void_p ] + +class Gui(object): + def __init__(self, ptr): + self._gui_ptr = ptr + + def start(self): + return libdfhack.Gui_Start(self._gui_ptr) + + def finish(self): + return libdfhack.Gui_Finish(self._gui_ptr) + + def read_pause_state(self): + return libdfhack.Gui_ReadPauseState(self._pos_ptr) > 0 + + def read_view_screen(self): + s = ViewScreen() + + if libdfhack.Gui_ReadViewScreen(self._gui_ptr, byref(s)) > 0: + return s + else: + return None diff --git a/dfhack/python/c api/items.py b/dfhack/python/c api/items.py new file mode 100644 index 000000000..47ad42907 --- /dev/null +++ b/dfhack/python/c api/items.py @@ -0,0 +1,33 @@ +from ctypes import * +from pydftypes import * + +libdfhack.Items_getItemDescription.argtypes = [ c_void_p, c_uint, c_void_ptr, _arr_create_func ] +libdfhack.Items_getItemDescription.restype = c_char_p +libdfhack.Items_getItemClass.argtypes = [ c_void_p, c_int, _arr_create_func ] +libdfhack.Item_getItemClass.restype = c_char_p + +class Items(object): + def __init__(self, ptr): + self._i_ptr = ptr + + def get_item_description(self, itemptr, materials): + def get_callback(count): + item_string = create_string_buffer(count) + + return byref(item_string) + + item_string = None + callback = _arr_create_func(get_callback) + + return libdfhack.Items_getItemDescription(self._i_ptr, itemptr, materials, callback) + + def get_item_class(self, index): + def get_callback(count): + item_string = create_string_buffer(count) + + return byref(item_string) + + item_string = None + callback = _arr_create_func(get_callback) + + return libdfhack.Items_getItemClass(self._i_ptr, index, callback) diff --git a/dfhack/python/c api/maps.py b/dfhack/python/c api/maps.py new file mode 100644 index 000000000..1db1985d8 --- /dev/null +++ b/dfhack/python/c api/maps.py @@ -0,0 +1,163 @@ +from ctypes import * +from pydftypes import libdfhack +from util import _uintify + +libdfhack.Maps_getSize.argtypes = [ c_void_p, uint_ptr, uint_ptr, uint_ptr ] +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_ReadDesignations.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Designations40d) ] +libdfhack.Maps_WriteDesignations.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Designations40d) ] +libdfhack.Maps_ReadTemperatures.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Temperatures) ] +libdfhack.Maps_WriteTemperatures.argtypes = [ c_void_p, c_uint, c_uint, c_uint, POINTER(Temperatures) ] +libdfhack.Maps_ReadOccupancy.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) ] + +class Maps(object): + def __init__(self, ptr): + self._map_ptr = ptr + + def start(self): + return libdfhack.Maps_Start(self._map_ptr) > 0 + + def finish(self): + return libdfhack.Maps_Finish(self._map_ptr) > 0 + + def is_valid_block(self, x, y, z): + return libdfhack.Maps_isValidBlock(self._map_ptr, *_uintify(x, y, z)) > 0 + + def read_tile_types(self, x, y, z): + tt = TileTypes40d() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadTileTypes(self._map_ptr, ux, uy, uz, tt) > 0: + return tt + else: + return None + + def write_tile_types(self, x, y, z, tt): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteTileTypes(self._map_ptr, ux, uy, uz, tt) > 0 + + def read_designations(self, x, y, z): + d = Designations40d() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadDesignations(self._map_ptr, ux, uy, uz, d) > 0: + return d + else: + return None + + def write_designations(self, x, y, z, d): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteDesignations(self._map_ptr, ux, uy, uz, d) > 0 + + def read_temperatures(self, x, y, z): + t = Temperatures() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadDesignations(self._map_ptr, ux, uy, uz, t) > 0: + return t + else: + return None + + def write_temperatures(self, x, y, z, t): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteDesignations(self._map_ptr, ux, uy, uz, t) > 0 + + def read_occupancy(self, x, y, z): + o = Occupancies40d() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadDesignations(self._map_ptr, ux, uy, uz, o) > 0: + return o + else: + return None + + def write_designations(self, x, y, z, o): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteDesignations(self._map_ptr, ux, uy, uz, o) > 0 + + def read_dirty_bit(self, x, y, z): + bit = c_int(0) + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadDirtyBit(self._map_ptr, ux, uy, uz, byref(bit)) > 0: + if bit > 0: + return True + else: + return False + else: + return None + + def write_dirty_bit(self, x, y, z, dirty): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteDirtyBit(self._map_ptr, ux, uy, uz, c_int(dirty)) > 0 + + def read_features(self, x, y, z): + lf = c_short() + gf = c_short() + + ux, uy, uz = _uintify(x, y, z) + + libdfhack.Maps_ReadFeatures(self._map_ptr, ux, uy, uz, byref(lf), byref(fg)) + + return (lf, gf) + + def write_local_feature(self, x, y, z, local_feature = -1): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteLocalFeature(self._map_ptr, ux, uy, uz, c_short(local_feature)) > 0 + + def write_global_feature(self, x, y, z, global_feature = -1): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteGlobalFeature(self._map_ptr, ux, uy, uz, c_short(global_feature)) > 0 + + def read_block_flags(self, x, y, z): + bf = BlockFlags() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadBlockFlags(self._map_ptr, ux, uy, uz, byref(bf)) > 0: + return bf + else: + return None + + def write_block_flags(self, x, y, z, block_flags): + ux, uy, uz = _uintify(x, y, z) + + return libdfhack.Maps_WriteBlockFlags(self._map_ptr, ux, uy, uz, block_flags) > 0 + + def read_region_offsets(self, x, y, z): + bi = BiomeIndices40d() + + ux, uy, uz = _uintify(x, y, z) + + if libdfhack.Maps_ReadRegionOffsets(self._map_ptr, ux, uy, uz, byref(bi)) > 0: + return bi + else: + return None + + @property + def size(self): + x = c_uint() + y = c_uint() + z = c_uint() + + retval = libdfhack.Maps_getSize(self._map_ptr, byref(x), byref(y), byref(z)) + + if retval > 0: + return (int(x.value), int(y.value), int(z.value)) + else: + return (-1, -1, -1) diff --git a/dfhack/python/c api/materials.py b/dfhack/python/c api/materials.py new file mode 100644 index 000000000..04d82fddb --- /dev/null +++ b/dfhack/python/c api/materials.py @@ -0,0 +1,141 @@ +from ctypes import * +from pydftypes import libdfhack +from util import * + +_get_arg_types = [ c_void_p, _arr_create_func ] + +libdfhack.Materials_getInorganic.argtypes = _get_arg_types +libdfhack.Materials_getOrganic.argtypes = _get_arg_types +libdfhack.Materials_getTree.argtypes = _get_arg_types +libdfhack.Materials_getPlant.argtypes = _get_arg_types +libdfhack.Materials_getRace.argtypes = _get_arg_types +#libdfhack.Materials_getRaceEx.argtypes = _get_arg_types +libdfhack.Materials_getColor.argtypes = _get_arg_types +libdfhack.Materials_getOther.argtypes = _get_arg_types + +class Materials(object): + def __init__(self, ptr): + self._mat_ptr = ptr + + self.inorganic = None + self.organic = None + self.tree = None + self.plant = None + self.race = None + self.race_ex = None + self.color = None + self.other = None + + def read_inorganic(self): + return libdfhack.Materials_ReadInorganicMaterials(self._mat_ptr) + + def read_organic(self): + return libdfhack.Materials_ReadOrganicMaterials(self._mat_ptr) + + def read_wood(self): + return libdfhack.Materials_ReadWoodMaterials(self._mat_ptr) + + def read_plant(self): + return libdfhack.Materials_ReadPlantMaterials(self._mat_ptr) + + def read_creature_types(self): + return libdfhack.Materials_ReadCreatureTypes(self._mat_ptr) + + def read_creature_types_ex(self): + return libdfhack.Materials_ReadCreatureTypesEx(self._mat_ptr) + + def read_descriptor_colors(self): + return libdfhack.Materials_ReadDescriptorColors(self._mat_ptr) + + def read_others(self): + return libdfhack.Materials_ReadOthers(self._mat_ptr) + + def read_all(self): + libdfhack.Materials_ReadAllMaterials(self._mat_ptr) + + def get_description(self, material): + return libdfhack.Materials_getDescription(self._mat_ptr, byref(material)) + + def update_inorganic_cache(self): + def update_callback(count): + allocated = _allocate_array(Matgloss, count) + + self.inorganic = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getInorganic(self._mat_ptr, callback) + + def update_organic_cache(self): + def update_callback(count): + allocated = _allocate_array(Matgloss, count) + + self.organic = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getOrganic(self._mat_ptr, callback) + + def update_tree_cache(self): + def update_callback(count): + allocated = _allocate_array(Matgloss, count) + + self.tree = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getTree(self._mat_ptr, callback) + + def update_plant_cache(self): + def update_callback(count): + allocated = _allocate_array(Matgloss, count) + + self.plant = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getPlant(self._mat_ptr, callback) + + def update_race_cache(self): + def update_callback(count): + allocated = _allocate_array(Matgloss, count) + + self.race = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getRace(self._mat_ptr, callback) + + def update_color_cache(self): + def update_callback(count): + allocated = _allocate_array(DescriptorColor, count) + + self.color = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getColor(self._mat_ptr, callback) + + def update_other_cache(self): + def update_callback(count): + allocated = _allocate_array(MatglossOther, count) + + self.other = allocated[0] + + return allocated[1] + + callback = _arr_create_func(update_callback) + + return libdfhack.Materials_getOther(self._mat_ptr, callback) diff --git a/dfhack/python/c api/position.py b/dfhack/python/c api/position.py new file mode 100644 index 000000000..ffa758150 --- /dev/null +++ b/dfhack/python/c api/position.py @@ -0,0 +1,43 @@ +from ctypes import * +from pydftypes import libdfhack + +class Position(object): + def __init__(self, ptr): + self._pos_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 get_view_coords(self): + if libdfhack.Position_getViewCoords(self._pos_ptr, byref(self._vx), byref(self._vy), byref(self._vz)) > 0: + return (self._vx.value, self._vy.value, self._vz.value) + else: + return (-1, -1, -1) + + def set_view_coords(self, v_coords): + self._vx.value, self._vy.value, self._vz.value = v_coords + + libdfhack.Position_setViewCoords(self._pos_ptr, self._vx, self._vy, self._vz) + + view_coords = property(get_view_coords, set_view_coords) + + def get_cursor_coords(self): + if libdfhack.Position_getCursorCoords(self._pos_ptr, byref(self._cx), byref(self._cy), byref(self._cz)) > 0: + return (self._cx.value, self._cy.value, self._cz.value) + else: + return (-1, -1, -1) + + def set_cursor_coords(self, c_coords): + self._cx.value, self._cy.value, self_cz.value = c_coords + + libdfhack.Position_setCursorCoords(self._pos_ptr, self._cx, self._cy, self._cz) + + cursor_coords = property(get_cursor_coords, set_cursor_coords) + + @property + def window_size(self): + if libdfhack.Position_getWindowSize(self._pos_ptr, byref(self._ww), byref(self._wh)) > 0: + return (self._ww.value, self._wh.value) + else: + return (-1, -1) diff --git a/dfhack/python/c api/util.py b/dfhack/python/c api/util.py new file mode 100644 index 000000000..d81b01ede --- /dev/null +++ b/dfhack/python/c api/util.py @@ -0,0 +1,14 @@ +from ctypes import * + +def _uintify(x, y, z): + return (c_uint(x), c_uint(y), c_uint(z)) + +def _allocate_array(t_type, count): + arr_type = t_type * count + + arr = arr_type() + + ptr = c_void_p() + ptr = addressof(arr) + + return (arr, ptr) diff --git a/dfhack/python/c api/vegetation.py b/dfhack/python/c api/vegetation.py new file mode 100644 index 000000000..0c22d26b5 --- /dev/null +++ b/dfhack/python/c api/vegetation.py @@ -0,0 +1,25 @@ +from ctypes import * +from pydftypes import libdfhack, Tree + +class Vegetation(object): + def __init__(self, ptr): + self._v_ptr = ptr + + def start(self): + n = c_uint(0) + + if libdfhack.Vegetation_Start(self._v_ptr, byref(n)) > 0: + return int(n.value) + else: + return -1 + + def finish(self): + return libdfhack.Vegetation_Finish(self._v_ptr) > 0 + + def read(self, index): + t = Tree() + + if libdfhack.Vegetation_Read(self._v_ptr, c_uint(index), byref(t)) > 0: + return t + else: + return None