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);
22 m_up = Portal_Transform_Apply(t1, m_up);
23 return vectoangles2(m_forward, m_up);
26 vector Portal_Transform_Invert(vector transform)
28 vector i_forward, i_up;
29 fixedmakevectors(transform);
30 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
31 // but these are orthogonal unit vectors!
32 // so to invert, we can simply vectoangles the TRANSPOSED matrix
33 // TODO is this always -transform?
34 i_forward_x = v_forward_x;
35 i_forward_y = -v_right_x;
42 v = vectoangles2(i_forward, i_up);
43 print("Transform: ", vtos(transform), "\n");
44 print("Inverted: ", vtos(v), "\n");
45 print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
46 fixedmakevectors(Portal_Transform_Multiply(v, transform));
47 print("Verify: ", vtos(v_forward), "\n");
48 print("Verify: ", vtos(v_right), "\n");
49 print("Verify: ", vtos(v_up), "\n");
51 return vectoangles2(i_forward, i_up);
54 vector Portal_Transform_TurnDirection(vector transform)
56 // turn 180 degrees around v_up
57 // changes in-direction to out-direction
58 fixedmakevectors(transform);
59 return vectoangles2(-1 * v_forward, 1 * v_up);
62 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
64 return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
67 float PlayerEdgeDistance(entity p, vector v)
71 if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
72 if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
73 if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
79 float Portal_TeleportPlayer(entity teleporter, entity player)
81 vector from, to, safe, step, transform, ang, newvel;
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 = Portal_Transform_Multiply(transform, ang);
132 ang_z = player.angles_z;
138 ang = Portal_Transform_Multiply(transform, player.angles);
142 // factor -1 allows chaining portals, but may be weird
143 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
145 if(player.flagcarried)
146 DropFlag(player.flagcarried);
147 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
149 // reset fade counter
150 teleporter.portal_wants_to_vanish = 0;
151 teleporter.fade_time = time + 15;
152 teleporter.enemy.health = 300;
157 float Portal_FindSafeOrigin(entity portal)
161 portal.mins = PL_MIN - '8 8 8';
162 portal.maxs = PL_MAX + '8 8 8';
163 fixedmakevectors(portal.angles);
164 portal.origin += 16 * v_forward;
165 if(!move_out_of_solid(portal))
168 print("NO SAFE ORIGIN\n");
172 portal.portal_safe_origin = portal.origin;
173 setorigin(portal, o);
179 if(trace_fraction < 1)
180 return; // only handle TouchAreaGrid ones (only these can teleport)
181 // for some unknown reason, this also gets collisions from SV_Impact sometimes
184 objerror("Portal_Touch called for [unconnected] portal\n");
187 if(other.classname == "porto")
189 if(other.portal_id == self.portal_id)
192 if(time < self.portal_activatetime)
193 if(other == self.owner)
195 self.portal_activatetime = time + 0.1;
198 if(other != self.owner)
199 if(other.classname == "player")
200 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
201 return; // cannot go through someone else's portal
202 if(other.owner != self.owner)
203 if(other.owner.classname == "player")
204 if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
205 return; // cannot go through someone else's portal
206 fixedmakevectors(self.angles);
207 if((other.origin - self.origin) * v_forward < 0)
209 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
210 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
212 // can't teleport this
216 if(Portal_TeleportPlayer(self, other))
217 if(other.classname == "porto")
218 if(other.effects & EF_RED)
219 other.effects += EF_BLUE - EF_RED;
223 void Portal_MakeBrokenPortal(entity portal)
225 portal.solid = SOLID_NOT;
226 portal.touch = SUB_Null;
227 portal.think = SUB_Null;
229 //portal.colormod = '1 1 1';
230 portal.nextthink = 0;
231 portal.takedamage = DAMAGE_NO;
234 void Portal_MakeWaitingPortal(entity portal)
236 portal.solid = SOLID_NOT;
237 portal.touch = SUB_Null;
238 portal.think = SUB_Null;
239 portal.effects = EF_ADDITIVE;
240 portal.colormod = '1 1 1';
241 portal.nextthink = 0;
242 portal.takedamage = DAMAGE_YES;
245 void Portal_MakeInPortal(entity portal)
247 portal.solid = SOLID_TRIGGER;
248 portal.touch = Portal_Touch;
249 portal.think = Portal_Think;
250 portal.effects = EF_RED;
251 portal.colormod = '1 0 0';
252 portal.nextthink = time;
253 portal.takedamage = DAMAGE_NO;
256 void Portal_MakeOutPortal(entity portal)
258 portal.solid = SOLID_NOT;
259 portal.touch = SUB_Null;
260 portal.think = SUB_Null;
261 portal.effects = EF_STARDUST | EF_BLUE;
262 portal.colormod = '0 0 1';
263 portal.nextthink = 0;
264 portal.takedamage = DAMAGE_YES;
267 void Portal_Disconnect(entity teleporter, entity destination)
269 teleporter.enemy = world;
270 destination.enemy = world;
271 Portal_MakeBrokenPortal(teleporter);
272 Portal_MakeBrokenPortal(destination);
275 void Portal_Connect(entity teleporter, entity destination)
277 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
281 // let's verify the transform
282 vector in_f, in_r, in_u;
283 vector out_f, out_r, out_u;
284 fixedmakevectors(teleporter.angles);
288 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
289 fixedmakevectors(destination.angles);
293 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
294 // INTENDED TRANSFORM:
298 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
299 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
300 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
302 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
303 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
307 teleporter.enemy = destination;
308 destination.enemy = teleporter;
309 Portal_MakeInPortal(teleporter);
310 Portal_MakeOutPortal(destination);
311 teleporter.fade_time = time + 15;
312 destination.fade_time = time + 15;
313 teleporter.portal_wants_to_vanish = 0;
314 destination.portal_wants_to_vanish = 0;
317 void Portal_Remove(entity portal, float killed)
324 Portal_Disconnect(portal, e);
325 Portal_Remove(e, killed);
328 if(portal == portal.owner.portal_in)
329 portal.owner.portal_in = world;
330 if(portal == portal.owner.portal_out)
331 portal.owner.portal_out = world;
332 portal.owner = world;
334 // makes the portal vanish
337 fixedmakevectors(portal.angles);
338 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
343 Portal_MakeBrokenPortal(portal);
344 SUB_SetFade(portal, time, 0.5);
348 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
350 if(deathtype == DEATH_TELEFRAG)
352 if(attacker != self.owner)
353 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
355 self.health -= damage;
358 Portal_Remove(self, 1);
366 if(self.solid != SOLID_TRIGGER)
367 error("Portal_Think called for a portal that should not be thinking");
370 self.solid = SOLID_BBOX;
374 if(time < self.portal_activatetime)
378 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
379 continue; // cannot go through someone else's portal
380 // if e would hit the portal in a frame...
381 // already teleport him
382 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
383 if(trace_ent == self)
384 Portal_TeleportPlayer(self, e);
386 self.solid = SOLID_TRIGGER;
389 self.nextthink = time;
391 if(time > self.fade_time)
392 Portal_Remove(self, 0);
395 float Portal_Customize()
397 if(other.classname == "spectator")
399 if(other == self.owner)
401 if(IS_INDEPENDENT_PLAYER(other))
403 if(IS_INDEPENDENT_PLAYER(self.owner))
409 // when creating in-portal:
411 // clear existing in-portal
414 // when creating out-portal:
416 // clear existing out-portal
419 // disconnect portals
420 // clear both portals
421 // after timeout of in-portal:
422 // disconnect portals
423 // clear both portals
424 // TODO: ensure only one portal shot at once
425 float Portal_SetInPortal(entity own, entity portal)
430 Portal_Disconnect(own.portal_in, own.portal_out);
431 Portal_Remove(own.portal_in, 0);
433 own.portal_in = portal;
436 own.portal_out.portal_id = portal.portal_id;
437 Portal_Connect(own.portal_in, own.portal_out);
441 float Portal_SetOutPortal(entity own, entity portal)
446 Portal_Disconnect(own.portal_in, own.portal_out);
447 Portal_Remove(own.portal_out, 0);
449 own.portal_out = portal;
452 own.portal_in.portal_id = portal.portal_id;
453 Portal_Connect(own.portal_in, own.portal_out);
457 void Portal_ClearAll(entity own)
460 Portal_Remove(own.portal_in, 0);
462 Portal_Remove(own.portal_out, 0);
464 void Portal_ClearWithID(entity own, float id)
467 if(own.portal_in.portal_id == id)
470 Portal_Disconnect(own.portal_in, own.portal_out);
471 Portal_Remove(own.portal_in, 0);
474 if(own.portal_out.portal_id == id)
477 Portal_Disconnect(own.portal_in, own.portal_out);
478 Portal_Remove(own.portal_out, 0);
482 entity Portal_Spawn(entity own, vector org, vector ang)
486 fixedmakevectors(ang);
487 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
491 portal.classname = "portal";
495 portal.think = Portal_Think;
496 portal.nextthink = 0;
497 portal.fade_time = time + 15;
498 portal.portal_activatetime = time + 0.1;
499 portal.event_damage = Portal_Damage;
501 setmodel(portal, "models/portal.md3");
502 portal.customizeentityforclient = Portal_Customize;
504 if(!Portal_FindSafeOrigin(portal))
510 setsize(portal, '-48 -48 -48', '48 48 48');
511 Portal_MakeWaitingPortal(portal);
516 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
522 if(trace_ent.movetype == MOVETYPE_WALK)
524 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
525 trace_plane_normal = '0 0 1';
526 dir = -1 * dir; // create telefrag portals the other way round
530 ang = vectoangles2(trace_plane_normal, dir);
531 fixedmakevectors(ang);
533 portal = Portal_Spawn(own, org, ang);
536 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
537 Portal_ClearAll(own);
541 portal.portal_id = portal_id_val;
542 Portal_SetInPortal(own, portal);
543 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
548 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
554 if(trace_ent.movetype == MOVETYPE_WALK)
556 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
557 trace_plane_normal = '0 0 1';
558 dir = -1 * dir; // create telefrag portals the other way round
562 ang = vectoangles2(trace_plane_normal, dir);
563 fixedmakevectors(ang);
565 portal = Portal_Spawn(own, org, ang);
568 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
569 Portal_ClearAll(own);
573 portal.portal_id = portal_id_val;
574 Portal_SetOutPortal(own, portal);
575 sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);