bd5a8540e22937cd791fb569dd89a24590431e6a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / portals.qc
1 #include "portals.qh"
2
3 #include <common/effects/all.qh>
4 #include "g_hook.qh"
5 #include "mutators/_mod.qh"
6 #include "../common/constants.qh"
7 #include "../common/deathtypes/all.qh"
8 #include "../common/notifications/all.qh"
9 #include "../common/mapobjects/teleporters.qh"
10 #include "../common/mapobjects/subs.qh"
11 #include "../common/util.qh"
12 #include <common/weapons/_all.qh>
13 #include "../lib/csqcmodel/sv_model.qh"
14 #include "../lib/warpzone/anglestransform.qh"
15 #include "../lib/warpzone/util_server.qh"
16 #include "../lib/warpzone/common.qh"
17 #include "../common/vehicles/vehicle.qh"
18 #include "../common/vehicles/sv_vehicles.qh"
19 #include <server/player.qh>
20
21 #define PORTALS_ARE_NOT_SOLID
22
23 const vector SAFENUDGE = '1 1 1';
24 const vector SAFERNUDGE = '8 8 8';
25
26 .vector portal_transform;
27 .vector portal_safe_origin;
28 .float portal_wants_to_vanish;
29 .float portal_activatetime;
30 .float savemodelindex;
31
32 float PlayerEdgeDistance(entity p, vector v)
33 {
34         vector vbest;
35
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;
39
40         return vbest * v;
41 }
42
43 vector Portal_ApplyTransformToPlayerAngle(vector transform, vector vangle)
44 {
45         vector old_forward, old_up;
46         vector old_yawforward;
47         vector new_forward, new_up;
48         vector new_yawforward;
49
50         vector ang;
51         ang = vangle;
52         /*
53            ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
54            ang = AnglesTransform_ApplyToVAngles(transform, ang);
55          */
56
57         // PLAYERS use different math
58 #if !(POSITIVE_PITCH_IS_DOWN)
59         ang.x = -ang.x;
60 #endif
61
62         //print("reference: ", vtos(AnglesTransform_ApplyToVAngles(transform, ang)), "\n");
63
64         fixedmakevectors(ang);
65         old_forward = v_forward;
66         old_up = v_up;
67         fixedmakevectors(ang.y * '0 1 0');
68         old_yawforward = v_forward;
69
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);
74
75         // but now find a new sense of direction
76         // this is NOT easy!
77         // assume new_forward points straight up.
78         // What is our yaw?
79         //
80         // new_up could now point forward OR backward... which direction to choose?
81
82         if(new_forward.z > 0.7 || new_forward.z < -0.7) // far up; in this case, the "up" vector points backwards
83         {
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)
88                         new_up = -1 * new_up;
89                 ang.y = vectoyaw(new_up); // this vector is the yaw we want
90                 //print("UP/DOWN path: ", vtos(ang), "\n");
91         }
92         else
93         {
94                 // good angles; here, "forward" suffices
95                 ang = fixedvectoangles(new_forward);
96                 //print("GOOD path: ", vtos(ang), "\n");
97         }
98
99 #if !(POSITIVE_PITCH_IS_DOWN)
100         ang.x = -ang.x;
101 #endif
102         ang.z = vangle.z;
103         return ang;
104 }
105
106 .vector right_vector;
107 float Portal_TeleportPlayer(entity teleporter, entity player, entity portal_owner)
108 {
109         vector from, to, safe, step, transform, ang, newvel;
110         float planeshift, s, t;
111
112         if (!teleporter.enemy)
113         {
114                 backtrace("Portal_TeleportPlayer called without other portal being set. Stop.");
115                 return 0;
116         }
117
118         from = teleporter.origin;
119         transform = teleporter.portal_transform;
120
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
125
126         // shift it out
127         fixedmakevectors(teleporter.enemy.mangle);
128
129         // first shift it ON the plane if needed
130         planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
131         /*
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));
135         else
136         */
137                 to += v_forward * planeshift;
138
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
145            +     s                                        * v_right
146            +     t                                        * v_up;
147
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);
151         if(trace_startsolid)
152         {
153                 LOG_INFO("'safe' teleport location is not safe!");
154                 // FAIL TODO why does this happen?
155                 return 0;
156         }
157         safe = trace_endpos + normalize(safe - trace_endpos) * 0;
158         tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
159         if(trace_startsolid)
160         {
161                 LOG_INFO("trace_endpos in solid, this can't be!");
162                 // FAIL TODO why does this happen? (reported by MrBougo)
163                 return 0;
164         }
165         to = trace_endpos + normalize(safe - trace_endpos) * 0;
166         //print(vtos(to), "\n");
167
168         // ang_x stuff works around weird quake angles
169         if(IS_PLAYER(player))
170                 ang = Portal_ApplyTransformToPlayerAngle(transform, player.v_angle);
171         else
172                 ang = AnglesTransform_ApplyToAngles(transform, player.angles);
173
174         // factor -1 allows chaining portals, but may be weird
175         player.right_vector = -1 * AnglesTransform_Apply(transform, player.right_vector);
176
177         MUTATOR_CALLHOOK(PortalTeleport, player);
178
179         if (!teleporter.enemy)
180         {
181                 backtrace("Portal_TeleportPlayer ended up without other portal being set BEFORE TeleportPlayer. Stop.");
182                 return 0;
183         }
184
185         tdeath_hit = 0;
186         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax, TELEPORT_FLAGS_PORTAL);
187         if(tdeath_hit)
188         {
189                 // telefrag within 1 second of portal creation = amazing
190                 if(time < teleporter.teleport_time + 1)
191                         Send_Notification(NOTIF_ONE, player, MSG_ANNCE, ANNCE_ACHIEVEMENT_AMAZING);
192         }
193         else if(player != portal_owner && IS_PLAYER(portal_owner) && IS_PLAYER(player))
194         {
195                 player.pusher = portal_owner;
196                 player.pushltime = time + autocvar_g_maxpushtime;
197                 player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
198         }
199
200         if (!teleporter.enemy)
201         {
202                 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
203                 return 0;
204         }
205
206         // reset fade counter
207         teleporter.portal_wants_to_vanish = 0;
208         teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
209         SetResourceExplicit(teleporter, RES_HEALTH, autocvar_g_balance_portal_health);
210         SetResourceExplicit(teleporter.enemy, RES_HEALTH, autocvar_g_balance_portal_health);
211
212         return 1;
213 }
214
215 float Portal_FindSafeOrigin(entity portal)
216 {
217         vector o;
218         o = portal.origin;
219         portal.mins = PL_MIN_CONST - SAFERNUDGE;
220         portal.maxs = PL_MAX_CONST + SAFERNUDGE;
221         fixedmakevectors(portal.mangle);
222         portal.origin += 16 * v_forward;
223         if(!move_out_of_solid(portal))
224         {
225 #ifdef DEBUG
226                 LOG_INFO("NO SAFE ORIGIN");
227 #endif
228                 return 0;
229         }
230         portal.portal_safe_origin = portal.origin;
231         setorigin(portal, o);
232         return 1;
233 }
234
235 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
236 {
237         float dist, distpersec, delta;
238         vector v;
239
240         dist = (eorg - porg) * pnorm;
241         dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
242         dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
243         dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
244         if(dist < -1) // other side?
245                 return 0;
246 #ifdef PORTALS_ARE_NOT_SOLID
247         distpersec = evel * pnorm;
248         if(distpersec >= 0) // going away from the portal?
249                 return 0;
250         // we don't need this check with solid portals, them being SOLID_BSP should suffice
251         delta = dist / distpersec;
252         v = eorg - evel * delta - porg;
253         v = v - pnorm * (pnorm * v);
254         return vlen(v) < psize;
255 #else
256         return 1;
257 #endif
258 }
259
260 void Portal_Touch(entity this, entity toucher)
261 {
262         vector g;
263
264 #ifdef PORTALS_ARE_NOT_SOLID
265         // portal is being removed?
266         if(this.solid != SOLID_TRIGGER)
267                 return; // possibly engine bug
268
269         if(IS_PLAYER(toucher))
270                 return; // handled by think
271 #endif
272
273         if(toucher.classname == "item_flag_team")
274                 return; // never portal these
275
276         if(toucher.classname == "grapplinghook")
277                 return; // handled by think
278
279         if(!autocvar_g_vehicles_teleportable)
280         if(IS_VEHICLE(toucher))
281                 return; // no teleporting vehicles?
282
283         if(!this.enemy)
284                 error("Portal_Touch called for a broken portal\n");
285
286 #ifdef PORTALS_ARE_NOT_SOLID
287         if(trace_fraction < 1)
288                 return; // only handle TouchAreaGrid ones (only these can teleport)
289 #else
290         if(trace_fraction >= 1)
291                 return; // only handle impacts
292 #endif
293
294         if(toucher.classname == "porto")
295         {
296                 if(toucher.portal_id == this.portal_id)
297                         return;
298         }
299         if(time < this.portal_activatetime)
300                 if(toucher == this.aiment)
301                 {
302                         this.portal_activatetime = time + 0.1;
303                         return;
304                 }
305         if(toucher != this.aiment)
306                 if(IS_PLAYER(toucher))
307                         if(IS_INDEPENDENT_PLAYER(toucher) || IS_INDEPENDENT_PLAYER(this.aiment))
308                                 return; // cannot go through someone else's portal
309         if(toucher.aiment != this.aiment)
310                 if(IS_PLAYER(toucher.aiment))
311                         if(IS_INDEPENDENT_PLAYER(toucher.aiment) || IS_INDEPENDENT_PLAYER(this.aiment))
312                                 return; // cannot go through someone else's portal
313         fixedmakevectors(this.mangle);
314         g = frametime * '0 0 -1' * autocvar_sv_gravity;
315         if(!Portal_WillHitPlane(toucher.origin, toucher.mins, toucher.maxs, toucher.velocity + g, this.origin, v_forward, this.maxs.x))
316                 return;
317
318         /*
319         if(toucher.mins_x < PL_MIN.x || toucher.mins_y < PL_MIN.y || toucher.mins_z < PL_MIN.z
320         || toucher.maxs_x > PL_MAX.x || toucher.maxs_y > PL_MAX.y || toucher.maxs_z > PL_MAX.z)
321         {
322                 // can't teleport this
323                 return;
324         }
325         */
326
327         if(Portal_TeleportPlayer(this, toucher, this.aiment))
328                 if(toucher.classname == "porto")
329                         if(toucher.effects & EF_RED)
330                                 toucher.effects += EF_BLUE - EF_RED;
331 }
332
333 void Portal_MakeBrokenPortal(entity portal)
334 {
335         portal.skin = 2;
336         portal.solid = SOLID_NOT;
337         settouch(portal, func_null);
338         setthink(portal, func_null);
339         portal.effects = 0;
340         portal.nextthink = 0;
341         portal.takedamage = DAMAGE_NO;
342 }
343
344 void Portal_MakeWaitingPortal(entity portal)
345 {
346         portal.skin = 2;
347         portal.solid = SOLID_NOT;
348         settouch(portal, func_null);
349         setthink(portal, func_null);
350         portal.effects = EF_ADDITIVE;
351         portal.nextthink = 0;
352         portal.takedamage = DAMAGE_YES;
353 }
354
355 void Portal_MakeInPortal(entity portal)
356 {
357         portal.skin = 0;
358         portal.solid = SOLID_NOT; // this is done when connecting them!
359         settouch(portal, Portal_Touch);
360         setthink(portal, Portal_Think);
361         portal.effects = EF_RED;
362         portal.nextthink = time;
363         portal.takedamage = DAMAGE_NO;
364 }
365
366 void Portal_MakeOutPortal(entity portal)
367 {
368         portal.skin = 1;
369         portal.solid = SOLID_NOT;
370         settouch(portal, func_null);
371         setthink(portal, func_null);
372         portal.effects = EF_STARDUST | EF_BLUE;
373         portal.nextthink = 0;
374         portal.takedamage = DAMAGE_YES;
375 }
376
377 void Portal_Disconnect(entity teleporter, entity destination)
378 {
379         teleporter.enemy = NULL;
380         destination.enemy = NULL;
381         Portal_MakeBrokenPortal(teleporter);
382         Portal_MakeBrokenPortal(destination);
383 }
384
385 void Portal_Connect(entity teleporter, entity destination)
386 {
387         teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
388
389         teleporter.enemy = destination;
390         destination.enemy = teleporter;
391         Portal_MakeInPortal(teleporter);
392         Portal_MakeOutPortal(destination);
393         teleporter.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
394         destination.fade_time = teleporter.fade_time;
395         teleporter.portal_wants_to_vanish = 0;
396         destination.portal_wants_to_vanish = 0;
397         teleporter.teleport_time = time;
398 #ifdef PORTALS_ARE_NOT_SOLID
399         teleporter.solid = SOLID_TRIGGER;
400 #else
401         teleporter.solid = SOLID_BSP;
402 #endif
403 }
404
405 void Portal_Remove(entity portal, float killed)
406 {
407         entity e;
408         e = portal.enemy;
409
410         if(e)
411         {
412                 Portal_Disconnect(portal, e);
413                 Portal_Remove(e, killed);
414         }
415
416         if(portal == portal.aiment.portal_in)
417                 portal.aiment.portal_in = NULL;
418         if(portal == portal.aiment.portal_out)
419                 portal.aiment.portal_out = NULL;
420         //portal.aiment = NULL;
421
422         // makes the portal vanish
423         if(killed)
424         {
425                 fixedmakevectors(portal.mangle);
426                 sound(portal, CH_SHOTS, SND_PORTO_EXPLODE, VOL_BASE, ATTEN_NORM);
427                 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
428                 delete(portal);
429         }
430         else
431         {
432                 Portal_MakeBrokenPortal(portal);
433                 sound(portal, CH_SHOTS, SND_PORTO_EXPIRE, VOL_BASE, ATTEN_NORM);
434                 SUB_SetFade(portal, time, 0.5);
435         }
436 }
437
438 void Portal_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
439 {
440         if(deathtype == DEATH_TELEFRAG.m_id)
441                 return;
442         if(attacker != this.aiment)
443                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(this.aiment))
444                         return;
445         TakeResource(this, RES_HEALTH, damage);
446         if(GetResource(this, RES_HEALTH) < 0)
447                 Portal_Remove(this, 1);
448 }
449
450 void Portal_Think_TryTeleportPlayer(entity this, entity e, vector g, entity portal_owner)
451 {
452         if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, this.origin, v_forward, this.maxs.x))
453                 return;
454
455         // if e would hit the portal in a frame...
456         // already teleport him
457         tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
458         if(trace_ent == this)
459                 Portal_TeleportPlayer(this, e, portal_owner);
460 }
461
462 void Portal_Think(entity this)
463 {
464         entity o;
465         vector g;
466
467 #ifdef PORTALS_ARE_NOT_SOLID
468         // portal is being removed?
469         if(this.solid != SOLID_TRIGGER)
470                 return; // possibly engine bug
471
472         if(!this.enemy)
473                 error("Portal_Think called for a broken portal\n");
474
475         o = this.aiment;
476         this.solid = SOLID_BBOX;
477         this.aiment = NULL;
478
479         g = frametime * '0 0 -1' * autocvar_sv_gravity;
480
481         fixedmakevectors(this.mangle);
482
483         FOREACH_CLIENT(IS_PLAYER(it), {
484                 if(it != o)
485                         if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o))
486                                 continue; // cannot go through someone else's portal
487
488                 if(it != o || time >= this.portal_activatetime)
489                         Portal_Think_TryTeleportPlayer(this, it, g, o);
490
491                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
492             {
493                 .entity weaponentity = weaponentities[slot];
494                 if(it.(weaponentity).hook)
495                         Portal_Think_TryTeleportPlayer(this, it.(weaponentity).hook, g, o);
496             }
497         });
498         this.solid = SOLID_TRIGGER;
499         this.aiment = o;
500 #endif
501
502         this.nextthink = time;
503
504         if(this.fade_time && time > this.fade_time)
505                 Portal_Remove(this, 0);
506 }
507
508 bool Portal_Customize(entity this, entity client)
509 {
510         if(IS_SPEC(client))
511                 client = client.enemy;
512         if(client == this.aiment)
513         {
514                 this.modelindex = this.savemodelindex;
515         }
516         else if(IS_INDEPENDENT_PLAYER(client) || IS_INDEPENDENT_PLAYER(this.aiment))
517         {
518                 this.modelindex = 0;
519         }
520         else
521         {
522                 this.modelindex = this.savemodelindex;
523         }
524         return true;
525 }
526
527 // cleanup:
528 //   when creating in-portal:
529 //     disconnect
530 //     clear existing in-portal
531 //     set as in-portal
532 //     connect
533 //   when creating out-portal:
534 //     disconnect
535 //     clear existing out-portal
536 //     set as out-portal
537 //   when player dies:
538 //     disconnect portals
539 //     clear both portals
540 //   after timeout of in-portal:
541 //     disconnect portals
542 //     clear both portals
543 //   TODO: ensure only one portal shot at once
544 float Portal_SetInPortal(entity own, entity portal)
545 {
546         if(own.portal_in)
547         {
548                 if(own.portal_out)
549                         Portal_Disconnect(own.portal_in, own.portal_out);
550                 Portal_Remove(own.portal_in, 0);
551         }
552         own.portal_in = portal;
553         if(own.portal_out)
554         {
555                 own.portal_out.portal_id = portal.portal_id;
556                 Portal_Connect(own.portal_in, own.portal_out);
557         }
558         return 2;
559 }
560 float Portal_SetOutPortal(entity own, entity portal)
561 {
562         if(own.portal_out)
563         {
564                 if(own.portal_in)
565                         Portal_Disconnect(own.portal_in, own.portal_out);
566                 Portal_Remove(own.portal_out, 0);
567         }
568         own.portal_out = portal;
569         if(own.portal_in)
570         {
571                 own.portal_in.portal_id = portal.portal_id;
572                 Portal_Connect(own.portal_in, own.portal_out);
573         }
574         return 1;
575 }
576 void Portal_ClearAll_PortalsOnly(entity own)
577 {
578         if(own.portal_in)
579                 Portal_Remove(own.portal_in, 0);
580         if(own.portal_out)
581                 Portal_Remove(own.portal_out, 0);
582 }
583 void Portal_ClearAll(entity own)
584 {
585         Portal_ClearAll_PortalsOnly(own);
586         W_Porto_Remove(own);
587 }
588 void Portal_RemoveLater_Think(entity this)
589 {
590         Portal_Remove(this, this.cnt);
591 }
592 void Portal_RemoveLater(entity portal, float kill)
593 {
594         Portal_MakeBrokenPortal(portal);
595         portal.cnt = kill;
596         setthink(portal, Portal_RemoveLater_Think);
597         portal.nextthink = time;
598 }
599 void Portal_ClearAllLater_PortalsOnly(entity own)
600 {
601         if(own.portal_in)
602                 Portal_RemoveLater(own.portal_in, 0);
603         if(own.portal_out)
604                 Portal_RemoveLater(own.portal_out, 0);
605 }
606 void Portal_ClearAllLater(entity own)
607 {
608         Portal_ClearAllLater_PortalsOnly(own);
609         W_Porto_Remove(own);
610 }
611 void Portal_ClearWithID(entity own, float id)
612 {
613         if(own.portal_in)
614                 if(own.portal_in.portal_id == id)
615                 {
616                         if(own.portal_out)
617                                 Portal_Disconnect(own.portal_in, own.portal_out);
618                         Portal_Remove(own.portal_in, 0);
619                 }
620         if(own.portal_out)
621                 if(own.portal_out.portal_id == id)
622                 {
623                         if(own.portal_in)
624                                 Portal_Disconnect(own.portal_in, own.portal_out);
625                         Portal_Remove(own.portal_out, 0);
626                 }
627 }
628
629 entity Portal_Spawn(entity own, vector org, vector ang)
630 {
631         entity portal;
632
633         fixedmakevectors(ang);
634         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
635                 return NULL;
636
637         portal = new(portal);
638         portal.aiment = own;
639         setorigin(portal, org);
640         portal.mangle = ang;
641         portal.angles = ang;
642         portal.angles_x = -portal.angles.x; // is a bmodel
643         setthink(portal, Portal_Think);
644         portal.nextthink = 0;
645         portal.portal_activatetime = time + 0.1;
646         portal.takedamage = DAMAGE_AIM;
647         portal.event_damage = Portal_Damage;
648         portal.fade_time = ((autocvar_g_balance_portal_lifetime >= 0) ? time + autocvar_g_balance_portal_lifetime : 0);
649         SetResourceExplicit(portal, RES_HEALTH, autocvar_g_balance_portal_health);
650         setmodel(portal, MDL_PORTAL);
651         portal.savemodelindex = portal.modelindex;
652         setcefc(portal, Portal_Customize);
653
654         if(!Portal_FindSafeOrigin(portal))
655         {
656                 delete(portal);
657                 return NULL;
658         }
659
660         setsize(portal, '-48 -48 -48', '48 48 48');
661         Portal_MakeWaitingPortal(portal);
662
663         return portal;
664 }
665
666 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
667 {
668         entity portal;
669         vector ang;
670         vector org;
671
672         org = trace_endpos;
673         ang = fixedvectoangles2(trace_plane_normal, dir);
674         fixedmakevectors(ang);
675
676         portal = Portal_Spawn(own, org, ang);
677         if(!portal)
678                 return 0;
679
680         portal.portal_id = portal_id_val;
681         Portal_SetInPortal(own, portal);
682
683         return 1;
684 }
685
686 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
687 {
688         entity portal;
689         vector ang;
690         vector org;
691
692         org = trace_endpos;
693         ang = fixedvectoangles2(trace_plane_normal, dir);
694         fixedmakevectors(ang);
695
696         portal = Portal_Spawn(own, org, ang);
697         if(!portal)
698                 return 0;
699
700         portal.portal_id = portal_id_val;
701         Portal_SetOutPortal(own, portal);
702
703         return 1;
704 }