]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/teleporters.qc
Merge branch 'TimePath/global_self' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / teleporters.qc
1 #include "teleporters.qh"
2
3 #if defined(CSQC)
4 #elif defined(MENUQC)
5 #elif defined(SVQC)
6         #include "../../server/_all.qh"
7     #include "../../warpzonelib/common.qh"
8     #include "../../warpzonelib/util_server.qh"
9     #include "../../warpzonelib/server.qh"
10     #include "../constants.qh"
11         #include "../triggers/subs.qh"
12     #include "../util.qh"
13     #include "../../server/weapons/csqcprojectile.qh"
14     #include "../../server/autocvars.qh"
15     #include "../../server/constants.qh"
16     #include "../../server/defs.qh"
17     #include "../deathtypes.qh"
18     #include "../turrets/sv_turrets.qh"
19     #include "../vehicles/all.qh"
20     #include "../mapinfo.qh"
21     #include "../../server/anticheat.qh"
22 #endif
23
24 #ifdef SVQC
25
26 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
27 {
28         if (IS_PLAYER(player) && player.health >= 1)
29         {
30                 TDEATHLOOP(org)
31                 {
32                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
33                                 if(IS_PLAYER(head))
34                                         if(head.health >= 1)
35                                                 return 1;
36                 }
37         }
38         return 0;
39 }
40
41 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
42 {
43         TDEATHLOOP(player.origin)
44         {
45                 if (IS_PLAYER(player) && player.health >= 1)
46                 {
47                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
48                         {
49                                 if(IS_PLAYER(head))
50                                         if(head.health >= 1)
51                                                 ++tdeath_hit;
52                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG, head.origin, '0 0 0');
53                         }
54                 }
55                 else // dead bodies and monsters gib themselves instead of telefragging
56                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG, telefragger.origin, '0 0 0');
57         }
58 }
59
60 void spawn_tdeath(vector v0, entity e, vector v)
61 {
62         tdeath(e, e, e, '0 0 0', '0 0 0');
63 }
64
65 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
66 {SELFPARAM();
67         entity telefragger;
68         vector from;
69
70         if(teleporter.owner)
71                 telefragger = teleporter.owner;
72         else
73                 telefragger = player;
74
75         makevectors (to_angles);
76
77         if(player.teleportable == TELEPORT_NORMAL) // don't play sounds or show particles for anything that isn't a player, maybe change later to block only observers
78         {
79                 if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
80                 {
81                         if(tflags & TELEPORT_FLAG_SOUND)
82                                 sound (player, CH_TRIGGER, "misc/teleport.wav", VOL_BASE, ATTEN_NORM);
83                         if(tflags & TELEPORT_FLAG_PARTICLES)
84                         {
85                                 Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
86                                 Send_Effect(EFFECT_TELEPORT, to + v_forward * 32, '0 0 0', 1);
87                         }
88                         self.pushltime = time + 0.2;
89                 }
90         }
91
92         // Relocate the player
93         // assuming to allows PL_MIN to PL_MAX box and some more
94         from = player.origin;
95         setorigin (player, to);
96         player.oldorigin = to; // don't undo the teleport by unsticking
97         player.angles = to_angles;
98         player.fixangle = true;
99         player.velocity = to_velocity;
100         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
101
102         makevectors(player.angles);
103         Reset_ArcBeam(player, v_forward);
104         UpdateCSQCProjectileAfterTeleport(player);
105
106         if(IS_PLAYER(player))
107         {
108                 if(tflags & TELEPORT_FLAG_TDEATH)
109                         if(player.takedamage && player.deadflag == DEAD_NO && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH)))
110                                 tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
111
112                 // player no longer is on ground
113                 player.flags &= ~FL_ONGROUND;
114
115                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
116                 player.oldvelocity = player.velocity;
117
118                 // reset tracking of who pushed you into a hazard (for kill credit)
119                 if(teleporter.owner)
120                 {
121                         player.pusher = teleporter.owner;
122                         player.pushltime = time + autocvar_g_maxpushtime;
123                         player.istypefrag = player.BUTTON_CHAT;
124                 }
125                 else
126                 {
127                         player.pushltime = 0;
128                         player.istypefrag = 0;
129                 }
130
131                 player.lastteleporttime = time;
132         }
133 }
134
135 entity Simple_TeleportPlayer(entity teleporter, entity player)
136 {
137         vector locout;
138         entity e;
139         float p;
140
141         // Find the output teleporter
142         if(teleporter.enemy)
143         {
144                 e = teleporter.enemy;
145         }
146         else
147         {
148                 RandomSelection_Init();
149                 for(e = world; (e = find(e, targetname, teleporter.target)); )
150                 {
151                         p = 1;
152                         if(autocvar_g_telefrags_avoid)
153                         {
154                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
155                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
156                                         p = 0;
157                         }
158                         RandomSelection_Add(e, 0, string_null, (e.cnt ? e.cnt : 1), p);
159                 }
160                 e = RandomSelection_chosen_ent;
161         }
162
163         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
164
165         makevectors(e.mangle);
166
167         if(e.speed)
168                 if(vlen(player.velocity) > e.speed)
169                         player.velocity = normalize(player.velocity) * max(0, e.speed);
170
171         if(autocvar_g_teleport_maxspeed)
172                 if(vlen(player.velocity) > autocvar_g_teleport_maxspeed)
173                         player.velocity = normalize(player.velocity) * max(0, autocvar_g_teleport_maxspeed);
174
175         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
176         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
177
178         return e;
179 }
180
181 void teleport_findtarget (void)
182 {SELFPARAM();
183         entity e;
184         float n;
185
186         n = 0;
187         for(e = world; (e = find(e, targetname, self.target)); )
188         {
189                 ++n;
190                 if(e.movetype == MOVETYPE_NONE)
191                         waypoint_spawnforteleporter(self, e.origin, 0);
192                 if(e.classname != "info_teleport_destination")
193                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
194         }
195
196         if(n == 0)
197         {
198                 // no dest!
199                 objerror ("Teleporter with nonexistant target");
200                 return;
201         }
202         else if(n == 1)
203         {
204                 // exactly one dest - bots love that
205                 self.enemy = find(e, targetname, self.target);
206         }
207         else
208         {
209                 // have to use random selection every single time
210                 self.enemy = world;
211         }
212
213         // now enable touch
214         self.touch = Teleport_Touch;
215 }
216
217 entity Teleport_Find(vector mi, vector ma)
218 {
219         entity e;
220         for(e = world; (e = find(e, classname, "trigger_teleport")); )
221                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
222                         return e;
223         return world;
224 }
225
226 void WarpZone_PostTeleportPlayer_Callback(entity pl)
227 {SELFPARAM();
228         makevectors(pl.angles);
229         Reset_ArcBeam(pl, v_forward);
230         UpdateCSQCProjectileAfterTeleport(pl);
231         {
232                 WITH(entity, self, pl, anticheat_fixangle());
233         }
234         // "disown" projectiles after teleport
235         if(pl.owner)
236         if(pl.owner == pl.realowner)
237         {
238                 if(!(pl.flags & FL_PROJECTILE))
239                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
240                 pl.owner = world;
241         }
242         if(IS_PLAYER(pl))
243         {
244                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
245                 pl.oldvelocity = pl.velocity;
246                 // reset teleport time tracking too (or multijump can cause insane speeds)
247                 pl.lastteleporttime = time;
248         }
249 }
250 #endif