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