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