]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
no idea about these portal crashes... added debug code
[divverent/nexuiz.git] / data / qcsrc / server / portals.qc
1 float portals_must_not_break; // debug code
2
3 .vector portal_transform;
4 .vector portal_safe_origin;
5 .float portal_wants_to_vanish;
6 .float portal_activatetime;
7 .float portal_id;
8
9 .entity portal_in, portal_out;
10
11 vector Portal_Transform_Apply(vector transform, vector v)
12 {
13         fixedmakevectors(transform);
14         return v_forward * v_x
15              + v_right   * (-v_y)
16                  + v_up      * v_z;
17 }
18
19 vector Portal_Transform_Multiply(vector t1, vector t2)
20 {
21         vector m_forward, m_up;
22         fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
23         m_forward = Portal_Transform_Apply(t1, m_forward); m_up = Portal_Transform_Apply(t1, m_up);
24         return fixedvectoangles2(m_forward, m_up);
25 }
26
27 vector Portal_Transform_Invert(vector transform)
28 {
29         vector i_forward, i_up;
30         fixedmakevectors(transform);
31         // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
32         // but these are orthogonal unit vectors!
33         // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
34         // TODO is this always -transform?
35         i_forward_x = v_forward_x;
36         i_forward_y = -v_right_x;
37         i_forward_z = v_up_x;
38         i_up_x = v_forward_z;
39         i_up_y = -v_right_z;
40         i_up_z = v_up_z;
41 #ifdef DEBUG
42         vector v;
43         v = fixedvectoangles2(i_forward, i_up);
44         print("Transform: ", vtos(transform), "\n");
45         print("Inverted: ", vtos(v), "\n");
46         print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
47         fixedmakevectors(Portal_Transform_Multiply(v, transform));
48         print("Verify: ", vtos(v_forward), "\n");
49         print("Verify: ", vtos(v_right), "\n");
50         print("Verify: ", vtos(v_up), "\n");
51 #endif
52         return fixedvectoangles2(i_forward, i_up);
53 }
54
55 vector Portal_Transform_TurnDirection(vector transform)
56 {
57         // turn 180 degrees around v_up
58         // changes in-direction to out-direction
59         fixedmakevectors(transform);
60         return fixedvectoangles2(-1 * v_forward, 1 * v_up);
61 }
62
63 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
64 {
65         return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
66 }
67
68 float PlayerEdgeDistance(entity p, vector v)
69 {
70         vector vbest;
71
72         if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
73         if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
74         if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
75
76         return vbest * v;
77 }
78
79 .vector right_vector;
80 float Portal_TeleportPlayer(entity teleporter, entity player)
81 {
82         vector from, to, safe, step, transform, ang, newvel;
83         vector new_forward, new_up;
84         vector old_forward, old_up;
85         vector new_yawforward;
86         vector old_yawforward;
87         float planeshift, s, t;
88
89         if(!portals_must_not_break)
90                 error("Portal_TeleportPlayer called for unprotected portals");
91         print("Portalling using ", etos(teleporter), " to ", etos(teleporter.enemy), "\n");
92
93         from = teleporter.origin;
94         transform = teleporter.portal_transform;
95
96         to = teleporter.enemy.origin;
97         to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
98         newvel = Portal_Transform_Apply(transform, player.velocity);
99         // this now is INSIDE the plane... can't use that
100         
101         // shift it out
102         fixedmakevectors(teleporter.enemy.angles);
103
104         // first shift it ON the plane if needed
105         planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
106         if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
107                 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
108                 to += newvel * (planeshift / (newvel * v_forward));
109         else
110                 to += trace_plane_normal * planeshift;
111         
112         s = (to - teleporter.enemy.origin) * v_right;
113         t = (to - teleporter.enemy.origin) * v_up;
114         s = bound(-48, s, 48);
115         t = bound(-48, t, 48);
116         to = teleporter.enemy.origin
117            + ((to - teleporter.enemy.origin) * v_forward) * v_forward
118            +     s                                        * v_right
119            +     t                                        * v_up;
120
121         safe = teleporter.enemy.portal_safe_origin; // a valid player origin
122         step = to + ((safe - to) * v_forward) * v_forward;
123         tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', step, MOVE_NOMONSTERS, player);
124         if(trace_startsolid)
125         {
126                 print("'safe' teleport location is not safe!\n");
127                 // FAIL TODO why does this happen?
128                 return 0;
129         }
130         safe = trace_endpos;
131         tracebox(safe, player.mins - '1 1 1', player.maxs + '1 1 1', to, MOVE_NOMONSTERS, player);
132         if(trace_startsolid)
133                 error("trace_endpos in solid!");
134         to = trace_endpos;
135
136         // ang_x stuff works around weird quake angles
137         if(player.classname == "player")
138         {
139                 ang = player.v_angle;
140                 /*
141                 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
142                 ang = Portal_Transform_Multiply(transform, ang);
143                 */
144
145                 // PLAYERS use different math
146                 ang_x = -ang_x;
147
148                 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
149
150                 fixedmakevectors(ang);
151                 old_forward = v_forward;
152                 old_up = v_up;
153                 fixedmakevectors(ang_y * '0 1 0');
154                 old_yawforward = v_forward;
155
156                 // their aiming directions are portalled...
157                 new_forward = Portal_Transform_Apply(transform, old_forward);
158                 new_up = Portal_Transform_Apply(transform, old_up);
159                 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
160
161                 // but now find a new sense of direction
162                 // this is NOT easy!
163                 // assume new_forward points straight up.
164                 // What is our yaw?
165                 //
166                 // new_up could now point forward OR backward... which direction to choose?
167
168                 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
169                 {
170                         // new_yawforward and new_yawup define the new aiming half-circle
171                         // we "just" need to find out whether new_up or -new_up is in that half circle
172                         ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
173                         if(new_up * new_yawforward < 0)
174                                 new_up = -1 * new_up;
175                         ang_y = vectoyaw(new_up); // this vector is the yaw we want
176                         //print("UP/DOWN path: ", vtos(ang), "\n");
177                 }
178                 else
179                 {
180                         // good angles; here, "forward" suffices
181                         ang = fixedvectoangles(new_forward);
182                         //print("GOOD path: ", vtos(ang), "\n");
183                 }
184
185                 ang_z = player.angles_z;
186         }
187         else
188         {
189                 ang = player.angles;
190                 ang_x = -ang_x;
191                 ang = Portal_Transform_Multiply(transform, player.angles);
192         }
193         ang_x = -ang_x;
194
195         // factor -1 allows chaining portals, but may be weird
196         player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
197
198         if(player.flagcarried)
199                 DropFlag(player.flagcarried);
200         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
201
202         // reset fade counter
203         if(!portals_must_not_break)
204                 error("Portal_TeleportPlayer ended with unprotected portals");
205         print("Portalled using ", etos(teleporter), " to ", etos(teleporter.enemy), "\n");
206         teleporter.portal_wants_to_vanish = 0;
207         teleporter.fade_time = time + 15;
208         teleporter.enemy.health = 300;
209
210         return 1;
211 }
212
213 float Portal_FindSafeOrigin(entity portal)
214 {
215         vector o;
216         o = portal.origin;
217         portal.mins = PL_MIN - '8 8 8';
218         portal.maxs = PL_MAX + '8 8 8';
219         fixedmakevectors(portal.angles);
220         portal.origin += 16 * v_forward;
221         if(!move_out_of_solid(portal))
222         {
223 #ifdef DEBUG
224                 print("NO SAFE ORIGIN\n");
225 #endif
226                 return 0;
227         }
228         portal.portal_safe_origin = portal.origin;
229         setorigin(portal, o);
230         return 1;
231 }
232
233 void Portal_Touch()
234 {
235         // portal is being removed?
236         if(self.solid != SOLID_TRIGGER)
237                 return; // possibly engine bug
238         
239         if(!self.enemy)
240                 error("Portal_Touch called for a broken portal\n");
241
242         if(trace_fraction < 1)
243                 return; // only handle TouchAreaGrid ones (only these can teleport)
244                 // for some unknown reason, this also gets collisions from SV_Impact sometimes
245
246         if(other.classname == "porto")
247         {
248                 if(other.portal_id == self.portal_id)
249                         return;
250         }
251         if(time < self.portal_activatetime)
252                 if(other == self.owner)
253                 {
254                         self.portal_activatetime = time + 0.1;
255                         return;
256                 }
257         if(other != self.owner)
258                 if(other.classname == "player")
259                         if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
260                                 return; // cannot go through someone else's portal
261         if(other.owner != self.owner)
262                 if(other.owner.classname == "player")
263                         if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
264                                 return; // cannot go through someone else's portal
265         fixedmakevectors(self.angles);
266         if((other.origin - self.origin) * v_forward < 0)
267                 return;
268         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
269         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
270         {
271                 // can't teleport this
272                 return;
273         }
274
275         print("Portal ID ", etos(self), " verified working.\n");
276         portals_must_not_break = 1;
277
278         if(Portal_TeleportPlayer(self, other))
279                 if(other.classname == "porto")
280                         if(other.effects & EF_RED)
281                                 other.effects += EF_BLUE - EF_RED;
282
283         portals_must_not_break = 0;
284 }
285
286 void Portal_Think();
287 void Portal_MakeBrokenPortal(entity portal)
288 {
289         if(portals_must_not_break)
290                 error("Trying to remove a portal during teleporting");
291
292         portal.solid = SOLID_NOT;
293         portal.touch = SUB_Null;
294         portal.think = SUB_Null;
295         portal.effects = 0;
296         //portal.colormod = '1 1 1';
297         portal.nextthink = 0;
298         portal.takedamage = DAMAGE_NO;
299 }
300
301 void Portal_MakeWaitingPortal(entity portal)
302 {
303         portal.solid = SOLID_NOT;
304         portal.touch = SUB_Null;
305         portal.think = SUB_Null;
306         portal.effects = EF_ADDITIVE;
307         portal.colormod = '1 1 1';
308         portal.nextthink = 0;
309         portal.takedamage = DAMAGE_YES;
310 }
311
312 void Portal_MakeInPortal(entity portal)
313 {
314         portal.solid = SOLID_TRIGGER;
315         portal.touch = Portal_Touch;
316         portal.think = Portal_Think;
317         portal.effects = EF_RED;
318         portal.colormod = '1 0 0';
319         portal.nextthink = time;
320         portal.takedamage = DAMAGE_NO;
321 }
322
323 void Portal_MakeOutPortal(entity portal)
324 {
325         portal.solid = SOLID_NOT;
326         portal.touch = SUB_Null;
327         portal.think = SUB_Null;
328         portal.effects = EF_STARDUST | EF_BLUE;
329         portal.colormod = '0 0 1';
330         portal.nextthink = 0;
331         portal.takedamage = DAMAGE_YES;
332 }
333
334 void Portal_Disconnect(entity teleporter, entity destination)
335 {
336         teleporter.enemy = world;
337         destination.enemy = world;
338         Portal_MakeBrokenPortal(teleporter);
339         Portal_MakeBrokenPortal(destination);
340 }
341
342 void Portal_Connect(entity teleporter, entity destination)
343 {
344         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
345
346 #ifdef DEBUG
347         {
348                 // let's verify the transform
349                 vector in_f, in_r, in_u;
350                 vector out_f, out_r, out_u;
351                 fixedmakevectors(teleporter.angles);
352                 in_f = v_forward;
353                 in_r = v_right;
354                 in_u = v_up;
355                 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
356                 fixedmakevectors(destination.angles);
357                 out_f = v_forward;
358                 out_r = v_right;
359                 out_u = v_up;
360                 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
361                 // INTENDED TRANSFORM:
362                 //   in_f -> -out_f
363                 //   in_r -> -out_r
364                 //   in_u -> +out_u
365                 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
366                 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
367                 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
368
369                 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
370                 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
371         }
372 #endif
373
374         teleporter.enemy = destination;
375         destination.enemy = teleporter;
376         Portal_MakeInPortal(teleporter);
377         Portal_MakeOutPortal(destination);
378         teleporter.fade_time = time + 15;
379         destination.fade_time = time + 15;
380         teleporter.portal_wants_to_vanish = 0;
381         destination.portal_wants_to_vanish = 0;
382 }
383
384 void Portal_Remove(entity portal, float killed)
385 {
386         entity e;
387         e = portal.enemy;
388
389         if(portals_must_not_break)
390                 error("Trying to remove a portal during teleporting");
391
392         if(e)
393         {
394                 Portal_Disconnect(portal, e);
395                 Portal_Remove(e, killed);
396         }
397
398         if(portal == portal.owner.portal_in)
399                 portal.owner.portal_in = world;
400         if(portal == portal.owner.portal_out)
401                 portal.owner.portal_out = world;
402         portal.owner = world;
403
404         // makes the portal vanish
405         if(killed)
406         {
407                 fixedmakevectors(portal.angles);
408                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
409                 remove(portal);
410         }
411         else
412         {
413                 Portal_MakeBrokenPortal(portal);
414                 SUB_SetFade(portal, time, 0.5);
415         }
416 }
417
418 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
419 {
420         if(deathtype == DEATH_TELEFRAG)
421                 return;
422         if(attacker != self.owner)
423                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
424                         return;
425         self.health -= damage;
426         if(self.health < 0)
427                 Portal_Remove(self, 1);
428 }
429
430 void Portal_Think()
431 {
432         entity e, o;
433
434         // portal is being removed?
435         if(self.solid != SOLID_TRIGGER)
436                 return; // possibly engine bug
437
438         if(!self.enemy)
439                 error("Portal_Think called for a broken portal\n");
440
441         print("Portal ID ", etos(self), " verified working.\n");
442         portals_must_not_break = 1;
443
444         o = self.owner;
445         self.solid = SOLID_BBOX;
446         self.owner = world;
447         FOR_EACH_PLAYER(e)
448         {
449                 if(time < self.portal_activatetime)
450                         if(e == o)
451                                 continue;
452                 if(e != o)
453                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
454                                 continue; // cannot go through someone else's portal
455                 // if e would hit the portal in a frame...
456                 // already teleport him
457                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
458                 if(trace_ent == self)
459                         Portal_TeleportPlayer(self, e);
460         }
461         self.solid = SOLID_TRIGGER;
462         self.owner = o;
463
464         portals_must_not_break = 0;
465
466         self.nextthink = time;
467
468         if(time > self.fade_time)
469                 Portal_Remove(self, 0);
470 }
471
472 float Portal_Customize()
473 {
474         if(other.classname == "spectator")
475                 other = other.enemy;
476         if(other == self.owner)
477         {
478                 self.modelindex = self.modelindex_lod0;
479         }
480         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
481         {
482                 self.modelindex = 0;
483         }
484         else
485         {
486                 self.modelindex = self.modelindex_lod0;
487         }
488         return TRUE;
489 }
490
491 // cleanup:
492 //   when creating in-portal:
493 //     disconnect
494 //     clear existing in-portal
495 //     set as in-portal
496 //     connect
497 //   when creating out-portal:
498 //     disconnect
499 //     clear existing out-portal
500 //     set as out-portal
501 //   when player dies:
502 //     disconnect portals
503 //     clear both portals
504 //   after timeout of in-portal:
505 //     disconnect portals
506 //     clear both portals
507 //   TODO: ensure only one portal shot at once
508 float Portal_SetInPortal(entity own, entity portal)
509 {
510         if(own.portal_in)
511         {
512                 if(own.portal_out)
513                         Portal_Disconnect(own.portal_in, own.portal_out);
514                 Portal_Remove(own.portal_in, 0);
515         }
516         own.portal_in = portal;
517         if(own.portal_out)
518         {
519                 own.portal_out.portal_id = portal.portal_id;
520                 Portal_Connect(own.portal_in, own.portal_out);
521         }
522         return 2;
523 }
524 float Portal_SetOutPortal(entity own, entity portal)
525 {
526         if(own.portal_out)
527         {
528                 if(own.portal_in)
529                         Portal_Disconnect(own.portal_in, own.portal_out);
530                 Portal_Remove(own.portal_out, 0);
531         }
532         own.portal_out = portal;
533         if(own.portal_in)
534         {
535                 own.portal_in.portal_id = portal.portal_id;
536                 Portal_Connect(own.portal_in, own.portal_out);
537         }
538         return 1;
539 }
540 void Portal_ClearAll(entity own)
541 {
542         if(own.portal_in)
543                 Portal_Remove(own.portal_in, 0);
544         if(own.portal_out)
545                 Portal_Remove(own.portal_out, 0);
546 }
547 void Portal_ClearWithID(entity own, float id)
548 {
549         if(own.portal_in)
550                 if(own.portal_in.portal_id == id)
551                 {
552                         if(own.portal_out)
553                                 Portal_Disconnect(own.portal_in, own.portal_out);
554                         Portal_Remove(own.portal_in, 0);
555                 }
556         if(own.portal_out)
557                 if(own.portal_out.portal_id == id)
558                 {
559                         if(own.portal_in)
560                                 Portal_Disconnect(own.portal_in, own.portal_out);
561                         Portal_Remove(own.portal_out, 0);
562                 }
563 }
564
565 entity Portal_Spawn(entity own, vector org, vector ang)
566 {
567         entity portal;
568
569         fixedmakevectors(ang);
570         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
571                 return world;
572
573         portal = spawn();
574         portal.classname = "portal";
575         portal.owner = own;
576         portal.origin = org;
577         portal.angles = ang;
578         portal.think = Portal_Think;
579         portal.nextthink = 0;
580         portal.fade_time = time + 15;
581         portal.portal_activatetime = time + 0.1;
582         portal.event_damage = Portal_Damage;
583         portal.health = 300;
584         setmodel(portal, "models/portal.md3");
585         portal.modelindex_lod0 = portal.modelindex;
586         portal.customizeentityforclient = Portal_Customize;
587
588         if(!Portal_FindSafeOrigin(portal))
589         {
590                 remove(portal);
591                 return world;
592         }
593
594         setsize(portal, '-48 -48 -48', '48 48 48');
595         Portal_MakeWaitingPortal(portal);
596
597         return portal;
598 }
599
600 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
601 {
602         entity portal;
603         vector ang;
604         vector org;
605
606         if(trace_ent.movetype == MOVETYPE_WALK)
607         {
608                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
609                 trace_plane_normal = '0 0 1';
610                 dir = -1 * dir; // create telefrag portals the other way round
611         }
612
613         org = trace_endpos;
614         ang = fixedvectoangles2(trace_plane_normal, dir);
615         fixedmakevectors(ang);
616
617         portal = Portal_Spawn(own, org, ang);
618         if(!portal)
619         {
620                 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
621                         Portal_ClearAll(own);
622                 return 0;
623         }
624
625         portal.portal_id = portal_id_val;
626         Portal_SetInPortal(own, portal);
627
628         return 1;
629 }
630
631 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
632 {
633         entity portal;
634         vector ang;
635         vector org;
636
637         if(trace_ent.movetype == MOVETYPE_WALK)
638         {
639                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
640                 trace_plane_normal = '0 0 1';
641                 dir = -1 * dir; // create telefrag portals the other way round
642         }
643
644         org = trace_endpos;
645         ang = fixedvectoangles2(trace_plane_normal, dir);
646         fixedmakevectors(ang);
647
648         portal = Portal_Spawn(own, org, ang);
649         if(!portal)
650         {
651                 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
652                         Portal_ClearAll(own);
653                 return 0;
654         }
655
656         portal.portal_id = portal_id_val;
657         Portal_SetOutPortal(own, portal);
658
659         return 1;
660 }