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.

develop
expwnent 2013-06-09 23:17:31 -04:00
parent 4e715ca44b
commit 07a4839d8b
5 changed files with 37 additions and 35 deletions

@ -47,7 +47,7 @@ void getRidOfOldJob(df::unit* unit) {
//delete job; //delete job;
} }
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) { 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) {
df::unit* firstInvader = df::unit::find(invaders[0]); df::unit* firstInvader = df::unit::find(invaders[0]);
if ( !firstInvader ) { if ( !firstInvader ) {
return -1; return -1;
@ -61,7 +61,7 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
pt1 = pt2; pt1 = pt2;
pt2 = temp; pt2 = temp;
} }
out.print("first important edge: (%d,%d,%d) -> (%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; int32_t jobId = -1;
@ -166,18 +166,18 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
df::job* job = new df::job; df::job* job = new df::job;
if ( up && down ) { if ( up && down ) {
job->job_type = df::enums::job_type::CarveUpDownStaircase; job->job_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 ) { } else if ( up && !down ) {
job->job_type = df::enums::job_type::CarveUpwardStaircase; 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 ) { } else if ( !up && down ) {
job->job_type = df::enums::job_type::CarveDownwardStaircase; 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 { } else {
job->job_type = df::enums::job_type::Dig; 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; job->pos = workHere;
firstInvader->path.dest = goHere; firstInvader->path.dest = goHere;
location = goHere; location = goHere;

@ -9,5 +9,5 @@
using namespace std; using namespace std;
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); 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);

@ -132,8 +132,8 @@ df::coord getRoot(df::coord point, unordered_map<df::coord, df::coord>& rootMap)
class PointComp { class PointComp {
public: public:
unordered_map<df::coord, int64_t, PointHash> *pointCost; unordered_map<df::coord, cost_t, PointHash> *pointCost;
PointComp(unordered_map<df::coord, int64_t, PointHash> *p): pointCost(p) { PointComp(unordered_map<df::coord, cost_t, PointHash> *p): pointCost(p) {
} }
@ -147,8 +147,8 @@ public:
return true; return true;
if ( i2 == pointCost->end() ) if ( i2 == pointCost->end() )
return false; return false;
int64_t c1 = (*i1).second; cost_t c1 = (*i1).second;
int64_t c2 = (*i2).second; cost_t c2 = (*i2).second;
if ( c1 != c2 ) if ( c1 != c2 )
return c1 < c2; return c1 < c2;
return p1 < p2; return p1 < p2;
@ -300,7 +300,7 @@ command_result diggingInvadersCommand(color_ostream& out, std::vector<std::strin
} else { } else {
return CR_WRONG_USAGE; return CR_WRONG_USAGE;
} }
int64_t value; cost_t value;
stringstream asdf(parameters[a+2]); stringstream asdf(parameters[a+2]);
asdf >> value; asdf >> value;
if ( value <= 0 ) if ( value <= 0 )
@ -330,12 +330,12 @@ vector<int32_t> invaders;
unordered_set<df::coord, PointHash> invaderPts; unordered_set<df::coord, PointHash> invaderPts;
unordered_set<df::coord, PointHash> localPts; unordered_set<df::coord, PointHash> localPts;
unordered_map<df::coord,df::coord,PointHash> parentMap; unordered_map<df::coord,df::coord,PointHash> parentMap;
unordered_map<df::coord,int64_t,PointHash> costMap; unordered_map<df::coord,cost_t,PointHash> costMap;
PointComp comp(&costMap); PointComp comp(&costMap);
set<df::coord, PointComp> fringe(comp); set<df::coord, PointComp> fringe(comp);
EventManager::EventHandler findJobTickHandler(findAndAssignInvasionJob, 1); EventManager::EventHandler findJobTickHandler(findAndAssignInvasionJob, 1);
const int32_t edgesPerFrame = 1000; const int32_t edgesPerFrame = 10000000;
int32_t localPtsFound = 0; int32_t localPtsFound = 0;
unordered_set<df::coord,PointHash> closedSet; unordered_set<df::coord,PointHash> 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(); clock_t edgeTime = clock();
vector<Edge>* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax); vector<Edge>* myEdges = getEdgeSet(out, pt, cache, xMax, yMax, zMax);
totalEdgeTime += (clock() - edgeTime); totalEdgeTime += (clock() - edgeTime);
@ -499,7 +499,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
// continue; // continue;
auto i = costMap.find(other); auto i = costMap.find(other);
if ( i != costMap.end() ) { if ( i != costMap.end() ) {
int64_t cost = (*i).second; cost_t cost = (*i).second;
if ( cost <= myCost + e.cost ) { if ( cost <= myCost + e.cost ) {
continue; continue;
} }
@ -513,7 +513,7 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
delete myEdges; delete myEdges;
} }
clock_t time = clock() - t0; 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(); fringe.clear();
if ( !foundTarget ) if ( !foundTarget )
@ -525,8 +525,8 @@ void findAndAssignInvasionJob(color_ostream& out, void* tickTime) {
//find important edges //find important edges
Edge firstImportantEdge(df::coord(), df::coord(), -1); Edge firstImportantEdge(df::coord(), df::coord(), -1);
df::coord closest; df::coord closest;
int64_t closestCostEstimate=0; cost_t closestCostEstimate=0;
int64_t closestCostActual=0; cost_t closestCostActual=0;
for ( auto i = localPts.begin(); i != localPts.end(); i++ ) { for ( auto i = localPts.begin(); i != localPts.end(); i++ ) {
df::coord pt = *i; df::coord pt = *i;
if ( costMap.find(pt) == costMap.end() ) if ( costMap.find(pt) == costMap.end() )

@ -17,7 +17,7 @@
#include <iostream> #include <iostream>
int64_t costWeight[] = { cost_t costWeight[] = {
//Distance //Distance
1, 1,
//Destroy Building //Destroy Building
@ -35,11 +35,11 @@ limitations
ramps ramps
cave-ins 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 dx = pt2.x - pt1.x;
int32_t dy = pt2.y - pt1.y; int32_t dy = pt2.y - pt1.y;
int32_t dz = pt2.z - pt1.z; 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 ) if ( Maps::getTileBlock(pt1) == NULL || Maps::getTileBlock(pt2) == NULL )
return -1; 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 //you can deconstruct a hatch from the side
if ( building1 && forbidden /*&& building1->getType() == df::building_type::Hatch*/ ) { 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)}; 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++ ) { for ( size_t a = 0; a < 4; a++ ) {
df::tiletype* supportType = Maps::getTileType(support[a]); df::tiletype* supportType = Maps::getTileType(support[a]);
df::tiletype_shape shape = ENUM_ATTR(tiletype, shape, *supportType); df::tiletype_shape shape = ENUM_ATTR(tiletype, shape, *supportType);
df::tiletype_shape_basic basic = ENUM_ATTR(tiletype_shape, basic_shape, shape); 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]) ) { if ( !Maps::canStepBetween(pt1, support[a]) ) {
switch(basic) { switch(basic) {
case tiletype_shape_basic::Open: case tiletype_shape_basic::Open:
@ -254,12 +254,12 @@ int64_t getEdgeCost(color_ostream& out, df::coord pt1, df::coord pt2) {
return cost; 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 //first, list all the facts
int32_t dx = pt2.x - pt1.x; int32_t dx = pt2.x - pt1.x;
int32_t dy = pt2.y - pt1.y; int32_t dy = pt2.y - pt1.y;
int32_t dz = pt2.z - pt1.z; int32_t dz = pt2.z - pt1.z;
int64_t cost = costWeight[CostDimension::Walk]; cost_t cost = costWeight[CostDimension::Walk];
if ( false ) { if ( false ) {
if ( Maps::canStepBetween(pt1,pt2) ) if ( Maps::canStepBetween(pt1,pt2) )
@ -402,7 +402,7 @@ vector<Edge>* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCach
continue; continue;
if ( dx == 0 && dy == 0 && dz == 0 ) if ( dx == 0 && dy == 0 && dz == 0 )
continue; continue;
int64_t cost = getEdgeCost(out, point, neighbor); cost_t cost = getEdgeCost(out, point, neighbor);
if ( cost == -1 ) if ( cost == -1 )
continue; continue;
Edge edge(point, neighbor, cost); Edge edge(point, neighbor, cost);

@ -21,9 +21,11 @@ enum CostDimension {
costDim 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 //Distance
1, 1,
//Destroy Building //Destroy Building
@ -40,14 +42,14 @@ public:
//static map<df::coord, int32_t> pointCost; //static map<df::coord, int32_t> pointCost;
df::coord p1; df::coord p1;
df::coord p2; df::coord p2;
int64_t cost; cost_t cost;
Edge() { Edge() {
cost = -1; cost = -1;
} }
Edge(const Edge& e): p1(e.p1), p2(e.p2), cost(e.cost) { 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 ) { if ( p2In < p1In ) {
p1 = p2In; p1 = p2In;
p2 = p1In; 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<Edge>* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCache& cache, int32_t xMax, int32_t yMax, int32_t zMax); std::vector<Edge>* getEdgeSet(color_ostream &out, df::coord point, MapExtras::MapCache& cache, int32_t xMax, int32_t yMax, int32_t zMax);