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;
86 from = teleporter.origin;
87 transform = teleporter.portal_transform;
89 to = teleporter.enemy.origin;
90 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
91 newvel = Portal_Transform_Apply(transform, player.velocity);
92 // this now is INSIDE the plane... can't use that
95 fixedmakevectors(teleporter.enemy.angles);
97 // first shift it ON the plane if needed
98 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
99 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
100 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
101 to += newvel * (planeshift / (newvel * v_forward));
103 to += trace_plane_normal * planeshift;
105 s = (to - teleporter.enemy.origin) * v_right;
106 t = (to - teleporter.enemy.origin) * v_up;
107 s = bound(-48, s, 48);
108 t = bound(-48, t, 48);
109 to = teleporter.enemy.origin
110 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
114 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
115 step = to + ((safe - to) * v_forward) * v_forward;
116 tracebox(safe, PL_MIN, PL_MAX, step, MOVE_NOMONSTERS, player);
119 bprint("'safe' teleport location is not safe!\n");
120 // FAIL TODO why does this happen?
124 tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
126 error("trace_endpos in solid!");
129 // ang_x stuff works around weird quake angles
130 if(player.classname == "player")
132 ang = player.v_angle;
134 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
135 ang = Portal_Transform_Multiply(transform, ang);
138 // PLAYERS use different math
141 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
143 fixedmakevectors(ang);
144 old_forward = v_forward;
146 fixedmakevectors(ang_y * '0 1 0');
147 old_yawforward = v_forward;
149 // their aiming directions are portalled...
150 new_forward = Portal_Transform_Apply(transform, old_forward);
151 new_up = Portal_Transform_Apply(transform, old_up);
152 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
154 // but now find a new sense of direction
156 // assume new_forward points straight up.
159 // new_up could now point forward OR backward... which direction to choose?
161 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
163 // new_yawforward and new_yawup define the new aiming half-circle
164 // we "just" need to find out whether new_up or -new_up is in that half circle
165 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
166 if(new_up * new_yawforward < 0)
167 new_up = -1 * new_up;
168 ang_y = vectoyaw(new_up); // this vector is the yaw we want
169 //print("UP/DOWN path: ", vtos(ang), "\n");
173 // good angles; here, "forward" suffices
174 ang = fixedvectoangles(new_forward);
175 //print("GOOD path: ", vtos(ang), "\n");
178 ang_z = player.angles_z;
184 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);
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 if(trace_fraction < 1)
226 return; // only handle TouchAreaGrid ones (only these can teleport)
227 // for some unknown reason, this also gets collisions from SV_Impact sometimes
230 objerror("Portal_Touch called for [unconnected] portal\n");
233 if(other.classname == "porto")
235 if(other.portal_id == self.portal_id)
238 if(time < self.portal_activatetime)
239 if(other == self.owner)
241 self.portal_activatetime = time + 0.1;
244 if(other != self.owner)
245 if(other.classname == "player")
246 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
247 return; // cannot go through someone else's portal
248 if(other.owner != self.owner)
249 if(other.owner.classname == "player")
250 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
251 return; // cannot go through someone else's portal
252 fixedmakevectors(self.angles);
253 if((other.origin - self.origin) * v_forward < 0)
255 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
256 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
258 // can't teleport this
262 if(Portal_TeleportPlayer(self, other))
263 if(other.classname == "porto")
264 if(other.effects & EF_RED)
265 other.effects += EF_BLUE - EF_RED;
269 void Portal_MakeBrokenPortal(entity portal)
271 portal.solid = SOLID_NOT;
272 portal.touch = SUB_Null;
273 portal.think = SUB_Null;
275 //portal.colormod = '1 1 1';
276 portal.nextthink = 0;
277 portal.takedamage = DAMAGE_NO;
280 void Portal_MakeWaitingPortal(entity portal)
282 portal.solid = SOLID_NOT;
283 portal.touch = SUB_Null;
284 portal.think = SUB_Null;
285 portal.effects = EF_ADDITIVE;
286 portal.colormod = '1 1 1';
287 portal.nextthink = 0;
288 portal.takedamage = DAMAGE_YES;
291 void Portal_MakeInPortal(entity portal)
293 portal.solid = SOLID_TRIGGER;
294 portal.touch = Portal_Touch;
295 portal.think = Portal_Think;
296 portal.effects = EF_RED;
297 portal.colormod = '1 0 0';
298 portal.nextthink = time;
299 portal.takedamage = DAMAGE_NO;
302 void Portal_MakeOutPortal(entity portal)
304 portal.solid = SOLID_NOT;
305 portal.touch = SUB_Null;
306 portal.think = SUB_Null;
307 portal.effects = EF_STARDUST | EF_BLUE;
308 portal.colormod = '0 0 1';
309 portal.nextthink = 0;
310 portal.takedamage = DAMAGE_YES;
313 void Portal_Disconnect(entity teleporter, entity destination)
315 teleporter.enemy = world;
316 destination.enemy = world;
317 Portal_MakeBrokenPortal(teleporter);
318 Portal_MakeBrokenPortal(destination);
321 void Portal_Connect(entity teleporter, entity destination)
323 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
327 // let's verify the transform
328 vector in_f, in_r, in_u;
329 vector out_f, out_r, out_u;
330 fixedmakevectors(teleporter.angles);
334 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
335 fixedmakevectors(destination.angles);
339 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
340 // INTENDED TRANSFORM:
344 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
345 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
346 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
348 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
349 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
353 teleporter.enemy = destination;
354 destination.enemy = teleporter;
355 Portal_MakeInPortal(teleporter);
356 Portal_MakeOutPortal(destination);
357 teleporter.fade_time = time + 15;
358 destination.fade_time = time + 15;
359 teleporter.portal_wants_to_vanish = 0;
360 destination.portal_wants_to_vanish = 0;
363 void Portal_Remove(entity portal, float killed)
370 Portal_Disconnect(portal, e);
371 Portal_Remove(e, killed);
374 if(portal == portal.owner.portal_in)
375 portal.owner.portal_in = world;
376 if(portal == portal.owner.portal_out)
377 portal.owner.portal_out = world;
378 portal.owner = world;
380 // makes the portal vanish
383 fixedmakevectors(portal.angles);
384 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
389 Portal_MakeBrokenPortal(portal);
390 SUB_SetFade(portal, time, 0.5);
394 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
396 if(deathtype == DEATH_TELEFRAG)
398 if(attacker != self.owner)
399 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
401 self.health -= damage;
404 Portal_Remove(self, 1);
412 if(self.solid != SOLID_TRIGGER)
413 error("Portal_Think called for a portal that should not be thinking");
416 self.solid = SOLID_BBOX;
420 if(time < self.portal_activatetime)
424 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
425 continue; // cannot go through someone else's portal
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);
432 self.solid = SOLID_TRIGGER;
435 self.nextthink = time;
437 if(time > self.fade_time)
438 Portal_Remove(self, 0);
441 float Portal_Customize()
443 if(other.classname == "spectator")
445 if(other == self.owner)
447 if(IS_INDEPENDENT_PLAYER(other))
449 if(IS_INDEPENDENT_PLAYER(self.owner))
455 // when creating in-portal:
457 // clear existing in-portal
460 // when creating out-portal:
462 // clear existing out-portal
465 // disconnect portals
466 // clear both portals
467 // after timeout of in-portal:
468 // disconnect portals
469 // clear both portals
470 // TODO: ensure only one portal shot at once
471 float Portal_SetInPortal(entity own, entity portal)
476 Portal_Disconnect(own.portal_in, own.portal_out);
477 Portal_Remove(own.portal_in, 0);
479 own.portal_in = portal;
482 own.portal_out.portal_id = portal.portal_id;
483 Portal_Connect(own.portal_in, own.portal_out);
487 float Portal_SetOutPortal(entity own, entity portal)
492 Portal_Disconnect(own.portal_in, own.portal_out);
493 Portal_Remove(own.portal_out, 0);
495 own.portal_out = portal;
498 own.portal_in.portal_id = portal.portal_id;
499 Portal_Connect(own.portal_in, own.portal_out);
503 void Portal_ClearAll(entity own)
506 Portal_Remove(own.portal_in, 0);
508 Portal_Remove(own.portal_out, 0);
510 void Portal_ClearWithID(entity own, float id)
513 if(own.portal_in.portal_id == id)
516 Portal_Disconnect(own.portal_in, own.portal_out);
517 Portal_Remove(own.portal_in, 0);
520 if(own.portal_out.portal_id == id)
523 Portal_Disconnect(own.portal_in, own.portal_out);
524 Portal_Remove(own.portal_out, 0);
528 entity Portal_Spawn(entity own, vector org, vector ang)
532 fixedmakevectors(ang);
533 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
537 portal.classname = "portal";
541 portal.think = Portal_Think;
542 portal.nextthink = 0;
543 portal.fade_time = time + 15;
544 portal.portal_activatetime = time + 0.1;
545 portal.event_damage = Portal_Damage;
547 setmodel(portal, "models/portal.md3");
548 portal.customizeentityforclient = Portal_Customize;
550 if(!Portal_FindSafeOrigin(portal))
556 setsize(portal, '-48 -48 -48', '48 48 48');
557 Portal_MakeWaitingPortal(portal);
562 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
568 if(trace_ent.movetype == MOVETYPE_WALK)
570 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
571 trace_plane_normal = '0 0 1';
572 dir = -1 * dir; // create telefrag portals the other way round
576 ang = fixedvectoangles2(trace_plane_normal, dir);
577 fixedmakevectors(ang);
579 portal = Portal_Spawn(own, org, ang);
582 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
583 Portal_ClearAll(own);
587 portal.portal_id = portal_id_val;
588 Portal_SetInPortal(own, portal);
589 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
594 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
600 if(trace_ent.movetype == MOVETYPE_WALK)
602 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
603 trace_plane_normal = '0 0 1';
604 dir = -1 * dir; // create telefrag portals the other way round
608 ang = fixedvectoangles2(trace_plane_normal, dir);
609 fixedmakevectors(ang);
611 portal = Portal_Spawn(own, org, ang);
614 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
615 Portal_ClearAll(own);
619 portal.portal_id = portal_id_val;
620 Portal_SetOutPortal(own, portal);
621 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);