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