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