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