Skip to content
Snippets Groups Projects
Commit 7d7cc676 authored by Ori Almog's avatar Ori Almog
Browse files

basic pathfinding

parent 410c33aa
No related branches found
No related tags found
No related merge requests found
#include "include/terrain.h"
#include "include/tiles.h"
#include "include/level.h"
#include "include/coord.h"
#include <vector>
#include <queue>
#include <map>
#include <math.h>
Algs::Algs(){}
void Algs::addPerps(Coord current, &std::vector<Coord> deltas, Level& lvl, std::map<Coord, Coord>& parents){
Coord target;
target = current + Coord(1, 0);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(-1, 0);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(0, 1);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(0, -1);
tryAdd(current, delta, lvl, parents, target);
}
void Algs::addDiags(Coord current, std::vector<Coord>& deltas, Level& lvl, std::map<Coord, Coord>& parents){
Coord target;
target = current + Coord(1, 1);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(-1, 1);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(-1, -1);
tryAdd(current, delta, lvl, parents, target);
target = current + Coord(1, -1);
tryAdd(current, delta, lvl, parents, target);
}
void Algs::tryAdd(Coord current, std::vector<Coord>& deltas, Level& lvl, std::map<Coord, Coord>& parents, Coord target){
if(lvl[target].isPassable() == Terrain::Passable && !lvl[target].checked){
delta.push_back(target);
lvl[target].checked = true;
parents[target, current];
}
}
std::vector<Coord> Algs::bfsDiag(Coord start, Coord end, Level& lvl){
std::map<Coord, Coord> parents;
std::queue<Coord> delta;
delta.push(start.copy());
while(!delta.empty()){
Coord current = delta.front();
delta.pop();
if (current == end){
break;
}
addPerps(current, delta, lvl, parents);
addDiags(current, delta, lvl, parents);
}
}
std::vector<Coord> Algs::bfsPerp(Coord start, Coord end){
}
std::vector<Coord> Algs::getPath(std::vector<Coord> delta, Coord start){
}
\ No newline at end of file
#pragma once
#include "include/terrain.h"
#include "include/tiles.h"
#include "include/level.h"
#include "include/coord.h"
#include <map>
#include <vector>
class Level;
class Algs {
public:
Algs();
//Performs BFS with diagonals
std::vector<Coord> bfsDiag(Coord, Coord);
//Performs BFS without diagonals
std::vector<Coord> bfsPerp(Coord, Coord);
//Gets a path given a delta vector and starting position
std::vector<Coord> getPath(Coord, std::vector<Coord>);
private:
//Add perpendicular coords
void addPerps(Coord, std::vector<Coord>, Level* , std::map<Coord, Coord>);
//Add diagonal coords
void addDiags(Coord, std::vector<Coord>, Level* , std::map<Coord, Coord>);
//Add diagonal coords
void tryAdd(Coord, std::vector<Coord>, Level* , std::map<Coord, Coord>, Coord);
};
\ No newline at end of file
......@@ -11,6 +11,7 @@ class Terrain {
char getSymbol();
Passability isPassable();
Visibility getVisibility();
bool checked = false;
private:
char character;
Visibility visible;
......
......@@ -16,4 +16,4 @@ Terrain::Passability Terrain::isPassable() {
Terrain::Visibility Terrain::getVisibility() {
return visible;
}
}
\ No newline at end of file
......@@ -91,6 +91,7 @@ void Tunnel::dig(Level& level){
delta *= -1;
}
//TODO: This is fine, but after the shuffle it might still break!
if (!p->touches(current + delta) && !q->touches(current + delta)){
path.push_back(delta);
current += delta;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment