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