]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
sounds by Tenshihan for the Port-O-Launch... but not perfect yet
[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_x = -ang_x;
186                 ang_z = player.angles_z;
187         }
188         else
189         {
190                 ang = player.angles;
191                 ang = Portal_Transform_Multiply(transform, player.angles);
192         }
193
194         // factor -1 allows chaining portals, but may be weird
195         player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
196
197         if(player.flagcarried)
198                 DropFlag(player.flagcarried);
199         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
200
201         // reset fade counter
202         if(!portals_must_not_break)
203                 error("Portal_TeleportPlayer ended with unprotected portals");
204         print("Portalled using ", etos(teleporter), " to ", etos(teleporter.enemy), "\n");
205         teleporter.portal_wants_to_vanish = 0;
206         teleporter.fade_time = time + 15;
207         teleporter.enemy.health = 300;
208
209         return 1;
210 }
211
212 float Portal_FindSafeOrigin(entity portal)
213 {
214         vector o;
215         o = portal.origin;
216         portal.mins = PL_MIN - '8 8 8';
217         portal.maxs = PL_MAX + '8 8 8';
218         fixedmakevectors(portal.angles);
219         portal.origin += 16 * v_forward;
220         if(!move_out_of_solid(portal))
221         {
222 #ifdef DEBUG
223                 print("NO SAFE ORIGIN\n");
224 #endif
225                 return 0;
226         }
227         portal.portal_safe_origin = portal.origin;
228         setorigin(portal, o);
229         return 1;
230 }
231
232 void Portal_Touch()
233 {
234         // portal is being removed?
235         if(self.solid != SOLID_TRIGGER)
236                 return; // possibly engine bug
237         
238         if(!self.enemy)
239                 error("Portal_Touch called for a broken portal\n");
240
241         if(trace_fraction < 1)
242                 return; // only handle TouchAreaGrid ones (only these can teleport)
243                 // for some unknown reason, this also gets collisions from SV_Impact sometimes
244
245         if(other.classname == "porto")
246         {
247                 if(other.portal_id == self.portal_id)
248                         return;
249         }
250         if(time < self.portal_activatetime)
251                 if(other == self.owner)
252                 {
253                         self.portal_activatetime = time + 0.1;
254                         return;
255                 }
256         if(other != self.owner)
257                 if(other.classname == "player")
258                         if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
259                                 return; // cannot go through someone else's portal
260         if(other.owner != self.owner)
261                 if(other.owner.classname == "player")
262                         if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
263                                 return; // cannot go through someone else's portal
264         fixedmakevectors(self.angles);
265         if((other.origin - self.origin) * v_forward < 0)
266                 return;
267         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
268         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
269         {
270                 // can't teleport this
271                 return;
272         }
273
274         print("Portal ID ", etos(self), " verified working.\n");
275         portals_must_not_break = 1;
276
277         if(Portal_TeleportPlayer(self, other))
278                 if(other.classname == "porto")
279                         if(other.effects & EF_RED)
280                                 other.effects += EF_BLUE - EF_RED;
281
282         portals_must_not_break = 0;
283 }
284
285 void Portal_Think();
286 void Portal_MakeBrokenPortal(entity portal)
287 {
288         if(portals_must_not_break)
289                 error("Trying to remove a portal during teleporting");
290
291         portal.solid = SOLID_NOT;
292         portal.touch = SUB_Null;
293         portal.think = SUB_Null;
294         portal.effects = 0;
295         //portal.colormod = '1 1 1';
296         portal.nextthink = 0;
297         portal.takedamage = DAMAGE_NO;
298 }
299
300 void Portal_MakeWaitingPortal(entity portal)
301 {
302         portal.solid = SOLID_NOT;
303         portal.touch = SUB_Null;
304         portal.think = SUB_Null;
305         portal.effects = EF_ADDITIVE;
306         portal.colormod = '1 1 1';
307         portal.nextthink = 0;
308         portal.takedamage = DAMAGE_YES;
309 }
310
311 void Portal_MakeInPortal(entity portal)
312 {
313         portal.solid = SOLID_TRIGGER;
314         portal.touch = Portal_Touch;
315         portal.think = Portal_Think;
316         portal.effects = EF_RED;
317         portal.colormod = '1 0 0';
318         portal.nextthink = time;
319         portal.takedamage = DAMAGE_NO;
320 }
321
322 void Portal_MakeOutPortal(entity portal)
323 {
324         portal.solid = SOLID_NOT;
325         portal.touch = SUB_Null;
326         portal.think = SUB_Null;
327         portal.effects = EF_STARDUST | EF_BLUE;
328         portal.colormod = '0 0 1';
329         portal.nextthink = 0;
330         portal.takedamage = DAMAGE_YES;
331 }
332
333 void Portal_Disconnect(entity teleporter, entity destination)
334 {
335         teleporter.enemy = world;
336         destination.enemy = world;
337         Portal_MakeBrokenPortal(teleporter);
338         Portal_MakeBrokenPortal(destination);
339 }
340
341 void Portal_Connect(entity teleporter, entity destination)
342 {
343         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
344
345 #ifdef DEBUG
346         {
347                 // let's verify the transform
348                 vector in_f, in_r, in_u;
349                 vector out_f, out_r, out_u;
350                 fixedmakevectors(teleporter.angles);
351                 in_f = v_forward;
352                 in_r = v_right;
353                 in_u = v_up;
354                 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
355                 fixedmakevectors(destination.angles);
356                 out_f = v_forward;
357                 out_r = v_right;
358                 out_u = v_up;
359                 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
360                 // INTENDED TRANSFORM:
361                 //   in_f -> -out_f
362                 //   in_r -> -out_r
363                 //   in_u -> +out_u
364                 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
365                 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
366                 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
367
368                 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
369                 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
370         }
371 #endif
372
373         teleporter.enemy = destination;
374         destination.enemy = teleporter;
375         Portal_MakeInPortal(teleporter);
376         Portal_MakeOutPortal(destination);
377         teleporter.fade_time = time + 15;
378         destination.fade_time = time + 15;
379         teleporter.portal_wants_to_vanish = 0;
380         destination.portal_wants_to_vanish = 0;
381 }
382
383 void Portal_Remove(entity portal, float killed)
384 {
385         entity e;
386         e = portal.enemy;
387
388         if(portals_must_not_break)
389                 error("Trying to remove a portal during teleporting");
390
391         if(e)
392         {
393                 Portal_Disconnect(portal, e);
394                 Portal_Remove(e, killed);
395         }
396
397         if(portal == portal.owner.portal_in)
398                 portal.owner.portal_in = world;
399         if(portal == portal.owner.portal_out)
400                 portal.owner.portal_out = world;
401         //portal.owner = world;
402
403         // makes the portal vanish
404         if(killed)
405         {
406                 fixedmakevectors(portal.angles);
407                 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
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                 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
415                 SUB_SetFade(portal, time, 0.5);
416         }
417 }
418
419 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
420 {
421         if(deathtype == DEATH_TELEFRAG)
422                 return;
423         if(attacker != self.owner)
424                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
425                         return;
426         self.health -= damage;
427         if(self.health < 0)
428                 Portal_Remove(self, 1);
429 }
430
431 void Portal_Think()
432 {
433         entity e, o;
434
435         // portal is being removed?
436         if(self.solid != SOLID_TRIGGER)
437                 return; // possibly engine bug
438
439         if(!self.enemy)
440                 error("Portal_Think called for a broken portal\n");
441
442         //print("Portal ID ", etos(self), " verified working.\n");
443         portals_must_not_break = 1;
444
445         o = self.owner;
446         self.solid = SOLID_BBOX;
447         self.owner = world;
448         FOR_EACH_PLAYER(e)
449         {
450                 if(time < self.portal_activatetime)
451                         if(e == o)
452                                 continue;
453                 if(e != o)
454                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
455                                 continue; // cannot go through someone else's portal
456                 // if e would hit the portal in a frame...
457                 // already teleport him
458                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
459                 if(trace_ent == self)
460                         Portal_TeleportPlayer(self, e);
461         }
462         self.solid = SOLID_TRIGGER;
463         self.owner = o;
464
465         portals_must_not_break = 0;
466
467         self.nextthink = time;
468
469         if(time > self.fade_time)
470                 Portal_Remove(self, 0);
471 }
472
473 float Portal_Customize()
474 {
475         if(other.classname == "spectator")
476                 other = other.enemy;
477         if(other == self.owner)
478         {
479                 self.modelindex = self.modelindex_lod0;
480         }
481         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
482         {
483                 self.modelindex = 0;
484         }
485         else
486         {
487                 self.modelindex = self.modelindex_lod0;
488         }
489         return TRUE;
490 }
491
492 // cleanup:
493 //   when creating in-portal:
494 //     disconnect
495 //     clear existing in-portal
496 //     set as in-portal
497 //     connect
498 //   when creating out-portal:
499 //     disconnect
500 //     clear existing out-portal
501 //     set as out-portal
502 //   when player dies:
503 //     disconnect portals
504 //     clear both portals
505 //   after timeout of in-portal:
506 //     disconnect portals
507 //     clear both portals
508 //   TODO: ensure only one portal shot at once
509 float Portal_SetInPortal(entity own, entity portal)
510 {
511         if(own.portal_in)
512         {
513                 if(own.portal_out)
514                         Portal_Disconnect(own.portal_in, own.portal_out);
515                 Portal_Remove(own.portal_in, 0);
516         }
517         own.portal_in = portal;
518         if(own.portal_out)
519         {
520                 own.portal_out.portal_id = portal.portal_id;
521                 Portal_Connect(own.portal_in, own.portal_out);
522         }
523         return 2;
524 }
525 float Portal_SetOutPortal(entity own, entity portal)
526 {
527         if(own.portal_out)
528         {
529                 if(own.portal_in)
530                         Portal_Disconnect(own.portal_in, own.portal_out);
531                 Portal_Remove(own.portal_out, 0);
532         }
533         own.portal_out = portal;
534         if(own.portal_in)
535         {
536                 own.portal_in.portal_id = portal.portal_id;
537                 Portal_Connect(own.portal_in, own.portal_out);
538         }
539         return 1;
540 }
541 void Portal_ClearAll(entity own)
542 {
543         if(own.portal_in)
544                 Portal_Remove(own.portal_in, 0);
545         if(own.portal_out)
546                 Portal_Remove(own.portal_out, 0);
547 }
548 void Portal_RemoveLater_Think()
549 {
550         Portal_Remove(self, self.cnt);
551 }
552 void Portal_RemoveLater(entity portal, float kill)
553 {
554         Portal_MakeBrokenPortal(portal);
555         portal.cnt = kill;
556         portal.think = Portal_RemoveLater_Think;
557         portal.nextthink = time;
558 }
559 void Portal_ClearAllLater(entity own)
560 {
561         if(own.portal_in)
562                 Portal_RemoveLater(own.portal_in, 0);
563         if(own.portal_out)
564                 Portal_RemoveLater(own.portal_out, 0);
565 }
566 void Portal_ClearWithID(entity own, float id)
567 {
568         if(own.portal_in)
569                 if(own.portal_in.portal_id == id)
570                 {
571                         if(own.portal_out)
572                                 Portal_Disconnect(own.portal_in, own.portal_out);
573                         Portal_Remove(own.portal_in, 0);
574                 }
575         if(own.portal_out)
576                 if(own.portal_out.portal_id == id)
577                 {
578                         if(own.portal_in)
579                                 Portal_Disconnect(own.portal_in, own.portal_out);
580                         Portal_Remove(own.portal_out, 0);
581                 }
582 }
583
584 entity Portal_Spawn(entity own, vector org, vector ang)
585 {
586         entity portal;
587
588         fixedmakevectors(ang);
589         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
590                 return world;
591
592         portal = spawn();
593         portal.classname = "portal";
594         portal.owner = own;
595         portal.origin = org;
596         portal.angles = ang;
597         portal.think = Portal_Think;
598         portal.nextthink = 0;
599         portal.fade_time = time + 15;
600         portal.portal_activatetime = time + 0.1;
601         portal.event_damage = Portal_Damage;
602         portal.health = 300;
603         setmodel(portal, "models/portal.md3");
604         portal.modelindex_lod0 = portal.modelindex;
605         portal.customizeentityforclient = Portal_Customize;
606
607         if(!Portal_FindSafeOrigin(portal))
608         {
609                 remove(portal);
610                 return world;
611         }
612
613         setsize(portal, '-48 -48 -48', '48 48 48');
614         Portal_MakeWaitingPortal(portal);
615
616         return portal;
617 }
618
619 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
620 {
621         entity portal;
622         vector ang;
623         vector org;
624
625         org = trace_endpos;
626         ang = fixedvectoangles2(trace_plane_normal, dir);
627         fixedmakevectors(ang);
628
629         portal = Portal_Spawn(own, org, ang);
630         if(!portal)
631         {
632                 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
633                         Portal_ClearAll(own);
634                 return 0;
635         }
636
637         portal.portal_id = portal_id_val;
638         Portal_SetInPortal(own, portal);
639
640         return 1;
641 }
642
643 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
644 {
645         entity portal;
646         vector ang;
647         vector org;
648
649         org = trace_endpos;
650         ang = fixedvectoangles2(trace_plane_normal, dir);
651         fixedmakevectors(ang);
652
653         portal = Portal_Spawn(own, org, ang);
654         if(!portal)
655         {
656                 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
657                         Portal_ClearAll(own);
658                 return 0;
659         }
660
661         portal.portal_id = portal_id_val;
662         Portal_SetOutPortal(own, portal);
663
664         return 1;
665 }