1 #define PORTALS_ARE_NOT_SOLID
3 #define SAFENUDGE '1 1 1'
4 #define SAFERNUDGE '8 8 8'
6 .vector portal_transform;
7 .vector portal_safe_origin;
8 .float portal_wants_to_vanish;
9 .float portal_activatetime;
11 float PlayerEdgeDistance(entity p, vector v)
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;
22 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
24 vector old_forward, old_up;
25 vector old_yawforward;
26 vector new_forward, new_up;
27 vector new_yawforward;
32 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
33 ang = AnglesTransform_ApplyToVAngles(transform, ang);
36 // PLAYERS use different math
37 #ifndef POSITIVE_PITCH_IS_DOWN
41 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
43 fixedmakevectors(ang);
44 old_forward = v_forward;
46 fixedmakevectors(ang_y * '0 1 0');
47 old_yawforward = v_forward;
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);
54 // but now find a new sense of direction
56 // assume new_forward points straight up.
59 // new_up could now point forward OR backward... which direction to choose?
61 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
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)
68 ang_y = vectoyaw(new_up); // this vector is the yaw we want
69 //print("UP/DOWN path: ", vtos(ang), "\n");
73 // good angles; here, "forward" suffices
74 ang = fixedvectoangles(new_forward);
75 //print("GOOD path: ", vtos(ang), "\n");
78 #ifndef POSITIVE_PITCH_IS_DOWN
86 float Portal_TeleportPlayer(entity teleporter, entity player)
88 vector from, to, safe, step, transform, ang, newvel;
89 float planeshift, s, t;
91 if not(teleporter.enemy)
93 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
97 from = teleporter.origin;
98 transform = teleporter.portal_transform;
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
106 fixedmakevectors(teleporter.enemy.mangle);
108 // first shift it ON the plane if needed
109 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
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));
116 to += v_forward * planeshift;
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
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);
132 print("'safe' teleport location is not safe!\n");
133 // FAIL TODO why does this happen?
136 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
137 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
140 print("trace_endpos in solid, this can't be!\n");
141 // FAIL TODO why does this happen? (reported by MrBougo)
144 to = trace_endpos + normalize(safe - trace_endpos) * 0;
145 //print(vtos(to), "\n");
147 // ang_x stuff works around weird quake angles
148 if(player.classname == "player")
149 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
151 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
153 // factor -1 allows chaining portals, but may be weird
154 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
156 if(player.flagcarried)
157 DropFlag(player.flagcarried, player, world);
159 if not(teleporter.enemy)
161 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
166 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
169 // telefrag within 1 second of portal creation = amazing
170 if(time < teleporter.teleport_time + 1)
171 AnnounceTo(player, "amazing");
174 if not(teleporter.enemy)
176 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
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;
189 float Portal_FindSafeOrigin(entity portal)
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))
200 print("NO SAFE ORIGIN\n");
204 portal.portal_safe_origin = portal.origin;
205 setorigin(portal, o);
209 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
211 float dist, distpersec, delta;
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?
220 #ifdef PORTALS_ARE_NOT_SOLID
221 distpersec = evel * pnorm;
222 if(distpersec >= 0) // going away from the portal?
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;
238 #ifdef PORTALS_ARE_NOT_SOLID
239 // portal is being removed?
240 if(self.solid != SOLID_TRIGGER)
241 return; // possibly engine bug
243 if(other.classname == "player")
244 return; // handled by think
247 if(other.classname == "item_flag_team")
248 return; // never portal these
250 if(other.classname == "grapplinghook")
251 return; // handled by think
254 error("Portal_Touch called for a broken portal\n");
256 #ifdef PORTALS_ARE_NOT_SOLID
257 if(trace_fraction < 1)
258 return; // only handle TouchAreaGrid ones (only these can teleport)
260 if(trace_fraction >= 1)
261 return; // only handle impacts
264 if(other.classname == "porto")
266 if(other.portal_id == self.portal_id)
269 if(time < self.portal_activatetime)
270 if(other == self.aiment)
272 self.portal_activatetime = time + 0.1;
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))
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)
292 // can't teleport this
297 if(Portal_TeleportPlayer(self, other))
298 if(other.classname == "porto")
299 if(other.effects & EF_RED)
300 other.effects += EF_BLUE - EF_RED;
304 void Portal_MakeBrokenPortal(entity portal)
307 portal.solid = SOLID_NOT;
308 portal.touch = SUB_Null;
309 portal.think = SUB_Null;
311 portal.nextthink = 0;
312 portal.takedamage = DAMAGE_NO;
315 void Portal_MakeWaitingPortal(entity portal)
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;
326 void Portal_MakeInPortal(entity portal)
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;
337 void Portal_MakeOutPortal(entity portal)
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;
348 void Portal_Disconnect(entity teleporter, entity destination)
350 teleporter.enemy = world;
351 destination.enemy = world;
352 Portal_MakeBrokenPortal(teleporter);
353 Portal_MakeBrokenPortal(destination);
356 void Portal_Connect(entity teleporter, entity destination)
358 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
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;
372 teleporter.solid = SOLID_BSP;
376 void Portal_Remove(entity portal, float killed)
383 Portal_Disconnect(portal, e);
384 Portal_Remove(e, killed);
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;
393 // makes the portal vanish
396 fixedmakevectors(portal.mangle);
397 sound(portal, CHAN_PROJECTILE, "porto/explode.wav", VOL_BASE, ATTN_NORM);
398 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
403 Portal_MakeBrokenPortal(portal);
404 sound(portal, CHAN_PROJECTILE, "porto/expire.wav", VOL_BASE, ATTN_NORM);
405 SUB_SetFade(portal, time, 0.5);
409 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
411 if(deathtype == DEATH_TELEFRAG)
413 if(attacker != self.aiment)
414 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.aiment))
416 self.health -= damage;
418 Portal_Remove(self, 1);
421 void Portal_Think_TryTeleportPlayer(entity e, vector g)
423 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, self.origin, v_forward, self.maxs_x))
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);
438 #ifdef PORTALS_ARE_NOT_SOLID
439 // portal is being removed?
440 if(self.solid != SOLID_TRIGGER)
441 return; // possibly engine bug
444 error("Portal_Think called for a broken portal\n");
447 self.solid = SOLID_BBOX;
450 g = frametime * '0 0 -1' * autocvar_sv_gravity;
452 fixedmakevectors(self.mangle);
457 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
458 continue; // cannot go through someone else's portal
460 if(e != o || time >= self.portal_activatetime)
461 Portal_Think_TryTeleportPlayer(e, g);
464 Portal_Think_TryTeleportPlayer(e.hook, g);
466 self.solid = SOLID_TRIGGER;
470 self.nextthink = time;
472 if(time > self.fade_time)
473 Portal_Remove(self, 0);
476 float Portal_Customize()
478 if(other.classname == "spectator")
480 if(other == self.aiment)
482 self.modelindex = self.modelindex_lod0;
484 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
490 self.modelindex = self.modelindex_lod0;
496 // when creating in-portal:
498 // clear existing in-portal
501 // when creating out-portal:
503 // clear existing out-portal
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)
517 Portal_Disconnect(own.portal_in, own.portal_out);
518 Portal_Remove(own.portal_in, 0);
520 own.portal_in = portal;
523 own.portal_out.portal_id = portal.portal_id;
524 Portal_Connect(own.portal_in, own.portal_out);
528 float Portal_SetOutPortal(entity own, entity portal)
533 Portal_Disconnect(own.portal_in, own.portal_out);
534 Portal_Remove(own.portal_out, 0);
536 own.portal_out = portal;
539 own.portal_in.portal_id = portal.portal_id;
540 Portal_Connect(own.portal_in, own.portal_out);
544 void Portal_ClearAll_PortalsOnly(entity own)
547 Portal_Remove(own.portal_in, 0);
549 Portal_Remove(own.portal_out, 0);
551 void Portal_ClearAll(entity own)
553 Portal_ClearAll_PortalsOnly(own);
556 void Portal_RemoveLater_Think()
558 Portal_Remove(self, self.cnt);
560 void Portal_RemoveLater(entity portal, float kill)
562 Portal_MakeBrokenPortal(portal);
564 portal.think = Portal_RemoveLater_Think;
565 portal.nextthink = time;
567 void Portal_ClearAllLater_PortalsOnly(entity own)
570 Portal_RemoveLater(own.portal_in, 0);
572 Portal_RemoveLater(own.portal_out, 0);
574 void Portal_ClearAllLater(entity own)
576 Portal_ClearAllLater_PortalsOnly(own);
579 void Portal_ClearWithID(entity own, float id)
582 if(own.portal_in.portal_id == id)
585 Portal_Disconnect(own.portal_in, own.portal_out);
586 Portal_Remove(own.portal_in, 0);
589 if(own.portal_out.portal_id == id)
592 Portal_Disconnect(own.portal_in, own.portal_out);
593 Portal_Remove(own.portal_out, 0);
597 entity Portal_Spawn(entity own, vector org, vector ang)
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))
606 portal.classname = "portal";
608 setorigin(portal, org);
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;
623 if(!Portal_FindSafeOrigin(portal))
629 setsize(portal, '-48 -48 -48', '48 48 48');
630 Portal_MakeWaitingPortal(portal);
635 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
642 ang = fixedvectoangles2(trace_plane_normal, dir);
643 fixedmakevectors(ang);
645 portal = Portal_Spawn(own, org, ang);
648 // if(!own.portal_out || own.portal_out.portal_id == portal_id_val)
649 Portal_ClearAll_PortalsOnly(own);
653 portal.portal_id = portal_id_val;
654 Portal_SetInPortal(own, portal);
659 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
666 ang = fixedvectoangles2(trace_plane_normal, dir);
667 fixedmakevectors(ang);
669 portal = Portal_Spawn(own, org, ang);
672 // if(!own.portal_in || own.portal_in.portal_id == portal_id_val)
673 Portal_ClearAll_PortalsOnly(own);
677 portal.portal_id = portal_id_val;
678 Portal_SetOutPortal(own, portal);