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