]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/multivibrator.qc
Merge branch 'terencehill/v_deathtilt_fix' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / trigger / multivibrator.qc
1 #ifdef SVQC
2 void multivibrator_send(entity this)
3 {
4         float newstate;
5         float cyclestart;
6
7         cyclestart = floor((time + this.phase) / (this.wait + this.respawntime)) * (this.wait + this.respawntime) - this.phase;
8
9         newstate = (time < cyclestart + this.wait);
10
11         if(this.state != newstate)
12                 SUB_UseTargets(this, this, NULL);
13         this.state = newstate;
14
15         if(this.state)
16                 this.nextthink = cyclestart + this.wait + 0.01;
17         else
18                 this.nextthink = cyclestart + this.wait + this.respawntime + 0.01;
19 }
20
21 void multivibrator_send_think(entity this)
22 {
23         multivibrator_send(this);
24 }
25
26 void multivibrator_toggle(entity this, entity actor, entity trigger)
27 {
28         if(this.nextthink == 0)
29         {
30                 multivibrator_send(this);
31         }
32         else
33         {
34                 if(this.state)
35                 {
36                         SUB_UseTargets(this, actor, trigger);
37                         this.state = 0;
38                 }
39                 this.nextthink = 0;
40         }
41 }
42
43 void multivibrator_reset(entity this)
44 {
45         if(!(this.spawnflags & 1))
46                 this.nextthink = 0; // wait for a trigger event
47         else
48                 this.nextthink = max(1, time);
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         if(!this.respawntime)
67                 this.respawntime = this.wait;
68
69         this.state = 0;
70         this.use = multivibrator_toggle;
71         setthink(this, multivibrator_send_think);
72         this.nextthink = max(1, time);
73
74         IFTARGETED
75                 multivibrator_reset(this);
76 }
77 #endif