diff --git a/map.cpp b/map.cpp index 77ddf6c..6f3f7d1 100644 --- a/map.cpp +++ b/map.cpp @@ -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 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(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() { diff --git a/pathing.cpp b/pathing.cpp index 8cc3bba..c58f3e6 100644 --- a/pathing.cpp +++ b/pathing.cpp @@ -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 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(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; diff --git a/pathing.hpp b/pathing.hpp index 4fa71fd..1f62073 100644 --- a/pathing.hpp +++ b/pathing.hpp @@ -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(); };