Debian Codegraph @

Descent3/trigger.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/trigger.cpp $
21 * $Revision: 26 $
22 * $Date: 5/20/99 9:03p $
23 * $Author: Matt $
24 *
25 * Trigger management
26 *
27 * $Log: /DescentIII/main/trigger.cpp $
28 *
29 * 26 5/20/99 9:03p Matt
30 * Changed trigger system so that collisions with rendered portals don't
31 * set off triggers on the portals.
32 *
33 * 25 5/13/99 3:42p Ardussi
34 * changes for compiling on the Mac
35 *
36 * 24 3/23/99 5:52p Matt
37 * Added clutter activators for triggers
38 *
39 * 23 2/21/99 4:35p Chris
40 * Improving the level goal system... Not done.
41 *
42 * 22 2/15/99 7:47p Matt
43 * Added a default case to CheckTrigger
44 *
45 * 21 2/11/99 8:30p Matt
46 * Took out Int3() when couldn't find an object's parent
47 *
48 * 20 2/11/99 1:10p Matt
49 * Added functions to get & set a triggers enabled/disabled state
50 *
51 * 19 2/02/99 8:43a Chris
52 * I made buildings with AI work correctly (ie really big robots should be
53 * buildings)
54 * anim to and from states are now shorts instead of bytes
55 *
56 * 18 1/21/99 11:34a Matt
57 * Got rid of portal triggers. Since we don't have multi-face portals, a
58 * face trigger works fine for a portal. Also fixed a few editor/trigger
59 * bugs.
60 *
61 * 17 1/14/99 11:06a Matt
62 * Added names to triggers
63 *
64 * 16 1/08/99 2:56p Samir
65 * Ripped out OSIRIS1.
66 *
67 * 15 12/17/98 12:08p Jeff
68 * first checkin of new implementation of OSIRIS (old OSIRIS no longer
69 * works)
70 *
71 * 14 10/21/98 9:44p Matt
72 * Fixed bug that caused 1-shot portal triggers to not be found after the
73 * first time.
74 *
75 * 13 9/01/98 12:04p Matt
76 * Ripped out multi-face portal code
77 *
78 * 12 8/11/98 1:04p Matt
79 * Fixed bug that caused one-shot triggers to be marked as used even when
80 * hit by an object that wasn't a valid activator.
81 *
82 * 11 5/26/98 7:06p Samir
83 * took script.is_custom into account for triggers.
84 *
85 * 10 4/24/98 1:53a Samir
86 * added trigger init and free.
87 *
88 * 9 1/23/98 5:54p Samir
89 * New style script trigger support.
90 *
91 * 8 1/20/98 4:12p Samir
92 * New script housekeeping system.
93 *
94 * 7 12/23/97 1:33p Samir
95 * Added pserror.h
96 *
97 * 6 9/24/97 6:18p Samir
98 * Use script names instead of script id values to identify scripts.
99 *
100 * 5 9/22/97 12:24p Matt
101 * Fixed bugs with face flags for portal triggers
102 *
103 * 4 9/12/97 3:58p Matt
104 * Added support for activators and one-shot triggers
105 *
106 * 3 9/11/97 1:57p Matt
107 * Added trigger struct & a bunch of functions. System is not done yet,
108 * though.
109 *
110 * 2 9/08/97 10:01a Matt
111 * Ripped out old trigger code
112 *
113 * $NoKeywords: $
114 */
115
116 #include "trigger.h"
117 #include "room.h"
118 #include "object.h"
119 //@$-#include "d3x.h"
120 #include "pserror.h"
121 #include "osiris_dll.h"
122 #include "levelgoal.h"
123
124 #include <stdlib.h>
125 #include <string.h>
126
127 // The maximum number of triggers that can be in the mine
128 #define MAX_TRIGGERS 100
129
130 // The list of triggers for the mine
131 trigger Triggers[MAX_TRIGGERS];
132
133 // The number of triggers currently in the mine
134 int Num_triggers = 0;
135
136 void FreeTriggers();
137
138 // initializes trigger system
139 void InitTriggers() {
140 int i;
141
142 for (i = 0; i < MAX_TRIGGERS; i++) {
143 Triggers[i].roomnum = -1;
144 Triggers[i].facenum = -1;
145 Triggers[i].flags = 0;
146
147 Triggers[i].osiris_script.script_id = -1;
148 Triggers[i].osiris_script.script_instance = NULL;
149 }
150
151 Num_triggers = 0;
152
153 atexit(FreeTriggers);
154 }
155
156 // Free up all the triggers
157 void FreeTriggers() {
158 int i;
159
160 for (i = 0; i < Num_triggers; i++)
161 FreeTriggerScript(&Triggers[i]);
162
163 Num_triggers = 0;
164 }
165
166 // Returns a pointer to the trigger attached to the given room:face
167 // Generates an error if the trigger cannot be found
168 // Paramaters: roomnum,facenum - the room and face with the trigger
169 trigger *FindTrigger(int roomnum, int facenum) {
170 face *fp = &Rooms[roomnum].faces[facenum];
171 trigger *tp;
172 int i;
173
174 // Go through all the triggers & look for the requested one
175 for (i = 0, tp = Triggers; i < Num_triggers; i++, tp++)
176 if (tp->roomnum == roomnum)
177 if (tp->facenum == facenum)
178 return tp;
179
180 Int3(); // Didn't find the requested trigger -- very bad!
181
182 return NULL;
183 }
184
185 // Called to see if a trigger was tripped
186 void CheckTrigger(int roomnum, int facenum, object *objp, int event_type) {
187 room *rp = &Rooms[roomnum];
188 face *fp = &rp->faces[facenum];
189
190 ASSERT((event_type == TT_PASS_THROUGH) || (event_type == TT_COLLIDE));
191
192 if (fp->flags & FF_HAS_TRIGGER) {
193 trigger *tp;
194 int type;
195
196 tp = FindTrigger(roomnum, facenum);
197 ASSERT(tp != NULL);
198
199 if (tp == NULL) {
200 Int3(); // very bad! Get Matt.
201 return;
202 }
203
204 // Check for dead or disabled trigger
205 if (tp->flags & (TF_DISABLED | TF_DEAD))
206 return;
207
208 // If portal trigger, make sure this is a pass-through event
209 if ((fp->portal_num != -1) && (event_type != TT_PASS_THROUGH))
210 return;
211
212 // Get the activator type for the object that hit the trigger
213 switch (objp->type) {
214 case OBJ_PLAYER:
215 type = AF_PLAYER;
216 break;
217 case OBJ_BUILDING:
218 case OBJ_ROBOT:
219 type = AF_ROBOT;
220 break;
221 case OBJ_CLUTTER:
222 type = AF_CLUTTER;
223 break;
224 case OBJ_WEAPON: {
225 object *parent = ObjGetUltimateParent(objp);
226 int parent_type = parent->type;
227 if (parent_type == OBJ_PLAYER)
228 type = AF_PLAYER_WEAPON;
229 else if (parent_type == OBJ_ROBOT || parent_type == OBJ_BUILDING)
230 type = AF_ROBOT_WEAPON;
231 else
232 return; // unknown parent for weapon
233 break;
234 }
235 default:
236 return;
237 }
238
239 // Check if this object is a valid activator for this trigger
240 if (tp->activator & type) {
241 mprintf(0, "Hit trigger %d\n", tp - Triggers);
242
243 // Execute this trigger's script
244 tOSIRISEventInfo ei;
245 ei.evt_collide.it_handle = objp->handle;
246 Osiris_CallTriggerEvent((tp - Triggers), EVT_COLLIDE, &ei);
247
248 // If one-shot, kill this trigger, and mark face as destroyed
249 if (tp->flags & TF_ONESHOT) {
250 tp->flags |= TF_DEAD;
251 if (fp->portal_num == -1) // don't destroy a portal face
252 fp->flags |= FF_DESTROYED;
253 }
254
255 if (tp->flags & TF_INFORM_ACTIVATE_TO_LG) {
256 Level_goals.Inform(LIT_TRIGGER, LGF_COMP_ACTIVATE, tp - Triggers);
257 }
258 }
259 }
260 }
261
262 // Enable or disable a trigger
263 void TriggerSetState(int trigger_num, bool enabled) {
264 trigger *tp = &Triggers[trigger_num];
265
266 if (enabled)
267 tp->flags &= ~TF_DISABLED;
268 else
269 tp->flags |= TF_DISABLED;
270 }
271
272 // Determines if a trigger is enabled or disabled
273 // Returns TRUE if enabled, else false
274 bool TriggerGetState(int trigger_num) {
275 trigger *tp = &Triggers[trigger_num];
276
277 return ((tp->flags & TF_DISABLED) == 0);
278 }
279
280 #ifdef EDITOR
281
282 #include "editor\d3edit.h"
283
284 //
285 // EDITOR functions follow
286 //
287
288 // Create a new trigger
289 // Parameters: roomnum,facenum - where the trigger is
290 // flags - flags for this trigger
291 // activator - activator mask
292 // script - handle for the script for this trigger
293 // Returns: trigger number of new trigger, or -1 if error
294 int AddTrigger(char *name, int roomnum, int facenum, int activator, const char *script) {
295 room *rp = &Rooms[roomnum];
296 face *fp = &rp->faces[facenum];
297 trigger *tp;
298
299 if (Num_triggers >= MAX_TRIGGERS)
300 return -1;
301
302 if (strlen(name) > TRIG_NAME_LEN) {
303 Int3();
304 return -1;
305 }
306
307 tp = &Triggers[Num_triggers];
308
309 strcpy(tp->name, name);
310 tp->roomnum = roomnum;
311 tp->facenum = facenum;
312 tp->activator = activator;
313 tp->flags = 0;
314
315 // Flag the face
316 fp->flags |= FF_HAS_TRIGGER;
317
318 // Update count
319 Num_triggers++;
320
321 // Update flag
322 World_changed = 1;
323
324 // Everything ok
325 return Num_triggers - 1;
326 }
327
328 // Remove a trigger
329 // Paramters: trig_num - the trigger to delete
330 void DeleteTrigger(int trig_num) {
331 trigger *tp = &Triggers[trig_num];
332 room *rp = &Rooms[tp->roomnum];
333 face *fp = &rp->faces[tp->facenum];
334
335 // Clear the face flag
336 fp->flags &= ~FF_HAS_TRIGGER;
337
338 // Free the script for this trigger
339 FreeTriggerScript(tp);
340
341 // Move other triggers down in list
342 for (int i = trig_num; i < Num_triggers - 1; i++)
343 Triggers[i] = Triggers[i + 1];
344
345 // Update count
346 Num_triggers--;
347 }
348
349 // Remove a trigger
350 // Paramters: roomnum,facenum - where the trigger is
351 void DeleteTrigger(int roomnum, int facenum) {
352 trigger *tp;
353
354 tp = FindTrigger(roomnum, facenum);
355
356 ASSERT(tp != NULL);
357
358 DeleteTrigger(tp - Triggers);
359 }
360
361 #endif