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 old_forward, new_forward;
82 float planeshift, s, t;
83 from = teleporter.origin;
84 transform = teleporter.portal_transform;
86 to = teleporter.enemy.origin;
87 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
88 newvel = Portal_Transform_Apply(transform, player.velocity);
89 // this now is INSIDE the plane... can't use that
92 fixedmakevectors(teleporter.enemy.angles);
94 // first shift it ON the plane if needed
95 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
96 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
97 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
98 to += newvel * (planeshift / (newvel * v_forward));
100 to += trace_plane_normal * planeshift;
102 s = (to - teleporter.enemy.origin) * v_right;
103 t = (to - teleporter.enemy.origin) * v_up;
104 s = bound(-48, s, 48);
105 t = bound(-48, t, 48);
106 to = teleporter.enemy.origin
107 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
111 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
112 step = to + ((safe - to) * v_forward) * v_forward;
113 tracebox(safe, PL_MIN, PL_MAX, step, MOVE_NOMONSTERS, player);
116 bprint("'safe' teleport location is not safe!\n");
117 // FAIL TODO why does this happen?
121 tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
123 error("trace_endpos in solid!");
126 // ang_x stuff works around weird quake angles
127 if(player.classname == "player")
129 ang = player.v_angle;
131 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
132 ang = Portal_Transform_Multiply(transform, ang);
135 // PLAYERS use different math
138 old_forward = v_forward;
141 // their forward aiming direction is portalled...
142 new_forward = Portal_Transform_Multiply(transform, old_forward);
143 new_up = Portal_Transform_Multiply(transform, old_up);
145 // but their "up" direction is difficult
146 // it must always go up...
147 // but if I used vectoangles2 on these now, it'd put part of it in the "roll" component
150 if(new_forward_z > 0.9) // far up; in this case, the "up" vector points backwards
152 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
153 ang_y = vectoyaw(-1 * new_up); // this vector is the yaw we want
155 else if(new_forward_z < -0.9) // far down; in this case, the "up" vector points forward
157 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
158 ang_y = vectoyaw(new_up); // this vector is the yaw we want
163 ang = fixedvectoangles2(new_forward, new_up);
166 ang_z = player.angles_z;
172 ang = Portal_Transform_Multiply(transform, player.angles);
176 // factor -1 allows chaining portals, but may be weird
177 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
179 if(player.flagcarried)
180 DropFlag(player.flagcarried);
181 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
183 // reset fade counter
184 teleporter.portal_wants_to_vanish = 0;
185 teleporter.fade_time = time + 15;
186 teleporter.enemy.health = 300;
191 float Portal_FindSafeOrigin(entity portal)
195 portal.mins = PL_MIN - '8 8 8';
196 portal.maxs = PL_MAX + '8 8 8';
197 fixedmakevectors(portal.angles);
198 portal.origin += 16 * v_forward;
199 if(!move_out_of_solid(portal))
202 print("NO SAFE ORIGIN\n");
206 portal.portal_safe_origin = portal.origin;
207 setorigin(portal, o);
213 if(trace_fraction < 1)
214 return; // only handle TouchAreaGrid ones (only these can teleport)
215 // for some unknown reason, this also gets collisions from SV_Impact sometimes
218 objerror("Portal_Touch called for [unconnected] portal\n");
221 if(other.classname == "porto")
223 if(other.portal_id == self.portal_id)
226 if(time < self.portal_activatetime)
227 if(other == self.owner)
229 self.portal_activatetime = time + 0.1;
232 if(other != self.owner)
233 if(other.classname == "player")
234 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
235 return; // cannot go through someone else's portal
236 if(other.owner != self.owner)
237 if(other.owner.classname == "player")
238 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
239 return; // cannot go through someone else's portal
240 fixedmakevectors(self.angles);
241 if((other.origin - self.origin) * v_forward < 0)
243 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
244 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
246 // can't teleport this
250 if(Portal_TeleportPlayer(self, other))
251 if(other.classname == "porto")
252 if(other.effects & EF_RED)
253 other.effects += EF_BLUE - EF_RED;
257 void Portal_MakeBrokenPortal(entity portal)
259 portal.solid = SOLID_NOT;
260 portal.touch = SUB_Null;
261 portal.think = SUB_Null;
263 //portal.colormod = '1 1 1';
264 portal.nextthink = 0;
265 portal.takedamage = DAMAGE_NO;
268 void Portal_MakeWaitingPortal(entity portal)
270 portal.solid = SOLID_NOT;
271 portal.touch = SUB_Null;
272 portal.think = SUB_Null;
273 portal.effects = EF_ADDITIVE;
274 portal.colormod = '1 1 1';
275 portal.nextthink = 0;
276 portal.takedamage = DAMAGE_YES;
279 void Portal_MakeInPortal(entity portal)
281 portal.solid = SOLID_TRIGGER;
282 portal.touch = Portal_Touch;
283 portal.think = Portal_Think;
284 portal.effects = EF_RED;
285 portal.colormod = '1 0 0';
286 portal.nextthink = time;
287 portal.takedamage = DAMAGE_NO;
290 void Portal_MakeOutPortal(entity portal)
292 portal.solid = SOLID_NOT;
293 portal.touch = SUB_Null;
294 portal.think = SUB_Null;
295 portal.effects = EF_STARDUST | EF_BLUE;
296 portal.colormod = '0 0 1';
297 portal.nextthink = 0;
298 portal.takedamage = DAMAGE_YES;
301 void Portal_Disconnect(entity teleporter, entity destination)
303 teleporter.enemy = world;
304 destination.enemy = world;
305 Portal_MakeBrokenPortal(teleporter);
306 Portal_MakeBrokenPortal(destination);
309 void Portal_Connect(entity teleporter, entity destination)
311 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
315 // let's verify the transform
316 vector in_f, in_r, in_u;
317 vector out_f, out_r, out_u;
318 fixedmakevectors(teleporter.angles);
322 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
323 fixedmakevectors(destination.angles);
327 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
328 // INTENDED TRANSFORM:
332 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
333 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
334 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
336 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
337 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
341 teleporter.enemy = destination;
342 destination.enemy = teleporter;
343 Portal_MakeInPortal(teleporter);
344 Portal_MakeOutPortal(destination);
345 teleporter.fade_time = time + 15;
346 destination.fade_time = time + 15;
347 teleporter.portal_wants_to_vanish = 0;
348 destination.portal_wants_to_vanish = 0;
351 void Portal_Remove(entity portal, float killed)
358 Portal_Disconnect(portal, e);
359 Portal_Remove(e, killed);
362 if(portal == portal.owner.portal_in)
363 portal.owner.portal_in = world;
364 if(portal == portal.owner.portal_out)
365 portal.owner.portal_out = world;
366 portal.owner = world;
368 // makes the portal vanish
371 fixedmakevectors(portal.angles);
372 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
377 Portal_MakeBrokenPortal(portal);
378 SUB_SetFade(portal, time, 0.5);
382 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
384 if(deathtype == DEATH_TELEFRAG)
386 if(attacker != self.owner)
387 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
389 self.health -= damage;
392 Portal_Remove(self, 1);
400 if(self.solid != SOLID_TRIGGER)
401 error("Portal_Think called for a portal that should not be thinking");
404 self.solid = SOLID_BBOX;
408 if(time < self.portal_activatetime)
412 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
413 continue; // cannot go through someone else's portal
414 // if e would hit the portal in a frame...
415 // already teleport him
416 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
417 if(trace_ent == self)
418 Portal_TeleportPlayer(self, e);
420 self.solid = SOLID_TRIGGER;
423 self.nextthink = time;
425 if(time > self.fade_time)
426 Portal_Remove(self, 0);
429 float Portal_Customize()
431 if(other.classname == "spectator")
433 if(other == self.owner)
435 if(IS_INDEPENDENT_PLAYER(other))
437 if(IS_INDEPENDENT_PLAYER(self.owner))
443 // when creating in-portal:
445 // clear existing in-portal
448 // when creating out-portal:
450 // clear existing out-portal
453 // disconnect portals
454 // clear both portals
455 // after timeout of in-portal:
456 // disconnect portals
457 // clear both portals
458 // TODO: ensure only one portal shot at once
459 float Portal_SetInPortal(entity own, entity portal)
464 Portal_Disconnect(own.portal_in, own.portal_out);
465 Portal_Remove(own.portal_in, 0);
467 own.portal_in = portal;
470 own.portal_out.portal_id = portal.portal_id;
471 Portal_Connect(own.portal_in, own.portal_out);
475 float Portal_SetOutPortal(entity own, entity portal)
480 Portal_Disconnect(own.portal_in, own.portal_out);
481 Portal_Remove(own.portal_out, 0);
483 own.portal_out = portal;
486 own.portal_in.portal_id = portal.portal_id;
487 Portal_Connect(own.portal_in, own.portal_out);
491 void Portal_ClearAll(entity own)
494 Portal_Remove(own.portal_in, 0);
496 Portal_Remove(own.portal_out, 0);
498 void Portal_ClearWithID(entity own, float id)
501 if(own.portal_in.portal_id == id)
504 Portal_Disconnect(own.portal_in, own.portal_out);
505 Portal_Remove(own.portal_in, 0);
508 if(own.portal_out.portal_id == id)
511 Portal_Disconnect(own.portal_in, own.portal_out);
512 Portal_Remove(own.portal_out, 0);
516 entity Portal_Spawn(entity own, vector org, vector ang)
520 fixedmakevectors(ang);
521 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
525 portal.classname = "portal";
529 portal.think = Portal_Think;
530 portal.nextthink = 0;
531 portal.fade_time = time + 15;
532 portal.portal_activatetime = time + 0.1;
533 portal.event_damage = Portal_Damage;
535 setmodel(portal, "models/portal.md3");
536 portal.customizeentityforclient = Portal_Customize;
538 if(!Portal_FindSafeOrigin(portal))
544 setsize(portal, '-48 -48 -48', '48 48 48');
545 Portal_MakeWaitingPortal(portal);
550 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
556 if(trace_ent.movetype == MOVETYPE_WALK)
558 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
559 trace_plane_normal = '0 0 1';
560 dir = -1 * dir; // create telefrag portals the other way round
564 ang = fixedvectoangles2(trace_plane_normal, dir);
565 fixedmakevectors(ang);
567 portal = Portal_Spawn(own, org, ang);
570 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
571 Portal_ClearAll(own);
575 portal.portal_id = portal_id_val;
576 Portal_SetInPortal(own, portal);
577 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
582 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
588 if(trace_ent.movetype == MOVETYPE_WALK)
590 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
591 trace_plane_normal = '0 0 1';
592 dir = -1 * dir; // create telefrag portals the other way round
596 ang = fixedvectoangles2(trace_plane_normal, dir);
597 fixedmakevectors(ang);
599 portal = Portal_Spawn(own, org, ang);
602 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
603 Portal_ClearAll(own);
607 portal.portal_id = portal_id_val;
608 Portal_SetOutPortal(own, portal);
609 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);