more digger testing

develop
mizipzor 2010-02-18 03:08:34 +08:00 committed by belal
parent c2faa074e8
commit 4e8399a655
1 changed files with 72 additions and 47 deletions

@ -33,31 +33,34 @@ int vec_count(vector<uint16_t>& vec, uint16_t t)
return count; return count;
} }
// manhattan distance //// manhattan distance
int source_distance(int sx, int sy, int sz, //int source_distance(int sx, int sy, int sz,
int x, int y, int z, int i) // int x, int y, int z, int i)
//{
// // TODO changing x and y seems to be optimized away (?)
// cout << x << " " << i << " " << i%16 << " " << x+(i%16) << endl;
//
// // handle the fact that x,y,z refers to a 16x16 grid
// //x += i%16;
// //y += i/16;
// int dx = i%16;
// int dy = i/16;
// //x *= 16;
// //y *= 16;
// //x += dx;
// //y += dy;
// return abs(sx-(x+(i%16)))+abs(sy-(y+(i/16)))+abs(sz-z);
//}
int manhattan_distance(int x, int y, int z, int xx, int yy, int zz)
{ {
// TODO changing x and y seems to be optimized away (?) return abs(x-xx)+abs(y-yy)+abs(z-zz);
cout << x << " " << i << " " << i%16 << " " << x+(i%16) << endl;
// handle the fact that x,y,z refers to a 16x16 grid
//x += i%16;
//y += i/16;
int dx = i%16;
int dy = i/16;
//x *= 16;
//y *= 16;
//x += dx;
//y += dy;
return abs(sx-(x+(i%16)))+abs(sy-(y+(i/16)))+abs(sz-z);
} }
struct DigTarget struct DigTarget
{ {
int source_distance; // the distance to the source coords, used for sorting int source_distance; // the distance to the source coords, used for sorting
int x, y, z; // call read designations with these and you have the tile at i int x, y, z;
int i; // 0 <= i <= 255
bool operator<(const DigTarget& o) { return source_distance < o.source_distance; } bool operator<(const DigTarget& o) { return source_distance < o.source_distance; }
}; };
@ -72,14 +75,17 @@ int dig(DFHack::API& DF,
return 0; // max limit of 0, nothing to do return 0; // max limit of 0, nothing to do
uint32_t x_max,y_max,z_max; uint32_t x_max,y_max,z_max;
DFHack::t_designation designations[256]; DFHack::t_designation designations[16][16];
uint16_t tiles[256]; uint16_t tiles[16][16];
//uint32_t count = 0; //uint32_t count = 0;
DF.getSize(x_max,y_max,z_max); DF.getSize(x_max,y_max,z_max);
// every tile found, will later be sorted by distance to source // every tile found, will later be sorted by distance to source
vector<DigTarget> candidates; vector<DigTarget> candidates;
cout << "============================" << endl;
cout << "source is " << x_source << " " << y_source << " " << z_source << endl;
// walk the map // walk the map
for(uint32_t x = 0; x < x_max; x++) for(uint32_t x = 0; x < x_max; x++)
{ {
@ -98,29 +104,35 @@ int dig(DFHack::API& DF,
// search all tiles for dig targets: // search all tiles for dig targets:
// visible, not yet marked for dig and matching tile type // visible, not yet marked for dig and matching tile type
for (uint32_t i = 0; i < 256; i++) for(uint32_t lx = 0; lx < 16; lx++)
{ {
if (designations[i].bits.hidden == 0 && for(uint32_t ly = 0; ly < 16; ly++)
designations[i].bits.dig == 0 && {
vec_count(targets, DFHack::tileTypeTable[tiles[i]].c) > 0) if (designations[lx][ly].bits.hidden == 0 &&
{ designations[lx][ly].bits.dig == 0 &&
//cout << "target found at: "; vec_count(targets, DFHack::tileTypeTable[tiles[lx][ly]].c) > 0)
//cout << x << "," << y << "," << z << "," << i << endl; {
//cout << "target found at: ";
//designations[i].bits.dig = DFHack::designation_default; //cout << x << "," << y << "," << z << "," << i << endl;
//++count;
//designations[i].bits.dig = DFHack::designation_default;
DigTarget dt; //++count;
dt.x = x;
dt.y = y; DigTarget dt;
dt.z = z; dt.x = x+lx;
dt.i = i; dt.y = y+ly;
dt.source_distance = source_distance( dt.z = z;
x_source, y_source, z_source, dt.source_distance = manhattan_distance(
x, y, z, i); x_source, y_source, z_source,
candidates.push_back(dt); //x, y, z, i);
} x+lx, y+ly, z);
} candidates.push_back(dt);
cout << "target found at " << x+lx << " " << y+ly << " " << z;
cout << ", " << dt.source_distance << " tiles to source" << endl;
}
} // local y
} // local x
} }
} }
} }
@ -137,13 +149,26 @@ int dig(DFHack::API& DF,
} }
num = candidates.size(); num = candidates.size();
cout << "============================" << endl;
cout << "source is " << x_source << " " << y_source << " " << z_source << endl;
// mark the tiles for actual digging // mark the tiles for actual digging
for (vector<DigTarget>::iterator i = candidates.begin(); i != candidates.end(); ++i) for (vector<DigTarget>::iterator i = candidates.begin(); i != candidates.end(); ++i)
{ {
int world_x = (*i).x/16;
int world_y = (*i).y/16;
int world_z = (*i).z;
int local_x = (*i).x-world_x;
int local_y = (*i).y-world_y;
cout << "designating at " << world_x+local_x << " " << world_y+local_y << " " << (*i).z;
cout << ", " << (*i).source_distance << " tiles to source" << endl;
// TODO this could probably be made much better, theres a big chance the trees are on the same grid // TODO this could probably be made much better, theres a big chance the trees are on the same grid
DF.ReadDesignations((*i).x, (*i).y, (*i).z, (uint32_t *) designations); DF.ReadDesignations(world_x, world_y, world_z, (uint32_t *) designations);
designations[(*i).i].bits.dig = DFHack::designation_default; designations[local_x][local_y].bits.dig = DFHack::designation_default;
DF.WriteDesignations((*i).x, (*i).y, (*i).z, (uint32_t *) designations); DF.WriteDesignations(world_x, world_y, world_z, (uint32_t *) designations);
} }
return num; return num;