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