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