1 #define SAFENUDGE '1 1 1'
2 #define SAFERNUDGE '8 8 8'
4 .vector portal_transform;
5 .vector portal_safe_origin;
6 .float portal_wants_to_vanish;
7 .float portal_activatetime;
10 .entity portal_in, portal_out;
12 vector Portal_Transform_Apply(vector transform, vector v)
14 fixedmakevectors(transform);
15 return v_forward * v_x
20 vector Portal_Transform_Multiply(vector t1, vector t2)
22 vector m_forward, m_up;
23 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
24 m_forward = Portal_Transform_Apply(t1, m_forward); m_up = Portal_Transform_Apply(t1, m_up);
25 return fixedvectoangles2(m_forward, m_up);
28 vector Portal_Transform_Invert(vector transform)
30 vector i_forward, i_up;
31 fixedmakevectors(transform);
32 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
33 // but these are orthogonal unit vectors!
34 // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
35 // TODO is this always -transform?
36 i_forward_x = v_forward_x;
37 i_forward_y = -v_right_x;
44 v = fixedvectoangles2(i_forward, i_up);
45 print("Transform: ", vtos(transform), "\n");
46 print("Inverted: ", vtos(v), "\n");
47 print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
48 fixedmakevectors(Portal_Transform_Multiply(v, transform));
49 print("Verify: ", vtos(v_forward), "\n");
50 print("Verify: ", vtos(v_right), "\n");
51 print("Verify: ", vtos(v_up), "\n");
53 return fixedvectoangles2(i_forward, i_up);
56 vector Portal_Transform_TurnDirection(vector transform)
58 // turn 180 degrees around v_up
59 // changes in-direction to out-direction
60 fixedmakevectors(transform);
61 return fixedvectoangles2(-1 * v_forward, 1 * v_up);
64 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
66 return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
69 float PlayerEdgeDistance(entity p, vector v)
73 if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
74 if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
75 if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
81 float Portal_TeleportPlayer(entity teleporter, entity player)
83 vector from, to, safe, step, transform, ang, newvel;
84 vector new_forward, new_up;
85 vector old_forward, old_up;
86 vector new_yawforward;
87 vector old_yawforward;
88 float planeshift, s, t;
90 from = teleporter.origin;
91 transform = teleporter.portal_transform;
93 to = teleporter.enemy.origin;
94 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
95 newvel = Portal_Transform_Apply(transform, player.velocity);
96 // this now is INSIDE the plane... can't use that
99 fixedmakevectors(teleporter.enemy.angles);
101 // first shift it ON the plane if needed
102 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
103 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
104 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
105 to += newvel * (planeshift / (newvel * v_forward));
107 to += trace_plane_normal * planeshift;
109 s = (to - teleporter.enemy.origin) * v_right;
110 t = (to - teleporter.enemy.origin) * v_up;
111 s = bound(-48, s, 48);
112 t = bound(-48, t, 48);
113 to = teleporter.enemy.origin
114 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
118 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
119 step = to + ((safe - to) * v_forward) * v_forward;
120 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
123 print("'safe' teleport location is not safe!\n");
124 // FAIL TODO why does this happen?
127 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
128 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
130 error("trace_endpos in solid!");
131 to = trace_endpos + normalize(safe - trace_endpos) * 0;
132 //print(vtos(to), "\n");
134 // ang_x stuff works around weird quake angles
135 if(player.classname == "player")
137 ang = player.v_angle;
139 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
140 ang = Portal_Transform_Multiply(transform, ang);
143 // PLAYERS use different math
146 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
148 fixedmakevectors(ang);
149 old_forward = v_forward;
151 fixedmakevectors(ang_y * '0 1 0');
152 old_yawforward = v_forward;
154 // their aiming directions are portalled...
155 new_forward = Portal_Transform_Apply(transform, old_forward);
156 new_up = Portal_Transform_Apply(transform, old_up);
157 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
159 // but now find a new sense of direction
161 // assume new_forward points straight up.
164 // new_up could now point forward OR backward... which direction to choose?
166 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
168 // new_yawforward and new_yawup define the new aiming half-circle
169 // we "just" need to find out whether new_up or -new_up is in that half circle
170 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
171 if(new_up * new_yawforward < 0)
172 new_up = -1 * new_up;
173 ang_y = vectoyaw(new_up); // this vector is the yaw we want
174 //print("UP/DOWN path: ", vtos(ang), "\n");
178 // good angles; here, "forward" suffices
179 ang = fixedvectoangles(new_forward);
180 //print("GOOD path: ", vtos(ang), "\n");
184 ang_z = player.angles_z;
189 ang = Portal_Transform_Multiply(transform, player.angles);
192 // factor -1 allows chaining portals, but may be weird
193 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
195 if(player.flagcarried)
196 DropFlag(player.flagcarried, player, world);
197 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
199 // reset fade counter
200 teleporter.portal_wants_to_vanish = 0;
201 teleporter.fade_time = time + 15;
202 teleporter.enemy.health = 300;
207 float Portal_FindSafeOrigin(entity portal)
211 portal.mins = PL_MIN - SAFERNUDGE;
212 portal.maxs = PL_MAX + SAFERNUDGE;
213 fixedmakevectors(portal.angles);
214 portal.origin += 16 * v_forward;
215 if(!move_out_of_solid(portal))
218 print("NO SAFE ORIGIN\n");
222 portal.portal_safe_origin = portal.origin;
223 setorigin(portal, o);
229 // portal is being removed?
230 if(self.solid != SOLID_TRIGGER)
231 return; // possibly engine bug
234 error("Portal_Touch called for a broken portal\n");
236 if(trace_fraction < 1)
237 return; // only handle TouchAreaGrid ones (only these can teleport)
238 // for some unknown reason, this also gets collisions from SV_Impact sometimes
240 if(other.classname == "porto")
242 if(other.portal_id == self.portal_id)
245 if(time < self.portal_activatetime)
246 if(other == self.owner)
248 self.portal_activatetime = time + 0.1;
251 if(other != self.owner)
252 if(other.classname == "player")
253 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
254 return; // cannot go through someone else's portal
255 if(other.owner != self.owner)
256 if(other.owner.classname == "player")
257 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
258 return; // cannot go through someone else's portal
259 fixedmakevectors(self.angles);
260 if((other.origin - self.origin) * v_forward < 0)
262 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
263 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
265 // can't teleport this
269 if(Portal_TeleportPlayer(self, other))
270 if(other.classname == "porto")
271 if(other.effects & EF_RED)
272 other.effects += EF_BLUE - EF_RED;
276 void Portal_MakeBrokenPortal(entity portal)
278 portal.solid = SOLID_NOT;
279 portal.touch = SUB_Null;
280 portal.think = SUB_Null;
282 //portal.colormod = '1 1 1';
283 portal.nextthink = 0;
284 portal.takedamage = DAMAGE_NO;
287 void Portal_MakeWaitingPortal(entity portal)
289 portal.solid = SOLID_NOT;
290 portal.touch = SUB_Null;
291 portal.think = SUB_Null;
292 portal.effects = EF_ADDITIVE;
293 portal.colormod = '1 1 1';
294 portal.nextthink = 0;
295 portal.takedamage = DAMAGE_YES;
298 void Portal_MakeInPortal(entity portal)
300 portal.solid = SOLID_TRIGGER;
301 portal.touch = Portal_Touch;
302 portal.think = Portal_Think;
303 portal.effects = EF_RED;
304 portal.colormod = '1 0 0';
305 portal.nextthink = time;
306 portal.takedamage = DAMAGE_NO;
309 void Portal_MakeOutPortal(entity portal)
311 portal.solid = SOLID_NOT;
312 portal.touch = SUB_Null;
313 portal.think = SUB_Null;
314 portal.effects = EF_STARDUST | EF_BLUE;
315 portal.colormod = '0 0 1';
316 portal.nextthink = 0;
317 portal.takedamage = DAMAGE_YES;
320 void Portal_Disconnect(entity teleporter, entity destination)
322 teleporter.enemy = world;
323 destination.enemy = world;
324 Portal_MakeBrokenPortal(teleporter);
325 Portal_MakeBrokenPortal(destination);
328 void Portal_Connect(entity teleporter, entity destination)
330 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
334 // let's verify the transform
335 vector in_f, in_r, in_u;
336 vector out_f, out_r, out_u;
337 fixedmakevectors(teleporter.angles);
341 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
342 fixedmakevectors(destination.angles);
346 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
347 // INTENDED TRANSFORM:
351 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
352 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
353 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
355 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
356 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
360 teleporter.enemy = destination;
361 destination.enemy = teleporter;
362 Portal_MakeInPortal(teleporter);
363 Portal_MakeOutPortal(destination);
364 teleporter.fade_time = time + 15;
365 destination.fade_time = time + 15;
366 teleporter.portal_wants_to_vanish = 0;
367 destination.portal_wants_to_vanish = 0;
370 void Portal_Remove(entity portal, float killed)
377 Portal_Disconnect(portal, e);
378 Portal_Remove(e, killed);
381 if(portal == portal.owner.portal_in)
382 portal.owner.portal_in = world;
383 if(portal == portal.owner.portal_out)
384 portal.owner.portal_out = world;
385 //portal.owner = world;
387 // makes the portal vanish
390 fixedmakevectors(portal.angles);
391 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
392 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
397 Portal_MakeBrokenPortal(portal);
398 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
399 SUB_SetFade(portal, time, 0.5);
403 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
405 if(deathtype == DEATH_TELEFRAG)
407 if(attacker != self.owner)
408 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
410 self.health -= damage;
412 Portal_Remove(self, 1);
419 // portal is being removed?
420 if(self.solid != SOLID_TRIGGER)
421 return; // possibly engine bug
424 error("Portal_Think called for a broken portal\n");
427 self.solid = SOLID_BBOX;
430 fixedmakevectors(self.angles);
434 if(time < self.portal_activatetime)
438 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
439 continue; // cannot go through someone else's portal
440 if((e.origin - self.origin) * v_forward < 0) // wrong side of the plane? no teleport
443 // if e would hit the portal in a frame...
444 // already teleport him
445 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
446 if(trace_ent == self)
447 Portal_TeleportPlayer(self, e);
449 self.solid = SOLID_TRIGGER;
452 self.nextthink = time;
454 if(time > self.fade_time)
455 Portal_Remove(self, 0);
458 float Portal_Customize()
460 if(other.classname == "spectator")
462 if(other == self.owner)
464 self.modelindex = self.modelindex_lod0;
466 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
472 self.modelindex = self.modelindex_lod0;
478 // when creating in-portal:
480 // clear existing in-portal
483 // when creating out-portal:
485 // clear existing out-portal
488 // disconnect portals
489 // clear both portals
490 // after timeout of in-portal:
491 // disconnect portals
492 // clear both portals
493 // TODO: ensure only one portal shot at once
494 float Portal_SetInPortal(entity own, entity portal)
499 Portal_Disconnect(own.portal_in, own.portal_out);
500 Portal_Remove(own.portal_in, 0);
502 own.portal_in = portal;
505 own.portal_out.portal_id = portal.portal_id;
506 Portal_Connect(own.portal_in, own.portal_out);
510 float Portal_SetOutPortal(entity own, entity portal)
515 Portal_Disconnect(own.portal_in, own.portal_out);
516 Portal_Remove(own.portal_out, 0);
518 own.portal_out = portal;
521 own.portal_in.portal_id = portal.portal_id;
522 Portal_Connect(own.portal_in, own.portal_out);
526 void Portal_ClearAll(entity own)
529 Portal_Remove(own.portal_in, 0);
531 Portal_Remove(own.portal_out, 0);
533 void Portal_RemoveLater_Think()
535 Portal_Remove(self, self.cnt);
537 void Portal_RemoveLater(entity portal, float kill)
539 Portal_MakeBrokenPortal(portal);
541 portal.think = Portal_RemoveLater_Think;
542 portal.nextthink = time;
544 void Portal_ClearAllLater(entity own)
547 Portal_RemoveLater(own.portal_in, 0);
549 Portal_RemoveLater(own.portal_out, 0);
551 void Portal_ClearWithID(entity own, float id)
554 if(own.portal_in.portal_id == id)
557 Portal_Disconnect(own.portal_in, own.portal_out);
558 Portal_Remove(own.portal_in, 0);
561 if(own.portal_out.portal_id == id)
564 Portal_Disconnect(own.portal_in, own.portal_out);
565 Portal_Remove(own.portal_out, 0);
569 entity Portal_Spawn(entity own, vector org, vector ang)
573 fixedmakevectors(ang);
574 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
578 portal.classname = "portal";
582 portal.think = Portal_Think;
583 portal.nextthink = 0;
584 portal.fade_time = time + 15;
585 portal.portal_activatetime = time + 0.1;
586 portal.event_damage = Portal_Damage;
588 setmodel(portal, "models/portal.md3");
589 portal.modelindex_lod0 = portal.modelindex;
590 portal.customizeentityforclient = Portal_Customize;
592 if(!Portal_FindSafeOrigin(portal))
598 setsize(portal, '-48 -48 -48', '48 48 48');
599 Portal_MakeWaitingPortal(portal);
604 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
611 ang = fixedvectoangles2(trace_plane_normal, dir);
612 fixedmakevectors(ang);
614 portal = Portal_Spawn(own, org, ang);
617 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
618 Portal_ClearAll(own);
622 portal.portal_id = portal_id_val;
623 Portal_SetInPortal(own, portal);
628 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
635 ang = fixedvectoangles2(trace_plane_normal, dir);
636 fixedmakevectors(ang);
638 portal = Portal_Spawn(own, org, ang);
641 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
642 Portal_ClearAll(own);
646 portal.portal_id = portal_id_val;
647 Portal_SetOutPortal(own, portal);