1 float portals_must_not_break; // debug code
3 .vector portal_transform;
4 .vector portal_safe_origin;
5 .float portal_wants_to_vanish;
6 .float portal_activatetime;
9 .entity portal_in, portal_out;
11 vector Portal_Transform_Apply(vector transform, vector v)
13 fixedmakevectors(transform);
14 return v_forward * v_x
19 vector Portal_Transform_Multiply(vector t1, vector t2)
21 vector m_forward, m_up;
22 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
23 m_forward = Portal_Transform_Apply(t1, m_forward); m_up = Portal_Transform_Apply(t1, m_up);
24 return fixedvectoangles2(m_forward, m_up);
27 vector Portal_Transform_Invert(vector transform)
29 vector i_forward, i_up;
30 fixedmakevectors(transform);
31 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
32 // but these are orthogonal unit vectors!
33 // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
34 // TODO is this always -transform?
35 i_forward_x = v_forward_x;
36 i_forward_y = -v_right_x;
43 v = fixedvectoangles2(i_forward, i_up);
44 print("Transform: ", vtos(transform), "\n");
45 print("Inverted: ", vtos(v), "\n");
46 print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
47 fixedmakevectors(Portal_Transform_Multiply(v, transform));
48 print("Verify: ", vtos(v_forward), "\n");
49 print("Verify: ", vtos(v_right), "\n");
50 print("Verify: ", vtos(v_up), "\n");
52 return fixedvectoangles2(i_forward, i_up);
55 vector Portal_Transform_TurnDirection(vector transform)
57 // turn 180 degrees around v_up
58 // changes in-direction to out-direction
59 fixedmakevectors(transform);
60 return fixedvectoangles2(-1 * v_forward, 1 * v_up);
63 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
65 return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
68 float PlayerEdgeDistance(entity p, vector v)
72 if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
73 if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
74 if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
80 float Portal_TeleportPlayer(entity teleporter, entity player)
82 vector from, to, safe, step, transform, ang, newvel;
83 vector new_forward, new_up;
84 vector old_forward, old_up;
85 vector new_yawforward;
86 vector old_yawforward;
87 float planeshift, s, t;
89 if(!portals_must_not_break)
90 error("Portal_TeleportPlayer called for unprotected portals");
91 print("Portalling using ", etos(teleporter), " to ", etos(teleporter.enemy), "\n");
93 from = teleporter.origin;
94 transform = teleporter.portal_transform;
96 to = teleporter.enemy.origin;
97 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
98 newvel = Portal_Transform_Apply(transform, player.velocity);
99 // this now is INSIDE the plane... can't use that
102 fixedmakevectors(teleporter.enemy.angles);
104 // first shift it ON the plane if needed
105 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
106 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
107 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
108 to += newvel * (planeshift / (newvel * v_forward));
110 to += trace_plane_normal * planeshift;
112 s = (to - teleporter.enemy.origin) * v_right;
113 t = (to - teleporter.enemy.origin) * v_up;
114 s = bound(-48, s, 48);
115 t = bound(-48, t, 48);
116 to = teleporter.enemy.origin
117 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
121 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
122 step = to + ((safe - to) * v_forward) * v_forward;
123 tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', step, MOVE_NOMONSTERS, player);
126 print("'safe' teleport location is not safe!\n");
127 // FAIL TODO why does this happen?
131 tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', to, MOVE_NOMONSTERS, player);
133 error("trace_endpos in solid!");
136 // ang_x stuff works around weird quake angles
137 if(player.classname == "player")
139 ang = player.v_angle;
141 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
142 ang = Portal_Transform_Multiply(transform, ang);
145 // PLAYERS use different math
148 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
150 fixedmakevectors(ang);
151 old_forward = v_forward;
153 fixedmakevectors(ang_y * '0 1 0');
154 old_yawforward = v_forward;
156 // their aiming directions are portalled...
157 new_forward = Portal_Transform_Apply(transform, old_forward);
158 new_up = Portal_Transform_Apply(transform, old_up);
159 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
161 // but now find a new sense of direction
163 // assume new_forward points straight up.
166 // new_up could now point forward OR backward... which direction to choose?
168 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
170 // new_yawforward and new_yawup define the new aiming half-circle
171 // we "just" need to find out whether new_up or -new_up is in that half circle
172 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
173 if(new_up * new_yawforward < 0)
174 new_up = -1 * new_up;
175 ang_y = vectoyaw(new_up); // this vector is the yaw we want
176 //print("UP/DOWN path: ", vtos(ang), "\n");
180 // good angles; here, "forward" suffices
181 ang = fixedvectoangles(new_forward);
182 //print("GOOD path: ", vtos(ang), "\n");
186 ang_z = player.angles_z;
191 ang = Portal_Transform_Multiply(transform, player.angles);
194 // factor -1 allows chaining portals, but may be weird
195 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
197 if(player.flagcarried)
198 DropFlag(player.flagcarried);
199 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
201 // reset fade counter
202 if(!portals_must_not_break)
203 error("Portal_TeleportPlayer ended with unprotected portals");
204 print("Portalled using ", etos(teleporter), " to ", etos(teleporter.enemy), "\n");
205 teleporter.portal_wants_to_vanish = 0;
206 teleporter.fade_time = time + 15;
207 teleporter.enemy.health = 300;
212 float Portal_FindSafeOrigin(entity portal)
216 portal.mins = PL_MIN - '8 8 8';
217 portal.maxs = PL_MAX + '8 8 8';
218 fixedmakevectors(portal.angles);
219 portal.origin += 16 * v_forward;
220 if(!move_out_of_solid(portal))
223 print("NO SAFE ORIGIN\n");
227 portal.portal_safe_origin = portal.origin;
228 setorigin(portal, o);
234 // portal is being removed?
235 if(self.solid != SOLID_TRIGGER)
236 return; // possibly engine bug
239 error("Portal_Touch called for a broken portal\n");
241 if(trace_fraction < 1)
242 return; // only handle TouchAreaGrid ones (only these can teleport)
243 // for some unknown reason, this also gets collisions from SV_Impact sometimes
245 if(other.classname == "porto")
247 if(other.portal_id == self.portal_id)
250 if(time < self.portal_activatetime)
251 if(other == self.owner)
253 self.portal_activatetime = time + 0.1;
256 if(other != self.owner)
257 if(other.classname == "player")
258 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
259 return; // cannot go through someone else's portal
260 if(other.owner != self.owner)
261 if(other.owner.classname == "player")
262 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
263 return; // cannot go through someone else's portal
264 fixedmakevectors(self.angles);
265 if((other.origin - self.origin) * v_forward < 0)
267 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
268 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
270 // can't teleport this
274 print("Portal ID ", etos(self), " verified working.\n");
275 portals_must_not_break = 1;
277 if(Portal_TeleportPlayer(self, other))
278 if(other.classname == "porto")
279 if(other.effects & EF_RED)
280 other.effects += EF_BLUE - EF_RED;
282 portals_must_not_break = 0;
286 void Portal_MakeBrokenPortal(entity portal)
288 if(portals_must_not_break)
289 error("Trying to remove a portal during teleporting");
291 portal.solid = SOLID_NOT;
292 portal.touch = SUB_Null;
293 portal.think = SUB_Null;
295 //portal.colormod = '1 1 1';
296 portal.nextthink = 0;
297 portal.takedamage = DAMAGE_NO;
300 void Portal_MakeWaitingPortal(entity portal)
302 portal.solid = SOLID_NOT;
303 portal.touch = SUB_Null;
304 portal.think = SUB_Null;
305 portal.effects = EF_ADDITIVE;
306 portal.colormod = '1 1 1';
307 portal.nextthink = 0;
308 portal.takedamage = DAMAGE_YES;
311 void Portal_MakeInPortal(entity portal)
313 portal.solid = SOLID_TRIGGER;
314 portal.touch = Portal_Touch;
315 portal.think = Portal_Think;
316 portal.effects = EF_RED;
317 portal.colormod = '1 0 0';
318 portal.nextthink = time;
319 portal.takedamage = DAMAGE_NO;
322 void Portal_MakeOutPortal(entity portal)
324 portal.solid = SOLID_NOT;
325 portal.touch = SUB_Null;
326 portal.think = SUB_Null;
327 portal.effects = EF_STARDUST | EF_BLUE;
328 portal.colormod = '0 0 1';
329 portal.nextthink = 0;
330 portal.takedamage = DAMAGE_YES;
333 void Portal_Disconnect(entity teleporter, entity destination)
335 teleporter.enemy = world;
336 destination.enemy = world;
337 Portal_MakeBrokenPortal(teleporter);
338 Portal_MakeBrokenPortal(destination);
341 void Portal_Connect(entity teleporter, entity destination)
343 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
347 // let's verify the transform
348 vector in_f, in_r, in_u;
349 vector out_f, out_r, out_u;
350 fixedmakevectors(teleporter.angles);
354 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
355 fixedmakevectors(destination.angles);
359 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
360 // INTENDED TRANSFORM:
364 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
365 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
366 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
368 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
369 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
373 teleporter.enemy = destination;
374 destination.enemy = teleporter;
375 Portal_MakeInPortal(teleporter);
376 Portal_MakeOutPortal(destination);
377 teleporter.fade_time = time + 15;
378 destination.fade_time = time + 15;
379 teleporter.portal_wants_to_vanish = 0;
380 destination.portal_wants_to_vanish = 0;
383 void Portal_Remove(entity portal, float killed)
388 if(portals_must_not_break)
389 error("Trying to remove a portal during teleporting");
393 Portal_Disconnect(portal, e);
394 Portal_Remove(e, killed);
397 if(portal == portal.owner.portal_in)
398 portal.owner.portal_in = world;
399 if(portal == portal.owner.portal_out)
400 portal.owner.portal_out = world;
401 //portal.owner = world;
403 // makes the portal vanish
406 fixedmakevectors(portal.angles);
407 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
408 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
413 Portal_MakeBrokenPortal(portal);
414 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
415 SUB_SetFade(portal, time, 0.5);
419 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
421 if(deathtype == DEATH_TELEFRAG)
423 if(attacker != self.owner)
424 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
426 self.health -= damage;
428 Portal_Remove(self, 1);
435 // portal is being removed?
436 if(self.solid != SOLID_TRIGGER)
437 return; // possibly engine bug
440 error("Portal_Think called for a broken portal\n");
442 //print("Portal ID ", etos(self), " verified working.\n");
443 portals_must_not_break = 1;
446 self.solid = SOLID_BBOX;
450 if(time < self.portal_activatetime)
454 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
455 continue; // cannot go through someone else's portal
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 == self)
460 Portal_TeleportPlayer(self, e);
462 self.solid = SOLID_TRIGGER;
465 portals_must_not_break = 0;
467 self.nextthink = time;
469 if(time > self.fade_time)
470 Portal_Remove(self, 0);
473 float Portal_Customize()
475 if(other.classname == "spectator")
477 if(other == self.owner)
479 self.modelindex = self.modelindex_lod0;
481 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
487 self.modelindex = self.modelindex_lod0;
493 // when creating in-portal:
495 // clear existing in-portal
498 // when creating out-portal:
500 // clear existing out-portal
503 // disconnect portals
504 // clear both portals
505 // after timeout of in-portal:
506 // disconnect portals
507 // clear both portals
508 // TODO: ensure only one portal shot at once
509 float Portal_SetInPortal(entity own, entity portal)
514 Portal_Disconnect(own.portal_in, own.portal_out);
515 Portal_Remove(own.portal_in, 0);
517 own.portal_in = portal;
520 own.portal_out.portal_id = portal.portal_id;
521 Portal_Connect(own.portal_in, own.portal_out);
525 float Portal_SetOutPortal(entity own, entity portal)
530 Portal_Disconnect(own.portal_in, own.portal_out);
531 Portal_Remove(own.portal_out, 0);
533 own.portal_out = portal;
536 own.portal_in.portal_id = portal.portal_id;
537 Portal_Connect(own.portal_in, own.portal_out);
541 void Portal_ClearAll(entity own)
544 Portal_Remove(own.portal_in, 0);
546 Portal_Remove(own.portal_out, 0);
548 void Portal_RemoveLater_Think()
550 Portal_Remove(self, self.cnt);
552 void Portal_RemoveLater(entity portal, float kill)
554 Portal_MakeBrokenPortal(portal);
556 portal.think = Portal_RemoveLater_Think;
557 portal.nextthink = time;
559 void Portal_ClearAllLater(entity own)
562 Portal_RemoveLater(own.portal_in, 0);
564 Portal_RemoveLater(own.portal_out, 0);
566 void Portal_ClearWithID(entity own, float id)
569 if(own.portal_in.portal_id == id)
572 Portal_Disconnect(own.portal_in, own.portal_out);
573 Portal_Remove(own.portal_in, 0);
576 if(own.portal_out.portal_id == id)
579 Portal_Disconnect(own.portal_in, own.portal_out);
580 Portal_Remove(own.portal_out, 0);
584 entity Portal_Spawn(entity own, vector org, vector ang)
588 fixedmakevectors(ang);
589 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
593 portal.classname = "portal";
597 portal.think = Portal_Think;
598 portal.nextthink = 0;
599 portal.fade_time = time + 15;
600 portal.portal_activatetime = time + 0.1;
601 portal.event_damage = Portal_Damage;
603 setmodel(portal, "models/portal.md3");
604 portal.modelindex_lod0 = portal.modelindex;
605 portal.customizeentityforclient = Portal_Customize;
607 if(!Portal_FindSafeOrigin(portal))
613 setsize(portal, '-48 -48 -48', '48 48 48');
614 Portal_MakeWaitingPortal(portal);
619 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
626 ang = fixedvectoangles2(trace_plane_normal, dir);
627 fixedmakevectors(ang);
629 portal = Portal_Spawn(own, org, ang);
632 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
633 Portal_ClearAll(own);
637 portal.portal_id = portal_id_val;
638 Portal_SetInPortal(own, portal);
643 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
650 ang = fixedvectoangles2(trace_plane_normal, dir);
651 fixedmakevectors(ang);
653 portal = Portal_Spawn(own, org, ang);
656 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
657 Portal_ClearAll(own);
661 portal.portal_id = portal_id_val;
662 Portal_SetOutPortal(own, portal);