]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
again: remove portals when owner dies, but this time without crash
[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                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
408                 remove(portal);
409         }
410         else
411         {
412                 Portal_MakeBrokenPortal(portal);
413                 SUB_SetFade(portal, time, 0.5);
414         }
415 }
416
417 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
418 {
419         if(deathtype == DEATH_TELEFRAG)
420                 return;
421         if(attacker != self.owner)
422                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
423                         return;
424         self.health -= damage;
425         if(self.health < 0)
426                 Portal_Remove(self, 1);
427 }
428
429 void Portal_Think()
430 {
431         entity e, o;
432
433         // portal is being removed?
434         if(self.solid != SOLID_TRIGGER)
435                 return; // possibly engine bug
436
437         if(!self.enemy)
438                 error("Portal_Think called for a broken portal\n");
439
440         //print("Portal ID ", etos(self), " verified working.\n");
441         portals_must_not_break = 1;
442
443         o = self.owner;
444         self.solid = SOLID_BBOX;
445         self.owner = world;
446         FOR_EACH_PLAYER(e)
447         {
448                 if(time < self.portal_activatetime)
449                         if(e == o)
450                                 continue;
451                 if(e != o)
452                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
453                                 continue; // cannot go through someone else's portal
454                 // if e would hit the portal in a frame...
455                 // already teleport him
456                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
457                 if(trace_ent == self)
458                         Portal_TeleportPlayer(self, e);
459         }
460         self.solid = SOLID_TRIGGER;
461         self.owner = o;
462
463         portals_must_not_break = 0;
464
465         self.nextthink = time;
466
467         if(time > self.fade_time)
468                 Portal_Remove(self, 0);
469 }
470
471 float Portal_Customize()
472 {
473         if(other.classname == "spectator")
474                 other = other.enemy;
475         if(other == self.owner)
476         {
477                 self.modelindex = self.modelindex_lod0;
478         }
479         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
480         {
481                 self.modelindex = 0;
482         }
483         else
484         {
485                 self.modelindex = self.modelindex_lod0;
486         }
487         return TRUE;
488 }
489
490 // cleanup:
491 //   when creating in-portal:
492 //     disconnect
493 //     clear existing in-portal
494 //     set as in-portal
495 //     connect
496 //   when creating out-portal:
497 //     disconnect
498 //     clear existing out-portal
499 //     set as out-portal
500 //   when player dies:
501 //     disconnect portals
502 //     clear both portals
503 //   after timeout of in-portal:
504 //     disconnect portals
505 //     clear both portals
506 //   TODO: ensure only one portal shot at once
507 float Portal_SetInPortal(entity own, entity portal)
508 {
509         if(own.portal_in)
510         {
511                 if(own.portal_out)
512                         Portal_Disconnect(own.portal_in, own.portal_out);
513                 Portal_Remove(own.portal_in, 0);
514         }
515         own.portal_in = portal;
516         if(own.portal_out)
517         {
518                 own.portal_out.portal_id = portal.portal_id;
519                 Portal_Connect(own.portal_in, own.portal_out);
520         }
521         return 2;
522 }
523 float Portal_SetOutPortal(entity own, entity portal)
524 {
525         if(own.portal_out)
526         {
527                 if(own.portal_in)
528                         Portal_Disconnect(own.portal_in, own.portal_out);
529                 Portal_Remove(own.portal_out, 0);
530         }
531         own.portal_out = portal;
532         if(own.portal_in)
533         {
534                 own.portal_in.portal_id = portal.portal_id;
535                 Portal_Connect(own.portal_in, own.portal_out);
536         }
537         return 1;
538 }
539 void Portal_ClearAll(entity own)
540 {
541         if(own.portal_in)
542                 Portal_Remove(own.portal_in, 0);
543         if(own.portal_out)
544                 Portal_Remove(own.portal_out, 0);
545 }
546 void Portal_RemoveLater_Think()
547 {
548         Portal_Remove(self, self.cnt);
549 }
550 void Portal_RemoveLater(entity portal, float kill)
551 {
552         Portal_MakeBrokenPortal(portal);
553         portal.cnt = kill;
554         portal.think = Portal_RemoveLater_Think;
555         portal.nextthink = time;
556 }
557 void Portal_ClearAllLater(entity own)
558 {
559         if(own.portal_in)
560                 Portal_RemoveLater(own.portal_in, 0);
561         if(own.portal_out)
562                 Portal_RemoveLater(own.portal_out, 0);
563 }
564 void Portal_ClearWithID(entity own, float id)
565 {
566         if(own.portal_in)
567                 if(own.portal_in.portal_id == id)
568                 {
569                         if(own.portal_out)
570                                 Portal_Disconnect(own.portal_in, own.portal_out);
571                         Portal_Remove(own.portal_in, 0);
572                 }
573         if(own.portal_out)
574                 if(own.portal_out.portal_id == id)
575                 {
576                         if(own.portal_in)
577                                 Portal_Disconnect(own.portal_in, own.portal_out);
578                         Portal_Remove(own.portal_out, 0);
579                 }
580 }
581
582 entity Portal_Spawn(entity own, vector org, vector ang)
583 {
584         entity portal;
585
586         fixedmakevectors(ang);
587         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
588                 return world;
589
590         portal = spawn();
591         portal.classname = "portal";
592         portal.owner = own;
593         portal.origin = org;
594         portal.angles = ang;
595         portal.think = Portal_Think;
596         portal.nextthink = 0;
597         portal.fade_time = time + 15;
598         portal.portal_activatetime = time + 0.1;
599         portal.event_damage = Portal_Damage;
600         portal.health = 300;
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(!self.portal_out || self.portal_out.portal_id == portal_id_val)
631                         Portal_ClearAll(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(!self.portal_in || self.portal_in.portal_id == portal_id_val)
655                         Portal_ClearAll(own);
656                 return 0;
657         }
658
659         portal.portal_id = portal_id_val;
660         Portal_SetOutPortal(own, portal);
661
662         return 1;
663 }