]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
more fixes - now we can have a companion cube :P
[divverent/nexuiz.git] / data / qcsrc / server / portals.qc
1 #define SAFENUDGE '1 1 1'
2 #define SAFERNUDGE '8 8 8'
3
4 .vector portal_transform;
5 .vector portal_safe_origin;
6 .float portal_wants_to_vanish;
7 .float portal_activatetime;
8
9 vector Portal_Transform_Apply(vector transform, vector v)
10 {
11         fixedmakevectors(transform);
12         return v_forward * v_x
13              + v_right   * (-v_y)
14                  + v_up      * v_z;
15 }
16
17 vector Portal_Transform_Multiply(vector t1, vector t2)
18 {
19         vector m_forward, m_up;
20         fixedmakevectors(t2); m_forward = v_forward; m_up = v_up;
21         m_forward = Portal_Transform_Apply(t1, m_forward); m_up = Portal_Transform_Apply(t1, m_up);
22         return fixedvectoangles2(m_forward, m_up);
23 }
24
25 vector Portal_Transform_Invert(vector transform)
26 {
27         vector i_forward, i_up;
28         fixedmakevectors(transform);
29         // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
30         // but these are orthogonal unit vectors!
31         // so to invert, we can simply fixedvectoangles the TRANSPOSED matrix
32         // TODO is this always -transform?
33         i_forward_x = v_forward_x;
34         i_forward_y = -v_right_x;
35         i_forward_z = v_up_x;
36         i_up_x = v_forward_z;
37         i_up_y = -v_right_z;
38         i_up_z = v_up_z;
39 #ifdef DEBUG
40         vector v;
41         v = fixedvectoangles2(i_forward, i_up);
42         print("Transform: ", vtos(transform), "\n");
43         print("Inverted: ", vtos(v), "\n");
44         print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
45         fixedmakevectors(Portal_Transform_Multiply(v, transform));
46         print("Verify: ", vtos(v_forward), "\n");
47         print("Verify: ", vtos(v_right), "\n");
48         print("Verify: ", vtos(v_up), "\n");
49 #endif
50         return fixedvectoangles2(i_forward, i_up);
51 }
52
53 vector Portal_Transform_TurnDirection(vector transform)
54 {
55         // turn 180 degrees around v_up
56         // changes in-direction to out-direction
57         fixedmakevectors(transform);
58         return fixedvectoangles2(-1 * v_forward, 1 * v_up);
59 }
60
61 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
62 {
63         return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
64 }
65
66 float PlayerEdgeDistance(entity p, vector v)
67 {
68         vector vbest;
69
70         if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
71         if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
72         if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
73
74         return vbest * v;
75 }
76
77 .vector right_vector;
78 float Portal_TeleportPlayer(entity teleporter, entity player)
79 {
80         vector from, to, safe, step, transform, ang, newvel;
81         vector new_forward, new_up;
82         vector old_forward, old_up;
83         vector new_yawforward;
84         vector old_yawforward;
85         float planeshift, s, t;
86
87         from = teleporter.origin;
88         transform = teleporter.portal_transform;
89
90         to = teleporter.enemy.origin;
91         to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
92         newvel = Portal_Transform_Apply(transform, player.velocity);
93         // this now is INSIDE the plane... can't use that
94         
95         // shift it out
96         fixedmakevectors(teleporter.enemy.angles);
97
98         // first shift it ON the plane if needed
99         planeshift = ((teleporter.enemy.origin - to) * v_forward) + PlayerEdgeDistance(player, v_forward) + 1;
100         if(planeshift > 0 && (newvel * v_forward) > vlen(newvel) * 0.01)
101                 // if we can't, let us not do the planeshift and do somewhat incorrect transformation in the end
102                 to += newvel * (planeshift / (newvel * v_forward));
103         else
104                 to += trace_plane_normal * planeshift;
105         
106         s = (to - teleporter.enemy.origin) * v_right;
107         t = (to - teleporter.enemy.origin) * v_up;
108         s = bound(-48, s, 48);
109         t = bound(-48, t, 48);
110         to = teleporter.enemy.origin
111            + ((to - teleporter.enemy.origin) * v_forward) * v_forward
112            +     s                                        * v_right
113            +     t                                        * v_up;
114
115         safe = teleporter.enemy.portal_safe_origin; // a valid player origin
116         step = to + ((safe - to) * v_forward) * v_forward;
117         tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, step, MOVE_NOMONSTERS, player);
118         if(trace_startsolid)
119         {
120                 print("'safe' teleport location is not safe!\n");
121                 // FAIL TODO why does this happen?
122                 return 0;
123         }
124         safe = trace_endpos + normalize(safe - trace_endpos) * 0;
125         tracebox(safe, player.mins - SAFENUDGE, player.maxs + SAFENUDGE, to, MOVE_NOMONSTERS, player);
126         if(trace_startsolid)
127         {
128                 print("trace_endpos in solid, this can't be!\n");
129                 // FAIL TODO why does this happen? (reported by MrBougo)
130                 return 0;
131         }
132         to = trace_endpos + normalize(safe - trace_endpos) * 0;
133         //print(vtos(to), "\n");
134
135         // ang_x stuff works around weird quake angles
136         if(player.classname == "player")
137         {
138                 ang = player.v_angle;
139                 /*
140                 ang_x = bound(-89, mod(-ang_x + 180, 360) - 180, 89);
141                 ang = Portal_Transform_Multiply(transform, ang);
142                 */
143
144                 // PLAYERS use different math
145                 ang_x = -ang_x;
146
147                 //print("reference: ", vtos(Portal_Transform_Multiply(transform, ang)), "\n");
148
149                 fixedmakevectors(ang);
150                 old_forward = v_forward;
151                 old_up = v_up;
152                 fixedmakevectors(ang_y * '0 1 0');
153                 old_yawforward = v_forward;
154
155                 // their aiming directions are portalled...
156                 new_forward = Portal_Transform_Apply(transform, old_forward);
157                 new_up = Portal_Transform_Apply(transform, old_up);
158                 new_yawforward = Portal_Transform_Apply(transform, old_yawforward);
159
160                 // but now find a new sense of direction
161                 // this is NOT easy!
162                 // assume new_forward points straight up.
163                 // What is our yaw?
164                 //
165                 // new_up could now point forward OR backward... which direction to choose?
166
167                 if(new_forward_z > 0.7 || new_forward_z < -0.7) // far up; in this case, the "up" vector points backwards
168                 {
169                         // new_yawforward and new_yawup define the new aiming half-circle
170                         // we "just" need to find out whether new_up or -new_up is in that half circle
171                         ang = fixedvectoangles(new_forward); // this still gets us a nice pitch value...
172                         if(new_up * new_yawforward < 0)
173                                 new_up = -1 * new_up;
174                         ang_y = vectoyaw(new_up); // this vector is the yaw we want
175                         //print("UP/DOWN path: ", vtos(ang), "\n");
176                 }
177                 else
178                 {
179                         // good angles; here, "forward" suffices
180                         ang = fixedvectoangles(new_forward);
181                         //print("GOOD path: ", vtos(ang), "\n");
182                 }
183
184                 ang_x = -ang_x;
185                 ang_z = player.angles_z;
186         }
187         else
188         {
189                 ang = player.angles;
190                 ang = Portal_Transform_Multiply(transform, player.angles);
191         }
192
193         // factor -1 allows chaining portals, but may be weird
194         player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
195
196         if(player.flagcarried)
197                 DropFlag(player.flagcarried, player, world);
198         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
199
200         // reset fade counter
201         teleporter.portal_wants_to_vanish = 0;
202         teleporter.fade_time = time + 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 == "grapplinghook")
243                 return;
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
268         /*
269         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
270         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
271         {
272                 // can't teleport this
273                 return;
274         }
275         */
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
283 void Portal_Think();
284 void Portal_MakeBrokenPortal(entity portal)
285 {
286         portal.solid = SOLID_NOT;
287         portal.touch = SUB_Null;
288         portal.think = SUB_Null;
289         portal.effects = 0;
290         //portal.colormod = '1 1 1';
291         portal.nextthink = 0;
292         portal.takedamage = DAMAGE_NO;
293 }
294
295 void Portal_MakeWaitingPortal(entity portal)
296 {
297         portal.solid = SOLID_NOT;
298         portal.touch = SUB_Null;
299         portal.think = SUB_Null;
300         portal.effects = EF_ADDITIVE;
301         portal.colormod = '1 1 1';
302         portal.nextthink = 0;
303         portal.takedamage = DAMAGE_YES;
304 }
305
306 void Portal_MakeInPortal(entity portal)
307 {
308         portal.solid = SOLID_TRIGGER;
309         portal.touch = Portal_Touch;
310         portal.think = Portal_Think;
311         portal.effects = EF_RED;
312         portal.colormod = '1 0 0';
313         portal.nextthink = time;
314         portal.takedamage = DAMAGE_NO;
315 }
316
317 void Portal_MakeOutPortal(entity portal)
318 {
319         portal.solid = SOLID_NOT;
320         portal.touch = SUB_Null;
321         portal.think = SUB_Null;
322         portal.effects = EF_STARDUST | EF_BLUE;
323         portal.colormod = '0 0 1';
324         portal.nextthink = 0;
325         portal.takedamage = DAMAGE_YES;
326 }
327
328 void Portal_Disconnect(entity teleporter, entity destination)
329 {
330         teleporter.enemy = world;
331         destination.enemy = world;
332         Portal_MakeBrokenPortal(teleporter);
333         Portal_MakeBrokenPortal(destination);
334 }
335
336 void Portal_Connect(entity teleporter, entity destination)
337 {
338         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
339
340 #ifdef DEBUG
341         {
342                 // let's verify the transform
343                 vector in_f, in_r, in_u;
344                 vector out_f, out_r, out_u;
345                 fixedmakevectors(teleporter.angles);
346                 in_f = v_forward;
347                 in_r = v_right;
348                 in_u = v_up;
349                 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
350                 fixedmakevectors(destination.angles);
351                 out_f = v_forward;
352                 out_r = v_right;
353                 out_u = v_up;
354                 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
355                 // INTENDED TRANSFORM:
356                 //   in_f -> -out_f
357                 //   in_r -> -out_r
358                 //   in_u -> +out_u
359                 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
360                 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
361                 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
362
363                 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
364                 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
365         }
366 #endif
367
368         teleporter.enemy = destination;
369         destination.enemy = teleporter;
370         Portal_MakeInPortal(teleporter);
371         Portal_MakeOutPortal(destination);
372         teleporter.fade_time = time + 15;
373         destination.fade_time = time + 15;
374         teleporter.portal_wants_to_vanish = 0;
375         destination.portal_wants_to_vanish = 0;
376 }
377
378 void Portal_Remove(entity portal, float killed)
379 {
380         entity e;
381         e = portal.enemy;
382
383         if(e)
384         {
385                 Portal_Disconnect(portal, e);
386                 Portal_Remove(e, killed);
387         }
388
389         if(portal == portal.owner.portal_in)
390                 portal.owner.portal_in = world;
391         if(portal == portal.owner.portal_out)
392                 portal.owner.portal_out = world;
393         //portal.owner = world;
394
395         // makes the portal vanish
396         if(killed)
397         {
398                 fixedmakevectors(portal.angles);
399                 sound(portal, CHAN_PROJECTILE, "porto/explode.ogg", VOL_BASE, ATTN_NORM);
400                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
401                 remove(portal);
402         }
403         else
404         {
405                 Portal_MakeBrokenPortal(portal);
406                 sound(portal, CHAN_PROJECTILE, "porto/expire.ogg", VOL_BASE, ATTN_NORM);
407                 SUB_SetFade(portal, time, 0.5);
408         }
409 }
410
411 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
412 {
413         if(deathtype == DEATH_TELEFRAG)
414                 return;
415         if(attacker != self.owner)
416                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
417                         return;
418         self.health -= damage;
419         if(self.health < 0)
420                 Portal_Remove(self, 1);
421 }
422
423 void Portal_Think()
424 {
425         entity e, o;
426
427         // portal is being removed?
428         if(self.solid != SOLID_TRIGGER)
429                 return; // possibly engine bug
430
431         if(!self.enemy)
432                 error("Portal_Think called for a broken portal\n");
433
434         o = self.owner;
435         self.solid = SOLID_BBOX;
436         self.owner = world;
437
438         fixedmakevectors(self.angles);
439
440         FOR_EACH_PLAYER(e)
441         {
442                 if(time < self.portal_activatetime)
443                         if(e == o)
444                                 continue;
445                 if(e != o)
446                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
447                                 continue; // cannot go through someone else's portal
448                 if((e.origin - self.origin) * v_forward < 0) // wrong side of the plane? no teleport
449                         continue;
450
451                 // if e would hit the portal in a frame...
452                 // already teleport him
453                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
454                 if(trace_ent == self)
455                         Portal_TeleportPlayer(self, e);
456         }
457         self.solid = SOLID_TRIGGER;
458         self.owner = o;
459
460         self.nextthink = time;
461
462         if(time > self.fade_time)
463                 Portal_Remove(self, 0);
464 }
465
466 float Portal_Customize()
467 {
468         if(other.classname == "spectator")
469                 other = other.enemy;
470         if(other == self.owner)
471         {
472                 self.modelindex = self.modelindex_lod0;
473         }
474         else if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
475         {
476                 self.modelindex = 0;
477         }
478         else
479         {
480                 self.modelindex = self.modelindex_lod0;
481         }
482         return TRUE;
483 }
484
485 // cleanup:
486 //   when creating in-portal:
487 //     disconnect
488 //     clear existing in-portal
489 //     set as in-portal
490 //     connect
491 //   when creating out-portal:
492 //     disconnect
493 //     clear existing out-portal
494 //     set as out-portal
495 //   when player dies:
496 //     disconnect portals
497 //     clear both portals
498 //   after timeout of in-portal:
499 //     disconnect portals
500 //     clear both portals
501 //   TODO: ensure only one portal shot at once
502 float Portal_SetInPortal(entity own, entity portal)
503 {
504         if(own.portal_in)
505         {
506                 if(own.portal_out)
507                         Portal_Disconnect(own.portal_in, own.portal_out);
508                 Portal_Remove(own.portal_in, 0);
509         }
510         own.portal_in = portal;
511         if(own.portal_out)
512         {
513                 own.portal_out.portal_id = portal.portal_id;
514                 Portal_Connect(own.portal_in, own.portal_out);
515         }
516         return 2;
517 }
518 float Portal_SetOutPortal(entity own, entity portal)
519 {
520         if(own.portal_out)
521         {
522                 if(own.portal_in)
523                         Portal_Disconnect(own.portal_in, own.portal_out);
524                 Portal_Remove(own.portal_out, 0);
525         }
526         own.portal_out = portal;
527         if(own.portal_in)
528         {
529                 own.portal_in.portal_id = portal.portal_id;
530                 Portal_Connect(own.portal_in, own.portal_out);
531         }
532         return 1;
533 }
534 void Portal_ClearAll_PortalsOnly(entity own)
535 {
536         if(own.portal_in)
537                 Portal_Remove(own.portal_in, 0);
538         if(own.portal_out)
539                 Portal_Remove(own.portal_out, 0);
540 }
541 void Portal_ClearAll(entity own)
542 {
543         Portal_ClearAll_PortalsOnly(own);
544         W_Porto_Remove(own);
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_PortalsOnly(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_ClearAllLater(entity own)
565 {
566         Portal_ClearAllLater_PortalsOnly(own);
567         W_Porto_Remove(own);
568 }
569 void Portal_ClearWithID(entity own, float id)
570 {
571         if(own.portal_in)
572                 if(own.portal_in.portal_id == id)
573                 {
574                         if(own.portal_out)
575                                 Portal_Disconnect(own.portal_in, own.portal_out);
576                         Portal_Remove(own.portal_in, 0);
577                 }
578         if(own.portal_out)
579                 if(own.portal_out.portal_id == id)
580                 {
581                         if(own.portal_in)
582                                 Portal_Disconnect(own.portal_in, own.portal_out);
583                         Portal_Remove(own.portal_out, 0);
584                 }
585 }
586
587 entity Portal_Spawn(entity own, vector org, vector ang)
588 {
589         entity portal;
590
591         fixedmakevectors(ang);
592         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
593                 return world;
594
595         portal = spawn();
596         portal.classname = "portal";
597         portal.owner = own;
598         portal.origin = org;
599         portal.angles = ang;
600         portal.think = Portal_Think;
601         portal.nextthink = 0;
602         portal.portal_activatetime = time + 1.1;
603         portal.event_damage = Portal_Damage;
604         portal.health = 200;
605         portal.fade_time = time + cvar("g_balance_portal_lifetime");
606         portal.health = cvar("g_balance_portal_health");
607         setmodel(portal, "models/portal.md3");
608         portal.modelindex_lod0 = portal.modelindex;
609         portal.customizeentityforclient = Portal_Customize;
610
611         if(!Portal_FindSafeOrigin(portal))
612         {
613                 remove(portal);
614                 return world;
615         }
616
617         setsize(portal, '-48 -48 -48', '48 48 48');
618         Portal_MakeWaitingPortal(portal);
619
620         return portal;
621 }
622
623 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
624 {
625         entity portal;
626         vector ang;
627         vector org;
628
629         org = trace_endpos;
630         ang = fixedvectoangles2(trace_plane_normal, dir);
631         fixedmakevectors(ang);
632
633         portal = Portal_Spawn(own, org, ang);
634         if(!portal)
635         {
636                 // if(!own.portal_out || own.portal_out.portal_id == portal_id_val)
637                         Portal_ClearAll_PortalsOnly(own);
638                 return 0;
639         }
640
641         portal.portal_id = portal_id_val;
642         Portal_SetInPortal(own, portal);
643
644         return 1;
645 }
646
647 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
648 {
649         entity portal;
650         vector ang;
651         vector org;
652
653         org = trace_endpos;
654         ang = fixedvectoangles2(trace_plane_normal, dir);
655         fixedmakevectors(ang);
656
657         portal = Portal_Spawn(own, org, ang);
658         if(!portal)
659         {
660                 // if(!own.portal_in || own.portal_in.portal_id == portal_id_val)
661                         Portal_ClearAll_PortalsOnly(own);
662                 return 0;
663         }
664
665         portal.portal_id = portal_id_val;
666         Portal_SetOutPortal(own, portal);
667
668         return 1;
669 }