Properly support team field on trigger_multiple
[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/triggers/teleporters.qh"
10 #include "../common/triggers/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
20 #define PORTALS_ARE_NOT_SOLID
21
22 const vector SAFENUDGE = '1 1 1';
23 const vector SAFERNUDGE = '8 8 8';
24
25 .vector portal_transform;
26 .vector portal_safe_origin;
27 .float portal_wants_to_vanish;
28 .float portal_activatetime;
29 .float savemodelindex;
30
31 float PlayerEdgeDistance(entity p, vector v)
32 {
33         vector vbest;
34
35         if(v.x < 0) vbest.x = p.mins.x; else vbest.x = p.maxs.x;
36         if(v.y < 0) vbest.y = p.mins.y; else vbest.y = p.maxs.y;
37         if(v.z < 0) vbest.z = p.mins.z; else vbest.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)
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
193         if (!teleporter.enemy)
194         {
195                 backtrace("Portal_TeleportPlayer ended up without other portal being set AFTER TeleportPlayer. Stop.");
196                 return 0;
197         }
198
199         // reset fade counter
200         teleporter.portal_wants_to_vanish = 0;
201         teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
202         teleporter.health = autocvar_g_balance_portal_health;
203         teleporter.enemy.health = autocvar_g_balance_portal_health;
204
205         return 1;
206 }
207
208 float Portal_FindSafeOrigin(entity portal)
209 {
210         vector o;
211         o = portal.origin;
212         portal.mins = PL_MIN_CONST - SAFERNUDGE;
213         portal.maxs = PL_MAX_CONST + SAFERNUDGE;
214         fixedmakevectors(portal.mangle);
215         portal.origin += 16 * v_forward;
216         if(!move_out_of_solid(portal))
217         {
218 #ifdef DEBUG
219                 LOG_INFO("NO SAFE ORIGIN");
220 #endif
221                 return 0;
222         }
223         portal.portal_safe_origin = portal.origin;
224         setorigin(portal, o);
225         return 1;
226 }
227
228 float Portal_WillHitPlane(vector eorg, vector emins, vector emaxs, vector evel, vector porg, vector pnorm, float psize)
229 {
230         float dist, distpersec, delta;
231         vector v;
232
233         dist = (eorg - porg) * pnorm;
234         dist += min(emins.x * pnorm.x, emaxs.x * pnorm.x);
235         dist += min(emins.y * pnorm.y, emaxs.y * pnorm.y);
236         dist += min(emins.z * pnorm.z, emaxs.z * pnorm.z);
237         if(dist < -1) // other side?
238                 return 0;
239 #ifdef PORTALS_ARE_NOT_SOLID
240         distpersec = evel * pnorm;
241         if(distpersec >= 0) // going away from the portal?
242                 return 0;
243         // we don't need this check with solid portals, them being SOLID_BSP should suffice
244         delta = dist / distpersec;
245         v = eorg - evel * delta - porg;
246         v = v - pnorm * (pnorm * v);
247         return vlen(v) < psize;
248 #else
249         return 1;
250 #endif
251 }
252
253 void Portal_Touch(entity this, entity toucher)
254 {
255         vector g;
256
257 #ifdef PORTALS_ARE_NOT_SOLID
258         // portal is being removed?
259         if(this.solid != SOLID_TRIGGER)
260                 return; // possibly engine bug
261
262         if(IS_PLAYER(toucher))
263                 return; // handled by think
264 #endif
265
266         if(toucher.classname == "item_flag_team")
267                 return; // never portal these
268
269         if(toucher.classname == "grapplinghook")
270                 return; // handled by think
271
272         if(!autocvar_g_vehicles_teleportable)
273         if(IS_VEHICLE(toucher))
274                 return; // no teleporting vehicles?
275
276         if(!this.enemy)
277                 error("Portal_Touch called for a broken portal\n");
278
279 #ifdef PORTALS_ARE_NOT_SOLID
280         if(trace_fraction < 1)
281                 return; // only handle TouchAreaGrid ones (only these can teleport)
282 #else
283         if(trace_fraction >= 1)
284                 return; // only handle impacts
285 #endif
286
287         if(toucher.classname == "porto")
288         {
289                 if(toucher.portal_id == this.portal_id)
290                         return;
291         }
292         if(time < this.portal_activatetime)
293                 if(toucher == this.aiment)
294                 {
295                         this.portal_activatetime = time + 0.1;
296                         return;
297                 }
298         if(toucher != this.aiment)
299                 if(IS_PLAYER(toucher))
300                         if(IS_INDEPENDENT_PLAYER(toucher) || IS_INDEPENDENT_PLAYER(this.aiment))
301                                 return; // cannot go through someone else's portal
302         if(toucher.aiment != this.aiment)
303                 if(IS_PLAYER(toucher.aiment))
304                         if(IS_INDEPENDENT_PLAYER(toucher.aiment) || IS_INDEPENDENT_PLAYER(this.aiment))
305                                 return; // cannot go through someone else's portal
306         fixedmakevectors(this.mangle);
307         g = frametime * '0 0 -1' * autocvar_sv_gravity;
308         if(!Portal_WillHitPlane(toucher.origin, toucher.mins, toucher.maxs, toucher.velocity + g, this.origin, v_forward, this.maxs.x))
309                 return;
310
311         /*
312         if(toucher.mins_x < PL_MIN.x || toucher.mins_y < PL_MIN.y || toucher.mins_z < PL_MIN.z
313         || toucher.maxs_x > PL_MAX.x || toucher.maxs_y > PL_MAX.y || toucher.maxs_z > PL_MAX.z)
314         {
315                 // can't teleport this
316                 return;
317         }
318         */
319
320         if(Portal_TeleportPlayer(this, toucher))
321                 if(toucher.classname == "porto")
322                         if(toucher.effects & EF_RED)
323                                 toucher.effects += EF_BLUE - EF_RED;
324 }
325
326 void Portal_Think(entity this);
327 void Portal_MakeBrokenPortal(entity portal)
328 {
329         portal.skin = 2;
330         portal.solid = SOLID_NOT;
331         settouch(portal, func_null);
332         setthink(portal, func_null);
333         portal.effects = 0;
334         portal.nextthink = 0;
335         portal.takedamage = DAMAGE_NO;
336 }
337
338 void Portal_MakeWaitingPortal(entity portal)
339 {
340         portal.skin = 2;
341         portal.solid = SOLID_NOT;
342         settouch(portal, func_null);
343         setthink(portal, func_null);
344         portal.effects = EF_ADDITIVE;
345         portal.nextthink = 0;
346         portal.takedamage = DAMAGE_YES;
347 }
348
349 void Portal_MakeInPortal(entity portal)
350 {
351         portal.skin = 0;
352         portal.solid = SOLID_NOT; // this is done when connecting them!
353         settouch(portal, Portal_Touch);
354         setthink(portal, Portal_Think);
355         portal.effects = EF_RED;
356         portal.nextthink = time;
357         portal.takedamage = DAMAGE_NO;
358 }
359
360 void Portal_MakeOutPortal(entity portal)
361 {
362         portal.skin = 1;
363         portal.solid = SOLID_NOT;
364         settouch(portal, func_null);
365         setthink(portal, func_null);
366         portal.effects = EF_STARDUST | EF_BLUE;
367         portal.nextthink = 0;
368         portal.takedamage = DAMAGE_YES;
369 }
370
371 void Portal_Disconnect(entity teleporter, entity destination)
372 {
373         teleporter.enemy = NULL;
374         destination.enemy = NULL;
375         Portal_MakeBrokenPortal(teleporter);
376         Portal_MakeBrokenPortal(destination);
377 }
378
379 void Portal_Connect(entity teleporter, entity destination)
380 {
381         teleporter.portal_transform = AnglesTransform_RightDivide(AnglesTransform_TurnDirectionFR(destination.mangle), teleporter.mangle);
382
383         teleporter.enemy = destination;
384         destination.enemy = teleporter;
385         Portal_MakeInPortal(teleporter);
386         Portal_MakeOutPortal(destination);
387         teleporter.fade_time = time + autocvar_g_balance_portal_lifetime;
388         destination.fade_time = teleporter.fade_time;
389         teleporter.portal_wants_to_vanish = 0;
390         destination.portal_wants_to_vanish = 0;
391         teleporter.teleport_time = time;
392 #ifdef PORTALS_ARE_NOT_SOLID
393         teleporter.solid = SOLID_TRIGGER;
394 #else
395         teleporter.solid = SOLID_BSP;
396 #endif
397 }
398
399 void Portal_Remove(entity portal, float killed)
400 {
401         entity e;
402         e = portal.enemy;
403
404         if(e)
405         {
406                 Portal_Disconnect(portal, e);
407                 Portal_Remove(e, killed);
408         }
409
410         if(portal == portal.aiment.portal_in)
411                 portal.aiment.portal_in = NULL;
412         if(portal == portal.aiment.portal_out)
413                 portal.aiment.portal_out = NULL;
414         //portal.aiment = NULL;
415
416         // makes the portal vanish
417         if(killed)
418         {
419                 fixedmakevectors(portal.mangle);
420                 sound(portal, CH_SHOTS, SND_PORTO_EXPLODE, VOL_BASE, ATTEN_NORM);
421                 Send_Effect(EFFECT_ROCKET_EXPLODE, portal.origin + v_forward * 16, v_forward * 1024, 4);
422                 delete(portal);
423         }
424         else
425         {
426                 Portal_MakeBrokenPortal(portal);
427                 sound(portal, CH_SHOTS, SND_PORTO_EXPIRE, VOL_BASE, ATTEN_NORM);
428                 SUB_SetFade(portal, time, 0.5);
429         }
430 }
431
432 void Portal_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
433 {
434         if(deathtype == DEATH_TELEFRAG.m_id)
435                 return;
436         if(attacker != this.aiment)
437                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(this.aiment))
438                         return;
439         this.health -= damage;
440         if(this.health < 0)
441                 Portal_Remove(this, 1);
442 }
443
444 void Portal_Think_TryTeleportPlayer(entity this, entity e, vector g)
445 {
446         if(!Portal_WillHitPlane(e.origin, e.mins, e.maxs, e.velocity + g, this.origin, v_forward, this.maxs.x))
447                 return;
448
449         // if e would hit the portal in a frame...
450         // already teleport him
451         tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
452         if(trace_ent == this)
453                 Portal_TeleportPlayer(this, e);
454 }
455
456 void Portal_Think(entity this)
457 {
458         entity o;
459         vector g;
460
461 #ifdef PORTALS_ARE_NOT_SOLID
462         // portal is being removed?
463         if(this.solid != SOLID_TRIGGER)
464                 return; // possibly engine bug
465
466         if(!this.enemy)
467                 error("Portal_Think called for a broken portal\n");
468
469         o = this.aiment;
470         this.solid = SOLID_BBOX;
471         this.aiment = NULL;
472
473         g = frametime * '0 0 -1' * autocvar_sv_gravity;
474
475         fixedmakevectors(this.mangle);
476
477         FOREACH_CLIENT(IS_PLAYER(it), {
478                 if(it != o)
479                         if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o))
480                                 continue; // cannot go through someone else's portal
481
482                 if(it != o || time >= this.portal_activatetime)
483                         Portal_Think_TryTeleportPlayer(this, it, g);
484
485                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
486             {
487                 .entity weaponentity = weaponentities[slot];
488                 if(it.(weaponentity).hook)
489                         Portal_Think_TryTeleportPlayer(this, it.(weaponentity).hook, g);
490             }
491         });
492         this.solid = SOLID_TRIGGER;
493         this.aiment = o;
494 #endif
495
496         this.nextthink = time;
497
498         if(time > this.fade_time)
499                 Portal_Remove(this, 0);
500 }
501
502 bool Portal_Customize(entity this, entity client)
503 {
504         if(IS_SPEC(client))
505                 client = client.enemy;
506         if(client == this.aiment)
507         {
508                 this.modelindex = this.savemodelindex;
509         }
510         else if(IS_INDEPENDENT_PLAYER(client) || IS_INDEPENDENT_PLAYER(this.aiment))
511         {
512                 this.modelindex = 0;
513         }
514         else
515         {
516                 this.modelindex = this.savemodelindex;
517         }
518         return true;
519 }
520
521 // cleanup:
522 //   when creating in-portal:
523 //     disconnect
524 //     clear existing in-portal
525 //     set as in-portal
526 //     connect
527 //   when creating out-portal:
528 //     disconnect
529 //     clear existing out-portal
530 //     set as out-portal
531 //   when player dies:
532 //     disconnect portals
533 //     clear both portals
534 //   after timeout of in-portal:
535 //     disconnect portals
536 //     clear both portals
537 //   TODO: ensure only one portal shot at once
538 float Portal_SetInPortal(entity own, entity portal)
539 {
540         if(own.portal_in)
541         {
542                 if(own.portal_out)
543                         Portal_Disconnect(own.portal_in, own.portal_out);
544                 Portal_Remove(own.portal_in, 0);
545         }
546         own.portal_in = portal;
547         if(own.portal_out)
548         {
549                 own.portal_out.portal_id = portal.portal_id;
550                 Portal_Connect(own.portal_in, own.portal_out);
551         }
552         return 2;
553 }
554 float Portal_SetOutPortal(entity own, entity portal)
555 {
556         if(own.portal_out)
557         {
558                 if(own.portal_in)
559                         Portal_Disconnect(own.portal_in, own.portal_out);
560                 Portal_Remove(own.portal_out, 0);
561         }
562         own.portal_out = portal;
563         if(own.portal_in)
564         {
565                 own.portal_in.portal_id = portal.portal_id;
566                 Portal_Connect(own.portal_in, own.portal_out);
567         }
568         return 1;
569 }
570 void Portal_ClearAll_PortalsOnly(entity own)
571 {
572         if(own.portal_in)
573                 Portal_Remove(own.portal_in, 0);
574         if(own.portal_out)
575                 Portal_Remove(own.portal_out, 0);
576 }
577 void Portal_ClearAll(entity own)
578 {
579         Portal_ClearAll_PortalsOnly(own);
580         W_Porto_Remove(own);
581 }
582 void Portal_RemoveLater_Think(entity this)
583 {
584         Portal_Remove(this, this.cnt);
585 }
586 void Portal_RemoveLater(entity portal, float kill)
587 {
588         Portal_MakeBrokenPortal(portal);
589         portal.cnt = kill;
590         setthink(portal, Portal_RemoveLater_Think);
591         portal.nextthink = time;
592 }
593 void Portal_ClearAllLater_PortalsOnly(entity own)
594 {
595         if(own.portal_in)
596                 Portal_RemoveLater(own.portal_in, 0);
597         if(own.portal_out)
598                 Portal_RemoveLater(own.portal_out, 0);
599 }
600 void Portal_ClearAllLater(entity own)
601 {
602         Portal_ClearAllLater_PortalsOnly(own);
603         W_Porto_Remove(own);
604 }
605 void Portal_ClearWithID(entity own, float id)
606 {
607         if(own.portal_in)
608                 if(own.portal_in.portal_id == id)
609                 {
610                         if(own.portal_out)
611                                 Portal_Disconnect(own.portal_in, own.portal_out);
612                         Portal_Remove(own.portal_in, 0);
613                 }
614         if(own.portal_out)
615                 if(own.portal_out.portal_id == id)
616                 {
617                         if(own.portal_in)
618                                 Portal_Disconnect(own.portal_in, own.portal_out);
619                         Portal_Remove(own.portal_out, 0);
620                 }
621 }
622
623 entity Portal_Spawn(entity own, vector org, vector ang)
624 {
625         entity portal;
626
627         fixedmakevectors(ang);
628         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
629                 return NULL;
630
631         portal = new(portal);
632         portal.aiment = own;
633         setorigin(portal, org);
634         portal.mangle = ang;
635         portal.angles = ang;
636         portal.angles_x = -portal.angles.x; // is a bmodel
637         setthink(portal, Portal_Think);
638         portal.nextthink = 0;
639         portal.portal_activatetime = time + 0.1;
640         portal.takedamage = DAMAGE_AIM;
641         portal.event_damage = Portal_Damage;
642         portal.fade_time = time + autocvar_g_balance_portal_lifetime;
643         portal.health = autocvar_g_balance_portal_health;
644         setmodel(portal, MDL_PORTAL);
645         portal.savemodelindex = portal.modelindex;
646         setcefc(portal, Portal_Customize);
647
648         if(!Portal_FindSafeOrigin(portal))
649         {
650                 delete(portal);
651                 return NULL;
652         }
653
654         setsize(portal, '-48 -48 -48', '48 48 48');
655         Portal_MakeWaitingPortal(portal);
656
657         return portal;
658 }
659
660 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
661 {
662         entity portal;
663         vector ang;
664         vector org;
665
666         org = trace_endpos;
667         ang = fixedvectoangles2(trace_plane_normal, dir);
668         fixedmakevectors(ang);
669
670         portal = Portal_Spawn(own, org, ang);
671         if(!portal)
672                 return 0;
673
674         portal.portal_id = portal_id_val;
675         Portal_SetInPortal(own, portal);
676
677         return 1;
678 }
679
680 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
681 {
682         entity portal;
683         vector ang;
684         vector org;
685
686         org = trace_endpos;
687         ang = fixedvectoangles2(trace_plane_normal, dir);
688         fixedmakevectors(ang);
689
690         portal = Portal_Spawn(own, org, ang);
691         if(!portal)
692                 return 0;
693
694         portal.portal_id = portal_id_val;
695         Portal_SetOutPortal(own, portal);
696
697         return 1;
698 }