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