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