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