Commit c15c8b86 authored by Stuart John Watson's avatar Stuart John Watson

Fixed Vision and implemented old marking stuffs!

*(the problem was using abs instead of fabs)*

	modified:   src/dungeon/types.h
	modified:   src/entity/vision/alive/hero/hero.cpp
	modified:   src/entity/vision/alive/hero/hero.h
	modified:   src/entity/vision/vision.cpp
	modified:   src/entity/vision/vision.h
	modified:   src/game/game.cpp
parent 35731358
......@@ -8,10 +8,11 @@ enum TILE_TYPES {
FLOOR,
DOOR,
STAIRS_UP,
STAIRS_DOWN
STAIRS_DOWN,
UNSEEN
};
const char TILES_TYPES_MAP[]= {' ','#','.','+','^','v'};
const int TILE_COLOUR_MAP[] = {0x607D8B,0x212121,0xFAFAFA,0x795548,0x4CAF50,0xFF9800};
const char TILES_TYPES_MAP[]= {' ','#','.','+','^','v','+'};
const int TILE_COLOUR_MAP[] = {0x607D8B,0x212121,0xFAFAFA,0x795548,0x4CAF50,0xFF9800,0x607D8B};
enum LOCATION {
TOP_LEFT, TOP_CENTER, TOP_RIGHT,
......@@ -19,4 +20,4 @@ enum LOCATION {
BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT
};
#endif
\ No newline at end of file
#endif
......@@ -16,3 +16,7 @@ bool Hero::levelComplete(){
return(false);
}
}
void Hero::turn(){ //Helpful for debugging
updateFOW();
}
......@@ -8,6 +8,7 @@ class Hero: public Alive {
using Alive::Alive;
//Hero(Dungeon &, int, int);
bool levelComplete();
void turn();
protected:
};
......
......@@ -11,57 +11,78 @@ Vision::Vision(Dungeon &_dungeon,int _x,int _y,int _viewDistance) : Entity(_dung
viewDistance = _viewDistance;
fov = new TILE_TYPES[dungeon.getWidth()*dungeon.getHeight()];
for(int i=0;i<dungeon.getWidth()*dungeon.getHeight();i++){
fov[i] = UNSET;
fov[i] = UNSEEN;
}
canSee = new char[(viewDistance*2+1)*(viewDistance*2+1)];
}
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);
char Vision::canSeeTile(int xFr,int yFr,int xTo,int yTo){
int y = yFr;
for(int x=xFr;x!=xTo;x+=sgn(deltax)){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
}
error += deltaerr;
while (error >= 0.5){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
}
y += sgn(deltay);
error -= 1;
}
float deltaX = xTo-xFr;
float deltaY = yTo-yFr;
char majorX;
int majorC;
int major1;
int major2;
float m;
int minor;
int minorC;
//std::cerr << "Checking line:" << xFr << "," << yFr << " -> " << xTo << "," << yTo << std::endl;
if(deltaX == 0 && deltaY == 0){
return 1;
} else if (xFr < 0 || xFr >= dungeon.getWidth() || yFr < 0 || yFr >= dungeon.getHeight()){
return 0;
} else if (abs(deltaX) > abs(deltaY)){
//std::cerr << "\tLine is major X" << std::endl;
majorX = 1;
majorC = sgn(deltaX);
major1 = xFr;
major2 = xTo;
m = fabs(deltaY/deltaX);
minor = yFr;
minorC = sgn(deltaY);
} else {
//std::cerr << "\tLine is major Y" << std::endl;
majorX = 0;
majorC = sgn(deltaY);
major1 = yFr;
major2 = yTo;
m = fabs(deltaX/deltaY);
minor = xFr;
minorC = sgn(deltaX);
}
return 1;
}
//std::cerr << "\t\t(m:" << m << ")" << std::endl;
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);
float err = 0;
int x = xFr;
for(int y=yFr;y!=yTo;y+=sgn(deltay)){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
major1+=majorC;
for(int major=major1;major!=major2;major+=majorC){
err+=m;
//std::cerr << " (err:" << err << ")" << std::endl;
if (err >= 0.5){
minor += minorC;
err--;
}
error += deltaerr;
while (error >= 0.5){
if (dungeon.getTile(x,y) != FLOOR){
if (majorX){
//std::cerr << "\t\tLooking up" << major << "," << minor;
if (dungeon.getTile(major,minor) == WALL){
//std::cerr << " Wall found." << std::endl;
return 0;
}
} else {
//std::cerr << "\t\tLooking up" << minor << "," << major;
if (dungeon.getTile(minor,major) == WALL){
//std::cerr << " Wall found." << std::endl;
return 0;
}
x += sgn(deltax);
error -= 1;
}
}
return 1;
}
//Returns 1 if can 0 if can't
/*/Returns 1 if can 0 if can't
char Vision::canSeeTile(int xFr,int yFr,int xTo,int yTo){
if (xFr == xTo && yFr == yTo){
return 1;
......@@ -73,18 +94,19 @@ char Vision::canSeeTile(int xFr,int yFr,int xTo,int yTo){
return _cstY(yFr,yFr,xTo,yTo);
}
}
*/
void Vision::updateFOW(){
const int VIEW_DIAM = viewDistance*2+1;
char *vMap = new char[VIEW_DIAM*VIEW_DIAM];
//Ray trace to each square
for(int sx=0;sx<VIEW_DIAM;sx++){
for(int sy=0;sy<VIEW_DIAM;sy++){
vMap[sy*VIEW_DIAM+sx] = canSeeTile(x,y,x+sx-viewDistance,y+sy-viewDistance);
canSee[sy*VIEW_DIAM+sx] = canSeeTile(x+sx-viewDistance,y+sy-viewDistance,x,y);
}
}
std::cerr << "view bounds" << (x-viewDistance) << "," << (y-viewDistance) << " to " << (x+viewDistance) << "," << (y+viewDistance) << std::endl;
//std::cerr << "view bounds" << (x-viewDistance) << "," << (y-viewDistance) << " to " << (x+viewDistance) << "," << (y+viewDistance) << std::endl;
/*/Flood fill to remove issues
......@@ -119,7 +141,7 @@ void Vision::updateFOW(){
//Update
for(int sx=0;sx<VIEW_DIAM;sx++){
for(int sy=0;sy<VIEW_DIAM;sy++){
if (vMap[sy*VIEW_DIAM+sx] == 1){ //Change back to 2 when uncommenting the above!!!
if (canSee[sy*VIEW_DIAM+sx] == 1){ //Change back to 2 when uncommenting the above!!!
int mapX = (x+sx-viewDistance);
int mapY = (y+sy-viewDistance);
fov[ mapY*dungeon.getWidth() + mapX ] = dungeon.getTile(mapX,mapY);
......@@ -131,11 +153,18 @@ void Vision::updateFOW(){
void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){
Colormap colormap;
colormap = DefaultColormap(dpy, 0);
const int VIEW_DIAM = viewDistance*2+1;
for(int x=0;x<dungeon.getWidth();x++){
for(int y=0;y<dungeon.getHeight();y++){
XSetForeground(dpy, gc, TILE_COLOUR_MAP[fov[ y*dungeon.getWidth() + x ]]);
XFillRectangle (dpy, w, gc, x*scale, y*scale, scale, scale);
for(int dx=0;dx<dungeon.getWidth();dx++){
for(int dy=0;dy<dungeon.getHeight();dy++){
int colour = TILE_COLOUR_MAP[fov[ dy*dungeon.getWidth() + dx ]];
int lx = dx-x+viewDistance;
int ly = dy-y+viewDistance;
if (lx<0 || ly<0 || lx>=VIEW_DIAM || ly>=VIEW_DIAM || canSee[ly*VIEW_DIAM+lx] == 0){
colour &= 0x555555;
}
XSetForeground(dpy, gc, colour);
XFillRectangle (dpy, w, gc, dx*scale, dy*scale, scale, scale);
}
}
}
......@@ -6,14 +6,13 @@
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:
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();
void drawLevel(Display*&,Window&,GC&,int);
protected:
TILE_TYPES *fov;
int viewDistance;
char *canSee;
void fogofwar();
};
......
......@@ -54,7 +54,8 @@ 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),20 /*vision*/);
BasicSolverHero levelHero = BasicSolverHero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),256 /*vision*/);
//Hero levelHero = Hero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),20 /*vision*/);
theHero = &levelHero;
// Generate nMobs
......
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