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"
17 #define PORTALS_ARE_NOT_SOLID
19 const vector SAFENUDGE = '1 1 1';
20 const vector SAFERNUDGE = '8 8 8';
22 .vector portal_transform;
23 .vector portal_safe_origin;
24 .float portal_wants_to_vanish;
25 .float portal_activatetime;
26 .float savemodelindex;
28 float PlayerEdgeDistance(entity p, vector v)
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;
39 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
41 vector old_forward, old_up;
42 vector old_yawforward;
43 vector new_forward, new_up;
44 vector new_yawforward;
49 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
50 ang = AnglesTransform_ApplyToVAngles(transform, ang);
53 // PLAYERS use different math
54 #ifndef POSITIVE_PITCH_IS_DOWN
58 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
60 fixedmakevectors(ang);
61 old_forward = v_forward;
63 fixedmakevectors(ang.y * '0 1 0');
64 old_yawforward = v_forward;
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);
71 // but now find a new sense of direction
73 // assume new_forward points straight up.
76 // new_up could now point forward OR backward... which direction to choose?
78 if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
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)
85 ang.y = vectoyaw(new_up); // this vector is the yaw we want
86 //print("UP/DOWN path: ", vtos(ang), "\n");
90 // good angles; here, "forward" suffices
91 ang = fixedvectoangles(new_forward);
92 //print("GOOD path: ", vtos(ang), "\n");
95 #ifndef POSITIVE_PITCH_IS_DOWN
102 .vector right_vector;
103 float Portal_TeleportPlayer(entity teleporter, entity player)
105 vector from, to, safe, step, transform, ang, newvel;
106 float planeshift, s, t;
108 if (!teleporter.enemy)
110 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
114 from = teleporter.origin;
115 transform = teleporter.portal_transform;
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
123 fixedmakevectors(teleporter.enemy.mangle);
125 // first shift it ON the plane if needed
126 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
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));
133 to += v_forward * planeshift;
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
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);
149 LOG_INFO("'safe' teleport location is not safe!\n");
150 // FAIL TODO why does this happen?
153 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
154 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
157 LOG_INFO("trace_endpos in solid, this can't be!\n");
158 // FAIL TODO why does this happen? (reported by MrBougo)
161 to = trace_endpos + normalize(safe - trace_endpos) * 0;
162 //print(vtos(to), "\n");
164 // ang_x stuff works around weird quake angles
165 if(IS_PLAYER(player))
166 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
168 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
170 // factor -1 allows chaining portals, but may be weird
171 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
173 MUTATOR_CALLHOOK(PortalTeleport, player);
175 if (!teleporter.enemy)
177 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
182 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
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);
190 if (!teleporter.enemy)
192 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
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;
205 float Portal_FindSafeOrigin(entity portal)
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))
216 LOG_INFO("NO SAFE ORIGIN\n");
220 portal.portal_safe_origin = portal.origin;
221 setorigin(portal, o);
225 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
227 float dist, distpersec, delta;
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?
236 #ifdef PORTALS_ARE_NOT_SOLID
237 distpersec = evel * pnorm;
238 if(distpersec >= 0) // going away from the portal?
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;
254 #ifdef PORTALS_ARE_NOT_SOLID
255 // portal is being removed?
256 if(self.solid != SOLID_TRIGGER)
257 return; // possibly engine bug
260 return; // handled by think
263 if(other.classname == "item_flag_team")
264 return; // never portal these
266 if(other.classname == "grapplinghook")
267 return; // handled by think
269 if(!autocvar_g_vehicles_teleportable)
270 if(other.vehicle_flags & VHF_ISVEHICLE)
271 return; // no teleporting vehicles?
274 error("Portal_Touch called for a broken portal\n");
276 #ifdef PORTALS_ARE_NOT_SOLID
277 if(trace_fraction < 1)
278 return; // only handle TouchAreaGrid ones (only these can teleport)
280 if(trace_fraction >= 1)
281 return; // only handle impacts
284 if(other.classname == "porto")
286 if(other.portal_id == self.portal_id)
289 if(time < self.portal_activatetime)
290 if(other == self.aiment)
292 self.portal_activatetime = time + 0.1;
295 if(other != self.aiment)
297 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
298 return; // cannot go through someone else's portal
299 if(other.aiment != self.aiment)
300 if(IS_PLAYER(other.aiment))
301 if(IS_INDEPENDENT_PLAYER(other.aiment) || IS_INDEPENDENT_PLAYER(self.aiment))
302 return; // cannot go through someone else's portal
303 fixedmakevectors(self.mangle);
304 g = frametime * '0 0 -1' * autocvar_sv_gravity;
305 if(!Portal_WillHitPlane(other.origin, other.mins, other.maxs, other.velocity + g, self.origin, v_forward, self.maxs.x))
309 if(other.mins_x < PL_MIN.x || other.mins_y < PL_MIN.y || other.mins_z < PL_MIN.z
310 || other.maxs_x > PL_MAX.x || other.maxs_y > PL_MAX.y || other.maxs_z > PL_MAX.z)
312 // can't teleport this
317 if(Portal_TeleportPlayer(self, other))
318 if(other.classname == "porto")
319 if(other.effects & EF_RED)
320 other.effects += EF_BLUE - EF_RED;
324 void Portal_MakeBrokenPortal(entity portal)
327 portal.solid = SOLID_NOT;
328 portal.touch = func_null;
329 portal.think = func_null;
331 portal.nextthink = 0;
332 portal.takedamage = DAMAGE_NO;
335 void Portal_MakeWaitingPortal(entity portal)
338 portal.solid = SOLID_NOT;
339 portal.touch = func_null;
340 portal.think = func_null;
341 portal.effects = EF_ADDITIVE;
342 portal.nextthink = 0;
343 portal.takedamage = DAMAGE_YES;
346 void Portal_MakeInPortal(entity portal)
349 portal.solid = SOLID_NOT; // this is done when connecting them!
350 portal.touch = Portal_Touch;
351 portal.think = Portal_Think;
352 portal.effects = EF_RED;
353 portal.nextthink = time;
354 portal.takedamage = DAMAGE_NO;
357 void Portal_MakeOutPortal(entity portal)
360 portal.solid = SOLID_NOT;
361 portal.touch = func_null;
362 portal.think = func_null;
363 portal.effects = EF_STARDUST | EF_BLUE;
364 portal.nextthink = 0;
365 portal.takedamage = DAMAGE_YES;
368 void Portal_Disconnect(entity teleporter, entity destination)
370 teleporter.enemy = world;
371 destination.enemy = world;
372 Portal_MakeBrokenPortal(teleporter);
373 Portal_MakeBrokenPortal(destination);
376 void Portal_Connect(entity teleporter, entity destination)
378 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
380 teleporter.enemy = destination;
381 destination.enemy = teleporter;
382 Portal_MakeInPortal(teleporter);
383 Portal_MakeOutPortal(destination);
384 teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
385 destination.fade_time = teleporter.fade_time;
386 teleporter.portal_wants_to_vanish = 0;
387 destination.portal_wants_to_vanish = 0;
388 teleporter.teleport_time = time;
389 #ifdef PORTALS_ARE_NOT_SOLID
390 teleporter.solid = SOLID_TRIGGER;
392 teleporter.solid = SOLID_BSP;
396 void Portal_Remove(entity portal, float killed)
403 Portal_Disconnect(portal, e);
404 Portal_Remove(e, killed);
407 if(portal == portal.aiment.portal_in)
408 portal.aiment.portal_in = world;
409 if(portal == portal.aiment.portal_out)
410 portal.aiment.portal_out = world;
411 //portal.aiment = world;
413 // makes the portal vanish
416 fixedmakevectors(portal.mangle);
417 sound(portal, CH_SHOTS, "porto/explode.wav", VOL_BASE, ATTEN_NORM);
418 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
423 Portal_MakeBrokenPortal(portal);
424 sound(portal, CH_SHOTS, "porto/expire.wav", VOL_BASE, ATTEN_NORM);
425 SUB_SetFade(portal, time, 0.5);
429 void Portal_Damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
431 if(deathtype == DEATH_TELEFRAG)
433 if(attacker != self.aiment)
434 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.aiment))
436 self.health -= damage;
438 Portal_Remove(self, 1);
441 void Portal_Think_TryTeleportPlayer(entity e, vector g)
443 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, self.origin, v_forward, self.maxs.x))
446 // if e would hit the portal in a frame...
447 // already teleport him
448 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
449 if(trace_ent == self)
450 Portal_TeleportPlayer(self, e);
458 #ifdef PORTALS_ARE_NOT_SOLID
459 // portal is being removed?
460 if(self.solid != SOLID_TRIGGER)
461 return; // possibly engine bug
464 error("Portal_Think called for a broken portal\n");
467 self.solid = SOLID_BBOX;
470 g = frametime * '0 0 -1' * autocvar_sv_gravity;
472 fixedmakevectors(self.mangle);
477 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
478 continue; // cannot go through someone else's portal
480 if(e != o || time >= self.portal_activatetime)
481 Portal_Think_TryTeleportPlayer(e, g);
484 Portal_Think_TryTeleportPlayer(e.hook, g);
486 self.solid = SOLID_TRIGGER;
490 self.nextthink = time;
492 if(time > self.fade_time)
493 Portal_Remove(self, 0);
496 float Portal_Customize()
500 if(other == self.aiment)
502 self.modelindex = self.savemodelindex;
504 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
510 self.modelindex = self.savemodelindex;
516 // when creating in-portal:
518 // clear existing in-portal
521 // when creating out-portal:
523 // clear existing out-portal
526 // disconnect portals
527 // clear both portals
528 // after timeout of in-portal:
529 // disconnect portals
530 // clear both portals
531 // TODO: ensure only one portal shot at once
532 float Portal_SetInPortal(entity own, entity portal)
537 Portal_Disconnect(own.portal_in, own.portal_out);
538 Portal_Remove(own.portal_in, 0);
540 own.portal_in = portal;
543 own.portal_out.portal_id = portal.portal_id;
544 Portal_Connect(own.portal_in, own.portal_out);
548 float Portal_SetOutPortal(entity own, entity portal)
553 Portal_Disconnect(own.portal_in, own.portal_out);
554 Portal_Remove(own.portal_out, 0);
556 own.portal_out = portal;
559 own.portal_in.portal_id = portal.portal_id;
560 Portal_Connect(own.portal_in, own.portal_out);
564 void Portal_ClearAll_PortalsOnly(entity own)
567 Portal_Remove(own.portal_in, 0);
569 Portal_Remove(own.portal_out, 0);
571 void Portal_ClearAll(entity own)
573 Portal_ClearAll_PortalsOnly(own);
576 void Portal_RemoveLater_Think()
578 Portal_Remove(self, self.cnt);
580 void Portal_RemoveLater(entity portal, float kill)
582 Portal_MakeBrokenPortal(portal);
584 portal.think = Portal_RemoveLater_Think;
585 portal.nextthink = time;
587 void Portal_ClearAllLater_PortalsOnly(entity own)
590 Portal_RemoveLater(own.portal_in, 0);
592 Portal_RemoveLater(own.portal_out, 0);
594 void Portal_ClearAllLater(entity own)
596 Portal_ClearAllLater_PortalsOnly(own);
599 void Portal_ClearWithID(entity own, float id)
602 if(own.portal_in.portal_id == id)
605 Portal_Disconnect(own.portal_in, own.portal_out);
606 Portal_Remove(own.portal_in, 0);
609 if(own.portal_out.portal_id == id)
612 Portal_Disconnect(own.portal_in, own.portal_out);
613 Portal_Remove(own.portal_out, 0);
617 entity Portal_Spawn(entity own, vector org, vector ang)
621 fixedmakevectors(ang);
622 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
626 portal.classname = "portal";
628 setorigin(portal, org);
631 portal.angles_x = -portal.angles.x; // is a bmodel
632 portal.think = Portal_Think;
633 portal.nextthink = 0;
634 portal.portal_activatetime = time + 0.1;
635 portal.takedamage = DAMAGE_AIM;
636 portal.event_damage = Portal_Damage;
637 portal.fade_time = time + autocvar_g_balance_portal_lifetime;
638 portal.health = autocvar_g_balance_portal_health;
639 setmodel(portal, "models/portal.md3");
640 portal.savemodelindex = portal.modelindex;
641 portal.customizeentityforclient = Portal_Customize;
643 if(!Portal_FindSafeOrigin(portal))
649 setsize(portal, '-48 -48 -48', '48 48 48');
650 Portal_MakeWaitingPortal(portal);
655 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
662 ang = fixedvectoangles2(trace_plane_normal, dir);
663 fixedmakevectors(ang);
665 portal = Portal_Spawn(own, org, ang);
669 portal.portal_id = portal_id_val;
670 Portal_SetInPortal(own, portal);
675 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
682 ang = fixedvectoangles2(trace_plane_normal, dir);
683 fixedmakevectors(ang);
685 portal = Portal_Spawn(own, org, ang);
689 portal.portal_id = portal_id_val;
690 Portal_SetOutPortal(own, portal);