Xonotic
multivibrator.qc
Go to the documentation of this file.
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  this.state = newstate;
15 
16  if(this.state)
17  this.nextthink = cyclestart + this.wait + 0.01;
18  else
19  this.nextthink = cyclestart + this.wait + this.respawntime + 0.01;
20 }
21 
22 void multivibrator_send_think(entity this)
23 {
24  multivibrator_send(this);
25 }
26 
27 void multivibrator_toggle(entity this, entity actor, entity trigger)
28 {
29  if(this.nextthink == 0)
30  {
31  multivibrator_send(this);
32  }
33  else
34  {
35  if(this.state)
36  {
37  SUB_UseTargets(this, actor, trigger);
38  this.state = 0;
39  }
40  this.nextthink = 0;
41  }
42 }
43 
44 void multivibrator_reset(entity this)
45 {
46  if(!(this.spawnflags & START_ENABLED))
47  this.nextthink = 0; // wait for a trigger event
48  else
49  this.nextthink = max(1, time);
50 }
51 
52 /*QUAKED trigger_multivibrator (.5 .5 .5) (-8 -8 -8) (8 8 8) START_ENABLED
53 "Multivibrator" trigger gate... repeatedly sends trigger events. When triggered, turns on or off.
54 -------- KEYS --------
55 target: trigger all entities with this targetname when it goes off
56 targetname: name that identifies this entity so it can be triggered; when off, it always uses the OFF state
57 phase: offset of the timing
58 wait: "on" cycle time (default: 1)
59 respawntime: "off" cycle time (default: same as wait)
60 -------- SPAWNFLAGS --------
61 START_ENABLED: assume it is already turned on (when targeted)
62 */
63 spawnfunc(trigger_multivibrator)
64 {
65  if(!this.wait)
66  this.wait = 1;
67  if(!this.respawntime)
68  this.respawntime = this.wait;
69 
70  this.state = 0;
71  this.use = multivibrator_toggle;
72  setthink(this, multivibrator_send_think);
73  this.nextthink = max(1, time);
74 
75  if(this.targetname && this.targetname != "")
76  multivibrator_reset(this);
77 }
78 #endif
float state
Definition: subs.qh:32
float respawntime
Definition: items.qh:36
entity() spawn
void SUB_UseTargets(entity this, entity actor, entity trigger)
Definition: triggers.qc:366
spawnfunc(info_player_attacker)
Definition: sv_assault.qc:283
const int START_ENABLED
Definition: defs.qh:6
float spawnflags
Definition: progsdefs.qc:191
float wait
Definition: subs.qh:39
#define NULL
Definition: post.qh:17
float nextthink
Definition: csprogsdefs.qc:121
string targetname
Definition: progsdefs.qc:194
#define setthink(e, f)
#define use
Definition: csprogsdefs.qh:50
float phase
Definition: platforms.qh:10
float time
Definition: csprogsdefs.qc:16