-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathg_entity_trigger.c
More file actions
627 lines (499 loc) · 17.3 KB
/
Copy pathg_entity_trigger.c
File metadata and controls
627 lines (499 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Copyright(c) 1997-2001 id Software, Inc.
* Copyright(c) 2002 The Quakeforge Project.
* Copyright(c) 2006 Quetoo.
*
* 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 "g_local.h"
#include "bg_pmove.h"
#define TRIGGERED 0x1
#define SHOOTABLE 0x2
/**
* @brief Initializes a trigger entity, setting its move direction, solid type, and model.
*/
static void G_Trigger_Init(g_entity_t *ent) {
if (!Vec3_Equal(ent->s.angles, Vec3_Zero())) {
G_SetMoveDir(ent);
}
ent->solid = SOLID_TRIGGER;
ent->move_type = MOVE_TYPE_NONE;
gi.SetModel(ent, ent->model);
ent->sv_flags = SVF_NO_CLIENT;
}
/**
* @brief The wait time has passed, so set back up for another activation
*/
static void G_trigger_multiple_Wait(g_entity_t *ent) {
ent->next_think = 0;
}
/**
* @brief Called after the wait period expires, re-enabling the trigger for another activation.
*/
static void G_trigger_multiple_Think(g_entity_t *ent) {
if (ent->next_think) {
return; // already been triggered
}
G_UseTargets(ent, ent->activator);
if (ent->wait > 0) {
ent->Think = G_trigger_multiple_Wait;
ent->next_think = g_level.time + ent->wait * 1000;
} else {
ent->Touch = NULL;
ent->next_think = g_level.time + QUETOO_TICK_MILLIS;
ent->Think = G_FreeEntity;
}
}
/**
* @brief Fires the trigger's targets after the optional delay, then resets or removes the trigger.
*/
static void G_trigger_multiple_Use(g_entity_t *ent, g_entity_t *other,
g_entity_t *activator) {
ent->activator = activator;
G_trigger_multiple_Think(ent);
}
/**
* @brief Handles use activation of a `trigger_multiple`, delegating to the think function.
*/
static void G_trigger_multiple_Touch(g_entity_t *ent, g_entity_t *other, const cm_trace_t *trace) {
if (!other->client) {
const bool isProjectile = other->owner && other->owner->client;
if (isProjectile && (ent->spawn_flags & SHOOTABLE)) {
// we're a shootable trigger, and we've been shot
} else {
return;
}
}
if (!Vec3_Equal(ent->move_dir, Vec3_Zero())) {
vec3_t forward;
Vec3_Vectors(other->s.angles, &forward, NULL, NULL);
if (Vec3_Dot(forward, ent->move_dir) < 0.0) {
return;
}
}
ent->activator = other;
G_trigger_multiple_Think(ent);
}
/**
* @brief Handles touch events on a `trigger_multiple`, activating it when a qualifying entity enters.
*/
static void G_trigger_multiple_Enable(g_entity_t *ent, g_entity_t *other,
g_entity_t *activator) {
ent->solid = SOLID_TRIGGER;
ent->Use = G_trigger_multiple_Use;
gi.LinkEntity(ent);
}
/*QUAKED trigger_multiple (.5 .5 .5) ? triggered shootable
Triggers multiple targets at fixed intervals.
-------- Keys --------
delay : Delay in seconds between activation and firing of targets (default 0).
wait : Interval in seconds between activations (default 0.2).
message : An optional string to display when activated.
target : The name of the entity or team to use on activation.
killtarget : The name of the entity or team to kill on activation.
targetname : The target name of this entity if it is to be triggered.
-------- Spawn flags --------
triggered : If set, this trigger must be targeted before it will activate.
shootable : If set, this trigger will fire when projectiles touch it.
*/
void G_trigger_multiple(g_entity_t *ent) {
ent->sound = gi.SoundIndex("misc/chat");
if (!ent->wait) {
ent->wait = 0.2;
}
ent->Touch = G_trigger_multiple_Touch;
ent->move_type = MOVE_TYPE_NONE;
ent->sv_flags |= SVF_NO_CLIENT;
if (ent->spawn_flags & TRIGGERED) {
ent->solid = SOLID_NOT;
ent->Use = G_trigger_multiple_Enable;
} else {
ent->solid = SOLID_TRIGGER;
ent->Use = G_trigger_multiple_Use;
}
if (!Vec3_Equal(ent->s.angles, Vec3_Zero())) {
G_SetMoveDir(ent);
}
gi.SetModel(ent, ent->model);
gi.LinkEntity(ent);
}
/*QUAKED trigger_once (.5 .5 .5) ? triggered
Triggers multiple targets once.
-------- Keys --------
delay : Delay in seconds between activation and firing of targets (default 0).
message : An optional string to display when activated.
target : The name of the entity or team to use on activation.
killtarget : The name of the entity or team to kill on activation.
targetname : The target name of this entity if it is to be triggered.
-------- Spawn flags --------
triggered : If set, this trigger must be targeted before it will activate.
*/
void G_trigger_once(g_entity_t *ent) {
ent->wait = -1;
G_trigger_multiple(ent);
}
/**
* @brief Enables a previously dormant triggered trigger, making it solid and ready to activate.
*/
static void G_trigger_relay_Use(g_entity_t *ent, g_entity_t *other,
g_entity_t *activator) {
G_UseTargets(ent, activator);
}
/*QUAKED trigger_relay (.5 .5 .5) (-8 -8 -8) (8 8 8)
A trigger that can not be touched, but must be triggered by another entity.
-------- Keys --------
delay : The delay in seconds between activation and firing of targets (default 0).
message : An optional string to display when activated.
target : The name of the entity or team to use on activation.
killtarget : The name of the entity or team to kill on activation.
targetname : The target name of this entity.
*/
void G_trigger_relay(g_entity_t *ent) {
ent->Use = G_trigger_relay_Use;
}
/*QUAKED trigger_always (.5 .5 .5) (-8 -8 -8) (8 8 8)
Triggers targets once at level spawn.
-------- Keys --------
delay : The delay in seconds between activation and firing of targets (default 0.2).
message : An optional message to display when this trigger fires.
target : The name of the entity or team to use on activation.
kill_target : The name of the entity or team to kill on activation.
*/
void G_trigger_always(g_entity_t *ent) {
// we must have some delay to make sure our use targets are present
if (ent->delay < 0.2) {
ent->delay = 0.2;
}
G_UseTargets(ent, ent);
}
#define PUSH_ONCE 1
#define PUSH_EFFECT 2
/**
* @brief Handles touch events on a `trigger_push`, applying velocity to the touching entity.
*/
static void G_trigger_push_Touch(g_entity_t *ent, g_entity_t *other, const cm_trace_t *trace) {
if (other->move_type == MOVE_TYPE_WALK || other->move_type == MOVE_TYPE_BOUNCE) {
other->velocity = Vec3_Scale(ent->move_dir, ent->speed * 10.0);
if (other->client) {
other->client->ps.pm_state.flags |= PMF_TIME_PUSHED;
other->client->ps.pm_state.time = 240;
}
if (other->push_time < g_level.time) {
other->push_time = g_level.time + 1500;
G_MulticastSound(&(const g_play_sound_t) {
.index = ent->move_info.sound_start,
.origin = &other->s.origin,
}, MULTICAST_PHS);
}
}
if (ent->spawn_flags & PUSH_ONCE) {
G_FreeEntity(ent);
}
}
/**
* @brief Creates an effect trail for the specified entity.
*/
static void G_trigger_push_Effect(g_entity_t *ent) {
g_entity_t *effect = G_AllocEntity(__func__);
effect->s.origin = Box3_Center(ent->bounds);
effect->move_type = MOVE_TYPE_NONE;
effect->s.trail = TRAIL_TELEPORTER;
gi.LinkEntity(effect);
}
/*QUAKED trigger_push (.5 .5 .5) ? push_once push_effects
Pushes the player in any direction. These are commonly used to make jump pads to send the player upwards. Using the angles key, you can project the player in any direction using "pitch yaw roll."
-------- Keys --------
angles : The direction to push the player in "pitch yaw roll" notation (e.g. -80 270 0).
sound : The sound effect to play when the player is pushed (default "trigger/push").
speed : The speed with which to push the player (default 100).
-------- Spawn flags --------
push_once : If set, the pusher is freed after it is used once.
push_effects : If set, emit particle effects to indicate that a pusher is here.
*/
void G_trigger_push(g_entity_t *ent) {
G_Trigger_Init(ent);
ent->Touch = G_trigger_push_Touch;
const cm_entity_t *sound = gi.EntityValue(ent->def, "sound");
if (sound->parsed & ENTITY_STRING) {
ent->move_info.sound_start = gi.SoundIndex(sound->string);
} else {
ent->move_info.sound_start = gi.SoundIndex("trigger/push");
}
if (!ent->speed) {
ent->speed = 100;
}
gi.LinkEntity(ent);
if (ent->spawn_flags & PUSH_EFFECT) {
G_trigger_push_Effect(ent);
}
}
/**
* @brief Handles use activation of a `trigger_hurt`, toggling its solidity on or off.
*/
static void G_trigger_hurt_Use(g_entity_t *ent, g_entity_t *other, g_entity_t *activator) {
if (ent->solid == SOLID_NOT) {
ent->solid = SOLID_TRIGGER;
} else {
ent->solid = SOLID_NOT;
}
gi.LinkEntity(ent);
if (!(ent->spawn_flags & 2)) {
ent->Use = NULL;
}
}
/**
* @brief Handles touch events on a `trigger_hurt`, dealing damage to entities that enter it.
*/
static void G_trigger_hurt_Touch(g_entity_t *ent, g_entity_t *other, const cm_trace_t *trace) {
if (!other->take_damage) { // deal with items that land on us
if (other->item) {
if (other->item->def.type == ITEM_FLAG) {
G_ResetDroppedFlag(other);
} else if (other->item->def.type == ITEM_TECH) {
G_ResetDroppedTech(other);
} else {
G_FreeEntity(other);
}
}
G_Debug("%s\n", etos(other));
return;
}
if (other->dead) {
return;
}
if (ent->timestamp > g_level.time) {
return;
}
if (ent->spawn_flags & 16) {
ent->timestamp = g_level.time + 1000;
} else {
ent->timestamp = g_level.time + 100;
}
const int16_t d = ent->damage;
int32_t dflags = DMG_NO_ARMOR;
if (ent->spawn_flags & 8) {
dflags = DMG_NO_GOD;
}
G_Damage(&(g_damage_t) {
.target = other,
.inflictor = ent,
.attacker = NULL,
.dir = Vec3_Zero(),
.point = other->s.origin,
.normal = Vec3_Zero(),
.damage = d,
.knockback = d >> 2,
.flags = dflags,
.mod = MOD_TRIGGER_HURT
});
}
/*QUAKED trigger_hurt (.5 .5 .5) ? start_off toggle ? no_protection slow
Any player that touches this will be hurt by "dmg" points of damage every 100ms (very fast).
-------- Keys --------
dmg : The damage done every 100ms to any player who touches this entity (default 2).
targetname : The target name of this entity, if it is to be triggered.
-------- Spawn flags --------
start_off : If set, this entity must be activated before it will hurt players.
toggle : If set, this entity is toggled each time it is activated.
?
no_protection : If set, armor will not be used to absorb damage inflicted by this entity.
slow : Decreases the damage rate to once per second.
*/
void G_trigger_hurt(g_entity_t *ent) {
G_Trigger_Init(ent);
ent->Touch = G_trigger_hurt_Touch;
if (!ent->damage) {
ent->damage = 2;
}
if (ent->spawn_flags & 1) {
ent->solid = SOLID_NOT;
} else {
ent->solid = SOLID_TRIGGER;
}
if (ent->spawn_flags & 2) {
ent->Use = G_trigger_hurt_Use;
}
gi.LinkEntity(ent);
}
/**
* @brief Handles touch events on a `trigger_exec`, executing a console command or script.
*/
static void G_trigger_exec_Touch(g_entity_t *ent, g_entity_t *other, const cm_trace_t *trace) {
if (ent->timestamp > g_level.time) {
return;
}
ent->timestamp = g_level.time + ent->delay * 1000;
const char *command = gi.EntityValue(ent->def, "command")->nullable_string;
if (command) {
gi.Cbuf(va("%s\n", command));
}
else {
const char *script = gi.EntityValue(ent->def, "script")->nullable_string;
if (script) {
gi.Cbuf(va("exec %s\n", script));
}
}
}
/**
* @brief Handles touch events on a `trigger_void`, pulling players into the void.
*/
static void G_trigger_void_Touch(g_entity_t *ent, g_entity_t *other, const cm_trace_t *trace) {
if (!other->take_damage) { // handle dropped items landing in the void
if (other->item) {
if (other->item->def.type == ITEM_FLAG) {
G_ResetDroppedFlag(other);
} else if (other->item->def.type == ITEM_TECH) {
G_ResetDroppedTech(other);
} else {
G_FreeEntity(other);
}
}
return;
}
if (!other->client || other->dead) {
return;
}
g_client_t *cl = other->client;
if (!cl->in_void) {
// Only enter void from the top half of the brush (side entry is ignored)
const float mid_z = (ent->abs_bounds.mins.z + ent->abs_bounds.maxs.z) * 0.5f;
if (other->s.origin.z < mid_z) {
return;
}
if (ent->spawn_flags & 1) { // VOID_TELEPORT: warp to a spawn instead of killing
const g_entity_t *spawn = NULL;
if (ent->target) { // a specific destination pad was requested
spawn = G_Find(NULL, EOFS(target_name), ent->target);
if (!spawn) {
gi.Warn("trigger_void: target \"%s\" not found\n", ent->target);
}
}
if (!spawn) { // no target, or it was missing; pick a random spawn point
spawn = G_SelectSpawnPoint(cl);
}
if (spawn) {
gi.UnlinkEntity(other);
other->s.origin = spawn->s.origin;
other->s.origin.z += 8.0;
cl->ps.pm_state.flags &= ~PMF_TIME_MASK;
cl->ps.pm_state.flags |= PMF_TIME_TELEPORT;
cl->ps.pm_state.time = 20;
cl->ps.pm_state.delta_angles = Vec3_Subtract(spawn->s.angles, cl->cmd_angles);
other->velocity = Vec3_Zero();
cl->cmd_angles = Vec3_Zero();
cl->angles = Vec3_Zero();
other->s.event = EV_CLIENT_TELEPORT;
G_MulticastSound(&(const g_play_sound_t) {
.index = g_media.sounds.teleport,
.origin = &other->s.origin,
}, MULTICAST_PHS);
gi.LinkEntity(other);
}
return;
}
// Mark as falling into the void, suppress HUD, play death sound
cl->in_void = true;
cl->void_touch_time = g_level.time;
G_MulticastSound(&(const g_play_sound_t) {
.index = gi.SoundIndex("*death_void"),
.entity = other,
}, MULTICAST_PHS);
return;
}
// Still overlapping the brush; refresh the touch time so the state isn't
// cleared as stale (see G_ClientBeginFrame).
cl->void_touch_time = g_level.time;
// Already falling — kill when the player's feet reach the brush's bottom face
if (other->abs_bounds.mins.z > ent->abs_bounds.mins.z) {
return;
}
// Strip powerups without dropping them
if (cl->inventory[POWERUP_QUAD]) {
cl->inventory[POWERUP_QUAD] = 0;
cl->quad_damage_time = 0;
other->s.effects &= ~EF_QUAD;
}
if (cl->inventory[POWERUP_INVISIBILITY]) {
cl->inventory[POWERUP_INVISIBILITY] = 0;
cl->invisibility_time = 0;
other->s.effects &= ~EF_INVISIBILITY;
}
if (cl->inventory[POWERUP_INVULNERABILITY]) {
cl->inventory[POWERUP_INVULNERABILITY] = 0;
cl->invulnerability_time = 0;
cl->invulnerability_countdown_time = 0;
other->s.effects &= ~EF_INVULNERABILITY;
}
// Return the carried CTF flag immediately rather than dropping it
if (g_level.ctf) {
G_ReturnFlag(cl);
}
// Respawn any carried tech back into its pool rather than dropping it
if (g_level.techs) {
g_entity_t *tech = G_TossTech(cl);
if (tech) {
G_ResetDroppedTech(tech);
}
}
// Instant kill bypassing armor and god mode; G_ClientDie handles gibs
cl->in_void = false;
G_Damage(&(g_damage_t) {
.target = other,
.inflictor = ent,
.attacker = NULL,
.dir = Vec3_Zero(),
.point = other->s.origin,
.normal = Vec3_Zero(),
.damage = 999,
.knockback = 0,
.flags = DMG_NO_GOD,
.mod = MOD_TRIGGER_VOID
});
}
/*QUAKED trigger_void (.5 .5 .5) ? teleport
Players stepping onto the top face are pulled into the void. A special death sound plays and
the HUD is suppressed during the fall. Upon reaching the bottom face, the player is instantly
killed: powerups are removed, carried techs are respawned, and any carried CTF flag is returned.
-------- Keys --------
target : With the teleport flag, the target_name of a specific destination (e.g. a
misc_teleporter_dest) to warp to. If unset, a random spawn point is chosen.
-------- Spawn flags --------
teleport : Instead of killing the player, teleport them to a spawn point (see target).
*/
void G_trigger_void(g_entity_t *ent) {
G_Trigger_Init(ent);
ent->Touch = G_trigger_void_Touch;
gi.LinkEntity(ent);
}
/*QUAKED trigger_exec (0 1 0) ?
Executes a console command or script file when activated.
-------- Keys --------
command : The console command(s) to execute.
script : The script file (.cfg) to execute.
delay : The delay in seconds between activation and execution of the commands.
*/
void G_trigger_exec(g_entity_t *ent) {
const char *command = gi.EntityValue(ent->def, "command")->nullable_string;
const char *script = gi.EntityValue(ent->def, "script")->nullable_string;
if (!command && !script) {
G_Debug("No command or script at %s", vtos(ent->s.origin));
G_FreeEntity(ent);
return;
}
G_Trigger_Init(ent);
ent->Touch = G_trigger_exec_Touch;
gi.LinkEntity(ent);
}