]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
support for announcer/male/impressive and announcer/male/headshot ;)
[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 + cvar("g_balance_portal_lifetime");
203         teleporter.health = cvar("g_balance_portal_health");
204         teleporter.enemy.health = cvar("g_balance_portal_health");
205
206         return 1;
207 }
208
209 float Portal_FindSafeOrigin(entity portal)
210 {
211         vector o;
212         o = portal.origin;
213         portal.mins = PL_MIN - SAFERNUDGE;
214         portal.maxs = PL_MAX + SAFERNUDGE;
215         fixedmakevectors(portal.angles);
216         portal.origin += 16 * v_forward;
217         if(!move_out_of_solid(portal))
218         {
219 #ifdef DEBUG
220                 print("NO SAFE ORIGIN\n");
221 #endif
222                 return 0;
223         }
224         portal.portal_safe_origin = portal.origin;
225         setorigin(portal, o);
226         return 1;
227 }
228
229 void Portal_Touch()
230 {
231         // portal is being removed?
232         if(self.solid != SOLID_TRIGGER)
233                 return; // possibly engine bug
234         
235         if(!self.enemy)
236                 error("Portal_Touch called for a broken portal\n");
237
238         if(trace_fraction < 1)
239                 return; // only handle TouchAreaGrid ones (only these can teleport)
240                 // for some unknown reason, this also gets collisions from SV_Impact sometimes
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                 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
394                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
395                 remove(portal);
396         }
397         else
398         {
399                 Portal_MakeBrokenPortal(portal);
400                 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
401                 SUB_SetFade(portal, time, 0.5);
402         }
403 }
404
405 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
406 {
407         if(deathtype == DEATH_TELEFRAG)
408                 return;
409         if(attacker != self.owner)
410                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
411                         return;
412         self.health -= damage;
413         if(self.health < 0)
414                 Portal_Remove(self, 1);
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                 error("Portal_Think called for a broken portal\n");
427
428         o = self.owner;
429         self.solid = SOLID_BBOX;
430         self.owner = world;
431
432         fixedmakevectors(self.angles);
433
434         FOR_EACH_PLAYER(e)
435         {
436                 if(time < self.portal_activatetime)
437                         if(e == o)
438                                 continue;
439                 if(e != o)
440                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
441                                 continue; // cannot go through someone else's portal
442                 if((e.origin - self.origin) * v_forward < 0) // wrong side of the plane? no teleport
443                         continue;
444
445                 // if e would hit the portal in a frame...
446                 // already teleport him
447                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
448                 if(trace_ent == self)
449                         Portal_TeleportPlayer(self, e);
450         }
451         self.solid = SOLID_TRIGGER;
452         self.owner = o;
453
454         self.nextthink = time;
455
456         if(time > self.fade_time)
457                 Portal_Remove(self, 0);
458 }
459
460 float Portal_Customize()
461 {
462         if(other.classname == "spectator")
463                 other = other.enemy;
464         if(other == self.owner)
465         {
466                 self.modelindex = self.modelindex_lod0;
467         }
468         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
469         {
470                 self.modelindex = 0;
471         }
472         else
473         {
474                 self.modelindex = self.modelindex_lod0;
475         }
476         return TRUE;
477 }
478
479 // cleanup:
480 //   when creating in-portal:
481 //     disconnect
482 //     clear existing in-portal
483 //     set as in-portal
484 //     connect
485 //   when creating out-portal:
486 //     disconnect
487 //     clear existing out-portal
488 //     set as out-portal
489 //   when player dies:
490 //     disconnect portals
491 //     clear both portals
492 //   after timeout of in-portal:
493 //     disconnect portals
494 //     clear both portals
495 //   TODO: ensure only one portal shot at once
496 float Portal_SetInPortal(entity own, entity portal)
497 {
498         if(own.portal_in)
499         {
500                 if(own.portal_out)
501                         Portal_Disconnect(own.portal_in, own.portal_out);
502                 Portal_Remove(own.portal_in, 0);
503         }
504         own.portal_in = portal;
505         if(own.portal_out)
506         {
507                 own.portal_out.portal_id = portal.portal_id;
508                 Portal_Connect(own.portal_in, own.portal_out);
509         }
510         return 2;
511 }
512 float Portal_SetOutPortal(entity own, entity portal)
513 {
514         if(own.portal_out)
515         {
516                 if(own.portal_in)
517                         Portal_Disconnect(own.portal_in, own.portal_out);
518                 Portal_Remove(own.portal_out, 0);
519         }
520         own.portal_out = portal;
521         if(own.portal_in)
522         {
523                 own.portal_in.portal_id = portal.portal_id;
524                 Portal_Connect(own.portal_in, own.portal_out);
525         }
526         return 1;
527 }
528 void Portal_ClearAll_PortalsOnly(entity own)
529 {
530         if(own.portal_in)
531                 Portal_Remove(own.portal_in, 0);
532         if(own.portal_out)
533                 Portal_Remove(own.portal_out, 0);
534 }
535 void Portal_ClearAll(entity own)
536 {
537         Portal_ClearAll_PortalsOnly(own);
538         W_Porto_Remove(own);
539 }
540 void Portal_RemoveLater_Think()
541 {
542         Portal_Remove(self, self.cnt);
543 }
544 void Portal_RemoveLater(entity portal, float kill)
545 {
546         Portal_MakeBrokenPortal(portal);
547         portal.cnt = kill;
548         portal.think = Portal_RemoveLater_Think;
549         portal.nextthink = time;
550 }
551 void Portal_ClearAllLater_PortalsOnly(entity own)
552 {
553         if(own.portal_in)
554                 Portal_RemoveLater(own.portal_in, 0);
555         if(own.portal_out)
556                 Portal_RemoveLater(own.portal_out, 0);
557 }
558 void Portal_ClearAllLater(entity own)
559 {
560         Portal_ClearAllLater_PortalsOnly(own);
561         W_Porto_Remove(own);
562 }
563 void Portal_ClearWithID(entity own, float id)
564 {
565         if(own.portal_in)
566                 if(own.portal_in.portal_id == id)
567                 {
568                         if(own.portal_out)
569                                 Portal_Disconnect(own.portal_in, own.portal_out);
570                         Portal_Remove(own.portal_in, 0);
571                 }
572         if(own.portal_out)
573                 if(own.portal_out.portal_id == id)
574                 {
575                         if(own.portal_in)
576                                 Portal_Disconnect(own.portal_in, own.portal_out);
577                         Portal_Remove(own.portal_out, 0);
578                 }
579 }
580
581 entity Portal_Spawn(entity own, vector org, vector ang)
582 {
583         entity portal;
584
585         fixedmakevectors(ang);
586         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
587                 return world;
588
589         portal = spawn();
590         portal.classname = "portal";
591         portal.owner = own;
592         portal.origin = org;
593         portal.angles = ang;
594         portal.think = Portal_Think;
595         portal.nextthink = 0;
596         portal.portal_activatetime = time + 1.1;
597         portal.event_damage = Portal_Damage;
598         portal.health = 200;
599         portal.fade_time = time + cvar("g_balance_portal_lifetime");
600         portal.health = cvar("g_balance_portal_health");
601         setmodel(portal, "models/portal.md3");
602         portal.modelindex_lod0 = portal.modelindex;
603         portal.customizeentityforclient = Portal_Customize;
604
605         if(!Portal_FindSafeOrigin(portal))
606         {
607                 remove(portal);
608                 return world;
609         }
610
611         setsize(portal, '-48 -48 -48', '48 48 48');
612         Portal_MakeWaitingPortal(portal);
613
614         return portal;
615 }
616
617 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
618 {
619         entity portal;
620         vector ang;
621         vector org;
622
623         org = trace_endpos;
624         ang = fixedvectoangles2(trace_plane_normal, dir);
625         fixedmakevectors(ang);
626
627         portal = Portal_Spawn(own, org, ang);
628         if(!portal)
629         {
630                 // if(!own.portal_out || own.portal_out.portal_id == portal_id_val)
631                         Portal_ClearAll_PortalsOnly(own);
632                 return 0;
633         }
634
635         portal.portal_id = portal_id_val;
636         Portal_SetInPortal(own, portal);
637
638         return 1;
639 }
640
641 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
642 {
643         entity portal;
644         vector ang;
645         vector org;
646
647         org = trace_endpos;
648         ang = fixedvectoangles2(trace_plane_normal, dir);
649         fixedmakevectors(ang);
650
651         portal = Portal_Spawn(own, org, ang);
652         if(!portal)
653         {
654                 // if(!own.portal_in || own.portal_in.portal_id == portal_id_val)
655                         Portal_ClearAll_PortalsOnly(own);
656                 return 0;
657         }
658
659         portal.portal_id = portal_id_val;
660         Portal_SetOutPortal(own, portal);
661
662         return 1;
663 }