Skip to content

Commit 07a4839

Browse files
committed
diggingInvaders: made it easy to flip between int64 and int32 for distance cost. It did not make a significant difference in performance when I timed it.
1 parent 4e715ca commit 07a4839

5 files changed

Lines changed: 37 additions & 35 deletions

File tree

plugins/diggingInvaders/assignJob.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void getRidOfOldJob(df::unit* unit) {
4747
//delete job;
4848
}
4949

50-
int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df::coord,df::coord,PointHash> parentMap, unordered_map<df::coord,int64_t,PointHash>& costMap, vector<int32_t>& invaders, unordered_set<df::coord,PointHash>& requiresZNeg, unordered_set<df::coord,PointHash>& requiresZPos, MapExtras::MapCache& cache) {
50+
int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df::coord,df::coord,PointHash> parentMap, unordered_map<df::coord,cost_t,PointHash>& costMap, vector<int32_t>& invaders, unordered_set<df::coord,PointHash>& requiresZNeg, unordered_set<df::coord,PointHash>& requiresZPos, MapExtras::MapCache& cache) {
5151
df::unit* firstInvader = df::unit::find(invaders[0]);
5252
if ( !firstInvader ) {
5353
return -1;
@@ -61,10 +61,10 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
6161
pt1 = pt2;
6262
pt2 = temp;
6363
}
64-
out.print("first important edge: (%d,%d,%d) -> (%d,%d,%d)\n", pt1.x,pt1.y,pt1.z, pt2.x,pt2.y,pt2.z);
65-
64+
//out.print("first important edge: (%d,%d,%d) -> (%d,%d,%d)\n", pt1.x,pt1.y,pt1.z, pt2.x,pt2.y,pt2.z);
65+
6666
int32_t jobId = -1;
67-
67+
6868
df::map_block* block1 = Maps::getTileBlock(pt1);
6969
df::map_block* block2 = Maps::getTileBlock(pt2);
7070
bool passable1 = block1->walkable[pt1.x&0xF][pt1.y&0xF];
@@ -166,18 +166,18 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
166166
df::job* job = new df::job;
167167
if ( up && down ) {
168168
job->job_type = df::enums::job_type::CarveUpDownStaircase;
169-
out.print("%s, line %d: type = up/down\n", __FILE__, __LINE__);
169+
//out.print("%s, line %d: type = up/down\n", __FILE__, __LINE__);
170170
} else if ( up && !down ) {
171171
job->job_type = df::enums::job_type::CarveUpwardStaircase;
172-
out.print("%s, line %d: type = up\n", __FILE__, __LINE__);
172+
//out.print("%s, line %d: type = up\n", __FILE__, __LINE__);
173173
} else if ( !up && down ) {
174174
job->job_type = df::enums::job_type::CarveDownwardStaircase;
175-
out.print("%s, line %d: type = down\n", __FILE__, __LINE__);
175+
//out.print("%s, line %d: type = down\n", __FILE__, __LINE__);
176176
} else {
177177
job->job_type = df::enums::job_type::Dig;
178-
out.print("%s, line %d: type = dig\n", __FILE__, __LINE__);
178+
//out.print("%s, line %d: type = dig\n", __FILE__, __LINE__);
179179
}
180-
out.print("%s, line %d: up=%d,up1=%d,up2=%d, down=%d,down1=%d,down2=%d\n", __FILE__, __LINE__, up,up1,up2, down,down1,down2);
180+
//out.print("%s, line %d: up=%d,up1=%d,up2=%d, down=%d,down1=%d,down2=%d\n", __FILE__, __LINE__, up,up1,up2, down,down1,down2);
181181
job->pos = workHere;
182182
firstInvader->path.dest = goHere;
183183
location = goHere;

plugins/diggingInvaders/assignJob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
using namespace std;
1111

12-
int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df::coord,df::coord,PointHash> parentMap, unordered_map<df::coord,int64_t,PointHash>& costMap, vector<int32_t>& invaders, unordered_set<df::coord,PointHash>& requiresZNeg, unordered_set<df::coord,PointHash>& requiresZPos, MapExtras::MapCache& cache);
12+
int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df::coord,df::coord,PointHash> parentMap, unordered_map<df::coord,cost_t,PointHash>& costMap, vector<int32_t>& invaders, unordered_set<df::coord,PointHash>& requiresZNeg, unordered_set<df::coord,PointHash>& requiresZPos, MapExtras::MapCache& cache);
1313

