forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.py
More file actions
291 lines (200 loc) · 9.73 KB
/
Copy pathmaps.py
File metadata and controls
291 lines (200 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from ctypes import *
from dftypes import *
from util import _uintify, int_ptr, uint_ptr, check_pointer_cache
_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_getPosition.argtypes = [ c_void_p, int_ptr, int_ptr, int_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) ]
libdfhack.Maps_ReadVegetation.restype = c_void_p
libdfhack.Maps_ReadStandardVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadFrozenVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadSpatterVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadGrassVeins.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadWorldConstructions.argtypes = [ c_void_p, c_uint, c_uint, c_uint ]
libdfhack.Maps_ReadStandardVeins.restype = c_void_p
libdfhack.Maps_ReadFrozenVeins.restype = c_void_p
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
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, byref(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, byref(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_occupancy(self, x, y, z, o):
ux, uy, uz = _uintify(x, y, z)
return libdfhack.Maps_WriteDesignations(self._map_ptr, ux, uy, uz, byref(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
def read_veins(self, x, y, z):
ux, uy, uz = _uintify(x, y, z)
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)
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)
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)
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)
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)
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 = check_pointer_cache(f, False)
if f_arr is not None:
for node in f_arr:
c = node.coordinate.xyz
coord = MapPoint(c.x, c.y, c.z)
f_list = [node.features[i] for i in xrange(node.feature_length)]
feature_dict[coord] = f_list
return feature_dict
@property
def size(self):
x, y, z = (c_uint(0), c_uint(0), c_uint(0))
retval = libdfhack.Maps_getSize(self._map_ptr, byref(x), byref(y), byref(z))
return (int(x.value), int(y.value), int(z.value))
@property
def position(self):
x, y, z = (c_int(0), c_int(0), c_int(0))
libdfhack.Maps_getPosition(self._map_ptr, byref(x), byref(y), byref(z))
return (int(x.value), int(y.value), int(z.value))
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)