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 Portal_Transform_Apply(vector transform, vector v)
10 fixedmakevectors(transform);
11 return v_forward * v_x
16 vector Portal_Transform_Multiply(vector t1, vector t2)
18 vector m_forward, m_up;
19 fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
20 m_forward = Portal_Transform_Apply(t1, m_forward);
21 m_up = Portal_Transform_Apply(t1, m_up);
22 return vectoangles2(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 vectoangles 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 = vectoangles2(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 vectoangles2(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 vectoangles2(-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;
77 void Portal_TeleportPlayer(entity teleporter, entity player)
79 vector from, to, safe, step, transform, ang, newvel;
81 from = teleporter.origin;
82 to = teleporter.enemy.origin;
83 transform = teleporter.portal_transform;
85 to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
86 // this now is INSIDE the plane... can't use that
89 fixedmakevectors(teleporter.enemy.angles);
91 // first shift it ON the plane if needed
92 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward);
94 to += v_forward * planeshift;
96 print("no planeshift?\n");
98 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
99 step = to + ((safe - to) * v_forward) * v_forward;
100 tracebox(safe, PL_MIN, PL_MAX, step, MOVE_NOMONSTERS, player);
103 bprint("'safe' teleport location is not safe!\n");
104 // FAIL TODO why does this happen?
108 tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
110 error("trace_endpos in solid!");
113 // ang_x stuff works around weird quake angles
114 if(player.classname == "player")
116 ang = player.v_angle;
118 ang = Portal_Transform_Multiply(transform, ang);
119 ang_z = player.angles_z;
125 ang = Portal_Transform_Multiply(transform, player.angles);
129 newvel = Portal_Transform_Apply(transform, player.velocity);
131 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
133 // reset fade counter
134 teleporter.portal_wants_to_vanish = 0;
135 teleporter.fade_time = time + 15;
138 float Portal_FindSafeOrigin(entity portal)
142 portal.mins = PL_MIN - '8 8 8';
143 portal.maxs = PL_MAX + '8 8 8';
144 fixedmakevectors(portal.angles);
145 portal.origin += 16 * v_forward;
146 if(!move_out_of_solid(portal))
149 print("NO SAFE ORIGIN\n");
153 portal.portal_safe_origin = portal.origin;
154 setorigin(portal, o);
160 if(other.classname == "porto")
162 if(time < self.portal_activatetime)
163 if(other == self.owner)
165 self.portal_activatetime = time + 0.1;
168 fixedmakevectors(self.angles);
169 if((other.origin - self.origin) * v_forward < 0)
171 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
172 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
174 // can't teleport this
177 Portal_TeleportPlayer(self, other);
180 void Portal_MakeBrokenPortal(entity portal)
182 portal.solid = SOLID_NOT;
183 portal.touch = SUB_Null;
185 //portal.colormod = '1 1 1';
186 portal.nextthink = 0;
187 portal.takedamage = DAMAGE_NO;
190 void Portal_MakeWaitingPortal(entity portal)
192 portal.solid = SOLID_NOT;
193 portal.touch = SUB_Null;
194 portal.effects = EF_ADDITIVE;
195 portal.colormod = '1 1 1';
196 portal.nextthink = 0;
197 portal.takedamage = DAMAGE_YES;
200 void Portal_MakeInPortal(entity portal)
202 portal.solid = SOLID_TRIGGER;
203 portal.touch = Portal_Touch;
204 portal.effects = EF_RED;
205 portal.colormod = '1 0 0';
206 portal.nextthink = time;
207 portal.takedamage = DAMAGE_NO;
210 void Portal_MakeOutPortal(entity portal)
212 portal.solid = SOLID_NOT;
213 portal.touch = SUB_Null;
214 portal.effects = EF_STARDUST | EF_BLUE;
215 portal.colormod = '0 0 1';
216 portal.nextthink = 0;
217 portal.takedamage = DAMAGE_YES;
220 void Portal_Disconnect(entity teleporter, entity destination)
222 teleporter.enemy = world;
223 destination.enemy = world;
224 Portal_MakeBrokenPortal(teleporter);
225 Portal_MakeBrokenPortal(destination);
228 void Portal_Connect(entity teleporter, entity destination)
230 teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
234 // let's verify the transform
235 vector in_f, in_r, in_u;
236 vector out_f, out_r, out_u;
237 fixedmakevectors(teleporter.angles);
241 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
242 fixedmakevectors(destination.angles);
246 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
247 // INTENDED TRANSFORM:
251 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
252 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
253 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
255 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
256 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
260 teleporter.enemy = destination;
261 destination.enemy = teleporter;
262 Portal_MakeInPortal(teleporter);
263 Portal_MakeOutPortal(destination);
264 teleporter.fade_time = time + 15;
265 destination.fade_time = time + 15;
266 teleporter.portal_wants_to_vanish = 0;
267 destination.portal_wants_to_vanish = 0;
270 void Portal_Remove(entity portal, float killed)
277 Portal_Disconnect(portal, e);
278 Portal_Remove(e, killed);
281 if(portal == portal.owner.portal_in)
282 portal.owner.portal_in = world;
283 if(portal == portal.owner.portal_out)
284 portal.owner.portal_out = world;
285 portal.owner = world;
287 // makes the portal vanish
290 fixedmakevectors(portal.angles);
291 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
296 Portal_MakeBrokenPortal(portal);
297 SUB_SetFade(portal, time, 0.5);
301 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
303 if(deathtype == DEATH_TELEFRAG)
305 self.health -= damage;
308 Portal_Remove(self, 1);
316 if(self.solid != SOLID_TRIGGER)
317 error("Portal_Think called for a portal that should not be thinking");
320 self.solid = SOLID_BBOX;
324 if(time < self.portal_activatetime)
327 // if e would hit the portal in a frame...
328 // already teleport him
329 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
330 if(trace_ent == self)
331 Portal_TeleportPlayer(self, e);
333 self.solid = SOLID_TRIGGER;
336 self.nextthink = time;
338 if(time > self.fade_time)
339 Portal_Remove(self, 0);
344 // when creating in-portal:
346 // clear existing out-portal
347 // make existing in-portal an out-portal and connect
349 // when creating out-portal:
351 // clear existing out-portal
354 // disconnect portals
355 // clear both portals
356 // after timeout of in-portal:
357 // disconnect portals
358 // clear both portals
359 // TODO: ensure only one portal shot at once
360 float Portal_SetInPortal(entity own, entity portal)
365 Portal_Disconnect(own.portal_in, own.portal_out);
366 Portal_Remove(own.portal_out, 0);
369 own.portal_out = own.portal_in;
370 own.portal_in = portal;
372 Portal_Connect(own.portal_in, own.portal_out);
375 float Portal_SetOutPortal(entity own, entity portal)
381 Portal_Disconnect(own.portal_in, own.portal_out);
382 Portal_Remove(own.portal_out, 0);
384 own.portal_out = portal;
385 Portal_Connect(own.portal_in, own.portal_out);
388 void Portal_ClearAll(entity own)
391 Portal_Remove(own.portal_in, 0);
393 Portal_Remove(own.portal_out, 0);
395 float Portal_VerifyPortal(entity own, vector org, vector ang)
397 fixedmakevectors(ang);
398 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
403 entity Portal_Spawn(entity own, vector org, vector ang)
407 portal.classname = "portal";
411 portal.think = Portal_Think;
412 portal.nextthink = 0;
413 portal.fade_time = time + 15;
414 portal.portal_activatetime = time + 0.1;
415 portal.event_damage = Portal_Damage;
417 setmodel(portal, "models/portal.md3");
419 if(!Portal_FindSafeOrigin(portal))
425 setsize(portal, '-48 -48 -48', '48 48 48');
426 Portal_MakeWaitingPortal(portal);
431 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
445 b = normalize(b - (b * a) * a);
446 print("f/u = ", vtos(a), " ", vtos(b), "\n");
447 a = vectoangles2(a, b);
448 print("ang = ", vtos(a), "\n");
450 print("f/u = ", vtos(v_forward), " ", vtos(v_up), "\n");
454 if(trace_ent.movetype == MOVETYPE_WALK)
456 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
457 trace_plane_normal = '0 0 1';
458 dir = -1 * dir; // create telefrag portals the other way round
462 ang = vectoangles2(trace_plane_normal, dir);
463 fixedmakevectors(ang);
465 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || !Portal_VerifyPortal(own, org, ang))
467 // cannot create a portal here
468 // clear all to make sure
469 Portal_ClearAll(own);
473 portal = Portal_Spawn(own, org, ang);
474 Portal_SetInPortal(own, portal);
479 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
485 if(trace_ent.movetype == MOVETYPE_WALK)
487 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
488 trace_plane_normal = '0 0 1';
489 dir = -1 * dir; // create telefrag portals the other way round
493 ang = vectoangles2(trace_plane_normal, dir);
494 fixedmakevectors(ang);
496 if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT) || !Portal_VerifyPortal(own, org, ang))
498 // cannot create a portal here
499 // clear all to make sure
500 Portal_ClearAll(own);
504 portal = Portal_Spawn(own, org, ang);
505 Portal_SetOutPortal(own, portal);