forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdig.cpp
More file actions
377 lines (354 loc) · 9.49 KB
/
Copy pathvdig.cpp
File metadata and controls
377 lines (354 loc) · 9.49 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <iostream>
#include <integers.h>
#include <string.h> // for memset
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <stdio.h>
using namespace std;
#include <DFTypes.h>
#include <DFTileTypes.h>
#include <DFHackAPI.h>
#include <modules/Maps.h>
#include <modules/Position.h>
#include <modules/Materials.h>
#include <DFTileTypes.h>
#define MAX_DIM 0x300
class Point
{
public:
Point(uint32_t x, uint32_t y)
{
this->x = x;
this->y = y;
}
bool operator==(const Point &other) const
{
return (other.x == x && other.y == y);
}
bool operator<(const Point &other) const
{
return ( (y*MAX_DIM + x) < (other.y*MAX_DIM + other.x));
}
Point operator/(int number) const
{
return Point(x/number, y/number);
}
Point operator%(int number) const
{
return Point(x%number, y%number);
}
uint32_t x;
uint32_t y;
};
class Block
{
public:
Block(DFHack::Maps *_m, uint32_t x_, uint32_t y_, uint32_t z_)
{
vector <DFHack::t_vein> veins;
m = _m;
dirty = false;
valid = false;
x = x_;
y = y_;
z = z_;
if(m->ReadBlock40d(x_,y_,z_,&raw))
{
memset(materials,-1,sizeof(materials));
memset(bitmap,0,sizeof(bitmap));
m->ReadVeins(x,y,z,&veins);
// for each vein
for(int i = 0; i < (int)veins.size();i++)
{
//iterate through vein rows
for(uint32_t j = 0;j<16;j++)
{
//iterate through the bits
for (uint32_t k = 0; k< 16;k++)
{
// check if it's really a vein (FIXME: doing this too many times)
int16_t tt = raw.tiletypes[k][j];
if(DFHack::isWallTerrain(tt) && DFHack::tileTypeTable[tt].m == DFHack::VEIN)
{
// and the bit array with a one-bit mask, check if the bit is set
bool set = !!(((1 << k) & veins[i].assignment[j]) >> k);
if(set)
{
// store matgloss
materials[k][j] = veins[i].type;
}
}
}
}
}
valid = true;
}
}
int16_t MaterialAt(Point p)
{
return materials[p.x][p.y];
}
void ClearMaterialAt(Point p)
{
materials[p.x][p.y] = -1;
}
int16_t TileTypeAt(Point p)
{
return raw.tiletypes[p.x][p.y];
}
DFHack::t_designation DesignationAt(Point p)
{
return raw.designation[p.x][p.y];
}
bool setDesignationAt(Point p, DFHack::t_designation des)
{
if(!valid) return false;
dirty = true;
//printf("setting block %d/%d/%d , %d %d\n",x,y,z, p.x, p.y);
raw.designation[p.x][p.y] = des;
return true;
}
bool WriteDesignations ()
{
if(!valid) return false;
if(dirty)
{
//printf("writing %d/%d/%d\n",x,y,z);
m->WriteDesignations(x,y,z, &raw.designation);
m->WriteDirtyBit(x,y,z,true);
}
return true;
}
bool valid;
bool dirty;
DFHack::Maps * m;
DFHack::mapblock40d raw;
uint32_t x;
uint32_t y;
uint32_t z;
int16_t materials[16][16];
int8_t bitmap[16][16];
};
class Layer
{
public:
Layer(DFHack::Maps * Maps, uint32_t _z)
{
valid = 0;
this->Maps = Maps;
z = _z;
uint32_t z_max;
Maps->getSize(x_bmax, y_bmax, z_max);
if(z < z_max)
valid = true;
};
~Layer()
{
map<Point, Block *>::iterator p;
for(p = blocks.begin(); p != blocks.end(); p++)
{
delete p->second;
//cout << stonetypes[p->first].id << " : " << p->second << endl;
}
}
bool isValid ()
{
return valid;
}
Block * BlockAt (Point blockcoord)
{
if(!valid) return 0;
map <Point, Block*>::iterator iter = blocks.find(blockcoord);
if(iter != blocks.end())
{
return (*iter).second;
}
else
{
Block * nblo = new Block(Maps,blockcoord.x,blockcoord.y,z);
blocks[blockcoord] = nblo;
return nblo;
}
}
uint16_t tiletypeAt (Point tilecoord)
{
Block * b= BlockAt(tilecoord / 16);
if(b && b->valid)
{
return b->TileTypeAt(tilecoord % 16);
}
return 0;
}
int16_t materialAt (Point tilecoord)
{
Block * b= BlockAt(tilecoord / 16);
if(b && b->valid)
{
return b->MaterialAt(tilecoord % 16);
}
return 0;
}
bool clearMaterialAt (Point tilecoord)
{
Block * b= BlockAt(tilecoord / 16);
if(b && b->valid)
{
b->ClearMaterialAt(tilecoord % 16);
}
return 0;
}
DFHack::t_designation designationAt (Point tilecoord)
{
Block * b= BlockAt(tilecoord / 16);
if(b && b->valid)
{
return b->DesignationAt(tilecoord % 16);
}
DFHack:: t_designation temp;
temp.whole = 0;
return temp;
}
bool setDesignationAt (Point tilecoord, DFHack::t_designation des)
{
Block * b= BlockAt(tilecoord / 16);
if(b && b->valid)
{
b->setDesignationAt(tilecoord % 16, des);
return true;
}
return false;
}
bool WriteAll()
{
map<Point, Block *>::iterator p;
for(p = blocks.begin(); p != blocks.end(); p++)
{
p->second->WriteDesignations();
//cout << stonetypes[p->first].id << " : " << p->second << endl;
}
return true;
}
private:
bool valid;
uint32_t z;
uint32_t x_bmax;
uint32_t y_bmax;
uint32_t x_tmax;
uint32_t y_tmax;
DFHack::Maps * Maps;
map<Point, Block *> blocks;
};
int main (int argc, const char* argv[])
{
DFHack::API DF("Memory.xml");
try
{
DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
uint32_t x_max,y_max,z_max;
DFHack::Maps * Maps = DF.getMaps();
DFHack::Materials * Mats = DF.getMaterials();
DFHack::Position * Pos = DF.getPosition();
// init the map
if(!Maps->Start())
{
cerr << "Can't init map. Make sure you have a map loaded in DF." << endl;
DF.Detach();
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
int32_t cx, cy, cz;
Maps->getSize(x_max,y_max,z_max);
Pos->getCursorCoords(cx,cy,cz);
while(cx == -30000)
{
cerr << "Cursor is not active. Point the cursor at a vein." << endl;
DF.Resume();
cin.ignore();
DF.Suspend();
Pos->getCursorCoords(cx,cy,cz);
}
Layer * L = new Layer(Maps,cz);
Point xy ((uint32_t)cx,(uint32_t)cy);
DFHack::t_designation des = L->designationAt(xy);
int16_t tt = L->tiletypeAt(xy);
int16_t veinmat = L->materialAt(xy);
if( veinmat == -1 )
{
cerr << "This tile is non-vein. Bye :)" << endl;
delete L;
DF.Detach();
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
printf("%d/%d/%d tiletype: %d, veinmat: %d, designation: 0x%x ... DIGGING!\n", cx,cy,cz, tt, veinmat, des.whole);
stack <Point> flood;
flood.push(xy);
uint32_t tx_max = x_max * 16;
uint32_t ty_max = y_max * 16;
while( !flood.empty() )
{
Point current = flood.top();
flood.pop();
int16_t vmat2 = L->materialAt(current);
if(vmat2!=veinmat)
continue;
// found a good tile, dig+unset material
DFHack::t_designation des = L->designationAt(current);
des.bits.dig = DFHack::designation_default;
if(L->setDesignationAt(current,des))
{
L->clearMaterialAt(current);
if(current.x < tx_max)
{
flood.push(Point(current.x + 1, current.y));
if(current.y < ty_max)
{
flood.push(Point(current.x + 1, current.y + 1));
flood.push(Point(current.x, current.y + 1));
}
if(current.y != 0)
{
flood.push(Point(current.x + 1, current.y - 1));
flood.push(Point(current.x, current.y - 1));
}
}
if(current.x != 0)
{
flood.push(Point(current.x - 1, current.y));
if(current.y < ty_max)
{
flood.push(Point(current.x - 1, current.y + 1));
flood.push(Point(current.x, current.y + 1));
}
if(current.y != 0)
{
flood.push(Point(current.x - 1, current.y - 1));
flood.push(Point(current.x, current.y - 1));
}
}
}
}
L->WriteAll();
delete L;
DF.Detach();
#ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl;
cin.ignore();
#endif
return 0;
}