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