]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/teleporters.qc
Merge branch 'terencehill/respawn_timer_fix' 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 <lib/warpzone/common.qh>
7     #include <lib/warpzone/util_server.qh>
8     #include <lib/warpzone/server.qh>
9     #include "../constants.qh"
10         #include "../triggers/subs.qh"
11     #include "../util.qh"
12     #include <server/weapons/csqcprojectile.qh>
13     #include <server/autocvars.qh>
14     #include <server/constants.qh>
15     #include <server/defs.qh>
16     #include "../deathtypes/all.qh"
17     #include "../turrets/sv_turrets.qh"
18     #include "../vehicles/all.qh"
19     #include "../mapinfo.qh"
20     #include <server/anticheat.qh>
21 #endif
22
23 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
24 {
25         if (IS_PLAYER(player) && !IS_DEAD(player))
26         {
27                 TDEATHLOOP(org)
28                 {
29                 #ifdef SVQC
30                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
31                 #endif
32                                 if(IS_PLAYER(head))
33                                         if(!IS_DEAD(head))
34                                                 return 1;
35                 }
36         }
37         return 0;
38 }
39
40 #ifdef SVQC
41
42 void trigger_teleport_link(entity this);
43
44 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
45 {
46         TDEATHLOOP(player.origin)
47         {
48                 if (IS_PLAYER(player) && player.health >= 1)
49                 {
50                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
51                         {
52                                 if(IS_PLAYER(head))
53                                         if(head.health >= 1)
54                                                 ++tdeath_hit;
55                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, head.origin, '0 0 0');
56                         }
57                 }
58                 else // dead bodies and monsters gib themselves instead of telefragging
59                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, telefragger.origin, '0 0 0');
60         }
61 }
62
63 void spawn_tdeath(vector v0, entity e, vector v)
64 {
65         tdeath(e, e, e, '0 0 0', '0 0 0');
66 }
67
68 #endif
69
70 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
71 {
72         entity telefragger;
73         vector from;
74
75         if(teleporter.owner)
76                 telefragger = teleporter.owner;
77         else
78                 telefragger = player;
79
80         makevectors (to_angles);
81
82 #ifdef SVQC
83     SELFPARAM();
84         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
85         {
86                 if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
87                 {
88                         if(tflags & TELEPORT_FLAG_SOUND)
89                         {
90                                 string thesound = SND(TELEPORT);
91                                 if(teleporter.noise != "")
92                                 {
93                                         RandomSelection_Init();
94                                         FOREACH_WORD(teleporter.noise, true,
95                                         {
96                                                 RandomSelection_Add(NULL, 0, it, 1, 1);
97                                         });
98                                         thesound = RandomSelection_chosen_string;
99                                 }
100                                 _sound (player, CH_TRIGGER, thesound, VOL_BASE, ATTEN_NORM);
101                         }
102                         if(tflags & TELEPORT_FLAG_PARTICLES)
103                         {
104                                 Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
105                                 Send_Effect(EFFECT_TELEPORT, to + v_forward * 32, '0 0 0', 1);
106                         }
107                         self.pushltime = time + 0.2;
108                 }
109         }
110 #endif
111
112         // Relocate the player
113         // assuming to allows PL_MIN to PL_MAX box and some more
114 #ifdef SVQC
115         from = player.origin;
116         setorigin (player, to);
117         player.oldorigin = to; // don't undo the teleport by unsticking
118         player.angles = to_angles;
119         player.fixangle = true;
120         player.velocity = to_velocity;
121         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
122
123         makevectors(player.angles);
124         Reset_ArcBeam(player, v_forward);
125         UpdateCSQCProjectileAfterTeleport(player);
126         UpdateItemAfterTeleport(player);
127 #elif defined(CSQC)
128         from = player.move_origin;
129         player.move_origin = to;
130         player.move_angles = to_angles;
131         player.move_velocity = to_velocity;
132         player.move_flags &= ~FL_ONGROUND;
133         player.iflags |= IFLAG_TELEPORTED | IFLAG_V_ANGLE | IFLAG_ANGLES;
134         player.csqcmodel_teleported = 1;
135         player.v_angle = to_angles;
136
137         if(player.isplayermodel) // not for anything but the main player
138         {
139                 setproperty(VF_ANGLES, player.move_angles);
140                 setproperty(VF_CL_VIEWANGLES, player.move_angles);
141         }
142
143         makevectors(player.move_angles);
144 #endif
145
146 #ifdef SVQC
147         if(IS_PLAYER(player))
148         {
149                 if(tflags & TELEPORT_FLAG_TDEATH)
150                         if(player.takedamage && !IS_DEAD(player) && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH)))
151                                 tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
152
153                 // player no longer is on ground
154                 UNSET_ONGROUND(player);
155
156                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
157                 player.oldvelocity = player.velocity;
158
159                 // reset tracking of who pushed you into a hazard (for kill credit)
160                 if(teleporter.owner)
161                 {
162                         player.pusher = teleporter.owner;
163                         player.pushltime = time + autocvar_g_maxpushtime;
164                         player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
165                 }
166                 else
167                 {
168                         player.pushltime = 0;
169                         player.istypefrag = 0;
170                 }
171
172                 player.lastteleporttime = time;
173         }
174 #endif
175 }
176
177 entity Simple_TeleportPlayer(entity teleporter, entity player)
178 {
179         vector locout;
180         entity e;
181         float p;
182
183         // Find the output teleporter
184         if(teleporter.enemy)
185         {
186                 e = teleporter.enemy;
187         }
188         else
189         {
190                 RandomSelection_Init();
191                 for(e = world; (e = find(e, targetname, teleporter.target)); )
192                 {
193                         p = 1;
194                         if(STAT(TELEPORT_TELEFRAG_AVOID, player))
195                         {
196                         #ifdef SVQC
197                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
198                         #elif defined(CSQC)
199                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
200                         #endif
201                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
202                                         p = 0;
203                         }
204                         RandomSelection_Add(e, 0, string_null, (e.cnt ? e.cnt : 1), p);
205                 }
206                 e = RandomSelection_chosen_ent;
207         }
208
209 #ifdef SVQC
210         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
211 #elif defined(CSQC)
212         if(!e) { LOG_INFO("Teleport destination could not be found from CSQC.\n"); }
213 #endif
214
215         makevectors(e.mangle);
216
217 #ifdef SVQC
218         if(e.speed)
219                 if(vdist(player.velocity, >, e.speed))
220                         player.velocity = normalize(player.velocity) * max(0, e.speed);
221 #elif defined(CSQC)
222         if(e.speed)
223                 if(vdist(player.move_velocity, >, e.speed))
224                         player.move_velocity = normalize(player.move_velocity) * max(0, e.speed);
225 #endif
226
227 #ifdef SVQC
228         if(STAT(TELEPORT_MAXSPEED, player))
229                 if(vdist(player.velocity, >, STAT(TELEPORT_MAXSPEED, player)))
230                         player.velocity = normalize(player.velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
231 #elif defined(CSQC)
232         if(STAT(TELEPORT_MAXSPEED, player))
233                 if(vdist(player.move_velocity, >, STAT(TELEPORT_MAXSPEED, player)))
234                         player.move_velocity = normalize(player.move_velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
235 #endif
236
237 #ifdef SVQC
238         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
239
240         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
241 #elif defined(CSQC)
242         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
243
244         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.move_velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
245 #endif
246
247         return e;
248 }
249
250 void teleport_findtarget(entity this)
251 {
252         int n = 0;
253         entity e;
254         for(e = world; (e = find(e, targetname, this.target)); )
255         {
256                 ++n;
257 #ifdef SVQC
258                 if(e.movetype == MOVETYPE_NONE)
259                         waypoint_spawnforteleporter(this, e.origin, 0);
260                 if(e.classname != "info_teleport_destination")
261                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
262 #endif
263         }
264
265         if(n == 0)
266         {
267                 // no dest!
268                 objerror ("Teleporter with nonexistant target");
269                 return;
270         }
271         else if(n == 1)
272         {
273                 // exactly one dest - bots love that
274                 this.enemy = find(e, targetname, this.target);
275         }
276         else
277         {
278                 // have to use random selection every single time
279                 this.enemy = world;
280         }
281
282         // now enable touch
283         settouch(this, Teleport_Touch);
284 #ifdef SVQC
285         trigger_teleport_link(this);
286 #endif
287 }
288
289 entity Teleport_Find(vector mi, vector ma)
290 {
291         entity e;
292         for(e = world; (e = find(e, classname, "trigger_teleport")); )
293                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
294                         return e;
295         return world;
296 }
297
298 void WarpZone_PostTeleportPlayer_Callback(entity pl)
299 {
300 #ifdef SVQC
301         makevectors(pl.angles);
302         Reset_ArcBeam(pl, v_forward);
303         UpdateCSQCProjectileAfterTeleport(pl);
304         UpdateItemAfterTeleport(pl);
305     if (IS_PLAYER(pl)) anticheat_fixangle(pl);
306 #endif
307         // "disown" projectiles after teleport
308         if(pl.owner)
309         if(pl.owner == pl.realowner)
310         {
311         #ifdef SVQC
312                 if(!(pl.flags & FL_PROJECTILE))
313         #elif defined(CSQC)
314                 if(!(pl.move_flags & BIT(15))) // FL_PROJECTILE
315         #endif
316                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
317                 pl.owner = world;
318         }
319         if(IS_PLAYER(pl))
320         {
321                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
322         #ifdef SVQC
323                 pl.oldvelocity = pl.velocity;
324         #endif
325                 // reset teleport time tracking too (or multijump can cause insane speeds)
326                 pl.lastteleporttime = time;
327         }
328 }