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