Commit 9e1e6fd0 authored by Stuart John Watson's avatar Stuart John Watson

Ported over old vision code.

 	modified:   CMakeLists.txt
 	modified:   src/entity/vision/alive/hero/basicSolverHero.cpp
 	modified:   src/entity/vision/alive/hero/hero.h
 	modified:   src/entity/vision/alive/mob/basicSolver.cpp
 	modified:   src/entity/vision/alive/mob/mob.h
 	modified:   src/entity/vision/alive/mob/randomMove.cpp
 	new file:   src/entity/vision/vision.cpp
 	modified:   src/entity/vision/vision.h
 	modified:   src/game/game.cpp
parent 31f4b42b
......@@ -15,6 +15,7 @@ add_executable(IdleCrawler
src/dungeon/rectangle.cpp
src/dungeon/space.cpp
src/entity/entity.cpp
src/entity/vision/vision.cpp
src/entity/vision/alive/alive.cpp
src/entity/vision/alive/mob/basicSolver.cpp
src/entity/vision/alive/mob/randomMove.cpp
......
......@@ -4,7 +4,7 @@
#include <tuple>
#include "hero.h"
BasicSolverHero::BasicSolverHero(Dungeon &_dungeon,int _x,int _y) : Hero(_dungeon,_x,_y){
BasicSolverHero::BasicSolverHero(Dungeon &_dungeon,int _x,int _y,int _viewDistance) : Hero(_dungeon,_x,_y,_viewDistance){
std::stack<std::tuple<int,int>> trail;
findSolution();
}
......
......@@ -15,7 +15,7 @@ class Hero: public Alive {
// Hero Basic Solver
class BasicSolverHero: public Hero {
public:
BasicSolverHero(Dungeon &,int,int);
BasicSolverHero(Dungeon &,int,int,int);
void turn();
void findSolution();
protected:
......
......@@ -4,7 +4,7 @@
#include <tuple>
#include "mob.h"
BasicSolver::BasicSolver(Dungeon &_dungeon,int _x,int _y) : Mob(_dungeon,_x,_y){
BasicSolver::BasicSolver(Dungeon &_dungeon,int _x,int _y,int _viewDistance) : Mob(_dungeon,_x,_y,_viewDistance){
std::stack<std::tuple<int,int>> trail;
findSolution();
}
......
......@@ -13,7 +13,7 @@ protected:
class RandomMove: public Mob {
public:
RandomMove(Dungeon &,int,int,int);
RandomMove(Dungeon &,int,int,int,int);
void turn();
protected:
std::default_random_engine generator;
......@@ -21,7 +21,7 @@ class RandomMove: public Mob {
class BasicSolver: public Mob {
public:
BasicSolver(Dungeon &,int,int);
BasicSolver(Dungeon &,int,int,int);
void turn();
protected:
void findSolution();
......
......@@ -6,7 +6,7 @@
//Random move example
RandomMove::RandomMove(Dungeon &_dungeon,int _x,int _y,int seed) : Mob(_dungeon,_x,_y){
RandomMove::RandomMove(Dungeon &_dungeon,int _x,int _y,int _viewDistance,int seed) : Mob(_dungeon,_x,_y,_viewDistance){
generator = std::default_random_engine(seed);
}
......
#include "vision.h"
#include "../entity.h"
#include <queue>
int sgn(int val){
return (0<val) - (val<0);
}
Vision::Vision(Dungeon &_dungeon,int _x,int _y,int _viewDistance) : Entity(_dungeon,_x,_y){
viewDistance = _viewDistance;
}
char Vision::_cstX(int xFr,int yFr,int xTo,int yTo){
double deltax = xTo-xFr;
double deltay = yTo-yFr;
double error = 0;
double deltaerr = abs(deltay/deltax);
int y = yFr;
for(int x=xFr;x<=xTo;x+=sgn(deltax)){
if (dungeon.getTile(x,y) == WALL){
return 0;
}
error += deltaerr;
while (error >= 0.5){
if (dungeon.getTile(x,y) == WALL){
return 0;
}
y += sgn(deltay);
error -= 1;
}
}
return 1;
}
char Vision::_cstY(int xFr,int yFr,int xTo,int yTo){
double deltax = xTo-xFr;
double deltay = yTo-yFr;
double error = 0;
double deltaerr = abs(deltax/deltay);
int x = xFr;
for(int y=yFr;y<yTo;y+=sgn(deltay)){
if (dungeon.getTile(x,y) == WALL){
return 0;
}
error += deltaerr;
while (error >= 0.5){
if (dungeon.getTile(x,y) == WALL){
return 0;
}
x += sgn(deltax);
error -= 1;
}
}
return 1;
}
//Returns 1 if can 0 if can't
char Vision::canSeeTile(int xFr,int yFr,int xTo,int yTo){
if (abs(xFr-xTo) > abs(yFr-yTo)){
return _cstX(xFr,yFr,xTo,yTo);
} else {
return _cstY(yFr,yFr,xTo,yTo);
}
}
void Vision::updateFOW(){
char *vMap = new char[viewDistance*viewDistance];
//Ray trace to each square
for(int sx=0;sx<viewDistance*2+1;sx++){
for(int sy=0;sy<viewDistance*2+1;sy++){
vMap[sx*viewDistance+sy] = canSeeTile(x,y,x-viewDistance+sx,y-viewDistance+sy);
}
}
//Flood fill to remove issues
std::queue<std::tuple<int,int>> toDo;
toDo.push(std::make_tuple(viewDistance,viewDistance));
while(!toDo.empty()){
std::tuple<int,int> pos = toDo.front();
toDo.pop();
int doX = std::get<0>(pos);
int doY = std::get<1>(pos);
if (vMap[doX*viewDistance+doY] != 1){
continue;
}
vMap[doX*viewDistance+doY] = 2;
if(doX>0){
toDo.push(std::make_tuple(doX-1,doY));
}
if(doY>0){
toDo.push(std::make_tuple(doX,doY-1));
}
if(doX<viewDistance*2+1){
toDo.push(std::make_tuple(doX+1,doY));
}
if(doY<viewDistance*2+1){
toDo.push(std::make_tuple(doX,doY+1));
}
}
//Update
for(int sx=0;sx<viewDistance*2+1;sx++){
for(int sy=0;sy<viewDistance*2+1;sy++){
if (vMap[sx*viewDistance+sy] == 2){
//TODO update
}
}
}
}
......@@ -5,10 +5,14 @@
class Vision: public Entity { // This class is supposed to give the hero, mob, anything else that needs vision it's field of view. Therefore everything that needs to see or react to the environment needs to inherit from it.
public:
using Entity::Entity;
//Vision(Dungeon &, int, int);
Vision(Dungeon&, int, int, int);
char _cstX(int, int, int, int);
char _cstY(int, int, int, int);
char canSeeTile(int, int, int, int);
void updateFOW();
protected:
TILE_TYPES *fov;
int viewDistance;
void fogofwar();
};
......
......@@ -54,7 +54,7 @@ void Game::loop(){
// Create hero at coordinates of the dungeon start
std::tuple<int,int> heroStart = levels.back().upStairPosition();
BasicSolverHero levelHero = BasicSolverHero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart));
BasicSolverHero levelHero = BasicSolverHero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),10 /*vision*/);
theHero = &levelHero;
// Generate nMobs
......@@ -71,7 +71,7 @@ void Game::loop(){
mobx = x_dist(generator);
moby = y_dist(generator);
}
std::shared_ptr<RandomMove> r(new RandomMove( levels.back(), mobx, moby, milliseconds_since_epoch+n));
std::shared_ptr<RandomMove> r(new RandomMove( levels.back(), mobx, moby, 0 /*vision*/, milliseconds_since_epoch+n));
entities.emplace_back( r );
}
......
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