plugins/diggingInvaders/diggingInvaders.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ df::coord getRoot(df::coord point, unordered_map<df::coord, df::coord>& rootMap)
132132

133133
class PointComp {
134134
public:
135-
unordered_map<df::coord, int64_t, PointHash> *pointCost;
136-
PointComp(unordered_map<df::coord, int64_t, PointHash> *p): pointCost(p) {
135+
unordered_map<df::coord, cost_t, PointHash> *pointCost;
136+
PointComp(unordered_map<df::coord, cost_t, PointHash> *p): pointCost(p) {
137137

138138
}
139139

@@ -147,8 +147,8 @@ class PointComp {
147147
return true;
148148
if ( i2 == pointCost->end() )
149149
return false;
150-
int64_t c1 = (*i1).second;
151-
int64_t c2 = (*i2).second;
150+
cost_t c1 = (*i1).second;
151+
cost_t c2 = (*i2).second;
152152
if ( c1 != c2 )
153153
return c1 < c2;
154154
return p1 < p2;
@@ -300,7 +300,7 @@ command_result diggingInvadersCommand(color_ostream& out, std::vector<std::strin
300300
} else {
301301
return CR_WRONG_USAGE;
302302
}
303-
int64_t value;
303+
cost_t value;
304304
stringstream asdf(parameters[a+2]);
305305
asdf >> value;
306306
if ( value <= 0 )
@@ -330,12 +330,12 @@ vector<int32_t> invaders;
330330
unordered_set<df::coord, PointHash> invaderPts;
331331
unordered_set<df::coord, PointHash> localPts;
332332
unordered_map<df::coord,df::coord,PointHash> parentMap;
333-
unordered_map<df::coord,int64_t,PointHash> costMap;
333+
unordered_map<df::coord,cost_t,PointHash> costMap;
334334

335335
PointComp comp(&costMap);
336336
set<df::coord, PointComp> fringe(comp);
337337
EventManager::EventHandler findJobTickHandler(findAndAssignInvasionJob, 1);
338-
const int32_t edgesPerFrame = 1000;
338+
const int32_t edgesPerFrame = 10000000;
339339

340340
int32_t localPtsFound = 0;
341341
unordered_set<df::coord,PointHash> closedSet;
@@ -483,7 +483,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
483483
}
484484
}
485485

486-
int64_t myCost = costMap[pt];
486+
cost_t myCost = costMap[pt];
487487
clock_t edgeTime = clock();
488488
vector<Edge>* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax);
489489
totalEdgeTime += (clock() - edgeTime);
@@ -499,7 +499,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
499499
// continue;
500500
auto i = costMap.find(other);
501501
if ( i != costMap.end() ) {
502-
int64_t cost = (*i).second;
502+
cost_t cost = (*i).second;
503503
if ( cost <= myCost + e.cost ) {
504504
continue;
505505
}
@@ -513,7 +513,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
513513
delete myEdges;
514514
}
515515
clock_t time = clock() - t0;
516-
//out.print("tickTime = %d, time = %d, totalEdgeTime = %d, total points = %d, total edges = %d, time per point = %.3f, time per edge = %.3f, clocks/sec = %d\n", (int32_t)tickTime, time, totalEdgeTime, closedSet.size(), edgeCount, (float)time / closedSet.size(), (float)time / edgeCount, CLOCKS_PER_SEC);
516+
out.print("tickTime = %d, time = %d, totalEdgeTime = %d, total points = %d, total edges = %d, time per point = %.3f, time per edge = %.3f, clocks/sec = %d\n", (int32_t)tickTime, time, totalEdgeTime, closedSet.size(), edgeCount, (float)time / closedSet.size(), (float)time / edgeCount, CLOCKS_PER_SEC);
517517
fringe.clear();
518518

