forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe.cpp
479 lines (434 loc) · 16.3 KB
/
probe.cpp
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Just show some position data
#include <climits>
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#include "Console.h"
#include "Core.h"
#include "Export.h"
#include "MiscUtils.h"
#include "PluginManager.h"
#include "modules/Buildings.h"
#include "modules/Gui.h"
#include "modules/MapCache.h"
#include "modules/Maps.h"
#include "modules/Materials.h"
#include "modules/Units.h"
#include "df/block_square_event_grassst.h"
#include "df/block_square_event_world_constructionst.h"
#include "df/building_def.h"
#include "df/building_nest_boxst.h"
#include "df/region_map_entry.h"
#include "df/unit_inventory_item.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/world_raws.h"
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("probe");
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(cursor);
command_result df_probe (color_ostream &out, vector <string> & parameters);
command_result df_cprobe (color_ostream &out, vector <string> & parameters);
command_result df_bprobe (color_ostream &out, vector <string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand("probe",
"A tile probe",
df_probe,
false,
"Hover the cursor over a tile to view its properties.\n"));
commands.push_back(PluginCommand("cprobe",
"A creature probe",
df_cprobe,
false,
"Select a creature to view its properties.\n"));
commands.push_back(PluginCommand("bprobe",
"A simple building probe",
df_bprobe,
false,
"Select a building to view its properties.\n"));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
command_result df_cprobe (color_ostream &out, vector <string> & parameters)
{
CoreSuspender suspend;
int32_t cursorX, cursorY, cursorZ;
Gui::getCursorCoords(cursorX,cursorY,cursorZ);
if(cursorX == -30000)
{
out.printerr("No cursor; place cursor over creature to probe.\n");
}
else
{
for(size_t i = 0; i < world->units.all.size(); i++)
{
df::unit * unit = world->units.all[i];
if(unit->pos.x == cursorX && unit->pos.y == cursorY && unit->pos.z == cursorZ)
{
out.print("Creature %d, race %d (%x), civ %d (%x)\n", unit->id, unit->race, unit->race, unit->civ_id, unit->civ_id);
for(size_t j=0; j<unit->inventory.size(); j++)
{
df::unit_inventory_item* inv_item = unit->inventory[j];
df::item* item = inv_item->item;
if(inv_item->mode == df::unit_inventory_item::T_mode::Worn)
{
out << " wears item: #" << item->id;
if(item->flags.bits.owned)
out << " (owned)";
else
out << " (not owned)";
if(item->getEffectiveArmorLevel() != 0)
out << ", armor";
out << endl;
}
}
// don't leave loop, there may be more than 1 creature at the cursor position
//break;
}
}
}
return CR_OK;
}
void describeTile(color_ostream &out, df::tiletype tiletype)
{
out.print("%d", tiletype);
if(tileName(tiletype))
out.print(" = %s",tileName(tiletype));
out.print(" (%s)", ENUM_KEY_STR(tiletype, tiletype).c_str());
out.print("\n");
df::tiletype_shape shape = tileShape(tiletype);
df::tiletype_material material = tileMaterial(tiletype);
df::tiletype_special special = tileSpecial(tiletype);
df::tiletype_variant variant = tileVariant(tiletype);
out.print("%-10s: %4d %s\n","Class" ,shape,
ENUM_KEY_STR(tiletype_shape, shape).c_str());
out.print("%-10s: %4d %s\n","Material" ,
material, ENUM_KEY_STR(tiletype_material, material).c_str());
out.print("%-10s: %4d %s\n","Special" ,
special, ENUM_KEY_STR(tiletype_special, special).c_str());
out.print("%-10s: %4d %s\n" ,"Variant" ,
variant, ENUM_KEY_STR(tiletype_variant, variant).c_str());
out.print("%-10s: %s\n" ,"Direction",
tileDirection(tiletype).getStr());
out.print("\n");
}
command_result df_probe (color_ostream &out, vector <string> & parameters)
{
//bool showBlock, showDesig, showOccup, showTile, showMisc;
/*
if (!parseOptions(parameters, showBlock, showDesig, showOccup,
showTile, showMisc))
{
out.printerr("Unknown parameters!\n");
return CR_FAILURE;
}
*/
CoreSuspender suspend;
DFHack::Materials *Materials = Core::getInstance().getMaterials();
std::vector<t_matglossInorganic> inorganic;
bool hasmats = Materials->CopyInorganicMaterials(inorganic);
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
MapExtras::MapCache mc;
int32_t regionX, regionY, regionZ;
Maps::getPosition(regionX,regionY,regionZ);
int32_t cursorX, cursorY, cursorZ;
Gui::getCursorCoords(cursorX,cursorY,cursorZ);
if(cursorX == -30000)
{
out.printerr("No cursor; place cursor over tile to probe.\n");
return CR_FAILURE;
}
DFCoord cursor (cursorX,cursorY,cursorZ);
uint32_t blockX = cursorX / 16;
uint32_t tileX = cursorX % 16;
uint32_t blockY = cursorY / 16;
uint32_t tileY = cursorY % 16;
MapExtras::Block * b = mc.BlockAt(cursor/16);
if(!b || !b->is_valid())
{
out.printerr("No data.\n");
return CR_OK;
}
auto &block = *b->getRaw();
out.print("block addr: 0x%p\n\n", &block);
/*
if (showBlock)
{
out.print("block flags:\n");
print_bits<uint32_t>(block.blockflags.whole,out);
out.print("\n\n");
}
*/
df::tiletype tiletype = mc.tiletypeAt(cursor);
df::tile_designation &des = block.designation[tileX][tileY];
df::tile_occupancy &occ = block.occupancy[tileX][tileY];
uint8_t fog_of_war = block.fog_of_war[tileX][tileY];
/*
if(showDesig)
{
out.print("designation\n");
print_bits<uint32_t>(block.designation[tileX][tileY].whole,
out);
out.print("\n\n");
}
if(showOccup)
{
out.print("occupancy\n");
print_bits<uint32_t>(block.occupancy[tileX][tileY].whole,
out);
out.print("\n\n");
}
*/
// tiletype
out.print("tiletype: ");
describeTile(out, tiletype);
out.print("static: ");
describeTile(out, mc.staticTiletypeAt(cursor));
out.print("base: ");
describeTile(out, mc.baseTiletypeAt(cursor));
out.print("temperature1: %d U\n",mc.temperature1At(cursor));
out.print("temperature2: %d U\n",mc.temperature2At(cursor));
int offset = block.region_offset[des.bits.biome];
int bx = clip_range(block.region_pos.x + (offset % 3) - 1, 0, world->world_data->world_width-1);
int by = clip_range(block.region_pos.y + (offset / 3) - 1, 0, world->world_data->world_height-1);
auto biome = &world->world_data->region_map[bx][by];
int sav = biome->savagery;
int evi = biome->evilness;
int sindex = sav > 65 ? 2 : sav < 33 ? 0 : 1;
int eindex = evi > 65 ? 2 : evi < 33 ? 0 : 1;
int surr = sindex + eindex * 3;
const char* surroundings[] = { "Serene", "Mirthful", "Joyous Wilds", "Calm", "Wilderness", "Untamed Wilds", "Sinister", "Haunted", "Terrifying" };
// biome, geolayer
out << "biome: " << des.bits.biome << " (" <<
"region id=" << biome->region_id << ", " <<
surroundings[surr] << ", " <<
"savagery " << biome->savagery << ", " <<
"evilness " << biome->evilness << ")" << std::endl;
out << "geolayer: " << des.bits.geolayer_index
<< std::endl;
int16_t base_rock = mc.layerMaterialAt(cursor);
if(base_rock != -1)
{
out << "Layer material: " << dec << base_rock;
if(hasmats)
out << " / " << inorganic[base_rock].id
<< " / "
<< inorganic[base_rock].name
<< endl;
else
out << endl;
}
int16_t vein_rock = mc.veinMaterialAt(cursor);
if(vein_rock != -1)
{
out << "Vein material (final): " << dec << vein_rock;
if(hasmats)
out << " / " << inorganic[vein_rock].id
<< " / "
<< inorganic[vein_rock].name
<< " ("
<< ENUM_KEY_STR(inclusion_type,b->veinTypeAt(cursor))
<< ")"
<< endl;
else
out << endl;
}
MaterialInfo minfo(mc.baseMaterialAt(cursor));
if (minfo.isValid())
out << "Base material: " << minfo.getToken() << " / " << minfo.toString() << endl;
minfo.decode(mc.staticMaterialAt(cursor));
if (minfo.isValid())
out << "Static material: " << minfo.getToken() << " / " << minfo.toString() << endl;
// liquids
if(des.bits.flow_size)
{
if(des.bits.liquid_type == tile_liquid::Magma)
out <<"magma: ";
else out <<"water: ";
out << des.bits.flow_size << std::endl;
}
if(des.bits.flow_forbid)
out << "flow forbid" << std::endl;
if(des.bits.pile)
out << "stockpile?" << std::endl;
if(des.bits.rained)
out << "rained?" << std::endl;
if(des.bits.smooth)
out << "smooth?" << std::endl;
if(des.bits.water_salt)
out << "salty" << endl;
if(des.bits.water_stagnant)
out << "stagnant" << endl;
out.print("%-16s= %s\n", "dig", ENUM_KEY_STR(tile_dig_designation, des.bits.dig).c_str());
out.print("%-16s= %s\n", "traffic", ENUM_KEY_STR(tile_traffic, des.bits.traffic).c_str());
#define PRINT_FLAG( FIELD, BIT ) out.print("%-16s= %c\n", #BIT , ( FIELD.bits.BIT ? 'Y' : ' ' ) )
PRINT_FLAG( des, hidden );
PRINT_FLAG( des, light );
PRINT_FLAG( des, outside );
PRINT_FLAG( des, subterranean );
PRINT_FLAG( des, water_table );
PRINT_FLAG( des, rained );
PRINT_FLAG( occ, monster_lair);
out.print("%-16s= %d\n", "fog_of_war", fog_of_war);
df::coord2d pc(blockX, blockY);
t_feature local;
t_feature global;
Maps::ReadFeatures(&block,&local,&global);
PRINT_FLAG( des, feature_local );
if(local.type != -1)
{
out.print("%-16s", "");
out.print(" %4d", block.local_feature);
out.print(" (%2d)", local.type);
out.print(" addr 0x%p ", local.origin);
out.print(" %s\n", sa_feature(local.type));
}
PRINT_FLAG( des, feature_global );
if(global.type != -1)
{
out.print("%-16s", "");
out.print(" %4d", block.global_feature);
out.print(" (%2d)", global.type);
out.print(" %s\n", sa_feature(global.type));
}
#undef PRINT_FLAG
out << "local feature idx: " << block.local_feature
<< endl;
out << "global feature idx: " << block.global_feature
<< endl;
out << std::endl;
if(block.occupancy[tileX][tileY].bits.no_grow)
out << "no grow" << endl;
for(size_t e=0; e<block.block_events.size(); e++)
{
df::block_square_event * blev = block.block_events[e];
df::block_square_event_type blevtype = blev->getType();
switch(blevtype)
{
case df::block_square_event_type::grass:
{
df::block_square_event_grassst * gr_ev = (df::block_square_event_grassst *)blev;
if(gr_ev->amount[tileX][tileY] > 0)
{
out << "amount of grass: " << (int)gr_ev->amount[tileX][tileY] << endl;
}
break;
}
case df::block_square_event_type::world_construction:
{
df::block_square_event_world_constructionst * co_ev = (df::block_square_event_world_constructionst*)blev;
uint16_t bits = co_ev->tile_bitmask[tileY];
out << "construction bits: " << bits << endl;
break;
}
default:
//out << "unhandled block event type!" << endl;
break;
}
}
return CR_OK;
}
command_result df_bprobe (color_ostream &out, vector <string> & parameters)
{
CoreSuspender suspend;
if(cursor->x == -30000)
{
out.printerr("No cursor; place cursor over tile to probe.\n");
return CR_FAILURE;
}
for (size_t i = 0; i < world->buildings.all.size(); i++)
{
Buildings::t_building building;
if (!Buildings::Read(i, building))
continue;
if (int32_t(building.x1) > cursor->x || cursor->x > int32_t(building.x2) ||
int32_t(building.y1) > cursor->y || cursor->y > int32_t(building.y2) ||
int32_t(building.z) != cursor->z)
continue;
string name;
building.origin->getName(&name);
out.print("Building %i - \"%s\" - type %s (%i)",
building.origin->id,
name.c_str(),
ENUM_KEY_STR(building_type, building.type).c_str(),
building.type);
switch (building.type)
{
case building_type::Civzone:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(civzone_type, building.civzone_type).c_str(),
building.civzone_type);
break;
case building_type::Furnace:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(furnace_type, building.furnace_type).c_str(),
building.furnace_type);
if (building.furnace_type == furnace_type::Custom)
out.print(", custom type %s (%i)",
world->raws.buildings.all[building.custom_type]->code.c_str(),
building.custom_type);
break;
case building_type::Workshop:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(workshop_type, building.workshop_type).c_str(),
building.workshop_type);
if (building.workshop_type == workshop_type::Custom)
out.print(", custom type %s (%i)",
world->raws.buildings.all[building.custom_type]->code.c_str(),
building.custom_type);
break;
case building_type::Construction:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(construction_type, building.construction_type).c_str(),
building.construction_type);
break;
case building_type::Shop:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(shop_type, building.shop_type).c_str(),
building.shop_type);
break;
case building_type::SiegeEngine:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(siegeengine_type, building.siegeengine_type).c_str(),
building.siegeengine_type);
break;
case building_type::Trap:
out.print(", subtype %s (%i)",
ENUM_KEY_STR(trap_type, building.trap_type).c_str(),
building.trap_type);
break;
case building_type::NestBox:
{
df::building_nest_boxst* nestbox = (df::building_nest_boxst*) building.origin;
out.print(", claimed:(%i), items:%zu", nestbox->claimed_by, nestbox->contained_items.size());
break;
}
default:
if (building.subtype != -1)
out.print(", subtype %i", building.subtype);
break;
}
if(building.origin->is_room) //isRoom())
out << ", room";
if(building.origin->getBuildStage()!=building.origin->getMaxBuildStage())
out << ", in construction";
out.print("\n");
}
return CR_OK;
}