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 fixedmakevectors(portal.mangle);
140 portal.origin += 16 * v_forward;
141 if(!move_out_of_solid(portal))
143 print("NO SAFE ORIGIN\n");
146 portal.portal_safe_origin = portal.origin;
147 setorigin(portal, o);
153 if(other.classname == "porto")
155 if(time < self.portal_activatetime)
156 if(other == self.owner)
158 self.portal_activatetime = time + 0.1;
161 fixedmakevectors(self.mangle);
162 if((other.origin - self.origin) * v_forward < 0)
164 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
165 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
167 // can't teleport this
170 Portal_TeleportPlayer(self, other);
173 void Portal_MakeBrokenPortal(entity portal)
175 portal.solid = SOLID_NOT;
176 portal.touch = SUB_Null;
178 //portal.colormod = '1 1 1';
179 portal.nextthink = 0;
182 void Portal_MakeWaitingPortal(entity portal)
184 portal.solid = SOLID_NOT;
185 portal.touch = SUB_Null;
186 portal.effects = EF_ADDITIVE;
187 portal.colormod = '1 1 1';
188 portal.nextthink = 0;
191 void Portal_MakeInPortal(entity portal)
193 portal.solid = SOLID_TRIGGER;
194 portal.touch = Portal_Touch;
195 portal.effects = EF_RED;
196 portal.colormod = '1 0 0';
197 portal.nextthink = time;
200 void Portal_MakeOutPortal(entity portal)
202 portal.solid = SOLID_NOT;
203 portal.touch = SUB_Null;
204 portal.effects = EF_STARDUST | EF_BLUE;
205 portal.colormod = '0 0 1';
206 portal.nextthink = 0;
209 void Portal_Disconnect(entity teleporter, entity destination)
211 teleporter.enemy = world;
212 destination.enemy = world;
213 Portal_MakeBrokenPortal(teleporter);
214 Portal_MakeBrokenPortal(destination);
217 void Portal_Connect(entity teleporter, entity destination)
219 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.mangle), teleporter.mangle);
220 teleporter.enemy = destination;
221 destination.enemy = teleporter;
222 Portal_MakeInPortal(teleporter);
223 Portal_MakeOutPortal(destination);
224 teleporter.fade_time = time + 10;
225 destination.fade_time = time + 10;
226 teleporter.portal_wants_to_vanish = 0;
227 destination.portal_wants_to_vanish = 0;
230 void Portal_Remove(entity portal)
237 Portal_Disconnect(portal, e);
241 if(portal == portal.owner.portal_in)
242 portal.owner.portal_in = world;
243 if(portal == portal.owner.portal_out)
244 portal.owner.portal_out = world;
245 portal.owner = world;
247 // makes the portal vanish
248 Portal_MakeBrokenPortal(portal);
249 SUB_SetFade(portal, time, 0.5);
256 if(self.solid != SOLID_TRIGGER)
257 error("Portal_Think called for a portal that should not be thinking");
260 self.solid = SOLID_BBOX;
264 if(time < self.portal_activatetime)
267 // if e would hit the portal in a frame...
268 // already teleport him
269 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
270 if(trace_ent == self)
271 Portal_TeleportPlayer(self, e);
273 self.solid = SOLID_TRIGGER;
276 self.nextthink = time;
278 if(time > self.fade_time)
284 // when creating in-portal:
286 // clear existing out-portal
287 // make existing in-portal an out-portal and connect
289 // when creating out-portal:
291 // clear existing out-portal
294 // disconnect portals
295 // clear both portals
296 // after timeout of in-portal:
297 // disconnect portals
298 // clear both portals
299 // TODO: ensure only one portal shot at once
300 float Portal_SetInPortal(entity own, entity portal)
303 Portal_Remove(own.portal_out);
305 own.portal_out = own.portal_in;
306 own.portal_in = portal;
308 Portal_Connect(own.portal_in, own.portal_out);
311 float Portal_SetOutPortal(entity own, entity portal)
316 Portal_Remove(own.portal_out);
317 own.portal_out = portal;
318 Portal_Connect(own.portal_in, own.portal_out);
321 void Portal_ClearAll(entity own)
324 Portal_Remove(own.portal_in);
326 Portal_Remove(own.portal_out);
328 float Portal_VerifyPortal(vector org, vector ang)
330 fixedmakevectors(ang);
331 if(!CheckWireframeBox(org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 16 * v_forward))
336 entity Portal_Spawn(entity own, vector org, vector ang)
340 portal.classname = "portal";
346 portal.think = Portal_Think;
347 portal.nextthink = 0;
348 portal.fade_time = time + 10;
349 portal.portal_activatetime = time + 0.1;
350 setmodel(portal, "models/portal.md3");
352 if(!Portal_FindSafeOrigin(portal))
358 setsize(portal, '-48 -48 -48', '48 48 48');
359 Portal_MakeWaitingPortal(portal);
364 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
370 if(trace_ent.classname == "player")
372 print("hit a player, adjusting...\n");
373 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
374 trace_plane_normal = '0 0 1';
378 ang = fixedvectoangles2(trace_plane_normal, dir);
380 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || !Portal_VerifyPortal(org, ang))
382 // cannot create a portal here
383 // clear all to make sure
384 Portal_ClearAll(own);
388 portal = Portal_Spawn(own, org, ang);
389 Portal_SetInPortal(own, portal);
394 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
400 if(trace_ent.classname == "player")
402 print("hit a player, adjusting...\n");
403 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
404 trace_plane_normal = '0 0 1';
407 dir = -1 * dir; // invert the sense of the second portal
410 ang = fixedvectoangles2(trace_plane_normal, dir);
412 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || !Portal_VerifyPortal(org, ang))
414 // cannot create a portal here
415 // clear all to make sure
416 Portal_ClearAll(own);
420 portal = Portal_Spawn(own, org, ang);
421 Portal_SetOutPortal(own, portal);