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,89 +20,59 @@ 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++)
{
for (y = y1; y <= y2; y++)
{
mx = x / MAP_TILE_SIZE;
my = y / MAP_TILE_SIZE;
if (isSolid(mx, my))
{
mapBlock.x = x;
mapBlock.y = y;
mapBlock.w = MAP_TILE_SIZE;
mapBlock.h = MAP_TILE_SIZE;
if (lineRectIntersection(sx, sy, tx, ty, &mapBlock))
{
return 1;
}
}
}
}
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.x = (int) MIN(src->x, dest->x);
losBounds.y = (int) MIN(src->y, dest->y); losBounds.y = (int) MIN(src->y, dest->y);
losBounds.w = (int) (MAX(src->x, dest->x) - losBounds.x); losBounds.w = (int) (MAX(src->x, dest->x) - losBounds.x);
losBounds.h = (int) (MAX(src->y, dest->y) - losBounds.y); 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); candidates = getAllEntsWithin(losBounds.x, losBounds.y, losBounds.w, losBounds.h, NULL);
getSlope(sx, sy, tx, ty, &dx, &dy);
dx *= 8;
dy *= 8;
while (1)
{
sx -= dx;
sy -= dy;
mx = sx / MAP_TILE_SIZE;
my = sy / MAP_TILE_SIZE;
if (isSolid(mx, my))
{
return 0;
}
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{ {
if (e == src || e == dest || !e->isSolid) if (e != src && collision(sx, sy, 8, 8, e->x, e->y, e->w, e->h))
{ {
continue; if (e->isSolid)
{
return 0;
} }
eBounds.x = e->x; if (e == dest)
eBounds.y = e->y;
eBounds.w = e->w;
eBounds.h = e->h;
if (lineRectIntersection(sx, sy, tx, ty, &eBounds))
{ {
return 1; return 1;
} }
} }
}
}
return 0; return 0;
} }

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;