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