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