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