3 #include <common/effects/all.qh>
5 #include "mutators/_mod.qh"
6 #include "../common/constants.qh"
7 #include "../common/deathtypes/all.qh"
8 #include "../common/notifications/all.qh"
9 #include "../common/mapobjects/teleporters.qh"
10 #include "../common/mapobjects/subs.qh"
11 #include "../common/util.qh"
12 #include <common/weapons/_all.qh>
13 #include "../lib/csqcmodel/sv_model.qh"
14 #include "../lib/warpzone/anglestransform.qh"
15 #include "../lib/warpzone/util_server.qh"
16 #include "../lib/warpzone/common.qh"
17 #include "../common/vehicles/vehicle.qh"
18 #include "../common/vehicles/sv_vehicles.qh"
19 #include <common/weapons/weapon/porto.qh>
20 #include <server/player.qh>
21 #include <server/g_damage.qh>
23 #define PORTALS_ARE_NOT_SOLID
25 const vector SAFENUDGE = '1 1 1';
26 const vector SAFERNUDGE = '8 8 8';
28 .vector portal_transform;
29 .vector portal_safe_origin;
30 .float portal_wants_to_vanish;
31 .float portal_activatetime;
32 .float savemodelindex;
34 float PlayerEdgeDistance(entity p, vector v)
37 ((v.x < 0) ? p.mins.x : p.maxs.x),
38 ((v.y < 0) ? p.mins.y : p.maxs.y),
39 ((v.z < 0) ? p.mins.z : p.maxs.z));
44 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
46 vector old_forward, old_up;
47 vector old_yawforward;
48 vector new_forward, new_up;
49 vector new_yawforward;
54 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
55 ang = AnglesTransform_ApplyToVAngles(transform, ang);
58 // PLAYERS use different math
59 #if !(POSITIVE_PITCH_IS_DOWN)
63 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
65 fixedmakevectors(ang);
66 old_forward = v_forward;
68 fixedmakevectors(ang.y * '0 1 0');
69 old_yawforward = v_forward;
71 // their aiming directions are portalled...
72 new_forward = AnglesTransform_Apply(transform, old_forward);
73 new_up = AnglesTransform_Apply(transform, old_up);
74 new_yawforward = AnglesTransform_Apply(transform, old_yawforward);
76 // but now find a new sense of direction
78 // assume new_forward points straight up.
81 // new_up could now point forward OR backward... which direction to choose?
83 if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
85 // new_yawforward and new_yawup define the new aiming half-circle
86 // we "just" need to find out whether new_up or -new_up is in that half circle
87 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
88 if(new_up * new_yawforward < 0)
90 ang.y = vectoyaw(new_up); // this vector is the yaw we want
91 //print("UP/DOWN path: ", vtos(ang), "\n");
95 // good angles; here, "forward" suffices
96 ang = fixedvectoangles(new_forward);
97 //print("GOOD path: ", vtos(ang), "\n");
100 #if !(POSITIVE_PITCH_IS_DOWN)
107 .vector right_vector;
108 float Portal_TeleportPlayer(entity teleporter, entity player, entity portal_owner)
110 vector from, to, safe, step, transform, ang, newvel;
111 float planeshift, s, t;
113 if (!teleporter.enemy)
115 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
119 from = teleporter.origin;
120 transform = teleporter.portal_transform;
122 to = teleporter.enemy.origin;
123 to = to + AnglesTransform_Apply(teleporter.portal_transform, player.origin - from);
124 newvel = AnglesTransform_Apply(transform, player.velocity);
125 // this now is INSIDE the plane... can't use that
128 fixedmakevectors(teleporter.enemy.mangle);
130 // first shift it ON the plane if needed
131 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
133 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
134 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
135 to += newvel * (planeshift / (newvel * v_forward));
138 to += v_forward * planeshift;
140 s = (to - teleporter.enemy.origin) * v_right;
141 t = (to - teleporter.enemy.origin) * v_up;
142 s = bound(-48, s, 48);
143 t = bound(-48, t, 48);
144 to = teleporter.enemy.origin
145 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
149 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
150 step = to + ((safe - to) * v_forward) * v_forward;
151 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
154 LOG_INFO("'safe' teleport location is not safe!");
155 // FAIL TODO why does this happen?
158 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
159 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
162 LOG_INFO("trace_endpos in solid, this can't be!");
163 // FAIL TODO why does this happen? (reported by MrBougo)
166 to = trace_endpos + normalize(safe - trace_endpos) * 0;
167 //print(vtos(to), "\n");
169 // ang_x stuff works around weird quake angles
170 if(IS_PLAYER(player))
171 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
173 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
175 // factor -1 allows chaining portals, but may be weird
176 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
178 MUTATOR_CALLHOOK(PortalTeleport, player);
180 if (!teleporter.enemy)
182 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
187 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
190 // telefrag within 1 second of portal creation = amazing
191 if(time < teleporter.teleport_time + 1)
192 Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
194 else if(player != portal_owner && IS_PLAYER(portal_owner) && IS_PLAYER(player))
196 player.pusher = portal_owner;
197 player.pushltime = time + autocvar_g_maxpushtime;
198 player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
201 if (!teleporter.enemy)
203 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
207 // reset fade counter
208 teleporter.portal_wants_to_vanish = 0;
209 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
210 SetResourceExplicit(teleporter, RES_HEALTH, autocvar_g_balance_portal_health);
211 SetResourceExplicit(teleporter.enemy, RES_HEALTH, autocvar_g_balance_portal_health);
216 float Portal_FindSafeOrigin(entity portal)
220 portal.mins = PL_MIN_CONST - SAFERNUDGE;
221 portal.maxs = PL_MAX_CONST + SAFERNUDGE;
222 fixedmakevectors(portal.mangle);
223 portal.origin += 16 * v_forward;
224 if(!move_out_of_solid(portal))
227 LOG_INFO("NO SAFE ORIGIN");
231 portal.portal_safe_origin = portal.origin;
232 setorigin(portal, o);
236 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
238 float dist, distpersec, delta;
241 dist = (eorg - porg) * pnorm;
242 dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
243 dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
244 dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
245 if(dist < -1) // other side?
247 #ifdef PORTALS_ARE_NOT_SOLID
248 distpersec = evel * pnorm;
249 if(distpersec >= 0) // going away from the portal?
251 // we don't need this check with solid portals, them being SOLID_BSP should suffice
252 delta = dist / distpersec;
253 v = eorg - evel * delta - porg;
254 v = v - pnorm * (pnorm * v);
255 return vlen(v) < psize;
261 void Portal_Touch(entity this, entity toucher)
265 #ifdef PORTALS_ARE_NOT_SOLID
266 // portal is being removed?
267 if(this.solid != SOLID_TRIGGER)
268 return; // possibly engine bug
270 if(IS_PLAYER(toucher))
271 return; // handled by think
274 if(toucher.classname == "item_flag_team")
275 return; // never portal these
277 if(toucher.classname == "grapplinghook")
278 return; // handled by think
280 if(!autocvar_g_vehicles_teleportable)
281 if(IS_VEHICLE(toucher))
282 return; // no teleporting vehicles?
285 error("Portal_Touch called for a broken portal\n");
287 #ifdef PORTALS_ARE_NOT_SOLID
288 if(trace_fraction < 1)
289 return; // only handle TouchAreaGrid ones (only these can teleport)
291 if(trace_fraction >= 1)
292 return; // only handle impacts
295 if(toucher.classname == "porto")
297 if(toucher.portal_id == this.portal_id)
300 if(time < this.portal_activatetime)
301 if(toucher == this.aiment)
303 this.portal_activatetime = time + 0.1;
306 if(toucher != this.aiment)
307 if(IS_PLAYER(toucher))
308 if(IS_INDEPENDENT_PLAYER(toucher) || IS_INDEPENDENT_PLAYER(this.aiment))
309 return; // cannot go through someone else's portal
310 if(toucher.aiment != this.aiment)
311 if(IS_PLAYER(toucher.aiment))
312 if(IS_INDEPENDENT_PLAYER(toucher.aiment) || IS_INDEPENDENT_PLAYER(this.aiment))
313 return; // cannot go through someone else's portal
314 fixedmakevectors(this.mangle);
315 g = frametime * '0 0 -1' * autocvar_sv_gravity;
316 if(!Portal_WillHitPlane(toucher.origin, toucher.mins, toucher.maxs, toucher.velocity + g, this.origin, v_forward, this.maxs.x))
320 if(toucher.mins_x < PL_MIN.x || toucher.mins_y < PL_MIN.y || toucher.mins_z < PL_MIN.z
321 || toucher.maxs_x > PL_MAX.x || toucher.maxs_y > PL_MAX.y || toucher.maxs_z > PL_MAX.z)
323 // can't teleport this
328 if(Portal_TeleportPlayer(this, toucher, this.aiment))
329 if(toucher.classname == "porto")
330 if(toucher.effects & EF_RED)
331 toucher.effects += EF_BLUE - EF_RED;
334 void Portal_MakeBrokenPortal(entity portal)
337 portal.solid = SOLID_NOT;
338 settouch(portal, func_null);
339 setthink(portal, func_null);
341 portal.nextthink = 0;
342 portal.takedamage = DAMAGE_NO;
345 void Portal_MakeWaitingPortal(entity portal)
348 portal.solid = SOLID_NOT;
349 settouch(portal, func_null);
350 setthink(portal, func_null);
351 portal.effects = EF_ADDITIVE;
352 portal.nextthink = 0;
353 portal.takedamage = DAMAGE_YES;
356 void Portal_MakeInPortal(entity portal)
359 portal.solid = SOLID_NOT; // this is done when connecting them!
360 settouch(portal, Portal_Touch);
361 setthink(portal, Portal_Think);
362 portal.effects = EF_RED;
363 portal.nextthink = time;
364 portal.takedamage = DAMAGE_NO;
367 void Portal_MakeOutPortal(entity portal)
370 portal.solid = SOLID_NOT;
371 settouch(portal, func_null);
372 setthink(portal, func_null);
373 portal.effects = EF_STARDUST | EF_BLUE;
374 portal.nextthink = 0;
375 portal.takedamage = DAMAGE_YES;
378 void Portal_Disconnect(entity teleporter, entity destination)
380 teleporter.enemy = NULL;
381 destination.enemy = NULL;
382 Portal_MakeBrokenPortal(teleporter);
383 Portal_MakeBrokenPortal(destination);
386 void Portal_Connect(entity teleporter, entity destination)
388 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
390 teleporter.enemy = destination;
391 destination.enemy = teleporter;
392 Portal_MakeInPortal(teleporter);
393 Portal_MakeOutPortal(destination);
394 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
395 destination.fade_time = teleporter.fade_time;
396 teleporter.portal_wants_to_vanish = 0;
397 destination.portal_wants_to_vanish = 0;
398 teleporter.teleport_time = time;
399 #ifdef PORTALS_ARE_NOT_SOLID
400 teleporter.solid = SOLID_TRIGGER;
402 teleporter.solid = SOLID_BSP;
406 void Portal_Remove(entity portal, float killed)
413 Portal_Disconnect(portal, e);
414 Portal_Remove(e, killed);
417 if(portal == portal.aiment.portal_in)
418 portal.aiment.portal_in = NULL;
419 if(portal == portal.aiment.portal_out)
420 portal.aiment.portal_out = NULL;
421 //portal.aiment = NULL;
423 // makes the portal vanish
426 fixedmakevectors(portal.mangle);
427 sound(portal, CH_SHOTS, SND_PORTO_EXPLODE, VOL_BASE, ATTEN_NORM);
428 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
433 Portal_MakeBrokenPortal(portal);
434 sound(portal, CH_SHOTS, SND_PORTO_EXPIRE, VOL_BASE, ATTEN_NORM);
435 SUB_SetFade(portal, time, 0.5);
439 void Portal_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
441 if(deathtype == DEATH_TELEFRAG.m_id)
443 if(attacker != this.aiment)
444 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(this.aiment))
446 TakeResource(this, RES_HEALTH, damage);
447 if(GetResource(this, RES_HEALTH) < 0)
448 Portal_Remove(this, 1);
451 void Portal_Think_TryTeleportPlayer(entity this, entity e, vector g, entity portal_owner)
453 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, this.origin, v_forward, this.maxs.x))
456 // if e would hit the portal in a frame...
457 // already teleport him
458 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
459 if(trace_ent == this)
460 Portal_TeleportPlayer(this, e, portal_owner);
463 void Portal_Think(entity this)
468 #ifdef PORTALS_ARE_NOT_SOLID
469 // portal is being removed?
470 if(this.solid != SOLID_TRIGGER)
471 return; // possibly engine bug
474 error("Portal_Think called for a broken portal\n");
477 this.solid = SOLID_BBOX;
480 g = frametime * '0 0 -1' * autocvar_sv_gravity;
482 fixedmakevectors(this.mangle);
484 FOREACH_CLIENT(IS_PLAYER(it), {
486 if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o))
487 continue; // cannot go through someone else's portal
489 if(it != o || time >= this.portal_activatetime)
490 Portal_Think_TryTeleportPlayer(this, it, g, o);
492 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
494 .entity weaponentity = weaponentities[slot];
495 if(it.(weaponentity).hook)
496 Portal_Think_TryTeleportPlayer(this, it.(weaponentity).hook, g, o);
499 this.solid = SOLID_TRIGGER;
503 this.nextthink = time;
505 if(this.fade_time && time > this.fade_time)
506 Portal_Remove(this, 0);
509 bool Portal_Customize(entity this, entity client)
512 client = client.enemy;
513 if(client == this.aiment)
515 this.modelindex = this.savemodelindex;
517 else if(IS_INDEPENDENT_PLAYER(client) || IS_INDEPENDENT_PLAYER(this.aiment))
523 this.modelindex = this.savemodelindex;
529 // when creating in-portal:
531 // clear existing in-portal
534 // when creating out-portal:
536 // clear existing out-portal
539 // disconnect portals
540 // clear both portals
541 // after timeout of in-portal:
542 // disconnect portals
543 // clear both portals
544 // TODO: ensure only one portal shot at once
545 float Portal_SetInPortal(entity own, entity portal)
550 Portal_Disconnect(own.portal_in, own.portal_out);
551 Portal_Remove(own.portal_in, 0);
553 own.portal_in = portal;
556 own.portal_out.portal_id = portal.portal_id;
557 Portal_Connect(own.portal_in, own.portal_out);
561 float Portal_SetOutPortal(entity own, entity portal)
566 Portal_Disconnect(own.portal_in, own.portal_out);
567 Portal_Remove(own.portal_out, 0);
569 own.portal_out = portal;
572 own.portal_in.portal_id = portal.portal_id;
573 Portal_Connect(own.portal_in, own.portal_out);
577 void Portal_ClearAll_PortalsOnly(entity own)
580 Portal_Remove(own.portal_in, 0);
582 Portal_Remove(own.portal_out, 0);
584 void Portal_ClearAll(entity own)
586 Portal_ClearAll_PortalsOnly(own);
589 void Portal_RemoveLater_Think(entity this)
591 Portal_Remove(this, this.cnt);
593 void Portal_RemoveLater(entity portal, float kill)
595 Portal_MakeBrokenPortal(portal);
597 setthink(portal, Portal_RemoveLater_Think);
598 portal.nextthink = time;
600 void Portal_ClearAllLater_PortalsOnly(entity own)
603 Portal_RemoveLater(own.portal_in, 0);
605 Portal_RemoveLater(own.portal_out, 0);
607 void Portal_ClearAllLater(entity own)
609 Portal_ClearAllLater_PortalsOnly(own);
612 void Portal_ClearWithID(entity own, float id)
615 if(own.portal_in.portal_id == id)
618 Portal_Disconnect(own.portal_in, own.portal_out);
619 Portal_Remove(own.portal_in, 0);
622 if(own.portal_out.portal_id == id)
625 Portal_Disconnect(own.portal_in, own.portal_out);
626 Portal_Remove(own.portal_out, 0);
630 entity Portal_Spawn(entity own, vector org, vector ang)
634 fixedmakevectors(ang);
635 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
638 portal = new(portal);
640 setorigin(portal, org);
643 portal.angles_x = -portal.angles.x; // is a bmodel
644 setthink(portal, Portal_Think);
645 portal.nextthink = 0;
646 portal.portal_activatetime = time + 0.1;
647 portal.takedamage = DAMAGE_AIM;
648 portal.event_damage = Portal_Damage;
649 portal.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
650 SetResourceExplicit(portal, RES_HEALTH, autocvar_g_balance_portal_health);
651 setmodel(portal, MDL_PORTAL);
652 portal.savemodelindex = portal.modelindex;
653 setcefc(portal, Portal_Customize);
655 if(!Portal_FindSafeOrigin(portal))
661 setsize(portal, '-48 -48 -48', '48 48 48');
662 Portal_MakeWaitingPortal(portal);
667 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
674 ang = fixedvectoangles2(trace_plane_normal, dir);
675 fixedmakevectors(ang);
677 portal = Portal_Spawn(own, org, ang);
681 portal.portal_id = portal_id_val;
682 Portal_SetInPortal(own, portal);
687 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
694 ang = fixedvectoangles2(trace_plane_normal, dir);
695 fixedmakevectors(ang);
697 portal = Portal_Spawn(own, org, ang);
701 portal.portal_id = portal_id_val;
702 Portal_SetOutPortal(own, portal);