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