1 .vector portal_transform;
2 .vector portal_safe_origin;
3 .float portal_wants_to_vanish;
4 .float portal_activatetime;
6 .entity portal_in, portal_out;
8 vector fixedvectoangles(vector v)
16 vector fixedvectoangles2(vector v, vector w)
19 a = vectoangles2(v, w);
24 void fixedmakevectors(vector a)
30 vector Portal_Transform_Apply(vector transform, vector v)
32 fixedmakevectors(transform);
33 return v_forward * v_x
38 vector Portal_Transform_Multiply(vector t1, vector t2)
40 vector m_forward, m_up;
41 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
42 m_forward = Portal_Transform_Apply(t1, m_forward);
43 m_up = Portal_Transform_Apply(t1, m_up);
44 return fixedvectoangles2(m_forward, m_up);
47 vector Portal_Transform_Invert(vector transform)
49 vector i_forward, i_up;
50 fixedmakevectors(transform);
51 // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
52 // but these are orthogonal unit vectors!
53 // so to invert, we can simply vectoangles the TRANSPOSED matrix
54 // TODO is this always -transform?
55 i_forward_x = v_forward_x;
56 i_forward_y = -v_right_x;
63 v = fixedvectoangles2(i_forward, i_up);
64 print("Transform: ", vtos(transform), "\n");
65 print("Inverted: ", vtos(v), "\n");
66 print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
67 fixedmakevectors(Portal_Transform_Multiply(v, transform));
68 print("Verify: ", vtos(v_forward), "\n");
69 print("Verify: ", vtos(v_right), "\n");
70 print("Verify: ", vtos(v_up), "\n");
72 return fixedvectoangles2(i_forward, i_up);
75 vector Portal_Transform_TurnDirection(vector transform)
78 t_angles_x = -transform_x;
79 t_angles_y = mod(transform_y + 180, 360);
80 t_angles_z = -transform_z;
84 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
86 return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
89 void Portal_TeleportPlayer(entity teleporter, entity player)
91 vector from, to, safe, step, transform, ang;
92 from = teleporter.origin;
93 to = teleporter.enemy.origin;
94 transform = teleporter.portal_transform;
96 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
97 // this now is INSIDE the plane... can't use that
100 fixedmakevectors(teleporter.enemy.mangle);
101 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
102 step = to + ((safe - to) * v_forward) * v_forward;
103 tracebox(safe, PL_MIN, PL_MAX, step, MOVE_NOMONSTERS, player);
106 bprint("'safe' teleport location is not safe!\n");
107 // FAIL TODO why does this happen?
111 tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
113 error("trace_endpos in solid!");
116 if(player.classname == "player")
118 ang = Portal_Transform_Multiply(transform, player.v_angle);
119 ang_z = player.angles_z;
123 ang = Portal_Transform_Multiply(transform, player.mangle);
126 TeleportPlayer(teleporter, player, to, ang, Portal_Transform_Apply(transform, player.velocity), teleporter.enemy.absmin, teleporter.enemy.absmax);
128 // reset fade counter
129 teleporter.portal_wants_to_vanish = 0;
130 teleporter.fade_time = time + 10;
133 float Portal_FindSafeOrigin(entity portal)
137 portal.mins = PL_MIN - '8 8 8';
138 portal.maxs = PL_MAX + '8 8 8';
139 if(!move_out_of_solid(portal))
141 print("NO SAFE ORIGIN\n");
144 portal.portal_safe_origin = portal.origin;
145 setorigin(portal, o);
151 if(other.classname == "porto")
153 if(time < self.portal_activatetime)
154 if(other == self.owner)
156 self.portal_activatetime = time + 0.1;
159 fixedmakevectors(self.mangle);
160 if((other.origin - self.origin) * v_forward < 0)
162 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
163 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
165 // can't teleport this
168 Portal_TeleportPlayer(self, other);
171 void Portal_MakeBrokenPortal(entity portal)
173 portal.solid = SOLID_NOT;
174 portal.touch = SUB_Null;
176 portal.colormod = '1 1 1';
177 portal.nextthink = 0;
180 void Portal_MakeInPortal(entity portal)
182 portal.solid = SOLID_TRIGGER;
183 portal.touch = Portal_Touch;
184 portal.effects = EF_RED;
185 portal.colormod = '1 0 0';
186 portal.nextthink = time;
189 void Portal_MakeOutPortal(entity portal)
191 portal.solid = SOLID_NOT;
192 portal.touch = SUB_Null;
193 portal.effects = EF_STARDUST;
194 portal.colormod = '0 0 1';
195 portal.nextthink = 0;
198 void Portal_Disconnect(entity teleporter, entity destination)
200 teleporter.enemy = world;
201 destination.enemy = world;
202 Portal_MakeBrokenPortal(teleporter);
203 Portal_MakeBrokenPortal(destination);
206 void Portal_Connect(entity teleporter, entity destination)
208 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.mangle), teleporter.mangle);
209 teleporter.enemy = destination;
210 destination.enemy = teleporter;
211 Portal_MakeInPortal(teleporter);
212 Portal_MakeOutPortal(destination);
213 teleporter.fade_time = time + 10;
214 destination.fade_time = time + 10;
215 teleporter.portal_wants_to_vanish = 0;
216 destination.portal_wants_to_vanish = 0;
219 void Portal_Remove(entity portal)
226 Portal_Disconnect(portal, e);
230 if(portal == portal.owner.portal_in)
231 portal.owner.portal_in = world;
232 if(portal == portal.owner.portal_out)
233 portal.owner.portal_out = world;
234 portal.owner = world;
236 // makes the portal vanish
237 Portal_MakeBrokenPortal(portal);
238 SUB_SetFade(portal, time, 0.5);
245 if(self.solid != SOLID_TRIGGER)
246 error("Portal_Think called for a portal that should not be thinking");
249 self.solid = SOLID_BBOX;
253 if(time < self.portal_activatetime)
256 // if e would hit the portal in a frame...
257 // already teleport him
258 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 3 * frametime, MOVE_NORMAL, e);
259 if(trace_ent == self)
260 Portal_TeleportPlayer(self, e);
262 self.solid = SOLID_TRIGGER;
265 self.nextthink = time;
267 if(time > self.fade_time)
273 // when creating in-portal:
275 // clear existing out-portal
276 // make existing in-portal an out-portal and connect
278 // when creating out-portal:
280 // clear existing out-portal
283 // disconnect portals
284 // clear both portals
285 // after timeout of in-portal:
286 // disconnect portals
287 // clear both portals
288 // TODO: ensure only one portal shot at once
289 float Portal_SetInPortal(entity own, entity portal)
292 Portal_Remove(own.portal_out);
294 own.portal_out = own.portal_in;
295 own.portal_in = portal;
297 Portal_Connect(own.portal_in, own.portal_out);
300 float Portal_SetOutPortal(entity own, entity portal)
305 Portal_Remove(own.portal_out);
306 own.portal_out = portal;
307 Portal_Connect(own.portal_in, own.portal_out);
310 void Portal_ClearAll(entity own)
313 Portal_Remove(own.portal_in);
315 Portal_Remove(own.portal_out);
317 float Portal_VerifyPortalAtTrace(vector ang)
319 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
321 fixedmakevectors(ang);
322 if(!CheckWireframeBox(trace_endpos - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 16 * v_forward))
327 entity Portal_Spawn(entity own, vector org, vector ang)
331 portal.classname = "portal";
337 portal.think = Portal_Think;
338 portal.nextthink = time;
339 portal.fade_time = time + 10;
340 portal.portal_activatetime = time + 0.1;
341 setmodel(portal, "models/portal.md3");
343 if(!Portal_FindSafeOrigin(portal))
349 setsize(portal, '-48 -48 -48', '48 48 48');
354 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
359 if(trace_ent.classname == "player")
361 print("hit a player, adjusting...\n");
362 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
363 trace_plane_normal = '0 0 1';
366 if(!Portal_VerifyPortalAtTrace(dir))
368 // cannot create a portal here
369 // clear all to make sure
370 Portal_ClearAll(own);
374 ang = fixedvectoangles2(trace_plane_normal, dir);
375 portal = Portal_Spawn(own, trace_endpos, ang);
376 Portal_SetInPortal(own, portal);
381 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
386 if(trace_ent.classname == "player")
388 print("hit a player, adjusting...\n");
389 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
390 trace_plane_normal = '0 0 1';
393 dir = -1 * dir; // invert the sense of the second portal
395 if(!Portal_VerifyPortalAtTrace(dir))
397 // cannot create a portal here
398 // clear all to make sure
399 Portal_ClearAll(own);
403 ang = fixedvectoangles2(trace_plane_normal, dir);
404 portal = Portal_Spawn(own, trace_endpos, ang);
405 Portal_SetOutPortal(own, portal);