]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/multivibrator.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / trigger / multivibrator.qc
1 #include "multivibrator.qh"
2 #ifdef SVQC
3 void multivibrator_send(entity this)
4 {
5         float newstate;
6         float cyclestart;
7
8         cyclestart = floor((time + this.phase) / (this.wait + this.respawntime)) * (this.wait + this.respawntime) - this.phase;
9
10         newstate = (time < cyclestart + this.wait);
11
12         if (this.state != newstate) {
13                 SUB_UseTargets(this, this, NULL);
14         }
15         this.state = newstate;
16
17         if (this.state) {
18                 this.nextthink = cyclestart + this.wait + 0.01;
19         } else {
20                 this.nextthink = cyclestart + this.wait + this.respawntime + 0.01;
21         }
22 }
23
24 void multivibrator_send_think(entity this)
25 {
26         multivibrator_send(this);
27 }
28
29 void multivibrator_toggle(entity this, entity actor, entity trigger)
30 {
31         if (this.nextthink == 0) {
32                 multivibrator_send(this);
33         } else {
34                 if (this.state) {
35                         SUB_UseTargets(this, actor, trigger);
36                         this.state = 0;
37                 }
38                 this.nextthink = 0;
39         }
40 }
41
42 void multivibrator_reset(entity this)
43 {
44         if (!(this.spawnflags & 1)) {
45                 this.nextthink = 0; // wait for a trigger event
46         } else {
47                 this.nextthink = max(1, time);
48         }
49 }
50
51 /*QUAKED trigger_multivibrator (.5 .5 .5) (-8 -8 -8) (8 8 8) START_ON
52 "Multivibrator" trigger gate... repeatedly sends trigger events. When triggered, turns on or off.
53 -------- KEYS --------
54 target: trigger all entities with this targetname when it goes off
55 targetname: name that identifies this entity so it can be triggered; when off, it always uses the OFF state
56 phase: offset of the timing
57 wait: "on" cycle time (default: 1)
58 respawntime: "off" cycle time (default: same as wait)
59 -------- SPAWNFLAGS --------
60 START_ON: assume it is already turned on (when targeted)
61 */
62 spawnfunc(trigger_multivibrator)
63 {
64         if (!this.wait) {
65                 this.wait = 1;
66         }
67         if (!this.respawntime) {
68                 this.respawntime = this.wait;
69         }
70
71         this.state = 0;
72         this.use = multivibrator_toggle;
73         setthink(this, multivibrator_send_think);
74         this.nextthink = max(1, time);
75
76         if (THIS_TARGETED) {
77                 multivibrator_reset(this);
78         }
79 }
80 #endif