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