Line of sight update.

This commit is contained in:
Steve 2018-02-23 07:47:55 +00:00
parent 575ce1eadd
commit fc9b8d2daa
2 changed files with 34 additions and 63 deletions

View File

@ -20,44 +20,53 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "combat.h" #include "combat.h"
static int isBlockedByMap(Entity *src, Entity *dest);
static int isBlockedByEntities(Entity *src, Entity *dest);
int hasLineOfSight(Entity *src, Entity *dest) int hasLineOfSight(Entity *src, Entity *dest)
{ {
return (!isBlockedByMap(src, dest) && !isBlockedByEntities(src, dest)); int i, mx, my;
} float sx, sy, tx, ty, dx, dy;
Entity **candidates, *e;
static int isBlockedByMap(Entity *src, Entity *dest) SDL_Rect losBounds;
{
SDL_Rect mapBlock;
int x, y, x1, y1, x2, y2, mx, my, sx, sy, tx, ty;
x1 = (int) MIN(src->x, dest->x);
y1 = (int) MIN(src->y, dest->y);
x2 = (int) MAX(src->x, dest->x);
y2 = (int) MAX(src->y, dest->y);
sx = src->x + (src->w / 2); sx = src->x + (src->w / 2);
sy = src->y + (src->h / 2); sy = src->y + (src->h / 2);
tx = dest->x + (dest->w / 2); tx = dest->x + (dest->w / 2);
ty = dest->y + (dest->h / 2); ty = dest->y + (dest->h / 2);
for (x = x1; x <= x2; x++) losBounds.x = (int) MIN(src->x, dest->x);
losBounds.y = (int) MIN(src->y, dest->y);
losBounds.w = (int) (MAX(src->x, dest->x) - losBounds.x);
losBounds.h = (int) (MAX(src->y, dest->y) - losBounds.y);
candidates = getAllEntsWithin(losBounds.x, losBounds.y, losBounds.w, losBounds.h, NULL);
getSlope(sx, sy, tx, ty, &dx, &dy);
dx *= 8;
dy *= 8;
while (1)
{ {
for (y = y1; y <= y2; y++) sx -= dx;
sy -= dy;
mx = sx / MAP_TILE_SIZE;
my = sy / MAP_TILE_SIZE;
if (isSolid(mx, my))
{ {
mx = x / MAP_TILE_SIZE; return 0;
my = y / MAP_TILE_SIZE; }
if (isSolid(mx, my)) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{
if (e != src && collision(sx, sy, 8, 8, e->x, e->y, e->w, e->h))
{ {
mapBlock.x = x; if (e->isSolid)
mapBlock.y = y; {
mapBlock.w = MAP_TILE_SIZE; return 0;
mapBlock.h = MAP_TILE_SIZE; }
if (lineRectIntersection(sx, sy, tx, ty, &mapBlock)) if (e == dest)
{ {
return 1; return 1;
} }
@ -68,45 +77,6 @@ static int isBlockedByMap(Entity *src, Entity *dest)
return 0; return 0;
} }
static int isBlockedByEntities(Entity *src, Entity *dest)
{
Entity **candidates, *e;
SDL_Rect losBounds, eBounds;
int i, sx, sy, tx, ty;
losBounds.x = (int) MIN(src->x, dest->x);
losBounds.y = (int) MIN(src->y, dest->y);
losBounds.w = (int) (MAX(src->x, dest->x) - losBounds.x);
losBounds.h = (int) (MAX(src->y, dest->y) - losBounds.y);
sx = src->x + (src->w / 2);
sy = src->y + (src->h / 2);
tx = dest->x + (dest->w / 2);
ty = dest->y + (dest->h / 2);
candidates = getAllEntsWithin(losBounds.x, losBounds.y, losBounds.w, losBounds.h, NULL);
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{
if (e == src || e == dest || !e->isSolid)
{
continue;
}
eBounds.x = e->x;
eBounds.y = e->y;
eBounds.w = e->w;
eBounds.h = e->h;
if (lineRectIntersection(sx, sy, tx, ty, &eBounds))
{
return 1;
}
}
return 0;
}
int enemyCanSeePlayer(Entity *e) int enemyCanSeePlayer(Entity *e)
{ {
return hasLineOfSight(e, (Entity*)world.bob); return hasLineOfSight(e, (Entity*)world.bob);

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern int isSolid(int x, int y); extern int isSolid(int x, int y);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore);
extern int lineRectIntersection(int x1, int y1, int x2, int y2, SDL_Rect *r); extern void getSlope(int x1, int y1, int x2, int y2, float *dx, float *dy);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern World world; extern World world;