From 9c279da3a28d08cc55bf3c4207eb206797f60dd6 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 24 Feb 2018 15:59:26 +0000 Subject: [PATCH] Ensure that sprite frames don't overrun array bounds. --- src/entities/blobs/bob.c | 19 ++++++++++++++++--- src/entities/entity.c | 15 ++++++++++++++- src/entities/evilBlobs/evilBlob.c | 8 +++++++- src/entities/evilBlobs/evilBlob.h | 4 ++-- src/entities/eyeDroids/eyeDroid.c | 2 +- src/entities/eyeDroids/eyeDroid.h | 4 ++-- src/entities/unit.c | 10 +++++++--- 7 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/entities/blobs/bob.c b/src/entities/blobs/bob.c index 58ae00e..dd1c7c8 100644 --- a/src/entities/blobs/bob.c +++ b/src/entities/blobs/bob.c @@ -645,17 +645,30 @@ static void die(void) static SDL_Rect *getCurrentSprite(void) { - if (world.bob->alive == ALIVE_ALIVE && world.bob->stunTimer <= 0) + Sprite *s; + + s = (world.bob->alive == ALIVE_ALIVE && world.bob->stunTimer <= 0) ? world.bob->sprite[world.bob->facing] : world.bob->sprite[FACING_DIE]; { return &world.bob->sprite[world.bob->facing]->frames[world.bob->spriteFrame]->rect; } - return &world.bob->sprite[FACING_DIE]->frames[world.bob->spriteFrame]->rect; + if (world.bob->spriteFrame >= s->numFrames) + { + world.bob->spriteFrame = 0; + } + + return &s->frames[world.bob->spriteFrame]->rect; } static void animate(void) { - if (world.bob->dx != 0 || world.bob->stunTimer > 0 || world.bob->flags & EF_WEIGHTLESS || world.bob->health <= 0) + if (world.bob->stunTimer > 0 || world.bob->alive != ALIVE_ALIVE) + { + world.bob->facing = FACING_DIE; + + superAnimate(); + } + else if (world.bob->dx != 0 || world.bob->flags & EF_WEIGHTLESS) { superAnimate(); } diff --git a/src/entities/entity.c b/src/entities/entity.c index 0dd7ccd..1287b9c 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -160,9 +160,22 @@ static void changeEnvironment(void) { } +void entityIdle(void) +{ +} + static SDL_Rect *getCurrentSprite(void) { - return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; + Sprite *s; + + s = self->sprite[self->facing]; + + if (self->spriteFrame >= s->numFrames) + { + self->spriteFrame = 0; + } + + return &s->frames[self->spriteFrame]->rect; } static void load(cJSON *root) diff --git a/src/entities/evilBlobs/evilBlob.c b/src/entities/evilBlobs/evilBlob.c index 8435cc9..08687ae 100644 --- a/src/entities/evilBlobs/evilBlob.c +++ b/src/entities/evilBlobs/evilBlob.c @@ -312,7 +312,13 @@ static void die(void) static void animate(void) { - if (self->dx != 0 || self->health <= 0) + if (self->alive != ALIVE_ALIVE) + { + self->facing = FACING_DIE; + + superAnimate(); + } + else if (self->dx != 0) { superAnimate(); } diff --git a/src/entities/evilBlobs/evilBlob.h b/src/entities/evilBlobs/evilBlob.h index c81460c..6ffb99e 100644 --- a/src/entities/evilBlobs/evilBlob.h +++ b/src/entities/evilBlobs/evilBlob.h @@ -24,14 +24,14 @@ extern void dropCarriedItem(void); extern int getDistance(int x1, int y1, int x2, int y2); extern double randF(void); extern void throwFleshChunks(float x, float y, int amount); -extern void addRandomWeapon(float x, float y); +extern void addRandomWeapon(int x, int y); extern float limit(float i, float a, float b); extern void playSound(int snd, int ch); extern void addBloodDecal(int x, int y); extern void updateObjective(char *targetName); extern int enemyCanSeePlayer(Entity *e); extern void fireTriggers(char *name); -extern void addRandomItems(float x, float y); +extern void addRandomItems(int x, int y); extern int rrnd(int low, int high); extern Unit *createUnit(void); diff --git a/src/entities/eyeDroids/eyeDroid.c b/src/entities/eyeDroids/eyeDroid.c index e9af9b0..4b0927e 100644 --- a/src/entities/eyeDroids/eyeDroid.c +++ b/src/entities/eyeDroids/eyeDroid.c @@ -85,7 +85,7 @@ static void touch(Entity *other) addRandomWeapon(u->x, u->y); } - addRandomItems((int) u->x, (int) u->y); + addRandomItems(u->x, u->y); updateObjective(u->name); updateObjective("ENEMY"); diff --git a/src/entities/eyeDroids/eyeDroid.h b/src/entities/eyeDroids/eyeDroid.h index d8f0c0b..03cf084 100644 --- a/src/entities/eyeDroids/eyeDroid.h +++ b/src/entities/eyeDroids/eyeDroid.h @@ -23,13 +23,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void dropCarriedItem(void); extern int getDistance(int x1, int y1, int x2, int y2); extern double randF(void); -extern void addRandomWeapon(float x, float y); +extern void addRandomWeapon(int x, int y); extern float limit(float i, float a, float b); extern void playSound(int snd, int ch); extern void updateObjective(char *targetName); extern int enemyCanSeePlayer(Entity *e); extern void fireTriggers(char *name); -extern void addRandomItems(float x, float y); +extern void addRandomItems(int x, int y); extern int rrnd(int low, int high); extern void addExplosion(float x, float y, int radius, Entity *owner); extern void throwDebris(float x, float y, int amount); diff --git a/src/entities/unit.c b/src/entities/unit.c index 58ef57c..ca04de0 100644 --- a/src/entities/unit.c +++ b/src/entities/unit.c @@ -284,12 +284,16 @@ static int canFire(Entity *target) static SDL_Rect *getCurrentSprite(void) { - if (self->alive == ALIVE_ALIVE) + Sprite *s; + + s = (self->alive == ALIVE_ALIVE) ? self->sprite[self->facing] : self->sprite[FACING_DIE]; + + if (self->spriteFrame >= s->numFrames) { - return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; + self->spriteFrame = 0; } - return &self->sprite[FACING_DIE]->frames[self->spriteFrame]->rect; + return &s->frames[self->spriteFrame]->rect; } static void load(cJSON *root)