9dcc710f0356b2102c0f20248c9a3e2e6739184b
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / trigger / gravity.qc
1 #ifdef SVQC
2 .entity trigger_gravity_check;
3 void trigger_gravity_remove(entity own)
4 {
5         if(own.trigger_gravity_check.owner == own)
6         {
7                 UpdateCSQCProjectile(own);
8                 own.gravity = own.trigger_gravity_check.gravity;
9                 remove(own.trigger_gravity_check);
10         }
11         else
12                 backtrace("Removing a trigger_gravity_check with no valid owner");
13         own.trigger_gravity_check = NULL;
14 }
15 void trigger_gravity_check_think(entity this)
16 {
17         // This spawns when a player enters the gravity zone and checks if he left.
18         // Each frame, this.count is set to 2 by trigger_gravity_touch() and decreased by 1 here.
19         // It the player has left the gravity trigger, this will be allowed to reach 0 and indicate that.
20         if(this.count <= 0)
21         {
22                 if(this.owner.trigger_gravity_check == this)
23                         trigger_gravity_remove(this.owner);
24                 else
25                         remove(this);
26                 return;
27         }
28         else
29         {
30                 this.count -= 1;
31                 this.nextthink = time;
32         }
33 }
34
35 void trigger_gravity_use(entity this, entity actor, entity trigger)
36 {
37         this.state = !this.state;
38 }
39
40 void trigger_gravity_touch(entity this)
41 {
42         float g;
43
44         if(this.state != true)
45                 return;
46
47         EXACTTRIGGER_TOUCH;
48
49         g = this.gravity;
50
51         if (!(this.spawnflags & 1))
52         {
53                 if(other.trigger_gravity_check)
54                 {
55                         if(this == other.trigger_gravity_check.enemy)
56                         {
57                                 // same?
58                                 other.trigger_gravity_check.count = 2; // gravity one more frame...
59                                 return;
60                         }
61
62                         // compare prio
63                         if(this.cnt > other.trigger_gravity_check.enemy.cnt)
64                                 trigger_gravity_remove(other);
65                         else
66                                 return;
67                 }
68                 other.trigger_gravity_check = spawn();
69                 other.trigger_gravity_check.enemy = this;
70                 other.trigger_gravity_check.owner = other;
71                 other.trigger_gravity_check.gravity = other.gravity;
72                 setthink(other.trigger_gravity_check, trigger_gravity_check_think);
73                 other.trigger_gravity_check.nextthink = time;
74                 other.trigger_gravity_check.count = 2;
75                 if(other.gravity)
76                         g *= other.gravity;
77         }
78
79         if (other.gravity != g)
80         {
81                 other.gravity = g;
82                 if(this.noise != "")
83                         _sound (other, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
84                 UpdateCSQCProjectile(this.owner);
85         }
86 }
87
88 spawnfunc(trigger_gravity)
89 {
90         if(this.gravity == 1)
91                 return;
92
93         EXACTTRIGGER_INIT;
94         settouch(this, trigger_gravity_touch);
95         if(this.noise != "")
96                 precache_sound(this.noise);
97
98         this.state = true;
99         IFTARGETED
100         {
101                 this.use = trigger_gravity_use;
102                 if(this.spawnflags & 2)
103                         this.state = false;
104         }
105 }
106 #endif