1 .vector portal_transform;
2 .vector portal_safe_origin;
3 .float portal_wants_to_vanish;
4 .float portal_activatetime;
7 .entity portal_in, portal_out;
9 vector Portal_Transform_Apply(vector transform, vector v)
11 fixedmakevectors(transform);
12 return v_forward * v_x
17 vector Portal_Transform_Multiply(vector t1, vector t2)
19 vector m_forward, m_up;
20 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
21 m_forward = Portal_Transform_Apply(t1, m_forward); m_up = Portal_Transform_Apply(t1, m_up);
22 return fixedvectoangles2(m_forward, m_up);
25 vector Portal_Transform_Invert(vector transform)
27 vector i_forward, i_up;
28 fixedmakevectors(transform);
29 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
30 // but these are orthogonal unit vectors!
31 // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
32 // TODO is this always -transform?
33 i_forward_x = v_forward_x;
34 i_forward_y = -v_right_x;
41 v = fixedvectoangles2(i_forward, i_up);
42 print("Transform: ", vtos(transform), "\n");
43 print("Inverted: ", vtos(v), "\n");
44 print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
45 fixedmakevectors(Portal_Transform_Multiply(v, transform));
46 print("Verify: ", vtos(v_forward), "\n");
47 print("Verify: ", vtos(v_right), "\n");
48 print("Verify: ", vtos(v_up), "\n");
50 return fixedvectoangles2(i_forward, i_up);
53 vector Portal_Transform_TurnDirection(vector transform)
55 // turn 180 degrees around v_up
56 // changes in-direction to out-direction
57 fixedmakevectors(transform);
58 return fixedvectoangles2(-1 * v_forward, 1 * v_up);
61 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
63 return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
66 float PlayerEdgeDistance(entity p, vector v)
70 if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
71 if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
72 if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
78 float Portal_TeleportPlayer(entity teleporter, entity player)
80 vector from, to, safe, step, transform, ang, newvel;
81 vector new_forward, new_up;
82 vector old_forward, old_up;
83 vector new_yawforward;
84 vector old_yawforward;
85 float planeshift, s, t;
87 from = teleporter.origin;
88 transform = teleporter.portal_transform;
90 to = teleporter.enemy.origin;
91 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
92 newvel = Portal_Transform_Apply(transform, player.velocity);
93 // this now is INSIDE the plane... can't use that
96 fixedmakevectors(teleporter.enemy.angles);
98 // first shift it ON the plane if needed
99 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
100 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
101 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
102 to += newvel * (planeshift / (newvel * v_forward));
104 to += trace_plane_normal * planeshift;
106 s = (to - teleporter.enemy.origin) * v_right;
107 t = (to - teleporter.enemy.origin) * v_up;
108 s = bound(-48, s, 48);
109 t = bound(-48, t, 48);
110 to = teleporter.enemy.origin
111 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
115 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
116 step = to + ((safe - to) * v_forward) * v_forward;
117 tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', step, MOVE_NOMONSTERS, player);
120 print("'safe' teleport location is not safe!\n");
121 // FAIL TODO why does this happen?
125 tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', to, MOVE_NOMONSTERS, player);
127 error("trace_endpos in solid!");
130 // ang_x stuff works around weird quake angles
131 if(player.classname == "player")
133 ang = player.v_angle;
135 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
136 ang = Portal_Transform_Multiply(transform, ang);
139 // PLAYERS use different math
142 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
144 fixedmakevectors(ang);
145 old_forward = v_forward;
147 fixedmakevectors(ang_y * '0 1 0');
148 old_yawforward = v_forward;
150 // their aiming directions are portalled...
151 new_forward = Portal_Transform_Apply(transform, old_forward);
152 new_up = Portal_Transform_Apply(transform, old_up);
153 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
155 // but now find a new sense of direction
157 // assume new_forward points straight up.
160 // new_up could now point forward OR backward... which direction to choose?
162 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
164 // new_yawforward and new_yawup define the new aiming half-circle
165 // we "just" need to find out whether new_up or -new_up is in that half circle
166 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
167 if(new_up * new_yawforward < 0)
168 new_up = -1 * new_up;
169 ang_y = vectoyaw(new_up); // this vector is the yaw we want
170 //print("UP/DOWN path: ", vtos(ang), "\n");
174 // good angles; here, "forward" suffices
175 ang = fixedvectoangles(new_forward);
176 //print("GOOD path: ", vtos(ang), "\n");
180 ang_z = player.angles_z;
185 ang = Portal_Transform_Multiply(transform, player.angles);
188 // factor -1 allows chaining portals, but may be weird
189 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
191 if(player.flagcarried)
192 DropFlag(player.flagcarried, 1);
193 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
195 // reset fade counter
196 teleporter.portal_wants_to_vanish = 0;
197 teleporter.fade_time = time + 15;
198 teleporter.enemy.health = 300;
203 float Portal_FindSafeOrigin(entity portal)
207 portal.mins = PL_MIN - '8 8 8';
208 portal.maxs = PL_MAX + '8 8 8';
209 fixedmakevectors(portal.angles);
210 portal.origin += 16 * v_forward;
211 if(!move_out_of_solid(portal))
214 print("NO SAFE ORIGIN\n");
218 portal.portal_safe_origin = portal.origin;
219 setorigin(portal, o);
225 // portal is being removed?
226 if(self.solid != SOLID_TRIGGER)
227 return; // possibly engine bug
230 error("Portal_Touch called for a broken portal\n");
232 if(trace_fraction < 1)
233 return; // only handle TouchAreaGrid ones (only these can teleport)
234 // for some unknown reason, this also gets collisions from SV_Impact sometimes
236 if(other.classname == "porto")
238 if(other.portal_id == self.portal_id)
241 if(time < self.portal_activatetime)
242 if(other == self.owner)
244 self.portal_activatetime = time + 0.1;
247 if(other != self.owner)
248 if(other.classname == "player")
249 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
250 return; // cannot go through someone else's portal
251 if(other.owner != self.owner)
252 if(other.owner.classname == "player")
253 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
254 return; // cannot go through someone else's portal
255 fixedmakevectors(self.angles);
256 if((other.origin - self.origin) * v_forward < 0)
258 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
259 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
261 // can't teleport this
265 if(Portal_TeleportPlayer(self, other))
266 if(other.classname == "porto")
267 if(other.effects & EF_RED)
268 other.effects += EF_BLUE - EF_RED;
272 void Portal_MakeBrokenPortal(entity portal)
274 portal.solid = SOLID_NOT;
275 portal.touch = SUB_Null;
276 portal.think = SUB_Null;
278 //portal.colormod = '1 1 1';
279 portal.nextthink = 0;
280 portal.takedamage = DAMAGE_NO;
283 void Portal_MakeWaitingPortal(entity portal)
285 portal.solid = SOLID_NOT;
286 portal.touch = SUB_Null;
287 portal.think = SUB_Null;
288 portal.effects = EF_ADDITIVE;
289 portal.colormod = '1 1 1';
290 portal.nextthink = 0;
291 portal.takedamage = DAMAGE_YES;
294 void Portal_MakeInPortal(entity portal)
296 portal.solid = SOLID_TRIGGER;
297 portal.touch = Portal_Touch;
298 portal.think = Portal_Think;
299 portal.effects = EF_RED;
300 portal.colormod = '1 0 0';
301 portal.nextthink = time;
302 portal.takedamage = DAMAGE_NO;
305 void Portal_MakeOutPortal(entity portal)
307 portal.solid = SOLID_NOT;
308 portal.touch = SUB_Null;
309 portal.think = SUB_Null;
310 portal.effects = EF_STARDUST | EF_BLUE;
311 portal.colormod = '0 0 1';
312 portal.nextthink = 0;
313 portal.takedamage = DAMAGE_YES;
316 void Portal_Disconnect(entity teleporter, entity destination)
318 teleporter.enemy = world;
319 destination.enemy = world;
320 Portal_MakeBrokenPortal(teleporter);
321 Portal_MakeBrokenPortal(destination);
324 void Portal_Connect(entity teleporter, entity destination)
326 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
330 // let's verify the transform
331 vector in_f, in_r, in_u;
332 vector out_f, out_r, out_u;
333 fixedmakevectors(teleporter.angles);
337 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
338 fixedmakevectors(destination.angles);
342 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
343 // INTENDED TRANSFORM:
347 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
348 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
349 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
351 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
352 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
356 teleporter.enemy = destination;
357 destination.enemy = teleporter;
358 Portal_MakeInPortal(teleporter);
359 Portal_MakeOutPortal(destination);
360 teleporter.fade_time = time + 15;
361 destination.fade_time = time + 15;
362 teleporter.portal_wants_to_vanish = 0;
363 destination.portal_wants_to_vanish = 0;
366 void Portal_Remove(entity portal, float killed)
373 Portal_Disconnect(portal, e);
374 Portal_Remove(e, killed);
377 if(portal == portal.owner.portal_in)
378 portal.owner.portal_in = world;
379 if(portal == portal.owner.portal_out)
380 portal.owner.portal_out = world;
381 //portal.owner = world;
383 // makes the portal vanish
386 fixedmakevectors(portal.angles);
387 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
388 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
393 Portal_MakeBrokenPortal(portal);
394 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
395 SUB_SetFade(portal, time, 0.5);
399 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
401 if(deathtype == DEATH_TELEFRAG)
403 if(attacker != self.owner)
404 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
406 self.health -= damage;
408 Portal_Remove(self, 1);
415 // portal is being removed?
416 if(self.solid != SOLID_TRIGGER)
417 return; // possibly engine bug
420 error("Portal_Think called for a broken portal\n");
423 self.solid = SOLID_BBOX;
426 fixedmakevectors(self.angles);
430 if(time < self.portal_activatetime)
434 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
435 continue; // cannot go through someone else's portal
436 if((e.origin - self.origin) * v_forward < 0) // wrong side of the plane? no teleport
439 // if e would hit the portal in a frame...
440 // already teleport him
441 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
442 if(trace_ent == self)
443 Portal_TeleportPlayer(self, e);
445 self.solid = SOLID_TRIGGER;
448 self.nextthink = time;
450 if(time > self.fade_time)
451 Portal_Remove(self, 0);
454 float Portal_Customize()
456 if(other.classname == "spectator")
458 if(other == self.owner)
460 self.modelindex = self.modelindex_lod0;
462 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
468 self.modelindex = self.modelindex_lod0;
474 // when creating in-portal:
476 // clear existing in-portal
479 // when creating out-portal:
481 // clear existing out-portal
484 // disconnect portals
485 // clear both portals
486 // after timeout of in-portal:
487 // disconnect portals
488 // clear both portals
489 // TODO: ensure only one portal shot at once
490 float Portal_SetInPortal(entity own, entity portal)
495 Portal_Disconnect(own.portal_in, own.portal_out);
496 Portal_Remove(own.portal_in, 0);
498 own.portal_in = portal;
501 own.portal_out.portal_id = portal.portal_id;
502 Portal_Connect(own.portal_in, own.portal_out);
506 float Portal_SetOutPortal(entity own, entity portal)
511 Portal_Disconnect(own.portal_in, own.portal_out);
512 Portal_Remove(own.portal_out, 0);
514 own.portal_out = portal;
517 own.portal_in.portal_id = portal.portal_id;
518 Portal_Connect(own.portal_in, own.portal_out);
522 void Portal_ClearAll(entity own)
525 Portal_Remove(own.portal_in, 0);
527 Portal_Remove(own.portal_out, 0);
529 void Portal_RemoveLater_Think()
531 Portal_Remove(self, self.cnt);
533 void Portal_RemoveLater(entity portal, float kill)
535 Portal_MakeBrokenPortal(portal);
537 portal.think = Portal_RemoveLater_Think;
538 portal.nextthink = time;
540 void Portal_ClearAllLater(entity own)
543 Portal_RemoveLater(own.portal_in, 0);
545 Portal_RemoveLater(own.portal_out, 0);
547 void Portal_ClearWithID(entity own, float id)
550 if(own.portal_in.portal_id == id)
553 Portal_Disconnect(own.portal_in, own.portal_out);
554 Portal_Remove(own.portal_in, 0);
557 if(own.portal_out.portal_id == id)
560 Portal_Disconnect(own.portal_in, own.portal_out);
561 Portal_Remove(own.portal_out, 0);
565 entity Portal_Spawn(entity own, vector org, vector ang)
569 fixedmakevectors(ang);
570 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
574 portal.classname = "portal";
578 portal.think = Portal_Think;
579 portal.nextthink = 0;
580 portal.fade_time = time + 15;
581 portal.portal_activatetime = time + 0.1;
582 portal.event_damage = Portal_Damage;
584 setmodel(portal, "models/portal.md3");
585 portal.modelindex_lod0 = portal.modelindex;
586 portal.customizeentityforclient = Portal_Customize;
588 if(!Portal_FindSafeOrigin(portal))
594 setsize(portal, '-48 -48 -48', '48 48 48');
595 Portal_MakeWaitingPortal(portal);
600 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
607 ang = fixedvectoangles2(trace_plane_normal, dir);
608 fixedmakevectors(ang);
610 portal = Portal_Spawn(own, org, ang);
613 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
614 Portal_ClearAll(own);
618 portal.portal_id = portal_id_val;
619 Portal_SetInPortal(own, portal);
624 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
631 ang = fixedvectoangles2(trace_plane_normal, dir);
632 fixedmakevectors(ang);
634 portal = Portal_Spawn(own, org, ang);
637 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
638 Portal_ClearAll(own);
642 portal.portal_id = portal_id_val;
643 Portal_SetOutPortal(own, portal);