Commit 6751b7c4 authored by Stuart John Watson's avatar Stuart John Watson

Vision sort of works, it fails to correctlyl do line of sisight though

parent 3571f621
......@@ -143,4 +143,4 @@ void Space::draw(Display*& dpy,Window& w,GC& gc,int scale){
XFillRectangle (dpy, w, gc, x*scale, y*scale, scale, scale);
}
}
}
\ No newline at end of file
}
......@@ -82,6 +82,7 @@ void BasicSolverHero::findSolution(){
}
void BasicSolverHero::turn(){
updateFOW();
if (trail.empty()){
return;
//dungeon.populate();
......
#include "vision.h"
#include "../entity.h"
#include <queue>
#include <iostream>
int sgn(int val){
return (0<val) - (val<0);
......@@ -8,6 +9,10 @@ int sgn(int val){
Vision::Vision(Dungeon &_dungeon,int _x,int _y,int _viewDistance) : Entity(_dungeon,_x,_y){
viewDistance = _viewDistance;
fov = new TILE_TYPES[dungeon.getWidth()*dungeon.getHeight()];
for(int i=0;i<dungeon.getWidth()*dungeon.getHeight();i++){
fov[i] = UNSET;
}
}
char Vision::_cstX(int xFr,int yFr,int xTo,int yTo){
......@@ -40,7 +45,7 @@ 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)){
for(int y=yFr;y<=yTo;y+=sgn(deltay)){
if (dungeon.getTile(x,y) == WALL){
return 0;
}
......@@ -66,16 +71,18 @@ char Vision::canSeeTile(int xFr,int yFr,int xTo,int yTo){
}
void Vision::updateFOW(){
char *vMap = new char[viewDistance*viewDistance];
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<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);
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);
}
}
//Flood fill to remove issues
/*/Flood fill to remove issues
std::queue<std::tuple<int,int>> toDo;
toDo.push(std::make_tuple(viewDistance,viewDistance));
while(!toDo.empty()){
......@@ -85,7 +92,7 @@ void Vision::updateFOW(){
int doX = std::get<0>(pos);
int doY = std::get<1>(pos);
if (vMap[doX*viewDistance+doY] != 1){
if (vMap[doX*(viewDistance*2+1)+doY] != 1){
continue;
}
vMap[doX*viewDistance+doY] = 2;
......@@ -102,14 +109,28 @@ void Vision::updateFOW(){
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
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!!!
int mapX = (x+sx-viewDistance);
int mapY = (y+sy-viewDistance);
fov[ mapX*dungeon.getHeight() + mapY ] = dungeon.getTile(mapX,mapY);
}
}
}
}
void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){
Colormap colormap;
colormap = DefaultColormap(dpy, 0);
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]]);
XFillRectangle (dpy, w, gc, x*scale, y*scale, scale, scale);
}
}
}
......@@ -10,6 +10,7 @@ class Vision: public Entity { // This class is supposed to give the hero, mob, a
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;
......
......@@ -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),10 /*vision*/);
BasicSolverHero levelHero = BasicSolverHero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),20 /*vision*/);
theHero = &levelHero;
// Generate nMobs
......@@ -82,32 +82,29 @@ void Game::loop(){
while(theHero->getCurrentHealth() > 0){
usleep(10000);
// Draw the Dungeon
levels.back().draw(dpy,double_buffer,gc,scale);
// Take hero turn.
theHero->turn();
// Now all the other Entities take turns but none extra for now
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
usleep(10000);
// Take hero turn.
theHero->turn();
// Now all the other Entities take turns but none extra for now
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->turn();
}
}
// Draw the Dungeon
theHero->drawLevel(dpy,double_buffer,gc,scale);
// Now draw the hero
theHero->draw(dpy,double_buffer,gc,scale);
// Now draw the hero
theHero->draw(dpy,double_buffer,gc,scale);
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->draw(dpy,double_buffer,gc,scale);
}
}
XCopyArea(dpy, double_buffer, w, gc, 0, 0, wa.width, wa.height, 0, 0);
XFlush(dpy);
if(theHero->levelComplete() == true){
XCopyArea(dpy, double_buffer, w, gc, 0, 0, wa.width, wa.height, 0, 0);
XFlush(dpy);
if(theHero->levelComplete() == true){
break;
}
}
}
if(theHero->getCurrentHealth() == 0){
......
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