]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_teleporters.qc
60a1ca7f01a3d833be0ba24b3df92a252587949d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / t_teleporters.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qh"
5     #include "../dpdefs/dpextensions.qh"
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 "tturrets/include/turrets_early.qh"
17     #include "vehicles/vehicles_def.qh"
18     #include "../common/mapinfo.qh"
19     #include "anticheat.qh"
20 #endif
21
22 void trigger_teleport_use()
23 {
24         if(teamplay)
25                 self.team = activator.team;
26 }
27
28 #define TDEATHLOOP(o) \
29         entity head; \
30         vector deathmin; \
31         vector deathmax; \
32         float deathradius; \
33         deathmin = (o) + player.mins; \
34         deathmax = (o) + player.maxs; \
35         if(telefragmin != telefragmax) \
36         { \
37                 if(deathmin.x > telefragmin.x) deathmin_x = telefragmin.x; \
38                 if(deathmin.y > telefragmin.y) deathmin_y = telefragmin.y; \
39                 if(deathmin.z > telefragmin.z) deathmin_z = telefragmin.z; \
40                 if(deathmax.x < telefragmax.x) deathmax_x = telefragmax.x; \
41                 if(deathmax.y < telefragmax.y) deathmax_y = telefragmax.y; \
42                 if(deathmax.z < telefragmax.z) deathmax_z = telefragmax.z; \
43         } \
44         deathradius = max(vlen(deathmin), vlen(deathmax)); \
45         for(head = findradius(o, deathradius); head; head = head.chain) \
46                 if(head != player) \
47                         if(head.takedamage) \
48                                 if(boxesoverlap(deathmin, deathmax, head.absmin, head.absmax))
49
50
51 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
52 {
53         if (IS_PLAYER(player) && player.health >= 1)
54         {
55                 TDEATHLOOP(org)
56                 {
57                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
58                                 if(IS_PLAYER(head))
59                                         if(head.health >= 1)
60                                                 return 1;
61                 }
62         }
63         return 0;
64 }
65 float tdeath_hit;
66 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
67 {
68         TDEATHLOOP(player.origin)
69         {
70                 if (IS_PLAYER(player) && player.health >= 1)
71                 {
72                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
73                         {
74                                 if(IS_PLAYER(head))
75                                         if(head.health >= 1)
76                                                 ++tdeath_hit;
77                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG, head.origin, '0 0 0');
78                         }
79                 }
80                 else // dead bodies and monsters gib themselves instead of telefragging
81                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG, telefragger.origin, '0 0 0');
82         }
83 }
84
85 void spawn_tdeath(vector v0, entity e, vector v)
86 {
87         tdeath(e, e, e, '0 0 0', '0 0 0');
88 }
89
90 .entity pusher;
91 const float TELEPORT_FLAG_SOUND = 1;
92 const float TELEPORT_FLAG_PARTICLES = 2;
93 const float TELEPORT_FLAG_TDEATH = 4;
94 const float TELEPORT_FLAG_FORCE_TDEATH = 8;
95
96 #define TELEPORT_FLAGS_WARPZONE   0
97 #define TELEPORT_FLAGS_PORTAL     (TELEPORT_FLAG_SOUND | TELEPORT_FLAG_PARTICLES | TELEPORT_FLAG_TDEATH | TELEPORT_FLAG_FORCE_TDEATH)
98 #define TELEPORT_FLAGS_TELEPORTER (TELEPORT_FLAG_SOUND | TELEPORT_FLAG_PARTICLES | TELEPORT_FLAG_TDEATH)
99
100 // types for .teleportable entity setting
101 const float TELEPORT_NORMAL = 1; // play sounds/effects etc
102 const float TELEPORT_SIMPLE = 2; // only do teleport, nothing special
103
104 void Reset_ArcBeam(entity player, vector forward);
105 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
106 {
107         entity telefragger;
108         vector from;
109
110         if(teleporter.owner)
111                 telefragger = teleporter.owner;
112         else
113                 telefragger = player;
114
115         makevectors (to_angles);
116
117         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
118         {
119                 if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
120                 {
121                         if(tflags & TELEPORT_FLAG_SOUND)
122                                 sound (player, CH_TRIGGER, "misc/teleport.wav", VOL_BASE, ATTEN_NORM);
123                         if(tflags & TELEPORT_FLAG_PARTICLES)
124                         {
125                                 pointparticles(particleeffectnum("teleport"), player.origin, '0 0 0', 1);
126                                 pointparticles(particleeffectnum("teleport"), to + v_forward * 32, '0 0 0', 1);
127                         }
128                         self.pushltime = time + 0.2;
129                 }
130         }
131
132         // Relocate the player
133         // assuming to allows PL_MIN to PL_MAX box and some more
134         from = player.origin;
135         setorigin (player, to);
136         player.oldorigin = to; // don't undo the teleport by unsticking
137         player.angles = to_angles;
138         player.fixangle = true;
139         player.velocity = to_velocity;
140         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
141
142         makevectors(player.angles);
143         Reset_ArcBeam(player, v_forward);
144         UpdateCSQCProjectileAfterTeleport(player);
145
146         if(IS_PLAYER(player))
147         {
148                 if(tflags & TELEPORT_FLAG_TDEATH)
149                         if(player.takedamage && player.deadflag == DEAD_NO && !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                 player.flags &= ~FL_ONGROUND;
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 = player.BUTTON_CHAT;
164                 }
165                 else
166                 {
167                         player.pushltime = 0;
168                         player.istypefrag = 0;
169                 }
170
171                 player.lastteleporttime = time;
172         }
173 }
174
175 entity Simple_TeleportPlayer(entity teleporter, entity player)
176 {
177         vector locout;
178         entity e;
179         float p;
180
181         // Find the output teleporter
182         if(teleporter.enemy)
183         {
184                 e = teleporter.enemy;
185         }
186         else
187         {
188                 RandomSelection_Init();
189                 for(e = world; (e = find(e, targetname, teleporter.target)); )
190                 {
191                         p = 1;
192                         if(autocvar_g_telefrags_avoid)
193                         {
194                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
195                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
196                                         p = 0;
197                         }
198                         RandomSelection_Add(e, 0, string_null, (e.cnt ? e.cnt : 1), p);
199                 }
200                 e = RandomSelection_chosen_ent;
201         }
202
203         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
204
205         makevectors(e.mangle);
206
207         if(e.speed)
208                 if(vlen(player.velocity) > e.speed)
209                         player.velocity = normalize(player.velocity) * max(0, e.speed);
210
211         if(autocvar_g_teleport_maxspeed)
212                 if(vlen(player.velocity) > autocvar_g_teleport_maxspeed)
213                         player.velocity = normalize(player.velocity) * max(0, autocvar_g_teleport_maxspeed);
214
215         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
216         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
217
218         return e;
219 }
220
221 void Teleport_Touch (void)
222 {
223         entity oldself;
224         string s;
225
226         if (self.active != ACTIVE_ACTIVE)
227                 return;
228
229         if (!other.teleportable)
230                 return;
231
232         if(other.vehicle)
233         if(!other.vehicle.teleportable)
234                 return;
235
236         if(other.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
237                 return;
238
239         if(other.deadflag != DEAD_NO)
240                 return;
241
242         if(self.team)
243                 if(((self.spawnflags & 4) == 0) == (self.team != other.team))
244                         return;
245
246         EXACTTRIGGER_TOUCH;
247
248         if(IS_PLAYER(other))
249                 RemoveGrapplingHook(other);
250
251         entity e;
252         e = Simple_TeleportPlayer(self, other);
253
254         activator = other;
255         s = self.target; self.target = string_null;
256         SUB_UseTargets();
257         if (!self.target) self.target = s;
258
259         oldself = self;
260         self = e;
261         SUB_UseTargets();
262         self = oldself;
263 }
264
265 void spawnfunc_info_teleport_destination (void)
266 {
267         self.classname = "info_teleport_destination";
268
269         self.mangle = self.angles;
270         self.angles = '0 0 0';
271
272         //setorigin (self, self.origin + '0 0 27');     // To fix a mappers' habit as old as Quake
273         setorigin (self, self.origin);
274
275         IFTARGETED
276         {
277         }
278         else
279                 objerror ("^3Teleport destination without a targetname");
280 }
281
282 void spawnfunc_misc_teleporter_dest (void)
283 {
284         spawnfunc_info_teleport_destination();
285 }
286
287 void spawnfunc_target_teleporter (void)
288 {
289         spawnfunc_info_teleport_destination();
290 }
291
292 void teleport_findtarget (void)
293 {
294         entity e;
295         float n;
296
297         n = 0;
298         for(e = world; (e = find(e, targetname, self.target)); )
299         {
300                 ++n;
301                 if(e.movetype == MOVETYPE_NONE)
302                         waypoint_spawnforteleporter(self, e.origin, 0);
303                 if(e.classname != "info_teleport_destination")
304                         print("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
305         }
306
307         if(n == 0)
308         {
309                 // no dest!
310                 objerror ("Teleporter with nonexistant target");
311                 return;
312         }
313         else if(n == 1)
314         {
315                 // exactly one dest - bots love that
316                 self.enemy = find(e, targetname, self.target);
317         }
318         else
319         {
320                 // have to use random selection every single time
321                 self.enemy = world;
322         }
323
324         // now enable touch
325         self.touch = Teleport_Touch;
326 }
327
328 entity Teleport_Find(vector mi, vector ma)
329 {
330         entity e;
331         for(e = world; (e = find(e, classname, "trigger_teleport")); )
332                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
333                         return e;
334         return world;
335 }
336
337 entity teleport_first;
338 .entity teleport_next;
339 void spawnfunc_trigger_teleport (void)
340 {
341         self.angles = '0 0 0';
342
343         EXACTTRIGGER_INIT;
344
345         self.active = ACTIVE_ACTIVE;
346
347         self.use = trigger_teleport_use;
348
349         // this must be called to spawn the teleport waypoints for bots
350         InitializeEntity(self, teleport_findtarget, INITPRIO_FINDTARGET);
351
352         if (self.target == "")
353         {
354                 objerror ("Teleporter with no target");
355                 return;
356         }
357
358         self.teleport_next = teleport_first;
359         teleport_first = self;
360 }
361
362 void WarpZone_PostTeleportPlayer_Callback(entity pl)
363 {
364         makevectors(pl.angles);
365         Reset_ArcBeam(pl, v_forward);
366         UpdateCSQCProjectileAfterTeleport(pl);
367         {
368                 entity oldself = self;
369                 self = pl;
370                 anticheat_fixangle();
371                 self = oldself;
372         }
373         // "disown" projectiles after teleport
374         if(pl.owner)
375         if(pl.owner == pl.realowner)
376         {
377                 if(!(pl.flags & FL_PROJECTILE))
378                         print("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
379                 pl.owner = world;
380         }
381         if(IS_PLAYER(pl))
382         {
383                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
384                 pl.oldvelocity = pl.velocity;
385                 // reset teleport time tracking too (or multijump can cause insane speeds)
386                 pl.lastteleporttime = time;
387         }
388 }