]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_teleporters.qc
a trigger_warpzone entity (a silent everything-teleporting teleporter). IT HAS NO...
[divverent/nexuiz.git] / data / 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                 if(player.isbot)
136                         player.lastteleporttime = time;
137
138                 // stop player name display
139                 {
140                         oldself = self;
141                         self = player;
142                         ClearSelectedPlayer();
143                         self = oldself;
144                 }
145         }
146 }
147
148 void Teleport_Touch (void)
149 {
150         entity oldself, e;
151         vector o;
152         float p;
153
154         if (other.health < 1)
155                 return;
156         if not(other.flags & FL_CLIENT) // FIXME: Make missiles firable through the teleport too
157                 return;
158
159         if(self.team)
160                 if((self.spawnflags & 4 == 0) == (self.team != other.team))
161                         return;
162
163         EXACTTRIGGER_TOUCH;
164
165         makevectors(self.enemy.mangle);
166
167         if(other.classname == "player")
168                 RemoveGrapplingHook(other);
169         
170         if(self.enemy)
171         {
172                 e = self.enemy;
173         }
174         else
175         {
176                 RandomSelection_Init();
177                 for(e = world; (e = find(e, targetname, self.target)); )
178                 {
179                         p = 1;
180                         if(cvar("g_telefrags_avoid"))
181                         {
182                                 o = e.origin + '0 0 1' * (1 - other.mins_z - 24);
183                                 if(check_tdeath(other, o, '0 0 0', '0 0 0'))
184                                         p = 0;
185                         }
186                         if(e.cnt)
187                                 RandomSelection_Add(e, 0, string_null, e.cnt, p);
188                         else
189                                 RandomSelection_Add(e, 0, string_null, 1, p);
190                 }
191                 e = RandomSelection_chosen_ent;
192         }
193
194         if(!e)
195         {
196                 sprint(other, "Teleport destination vanished. Sorry... please complain to the mapper.\n");
197         }
198
199         if(e.speed)
200                 if(vlen(other.velocity) > e.speed)
201                         other.velocity = normalize(other.velocity) * max(0, e.speed);
202         if(cvar("g_teleport_maxspeed"))
203                 if(vlen(other.velocity) > cvar("g_teleport_maxspeed"))
204                         other.velocity = normalize(other.velocity) * max(0, cvar("g_teleport_maxspeed"));
205
206         o = e.origin + '0 0 1' * (1 - other.mins_z - 24);
207         TeleportPlayer(self, other, o, e.mangle, v_forward * vlen(other.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
208
209         if(e.target)
210         {
211                 oldself = self;
212                 activator = other;
213                 self = e;
214                 SUB_UseTargets();
215                 self = oldself;
216         }
217 }
218
219 void spawnfunc_info_teleport_destination (void)
220 {
221         self.classname = "info_teleport_destination";
222
223         self.mangle = self.angles;
224         self.angles = '0 0 0';
225
226         //setorigin (self, self.origin + '0 0 27');     // To fix a mappers' habit as old as Quake
227         setorigin (self, self.origin);
228
229         IFTARGETED
230         {
231         }
232         else
233                 objerror ("^3Teleport destination without a targetname");
234 }
235
236 void spawnfunc_misc_teleporter_dest (void)
237 {
238         spawnfunc_info_teleport_destination();
239 }
240
241 void spawnfunc_target_teleporter (void)
242 {
243         spawnfunc_info_teleport_destination();
244 }
245
246 void teleport_findtarget (void)
247 {
248         entity e;
249         float n;
250
251         n = 0;
252         for(e = world; (e = find(e, targetname, self.target)); )
253         {
254                 ++n;
255                 if(e.movetype == MOVETYPE_NONE)
256                         waypoint_spawnforteleporter(self, e.origin, 0);
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
261         if(n == 0)
262         {
263                 // no dest!
264                 objerror ("Teleporter with nonexistant target");
265                 return;
266         }
267         else if(n == 1)
268         {
269                 // exactly one dest - bots love that
270                 self.enemy = find(e, targetname, self.target);
271                 self.dest = self.enemy.origin;
272         }
273         else
274         {
275                 // have to use random selection every single time
276                 self.enemy = world;
277         }
278
279         // now enable touch
280         self.touch = Teleport_Touch;
281 }
282
283 void spawnfunc_trigger_teleport (void)
284 {
285         self.angles = '0 0 0';
286
287         EXACTTRIGGER_INIT;
288
289         self.use = trigger_teleport_use;
290
291         // this must be called to spawn the teleport waypoints for bots
292         InitializeEntity(self, teleport_findtarget, INITPRIO_FINDTARGET);
293
294         if (!self.target)
295         {
296                 objerror ("Teleporter with no target");
297                 return;
298         }
299 }
300
301
302 // the transform
303 .vector warpzone_origin, warpzone_angles;
304 .vector warpzone_forward;
305 .vector warpzone_transform;
306
307 void warpzone_updatetransform()
308 {
309         // 1. update this, and the enemy, warp zone
310         self.warpzone_origin = self.aiment.origin;
311         self.warpzone_angles = self.aiment.angles;
312         self.enemy.warpzone_origin = self.enemy.aiment.origin;
313         self.enemy.warpzone_angles = self.enemy.aiment.angles;
314
315         // 2. combine the angle transforms
316         //    current forward must be turned into previous backward
317         self.warpzone_transform = AnglesTransform_Divide(AnglesTransform_TurnDirection(self.enemy.warpzone_angles), self.warpzone_angles);
318
319         // 3. store off a saved forward vector for plane hit decisions
320         fixedmakevectors(self.warpzone_angles);
321         self.warpzone_forward = v_forward;
322 }
323
324 float warpzone_teleport(entity player)
325 {
326         vector o0, a0, v0, o1, a1, v1;
327
328         o0 = player.origin;
329         v0 = player.velocity;
330         a0 = player.angles;
331
332         if((o0 - self.warpzone_origin) * self.warpzone_forward >= 0) // wrong side of the portal
333                 return 2;
334                 // no failure, we simply don't want to teleport yet; TODO in
335                 // this situation we may want to create a temporary clone
336                 // entity of the player to fix graphics glitch
337
338         o1 = AnglesTransform_Apply(self.warpzone_transform, o0 - self.warpzone_origin) + self.enemy.warpzone_origin;
339         v1 = AnglesTransform_Apply(self.warpzone_transform, v0);
340         if(player.classname == "player")
341                 a1 = Portal_ApplyTransformToPlayerAngle(self.warpzone_transform, player.v_angle);
342         else
343                 a1 = AnglesTransform_Multiply(self.warpzone_transform, a0);
344
345         // put him inside solid
346         tracebox(o1, player.mins, player.maxs, o1, MOVE_NOMONSTERS, player);
347         if(trace_startsolid)
348         {
349                 print("in solid\n");
350                 return 0; // TODO nudge out of solid here!
351         }
352
353         if((o1 - self.enemy.warpzone_origin) * self.enemy.warpzone_forward <= 0) // wrong side of the portal post-teleport
354         {
355                 print("inconsistent warp zones or evil roundoff error\n");
356                 return 0;
357         }
358
359         //print(sprintf("warpzone: %f %f %f -> %f %f %f\n", o0_x, o0_y, o0_z, o1_x, o1_y, o1_z));
360
361         //o1 = trace_endpos;
362         TeleportPlayer(self, other, o1, a1, v1, '0 0 0', '0 0 0', TELEPORT_FLAGS_WARPZONE);
363
364         return 1;
365 }
366
367 void warpzone_touch (void)
368 {
369         entity oldself, e;
370
371         if (other.health < 1)
372                 return;
373
374         if(self.team)
375                 if((self.spawnflags & 4 == 0) == (self.team != other.team))
376                         return;
377
378         EXACTTRIGGER_TOUCH;
379
380         e = self.enemy;
381         if(warpzone_teleport(other))
382         {
383         }
384         else
385         {
386                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
387         }
388
389         if(self.aiment.target)
390         {
391                 oldself = self;
392                 activator = other;
393                 self = self.aiment;
394                 SUB_UseTargets();
395                 self = oldself;
396         }
397 }
398 void warpzone_findtarget (void)
399 {
400         if(self.target == "")
401         {
402                 objerror("Warp zone with no target");
403                 return;
404         }
405         self.enemy = find(world, targetname, self.target);
406         if(self.enemy == world)
407         {
408                 objerror("Warp zone with nonexisting target");
409                 return;
410         }
411
412         if(self.killtarget == "")
413         {
414                 objerror("Warp zone with no killtarget");
415                 return;
416         }
417         self.aiment = find(world, targetname, self.killtarget);
418         if(self.aiment == world)
419         {
420                 objerror("Warp zone with nonexisting killtarget");
421                 return;
422         }
423
424         // now enable touch
425         self.touch = warpzone_touch;
426 }
427
428 void spawnfunc_trigger_warpzone(void)
429 {
430         // warp zone entities must have:
431         // "killtarget" pointing to a target_position with a direction arrow
432         //              that points AWAY from the warp zone, and that is inside
433         //              the warp zone trigger
434         // "target"     pointing to an identical warp zone at another place in
435         //              the map, with another killtarget to designate its
436         //              orientation
437         
438         EXACTTRIGGER_INIT;
439
440         self.use = trigger_teleport_use; // set team
441         InitializeEntity(self, warpzone_findtarget, INITPRIO_FINDTARGET);
442         InitializeEntity(self, warpzone_updatetransform, INITPRIO_LAST);
443 }