Refactor the Map::neighbors so that it's part of pathing where it should be.

master
Zed A. Shaw 2 weeks ago
parent d4355a608d
commit e6c225f1c8
  1. 46
      map.cpp
  2. 47
      pathing.cpp
  3. 1
      pathing.hpp

@ -118,51 +118,7 @@ Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
* in and out.
*/
bool Map::neighbors(Point &out, bool random, int direction) {
Matrix &paths = $paths.$paths;
bool zero_found = false;
// just make a list of the four directions
std::array<Point, 4> dirs{{
{out.x,out.y-1}, // north
{out.x+1,out.y}, // east
{out.x,out.y+1}, // south
{out.x-1,out.y} // west
}};
// get the current dijkstra number
int cur = paths[out.y][out.x];
// pick a random start of directions
// BUG: is uniform inclusive of the dir.size()?
int rand_start = Random::uniform<int>(0, dirs.size());
// go through all possible directions
for(size_t i = 0; i < dirs.size(); i++) {
// but start at the random start, effectively randomizing
// which valid direction to go
// BUG: this might be wrong given the above ranom from 0-size
Point dir = dirs[(i + rand_start) % dirs.size()];
if(!inmap(dir.x, dir.y)) continue; //skip unpathable stuff
int weight = cur - paths[dir.y][dir.x];
if(weight == direction) {
// no matter what we follow direct paths
out = dir;
return true;
} else if(random && weight == 0) {
// if random is selected and it's a 0 path take it
out = dir;
return true;
} else if(weight == 0) {
// otherwise keep the last zero path for after
out = dir;
zero_found = true;
}
}
// if we reach this then either zero was found and
// zero_found is set true, or it wasn't and nothing found
return zero_found;
return $paths.random_walk(out, random, direction);
}
bool Map::INVARIANT() {

@ -74,6 +74,53 @@ void Pathing::clear_target(const Point &at) {
$input[at.y][at.x] = 1;
}
bool Pathing::random_walk(Point &out, bool random, int direction) {
bool zero_found = false;
// just make a list of the four directions
std::array<Point, 4> dirs{{
{out.x,out.y-1}, // north
{out.x+1,out.y}, // east
{out.x,out.y+1}, // south
{out.x-1,out.y} // west
}};
// get the current dijkstra number
int cur = $paths[out.y][out.x];
// pick a random start of directions
// BUG: is uniform inclusive of the dir.size()?
int rand_start = Random::uniform<int>(0, dirs.size());
// go through all possible directions
for(size_t i = 0; i < dirs.size(); i++) {
// but start at the random start, effectively randomizing
// which valid direction to go
// BUG: this might be wrong given the above ranom from 0-size
Point dir = dirs[(i + rand_start) % dirs.size()];
if(!shiterator::inbounds($paths, dir.x, dir.y)) continue; //skip unpathable stuff
int weight = cur - $paths[dir.y][dir.x];
if(weight == direction) {
// no matter what we follow direct paths
out = dir;
return true;
} else if(random && weight == 0) {
// if random is selected and it's a 0 path take it
out = dir;
return true;
} else if(weight == 0) {
// otherwise keep the last zero path for after
out = dir;
zero_found = true;
}
}
// if we reach this then either zero was found and
// zero_found is set true, or it wasn't and nothing found
return zero_found;
}
bool Pathing::INVARIANT() {
using dbc::check;

@ -25,6 +25,7 @@ public:
Matrix &paths() { return $paths; }
Matrix &input() { return $input; }
int distance(Point to) { return $paths[to.y][to.x];}
bool random_walk(Point &out, bool random, int direction);
bool INVARIANT();
};

Loading…
Cancel
Save