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