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 <server/player.qh>
21 #define PORTALS_ARE_NOT_SOLID
23 const vector SAFENUDGE = '1 1 1';
24 const vector SAFERNUDGE = '8 8 8';
26 .vector portal_transform;
27 .vector portal_safe_origin;
28 .float portal_wants_to_vanish;
29 .float portal_activatetime;
30 .float savemodelindex;
32 float PlayerEdgeDistance(entity p, vector v)
36 if(v.x < 0) vbest.x = p.mins.x; else vbest.x = p.maxs.x;
37 if(v.y < 0) vbest.y = p.mins.y; else vbest.y = p.maxs.y;
38 if(v.z < 0) vbest.z = p.mins.z; else vbest.z = p.maxs.z;
43 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
45 vector old_forward, old_up;
46 vector old_yawforward;
47 vector new_forward, new_up;
48 vector new_yawforward;
53 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
54 ang = AnglesTransform_ApplyToVAngles(transform, ang);
57 // PLAYERS use different math
58 #if !(POSITIVE_PITCH_IS_DOWN)
62 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
64 fixedmakevectors(ang);
65 old_forward = v_forward;
67 fixedmakevectors(ang.y * '0 1 0');
68 old_yawforward = v_forward;
70 // their aiming directions are portalled...
71 new_forward = AnglesTransform_Apply(transform, old_forward);
72 new_up = AnglesTransform_Apply(transform, old_up);
73 new_yawforward = AnglesTransform_Apply(transform, old_yawforward);
75 // but now find a new sense of direction
77 // assume new_forward points straight up.
80 // new_up could now point forward OR backward... which direction to choose?
82 if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
84 // new_yawforward and new_yawup define the new aiming half-circle
85 // we "just" need to find out whether new_up or -new_up is in that half circle
86 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
87 if(new_up * new_yawforward < 0)
89 ang.y = vectoyaw(new_up); // this vector is the yaw we want
90 //print("UP/DOWN path: ", vtos(ang), "\n");
94 // good angles; here, "forward" suffices
95 ang = fixedvectoangles(new_forward);
96 //print("GOOD path: ", vtos(ang), "\n");
99 #if !(POSITIVE_PITCH_IS_DOWN)
106 .vector right_vector;
107 float Portal_TeleportPlayer(entity teleporter, entity player, entity portal_owner)
109 vector from, to, safe, step, transform, ang, newvel;
110 float planeshift, s, t;
112 if (!teleporter.enemy)
114 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
118 from = teleporter.origin;
119 transform = teleporter.portal_transform;
121 to = teleporter.enemy.origin;
122 to = to + AnglesTransform_Apply(teleporter.portal_transform, player.origin - from);
123 newvel = AnglesTransform_Apply(transform, player.velocity);
124 // this now is INSIDE the plane... can't use that
127 fixedmakevectors(teleporter.enemy.mangle);
129 // first shift it ON the plane if needed
130 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
132 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
133 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
134 to += newvel * (planeshift / (newvel * v_forward));
137 to += v_forward * planeshift;
139 s = (to - teleporter.enemy.origin) * v_right;
140 t = (to - teleporter.enemy.origin) * v_up;
141 s = bound(-48, s, 48);
142 t = bound(-48, t, 48);
143 to = teleporter.enemy.origin
144 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
148 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
149 step = to + ((safe - to) * v_forward) * v_forward;
150 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
153 LOG_INFO("'safe' teleport location is not safe!");
154 // FAIL TODO why does this happen?
157 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
158 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
161 LOG_INFO("trace_endpos in solid, this can't be!");
162 // FAIL TODO why does this happen? (reported by MrBougo)
165 to = trace_endpos + normalize(safe - trace_endpos) * 0;
166 //print(vtos(to), "\n");
168 // ang_x stuff works around weird quake angles
169 if(IS_PLAYER(player))
170 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
172 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
174 // factor -1 allows chaining portals, but may be weird
175 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
177 MUTATOR_CALLHOOK(PortalTeleport, player);
179 if (!teleporter.enemy)
181 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
186 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
189 // telefrag within 1 second of portal creation = amazing
190 if(time < teleporter.teleport_time + 1)
191 Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
193 else if(player != portal_owner && IS_PLAYER(portal_owner) && IS_PLAYER(player))
195 player.pusher = portal_owner;
196 player.pushltime = time + autocvar_g_maxpushtime;
197 player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
200 if (!teleporter.enemy)
202 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
206 // reset fade counter
207 teleporter.portal_wants_to_vanish = 0;
208 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
209 SetResourceExplicit(teleporter, RES_HEALTH, autocvar_g_balance_portal_health);
210 SetResourceExplicit(teleporter.enemy, RES_HEALTH, autocvar_g_balance_portal_health);
215 float Portal_FindSafeOrigin(entity portal)
219 portal.mins = PL_MIN_CONST - SAFERNUDGE;
220 portal.maxs = PL_MAX_CONST + SAFERNUDGE;
221 fixedmakevectors(portal.mangle);
222 portal.origin += 16 * v_forward;
223 if(!move_out_of_solid(portal))
226 LOG_INFO("NO SAFE ORIGIN");
230 portal.portal_safe_origin = portal.origin;
231 setorigin(portal, o);
235 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
237 float dist, distpersec, delta;
240 dist = (eorg - porg) * pnorm;
241 dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
242 dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
243 dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
244 if(dist < -1) // other side?
246 #ifdef PORTALS_ARE_NOT_SOLID
247 distpersec = evel * pnorm;
248 if(distpersec >= 0) // going away from the portal?
250 // we don't need this check with solid portals, them being SOLID_BSP should suffice
251 delta = dist / distpersec;
252 v = eorg - evel * delta - porg;
253 v = v - pnorm * (pnorm * v);
254 return vlen(v) < psize;
260 void Portal_Touch(entity this, entity toucher)
264 #ifdef PORTALS_ARE_NOT_SOLID
265 // portal is being removed?
266 if(this.solid != SOLID_TRIGGER)
267 return; // possibly engine bug
269 if(IS_PLAYER(toucher))
270 return; // handled by think
273 if(toucher.classname == "item_flag_team")
274 return; // never portal these
276 if(toucher.classname == "grapplinghook")
277 return; // handled by think
279 if(!autocvar_g_vehicles_teleportable)
280 if(IS_VEHICLE(toucher))
281 return; // no teleporting vehicles?
284 error("Portal_Touch called for a broken portal\n");
286 #ifdef PORTALS_ARE_NOT_SOLID
287 if(trace_fraction < 1)
288 return; // only handle TouchAreaGrid ones (only these can teleport)
290 if(trace_fraction >= 1)
291 return; // only handle impacts
294 if(toucher.classname == "porto")
296 if(toucher.portal_id == this.portal_id)
299 if(time < this.portal_activatetime)
300 if(toucher == this.aiment)
302 this.portal_activatetime = time + 0.1;
305 if(toucher != this.aiment)
306 if(IS_PLAYER(toucher))
307 if(IS_INDEPENDENT_PLAYER(toucher) || IS_INDEPENDENT_PLAYER(this.aiment))
308 return; // cannot go through someone else's portal
309 if(toucher.aiment != this.aiment)
310 if(IS_PLAYER(toucher.aiment))
311 if(IS_INDEPENDENT_PLAYER(toucher.aiment) || IS_INDEPENDENT_PLAYER(this.aiment))
312 return; // cannot go through someone else's portal
313 fixedmakevectors(this.mangle);
314 g = frametime * '0 0 -1' * autocvar_sv_gravity;
315 if(!Portal_WillHitPlane(toucher.origin, toucher.mins, toucher.maxs, toucher.velocity + g, this.origin, v_forward, this.maxs.x))
319 if(toucher.mins_x < PL_MIN.x || toucher.mins_y < PL_MIN.y || toucher.mins_z < PL_MIN.z
320 || toucher.maxs_x > PL_MAX.x || toucher.maxs_y > PL_MAX.y || toucher.maxs_z > PL_MAX.z)
322 // can't teleport this
327 if(Portal_TeleportPlayer(this, toucher, this.aiment))
328 if(toucher.classname == "porto")
329 if(toucher.effects & EF_RED)
330 toucher.effects += EF_BLUE - EF_RED;
333 void Portal_MakeBrokenPortal(entity portal)
336 portal.solid = SOLID_NOT;
337 settouch(portal, func_null);
338 setthink(portal, func_null);
340 portal.nextthink = 0;
341 portal.takedamage = DAMAGE_NO;
344 void Portal_MakeWaitingPortal(entity portal)
347 portal.solid = SOLID_NOT;
348 settouch(portal, func_null);
349 setthink(portal, func_null);
350 portal.effects = EF_ADDITIVE;
351 portal.nextthink = 0;
352 portal.takedamage = DAMAGE_YES;
355 void Portal_MakeInPortal(entity portal)
358 portal.solid = SOLID_NOT; // this is done when connecting them!
359 settouch(portal, Portal_Touch);
360 setthink(portal, Portal_Think);
361 portal.effects = EF_RED;
362 portal.nextthink = time;
363 portal.takedamage = DAMAGE_NO;
366 void Portal_MakeOutPortal(entity portal)
369 portal.solid = SOLID_NOT;
370 settouch(portal, func_null);
371 setthink(portal, func_null);
372 portal.effects = EF_STARDUST | EF_BLUE;
373 portal.nextthink = 0;
374 portal.takedamage = DAMAGE_YES;
377 void Portal_Disconnect(entity teleporter, entity destination)
379 teleporter.enemy = NULL;
380 destination.enemy = NULL;
381 Portal_MakeBrokenPortal(teleporter);
382 Portal_MakeBrokenPortal(destination);
385 void Portal_Connect(entity teleporter, entity destination)
387 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
389 teleporter.enemy = destination;
390 destination.enemy = teleporter;
391 Portal_MakeInPortal(teleporter);
392 Portal_MakeOutPortal(destination);
393 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
394 destination.fade_time = teleporter.fade_time;
395 teleporter.portal_wants_to_vanish = 0;
396 destination.portal_wants_to_vanish = 0;
397 teleporter.teleport_time = time;
398 #ifdef PORTALS_ARE_NOT_SOLID
399 teleporter.solid = SOLID_TRIGGER;
401 teleporter.solid = SOLID_BSP;
405 void Portal_Remove(entity portal, float killed)
412 Portal_Disconnect(portal, e);
413 Portal_Remove(e, killed);
416 if(portal == portal.aiment.portal_in)
417 portal.aiment.portal_in = NULL;
418 if(portal == portal.aiment.portal_out)
419 portal.aiment.portal_out = NULL;
420 //portal.aiment = NULL;
422 // makes the portal vanish
425 fixedmakevectors(portal.mangle);
426 sound(portal, CH_SHOTS, SND_PORTO_EXPLODE, VOL_BASE, ATTEN_NORM);
427 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
432 Portal_MakeBrokenPortal(portal);
433 sound(portal, CH_SHOTS, SND_PORTO_EXPIRE, VOL_BASE, ATTEN_NORM);
434 SUB_SetFade(portal, time, 0.5);
438 void Portal_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
440 if(deathtype == DEATH_TELEFRAG.m_id)
442 if(attacker != this.aiment)
443 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(this.aiment))
445 TakeResource(this, RES_HEALTH, damage);
446 if(GetResource(this, RES_HEALTH) < 0)
447 Portal_Remove(this, 1);
450 void Portal_Think_TryTeleportPlayer(entity this, entity e, vector g, entity portal_owner)
452 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, this.origin, v_forward, this.maxs.x))
455 // if e would hit the portal in a frame...
456 // already teleport him
457 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
458 if(trace_ent == this)
459 Portal_TeleportPlayer(this, e, portal_owner);
462 void Portal_Think(entity this)
467 #ifdef PORTALS_ARE_NOT_SOLID
468 // portal is being removed?
469 if(this.solid != SOLID_TRIGGER)
470 return; // possibly engine bug
473 error("Portal_Think called for a broken portal\n");
476 this.solid = SOLID_BBOX;
479 g = frametime * '0 0 -1' * autocvar_sv_gravity;
481 fixedmakevectors(this.mangle);
483 FOREACH_CLIENT(IS_PLAYER(it), {
485 if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o))
486 continue; // cannot go through someone else's portal
488 if(it != o || time >= this.portal_activatetime)
489 Portal_Think_TryTeleportPlayer(this, it, g, o);
491 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
493 .entity weaponentity = weaponentities[slot];
494 if(it.(weaponentity).hook)
495 Portal_Think_TryTeleportPlayer(this, it.(weaponentity).hook, g, o);
498 this.solid = SOLID_TRIGGER;
502 this.nextthink = time;
504 if(this.fade_time && time > this.fade_time)
505 Portal_Remove(this, 0);
508 bool Portal_Customize(entity this, entity client)
511 client = client.enemy;
512 if(client == this.aiment)
514 this.modelindex = this.savemodelindex;
516 else if(IS_INDEPENDENT_PLAYER(client) || IS_INDEPENDENT_PLAYER(this.aiment))
522 this.modelindex = this.savemodelindex;
528 // when creating in-portal:
530 // clear existing in-portal
533 // when creating out-portal:
535 // clear existing out-portal
538 // disconnect portals
539 // clear both portals
540 // after timeout of in-portal:
541 // disconnect portals
542 // clear both portals
543 // TODO: ensure only one portal shot at once
544 float Portal_SetInPortal(entity own, entity portal)
549 Portal_Disconnect(own.portal_in, own.portal_out);
550 Portal_Remove(own.portal_in, 0);
552 own.portal_in = portal;
555 own.portal_out.portal_id = portal.portal_id;
556 Portal_Connect(own.portal_in, own.portal_out);
560 float Portal_SetOutPortal(entity own, entity portal)
565 Portal_Disconnect(own.portal_in, own.portal_out);
566 Portal_Remove(own.portal_out, 0);
568 own.portal_out = portal;
571 own.portal_in.portal_id = portal.portal_id;
572 Portal_Connect(own.portal_in, own.portal_out);
576 void Portal_ClearAll_PortalsOnly(entity own)
579 Portal_Remove(own.portal_in, 0);
581 Portal_Remove(own.portal_out, 0);
583 void Portal_ClearAll(entity own)
585 Portal_ClearAll_PortalsOnly(own);
588 void Portal_RemoveLater_Think(entity this)
590 Portal_Remove(this, this.cnt);
592 void Portal_RemoveLater(entity portal, float kill)
594 Portal_MakeBrokenPortal(portal);
596 setthink(portal, Portal_RemoveLater_Think);
597 portal.nextthink = time;
599 void Portal_ClearAllLater_PortalsOnly(entity own)
602 Portal_RemoveLater(own.portal_in, 0);
604 Portal_RemoveLater(own.portal_out, 0);
606 void Portal_ClearAllLater(entity own)
608 Portal_ClearAllLater_PortalsOnly(own);
611 void Portal_ClearWithID(entity own, float id)
614 if(own.portal_in.portal_id == id)
617 Portal_Disconnect(own.portal_in, own.portal_out);
618 Portal_Remove(own.portal_in, 0);
621 if(own.portal_out.portal_id == id)
624 Portal_Disconnect(own.portal_in, own.portal_out);
625 Portal_Remove(own.portal_out, 0);
629 entity Portal_Spawn(entity own, vector org, vector ang)
633 fixedmakevectors(ang);
634 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
637 portal = new(portal);
639 setorigin(portal, org);
642 portal.angles_x = -portal.angles.x; // is a bmodel
643 setthink(portal, Portal_Think);
644 portal.nextthink = 0;
645 portal.portal_activatetime = time + 0.1;
646 portal.takedamage = DAMAGE_AIM;
647 portal.event_damage = Portal_Damage;
648 portal.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
649 SetResourceExplicit(portal, RES_HEALTH, autocvar_g_balance_portal_health);
650 setmodel(portal, MDL_PORTAL);
651 portal.savemodelindex = portal.modelindex;
652 setcefc(portal, Portal_Customize);
654 if(!Portal_FindSafeOrigin(portal))
660 setsize(portal, '-48 -48 -48', '48 48 48');
661 Portal_MakeWaitingPortal(portal);
666 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
673 ang = fixedvectoangles2(trace_plane_normal, dir);
674 fixedmakevectors(ang);
676 portal = Portal_Spawn(own, org, ang);
680 portal.portal_id = portal_id_val;
681 Portal_SetInPortal(own, portal);
686 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
693 ang = fixedvectoangles2(trace_plane_normal, dir);
694 fixedmakevectors(ang);
696 portal = Portal_Spawn(own, org, ang);
700 portal.portal_id = portal_id_val;
701 Portal_SetOutPortal(own, portal);