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