1 /*
2 Copyright (C) 2009-2021 Parallel Realities
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19 
20 #include "../headers.h"
21 
22 #include "../audio/audio.h"
23 #include "../collisions.h"
24 #include "../entity.h"
25 #include "../graphics/animation.h"
26 #include "../hud.h"
27 #include "../system/error.h"
28 #include "../system/properties.h"
29 
30 extern Entity *self;
31 extern Game game;
32 
33 static void init(void);
34 static void entityWait(void);
35 static void activate(int);
36 static void touch(Entity *);
37 
addPressurePlate(char * name,int x,int y)38 Entity *addPressurePlate(char *name, int x, int y)
39 {
40 	Entity *e = getFreeEntity();
41 
42 	if (e == NULL)
43 	{
44 		showErrorAndExit("No free slots to add a Pressure Plate");
45 	}
46 
47 	loadProperties(name, e);
48 
49 	e->touch = &touch;
50 
51 	e->action = &init;
52 
53 	e->draw = &drawLoopingAnimationToMap;
54 
55 	e->activate = &activate;
56 
57 	e->x = x;
58 	e->y = y;
59 
60 	setEntityAnimation(e, "STAND");
61 
62 	return e;
63 }
64 
init()65 static void init()
66 {
67 	setEntityAnimation(self, self->thinkTime <= 0 ? "STAND" : "WALK");
68 
69 	self->action = &entityWait;
70 }
71 
entityWait()72 static void entityWait()
73 {
74 	self->thinkTime--;
75 
76 	if (self->thinkTime == 0)
77 	{
78 		setEntityAnimation(self, "STAND");
79 
80 		activateEntitiesWithRequiredName(self->objectiveName, FALSE);
81 
82 		self->active = FALSE;
83 	}
84 
85 	else if (self->thinkTime < 0)
86 	{
87 		self->thinkTime = -1;
88 	}
89 
90 	checkToMap(self);
91 }
92 
touch(Entity * other)93 static void touch(Entity *other)
94 {
95 	/* Place the entity as close as possible */
96 
97 	other->y = self->y + self->box.y;
98 	other->y -= other->box.h + other->box.y;
99 
100 	other->standingOn = self;
101 	other->dirY = 0;
102 	other->flags |= ON_GROUND;
103 
104 	if (other->standingOn == self)
105 	{
106 		if (strlen(self->requires) == 0 || strcmpignorecase(self->requires, other->objectiveName) == 0)
107 		{
108 			activate(1);
109 		}
110 
111 		else if (other->type == PLAYER)
112 		{
113 			setInfoBoxMessage(0, 255, 255, 255, _("%s is required to use this Pressure Plate"), _(self->requires));
114 		}
115 	}
116 }
117 
activate(int val)118 static void activate(int val)
119 {
120 	int remaining, total;
121 
122 	if (self->thinkTime == -1)
123 	{
124 		setEntityAnimation(self, "WALK");
125 
126 		self->active = TRUE;
127 
128 		remaining = self->mental == -1 ? 0 : countSiblings(self, &total);
129 
130 		if (remaining == 0)
131 		{
132 			activateEntitiesWithRequiredName(self->objectiveName, TRUE);
133 
134 			if (total > 0 && self->mental != -1)
135 			{
136 				setInfoBoxMessage(30, 255, 255, 255, _("Complete"), remaining);
137 			}
138 		}
139 
140 		else
141 		{
142 			setInfoBoxMessage(30, 255, 255, 255, _("%d more to go..."), remaining);
143 		}
144 
145 		self->maxHealth = self->health;
146 
147 		playSoundToMap("sound/common/switch", -1, self->x, self->y, 0);
148 	}
149 
150 	self->thinkTime = 5;
151 }
152