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"
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void initDoor(Entity *e)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
initEntity(e);
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s = (Structure*)e;
|
|
|
|
|
2018-01-28 10:34:14 +01:00
|
|
|
s->type = ET_DOOR;
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->isSolid = 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-28 09:30:53 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSpriteIndex("Door");
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->closedX == -1 && s->closedY == -1)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
s->closedX = (int) s->x;
|
|
|
|
s->closedY = (int) s->y;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->tick = tick;
|
|
|
|
s->touch = touch;
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initBronzeDoor(Entity *e)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
initDoor(e);
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s = (Structure*)e;
|
|
|
|
|
|
|
|
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-28 09:30:53 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSpriteIndex("BronzeDoor");
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initSilverDoor(Entity *e)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
initDoor(e);
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s = (Structure*)e;
|
|
|
|
|
|
|
|
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-28 09:30:53 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSpriteIndex("SilverDoor");
|
2018-01-27 11:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initGoldDoor(Entity *e)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Structure *s;
|
|
|
|
|
2018-01-27 11:21:45 +01:00
|
|
|
initDoor(e);
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s = (Structure*)e;
|
|
|
|
|
|
|
|
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-28 09:30:53 +01:00
|
|
|
s->sprite[0] = s->sprite[1] = e->sprite[2] = getSpriteIndex("GoldDoor");
|
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-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-01-28 09:30:53 +01:00
|
|
|
if (abs(s->x - s->tx) < s->speed && abs(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-01-28 09:30:53 +01:00
|
|
|
if (abs(s->x - s->closedX) < s->speed && abs(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
|
|
|
|
|
|
|
playSound(SND_DOOR_FINISH, CH_MECHANICAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
{
|
|
|
|
setGameplayMessage(MSG_GAMEPLAY, "Door is locked");
|
|
|
|
|
|
|
|
playSound(SND_DENIED, CH_MECHANICAL);
|
|
|
|
}
|
|
|
|
|
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-01-28 09:30:53 +01:00
|
|
|
if (s->state != DOOR_OPEN)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
|
|
|
playSound(SND_DOOR_START, CH_MECHANICAL);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (hasItem(s->requiredItem))
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->thinkTime <= 0)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, "%s required", s->requiredItem);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
|
|
|
playSound(SND_DENIED, CH_MECHANICAL);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->thinkTime = 2;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
removeItem(s->requiredItem);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, "%s removed", s->requiredItem);
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
STRNCPY(s->requiredItem, "", MAX_NAME_LENGTH);
|
|
|
|
s->isLocked = 0;
|
2018-01-27 11:21:45 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (s->state != DOOR_OPEN)
|
2018-01-27 11:21:45 +01:00
|
|
|
{
|
|
|
|
playSound(SND_DOOR_START, CH_MECHANICAL);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
s->state = DOOR_OPEN;
|
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
|
|
|
}
|