Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / t_teleporters.qc
1 #include "t_teleporters.qh"
2
3 #if defined(CSQC)
4 #elif defined(MENUQC)
5 #elif defined(SVQC)
6     #include "../warpzonelib/common.qh"
7     #include "../warpzonelib/util_server.qh"
8     #include "../warpzonelib/server.qh"
9     #include "../common/constants.qh"
10     #include "../common/util.qh"
11     #include "weapons/csqcprojectile.qh"
12     #include "autocvars.qh"
13     #include "constants.qh"
14     #include "defs.qh"
15     #include "../common/deathtypes.qh"
16     #include "vehicles/vehicles_def.qh"
17     #include "../common/mapinfo.qh"
18     #include "anticheat.qh"
19 #endif
20
21 void trigger_teleport_use()
22 {
23         if(teamplay)
24                 self.team = activator.team;
25 }
26
27 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
28 {
29         if (IS_PLAYER(player) && player.health >= 1)
30         {
31                 TDEATHLOOP(org)
32                 {
33                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
34                                 if(IS_PLAYER(head))
35                                         if(head.health >= 1)
36                                                 return 1;
37                 }
38         }
39         return 0;
40 }
41
42 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
43 {
44         TDEATHLOOP(player.origin)
45         {
46                 if (IS_PLAYER(player) && player.health >= 1)
47                 {
48                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
49                         {
50                                 if(IS_PLAYER(head))
51                                         if(head.health >= 1)
52                                                 ++tdeath_hit;
53                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG, head.origin, '0 0 0');
54                         }
55                 }
56                 else // dead bodies and monsters gib themselves instead of telefragging
57                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG, telefragger.origin, '0 0 0');
58         }
59 }
60
61 void spawn_tdeath(vector v0, entity e, vector v)
62 {
63         tdeath(e, e, e, '0 0 0', '0 0 0');
64 }
65
66 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
67 {
68         entity telefragger;
69         vector from;
70
71         if(teleporter.owner)
72                 telefragger = teleporter.owner;
73         else
74                 telefragger = player;
75
76         makevectors (to_angles);
77
78         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
79         {
80                 if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
81                 {
82                         if(tflags & TELEPORT_FLAG_SOUND)
83                                 sound (player, CH_TRIGGER, "misc/teleport.wav", VOL_BASE, ATTEN_NORM);
84                         if(tflags & TELEPORT_FLAG_PARTICLES)
85                         {
86                                 pointparticles(particleeffectnum("teleport"), player.origin, '0 0 0', 1);
87                                 pointparticles(particleeffectnum("teleport"), to + v_forward * 32, '0 0 0', 1);
88                         }
89                         self.pushltime = time + 0.2;
90                 }
91         }
92
93         // Relocate the player
94         // assuming to allows PL_MIN to PL_MAX box and some more
95         from = player.origin;
96         setorigin (player, to);
97         player.oldorigin = to; // don't undo the teleport by unsticking
98         player.angles = to_angles;
99         player.fixangle = true;
100         player.velocity = to_velocity;
101         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
102
103         makevectors(player.angles);
104         Reset_ArcBeam(player, v_forward);
105         UpdateCSQCProjectileAfterTeleport(player);
106
107         if(IS_PLAYER(player))
108         {
109                 if(tflags & TELEPORT_FLAG_TDEATH)
110                         if(player.takedamage && player.deadflag == DEAD_NO && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH)))
111                                 tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
112
113                 // player no longer is on ground
114                 player.flags &= ~FL_ONGROUND;
115
116                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
117                 player.oldvelocity = player.velocity;
118
119                 // reset tracking of who pushed you into a hazard (for kill credit)
120                 if(teleporter.owner)
121                 {
122                         player.pusher = teleporter.owner;
123                         player.pushltime = time + autocvar_g_maxpushtime;
124                         player.istypefrag = player.BUTTON_CHAT;
125                 }
126                 else
127                 {
128                         player.pushltime = 0;
129                         player.istypefrag = 0;
130                 }
131
132                 player.lastteleporttime = time;
133         }
134 }
135
136 entity Simple_TeleportPlayer(entity teleporter, entity player)
137 {
138         vector locout;
139         entity e;
140         float p;
141
142         // Find the output teleporter
143         if(teleporter.enemy)
144         {
145                 e = teleporter.enemy;
146         }
147         else
148         {
149                 RandomSelection_Init();
150                 for(e = world; (e = find(e, targetname, teleporter.target)); )
151                 {
152                         p = 1;
153                         if(autocvar_g_telefrags_avoid)
154                         {
155                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
156                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
157                                         p = 0;
158                         }
159                         RandomSelection_Add(e, 0, string_null, (e.cnt ? e.cnt : 1), p);
160                 }
161                 e = RandomSelection_chosen_ent;
162         }
163
164         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
165
166         makevectors(e.mangle);
167
168         if(e.speed)
169                 if(vlen(player.velocity) > e.speed)
170                         player.velocity = normalize(player.velocity) * max(0, e.speed);
171
172         if(autocvar_g_teleport_maxspeed)
173                 if(vlen(player.velocity) > autocvar_g_teleport_maxspeed)
174                         player.velocity = normalize(player.velocity) * max(0, autocvar_g_teleport_maxspeed);
175
176         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
177         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
178
179         return e;
180 }
181
182 void Teleport_Touch (void)
183 {
184         entity oldself;
185         string s;
186
187         if (self.active != ACTIVE_ACTIVE)
188                 return;
189
190         if (!other.teleportable)
191                 return;
192
193         if(other.vehicle)
194         if(!other.vehicle.teleportable)
195                 return;
196
197         if(other.turret_flags & TUR_FLAG_ISTURRET)
198                 return;
199
200         if(other.deadflag != DEAD_NO)
201                 return;
202
203         if(self.team)
204                 if(((self.spawnflags & 4) == 0) == (self.team != other.team))
205                         return;
206
207         EXACTTRIGGER_TOUCH;
208
209         if(IS_PLAYER(other))
210                 RemoveGrapplingHook(other);
211
212         entity e;
213         e = Simple_TeleportPlayer(self, other);
214
215         activator = other;
216         s = self.target; self.target = string_null;
217         SUB_UseTargets();
218         if (!self.target) self.target = s;
219
220         oldself = self;
221         self = e;
222         SUB_UseTargets();
223         self = oldself;
224 }
225
226 void spawnfunc_info_teleport_destination (void)
227 {
228         self.classname = "info_teleport_destination";
229
230         self.mangle = self.angles;
231         self.angles = '0 0 0';
232
233         //setorigin (self, self.origin + '0 0 27');     // To fix a mappers' habit as old as Quake
234         setorigin (self, self.origin);
235
236         IFTARGETED
237         {
238         }
239         else
240                 objerror ("^3Teleport destination without a targetname");
241 }
242
243 void spawnfunc_misc_teleporter_dest (void)
244 {
245         spawnfunc_info_teleport_destination();
246 }
247
248 void spawnfunc_target_teleporter (void)
249 {
250         spawnfunc_info_teleport_destination();
251 }
252
253 void teleport_findtarget (void)
254 {
255         entity e;
256         float n;
257
258         n = 0;
259         for(e = world; (e = find(e, targetname, self.target)); )
260         {
261                 ++n;
262                 if(e.movetype == MOVETYPE_NONE)
263                         waypoint_spawnforteleporter(self, e.origin, 0);
264                 if(e.classname != "info_teleport_destination")
265                         print("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
266         }
267
268         if(n == 0)
269         {
270                 // no dest!
271                 objerror ("Teleporter with nonexistant target");
272                 return;
273         }
274         else if(n == 1)
275         {
276                 // exactly one dest - bots love that
277                 self.enemy = find(e, targetname, self.target);
278         }
279         else
280         {
281                 // have to use random selection every single time
282                 self.enemy = world;
283         }
284
285         // now enable touch
286         self.touch = Teleport_Touch;
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 spawnfunc_trigger_teleport (void)
299 {
300         self.angles = '0 0 0';
301
302         EXACTTRIGGER_INIT;
303
304         self.active = ACTIVE_ACTIVE;
305
306         self.use = trigger_teleport_use;
307
308         // this must be called to spawn the teleport waypoints for bots
309         InitializeEntity(self, teleport_findtarget, INITPRIO_FINDTARGET);
310
311         if (self.target == "")
312         {
313                 objerror ("Teleporter with no target");
314                 return;
315         }
316
317         self.teleport_next = teleport_first;
318         teleport_first = self;
319 }
320
321 void WarpZone_PostTeleportPlayer_Callback(entity pl)
322 {
323         makevectors(pl.angles);
324         Reset_ArcBeam(pl, v_forward);
325         UpdateCSQCProjectileAfterTeleport(pl);
326         {
327                 entity oldself = self;
328                 self = pl;
329                 anticheat_fixangle();
330                 self = oldself;
331         }
332         // "disown" projectiles after teleport
333         if(pl.owner)
334         if(pl.owner == pl.realowner)
335         {
336                 if(!(pl.flags & FL_PROJECTILE))
337                         print("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
338                 pl.owner = world;
339         }
340         if(IS_PLAYER(pl))
341         {
342                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
343                 pl.oldvelocity = pl.velocity;
344                 // reset teleport time tracking too (or multijump can cause insane speeds)
345                 pl.lastteleporttime = time;
346         }
347 }