4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../warpzonelib/anglestransform.qh"
7 #include "../warpzonelib/util_server.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
10 #include "../common/weapons/weapons.qh"
11 #include "autocvars.qh"
13 #include "../common/notifications.qh"
14 #include "../common/deathtypes.qh"
15 #include "mutators/mutators_include.qh"
16 #include "../csqcmodellib/sv_model.qh"
21 #define PORTALS_ARE_NOT_SOLID
23 const vector SAFENUDGE = '1 1 1';
24 const vector SAFERNUDGE = '8 8 8';
26 .vector portal_transform;
27 .vector portal_safe_origin;
28 .float portal_wants_to_vanish;
29 .float portal_activatetime;
30 .float savemodelindex;
32 float PlayerEdgeDistance(entity p, vector v)
36 if(v.x < 0) vbest.x = p.mins.x; else vbest.x = p.maxs.x;
37 if(v.y < 0) vbest.y = p.mins.y; else vbest.y = p.maxs.y;
38 if(v.z < 0) vbest.z = p.mins.z; else vbest.z = p.maxs.z;
43 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
45 vector old_forward, old_up;
46 vector old_yawforward;
47 vector new_forward, new_up;
48 vector new_yawforward;
53 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
54 ang = AnglesTransform_ApplyToVAngles(transform, ang);
57 // PLAYERS use different math
58 #ifndef POSITIVE_PITCH_IS_DOWN
62 //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
64 fixedmakevectors(ang);
65 old_forward = v_forward;
67 fixedmakevectors(ang.y * '0 1 0');
68 old_yawforward = v_forward;
70 // their aiming directions are portalled...
71 new_forward = AnglesTransform_Apply(transform, old_forward);
72 new_up = AnglesTransform_Apply(transform, old_up);
73 new_yawforward = AnglesTransform_Apply(transform, old_yawforward);
75 // but now find a new sense of direction
77 // assume new_forward points straight up.
80 // new_up could now point forward OR backward... which direction to choose?
82 if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
84 // new_yawforward and new_yawup define the new aiming half-circle
85 // we "just" need to find out whether new_up or -new_up is in that half circle
86 ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
87 if(new_up * new_yawforward < 0)
89 ang.y = vectoyaw(new_up); // this vector is the yaw we want
90 //print("UP/DOWN path: ", vtos(ang), "\n");
94 // good angles; here, "forward" suffices
95 ang = fixedvectoangles(new_forward);
96 //print("GOOD path: ", vtos(ang), "\n");
99 #ifndef POSITIVE_PITCH_IS_DOWN
106 .vector right_vector;
107 float Portal_TeleportPlayer(entity teleporter, entity player)
109 vector from, to, safe, step, transform, ang, newvel;
110 float planeshift, s, t;
112 if (!teleporter.enemy)
114 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
118 from = teleporter.origin;
119 transform = teleporter.portal_transform;
121 to = teleporter.enemy.origin;
122 to = to + AnglesTransform_Apply(teleporter.portal_transform, player.origin - from);
123 newvel = AnglesTransform_Apply(transform, player.velocity);
124 // this now is INSIDE the plane... can't use that
127 fixedmakevectors(teleporter.enemy.mangle);
129 // first shift it ON the plane if needed
130 planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
132 if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
133 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
134 to += newvel * (planeshift / (newvel * v_forward));
137 to += v_forward * planeshift;
139 s = (to - teleporter.enemy.origin) * v_right;
140 t = (to - teleporter.enemy.origin) * v_up;
141 s = bound(-48, s, 48);
142 t = bound(-48, t, 48);
143 to = teleporter.enemy.origin
144 + ((to - teleporter.enemy.origin) * v_forward) * v_forward
148 safe = teleporter.enemy.portal_safe_origin; // a valid player origin
149 step = to + ((safe - to) * v_forward) * v_forward;
150 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
153 print("'safe' teleport location is not safe!\n");
154 // FAIL TODO why does this happen?
157 safe = trace_endpos + normalize(safe - trace_endpos) * 0;
158 tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
161 print("trace_endpos in solid, this can't be!\n");
162 // FAIL TODO why does this happen? (reported by MrBougo)
165 to = trace_endpos + normalize(safe - trace_endpos) * 0;
166 //print(vtos(to), "\n");
168 // ang_x stuff works around weird quake angles
169 if(IS_PLAYER(player))
170 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
172 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
174 // factor -1 allows chaining portals, but may be weird
175 player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
177 entity oldself = self;
179 MUTATOR_CALLHOOK(PortalTeleport);
183 if (!teleporter.enemy)
185 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
190 TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
193 // telefrag within 1 second of portal creation = amazing
194 if(time < teleporter.teleport_time + 1)
195 Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
198 if (!teleporter.enemy)
200 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
204 // reset fade counter
205 teleporter.portal_wants_to_vanish = 0;
206 teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
207 teleporter.health = autocvar_g_balance_portal_health;
208 teleporter.enemy.health = autocvar_g_balance_portal_health;
213 float Portal_FindSafeOrigin(entity portal)
217 portal.mins = PL_MIN - SAFERNUDGE;
218 portal.maxs = PL_MAX + SAFERNUDGE;
219 fixedmakevectors(portal.mangle);
220 portal.origin += 16 * v_forward;
221 if(!move_out_of_solid(portal))
224 print("NO SAFE ORIGIN\n");
228 portal.portal_safe_origin = portal.origin;
229 setorigin(portal, o);
233 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
235 float dist, distpersec, delta;
238 dist = (eorg - porg) * pnorm;
239 dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
240 dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
241 dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
242 if(dist < -1) // other side?
244 #ifdef PORTALS_ARE_NOT_SOLID
245 distpersec = evel * pnorm;
246 if(distpersec >= 0) // going away from the portal?
248 // we don't need this check with solid portals, them being SOLID_BSP should suffice
249 delta = dist / distpersec;
250 v = eorg - evel * delta - porg;
251 v = v - pnorm * (pnorm * v);
252 return vlen(v) < psize;
262 #ifdef PORTALS_ARE_NOT_SOLID
263 // portal is being removed?
264 if(self.solid != SOLID_TRIGGER)
265 return; // possibly engine bug
268 return; // handled by think
271 if(other.classname == "item_flag_team")
272 return; // never portal these
274 if(other.classname == "grapplinghook")
275 return; // handled by think
278 error("Portal_Touch called for a broken portal\n");
280 #ifdef PORTALS_ARE_NOT_SOLID
281 if(trace_fraction < 1)
282 return; // only handle TouchAreaGrid ones (only these can teleport)
284 if(trace_fraction >= 1)
285 return; // only handle impacts
288 if(other.classname == "porto")
290 if(other.portal_id == self.portal_id)
293 if(time < self.portal_activatetime)
294 if(other == self.aiment)
296 self.portal_activatetime = time + 0.1;
299 if(other != self.aiment)
301 if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
302 return; // cannot go through someone else's portal
303 if(other.aiment != self.aiment)
304 if(IS_PLAYER(other.aiment))
305 if(IS_INDEPENDENT_PLAYER(other.aiment) || IS_INDEPENDENT_PLAYER(self.aiment))
306 return; // cannot go through someone else's portal
307 fixedmakevectors(self.mangle);
308 g = frametime * '0 0 -1' * autocvar_sv_gravity;
309 if(!Portal_WillHitPlane(other.origin, other.mins, other.maxs, other.velocity + g, self.origin, v_forward, self.maxs.x))
313 if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
314 || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
316 // can't teleport this
321 if(Portal_TeleportPlayer(self, other))
322 if(other.classname == "porto")
323 if(other.effects & EF_RED)
324 other.effects += EF_BLUE - EF_RED;
328 void Portal_MakeBrokenPortal(entity portal)
331 portal.solid = SOLID_NOT;
332 portal.touch = func_null;
333 portal.think = func_null;
335 portal.nextthink = 0;
336 portal.takedamage = DAMAGE_NO;
339 void Portal_MakeWaitingPortal(entity portal)
342 portal.solid = SOLID_NOT;
343 portal.touch = func_null;
344 portal.think = func_null;
345 portal.effects = EF_ADDITIVE;
346 portal.nextthink = 0;
347 portal.takedamage = DAMAGE_YES;
350 void Portal_MakeInPortal(entity portal)
353 portal.solid = SOLID_NOT; // this is done when connecting them!
354 portal.touch = Portal_Touch;
355 portal.think = Portal_Think;
356 portal.effects = EF_RED;
357 portal.nextthink = time;
358 portal.takedamage = DAMAGE_NO;
361 void Portal_MakeOutPortal(entity portal)
364 portal.solid = SOLID_NOT;
365 portal.touch = func_null;
366 portal.think = func_null;
367 portal.effects = EF_STARDUST | EF_BLUE;
368 portal.nextthink = 0;
369 portal.takedamage = DAMAGE_YES;
372 void Portal_Disconnect(entity teleporter, entity destination)
374 teleporter.enemy = world;
375 destination.enemy = world;
376 Portal_MakeBrokenPortal(teleporter);
377 Portal_MakeBrokenPortal(destination);
380 void Portal_Connect(entity teleporter, entity destination)
382 teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
384 teleporter.enemy = destination;
385 destination.enemy = teleporter;
386 Portal_MakeInPortal(teleporter);
387 Portal_MakeOutPortal(destination);
388 teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
389 destination.fade_time = teleporter.fade_time;
390 teleporter.portal_wants_to_vanish = 0;
391 destination.portal_wants_to_vanish = 0;
392 teleporter.teleport_time = time;
393 #ifdef PORTALS_ARE_NOT_SOLID
394 teleporter.solid = SOLID_TRIGGER;
396 teleporter.solid = SOLID_BSP;
400 void Portal_Remove(entity portal, float killed)
407 Portal_Disconnect(portal, e);
408 Portal_Remove(e, killed);
411 if(portal == portal.aiment.portal_in)
412 portal.aiment.portal_in = world;
413 if(portal == portal.aiment.portal_out)
414 portal.aiment.portal_out = world;
415 //portal.aiment = world;
417 // makes the portal vanish
420 fixedmakevectors(portal.mangle);
421 sound(portal, CH_SHOTS, "porto/explode.wav", VOL_BASE, ATTEN_NORM);
422 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
427 Portal_MakeBrokenPortal(portal);
428 sound(portal, CH_SHOTS, "porto/expire.wav", VOL_BASE, ATTEN_NORM);
429 SUB_SetFade(portal, time, 0.5);
433 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
435 if(deathtype == DEATH_TELEFRAG)
437 if(attacker != self.aiment)
438 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.aiment))
440 self.health -= damage;
442 Portal_Remove(self, 1);
445 void Portal_Think_TryTeleportPlayer(entity e, vector g)
447 if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, self.origin, v_forward, self.maxs.x))
450 // if e would hit the portal in a frame...
451 // already teleport him
452 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
453 if(trace_ent == self)
454 Portal_TeleportPlayer(self, e);
462 #ifdef PORTALS_ARE_NOT_SOLID
463 // portal is being removed?
464 if(self.solid != SOLID_TRIGGER)
465 return; // possibly engine bug
468 error("Portal_Think called for a broken portal\n");
471 self.solid = SOLID_BBOX;
474 g = frametime * '0 0 -1' * autocvar_sv_gravity;
476 fixedmakevectors(self.mangle);
481 if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
482 continue; // cannot go through someone else's portal
484 if(e != o || time >= self.portal_activatetime)
485 Portal_Think_TryTeleportPlayer(e, g);
488 Portal_Think_TryTeleportPlayer(e.hook, g);
490 self.solid = SOLID_TRIGGER;
494 self.nextthink = time;
496 if(time > self.fade_time)
497 Portal_Remove(self, 0);
500 float Portal_Customize()
504 if(other == self.aiment)
506 self.modelindex = self.savemodelindex;
508 else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.aiment))
514 self.modelindex = self.savemodelindex;
520 // when creating in-portal:
522 // clear existing in-portal
525 // when creating out-portal:
527 // clear existing out-portal
530 // disconnect portals
531 // clear both portals
532 // after timeout of in-portal:
533 // disconnect portals
534 // clear both portals
535 // TODO: ensure only one portal shot at once
536 float Portal_SetInPortal(entity own, entity portal)
541 Portal_Disconnect(own.portal_in, own.portal_out);
542 Portal_Remove(own.portal_in, 0);
544 own.portal_in = portal;
547 own.portal_out.portal_id = portal.portal_id;
548 Portal_Connect(own.portal_in, own.portal_out);
552 float Portal_SetOutPortal(entity own, entity portal)
557 Portal_Disconnect(own.portal_in, own.portal_out);
558 Portal_Remove(own.portal_out, 0);
560 own.portal_out = portal;
563 own.portal_in.portal_id = portal.portal_id;
564 Portal_Connect(own.portal_in, own.portal_out);
568 void Portal_ClearAll_PortalsOnly(entity own)
571 Portal_Remove(own.portal_in, 0);
573 Portal_Remove(own.portal_out, 0);
575 void Portal_ClearAll(entity own)
577 Portal_ClearAll_PortalsOnly(own);
580 void Portal_RemoveLater_Think()
582 Portal_Remove(self, self.cnt);
584 void Portal_RemoveLater(entity portal, float kill)
586 Portal_MakeBrokenPortal(portal);
588 portal.think = Portal_RemoveLater_Think;
589 portal.nextthink = time;
591 void Portal_ClearAllLater_PortalsOnly(entity own)
594 Portal_RemoveLater(own.portal_in, 0);
596 Portal_RemoveLater(own.portal_out, 0);
598 void Portal_ClearAllLater(entity own)
600 Portal_ClearAllLater_PortalsOnly(own);
603 void Portal_ClearWithID(entity own, float id)
606 if(own.portal_in.portal_id == id)
609 Portal_Disconnect(own.portal_in, own.portal_out);
610 Portal_Remove(own.portal_in, 0);
613 if(own.portal_out.portal_id == id)
616 Portal_Disconnect(own.portal_in, own.portal_out);
617 Portal_Remove(own.portal_out, 0);
621 entity Portal_Spawn(entity own, vector org, vector ang)
625 fixedmakevectors(ang);
626 if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
630 portal.classname = "portal";
632 setorigin(portal, org);
635 portal.angles_x = -portal.angles.x; // is a bmodel
636 portal.think = Portal_Think;
637 portal.nextthink = 0;
638 portal.portal_activatetime = time + 0.1;
639 portal.takedamage = DAMAGE_AIM;
640 portal.event_damage = Portal_Damage;
641 portal.fade_time = time + autocvar_g_balance_portal_lifetime;
642 portal.health = autocvar_g_balance_portal_health;
643 setmodel(portal, "models/portal.md3");
644 portal.savemodelindex = portal.modelindex;
645 portal.customizeentityforclient = Portal_Customize;
647 if(!Portal_FindSafeOrigin(portal))
653 setsize(portal, '-48 -48 -48', '48 48 48');
654 Portal_MakeWaitingPortal(portal);
659 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
666 ang = fixedvectoangles2(trace_plane_normal, dir);
667 fixedmakevectors(ang);
669 portal = Portal_Spawn(own, org, ang);
673 portal.portal_id = portal_id_val;
674 Portal_SetInPortal(own, portal);
679 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
686 ang = fixedvectoangles2(trace_plane_normal, dir);
687 fixedmakevectors(ang);
689 portal = Portal_Spawn(own, org, ang);
693 portal.portal_id = portal_id_val;
694 Portal_SetOutPortal(own, portal);