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

Fixed Program segfaulting when trying to see out of bounds and silently...

Fixed Program segfaulting when trying to see out of bounds and silently crashing when seeing own square

Issues:
 * Cannot see North or South
 * Still sees through walls

	modified:   ../src/entity/vision/vision.cpp
parent 9170ffe6
......@@ -22,13 +22,13 @@ char Vision::_cstX(int xFr,int yFr,int xTo,int yTo){
double deltaerr = abs(deltay/deltax);
int y = yFr;
for(int x=xFr;x<=xTo;x+=sgn(deltax)){
if (dungeon.getTile(x,y) == WALL){
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) == WALL){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
}
y += sgn(deltay);
......@@ -45,13 +45,13 @@ char Vision::_cstY(int xFr,int yFr,int xTo,int yTo){
double deltaerr = abs(deltax/deltay);
int x = xFr;
for(int y=yFr;y<=yTo;y+=sgn(deltay)){
if (dungeon.getTile(x,y) == WALL){
for(int y=yFr;y!=yTo;y+=sgn(deltay)){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
}
error += deltaerr;
while (error >= 0.5){
if (dungeon.getTile(x,y) == WALL){
if (dungeon.getTile(x,y) != FLOOR){
return 0;
}
x += sgn(deltax);
......@@ -63,7 +63,11 @@ char Vision::_cstY(int xFr,int yFr,int xTo,int yTo){
//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)){
if (xFr == xTo && yFr == yTo){
return 1;
} else if (xTo < 0 || xTo >= dungeon.getWidth() || yTo < 0 || yTo >= dungeon.getHeight() ){
return 0;
} else if (abs(xFr-xTo) > abs(yFr-yTo)){
return _cstX(xFr,yFr,xTo,yTo);
} else {
return _cstY(yFr,yFr,xTo,yTo);
......@@ -77,9 +81,10 @@ void Vision::updateFOW(){
//Ray trace to each square
for(int sx=0;sx<VIEW_DIAM;sx++){
for(int sy=0;sy<VIEW_DIAM;sy++){
vMap[sx*VIEW_DIAM+sy] = canSeeTile(x,y,x+sx-viewDistance,y+sy-viewDistance);
vMap[sy*VIEW_DIAM+sx] = canSeeTile(x,y,x+sx-viewDistance,y+sy-viewDistance);
}
}
std::cerr << "view bounds" << (x-viewDistance) << "," << (y-viewDistance) << " to " << (x+viewDistance) << "," << (y+viewDistance) << std::endl;
/*/Flood fill to remove issues
......@@ -114,10 +119,10 @@ void Vision::updateFOW(){
//Update
for(int sx=0;sx<VIEW_DIAM;sx++){
for(int sy=0;sy<VIEW_DIAM;sy++){
if (vMap[sx*VIEW_DIAM+sy] == 1){ //Change back to 2 when uncommenting the above!!!
if (vMap[sy*VIEW_DIAM+sx] == 1){ //Change back to 2 when uncommenting the above!!!
int mapX = (x+sx-viewDistance);
int mapY = (y+sy-viewDistance);
fov[ mapX*dungeon.getHeight() + mapY ] = dungeon.getTile(mapX,mapY);
fov[ mapY*dungeon.getWidth() + mapX ] = dungeon.getTile(mapX,mapY);
}
}
}
......@@ -129,7 +134,7 @@ void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){
for(int x=0;x<dungeon.getWidth();x++){
for(int y=0;y<dungeon.getHeight();y++){
XSetForeground(dpy, gc, TILE_COLOUR_MAP[fov[x*dungeon.getHeight()+y]]);
XSetForeground(dpy, gc, TILE_COLOUR_MAP[fov[ y*dungeon.getWidth() + x ]]);
XFillRectangle (dpy, w, gc, x*scale, y*scale, scale, scale);
}
}
......
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