3 #include <common/constants.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/effects/all.qh>
6 #include <common/mapobjects/subs.qh>
7 #include <common/mapobjects/teleporters.qh>
8 #include <common/mapobjects/triggers.qh>
9 #include <common/notifications/all.qh>
10 #include <common/util.qh>
11 #include <common/vehicles/sv_vehicles.qh>
12 #include <common/vehicles/vehicle.qh>
13 #include <common/weapons/_all.qh>
14 #include <common/weapons/weapon/porto.qh>
15 #include <lib/csqcmodel/sv_model.qh>
16 #include <lib/warpzone/anglestransform.qh>
17 #include <lib/warpzone/common.qh>
18 #include <lib/warpzone/util_server.qh>
19 #include <server/client.qh>
20 #include <server/damage.qh>
21 #include <server/hook.qh>
22 #include <server/mutators/_mod.qh>
23 #include <server/player.qh>
24 #include <server/weapons/common.qh>
26 #define PORTALS_ARE_NOT_SOLID
28 const vector SAFENUDGE = '1 1 1';
29 const vector SAFERNUDGE = '8 8 8';
31 .vector portal_transform;
32 .vector portal_safe_origin;
33 .float portal_wants_to_vanish;
34 .float portal_activatetime;
35 .float savemodelindex;
37 float PlayerEdgeDistance(entity p, vector v)
40 ((v.x < 0) ? p.mins.x : p.maxs.x),
41 ((v.y < 0) ? p.mins.y : p.maxs.y),
42 ((v.z < 0) ? p.mins.z : p.maxs.z));
47 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
49 vector old_forward, old_up;
50 vector old_yawforward;
51 vector new_forward, new_up;
52 vector new_yawforward;
57 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
58 ang = AnglesTransform_ApplyToVAngles(transform, ang);
61 // PLAYERS use different math
62 #if !(POSITIVE_PITCH_IS_DOWN)
66 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
68 fixedmakevectors(ang);
69 old_forward = v_forward;
71 fixedmakevectors(ang.y * '0 1 0');
72 old_yawforward = v_forward;
74 // their aiming directions are portalled...
75 new_forward = AnglesTransform_Apply(transform, old_forward);
76 new_up = AnglesTransform_Apply(transform, old_up);
77 new_yawforward = AnglesTransform_Apply(transform, old_yawforward);
79 // but now find a new sense of direction
81 // assume new_forward points straight up.
84 // new_up could now point forward OR backward... which direction to choose?
86 if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
88 // new_yawforward and new_yawup define the new aiming half-circle
89 // we "just" need to find out whether new_up or -new_up is in that half circle
90 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
91 if(new_up * new_yawforward < 0)
93 ang.y = vectoyaw(new_up); // this vector is the yaw we want
94 //print("UP/DOWN path: ", vtos(ang), "\n");
98 // good angles; here, "forward" suffices
99 ang = fixedvectoangles(new_forward);
100 //print("GOOD path: ", vtos(ang), "\n");
103 #if !(POSITIVE_PITCH_IS_DOWN)
110 .vector right_vector;
111 float Portal_TeleportPlayer(entity teleporter, entity player, entity portal_owner)
113 vector from, to, safe, step, transform, ang, newvel;
114 float planeshift, s, t;
116 if (!teleporter.enemy)
118 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
122 from = teleporter.origin;
123 transform = teleporter.portal_transform;
125 to = teleporter.enemy.origin;
126 to = to + AnglesTransform_Apply(teleporter.portal_transform, player.origin - from);
127 newvel = AnglesTransform_Apply(transform, player.velocity);
128 // this now is INSIDE the plane... can't use that
131 fixedmakevectors(teleporter.enemy.mangle);
133 // first shift it ON the plane if needed
134 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
136 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
137 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
138 to += newvel * (planeshift / (newvel * v_forward));
141 to += v_forward * planeshift;
143 s = (to - teleporter.enemy.origin) * v_right;
144 t = (to - teleporter.enemy.origin) * v_up;
145 s = bound(-48, s, 48);
146 t = bound(-48, t, 48);
147 to = teleporter.enemy.origin
148 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
152 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
153 step = to + ((safe - to) * v_forward) * v_forward;
154 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
157 LOG_INFO("'safe' teleport location is not safe!");
158 // FAIL TODO why does this happen?
161 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
162 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
165 LOG_INFO("trace_endpos in solid, this can't be!");
166 // FAIL TODO why does this happen? (reported by MrBougo)
169 to = trace_endpos + normalize(safe - trace_endpos) * 0;
170 //print(vtos(to), "\n");
172 // ang_x stuff works around weird quake angles
173 if(IS_PLAYER(player))
174 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
176 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
178 // factor -1 allows chaining portals, but may be weird
179 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
181 MUTATOR_CALLHOOK(PortalTeleport, player);
183 if (!teleporter.enemy)
185 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
190 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
193 // telefrag within 1 second of portal creation = amazing
194 if(time < teleporter.teleport_time + 1)
195 Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
197 else if(player != portal_owner && IS_PLAYER(portal_owner) && IS_PLAYER(player))
199 player.pusher = portal_owner;
200 player.pushltime = time + autocvar_g_maxpushtime;
201 player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
204 if (!teleporter.enemy)
206 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
210 // reset fade counter
211 teleporter.portal_wants_to_vanish = 0;
212 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
213 SetResourceExplicit(teleporter, RES_HEALTH, autocvar_g_balance_portal_health);
214 SetResourceExplicit(teleporter.enemy, RES_HEALTH, autocvar_g_balance_portal_health);
219 float Portal_FindSafeOrigin(entity portal)
223 portal.mins = PL_MIN_CONST - SAFERNUDGE;
224 portal.maxs = PL_MAX_CONST + SAFERNUDGE;
225 fixedmakevectors(portal.mangle);
226 portal.origin += 16 * v_forward;
227 if(!move_out_of_solid(portal))
230 LOG_INFO("NO SAFE ORIGIN");
234 portal.portal_safe_origin = portal.origin;
235 setorigin(portal, o);
239 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
241 float dist, distpersec, delta;
244 dist = (eorg - porg) * pnorm;
245 dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
246 dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
247 dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
248 if(dist < -1) // other side?
250 #ifdef PORTALS_ARE_NOT_SOLID
251 distpersec = evel * pnorm;
252 if(distpersec >= 0) // going away from the portal?
254 // we don't need this check with solid portals, them being SOLID_BSP should suffice
255 delta = dist / distpersec;
256 v = eorg - evel * delta - porg;
257 v = v - pnorm * (pnorm * v);
258 return vlen(v) < psize;
264 void Portal_Touch(entity this, entity toucher)
268 #ifdef PORTALS_ARE_NOT_SOLID
269 // portal is being removed?
270 if(this.solid != SOLID_TRIGGER)
271 return; // possibly engine bug
273 if(IS_PLAYER(toucher))
274 return; // handled by think
277 if(toucher.classname == "item_flag_team")
278 return; // never portal these
280 if(toucher.classname == "grapplinghook")
281 return; // handled by think
283 if(!autocvar_g_vehicles_teleportable)
284 if(IS_VEHICLE(toucher))
285 return; // no teleporting vehicles?
288 error("Portal_Touch called for a broken portal\n");
290 #ifdef PORTALS_ARE_NOT_SOLID
291 if(trace_fraction < 1)
292 return; // only handle TouchAreaGrid ones (only these can teleport)
294 if(trace_fraction >= 1)
295 return; // only handle impacts
298 if(toucher.classname == "porto")
300 if(toucher.portal_id == this.portal_id)
303 if(time < this.portal_activatetime)
304 if(toucher == this.aiment)
306 this.portal_activatetime = time + 0.1;
309 if(toucher != this.aiment)
310 if(IS_PLAYER(toucher))
311 if(IS_INDEPENDENT_PLAYER(toucher) || IS_INDEPENDENT_PLAYER(this.aiment))
312 return; // cannot go through someone else's portal
313 if(toucher.aiment != this.aiment)
314 if(IS_PLAYER(toucher.aiment))
315 if(IS_INDEPENDENT_PLAYER(toucher.aiment) || IS_INDEPENDENT_PLAYER(this.aiment))
316 return; // cannot go through someone else's portal
317 fixedmakevectors(this.mangle);
318 g = frametime * '0 0 -1' * autocvar_sv_gravity;
319 if(!Portal_WillHitPlane(toucher.origin, toucher.mins, toucher.maxs, toucher.velocity + g, this.origin, v_forward, this.maxs.x))
323 if(toucher.mins_x < PL_MIN.x || toucher.mins_y < PL_MIN.y || toucher.mins_z < PL_MIN.z
324 || toucher.maxs_x > PL_MAX.x || toucher.maxs_y > PL_MAX.y || toucher.maxs_z > PL_MAX.z)
326 // can't teleport this
331 if(Portal_TeleportPlayer(this, toucher, this.aiment))
332 if(toucher.classname == "porto")
333 if(toucher.effects & EF_RED)
334 toucher.effects += EF_BLUE - EF_RED;
337 void Portal_MakeBrokenPortal(entity portal)
340 portal.solid = SOLID_NOT;
341 settouch(portal, func_null);
342 setthink(portal, func_null);
344 portal.nextthink = 0;
345 portal.takedamage = DAMAGE_NO;
348 void Portal_MakeWaitingPortal(entity portal)
351 portal.solid = SOLID_NOT;
352 settouch(portal, func_null);
353 setthink(portal, func_null);
354 portal.effects = EF_ADDITIVE;
355 portal.nextthink = 0;
356 portal.takedamage = DAMAGE_YES;
359 void Portal_MakeInPortal(entity portal)
362 portal.solid = SOLID_NOT; // this is done when connecting them!
363 settouch(portal, Portal_Touch);
364 setthink(portal, Portal_Think);
365 portal.effects = EF_RED;
366 portal.nextthink = time;
367 portal.takedamage = DAMAGE_NO;
370 void Portal_MakeOutPortal(entity portal)
373 portal.solid = SOLID_NOT;
374 settouch(portal, func_null);
375 setthink(portal, func_null);
376 portal.effects = EF_STARDUST | EF_BLUE;
377 portal.nextthink = 0;
378 portal.takedamage = DAMAGE_YES;
381 void Portal_Disconnect(entity teleporter, entity destination)
383 teleporter.enemy = NULL;
384 destination.enemy = NULL;
385 Portal_MakeBrokenPortal(teleporter);
386 Portal_MakeBrokenPortal(destination);
389 void Portal_Connect(entity teleporter, entity destination)
391 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
393 teleporter.enemy = destination;
394 destination.enemy = teleporter;
395 Portal_MakeInPortal(teleporter);
396 Portal_MakeOutPortal(destination);
397 teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
398 destination.fade_time = teleporter.fade_time;
399 teleporter.portal_wants_to_vanish = 0;
400 destination.portal_wants_to_vanish = 0;
401 teleporter.teleport_time = time;
402 #ifdef PORTALS_ARE_NOT_SOLID
403 teleporter.solid = SOLID_TRIGGER;
405 teleporter.solid = SOLID_BSP;
409 void Portal_Remove(entity portal, float killed)
416 Portal_Disconnect(portal, e);
417 Portal_Remove(e, killed);
420 if(portal == portal.aiment.portal_in)
421 portal.aiment.portal_in = NULL;
422 if(portal == portal.aiment.portal_out)
423 portal.aiment.portal_out = NULL;
424 //portal.aiment = NULL;
426 // makes the portal vanish
429 fixedmakevectors(portal.mangle);
430 sound(portal, CH_SHOTS, SND_PORTO_EXPLODE, VOL_BASE, ATTEN_NORM);
431 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
436 Portal_MakeBrokenPortal(portal);
437 sound(portal, CH_SHOTS, SND_PORTO_EXPIRE, VOL_BASE, ATTEN_NORM);
438 SUB_SetFade(portal, time, 0.5);
442 void Portal_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
444 if(deathtype == DEATH_TELEFRAG.m_id)
446 if(attacker != this.aiment)
447 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(this.aiment))
449 TakeResource(this, RES_HEALTH, damage);
450 if(GetResource(this, RES_HEALTH) < 0)
451 Portal_Remove(this, 1);
454 void Portal_Think_TryTeleportPlayer(entity this, entity e, vector g, entity portal_owner)
456 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, this.origin, v_forward, this.maxs.x))
459 // if e would hit the portal in a frame...
460 // already teleport him
461 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
462 if(trace_ent == this)
463 Portal_TeleportPlayer(this, e, portal_owner);
466 void Portal_Think(entity this)
471 #ifdef PORTALS_ARE_NOT_SOLID
472 // portal is being removed?
473 if(this.solid != SOLID_TRIGGER)
474 return; // possibly engine bug
477 error("Portal_Think called for a broken portal\n");
480 this.solid = SOLID_BBOX;
483 g = frametime * '0 0 -1' * autocvar_sv_gravity;
485 fixedmakevectors(this.mangle);
487 FOREACH_CLIENT(IS_PLAYER(it), {
489 if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o))
490 continue; // cannot go through someone else's portal
492 if(it != o || time >= this.portal_activatetime)
493 Portal_Think_TryTeleportPlayer(this, it, g, o);
495 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
497 .entity weaponentity = weaponentities[slot];
498 if(it.(weaponentity).hook)
499 Portal_Think_TryTeleportPlayer(this, it.(weaponentity).hook, g, o);
502 this.solid = SOLID_TRIGGER;
506 this.nextthink = time;
508 if(this.fade_time && time > this.fade_time)
509 Portal_Remove(this, 0);
512 bool Portal_Customize(entity this, entity client)
515 client = client.enemy;
516 if(client == this.aiment)
518 this.modelindex = this.savemodelindex;
520 else if(IS_INDEPENDENT_PLAYER(client) || IS_INDEPENDENT_PLAYER(this.aiment))
526 this.modelindex = this.savemodelindex;
532 // when creating in-portal:
534 // clear existing in-portal
537 // when creating out-portal:
539 // clear existing out-portal
542 // disconnect portals
543 // clear both portals
544 // after timeout of in-portal:
545 // disconnect portals
546 // clear both portals
547 // TODO: ensure only one portal shot at once
548 float Portal_SetInPortal(entity own, entity portal)
553 Portal_Disconnect(own.portal_in, own.portal_out);
554 Portal_Remove(own.portal_in, 0);
556 own.portal_in = portal;
559 own.portal_out.portal_id = portal.portal_id;
560 Portal_Connect(own.portal_in, own.portal_out);
564 float Portal_SetOutPortal(entity own, entity portal)
569 Portal_Disconnect(own.portal_in, own.portal_out);
570 Portal_Remove(own.portal_out, 0);
572 own.portal_out = portal;
575 own.portal_in.portal_id = portal.portal_id;
576 Portal_Connect(own.portal_in, own.portal_out);
580 void Portal_ClearAll_PortalsOnly(entity own)
583 Portal_Remove(own.portal_in, 0);
585 Portal_Remove(own.portal_out, 0);
587 void Portal_ClearAll(entity own)
589 Portal_ClearAll_PortalsOnly(own);
592 void Portal_RemoveLater_Think(entity this)
594 Portal_Remove(this, this.cnt);
596 void Portal_RemoveLater(entity portal, float kill)
598 Portal_MakeBrokenPortal(portal);
600 setthink(portal, Portal_RemoveLater_Think);
601 portal.nextthink = time;
603 void Portal_ClearAllLater_PortalsOnly(entity own)
606 Portal_RemoveLater(own.portal_in, 0);
608 Portal_RemoveLater(own.portal_out, 0);
610 void Portal_ClearAllLater(entity own)
612 Portal_ClearAllLater_PortalsOnly(own);
615 void Portal_ClearWithID(entity own, float id)
618 if(own.portal_in.portal_id == id)
621 Portal_Disconnect(own.portal_in, own.portal_out);
622 Portal_Remove(own.portal_in, 0);
625 if(own.portal_out.portal_id == id)
628 Portal_Disconnect(own.portal_in, own.portal_out);
629 Portal_Remove(own.portal_out, 0);
633 entity Portal_Spawn(entity own, vector org, vector ang)
637 fixedmakevectors(ang);
638 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
641 portal = new(portal);
643 setorigin(portal, org);
646 portal.angles_x = -portal.angles.x; // is a bmodel
647 setthink(portal, Portal_Think);
648 portal.nextthink = 0;
649 portal.portal_activatetime = time + 0.1;
650 portal.takedamage = DAMAGE_AIM;
651 portal.event_damage = Portal_Damage;
652 portal.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
653 SetResourceExplicit(portal, RES_HEALTH, autocvar_g_balance_portal_health);
654 setmodel(portal, MDL_PORTAL);
655 portal.savemodelindex = portal.modelindex;
656 setcefc(portal, Portal_Customize);
658 if(!Portal_FindSafeOrigin(portal))
664 setsize(portal, '-48 -48 -48', '48 48 48');
665 Portal_MakeWaitingPortal(portal);
670 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
677 ang = fixedvectoangles2(trace_plane_normal, dir);
678 fixedmakevectors(ang);
680 portal = Portal_Spawn(own, org, ang);
684 portal.portal_id = portal_id_val;
685 Portal_SetInPortal(own, portal);
690 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
697 ang = fixedvectoangles2(trace_plane_normal, dir);
698 fixedmakevectors(ang);
700 portal = Portal_Spawn(own, org, ang);
704 portal.portal_id = portal_id_val;
705 Portal_SetOutPortal(own, portal);