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