519519
if ( !foundTarget )
@@ -525,8 +525,8 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
525525
//find important edges
526526
Edge firstImportantEdge(df::coord(), df::coord(), -1);
527527
df::coord closest;
528-
int64_t closestCostEstimate=0;
529-
int64_t closestCostActual=0;
528+
cost_t closestCostEstimate=0;
529+
cost_t closestCostActual=0;
530530
for ( auto i = localPts.begin(); i != localPts.end(); i++ ) {
531531
df::coord pt = *i;
532532
if ( costMap.find(pt) == costMap.end() )

plugins/diggingInvaders/edgeCost.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <iostream>
1919

20-
int64_t costWeight[] = {
20+
cost_t costWeight[] = {
2121
//Distance
2222
1,
2323
//Destroy Building
@@ -35,11 +35,11 @@ limitations
3535
ramps
3636
cave-ins
3737
*/
38-
int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) {
38+
cost_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) {
3939
int32_t dx = pt2.x - pt1.x;
4040
int32_t dy = pt2.y - pt1.y;
4141
int32_t dz = pt2.z - pt1.z;
42-
int64_t cost = costWeight[CostDimension::Walk];
42+
cost_t cost = costWeight[CostDimension::Walk];
4343

4444
if ( Maps::getTileBlock(pt1) == NULL || Maps::getTileBlock(pt2) == NULL )
4545
return -1;
@@ -203,12 +203,12 @@ int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) {
203203
//you can deconstruct a hatch from the side
204204
if ( building1 && forbidden /*&& building1->getType() == df::building_type::Hatch*/ ) {
205205
df::coord support[] = {df::coord(pt1.x-1, pt1.y, pt1.z), df::coord(pt1.x+1,pt1.y,pt1.z), df::coord(pt1.x,pt1.y-1,pt1.z), df::coord(pt1.x,pt1.y+1,pt1.z)};
206-
int64_t minCost = -1;
206+
cost_t minCost = -1;
207207
for ( size_t a = 0; a < 4; a++ ) {
208208
df::tiletype* supportType = Maps::getTileType(support[a]);
209209
df::tiletype_shape shape = ENUM_ATTR(tiletype, shape, *supportType);
210210
df::tiletype_shape_basic basic = ENUM_ATTR(tiletype_shape, basic_shape, shape);
211-
int64_t cost2 = 2*costWeight[CostDimension::Walk] + costWeight[CostDimension::DestroyBuilding];
211+
cost_t cost2 = 2*costWeight[CostDimension::Walk] + costWeight[CostDimension::DestroyBuilding];
212212
if ( !Maps::canStepBetween(pt1, support[a]) ) {
213213
switch(basic) {
214214
case tiletype_shape_basic::Open:
@@ -254,12 +254,12 @@ int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) {
254254
return cost;
255255
}
256256

257-
int64_t getEdgeCostOld(color_ostream& out, df::coord pt1, df::coord pt2) {
257+
cost_t getEdgeCostOld(color_ostream& out, df::coord pt1, df::coord pt2) {
258258
//first, list all the facts
259259
int32_t dx = pt2.x - pt1.x;
260260
int32_t dy = pt2.y - pt1.y;
261261
int32_t dz = pt2.z - pt1.z;
262-
int64_t cost = costWeight[CostDimension::Walk];
262+
cost_t cost = costWeight[CostDimension::Walk];
263263

264264
if ( false ) {
265265
if ( Maps::canStepBetween(pt1,pt2) )
@@ -402,7 +402,7 @@ vector<Edge>* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCach
402402
continue;
403403
if ( dx == 0 && dy == 0 && dz == 0 )
404404
continue;
405-
int64_t cost = getEdgeCost(out, point, neighbor);
405+
cost_t cost = getEdgeCost(out, point, neighbor);
406406
if ( cost == -1 )
407407
continue;
408408
Edge edge(point, neighbor, cost);

plugins/diggingInvaders/edgeCost.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ enum CostDimension {
2121
costDim
2222
};
2323

24-
extern int64_t costWeight[costDim];
24+
typedef int64_t cost_t;
25+
26+
extern cost_t costWeight[costDim];
2527
/*
26-
const int64_t costWeight[] = {
28+
const cost_t costWeight[] = {
2729
//Distance
2830
1,
2931
//Destroy Building
@@ -40,14 +42,14 @@ class Edge {
4042
//static map<df::coord, int32_t> pointCost;
4143
df::coord p1;
4244
df::coord p2;
43-
int64_t cost;
45+
cost_t cost;
4446
Edge() {
4547
cost = -1;
4648
}
4749
Edge(const Edge& e): p1(e.p1), p2(e.p2), cost(e.cost) {
4850

4951
}
50-
Edge(df::coord p1In, df::coord p2In, int64_t costIn): cost(costIn) {
52+
Edge(df::coord p1In, df::coord p2In, cost_t costIn): cost(costIn) {
5153
if ( p2In < p1In ) {
5254
p1 = p2In;
5355
p2 = p1In;
@@ -82,6 +84,6 @@ struct PointHash {
8284
}
8385
};
8486

85-
int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2);
87+
cost_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2);
8688
std::vector<Edge>* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCache& cache, int32_t xMax, int32_t yMax, int32_t zMax);
8789

0 commit comments

Comments
 (0)