From 07a4839d8b0e7de5f99e71d93103862d5b27ea5b Mon Sep 17 00:00:00 2001 From: expwnent Date: Sun, 9 Jun 2013 23:17:31 -0400 Subject: [PATCH] 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. --- plugins/diggingInvaders/assignJob.cpp | 18 ++++++++-------- plugins/diggingInvaders/assignJob.h | 2 +- plugins/diggingInvaders/diggingInvaders.cpp | 24 ++++++++++----------- plugins/diggingInvaders/edgeCost.cpp | 16 +++++++------- plugins/diggingInvaders/edgeCost.h | 12 ++++++----- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/plugins/diggingInvaders/assignJob.cpp b/plugins/diggingInvaders/assignJob.cpp index 2b49fa02c..f43eaf207 100644 --- a/plugins/diggingInvaders/assignJob.cpp +++ b/plugins/diggingInvaders/assignJob.cpp @@ -47,7 +47,7 @@ void getRidOfOldJob(df::unit* unit) { //delete job; } -int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map parentMap, unordered_map& costMap, vector& invaders, unordered_set& requiresZNeg, unordered_set& requiresZPos, MapExtras::MapCache& cache) { +int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map parentMap, unordered_map& costMap, vector& invaders, unordered_set& requiresZNeg, unordered_set& requiresZPos, MapExtras::MapCache& cache) { df::unit* firstInvader = df::unit::find(invaders[0]); if ( !firstInvader ) { return -1; @@ -61,10 +61,10 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map (%d,%d,%d)\n", pt1.x,pt1.y,pt1.z, pt2.x,pt2.y,pt2.z); - + //out.print("first important edge: (%d,%d,%d) -> (%d,%d,%d)\n", pt1.x,pt1.y,pt1.z, pt2.x,pt2.y,pt2.z); + int32_t jobId = -1; - + df::map_block* block1 = Maps::getTileBlock(pt1); df::map_block* block2 = Maps::getTileBlock(pt2); bool passable1 = block1->walkable[pt1.x&0xF][pt1.y&0xF]; @@ -166,18 +166,18 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_mapjob_type = df::enums::job_type::CarveUpDownStaircase; - out.print("%s, line %d: type = up/down\n", __FILE__, __LINE__); + //out.print("%s, line %d: type = up/down\n", __FILE__, __LINE__); } else if ( up && !down ) { job->job_type = df::enums::job_type::CarveUpwardStaircase; - out.print("%s, line %d: type = up\n", __FILE__, __LINE__); + //out.print("%s, line %d: type = up\n", __FILE__, __LINE__); } else if ( !up && down ) { job->job_type = df::enums::job_type::CarveDownwardStaircase; - out.print("%s, line %d: type = down\n", __FILE__, __LINE__); + //out.print("%s, line %d: type = down\n", __FILE__, __LINE__); } else { job->job_type = df::enums::job_type::Dig; - out.print("%s, line %d: type = dig\n", __FILE__, __LINE__); + //out.print("%s, line %d: type = dig\n", __FILE__, __LINE__); } - 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); + //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); job->pos = workHere; firstInvader->path.dest = goHere; location = goHere; diff --git a/plugins/diggingInvaders/assignJob.h b/plugins/diggingInvaders/assignJob.h index 5a5e93d99..9d0837a3a 100644 --- a/plugins/diggingInvaders/assignJob.h +++ b/plugins/diggingInvaders/assignJob.h @@ -9,5 +9,5 @@ using namespace std; -int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map parentMap, unordered_map& costMap, vector& invaders, unordered_set& requiresZNeg, unordered_set& requiresZPos, MapExtras::MapCache& cache); +int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map parentMap, unordered_map& costMap, vector& invaders, unordered_set& requiresZNeg, unordered_set& requiresZPos, MapExtras::MapCache& cache); diff --git a/plugins/diggingInvaders/diggingInvaders.cpp b/plugins/diggingInvaders/diggingInvaders.cpp index 99607a531..ee78c3d6b 100644 --- a/plugins/diggingInvaders/diggingInvaders.cpp +++ b/plugins/diggingInvaders/diggingInvaders.cpp @@ -132,8 +132,8 @@ df::coord getRoot(df::coord point, unordered_map& rootMap) class PointComp { public: - unordered_map *pointCost; - PointComp(unordered_map *p): pointCost(p) { + unordered_map *pointCost; + PointComp(unordered_map *p): pointCost(p) { } @@ -147,8 +147,8 @@ public: return true; if ( i2 == pointCost->end() ) return false; - int64_t c1 = (*i1).second; - int64_t c2 = (*i2).second; + cost_t c1 = (*i1).second; + cost_t c2 = (*i2).second; if ( c1 != c2 ) return c1 < c2; return p1 < p2; @@ -300,7 +300,7 @@ command_result diggingInvadersCommand(color_ostream& out, std::vector> value; if ( value <= 0 ) @@ -330,12 +330,12 @@ vector invaders; unordered_set invaderPts; unordered_set localPts; unordered_map parentMap; -unordered_map costMap; +unordered_map costMap; PointComp comp(&costMap); set fringe(comp); EventManager::EventHandler findJobTickHandler(findAndAssignInvasionJob, 1); -const int32_t edgesPerFrame = 1000; +const int32_t edgesPerFrame = 10000000; int32_t localPtsFound = 0; unordered_set closedSet; @@ -483,7 +483,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) { } } - int64_t myCost = costMap[pt]; + cost_t myCost = costMap[pt]; clock_t edgeTime = clock(); vector* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax); totalEdgeTime += (clock() - edgeTime); @@ -499,7 +499,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) { // continue; auto i = costMap.find(other); if ( i != costMap.end() ) { - int64_t cost = (*i).second; + cost_t cost = (*i).second; if ( cost <= myCost + e.cost ) { continue; } @@ -513,7 +513,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) { delete myEdges; } clock_t time = clock() - t0; - //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); + 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); fringe.clear(); if ( !foundTarget ) @@ -525,8 +525,8 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) { //find important edges Edge firstImportantEdge(df::coord(), df::coord(), -1); df::coord closest; - int64_t closestCostEstimate=0; - int64_t closestCostActual=0; + 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() ) diff --git a/plugins/diggingInvaders/edgeCost.cpp b/plugins/diggingInvaders/edgeCost.cpp index dcef640f4..1eaf07aa6 100644 --- a/plugins/diggingInvaders/edgeCost.cpp +++ b/plugins/diggingInvaders/edgeCost.cpp @@ -17,7 +17,7 @@ #include -int64_t costWeight[] = { +cost_t costWeight[] = { //Distance 1, //Destroy Building @@ -35,11 +35,11 @@ limitations ramps cave-ins */ -int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) { +cost_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) { int32_t dx = pt2.x - pt1.x; int32_t dy = pt2.y - pt1.y; int32_t dz = pt2.z - pt1.z; - int64_t cost = costWeight[CostDimension::Walk]; + cost_t cost = costWeight[CostDimension::Walk]; if ( Maps::getTileBlock(pt1) == NULL || Maps::getTileBlock(pt2) == NULL ) return -1; @@ -203,12 +203,12 @@ int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) { //you can deconstruct a hatch from the side if ( building1 && forbidden /*&& building1->getType() == df::building_type::Hatch*/ ) { 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)}; - int64_t minCost = -1; + cost_t minCost = -1; for ( size_t a = 0; a < 4; a++ ) { df::tiletype* supportType = Maps::getTileType(support[a]); df::tiletype_shape shape = ENUM_ATTR(tiletype, shape, *supportType); df::tiletype_shape_basic basic = ENUM_ATTR(tiletype_shape, basic_shape, shape); - int64_t cost2 = 2*costWeight[CostDimension::Walk] + costWeight[CostDimension::DestroyBuilding]; + cost_t cost2 = 2*costWeight[CostDimension::Walk] + costWeight[CostDimension::DestroyBuilding]; if ( !Maps::canStepBetween(pt1, support[a]) ) { switch(basic) { case tiletype_shape_basic::Open: @@ -254,12 +254,12 @@ int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) { return cost; } -int64_t getEdgeCostOld(color_ostream& out, df::coord pt1, df::coord pt2) { +cost_t getEdgeCostOld(color_ostream& out, df::coord pt1, df::coord pt2) { //first, list all the facts int32_t dx = pt2.x - pt1.x; int32_t dy = pt2.y - pt1.y; int32_t dz = pt2.z - pt1.z; - int64_t cost = costWeight[CostDimension::Walk]; + cost_t cost = costWeight[CostDimension::Walk]; if ( false ) { if ( Maps::canStepBetween(pt1,pt2) ) @@ -402,7 +402,7 @@ vector* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCach continue; if ( dx == 0 && dy == 0 && dz == 0 ) continue; - int64_t cost = getEdgeCost(out, point, neighbor); + cost_t cost = getEdgeCost(out, point, neighbor); if ( cost == -1 ) continue; Edge edge(point, neighbor, cost); diff --git a/plugins/diggingInvaders/edgeCost.h b/plugins/diggingInvaders/edgeCost.h index 19fd7ff28..fd1ab0e27 100644 --- a/plugins/diggingInvaders/edgeCost.h +++ b/plugins/diggingInvaders/edgeCost.h @@ -21,9 +21,11 @@ enum CostDimension { costDim }; -extern int64_t costWeight[costDim]; +typedef int64_t cost_t; + +extern cost_t costWeight[costDim]; /* -const int64_t costWeight[] = { +const cost_t costWeight[] = { //Distance 1, //Destroy Building @@ -40,14 +42,14 @@ public: //static map pointCost; df::coord p1; df::coord p2; - int64_t cost; + cost_t cost; Edge() { cost = -1; } Edge(const Edge& e): p1(e.p1), p2(e.p2), cost(e.cost) { } - Edge(df::coord p1In, df::coord p2In, int64_t costIn): cost(costIn) { + Edge(df::coord p1In, df::coord p2In, cost_t costIn): cost(costIn) { if ( p2In < p1In ) { p1 = p2In; p2 = p1In; @@ -82,6 +84,6 @@ struct PointHash { } }; -int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2); +cost_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2); std::vector* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCache& cache, int32_t xMax, int32_t yMax, int32_t zMax);