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

Tunnels work using real pathfinding

parent cb9a7c4e
No related branches found
No related tags found
No related merge requests found
......@@ -66,7 +66,7 @@ class Level {
static const int ROOM_PADDING = 2;
static const int MIN_ROOM_DIM = 3;
static const int X_SIZE = 80, Y_SIZE = 35;//80,25
std::vector<std::vector<Terrain> > tiles;
std::vector<std::vector<Terrain>> tiles;
std::vector<Room> rooms;
std::vector<ClockItem> mobs;
std::vector<GoldPile> golds;
......@@ -77,14 +77,14 @@ class Level {
int depth;
//Add perpendicular coords
void addPerps(Coord, std::queue<Coord>&, std::map<Coord, Coord>&);
void addPerps(Coord, std::queue<Coord>&);
//Add diagonal coords
void addDiags(Coord, std::queue<Coord>&, std::map<Coord, Coord>&);
void addDiags(Coord, std::queue<Coord>&);
//Try to add a coord to the q
void tryAddPassable(Coord, std::queue<Coord>&, std::map<Coord, Coord>&, Coord);
void tryAddPassable(Coord, std::queue<Coord>&, Coord);
//Try to add a coord to the q
void tryAdd(Coord, std::queue<Coord>&, std::map<Coord, Coord>&, Coord);
void tryAdd(Coord, std::queue<Coord>&, Coord);
};
#include "coord.h"
#ifndef TERRAIN_H
#define TERRAIN_H
class Coord;
class Terrain {
public:
enum Passability {Blocked, Passable};
......@@ -12,6 +15,7 @@ class Terrain {
Passability isPassable();
Visibility getVisibility();
bool checked = false;
Coord parent;
private:
char character;
Visibility visible;
......
......@@ -188,83 +188,85 @@ void Level::addTunnel(int i, int j, bool* a, bool* b, Generator gen){
}
}
void Level::addPerps(Coord current, std::queue<Coord>& deltas, std::map<Coord, Coord>& parents){
void Level::addPerps(Coord current, std::queue<Coord>& deltas){
Coord target;
target = current + Coord(1, 0);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(-1, 0);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(0, 1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(0, -1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
}
void Level::addDiags(Coord current, std::queue<Coord>& deltas, std::map<Coord, Coord>& parents){
void Level::addDiags(Coord current, std::queue<Coord>& deltas){
Coord target;
target = current + Coord(1, 1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(-1, 1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(-1, -1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
target = current + Coord(1, -1);
tryAdd(current, deltas, parents, target);
tryAdd(current, deltas, target);
}
void Level::tryAddPassable(Coord current, std::queue<Coord>& deltas, std::map<Coord, Coord>& parents, Coord target){
void Level::tryAddPassable(Coord current, std::queue<Coord>& q, Coord target){
if (target[0] > 0 && target[0] < size[0] && target[1] > 0 && target[1] < size[1]){
if(tiles[target[0]][target[1]].isPassable() == Terrain::Passable && !tiles[target[0]][target[1]].checked){
deltas.push(target.copy());
q.push(target.copy());
tiles[target[0]][target[1]].checked = true;
Coord t_ = target.copy();
Coord c_ = current.copy();
parents.insert(std::make_pair(t_, c_));
//parents.insert(std::make_pair(t_, c_));
}
}
}
void Level::tryAdd(Coord current, std::queue<Coord>& deltas, std::map<Coord, Coord>& parents, Coord target){
void Level::tryAdd(Coord current, std::queue<Coord>& deltas, Coord target){
if (target[0] > 0 && target[0] < size[0] && target[1] > 0 && target[1] < size[1]){
if(!tiles[target[0]][target[1]].checked){
if(!(tiles[target[0]][target[1]].checked)){
Coord t_ = target.copy();
Coord c_ = current.copy();
Coord p_ = target.copy();
deltas.push(p_);
Coord f_ = target.copy();
tiles[target[0]][target[1]].checked = true;
deltas.push(p_);
parents.insert(std::make_pair(t_, c_));
tiles[f_[0]][f_[1]].checked = true;
tiles[f_[0]][f_[1]].parent = c_;
}
}
}
std::vector<Coord> Level::bfsDiag(Coord start, Coord end){
/*
for (auto x=0; x < size[0]; x++) {
for (auto y=0; y < size[1]; y++) {
tiles[x][y].checked = false;
}
}
std::map<Coord, Coord> parents;
std::queue<Coord> delta;
delta.push(start.copy());
......@@ -277,8 +279,8 @@ std::vector<Coord> Level::bfsDiag(Coord start, Coord end){
break;
}
addPerps(current, delta, parents);
addDiags(current, delta, parents);
//addPerps(current, delta, parents);
//addDiags(current, delta, parents);
}
//TODO: Put this into its own function
......@@ -287,29 +289,33 @@ std::vector<Coord> Level::bfsDiag(Coord start, Coord end){
while(current != start){
path.push_back(current);
current = parents[current].copy();
current = parent[current[0]][current[1]].copy();
}
path.push_back(start.copy());
return path;
*/
}
std::vector<Coord> Level::bfsPerp(Coord start, Coord end){
std::cout << "AAA" << std::endl;
for (auto x=0; x < size[0]; x++) {
for (auto y=0; y < size[1]; y++) {
tiles[x][y].checked = false;
tiles[x][y].parent = Coord(0,0);
}
}
std::map<Coord, Coord> parents;
std::queue<Coord> delta;
delta.push(start.copy());
std::queue<Coord> q;
q.push(start.copy());
while(!delta.empty()){
while(!q.empty()){
Coord current = delta.front();
delta.pop();
Coord current = q.front().copy();
q.pop();
if (current == end){
break;
......@@ -318,49 +324,38 @@ std::vector<Coord> Level::bfsPerp(Coord start, Coord end){
Coord target;
target = current + Coord(1, 0);
tryAdd(current, delta, parents, target);
tryAdd(current, q, target);
target = current + Coord(-1, 0);
tryAdd(current, delta, parents, target);
tryAdd(current, q, target);
target = current + Coord(0, 1);
tryAdd(current, delta, parents, target);
tryAdd(current, q, target);
target = current + Coord(0, -1);
tryAdd(current, delta, parents, target);
tryAdd(current, q, target);
}
//TODO: Put this into its own function
std::vector<Coord> path;
Coord current = end.copy();
/*
std::cout << "FEF" << std::endl;
std::cout << "Parents:" << std::endl;
std::map<Coord, Coord>::iterator it = parents.begin();
while(it != parents.end())
{
std::cout << (it->first.toString()) << " ---> " << (it->second.toString()) << " (" << ((it->second)-(it->first)).toString() << ")" << std::endl;
it++;
}
std::vector<Coord> path;
Coord current = end.copy();
std::cout << "Going from: " << start[0] << ", " << start[1] << std::endl;
std::cout << "Going to: " << end[0] << ", " << end[1] << std::endl;
*/
int count = 0;
//std::cout << current[0] << ", " << current[1] << std::endl;
std::cout << current[0] << ", " << current[1] << std::endl;
while(current != start){
Coord c_ = current.copy();
path.push_back(c_);
current = parents[current].copy();
current = tiles[current[0]][current[1]].parent.copy();
//std::cout << current[0] << ", " << current[1] << std::endl;
std::cout << current[0] << ", " << current[1] << std::endl;
count++;
......@@ -370,6 +365,9 @@ std::vector<Coord> Level::bfsPerp(Coord start, Coord end){
}
}
std::cout << "Start: " << start[0] << ", " << start[1] << std::endl;
std::cout << "Current: " << current[0] << ", " << current[1] << std::endl;
path.push_back(start.copy());
return path;
......
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