]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/portals.qc
7acbc659f06fa057ca62abd8706bdcf4fee6b7a1
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / portals.qc
1 #include "portals.qh"
2 #include "_all.qh"
3
4 #include "g_hook.qh"
5 #include "mutators/mutators_include.qh"
6 #include "../common/constants.qh"
7 #include "../common/deathtypes.qh"
8 #include "../common/notifications.qh"
9 #include "../common/triggers/teleporters.qh"
10 #include "../common/triggers/subs.qh"
11 #include "../common/util.qh"
12 #include "../common/weapons/all.qh"
13 #include "../csqcmodellib/sv_model.qh"
14 #include "../warpzonelib/anglestransform.qh"
15 #include "../warpzonelib/util_server.qh"
16
17 #define PORTALS_ARE_NOT_SOLID
18
19 const vector SAFENUDGE = '1 1 1';
20 const vector SAFERNUDGE = '8 8 8';
21
22 .vector portal_transform;
23 .vector portal_safe_origin;
24 .float portal_wants_to_vanish;
25 .float portal_activatetime;
26 .float savemodelindex;
27
28 float PlayerEdgeDistance(entity p, vector v)
29 {
30         vector vbest;
31
32         if(v.x < 0) vbest.x = p.mins.x; else vbest.x = p.maxs.x;
33         if(v.y < 0) vbest.y = p.mins.y; else vbest.y = p.maxs.y;
34         if(v.z < 0) vbest.z = p.mins.z; else vbest.z = p.maxs.z;
35
36         return vbest * v;
37 }
38
39 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
40 {
41         vector old_forward, old_up;
42         vector old_yawforward;
43         vector new_forward, new_up;
44         vector new_yawforward;
45
46         vector ang;
47         ang = vangle;
48         /*
49            ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
50            ang = AnglesTransform_ApplyToVAngles(transform, ang);
51          */
52
53         // PLAYERS use different math
54 #ifndef POSITIVE_PITCH_IS_DOWN
55         ang.x = -ang.x;
56 #endif
57
58         //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
59
60         fixedmakevectors(ang);
61         old_forward = v_forward;
62         old_up = v_up;
63         fixedmakevectors(ang.y * '0 1 0');
64         old_yawforward = v_forward;
65
66         // their aiming directions are portalled...
67         new_forward = AnglesTransform_Apply(transform, old_forward);
68         new_up = AnglesTransform_Apply(transform, old_up);
69         new_yawforward = AnglesTransform_Apply(transform, old_yawforward);
70
71         // but now find a new sense of direction
72         // this is NOT easy!
73         // assume new_forward points straight up.
74         // What is our yaw?
75         //
76         // new_up could now point forward OR backward... which direction to choose?
77
78         if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
79         {
80                 // new_yawforward and new_yawup define the new aiming half-circle
81                 // we "just" need to find out whether new_up or -new_up is in that half circle
82                 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
83                 if(new_up * new_yawforward < 0)
84                         new_up = -1 * new_up;
85                 ang.y = vectoyaw(new_up); // this vector is the yaw we want
86                 //print("UP/DOWN path: ", vtos(ang), "\n");
87         }
88         else
89         {
90                 // good angles; here, "forward" suffices
91                 ang = fixedvectoangles(new_forward);
92                 //print("GOOD path: ", vtos(ang), "\n");
93         }
94
95 #ifndef POSITIVE_PITCH_IS_DOWN
96         ang.x = -ang.x;
97 #endif
98         ang.z = vangle.z;
99         return ang;
100 }
101
102 .vector right_vector;
103 float Portal_TeleportPlayer(entity teleporter, entity player)
104 {
105         vector from, to, safe, step, transform, ang, newvel;
106         float planeshift, s, t;
107
108         if (!teleporter.enemy)
109         {
110                 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
111                 return 0;
112         }
113
114         from = teleporter.origin;
115         transform = teleporter.portal_transform;
116
117         to = teleporter.enemy.origin;
118         to = to + AnglesTransform_Apply(teleporter.portal_transform, player.origin - from);
119         newvel = AnglesTransform_Apply(transform, player.velocity);
120         // this now is INSIDE the plane... can't use that
121
122         // shift it out
123         fixedmakevectors(teleporter.enemy.mangle);
124
125         // first shift it ON the plane if needed
126         planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
127         /*
128         if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
129                 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
130                 to += newvel * (planeshift / (newvel * v_forward));
131         else
132         */
133                 to += v_forward * planeshift;
134
135         s = (to - teleporter.enemy.origin) * v_right;
136         t = (to - teleporter.enemy.origin) * v_up;
137         s = bound(-48, s, 48);
138         t = bound(-48, t, 48);
139         to = teleporter.enemy.origin
140            + ((to - teleporter.enemy.origin) * v_forward) * v_forward
141            +     s                                        * v_right
142            +     t                                        * v_up;
143
144         safe = teleporter.enemy.portal_safe_origin; // a valid player origin
145         step = to + ((safe - to) * v_forward) * v_forward;
146         tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
147         if(trace_startsolid)
148         {
149                 print("'safe' teleport location is not safe!\n");
150                 // FAIL TODO why does this happen?
151                 return 0;
152         }
153         safe = trace_endpos + normalize(safe - trace_endpos) * 0;
154         tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
155         if(trace_startsolid)
156         {
157                 print("trace_endpos in solid, this can't be!\n");
158                 // FAIL TODO why does this happen? (reported by MrBougo)
159                 return 0;
160         }
161         to = trace_endpos + normalize(safe - trace_endpos) * 0;
162         //print(vtos(to), "\n");
163
164         // ang_x stuff works around weird quake angles
165         if(IS_PLAYER(player))
166                 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
167         else
168                 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
169
170         // factor -1 allows chaining portals, but may be weird
171         player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
172
173         MUTATOR_CALLHOOK(PortalTeleport, player);
174
175         if (!teleporter.enemy)
176         {
177                 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
178                 return 0;
179         }
180
181         tdeath_hit = 0;
182         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
183         if(tdeath_hit)
184         {
185                 // telefrag within 1 second of portal creation = amazing
186                 if(time < teleporter.teleport_time + 1)
187                         Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
188         }
189
190         if (!teleporter.enemy)
191         {
192                 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
193                 return 0;
194         }
195
196         // reset fade counter
197         teleporter.portal_wants_to_vanish = 0;
198         teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
199         teleporter.health = autocvar_g_balance_portal_health;
200         teleporter.enemy.health = autocvar_g_balance_portal_health;
201
202         return 1;
203 }
204
205 float Portal_FindSafeOrigin(entity portal)
206 {
207         vector o;
208         o = portal.origin;
209         portal.mins = PL_MIN - SAFERNUDGE;
210         portal.maxs = PL_MAX + SAFERNUDGE;
211         fixedmakevectors(portal.mangle);
212         portal.origin += 16 * v_forward;
213         if(!move_out_of_solid(portal))
214         {
215 #ifdef DEBUG
216                 print("NO SAFE ORIGIN\n");
217 #endif
218                 return 0;
219         }
220         portal.portal_safe_origin = portal.origin;
221         setorigin(portal, o);
222         return 1;
223 }
224
225 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
226 {
227         float dist, distpersec, delta;
228         vector v;
229
230         dist = (eorg - porg) * pnorm;
231         dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
232         dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
233         dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
234         if(dist < -1) // other side?
235                 return 0;
236 #ifdef PORTALS_ARE_NOT_SOLID
237         distpersec = evel * pnorm;
238         if(distpersec >= 0) // going away from the portal?
239                 return 0;
240         // we don't need this check with solid portals, them being SOLID_BSP should suffice
241         delta = dist / distpersec;
242         v = eorg - evel * delta - porg;
243         v = v - pnorm * (pnorm * v);
244         return vlen(v) < psize;
245 #else
246         return 1;
247 #endif
248 }
249
250 void Portal_Touch()
251 {
252         vector g;
253
254 #ifdef PORTALS_ARE_NOT_SOLID
255         // portal is being removed?
256         if(self.solid != SOLID_TRIGGER)
257                 return; // possibly engine bug
258
259         if(IS_PLAYER(other))
260                 return; // handled by think
261 #endif
262
263         if(other.classname == "item_flag_team")
264                 return; // never portal these
265
266         if(other.classname == "grapplinghook")
267                 return; // handled by think
268
269         if(!self.enemy)
270                 error("Portal_Touch called for a broken portal\n");
271
272 #ifdef PORTALS_ARE_NOT_SOLID
273         if(trace_fraction < 1)
274                 return; // only handle TouchAreaGrid ones (only these can teleport)
275 #else
276         if(trace_fraction >= 1)
277                 return; // only handle impacts
278 #endif
279
280         if(other.classname == "porto")
281         {
282                 if(other.portal_id == self.portal_id)
283                         return;
284         }
285         if(time < self.portal_activatetime)
286                 if(other == self.aiment)
287                 {
288                         self.portal_activatetime = time + 0.1;
289                         return;
290                 }
291         if(other != self.aiment)
292                 if(IS_PLAYER(other))
293                         if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
294                                 return; // cannot go through someone else's portal
295         if(other.aiment != self.aiment)
296                 if(IS_PLAYER(other.aiment))
297                         if(IS_INDEPENDENT_PLAYER(other.aiment) || IS_INDEPENDENT_PLAYER(self.aiment))
298                                 return; // cannot go through someone else's portal
299         fixedmakevectors(self.mangle);
300         g = frametime * '0 0 -1' * autocvar_sv_gravity;
301         if(!Portal_WillHitPlane(other.origin, other.mins, other.maxs, other.velocity + g, self.origin, v_forward, self.maxs.x))
302                 return;
303
304         /*
305         if(other.mins_x < PL_MIN.x || other.mins_y < PL_MIN.y || other.mins_z < PL_MIN.z
306         || other.maxs_x > PL_MAX.x || other.maxs_y > PL_MAX.y || other.maxs_z > PL_MAX.z)
307         {
308                 // can't teleport this
309                 return;
310         }
311         */
312
313         if(Portal_TeleportPlayer(self, other))
314                 if(other.classname == "porto")
315                         if(other.effects & EF_RED)
316                                 other.effects += EF_BLUE - EF_RED;
317 }
318
319 void Portal_Think();
320 void Portal_MakeBrokenPortal(entity portal)
321 {
322         portal.skin = 2;
323         portal.solid = SOLID_NOT;
324         portal.touch = func_null;
325         portal.think = func_null;
326         portal.effects = 0;
327         portal.nextthink = 0;
328         portal.takedamage = DAMAGE_NO;
329 }
330
331 void Portal_MakeWaitingPortal(entity portal)
332 {
333         portal.skin = 2;
334         portal.solid = SOLID_NOT;
335         portal.touch = func_null;
336         portal.think = func_null;
337         portal.effects = EF_ADDITIVE;
338         portal.nextthink = 0;
339         portal.takedamage = DAMAGE_YES;
340 }
341
342 void Portal_MakeInPortal(entity portal)
343 {
344         portal.skin = 0;
345         portal.solid = SOLID_NOT; // this is done when connecting them!
346         portal.touch = Portal_Touch;
347         portal.think = Portal_Think;
348         portal.effects = EF_RED;
349         portal.nextthink = time;
350         portal.takedamage = DAMAGE_NO;
351 }
352
353 void Portal_MakeOutPortal(entity portal)
354 {
355         portal.skin = 1;
356         portal.solid = SOLID_NOT;
357         portal.touch = func_null;
358         portal.think = func_null;
359         portal.effects = EF_STARDUST | EF_BLUE;
360         portal.nextthink = 0;
361         portal.takedamage = DAMAGE_YES;
362 }
363
364 void Portal_Disconnect(entity teleporter, entity destination)
365 {
366         teleporter.enemy = world;
367         destination.enemy = world;
368         Portal_MakeBrokenPortal(teleporter);
369         Portal_MakeBrokenPortal(destination);
370 }
371
372 void Portal_Connect(entity teleporter, entity destination)
373 {
374         teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
375
376         teleporter.enemy = destination;
377         destination.enemy = teleporter;
378         Portal_MakeInPortal(teleporter);
379         Portal_MakeOutPortal(destination);
380         teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
381         destination.fade_time = teleporter.fade_time;
382         teleporter.portal_wants_to_vanish = 0;
383         destination.portal_wants_to_vanish = 0;
384         teleporter.teleport_time = time;
385 #ifdef PORTALS_ARE_NOT_SOLID
386         teleporter.solid = SOLID_TRIGGER;
387 #else
388         teleporter.solid = SOLID_BSP;
389 #endif
390 }
391
392 void Portal_Remove(entity portal, float killed)
393 {
394         entity e;
395         e = portal.enemy;
396
397         if(e)
398         {
399                 Portal_Disconnect(portal, e);
400                 Portal_Remove(e, killed);
401         }
402
403         if(portal == portal.aiment.portal_in)
404                 portal.aiment.portal_in = world;
405         if(portal == portal.aiment.portal_out)
406                 portal.aiment.portal_out = world;
407         //portal.aiment = world;
408
409         // makes the portal vanish
410         if(killed)
411         {
412                 fixedmakevectors(portal.mangle);
413                 sound(portal, CH_SHOTS, "porto/explode.wav", VOL_BASE, ATTEN_NORM);
414                 Send_Effect("rocket_explode", portal.origin + v_forward * 16, v_forward * 1024, 4);
415                 remove(portal);
416         }
417         else
418         {
419                 Portal_MakeBrokenPortal(portal);
420                 sound(portal, CH_SHOTS, "porto/expire.wav", VOL_BASE, ATTEN_NORM);
421                 SUB_SetFade(portal, time, 0.5);
422         }
423 }
424
425 void Portal_Damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
426 {
427         if(deathtype == DEATH_TELEFRAG)
428                 return;
429         if(attacker != self.aiment)
430                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.aiment))
431                         return;
432         self.health -= damage;
433         if(self.health < 0)
434                 Portal_Remove(self, 1);
435 }
436
437 void Portal_Think_TryTeleportPlayer(entity e, vector g)
438 {
439         if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, self.origin, v_forward, self.maxs.x))
440                 return;
441
442         // if e would hit the portal in a frame...
443         // already teleport him
444         tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
445         if(trace_ent == self)
446                 Portal_TeleportPlayer(self, e);
447 }
448
449 void Portal_Think()
450 {
451         entity e, o;
452         vector g;
453
454 #ifdef PORTALS_ARE_NOT_SOLID
455         // portal is being removed?
456         if(self.solid != SOLID_TRIGGER)
457                 return; // possibly engine bug
458
459         if(!self.enemy)
460                 error("Portal_Think called for a broken portal\n");
461
462         o = self.aiment;
463         self.solid = SOLID_BBOX;
464         self.aiment = world;
465
466         g = frametime * '0 0 -1' * autocvar_sv_gravity;
467
468         fixedmakevectors(self.mangle);
469
470         FOR_EACH_PLAYER(e)
471         {
472                 if(e != o)
473                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
474                                 continue; // cannot go through someone else's portal
475
476                 if(e != o || time >= self.portal_activatetime)
477                         Portal_Think_TryTeleportPlayer(e, g);
478
479                 if(e.hook)
480                         Portal_Think_TryTeleportPlayer(e.hook, g);
481         }
482         self.solid = SOLID_TRIGGER;
483         self.aiment = o;
484 #endif
485
486         self.nextthink = time;
487
488         if(time > self.fade_time)
489                 Portal_Remove(self, 0);
490 }
491
492 float Portal_Customize()
493 {
494         if(IS_SPEC(other))
495                 other = other.enemy;
496         if(other == self.aiment)
497         {
498                 self.modelindex = self.savemodelindex;
499         }
500         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
501         {
502                 self.modelindex = 0;
503         }
504         else
505         {
506                 self.modelindex = self.savemodelindex;
507         }
508         return true;
509 }
510
511 // cleanup:
512 //   when creating in-portal:
513 //     disconnect
514 //     clear existing in-portal
515 //     set as in-portal
516 //     connect
517 //   when creating out-portal:
518 //     disconnect
519 //     clear existing out-portal
520 //     set as out-portal
521 //   when player dies:
522 //     disconnect portals
523 //     clear both portals
524 //   after timeout of in-portal:
525 //     disconnect portals
526 //     clear both portals
527 //   TODO: ensure only one portal shot at once
528 float Portal_SetInPortal(entity own, entity portal)
529 {
530         if(own.portal_in)
531         {
532                 if(own.portal_out)
533                         Portal_Disconnect(own.portal_in, own.portal_out);
534                 Portal_Remove(own.portal_in, 0);
535         }
536         own.portal_in = portal;
537         if(own.portal_out)
538         {
539                 own.portal_out.portal_id = portal.portal_id;
540                 Portal_Connect(own.portal_in, own.portal_out);
541         }
542         return 2;
543 }
544 float Portal_SetOutPortal(entity own, entity portal)
545 {
546         if(own.portal_out)
547         {
548                 if(own.portal_in)
549                         Portal_Disconnect(own.portal_in, own.portal_out);
550                 Portal_Remove(own.portal_out, 0);
551         }
552         own.portal_out = portal;
553         if(own.portal_in)
554         {
555                 own.portal_in.portal_id = portal.portal_id;
556                 Portal_Connect(own.portal_in, own.portal_out);
557         }
558         return 1;
559 }
560 void Portal_ClearAll_PortalsOnly(entity own)
561 {
562         if(own.portal_in)
563                 Portal_Remove(own.portal_in, 0);
564         if(own.portal_out)
565                 Portal_Remove(own.portal_out, 0);
566 }
567 void Portal_ClearAll(entity own)
568 {
569         Portal_ClearAll_PortalsOnly(own);
570         W_Porto_Remove(own);
571 }
572 void Portal_RemoveLater_Think()
573 {
574         Portal_Remove(self, self.cnt);
575 }
576 void Portal_RemoveLater(entity portal, float kill)
577 {
578         Portal_MakeBrokenPortal(portal);
579         portal.cnt = kill;
580         portal.think = Portal_RemoveLater_Think;
581         portal.nextthink = time;
582 }
583 void Portal_ClearAllLater_PortalsOnly(entity own)
584 {
585         if(own.portal_in)
586                 Portal_RemoveLater(own.portal_in, 0);
587         if(own.portal_out)
588                 Portal_RemoveLater(own.portal_out, 0);
589 }
590 void Portal_ClearAllLater(entity own)
591 {
592         Portal_ClearAllLater_PortalsOnly(own);
593         W_Porto_Remove(own);
594 }
595 void Portal_ClearWithID(entity own, float id)
596 {
597         if(own.portal_in)
598                 if(own.portal_in.portal_id == id)
599                 {
600                         if(own.portal_out)
601                                 Portal_Disconnect(own.portal_in, own.portal_out);
602                         Portal_Remove(own.portal_in, 0);
603                 }
604         if(own.portal_out)
605                 if(own.portal_out.portal_id == id)
606                 {
607                         if(own.portal_in)
608                                 Portal_Disconnect(own.portal_in, own.portal_out);
609                         Portal_Remove(own.portal_out, 0);
610                 }
611 }
612
613 entity Portal_Spawn(entity own, vector org, vector ang)
614 {
615         entity portal;
616
617         fixedmakevectors(ang);
618         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
619                 return world;
620
621         portal = spawn();
622         portal.classname = "portal";
623         portal.aiment = own;
624         setorigin(portal, org);
625         portal.mangle = ang;
626         portal.angles = ang;
627         portal.angles_x = -portal.angles.x; // is a bmodel
628         portal.think = Portal_Think;
629         portal.nextthink = 0;
630         portal.portal_activatetime = time + 0.1;
631         portal.takedamage = DAMAGE_AIM;
632         portal.event_damage = Portal_Damage;
633         portal.fade_time = time + autocvar_g_balance_portal_lifetime;
634         portal.health = autocvar_g_balance_portal_health;
635         setmodel(portal, "models/portal.md3");
636         portal.savemodelindex = portal.modelindex;
637         portal.customizeentityforclient = Portal_Customize;
638
639         if(!Portal_FindSafeOrigin(portal))
640         {
641                 remove(portal);
642                 return world;
643         }
644
645         setsize(portal, '-48 -48 -48', '48 48 48');
646         Portal_MakeWaitingPortal(portal);
647
648         return portal;
649 }
650
651 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
652 {
653         entity portal;
654         vector ang;
655         vector org;
656
657         org = trace_endpos;
658         ang = fixedvectoangles2(trace_plane_normal, dir);
659         fixedmakevectors(ang);
660
661         portal = Portal_Spawn(own, org, ang);
662         if(!portal)
663                 return 0;
664
665         portal.portal_id = portal_id_val;
666         Portal_SetInPortal(own, portal);
667
668         return 1;
669 }
670
671 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
672 {
673         entity portal;
674         vector ang;
675         vector org;
676
677         org = trace_endpos;
678         ang = fixedvectoangles2(trace_plane_normal, dir);
679         fixedmakevectors(ang);
680
681         portal = Portal_Spawn(own, org, ang);
682         if(!portal)
683                 return 0;
684
685         portal.portal_id = portal_id_val;
686         Portal_SetOutPortal(own, portal);
687
688         return 1;
689 }