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