blobwarsAttrition/src/combat/combat.c

84 lines
1.8 KiB
C
Raw Normal View History

2018-01-21 17:50:14 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-01-21 17:50:14 +01:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "combat.h"
int hasLineOfSight(Entity *src, Entity *dest)
{
2018-02-23 08:47:55 +01:00
int i, mx, my;
float sx, sy, tx, ty, dx, dy;
Entity **candidates, *e;
SDL_Rect losBounds;
2019-06-02 17:13:34 +02:00
2018-02-09 23:33:38 +01:00
sx = src->x + (src->w / 2);
sy = src->y + (src->h / 2);
tx = dest->x + (dest->w / 2);
ty = dest->y + (dest->h / 2);
2019-06-02 17:13:34 +02:00
2018-01-21 17:50:14 +01:00
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);
2019-06-02 17:13:34 +02:00
2018-01-21 17:50:14 +01:00
candidates = getAllEntsWithin(losBounds.x, losBounds.y, losBounds.w, losBounds.h, NULL);
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
getSlope(sx, sy, tx, ty, &dx, &dy);
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
dx *= 8;
dy *= 8;
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
while (1)
2018-01-21 17:50:14 +01:00
{
2018-02-23 08:47:55 +01:00
sx -= dx;
sy -= dy;
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
mx = sx / MAP_TILE_SIZE;
my = sy / MAP_TILE_SIZE;
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
if (isSolid(mx, my))
2018-01-21 17:50:14 +01:00
{
2018-02-23 08:47:55 +01:00
return 0;
2018-01-21 17:50:14 +01:00
}
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
2018-01-21 17:50:14 +01:00
{
2018-02-23 08:47:55 +01:00
if (e != src && collision(sx, sy, 8, 8, e->x, e->y, e->w, e->h))
{
if (e->isSolid)
{
return 0;
}
2019-06-02 17:13:34 +02:00
2018-02-23 08:47:55 +01:00
if (e == dest)
{
return 1;
}
}
2018-01-21 17:50:14 +01:00
}
}
2019-06-02 17:13:34 +02:00
2018-01-21 17:50:14 +01:00
return 0;
}
2018-01-22 08:44:15 +01:00
int enemyCanSeePlayer(Entity *e)
{
return hasLineOfSight(e, (Entity*)world.bob);
2018-01-24 23:15:24 +01:00
}