Commit 9170ffe6 authored by Stuart John Watson's avatar Stuart John Watson

Merge branch 'vision' of gitlab.sucs.swan.ac.uk:rjames93/IdleCrawl into vision

parents 6751b7c4 8c0ce19a
#include <stack>
#include <queue>
#include <iostream>
#include <tuple>
#include "entity.h"
#include "../dungeon/space.h"
BasicSolver::BasicSolver(Dungeon &_dungeon,int _x,int _y) : Mob(_dungeon,_x,_y){
std::stack<std::tuple<int,int>> trail;
findSolution();
}
void BasicSolver::findSolution(){
std::queue<std::tuple<int,int>> toDo;
int tiles[dungeon.getWidth()*dungeon.getHeight()] = {0};
int height = dungeon.getHeight();
toDo.push(std::make_tuple(x,y));
tiles[x*height+y] = 1;
while (!toDo.empty()){
std::tuple<int,int> pos = toDo.front();
toDo.pop();
int doX = std::get<0>(pos);
int doY = std::get<1>(pos);
int doN = tiles[doX*height+doY]+1;
if(dungeon.getTile(doX,doY) == STAIRS_DOWN){
trail.push(std::make_tuple(doX,doY));
break;
}
if(tiles[(doX-1)*height+doY] == 0 && dungeon.getTile(doX-1,doY) != WALL){
tiles[(doX-1)*height+doY] = doN;
toDo.push(std::make_tuple(doX-1,doY));
}
if(tiles[(doX+1)*height+doY] == 0 && dungeon.getTile(doX+1,doY) != WALL){
tiles[(doX+1)*height+doY] = doN;
toDo.push(std::make_tuple(doX+1,doY));
}
if(tiles[doX*height+doY-1] == 0 && dungeon.getTile(doX,doY-1) != WALL){
tiles[doX*height+doY-1] = doN;
toDo.push(std::make_tuple(doX,doY-1));
}
if(tiles[doX*height+doY+1] == 0 && dungeon.getTile(doX,doY+1) != WALL){
tiles[doX*height+doY+1] = doN;
toDo.push(std::make_tuple(doX,doY+1));
}
}
if (trail.empty()){
std::cerr << "Could not solve" << std::endl;
return;
}
while (1){
std::tuple<int,int> pos = trail.top();
int doX = std::get<0>(pos);
int doY = std::get<1>(pos);
int doN = tiles[doX*height+doY]-1;
if (doN == 0) {break;}
if(tiles[(doX-1)*height+doY] == doN){
trail.push(std::make_tuple(doX-1,doY));
}
else if(tiles[(doX+1)*height+doY] == doN){
trail.push(std::make_tuple(doX+1,doY));
}
else if(tiles[doX*height+doY-1] == doN){
trail.push(std::make_tuple(doX,doY-1));
}
else if(tiles[doX*height+doY+1] == doN){
trail.push(std::make_tuple(doX,doY+1));
} else {
std::cerr << "ERR" << std::endl;
break;
}
}
}
void BasicSolver::turn(){
if (trail.empty()){
return;
//dungeon.populate();
std::tuple<int,int> start = dungeon.upStairPosition();
moveTo(std::get<0>(start),std::get<1>(start));
findSolution();
}
std::tuple<int,int> pos = trail.top();
int toX = std::get<0>(pos);
int toY = std::get<1>(pos);
trail.pop();
moveTo(toX,toY);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment