From fc9b8d2daa2929d01ed30d7c23f1aa397fc8baab Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 23 Feb 2018 07:47:55 +0000 Subject: [PATCH] Line of sight update. --- src/combat/combat.c | 94 +++++++++++++++------------------------------ src/combat/combat.h | 3 +- 2 files changed, 34 insertions(+), 63 deletions(-) diff --git a/src/combat/combat.c b/src/combat/combat.c index bc632aa..48e14c7 100644 --- a/src/combat/combat.c +++ b/src/combat/combat.c @@ -20,87 +20,57 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "combat.h" -static int isBlockedByMap(Entity *src, Entity *dest); -static int isBlockedByEntities(Entity *src, Entity *dest); - int hasLineOfSight(Entity *src, Entity *dest) { - return (!isBlockedByMap(src, dest) && !isBlockedByEntities(src, dest)); -} - -static int isBlockedByMap(Entity *src, Entity *dest) -{ - 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); + int i, mx, my; + float sx, sy, tx, ty, dx, dy; + Entity **candidates, *e; + SDL_Rect losBounds; sx = src->x + (src->w / 2); sy = src->y + (src->h / 2); tx = dest->x + (dest->w / 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.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]) + + getSlope(sx, sy, tx, ty, &dx, &dy); + + dx *= 8; + dy *= 8; + + while (1) { - if (e == src || e == dest || !e->isSolid) + sx -= dx; + sy -= dy; + + mx = sx / MAP_TILE_SIZE; + my = sy / MAP_TILE_SIZE; + + if (isSolid(mx, my)) { - continue; + return 0; } - eBounds.x = e->x; - eBounds.y = e->y; - eBounds.w = e->w; - eBounds.h = e->h; - - if (lineRectIntersection(sx, sy, tx, ty, &eBounds)) + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) { - return 1; + if (e != src && collision(sx, sy, 8, 8, e->x, e->y, e->w, e->h)) + { + if (e->isSolid) + { + return 0; + } + + if (e == dest) + { + return 1; + } + } } } diff --git a/src/combat/combat.h b/src/combat/combat.h index a75cb41..8cffa30 100644 --- a/src/combat/combat.h +++ b/src/combat/combat.h @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern int isSolid(int x, int y); 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;