2018-01-24 23:15:24 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2018 Parallel Realities
|
|
|
|
|
|
|
|
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 "unit.h"
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void tick(void);
|
|
|
|
static void init(void);
|
2018-01-24 23:15:24 +01:00
|
|
|
static void attack(void);
|
2018-02-05 09:38:21 +01:00
|
|
|
static void applyDamage(int damage);
|
2018-01-28 09:30:53 +01:00
|
|
|
static int canFire(Entity *target);
|
2018-01-31 23:28:00 +01:00
|
|
|
static SDL_Rect *getCurrentSprite(void);
|
2018-01-29 23:12:18 +01:00
|
|
|
static void preFire(void);
|
2018-01-30 23:47:40 +01:00
|
|
|
static void load(cJSON *root);
|
|
|
|
static void save(cJSON *root);
|
2018-01-24 23:15:24 +01:00
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
Unit *createUnit(void)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
u = malloc(sizeof(Unit));
|
|
|
|
memset(u, 0, sizeof(Unit));
|
2018-01-24 23:15:24 +01:00
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
initEntity((Entity*)u);
|
2018-01-28 09:30:53 +01:00
|
|
|
|
2018-01-31 22:50:49 +01:00
|
|
|
u->type = ET_ENEMY;
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->oxygen = MAX_OXYGEN;
|
2018-01-24 23:15:24 +01:00
|
|
|
|
2018-02-03 00:01:51 +01:00
|
|
|
u->spriteTime = 0;
|
2018-01-28 09:30:53 +01:00
|
|
|
u->spriteFrame = 0;
|
2018-01-24 23:15:24 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->startX = u->startY = -1;
|
2018-01-24 23:15:24 +01:00
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
u->init = init;
|
|
|
|
u->tick = tick;
|
2018-01-29 23:12:18 +01:00
|
|
|
u->preFire = preFire;
|
2018-01-28 09:30:53 +01:00
|
|
|
u->attack = attack;
|
|
|
|
u->canFire = canFire;
|
2018-01-31 23:28:00 +01:00
|
|
|
u->getCurrentSprite = getCurrentSprite;
|
2018-02-05 09:38:21 +01:00
|
|
|
u->applyDamage = applyDamage;
|
2018-01-30 23:47:40 +01:00
|
|
|
u->load = load;
|
|
|
|
u->save = save;
|
2018-01-29 23:12:18 +01:00
|
|
|
|
|
|
|
return u;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void init(void)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
if (u->startX == -1 && u->startY == -1)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->startX = (int) u->x;
|
|
|
|
u->startY = (int) u->y;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
2018-05-01 09:38:46 +02:00
|
|
|
|
|
|
|
u->canCarryItem = rand() % 100 < 85;
|
|
|
|
|
2018-05-01 19:15:56 +02:00
|
|
|
if (world.missionType == MT_OUTPOST)
|
2018-05-01 09:38:46 +02:00
|
|
|
{
|
|
|
|
u->canCarryItem = 1;
|
|
|
|
u->health = u->healthMax = rrnd(1, 4);
|
2018-05-01 19:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (game.plus == PLUS_PLUS_PLUS)
|
|
|
|
{
|
|
|
|
u->canCarryItem = 1;
|
|
|
|
u->health = u->healthMax = rrnd(4, 8);
|
2018-05-01 09:38:46 +02:00
|
|
|
}
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void tick(void)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
if (u->alive == ALIVE_ALIVE)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->reload = limit(u->reload - 1, 0, FPS);
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
switch (u->environment)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
|
|
|
case ENV_AIR:
|
2018-01-28 09:30:53 +01:00
|
|
|
u->oxygen = limit(u->oxygen + 4, 0, MAX_OXYGEN);
|
2018-01-24 23:15:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ENV_WATER:
|
2018-01-28 09:30:53 +01:00
|
|
|
u->oxygen = limit(u->oxygen - 1, 0, MAX_OXYGEN);
|
|
|
|
if (u->oxygen == 0 && world.frameCounter % 30 == 0)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->health--;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENV_SLIME:
|
|
|
|
case ENV_LAVA:
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->alive == ALIVE_ALIVE)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->health = 0;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->flags & EF_WATER_BREATHING)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->oxygen = MAX_OXYGEN;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->spawnedIn)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-03-07 22:37:31 +01:00
|
|
|
if (getDistance(u->x, u->y, world.bob->x, world.bob->y) < SCREEN_WIDTH)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->spawnedInTimer = FPS * 5;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 23:27:53 +01:00
|
|
|
if (--u->spawnedInTimer <= 0)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->alive = ALIVE_DEAD;
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 22:37:31 +01:00
|
|
|
|
|
|
|
if (u->carriedItem != NULL)
|
|
|
|
{
|
2018-03-08 08:53:57 +01:00
|
|
|
u->carriedItem->x = u->x;
|
|
|
|
u->carriedItem->y = u->y;
|
2018-03-07 22:37:31 +01:00
|
|
|
}
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 09:38:21 +01:00
|
|
|
static void reappear(void)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-02-19 20:14:32 +01:00
|
|
|
int r;
|
2018-02-06 23:27:53 +01:00
|
|
|
|
2018-02-19 20:14:32 +01:00
|
|
|
r = rand() % MAX_CHECKPOINTS;
|
|
|
|
self->x = world.bob->checkpoints[r].x;
|
|
|
|
self->y = world.bob->checkpoints[r].y;
|
2018-02-06 23:27:53 +01:00
|
|
|
|
2018-02-19 20:14:32 +01:00
|
|
|
if (self->x != 0 && self->y != 0)
|
2018-02-06 23:27:53 +01:00
|
|
|
{
|
2018-02-19 20:14:32 +01:00
|
|
|
self->y -= (self->h + 10);
|
|
|
|
|
|
|
|
self->action = self->walk;
|
|
|
|
|
2018-03-18 13:55:51 +01:00
|
|
|
self->flags &= ~(EF_GONE | EF_ALWAYS_PROCESS);
|
2018-02-06 23:27:53 +01:00
|
|
|
|
2018-02-19 20:14:32 +01:00
|
|
|
addTeleportStars(self);
|
2018-02-06 23:27:53 +01:00
|
|
|
|
2018-02-26 19:56:13 +01:00
|
|
|
playBattleSound(SND_APPEAR, self->uniqueId % MAX_SND_CHANNELS, self->x, self->y);
|
2018-02-19 20:14:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->thinkTime = FPS;
|
|
|
|
}
|
2018-01-24 23:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 09:38:21 +01:00
|
|
|
static void applyDamage(int damage)
|
|
|
|
{
|
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
2018-03-18 09:36:51 +01:00
|
|
|
if (u->alive != ALIVE_DEAD)
|
2018-02-05 09:38:21 +01:00
|
|
|
{
|
2018-03-18 09:36:51 +01:00
|
|
|
if (u->health < 0)
|
|
|
|
{
|
|
|
|
u->health = 0;
|
|
|
|
u->alive = ALIVE_ALIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
u->health -= damage;
|
2018-02-05 09:38:21 +01:00
|
|
|
|
2018-03-18 09:36:51 +01:00
|
|
|
if (u->health > 0)
|
|
|
|
{
|
|
|
|
u->thinkTime = 0;
|
2018-02-05 09:38:21 +01:00
|
|
|
|
2018-03-18 09:36:51 +01:00
|
|
|
u->facing = u->x < world.bob->x ? FACING_RIGHT : FACING_LEFT;
|
2018-02-05 09:38:21 +01:00
|
|
|
|
2018-03-18 09:36:51 +01:00
|
|
|
if (u->isMissionTarget && rand() % 100 < 10)
|
|
|
|
{
|
|
|
|
u->action = reappear;
|
2018-03-18 13:55:51 +01:00
|
|
|
u->flags |= (EF_GONE | EF_ALWAYS_PROCESS);
|
2018-03-18 09:36:51 +01:00
|
|
|
u->thinkTime = rrnd(FPS, FPS * 2);
|
|
|
|
addTeleportStars(self);
|
|
|
|
playBattleSound(SND_APPEAR, self->uniqueId % MAX_SND_CHANNELS, u->x, u->y);
|
|
|
|
}
|
2018-02-05 09:38:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 23:15:24 +01:00
|
|
|
static void attack(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
if (u->canFire((Entity*)world.bob))
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
switch (u->weaponType)
|
2018-01-24 23:15:24 +01:00
|
|
|
{
|
|
|
|
case WPN_AIMED_PISTOL:
|
|
|
|
fireAimedShot(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_MACHINE_GUN:
|
|
|
|
fireMachineGun(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_GRENADES:
|
|
|
|
fireGrenade(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_PLASMA:
|
|
|
|
firePlasma(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_SPREAD:
|
|
|
|
fireSpread(self, 3);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_LASER:
|
|
|
|
fireLaser(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_SHOTGUN:
|
|
|
|
fireShotgun(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WPN_MISSILE:
|
|
|
|
fireMissile(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-28 09:30:53 +01:00
|
|
|
printf("Can't fire weapon: %d\n", u->weaponType);
|
2018-01-24 23:15:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
static void preFire(void)
|
|
|
|
{
|
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
if (!(u->flags & EF_WEIGHTLESS))
|
|
|
|
{
|
|
|
|
if (world.bob->y < u->y && u->isOnGround && rand() % 4 == 0)
|
|
|
|
{
|
|
|
|
u->dy = JUMP_POWER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u->facing = (world.bob->x < u->x) ? FACING_LEFT : FACING_RIGHT;
|
|
|
|
|
|
|
|
if (u->reload > 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u->attack();
|
2018-02-05 09:38:21 +01:00
|
|
|
|
2018-03-06 09:24:45 +01:00
|
|
|
if (--u->shotsToFire == 0)
|
2018-01-29 23:12:18 +01:00
|
|
|
{
|
2018-02-05 09:38:21 +01:00
|
|
|
u->action = u->walk;
|
2018-01-29 23:12:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
static int canFire(Entity *target)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-30 23:47:40 +01:00
|
|
|
|
2018-01-31 23:28:00 +01:00
|
|
|
static SDL_Rect *getCurrentSprite(void)
|
|
|
|
{
|
2018-02-24 16:59:26 +01:00
|
|
|
Sprite *s;
|
|
|
|
|
|
|
|
s = (self->alive == ALIVE_ALIVE) ? self->sprite[self->facing] : self->sprite[FACING_DIE];
|
|
|
|
|
|
|
|
if (self->spriteFrame >= s->numFrames)
|
2018-01-31 23:28:00 +01:00
|
|
|
{
|
2018-02-25 13:36:34 +01:00
|
|
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "WARNING: %s (%d) bad sprite frames - %d > %d\n", self->name, self->type, self->spriteFrame, s->numFrames);
|
2018-02-24 16:59:26 +01:00
|
|
|
self->spriteFrame = 0;
|
2018-01-31 23:28:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 16:59:26 +01:00
|
|
|
return &s->frames[self->spriteFrame]->rect;
|
2018-01-31 23:28:00 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 23:47:40 +01:00
|
|
|
static void load(cJSON *root)
|
|
|
|
{
|
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
u->canCarryItem = cJSON_GetObjectItem(root, "canCarryItem")->valueint;
|
|
|
|
u->startX = cJSON_GetObjectItem(root, "startX")->valueint;
|
|
|
|
u->startY = cJSON_GetObjectItem(root, "startY")->valueint;
|
|
|
|
u->isMissionTarget = cJSON_GetObjectItem(root, "isMissionTarget")->valueint;
|
|
|
|
u->health = cJSON_GetObjectItem(root, "health")->valueint;
|
|
|
|
u->healthMax = cJSON_GetObjectItem(root, "healthMax")->valueint;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save(cJSON *root)
|
|
|
|
{
|
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
2018-01-31 09:08:50 +01:00
|
|
|
cJSON_AddStringToObject(root, "type", u->unitType);
|
2018-01-30 23:47:40 +01:00
|
|
|
cJSON_AddNumberToObject(root, "canCarryItem", u->canCarryItem);
|
|
|
|
cJSON_AddNumberToObject(root, "startX", u->startX);
|
|
|
|
cJSON_AddNumberToObject(root, "startY", u->startY);
|
|
|
|
cJSON_AddNumberToObject(root, "isMissionTarget", u->isMissionTarget);
|
|
|
|
cJSON_AddNumberToObject(root, "health", u->health);
|
|
|
|
cJSON_AddNumberToObject(root, "healthMax", u->healthMax);
|
|
|
|
}
|