Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Last active July 21, 2020 03:11
Show Gist options
  • Save maxgoren/20dbcb9efde1f634ba2fb89ba50b1631 to your computer and use it in GitHub Desktop.
Save maxgoren/20dbcb9efde1f634ba2fb89ba50b1631 to your computer and use it in GitHub Desktop.
After fixing a myriad of bugs, it works. now time for optimization.
struct Point {
int x;
int y;
char s;
bool blocks;
color_t color;
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
bool operator!=(const Point& other) const {
return x != other.x || y != other.y;
}
bool operator<(const Point& other) const {
return x < other.x || y < other.y;
}
Point operator=(Point other) {
x = other.x;
y = other.y;
s = other.s;
return other;
}
Point operator+(Point other) {
x += other.x;
y += other.y;
return Point({x,y,'F'});
}
};
class bFirst {
public:
World* map;
std::array<Point,8> cdir;
std::queue<Point> que;
std::set<Point> visited;
void addNeighbors(Point&);
void bFS(ent* g, Point* playerPos);
bool inBounds(Point&);
double long getDistance(Point A, Point B);
bFirst(World* map);
};
bFirst::bFirst(World* Map)
{
this->map = Map;
Point N({0,-1,'^'});Point S({0,1,'v'});
Point E({1,0,'>'});Point W({-1,0,'<'});
Point NW({1,-1,'<'});Point NE({1,1,'>'});
Point SW({-1,-1,'<'});Point SE({-1,1,'>'});
cdir[0]=N; cdir[1]=S; cdir[2]=E; cdir[3]=W;
cdir[4]=NE; cdir[5]=SW; cdir[6]=NW; cdir[7]=SE;
}
bool bFirst::inBounds(Point& p)
{
return 0 <= p.x && p.x < map->mapW
&& 0 <= p.y && p.y < map->mapH;
}
double long bFirst::getDistance(Point A, Point B)
{
int dx, dy;
double long dist;
dx = A.x - B.x;
dy = A.y - B.y;
return sqrtf(dx*dx+dy*dy);
}
void bFirst::addNeighbors(Point& p)
{
bool rediculos = false;
for (auto dir : cdir)
{
Point next({p.x + dir.x, p.y + dir.y, dir.s});
if (inBounds(next))
{
if (map->layout[next.x][next.y].blocks == false)
{
for (auto all : visited)
{
if (all == next) //this is just utterly insane.
{ rediculos = true; } //but ugly is better than broken.
}
if (rediculos == false) {
std::cout<<p.x<<"/"<<p.y<< " && "<<next.x<<"/"<<next.y<<" "<<" que size: "<<que.size()<<std::endl;
visited.insert(next);
que.push(next);
} rediculos = false;
}
}
}
}
void bFirst::bFS(ent* g, Point* playerPos)
{
Point start; //first point
Point current; //point being evaluated
Point next; //next point to be evaluated
Point headed; //the point chosen
double closest = 100; //arbitrary high number to start a < comparison
int x , y; //gird position
int dx, dy; //distance
double long dist; //euclidean distance;
start = g->pos; //goblins position is starting point
que.push(start);//add start to que
visited.insert(start);
while (!que.empty())
{
current = que.front();
if (current == *playerPos)
break;
que.pop();
visited.insert(current);
addNeighbors(current);
map->layout[current.x][current.y].s = current.s;
drawAll(map);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment