From 0b9605e08a450d7a5b9f217cc94b26a22f03e7ef Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 18 Jun 2010 10:39:40 -0500 Subject: [PATCH 1/5] changed def to class (durrrr) --- library/python/pydfhack/buildings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/python/pydfhack/buildings.py b/library/python/pydfhack/buildings.py index c61fd9f2b..32c330201 100644 --- a/library/python/pydfhack/buildings.py +++ b/library/python/pydfhack/buildings.py @@ -4,7 +4,7 @@ import util libdfhack.Buildings_GetCustomWorkshopType.argtypes = [ c_void_p, POINTER(CustomWorkshop) ] -def Buildings(object): +class Buildings(object): def __init__(self, ptr): self._b_ptr = ptr From 3abdd0f2f256dc5b3a242f9acf9a5b711c8bcb30 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Jul 2010 13:17:55 -0500 Subject: [PATCH 2/5] added vein reading functions --- library/include/dfhack-c/DFTypes_C.h | 5 + library/include/dfhack-c/modules/Maps_C.h | 5 + library/modules/Maps_C.cpp | 162 ++++++++++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index bf604e13e..4e4817875 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -27,6 +27,7 @@ distribution. #include "DFHack_C.h" #include "dfhack/DFTypes.h" +#include "dfhack/modules/Maps.h" #include "dfhack/modules/Materials.h" #ifdef __cplusplus @@ -100,6 +101,10 @@ DFHACK_EXPORT extern int (*alloc_empty_creaturetype_callback)(c_creaturetype*); DFHACK_EXPORT extern int (*alloc_creaturetype_callback)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t); DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype*, uint32_t); +DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein*, uint32_t); +DFHACK_EXPORT extern int (*alloc_frozenliquidvein_callback)(t_frozenliquidvein*, uint32_t); +DFHACK_EXPORT extern int (*alloc_spattervein_callback)(t_spattervein*, uint32_t); + #ifdef __cplusplus } #endif diff --git a/library/include/dfhack-c/modules/Maps_C.h b/library/include/dfhack-c/modules/Maps_C.h index 299f0d5f5..5cebde9ea 100644 --- a/library/include/dfhack-c/modules/Maps_C.h +++ b/library/include/dfhack-c/modules/Maps_C.h @@ -69,6 +69,11 @@ DFHACK_EXPORT int Maps_WriteBlockFlags(DFHackObject* maps, uint32_t x, uint32_t DFHACK_EXPORT int Maps_ReadRegionOffsets(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, biome_indices40d* buffer); +DFHACK_EXPORT int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer, t_frozenliquidvein* frozenvein_buffer, t_spattervein* spattervein_buffer); +DFHACK_EXPORT int Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer); +DFHACK_EXPORT int Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_frozenliquidvein* frozenvein_buffer); +DFHACK_EXPORT int Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_spattervein* spattervein_buffer); + #ifdef __cplusplus } #endif diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index 5b41fff07..aacf24111 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -22,6 +22,12 @@ must not be misrepresented as being the original software. distribution. */ +#include +#include + +using namespace std; + +#include "dfhack-c/DFTypes_C.h" #include "dfhack-c/modules/Maps_C.h" #ifdef __cplusplus @@ -268,6 +274,162 @@ int Maps_ReadRegionOffsets(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t return -1; } +int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer, t_frozenliquidvein* frozenvein_buffer, t_spattervein* spattervein_buffer) +{ + if(maps != NULL) + { + if(alloc_vein_buffer_callback == NULL || alloc_frozenliquidvein_callback == NULL || alloc_spattervein_callback == NULL) + return 0; + + vector veins; + vector frozen_veins; + vector spatter_veins; + bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins, &frozen_veins, &spatter_veins); + + if(result) + { + t_vein* v_buf = NULL; + t_frozenliquidvein* fv_buf = NULL; + t_spattervein* sv_buf = NULL; + + if(veins.size() > 0) + { + ((*alloc_vein_buffer_callback)(v_buf, veins.size())); + + copy(veins.begin(), veins.end(), v_buf); + } + + if(frozen_veins.size() > 0) + { + ((*alloc_frozenliquidvein_callback)(fv_buf, frozen_veins.size())); + + copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); + } + + if(spatter_veins.size() > 0) + { + ((*alloc_spattervein_callback)(sv_buf, spatter_veins.size())); + + copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); + } + + return 1; + } + else + return 0; + } + + return -1; +} + +int Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer) +{ + if(maps != NULL) + { + if(alloc_vein_buffer_callback == NULL) + return 0; + + vector veins; + bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins); + + if(result) + { + t_vein* v_buf = NULL; + + if(veins.size() > 0) + { + ((*alloc_vein_buffer_callback)(v_buf, veins.size())); + + copy(veins.begin(), veins.end(), v_buf); + } + + return 1; + } + else + return 0; + } + + return -1; +} + +int Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_frozenliquidvein* frozenvein_buffer) +{ + if(maps != NULL) + { + if(alloc_vein_buffer_callback == NULL || alloc_frozenliquidvein_callback == NULL) + return 0; + + vector veins; + vector frozen_veins; + bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins, &frozen_veins); + + if(result) + { + t_vein* v_buf = NULL; + t_frozenliquidvein* fv_buf = NULL; + + if(veins.size() > 0) + { + ((*alloc_vein_buffer_callback)(v_buf, veins.size())); + + copy(veins.begin(), veins.end(), v_buf); + } + + if(frozen_veins.size() > 0) + { + ((*alloc_frozenliquidvein_callback)(fv_buf, frozen_veins.size())); + + copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); + } + + return 1; + } + else + return 0; + } + + return -1; +} + +int Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_spattervein* spattervein_buffer) +{ + if(maps != NULL) + { + if(alloc_vein_buffer_callback == NULL || alloc_spattervein_callback == NULL) + return 0; + + vector veins; + vector spatter_veins; + bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins, 0, &spatter_veins); + + if(result) + { + t_vein* v_buf = NULL; + t_spattervein* sv_buf = NULL; + + if(veins.size() > 0) + { + ((*alloc_vein_buffer_callback)(v_buf, veins.size())); + + copy(veins.begin(), veins.end(), v_buf); + } + + if(spatter_veins.size() > 0) + { + ((*alloc_spattervein_callback)(sv_buf, spatter_veins.size())); + + copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); + } + + return 1; + } + else + return 0; + } + + return -1; +} + #ifdef __cplusplus } #endif From 9ef7a74a445701840d19001c07ee22c00c678eb0 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Jul 2010 13:19:55 -0500 Subject: [PATCH 3/5] added MapPoint class --- library/python/pydfhack/maps.py | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/library/python/pydfhack/maps.py b/library/python/pydfhack/maps.py index f5f69cfb5..c889719e8 100644 --- a/library/python/pydfhack/maps.py +++ b/library/python/pydfhack/maps.py @@ -2,6 +2,9 @@ from ctypes import * from pydftypes import * from util import _uintify, uint_ptr +_MAX_DIM = 0x300 +_MAX_DIM_SQR = _MAX_DIM * _MAX_DIM + 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) ] @@ -161,3 +164,63 @@ class Maps(object): return (int(x.value), int(y.value), int(z.value)) else: return (-1, -1, -1) + +class MapPoint(object): + __slots__ = ["_x", "_y", "_z", "_cmp_val"] + + def __init__(self, x = 0, y = 0, z = 0): + self._x, self._y, self._z = x, y, z + + self._cmp_val = self._get_cmp_value() + + def _val_set(self, which, val): + if which == 0: + self._x = val + elif which == 1: + self._y = val + elif which == 2: + self._z = val + + self._cmp_val = self._get_cmp_value() + + x = property(fget = lambda self: self._x, fset = lambda self, v: self._val_set(0, v)) + y = property(fget = lambda self: self._y, fset = lambda self, v: self._val_set(1, v)) + z = property(fget = lambda self: self._z, fset = lambda self, v: self._val_set(2, v)) + + def _get_cmp_value(self): + return (self.z * _MAX_DIM_SQR) + (self.y * _MAX_DIM) + self.x + + #comparison operators + def __eq__(self, other): + return self.x == other.x and self.y == other.y and self.z == other.z + + def __ne__(self, other): + return not self == other + + def __lt__(self, other): + return self._cmp_val < other._cmp_val + + def __le__(self, other): + return self < other or self == other + + def __gt__(self, other): + return self._cmp_val > other._cmp_val + + def __ge__(self, other): + return self > other or self == other + + #arithmetic operators + def __add__(self, num): + return MapPoint(self.x, self.y, self.z + num) + + def __sub__(self, num): + return MapPoint(self.x, self.y, self.z - num) + + def __div__(self, num): + return MapPoint(self.x / num, self.y / num, self.z) + + def __mul__(self, num): + return MapPoint(self.x * num, self.y * num, self.z) + + def __mod__(self, num): + return MapPoint(self.x % num, self.y % num, self.z) \ No newline at end of file From e7751f8fd93d1cd7ce45b6d2f3543bd439604933 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Jul 2010 13:45:15 -0500 Subject: [PATCH 4/5] changed vein readers to return a pointer to a buffer of just one kind of vein at a time --- library/include/dfhack-c/DFTypes_C.h | 4 +- library/include/dfhack-c/modules/Maps_C.h | 7 +- library/modules/Maps_C.cpp | 102 ++++------------------ 3 files changed, 24 insertions(+), 89 deletions(-) diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index 4e4817875..8346dc5ff 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -102,8 +102,8 @@ DFHACK_EXPORT extern int (*alloc_creaturetype_callback)(c_creaturetype*, const c DFHACK_EXPORT extern int (*alloc_creaturetype_buffer_callback)(c_creaturetype*, uint32_t); DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_frozenliquidvein_callback)(t_frozenliquidvein*, uint32_t); -DFHACK_EXPORT extern int (*alloc_spattervein_callback)(t_spattervein*, uint32_t); +DFHACK_EXPORT extern int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein*, uint32_t); +DFHACK_EXPORT extern int (*alloc_spattervein_buffer_callback)(t_spattervein*, uint32_t); #ifdef __cplusplus } diff --git a/library/include/dfhack-c/modules/Maps_C.h b/library/include/dfhack-c/modules/Maps_C.h index 5cebde9ea..d1bad823e 100644 --- a/library/include/dfhack-c/modules/Maps_C.h +++ b/library/include/dfhack-c/modules/Maps_C.h @@ -69,10 +69,9 @@ DFHACK_EXPORT int Maps_WriteBlockFlags(DFHackObject* maps, uint32_t x, uint32_t DFHACK_EXPORT int Maps_ReadRegionOffsets(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, biome_indices40d* buffer); -DFHACK_EXPORT int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer, t_frozenliquidvein* frozenvein_buffer, t_spattervein* spattervein_buffer); -DFHACK_EXPORT int Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer); -DFHACK_EXPORT int Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_frozenliquidvein* frozenvein_buffer); -DFHACK_EXPORT int Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_spattervein* spattervein_buffer); +DFHACK_EXPORT t_vein* Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); +DFHACK_EXPORT t_frozenliquidvein* Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); +DFHACK_EXPORT t_spattervein* Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z); #ifdef __cplusplus } diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index aacf24111..0700e0a9a 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -274,60 +274,12 @@ int Maps_ReadRegionOffsets(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t return -1; } -int Maps_ReadAllVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer, t_frozenliquidvein* frozenvein_buffer, t_spattervein* spattervein_buffer) -{ - if(maps != NULL) - { - if(alloc_vein_buffer_callback == NULL || alloc_frozenliquidvein_callback == NULL || alloc_spattervein_callback == NULL) - return 0; - - vector veins; - vector frozen_veins; - vector spatter_veins; - bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins, &frozen_veins, &spatter_veins); - - if(result) - { - t_vein* v_buf = NULL; - t_frozenliquidvein* fv_buf = NULL; - t_spattervein* sv_buf = NULL; - - if(veins.size() > 0) - { - ((*alloc_vein_buffer_callback)(v_buf, veins.size())); - - copy(veins.begin(), veins.end(), v_buf); - } - - if(frozen_veins.size() > 0) - { - ((*alloc_frozenliquidvein_callback)(fv_buf, frozen_veins.size())); - - copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); - } - - if(spatter_veins.size() > 0) - { - ((*alloc_spattervein_callback)(sv_buf, spatter_veins.size())); - - copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); - } - - return 1; - } - else - return 0; - } - - return -1; -} - -int Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_vein* vein_buffer) +t_vein* Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z) { if(maps != NULL) { if(alloc_vein_buffer_callback == NULL) - return 0; + return NULL; vector veins; bool result = ((DFHack::Maps*)maps)->ReadVeins(x, y, z, &veins); @@ -343,21 +295,21 @@ int Maps_ReadStandardVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t copy(veins.begin(), veins.end(), v_buf); } - return 1; + return v_buf; } else - return 0; + return NULL; } - return -1; + return NULL; } -int Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_frozenliquidvein* frozenvein_buffer) +t_frozenliquidvein* Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z) { if(maps != NULL) { - if(alloc_vein_buffer_callback == NULL || alloc_frozenliquidvein_callback == NULL) - return 0; + if(alloc_frozenliquidvein_callback == NULL) + return NULL; vector veins; vector frozen_veins; @@ -365,38 +317,30 @@ int Maps_ReadFrozenVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, if(result) { - t_vein* v_buf = NULL; t_frozenliquidvein* fv_buf = NULL; - if(veins.size() > 0) - { - ((*alloc_vein_buffer_callback)(v_buf, veins.size())); - - copy(veins.begin(), veins.end(), v_buf); - } - if(frozen_veins.size() > 0) { - ((*alloc_frozenliquidvein_callback)(fv_buf, frozen_veins.size())); + ((*alloc_frozenliquidvein_buffer_callback)(fv_buf, frozen_veins.size())); copy(frozen_veins.begin(), frozen_veins.end(), fv_buf); } - return 1; + return fv_buf; } else - return 0; + return NULL; } - return -1; + return NULL; } -int Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z, t_spattervein* spattervein_buffer) +t_spattervein* Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z) { if(maps != NULL) { - if(alloc_vein_buffer_callback == NULL || alloc_spattervein_callback == NULL) - return 0; + if(alloc_spattervein_callback == NULL) + return NULL; vector veins; vector spatter_veins; @@ -404,30 +348,22 @@ int Maps_ReadSpatterVeins(DFHackObject* maps, uint32_t x, uint32_t y, uint32_t z if(result) { - t_vein* v_buf = NULL; t_spattervein* sv_buf = NULL; - if(veins.size() > 0) - { - ((*alloc_vein_buffer_callback)(v_buf, veins.size())); - - copy(veins.begin(), veins.end(), v_buf); - } - if(spatter_veins.size() > 0) { - ((*alloc_spattervein_callback)(sv_buf, spatter_veins.size())); + ((*alloc_spattervein_buffer_callback)(sv_buf, spatter_veins.size())); copy(spatter_veins.begin(), spatter_veins.end(), sv_buf); } - return 1; + return sv_buf; } else - return 0; + return NULL; } - return -1; + return NULL; } #ifdef __cplusplus From a8b5c461fea689cbef2cf78f0f5764e396ee162f Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 6 Jul 2010 14:07:10 -0500 Subject: [PATCH 5/5] added terrain checkers --- library/DFTypes_C.cpp | 34 ++++++++++++++++++++++++++++ library/include/dfhack-c/DFTypes_C.h | 7 ++++++ 2 files changed, 41 insertions(+) diff --git a/library/DFTypes_C.cpp b/library/DFTypes_C.cpp index 6d3f8d789..b8f0a6a9b 100644 --- a/library/DFTypes_C.cpp +++ b/library/DFTypes_C.cpp @@ -67,6 +67,40 @@ int (*alloc_empty_creaturetype_callback)(c_creaturetype*) = NULL; int (*alloc_creaturetype_callback)(c_creaturetype*, const char*, uint32_t, uint32_t, uint8_t, uint16_t, uint16_t, uint16_t) = NULL; int (*alloc_creaturetype_buffer_callback)(c_creaturetype*, uint32_t) = NULL; +int (*alloc_vein_buffer_callback)(t_vein*, uint32_t) = NULL; +int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein*, uint32_t) = NULL; +int (*alloc_spattervein_buffer_callback)(t_spattervein*, uint32_t) = NULL; + +int DFHack_isWallTerrain(int in) +{ + return DFHack::isWallTerrain(in); +} + +int DFHack_isFloorTerrain(int in) +{ + return DFHack::isFloorTerrain(in); +} + +int DFHack_isRampTerrain(int in) +{ + return DFHack::isRampTerrain(in); +} + +int DFHack_isStairTerrain(int in) +{ + return DFHack::isStairTerrain(in); +} + +int DFHack_isOpenTerrain(int in) +{ + return DFHack::isOpenTerrain(in); +} + +int DFHack_getVegetationType(int in) +{ + return DFHack::getVegetationType(in); +} + #ifdef __cplusplus } #endif diff --git a/library/include/dfhack-c/DFTypes_C.h b/library/include/dfhack-c/DFTypes_C.h index 8346dc5ff..fbd883ead 100644 --- a/library/include/dfhack-c/DFTypes_C.h +++ b/library/include/dfhack-c/DFTypes_C.h @@ -105,6 +105,13 @@ DFHACK_EXPORT extern int (*alloc_vein_buffer_callback)(t_vein*, uint32_t); DFHACK_EXPORT extern int (*alloc_frozenliquidvein_buffer_callback)(t_frozenliquidvein*, uint32_t); DFHACK_EXPORT extern int (*alloc_spattervein_buffer_callback)(t_spattervein*, uint32_t); +DFHACK_EXPORT extern int DFHack_isWallTerrain(int in); +DFHACK_EXPORT extern int DFHack_isFloorTerrain(int in); +DFHACK_EXPORT extern int DFHack_isRampTerrain(int in); +DFHACK_EXPORT extern int DFHack_isStairTerrain(int in); +DFHACK_EXPORT extern int DFHack_isOpenTerrain(int in); +DFHACK_EXPORT extern int DFHack_getVegetationType(int in); + #ifdef __cplusplus } #endif