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