-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathdiggingInvaders.cpp
More file actions
612 lines (553 loc) · 21.3 KB
/
diggingInvaders.cpp
File metadata and controls
612 lines (553 loc) · 21.3 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include "assignJob.h"
#include "edgeCost.h"
#include "Console.h"
#include "DataDefs.h"
#include "Export.h"
#include "PluginManager.h"
#include "Types.h"
#include "modules/Buildings.h"
#include "modules/EventManager.h"
#include "modules/Gui.h"
#include "modules/Job.h"
#include "modules/Maps.h"
#include "modules/MapCache.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "df/body_part_raw_flags.h"
#include "df/building.h"
#include "df/building_type.h"
#include "df/caste_body_info.h"
#include "df/coord.h"
#include "df/creature_raw.h"
#include "df/general_ref.h"
#include "df/general_ref_building_holderst.h"
#include "df/general_ref_unit.h"
#include "df/general_ref_unit_holderst.h"
#include "df/general_ref_unit_workerst.h"
#include "df/global_objects.h"
#include "df/invasion_info.h"
#include "df/item.h"
#include "df/itemdef_weaponst.h"
#include "df/item_quality.h"
#include "df/item_weaponst.h"
#include "df/inorganic_raw.h"
#include "df/job.h"
#include "df/job_list_link.h"
#include "df/job_skill.h"
#include "df/job_type.h"
#include "df/map_block.h"
#include "df/strain_type.h"
#include "df/tile_building_occ.h"
#include "df/tile_occupancy.h"
#include "df/tiletype.h"
#include "df/tiletype_material.h"
#include "df/tiletype_shape.h"
#include "df/tiletype_shape_basic.h"
#include "df/plotinfost.h"
#include "df/unit.h"
#include "df/unit_inventory_item.h"
#include "df/world.h"
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cinttypes>
using namespace std;
using namespace DFHack;
using namespace df::enums;
command_result diggingInvadersCommand(color_ostream &out, std::vector <std::string> & parameters);
void watchForJobComplete(color_ostream& out, void* ptr);
void newInvasionHandler(color_ostream& out, void* ptr);
void clearDijkstra();
void findAndAssignInvasionJob(color_ostream& out, void*);
//int32_t manageInvasion(color_ostream& out);
DFHACK_PLUGIN_IS_ENABLED(enabled);
DFHACK_PLUGIN("diggingInvaders");
REQUIRE_GLOBAL(world);
//TODO: when world unloads
static int32_t lastInvasionJob=-1;
static int32_t lastInvasionDigger = -1;
static int32_t edgesPerTick = 100;
//static EventManager::EventHandler jobCompleteHandler(plugin_self, watchForJobComplete, 5);
static bool activeDigging=false;
static unordered_set<string> diggingRaces;
static unordered_set<int32_t> invaderJobs;
static df::coord lastDebugEdgeCostPoint;
unordered_map<string, DigAbilities> digAbilities;
static cost_t costWeightDefault[] = {
//Distance
1,
//Destroy Building
2,
//Dig
10000,
//DestroyRoughConstruction
1000,
//DestroySmoothConstruction
100,
};
static int32_t jobDelayDefault[] = {
//Distance
-1,
//Destroy Building
1000,
//Dig
1000,
//DestroyRoughConstruction
1000,
//DestroySmoothConstruction
100,
};
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
"diggingInvaders",
"Makes invaders dig to your dwarves.",
diggingInvadersCommand));
//*df::global::debug_showambush = true;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
if ( enabled == enable )
return CR_OK;
enabled = enable;
EventManager::unregisterAll(plugin_self);
clearDijkstra();
lastInvasionJob = lastInvasionDigger = -1;
activeDigging = false;
invaderJobs.clear();
if ( enabled ) {
EventManager::EventHandler handler(plugin_self, newInvasionHandler, 1000);
EventManager::registerListener(EventManager::EventType::INVASION, handler);
findAndAssignInvasionJob(out, (void*)0);
}
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case DFHack::SC_WORLD_LOADED:
//TODO: check game mode
//in case there are invaders when the game is loaded, we check if there's work to be done
activeDigging = enabled;
clearDijkstra();
findAndAssignInvasionJob(out, (void*)0);
break;
case DFHack::SC_WORLD_UNLOADED:
// cleanup
plugin_enable(out, false);
break;
default:
break;
}
return CR_OK;
}
df::coord getRoot(df::coord point, unordered_map<df::coord, df::coord>& rootMap);
class PointComp {
public:
unordered_map<df::coord, cost_t, PointHash> *pointCost;
PointComp(unordered_map<df::coord, cost_t, PointHash> *p): pointCost(p) {
}
int32_t operator()(df::coord p1, df::coord p2) const {
if ( p1 == p2 ) return 0;
auto i1 = pointCost->find(p1);
auto i2 = pointCost->find(p2);
if ( i1 == pointCost->end() && i2 == pointCost->end() )
return p1 < p2;
if ( i1 == pointCost->end() )
return true;
if ( i2 == pointCost->end() )
return false;
cost_t c1 = (*i1).second;
cost_t c2 = (*i2).second;
if ( c1 != c2 )
return c1 < c2;
return p1 < p2;
}
};
//bool important(df::coord pos, map<df::coord, set<Edge> >& edges, df::coord prev, set<df::coord>& importantPoints, set<Edge>& importantEdges);
void newInvasionHandler(color_ostream& out, void* ptr) {
if ( activeDigging )
return;
activeDigging = true;
findAndAssignInvasionJob(out, (void*)0);
}
command_result diggingInvadersCommand(color_ostream& out, std::vector<std::string>& parameters) {
for ( size_t a = 0; a < parameters.size(); a++ ) {
if ( parameters[a] == "1" || parameters[a] == "enable" ) {
plugin_enable(out,true);
} else if ( parameters[a] == "0" || parameters[a] == "disable" ) {
plugin_enable(out,false);
} else if ( parameters[a] == "add" || parameters[a] == "remove" ) {
if ( a+1 >= parameters.size() )
return CR_WRONG_USAGE;
string race = parameters[a+1];
if ( parameters[a] == "add" ) {
diggingRaces.insert(race);
DigAbilities& abilities = digAbilities[race];
memcpy(abilities.costWeight, costWeightDefault, costDim*sizeof(cost_t));
memcpy(abilities.jobDelay, jobDelayDefault, costDim*sizeof(int32_t));
} else {
diggingRaces.erase(race);
digAbilities.erase(race);
}
a++;
} else if ( parameters[a] == "setCost" || parameters[a] == "setDelay" ) {
if ( a+3 >= parameters.size() )
return CR_WRONG_USAGE;
string raceString = parameters[a+1];
if ( digAbilities.find(raceString) == digAbilities.end() ) {
DigAbilities bob;
memset(&bob, 0xFF, sizeof(bob));
digAbilities[raceString] = bob;
}
DigAbilities& abilities = digAbilities[raceString];
string costStr = parameters[a+2];
int32_t costDim = -1;
if ( costStr == "walk" ) {
costDim = CostDimension::Walk;
if ( parameters[a] == "setDelay" )
return CR_WRONG_USAGE;
} else if ( costStr == "destroyBuilding" ) {
costDim = CostDimension::DestroyBuilding;
} else if ( costStr == "dig" ) {
costDim = CostDimension::Dig;
} else if ( costStr == "destroyRoughConstruction" ) {
costDim = CostDimension::DestroyRoughConstruction;
} else if ( costStr == "destroySmoothConstruction" ) {
costDim = CostDimension::DestroySmoothConstruction;
} else {
return CR_WRONG_USAGE;
}
cost_t value;
stringstream asdf(parameters[a+3]);
asdf >> value;
//if ( parameters[a] == "setCost" && value <= 0 )
// return CR_WRONG_USAGE;
if ( parameters[a] == "setCost" ) {
abilities.costWeight[costDim] = value;
} else {
abilities.jobDelay[costDim] = value;
}
a += 3;
} else if ( parameters[a] == "edgeCost" ) {
if ( a+1 >= parameters.size() )
return CR_WRONG_USAGE;
string raceString = parameters[a+1];
if ( digAbilities.find(raceString) == digAbilities.end() ) {
out.print("Race {} does not have dig abilities assigned.\n", raceString);
return CR_WRONG_USAGE;
}
DigAbilities& abilities = digAbilities[raceString];
df::coord bob = Gui::getCursorPos();
out.print("({},{},{}), ({},{},{}): cost = {}\n", lastDebugEdgeCostPoint.x, lastDebugEdgeCostPoint.y, lastDebugEdgeCostPoint.z, bob.x, bob.y, bob.z, getEdgeCost(out, lastDebugEdgeCostPoint, bob, abilities));
lastDebugEdgeCostPoint = bob;
a++;
} else if ( parameters[a] == "now" ) {
activeDigging = true;
findAndAssignInvasionJob(out, (void*)0);
} else if ( parameters[a] == "clear" ) {
diggingRaces.clear();
digAbilities.clear();
} else if ( parameters[a] == "edgesPerTick" ) {
if ( a+1 >= parameters.size() )
return CR_WRONG_USAGE;
stringstream asdf(parameters[a+1]);
int32_t edgeCount = 100;
asdf >> edgeCount;
edgesPerTick = edgeCount;
a++;
}
else {
return CR_WRONG_USAGE;
}
}
activeDigging = enabled;
out.print("diggingInvaders: enabled = {}, activeDigging = {}, edgesPerTick = {}\n", enabled, activeDigging, edgesPerTick);
return CR_OK;
}
/////////////////////////////////////////////////////////////////////////////////////////
//dijkstra globals
vector<int32_t> invaders;
unordered_set<df::coord, PointHash> invaderPts;
unordered_set<df::coord, PointHash> localPts;
unordered_map<df::coord,df::coord,PointHash> parentMap;
unordered_map<df::coord,cost_t,PointHash> costMap;
PointComp comp(&costMap);
set<df::coord, PointComp> fringe(comp);
EventManager::EventHandler findJobTickHandler(plugin_self, findAndAssignInvasionJob, 1);
int32_t localPtsFound = 0;
unordered_set<df::coord,PointHash> closedSet;
unordered_map<df::coord,int32_t,PointHash> workNeeded; //non-walking work needed to get there
bool foundTarget = false;
int32_t edgeCount = 0;
void clearDijkstra() {
invaders.clear();
invaderPts.clear();
localPts.clear();
parentMap.clear();
costMap.clear();
comp = PointComp(&costMap);
fringe = set<df::coord,PointComp>(comp);
localPtsFound = edgeCount = 0;
foundTarget = false;
closedSet.clear();
workNeeded.clear();
}
/////////////////////////////////////////////////////////////////////////////////////////
void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
//returns the worker id of the job created //used to
//out.print("%s, %d: %d\n", __FILE__, __LINE__, (int32_t)tickTime);
if ( !enabled || !activeDigging ) {
clearDijkstra();
return;
}
//we're going to unregister just in case this function has been called 20 times or something.
EventManager::unregister(EventManager::EventType::TICK, findJobTickHandler);
EventManager::registerTick(findJobTickHandler, 1);
if ( fringe.empty() ) {
df::unit* lastDigger = df::unit::find(lastInvasionDigger);
if ( lastDigger && lastDigger->job.current_job && lastDigger->job.current_job->id == lastInvasionJob ) {
return;
}
//out.print("{},{}: lastDigger = {}, last job = {}, last digger's job = {}\n", __FILE__, __LINE__, lastInvasionDigger, lastInvasionJob, !lastDigger ? -1 : (!lastDigger->job.current_job ? -1 : lastDigger->job.current_job->id));
lastInvasionDigger = lastInvasionJob = -1;
clearDijkstra();
unordered_set<uint16_t> invaderConnectivity;
unordered_set<uint16_t> localConnectivity;
//find all locals and invaders
for ( size_t a = 0; a < world->units.all.size(); a++ ) {
df::unit* unit = world->units.all[a];
if ( !Units::isActive(unit) )
continue;
if ( Units::isCitizen(unit) ) {
if ( localPts.find(unit->pos) != localPts.end() )
continue;
localPts.insert(unit->pos);
df::map_block* block = Maps::getTileBlock(unit->pos);
localConnectivity.insert(block->walkable[unit->pos.x&0xF][unit->pos.y&0xF]);
} else if ( unit->flags1.bits.active_invader ) {
df::creature_raw* raw = df::creature_raw::find(unit->race);
if ( raw == NULL ) {
out.print("{},{}: WTF? Couldn't find creature raw.\n", __FILE__, __LINE__);
continue;
}
/*
if ( diggingRaces.find(raw->creature_id) == diggingRaces.end() )
continue;
*/
if ( digAbilities.find(raw->creature_id) == digAbilities.end() )
continue;
if ( invaderPts.find(unit->pos) != invaderPts.end() )
continue;
//must be able to wield a pick: this is overly pessimistic
if ( unit->status2.limbs_grasp_max <= 0 || unit->status2.limbs_grasp_count < unit->status2.limbs_grasp_max )
continue;
df::map_block* block = Maps::getTileBlock(unit->pos);
invaderConnectivity.insert(block->walkable[unit->pos.x&0xF][unit->pos.y&0xF]);
if ( invaderPts.size() > 0 )
continue;
invaderPts.insert(unit->pos);
costMap[unit->pos] = 0;
fringe.insert(unit->pos);
invaders.push_back(unit->id);
} else {
continue;
}
}
if ( invaders.empty() || localPts.empty() ) {
activeDigging = false;
return;
}
//if local connectivity is not disjoint from invader connectivity, no digging required
bool overlap = false;
for ( auto a = localConnectivity.begin(); a != localConnectivity.end(); a++ ) {
uint16_t conn = *a;
if ( invaderConnectivity.find(conn) == invaderConnectivity.end() )
continue;
overlap = true;
break;
}
if ( overlap ) {
//still keep checking next frame: might kill a few outsiders then dig down
return;
}
}
df::unit* firstInvader = df::unit::find(invaders[0]);
if ( firstInvader == NULL ) {
fringe.clear();
return;
}
df::creature_raw* creature_raw = df::creature_raw::find(firstInvader->race);
if ( creature_raw == NULL || digAbilities.find(creature_raw->creature_id) == digAbilities.end() ) {
//inappropriate digger: no dig abilities
fringe.clear();
return;
}
DigAbilities& abilities = digAbilities[creature_raw->creature_id];
//TODO: check that firstInvader is an appropriate digger
//out << firstInvader->id << endl;
//out << firstInvader->pos.x << ", " << firstInvader->pos.y << ", " << firstInvader->pos.z << endl;
//out << __LINE__ << endl;
uint32_t xMax, yMax, zMax;
Maps::getSize(xMax,yMax,zMax);
xMax *= 16;
yMax *= 16;
MapExtras::MapCache cache;
// clock_t t0 = clock();
clock_t totalEdgeTime = 0;
int32_t edgesExpanded = 0;
while(!fringe.empty()) {
if ( edgesPerTick > 0 && edgesExpanded++ >= edgesPerTick ) {
return;
}
df::coord pt = *(fringe.begin());
fringe.erase(fringe.begin());
//out.print("line %d: fringe size = %d, localPtsFound = %d / %d, closedSetSize = %d, pt = %d,%d,%d\n", __LINE__, fringe.size(), localPtsFound, localPts.size(), closedSet.size(), pt.x,pt.y,pt.z);
if ( closedSet.find(pt) != closedSet.end() ) {
out.print("{},{}: Double closure! Bad!\n", __FILE__, __LINE__);
break;
}
closedSet.insert(pt);
if ( localPts.find(pt) != localPts.end() ) {
localPtsFound++;
if ( true || localPtsFound >= localPts.size() ) {
foundTarget = true;
break;
}
if ( workNeeded.find(pt) == workNeeded.end() || workNeeded[pt] == 0 ) {
//there are still dwarves to kill that don't require digging to get to
return;
}
}
cost_t myCost = costMap[pt];
clock_t edgeTime = clock();
vector<Edge>* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax, abilities);
totalEdgeTime += (clock() - edgeTime);
for ( auto a = myEdges->begin(); a != myEdges->end(); a++ ) {
Edge &e = *a;
if ( e.p1 == df::coord() )
break;
edgeCount++;
df::coord& other = e.p1;
if ( other == pt )
other = e.p2;
//if ( closedSet.find(other) != closedSet.end() )
// continue;
auto i = costMap.find(other);
if ( i != costMap.end() ) {
cost_t cost = (*i).second;
if ( cost <= myCost + e.cost ) {
continue;
}
fringe.erase((*i).first);
}
costMap[other] = myCost + e.cost;
fringe.insert(other);
parentMap[other] = pt;
workNeeded[other] = (e.cost > 1 ? 1 : 0) + workNeeded[pt];
}
delete myEdges;
}
// clock_t time = clock() - t0;
//out.print("tickTime = {}, time = {}, totalEdgeTime = {}, total points = {}, total edges = {}, time per point = {:.3f}, time per edge = {:.3f}, clocks/sec = {}\n", (int32_t)tickTime, time, totalEdgeTime, closedSet.size(), edgeCount, (float)time / closedSet.size(), (float)time / edgeCount, CLOCKS_PER_SEC);
fringe.clear();
if ( !foundTarget )
return;
unordered_set<df::coord, PointHash> requiresZNeg;
unordered_set<df::coord, PointHash> requiresZPos;
//find important edges
Edge firstImportantEdge(df::coord(), df::coord(), -1);
//df::coord closest;
//cost_t closestCostEstimate=0;
//cost_t closestCostActual=0;
for ( auto i = localPts.begin(); i != localPts.end(); i++ ) {
df::coord pt = *i;
if ( costMap.find(pt) == costMap.end() )
continue;
if ( parentMap.find(pt) == parentMap.end() )
continue;
//closest = pt;
//closestCostEstimate = costMap[closest];
//if ( workNeeded[pt] == 0 )
// continue;
while ( parentMap.find(pt) != parentMap.end() ) {
//out.print("(%d,%d,%d)\n", pt.x, pt.y, pt.z);
df::coord parent = parentMap[pt];
cost_t cost = getEdgeCost(out, parent, pt, abilities);
if ( cost < 0 ) {
//path invalidated
return;
}
//closestCostActual += cost;
if ( Maps::canStepBetween(parent, pt) ) {
} else {
if ( pt.x == parent.x && pt.y == parent.y ) {
if ( pt.z < parent.z ) {
requiresZNeg.insert(parent);
requiresZPos.insert(pt);
} else if ( pt.z > parent.z ) {
requiresZNeg.insert(pt);
requiresZPos.insert(parent);
}
}
//if ( workNeeded[pt] > workNeeded[parent] ) {
//importantEdges.push_front(Edge(pt,parent,0));
//}
firstImportantEdge = Edge(pt,parent,0);
//out.print("(%d,%d,%d) -> (%d,%d,%d)\n", parent.x,parent.y,parent.z, pt.x,pt.y,pt.z);
}
pt = parent;
}
break;
}
if ( firstImportantEdge.p1 == df::coord() )
return;
/*
if ( closestCostActual != closestCostEstimate ) {
out.print("%s,%d: closest = (%d,%d,%d), estimate = %lld != actual = %lld\n", __FILE__, __LINE__, closest.x,closest.y,closest.z, closestCostEstimate, closestCostActual);
return;
}
*/
assignJob(out, firstImportantEdge, parentMap, costMap, invaders, requiresZNeg, requiresZPos, cache, abilities);
lastInvasionDigger = firstInvader->id;
lastInvasionJob = firstInvader->job.current_job ? firstInvader->job.current_job->id : -1;
invaderJobs.erase(lastInvasionJob);
for ( df::job_list_link* link = &world->jobs.list; link != NULL; link = link->next ) {
if ( link->item == NULL )
continue;
df::job* job = link->item;
if ( invaderJobs.find(job->id) == invaderJobs.end() ) {
continue;
}
//cancel it
job->flags.bits.item_lost = 1;
out.print("{},{}: cancelling job {}.\n", __FILE__,__LINE__, job->id);
//invaderJobs.remove(job->id);
}
invaderJobs.erase(lastInvasionJob);
return;
}
df::coord getRoot(df::coord point, map<df::coord, df::coord>& rootMap) {
map<df::coord, df::coord>::iterator i = rootMap.find(point);
if ( i == rootMap.end() ) {
rootMap[point] = point;
return point;
}
df::coord parent = (*i).second;
if ( parent == point )
return parent;
df::coord root = getRoot(parent, rootMap);
rootMap[point] = root;
return root;
}