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"cxx . . $ `<file>/Descent3/trigger.h`/
117 #include "room.h"cxx . . $ `<file>/Descent3/room.h`/
118 #include "object.h"cxx . . $ `<file>/Descent3/object.h`/
119 //@$-#include "d3x.h"
120 #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/
121 #include "osiris_dll.h"cxx . . $ `<file>/Descent3/osiris_dll.h`/
122 #include "levelgoal.h"cxx . . $ `<file>/Descent3/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_TRIGGERScxx . . $ `Descent3/trigger.cpp:128:9`! 100
129
130 // The list of triggers for the mine
131 triggercxx . . $ trigger# Triggerscxx . . $ Triggers.[MAX_TRIGGERScxx . . $ `Descent3/trigger.cpp:128:9`!];
132
133 // The number of triggers currently in the mine
134 int Num_triggerscxx . . $ Num_triggers. = 0;
135
136 void FreeTriggerscxx . . $ FreeTriggers(49f6e7a06ebc5aa8).();
137
138 // initializes trigger system
139 void InitTriggerscxx . . $ InitTriggers(49f6e7a06ebc5aa8).() {
140 int i;
141
142 for (i = 0; i < MAX_TRIGGERScxx . . $ `Descent3/trigger.cpp:128:9`!; i++) {
143 Triggerscxx . . $ Triggers.[i].roomnumcxx . . $ trigger#roomnum. = -1;
144 Triggerscxx . . $ Triggers.[i].facenumcxx . . $ trigger#facenum. = -1;
145 Triggerscxx . . $ Triggers.[i].flagscxx . . $ trigger#flags. = 0;
146
147 Triggerscxx . . $ Triggers.[i].osiris_scriptcxx . . $ trigger#osiris_script..script_idcxx . . $ tOSIRISTriggerScript#script_id. = -1;
148 Triggerscxx . . $ Triggers.[i].osiris_scriptcxx . . $ trigger#osiris_script..script_instancecxx . . $ tOSIRISTriggerScript#script_instance. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
149 }
150
151 Num_triggerscxx . . $ Num_triggers. = 0;
152
153 atexitcxx . . $ atexit(cc8c495369588262).(FreeTriggerscxx . . $ FreeTriggers(49f6e7a06ebc5aa8).);
154 }
155
156 // Free up all the triggers
157 void FreeTriggerscxx . . $ FreeTriggers(49f6e7a06ebc5aa8).() {
158 int i;
159
160 for (i = 0; i < Num_triggerscxx . . $ Num_triggers.; i++)
161 FreeTriggerScriptcxx . . $ FreeTriggerScript(2141560ee082416a).(&Triggerscxx . . $ Triggers.[i]);
162
163 Num_triggerscxx . . $ 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 triggercxx . . $ trigger# *FindTriggercxx . . $ FindTrigger(2eaec2b30530990b).(int roomnum, int facenum) {
170 facecxx . . $ face# *fp = &Roomscxx . . $ Rooms.[roomnum].facescxx . . $ room#faces.[facenum];
171 triggercxx . . $ trigger# *tp;
172 int i;
173
174 // Go through all the triggers & look for the requested one
175 for (i = 0, tp = Triggerscxx . . $ Triggers.; i < Num_triggerscxx . . $ Num_triggers.; i++, tp++)
176 if (tp->roomnumcxx . . $ trigger#roomnum. == roomnum)
177 if (tp->facenumcxx . . $ trigger#facenum. == facenum)
178 return tp;
179
180 Int3cxx . . $ `misc/pserror.h:247:9`!(); // Didn't find the requested trigger -- very bad!
181
182 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
183 }
184
185 // Called to see if a trigger was tripped
186 void CheckTriggercxx . . $ CheckTrigger(11e0cce86948d892).(int roomnum, int facenum, objectcxx . . $ object# *objp, int event_type) {
187 roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[roomnum];
188 facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[facenum];
189
190 ASSERTcxx . . $ `misc/pserror.h:246:9`!((event_type == TT_PASS_THROUGHcxx . . $ `Descent3/trigger.h:143:9`!) || (event_type == TT_COLLIDEcxx . . $ `Descent3/trigger.h:144:9`!));
191
192 if (fp->flagscxx . . $ face#flags. & FF_HAS_TRIGGERcxx . . $ `Descent3/room_external.h:116:9`!) {
193 triggercxx . . $ trigger# *tp;
194 int type;
195
196 tp = FindTriggercxx . . $ FindTrigger(2eaec2b30530990b).(roomnum, facenum);
197 ASSERTcxx . . $ `misc/pserror.h:246:9`!(tp != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
198
199 if (tp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
200 Int3cxx . . $ `misc/pserror.h:247:9`!(); // very bad! Get Matt.
201 return;
202 }
203
204 // Check for dead or disabled trigger
205 if (tp->flagscxx . . $ trigger#flags. & (TF_DISABLEDcxx . . $ `Descent3/trigger.h:94:9`! | TF_DEADcxx . . $ `Descent3/trigger.h:95:9`!))
206 return;
207
208 // If portal trigger, make sure this is a pass-through event
209 if ((fp->portal_numcxx . . $ face#portal_num. != -1) && (event_type != TT_PASS_THROUGHcxx . . $ `Descent3/trigger.h:143:9`!))
210 return;
211
212 // Get the activator type for the object that hit the trigger
213 switch (objp->typecxx . . $ object#type.) {
214 case OBJ_PLAYERcxx . . $ `Descent3/object_external.h:164:9`!:
215 type = AF_PLAYERcxx . . $ `Descent3/trigger.h:100:9`!;
216 break;
217 case OBJ_BUILDINGcxx . . $ `Descent3/object_external.h:176:9`!:
218 case OBJ_ROBOTcxx . . $ `Descent3/object_external.h:162:9`!:
219 type = AF_ROBOTcxx . . $ `Descent3/trigger.h:102:9`!;
220 break;
221 case OBJ_CLUTTERcxx . . $ `Descent3/object_external.h:171:9`!:
222 type = AF_CLUTTERcxx . . $ `Descent3/trigger.h:104:9`!;
223 break;
224 case OBJ_WEAPONcxx . . $ `Descent3/object_external.h:165:9`!: {
225 objectcxx . . $ object# *parent = ObjGetUltimateParentcxx . . $ ObjGetUltimateParent(64a00b84ed3f82ee).(objp);
226 int parent_type = parent->typecxx . . $ object#type.;
227 if (parent_type == OBJ_PLAYERcxx . . $ `Descent3/object_external.h:164:9`!)
228 type = AF_PLAYER_WEAPONcxx . . $ `Descent3/trigger.h:101:9`!;
229 else if (parent_type == OBJ_ROBOTcxx . . $ `Descent3/object_external.h:162:9`! || parent_type == OBJ_BUILDINGcxx . . $ `Descent3/object_external.h:176:9`!)
230 type = AF_ROBOT_WEAPONcxx . . $ `Descent3/trigger.h:103:9`!;
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->activatorcxx . . $ trigger#activator. & type) {
241 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Hit trigger %d\n", tp - Triggers);
242
243 // Execute this trigger's script
244 tOSIRISEventInfocxx . . $ tOSIRISEventInfo# ei;
245 ei.evt_collidecxx . . $ tOSIRISEventInfo#$anonymous_type_1#evt_collide..it_handlecxx . . $ tOSIRISEVTCOLLIDE#it_handle. = objp->handlecxx . . $ object#handle.;
246 Osiris_CallTriggerEventcxx . . $ Osiris_CallTriggerEvent(a44368be0f65be02).((tp - Triggerscxx . . $ Triggers.), EVT_COLLIDEcxx . . $ `scripts/osiris_common.h:366:9`!, &ei);
247
248 // If one-shot, kill this trigger, and mark face as destroyed
249 if (tp->flagscxx . . $ trigger#flags. & TF_ONESHOTcxx . . $ `Descent3/trigger.h:96:9`!) {
250 tp->flagscxx . . $ trigger#flags. |= TF_DEADcxx . . $ `Descent3/trigger.h:95:9`!;
251 if (fp->portal_numcxx . . $ face#portal_num. == -1) // don't destroy a portal face
252 fp->flagscxx . . $ face#flags. |= FF_DESTROYEDcxx . . $ `Descent3/room_external.h:119:9`!;
253 }
254
255 if (tp->flagscxx . . $ trigger#flags. & TF_INFORM_ACTIVATE_TO_LGcxx . . $ `Descent3/trigger.h:97:9`!) {
256 Level_goalscxx . . $ Level_goals..Informcxx . . $ levelgoals#Inform(8589f1c7c2d24df6).(LIT_TRIGGERcxx . . $ `Descent3/levelgoal_external.h:29:9`!, LGF_COMP_ACTIVATEcxx . . $ `Descent3/levelgoal_external.h:46:9`!, tp - Triggerscxx . . $ Triggers.);
257 }
258 }
259 }
260 }
261
262 // Enable or disable a trigger
263 void TriggerSetStatecxx . . $ TriggerSetState(918ebd27a9ddbad3).(int trigger_num, bool enabled) {
264 triggercxx . . $ trigger# *tp = &Triggerscxx . . $ Triggers.[trigger_num];
265
266 if (enabled)
267 tp->flagscxx . . $ trigger#flags. &= ~TF_DISABLEDcxx . . $ `Descent3/trigger.h:94:9`!;
268 else
269 tp->flagscxx . . $ trigger#flags. |= TF_DISABLEDcxx . . $ `Descent3/trigger.h:94:9`!;
270 }
271
272 // Determines if a trigger is enabled or disabled
273 // Returns TRUE if enabled, else false
274 bool TriggerGetStatecxx . . $ TriggerGetState(83c267b20bac841a).(int trigger_num) {
275 triggercxx . . $ trigger# *tp = &Triggerscxx . . $ Triggers.[trigger_num];
276
277 return ((tp->flagscxx . . $ trigger#flags. & TF_DISABLEDcxx . . $ `Descent3/trigger.h:94:9`!) == 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