2018-01-27 11:21:45 +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 "door.h"
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void init(void);
|
2018-01-27 11:21:45 +01:00
|
|
|
static void tick(void);
|
|
|
|
static void touch(Entity *other);
|
|
|
|
static void openWithKey(void);
|
|
|
|
static int isClosed(void);
|
|
|
|
static int isOpening(void);
|
|
|
|
static int isClosing(void);
|
2018-02-03 16:53:21 +01:00
|
|
|
static void activate(int active);
|
2018-01-30 23:47:40 +01:00
|
|
|
static void load(cJSON *root);
|
|
|
|
static void save(cJSON *root);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
Entity *initDoor(void)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
s = createStructure();
|
2018-01-28 09:30:53 +01:00
|
|
|
|
2018-01-28 10:34:14 +01:00
|
|
|
s->type = ET_DOOR;
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isSolid = 1;
|
2018-02-16 09:08:33 +01:00
|
|
|
|
|
|
|
s->isStatic = 1;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->flags |= EF_WEIGHTLESS | EF_NO_ENVIRONMENT | EF_NO_CLIP | EF_EXPLODES | EF_NO_TELEPORT;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->closedX = s->closedY = -1;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->state = DOOR_CLOSED;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->speed = 8;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isLocked = 1;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-30 00:00:14 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Door");
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
s->init = init;
|
2018-01-28 09:30:53 +01:00
|
|
|
s->tick = tick;
|
|
|
|
s->touch = touch;
|
2018-02-03 16:53:21 +01:00
|
|
|
s->activate = activate;
|
2018-01-30 23:47:40 +01:00
|
|
|
s->load = load;
|
|
|
|
s->save = save;
|
2018-01-30 09:29:09 +01:00
|
|
|
|
|
|
|
return (Entity*)s;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void init(void)
|
|
|
|
{
|
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
|
|
|
if (s->closedX == -1 && s->closedY == -1)
|
|
|
|
{
|
|
|
|
s->closedX = s->x;
|
|
|
|
s->closedY = s->y;
|
|
|
|
}
|
2018-05-01 19:15:56 +02:00
|
|
|
|
|
|
|
if (game.plus != PLUS_NONE)
|
|
|
|
{
|
|
|
|
s->alive = ALIVE_DEAD;
|
|
|
|
}
|
2018-02-01 08:50:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
Entity *initBronzeDoor(void)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
s = (Structure*)initDoor();
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
STRNCPY(s->requiredItem, "Bronze Key", MAX_NAME_LENGTH);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->speed = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-30 00:00:14 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("BronzeDoor");
|
2018-01-30 09:29:09 +01:00
|
|
|
|
|
|
|
return (Entity*)s;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
Entity *initSilverDoor(void)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
s = (Structure*)initDoor();
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
STRNCPY(s->requiredItem, "Silver Key", MAX_NAME_LENGTH);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->speed = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-30 00:00:14 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("SilverDoor");
|
2018-01-30 09:29:09 +01:00
|
|
|
|
|
|
|
return (Entity*)s;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
Entity *initGoldDoor(void)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
s = (Structure*)initDoor();
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
STRNCPY(s->requiredItem, "Gold Key", MAX_NAME_LENGTH);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->speed = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("GoldDoor");
|
|
|
|
|
|
|
|
return (Entity*)s;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tick(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
|
|
|
s->dx = s->dy = 0;
|
2018-02-03 12:32:03 +01:00
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
if (isOpening())
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
getSlope(s->tx, s->ty, s->x, s->y, &s->dx, &s->dy);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->dx *= s->speed;
|
|
|
|
s->dy *= s->speed;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-03-29 09:21:36 +02:00
|
|
|
if (fabs(s->x - s->tx) < s->speed && fabs(s->y - s->ty) < s->speed)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
s->x = s->tx;
|
|
|
|
s->y = s->ty;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isStatic = 0;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
else if (isClosing())
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
getSlope(s->closedX, s->closedY, s->x, s->y, &s->dx, &s->dy);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->dx *= s->speed;
|
|
|
|
s->dy *= s->speed;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-03-29 09:21:36 +02:00
|
|
|
if (fabs(s->x - s->closedX) < s->speed && fabs(self->y - s->closedY) < s->speed)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
s->x = s->closedX;
|
|
|
|
s->y = s->closedY;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isStatic = 0;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->dx == 0 && s->dy == 0 && !s->isStatic)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isStatic = 1;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-02-26 19:56:13 +01:00
|
|
|
playBattleSound(SND_DOOR_FINISH, s->uniqueId % MAX_SND_CHANNELS, s->x, s->y);
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void touch(Entity *other)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
|
|
|
if ((other->type != ET_BOB && s->isLocked) || (other->type != ET_BOB && other->type != ET_ENEMY))
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-02-03 14:07:56 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->isLocked && !dev.cheatKeys)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
|
|
|
if (isClosed())
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
if (strlen(s->requiredItem) != 0)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
|
|
|
openWithKey();
|
|
|
|
}
|
2018-01-28 09:30:53 +01:00
|
|
|
else if (s->thinkTime == 0)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_LOCKED]);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_DENIED, s->uniqueId % MAX_SND_CHANNELS);
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->thinkTime = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (isClosing())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 14:07:56 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->state != DOOR_OPEN)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-02-26 19:56:13 +01:00
|
|
|
playBattleSound(SND_DOOR_START, s->uniqueId % MAX_SND_CHANNELS, s->x, s->y);
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->state = DOOR_OPEN;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void openWithKey(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
2018-02-04 09:11:42 +01:00
|
|
|
if (hasItem(s->requiredItem) || dev.cheatKeys)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-02-04 09:11:42 +01:00
|
|
|
removeItem(s->requiredItem);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_REMOVED], s->requiredItem);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-02-04 09:11:42 +01:00
|
|
|
STRNCPY(s->requiredItem, "", MAX_NAME_LENGTH);
|
|
|
|
s->isLocked = 0;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-02-04 09:11:42 +01:00
|
|
|
if (s->state != DOOR_OPEN)
|
|
|
|
{
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_DOOR_START, s->uniqueId % MAX_SND_CHANNELS);
|
2018-02-04 09:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s->state = DOOR_OPEN;
|
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-04 09:11:42 +01:00
|
|
|
|
|
|
|
if (s->thinkTime <= 0)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_REQUIRED], s->requiredItem);
|
2018-02-04 09:11:42 +01:00
|
|
|
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_DENIED, s->uniqueId % MAX_SND_CHANNELS);
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:11:42 +01:00
|
|
|
s->thinkTime = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int isOpening(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s = (Structure*)self;
|
|
|
|
|
|
|
|
return (s->state == DOOR_OPEN && ((int) s->x != (int) s->tx || (int) s->y != (int) s->ty));
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int isClosing(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s = (Structure*)self;
|
|
|
|
|
|
|
|
return (s->state == DOOR_CLOSED && ((int) s->x != s->closedX || (int) s->y != s->closedY));
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int isClosed(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s = (Structure*)self;
|
|
|
|
|
|
|
|
return (s->state == DOOR_CLOSED && ((int) s->x == s->closedX && (int) s->y == s->closedY));
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
2018-01-30 23:47:40 +01:00
|
|
|
|
2018-02-03 16:53:21 +01:00
|
|
|
static void activate(int active)
|
|
|
|
{
|
|
|
|
Structure *s = (Structure*)self;
|
|
|
|
|
|
|
|
s->state = (s->state == DOOR_CLOSED) ? DOOR_OPEN : DOOR_CLOSED;
|
|
|
|
|
2018-02-26 19:56:13 +01:00
|
|
|
playBattleSound(SND_DOOR_START, s->uniqueId % MAX_SND_CHANNELS, s->x, s->y);
|
2018-02-03 16:53:21 +01:00
|
|
|
|
|
|
|
if (active)
|
|
|
|
{
|
|
|
|
observeActivation(self);
|
|
|
|
|
|
|
|
if (!isOnScreen(self))
|
|
|
|
{
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_OPENED]);
|
2018-02-03 16:53:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 23:47:40 +01:00
|
|
|
static void load(cJSON *root)
|
|
|
|
{
|
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
2018-02-03 14:07:56 +01:00
|
|
|
s->isLocked = cJSON_GetObjectItem(root, "isLocked")->valueint;
|
|
|
|
s->active = s->isLocked;
|
2018-01-30 23:47:40 +01:00
|
|
|
if (cJSON_GetObjectItem(root, "requiredKey"))
|
|
|
|
{
|
|
|
|
STRNCPY(s->requiredItem, cJSON_GetObjectItem(root, "requiredKey")->valuestring, MAX_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
|
|
|
|
s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
|
|
|
|
s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
|
2018-02-03 12:32:03 +01:00
|
|
|
s->state = lookup(cJSON_GetObjectItem(root, "state")->valuestring);
|
2018-03-13 09:25:41 +01:00
|
|
|
|
|
|
|
if (cJSON_GetObjectItem(root, "closedX"))
|
|
|
|
{
|
|
|
|
s->closedX = cJSON_GetObjectItem(root, "closedX")->valueint;
|
|
|
|
s->closedY = cJSON_GetObjectItem(root, "closedY")->valueint;
|
|
|
|
}
|
2018-01-30 23:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void save(cJSON *root)
|
|
|
|
{
|
|
|
|
Structure *s;
|
|
|
|
|
|
|
|
s = (Structure*)self;
|
|
|
|
|
2018-02-10 19:35:56 +01:00
|
|
|
cJSON_AddStringToObject(root, "type", s->sprite[0]->name);
|
2018-01-30 23:47:40 +01:00
|
|
|
cJSON_AddNumberToObject(root, "isLocked", s->isLocked);
|
|
|
|
cJSON_AddNumberToObject(root, "tx", s->tx);
|
|
|
|
cJSON_AddNumberToObject(root, "ty", s->ty);
|
2018-03-13 09:25:41 +01:00
|
|
|
cJSON_AddNumberToObject(root, "closedX", s->closedX);
|
|
|
|
cJSON_AddNumberToObject(root, "closedY", s->closedY);
|
2018-01-30 23:47:40 +01:00
|
|
|
cJSON_AddNumberToObject(root, "speed", s->speed);
|
2018-02-03 12:32:03 +01:00
|
|
|
cJSON_AddStringToObject(root, "state", getLookupName("DOOR_", s->state));
|
2018-01-30 23:47:40 +01:00
|
|
|
cJSON_AddStringToObject(root, "requiredKey", s->requiredItem);
|
|
|
|
|
|
|
|
if (strcmp(s->sprite[0]->name, "Door") != 0)
|
|
|
|
{
|
|
|
|
cJSON_AddStringToObject(root, "type", s->sprite[0]->name);
|
|
|
|
}
|
|
|
|
}
|