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