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);
97 to += newvel * (planeshift / (newvel * v_forward));
99 s = (to - teleporter.enemy.origin) * v_right;
100 t = (to - teleporter.enemy.origin) * v_up;
101 s = bound(-48, s, 48);
102 t = bound(-48, t, 48);
103 to = teleporter.enemy.origin
104 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
108 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
109 step = to + ((safe - to) * v_forward) * v_forward;
110 tracebox(safe, PL_MIN, PL_MAX, step, MOVE_NOMONSTERS, player);
113 bprint("'safe' teleport location is not safe!\n");
114 // FAIL TODO why does this happen?
118 tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
120 error("trace_endpos in solid!");
123 // ang_x stuff works around weird quake angles
124 if(player.classname == "player")
126 ang = player.v_angle;
128 ang = Portal_Transform_Multiply(transform, ang);
129 ang_z = player.angles_z;
135 ang = Portal_Transform_Multiply(transform, player.angles);
139 // factor -1 allows chaining portals, but may be weird
140 player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
142 if(player.flagcarried)
143 DropFlag(player.flagcarried);
144 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
146 // reset fade counter
147 teleporter.portal_wants_to_vanish = 0;
148 teleporter.fade_time = time + 15;
149 teleporter.enemy.health = 300;
154 float Portal_FindSafeOrigin(entity portal)
158 portal.mins = PL_MIN - '8 8 8';
159 portal.maxs = PL_MAX + '8 8 8';
160 fixedmakevectors(portal.angles);
161 portal.origin += 16 * v_forward;
162 if(!move_out_of_solid(portal))
165 print("NO SAFE ORIGIN\n");
169 portal.portal_safe_origin = portal.origin;
170 setorigin(portal, o);
176 if(other.classname == "porto")
178 if(other.portal_id == self.portal_id)
181 if(time < self.portal_activatetime)
182 if(other == self.owner)
184 self.portal_activatetime = time + 0.1;
187 if(other != self.owner)
188 if(other.classname == "player")
189 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
190 return; // cannot go through someone else's portal
191 fixedmakevectors(self.angles);
192 if((other.origin - self.origin) * v_forward < 0)
194 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
195 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
197 // can't teleport this
200 if(Portal_TeleportPlayer(self, other))
201 if(other.classname == "porto")
206 void Portal_MakeBrokenPortal(entity portal)
208 portal.solid = SOLID_NOT;
209 portal.touch = SUB_Null;
211 //portal.colormod = '1 1 1';
212 portal.nextthink = 0;
213 portal.takedamage = DAMAGE_NO;
216 void Portal_MakeWaitingPortal(entity portal)
218 portal.solid = SOLID_NOT;
219 portal.touch = SUB_Null;
220 portal.effects = EF_ADDITIVE;
221 portal.colormod = '1 1 1';
222 portal.nextthink = 0;
223 portal.takedamage = DAMAGE_YES;
226 void Portal_MakeInPortal(entity portal)
228 portal.solid = SOLID_TRIGGER;
229 portal.touch = Portal_Touch;
230 portal.effects = EF_RED;
231 portal.colormod = '1 0 0';
232 portal.nextthink = time;
233 portal.takedamage = DAMAGE_NO;
236 void Portal_MakeOutPortal(entity portal)
238 portal.solid = SOLID_NOT;
239 portal.touch = SUB_Null;
240 portal.effects = EF_STARDUST | EF_BLUE;
241 portal.colormod = '0 0 1';
242 portal.nextthink = 0;
243 portal.takedamage = DAMAGE_YES;
246 void Portal_Disconnect(entity teleporter, entity destination)
248 teleporter.enemy = world;
249 destination.enemy = world;
250 Portal_MakeBrokenPortal(teleporter);
251 Portal_MakeBrokenPortal(destination);
254 void Portal_Connect(entity teleporter, entity destination)
256 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
260 // let's verify the transform
261 vector in_f, in_r, in_u;
262 vector out_f, out_r, out_u;
263 fixedmakevectors(teleporter.angles);
267 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
268 fixedmakevectors(destination.angles);
272 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
273 // INTENDED TRANSFORM:
277 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
278 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
279 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
281 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
282 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
286 teleporter.enemy = destination;
287 destination.enemy = teleporter;
288 Portal_MakeInPortal(teleporter);
289 Portal_MakeOutPortal(destination);
290 teleporter.fade_time = time + 15;
291 destination.fade_time = time + 15;
292 teleporter.portal_wants_to_vanish = 0;
293 destination.portal_wants_to_vanish = 0;
296 void Portal_Remove(entity portal, float killed)
303 Portal_Disconnect(portal, e);
304 Portal_Remove(e, killed);
307 if(portal == portal.owner.portal_in)
308 portal.owner.portal_in = world;
309 if(portal == portal.owner.portal_out)
310 portal.owner.portal_out = world;
311 portal.owner = world;
313 // makes the portal vanish
316 fixedmakevectors(portal.angles);
317 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
322 Portal_MakeBrokenPortal(portal);
323 SUB_SetFade(portal, time, 0.5);
327 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
329 if(deathtype == DEATH_TELEFRAG)
331 self.health -= damage;
334 Portal_Remove(self, 1);
342 if(self.solid != SOLID_TRIGGER)
343 error("Portal_Think called for a portal that should not be thinking");
346 self.solid = SOLID_BBOX;
350 if(time < self.portal_activatetime)
354 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
355 continue; // cannot go through someone else's portal
356 // if e would hit the portal in a frame...
357 // already teleport him
358 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
359 if(trace_ent == self)
360 Portal_TeleportPlayer(self, e);
362 self.solid = SOLID_TRIGGER;
365 self.nextthink = time;
367 if(time > self.fade_time)
368 Portal_Remove(self, 0);
373 // when creating in-portal:
375 // clear existing in-portal
378 // when creating out-portal:
380 // clear existing out-portal
383 // disconnect portals
384 // clear both portals
385 // after timeout of in-portal:
386 // disconnect portals
387 // clear both portals
388 // TODO: ensure only one portal shot at once
389 float Portal_SetInPortal(entity own, entity portal)
394 Portal_Disconnect(own.portal_in, own.portal_out);
395 Portal_Remove(own.portal_in, 0);
397 own.portal_in = portal;
399 Portal_Connect(own.portal_in, own.portal_out);
402 float Portal_SetOutPortal(entity own, entity portal)
407 Portal_Disconnect(own.portal_in, own.portal_out);
408 Portal_Remove(own.portal_out, 0);
410 own.portal_out = portal;
412 Portal_Connect(own.portal_in, own.portal_out);
415 void Portal_ClearAll(entity own)
418 Portal_Remove(own.portal_in, 0);
420 Portal_Remove(own.portal_out, 0);
422 void Portal_ClearWithID(entity own, float id)
425 if(own.portal_in.portal_id == id)
428 Portal_Disconnect(own.portal_in, own.portal_out);
429 Portal_Remove(own.portal_in, 0);
432 if(own.portal_out.portal_id == id)
435 Portal_Disconnect(own.portal_in, own.portal_out);
436 Portal_Remove(own.portal_out, 0);
440 entity Portal_Spawn(entity own, vector org, vector ang)
444 fixedmakevectors(ang);
445 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
449 portal.classname = "portal";
453 portal.think = Portal_Think;
454 portal.nextthink = 0;
455 portal.fade_time = time + 15;
456 portal.portal_activatetime = time + 0.1;
457 portal.event_damage = Portal_Damage;
459 setmodel(portal, "models/portal.md3");
461 if(!Portal_FindSafeOrigin(portal))
467 setsize(portal, '-48 -48 -48', '48 48 48');
468 Portal_MakeWaitingPortal(portal);
473 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
479 if(trace_ent.movetype == MOVETYPE_WALK)
481 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
482 trace_plane_normal = '0 0 1';
483 dir = -1 * dir; // create telefrag portals the other way round
487 ang = vectoangles2(trace_plane_normal, dir);
488 fixedmakevectors(ang);
490 portal = Portal_Spawn(own, org, ang);
493 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
494 Portal_ClearAll(own);
498 portal.portal_id = portal_id_val;
499 Portal_SetInPortal(own, portal);
504 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
510 if(trace_ent.movetype == MOVETYPE_WALK)
512 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
513 trace_plane_normal = '0 0 1';
514 dir = -1 * dir; // create telefrag portals the other way round
518 ang = vectoangles2(trace_plane_normal, dir);
519 fixedmakevectors(ang);
521 portal = Portal_Spawn(own, org, ang);
524 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
525 Portal_ClearAll(own);
529 portal.portal_id = portal_id_val;
530 Portal_SetOutPortal(own, portal);