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