]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
odd, but sometimes I get collisions to a SOLID_TRIGGER entity from SV_Impact. Added...
[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);
22         m_up = Portal_Transform_Apply(t1, m_up);
23         return vectoangles2(m_forward, m_up);
24 }
25
26 vector Portal_Transform_Invert(vector transform)
27 {
28         vector i_forward, i_up;
29         fixedmakevectors(transform);
30         // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
31         // but these are orthogonal unit vectors!
32         // so to invert, we can simply vectoangles the TRANSPOSED matrix
33         // TODO is this always -transform?
34         i_forward_x = v_forward_x;
35         i_forward_y = -v_right_x;
36         i_forward_z = v_up_x;
37         i_up_x = v_forward_z;
38         i_up_y = -v_right_z;
39         i_up_z = v_up_z;
40 #ifdef DEBUG
41         vector v;
42         v = vectoangles2(i_forward, i_up);
43         print("Transform: ", vtos(transform), "\n");
44         print("Inverted: ", vtos(v), "\n");
45         print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
46         fixedmakevectors(Portal_Transform_Multiply(v, transform));
47         print("Verify: ", vtos(v_forward), "\n");
48         print("Verify: ", vtos(v_right), "\n");
49         print("Verify: ", vtos(v_up), "\n");
50 #endif
51         return vectoangles2(i_forward, i_up);
52 }
53
54 vector Portal_Transform_TurnDirection(vector transform)
55 {
56         // turn 180 degrees around v_up
57         // changes in-direction to out-direction
58         fixedmakevectors(transform);
59         return vectoangles2(-1 * v_forward, 1 * v_up);
60 }
61
62 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
63 {
64         return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
65 }
66
67 float PlayerEdgeDistance(entity p, vector v)
68 {
69         vector vbest;
70
71         if(v_x < 0) vbest_x = p.mins_x; else vbest_x = p.maxs_x;
72         if(v_y < 0) vbest_y = p.mins_y; else vbest_y = p.maxs_y;
73         if(v_z < 0) vbest_z = p.mins_z; else vbest_z = p.maxs_z;
74
75         return vbest * v;
76 }
77
78 .vector right_vector;
79 float Portal_TeleportPlayer(entity teleporter, entity player)
80 {
81         vector from, to, safe, step, transform, ang, newvel;
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                 ang_x = -ang_x;
131                 ang = Portal_Transform_Multiply(transform, ang);
132                 ang_z = player.angles_z;
133         }
134         else
135         {
136                 ang = player.angles;
137                 ang_x = -ang_x;
138                 ang = Portal_Transform_Multiply(transform, player.angles);
139         }
140         ang_x = -ang_x;
141
142         // factor -1 allows chaining portals, but may be weird
143         player.right_vector = -1 * Portal_Transform_Apply(transform, player.right_vector);
144
145         if(player.flagcarried)
146                 DropFlag(player.flagcarried);
147         TeleportPlayer(teleporter, player, to, ang, newvel, teleporter.enemy.absmin, teleporter.enemy.absmax);
148
149         // reset fade counter
150         teleporter.portal_wants_to_vanish = 0;
151         teleporter.fade_time = time + 15;
152         teleporter.enemy.health = 300;
153
154         return 1;
155 }
156
157 float Portal_FindSafeOrigin(entity portal)
158 {
159         vector o;
160         o = portal.origin;
161         portal.mins = PL_MIN - '8 8 8';
162         portal.maxs = PL_MAX + '8 8 8';
163         fixedmakevectors(portal.angles);
164         portal.origin += 16 * v_forward;
165         if(!move_out_of_solid(portal))
166         {
167 #ifdef DEBUG
168                 print("NO SAFE ORIGIN\n");
169 #endif
170                 return 0;
171         }
172         portal.portal_safe_origin = portal.origin;
173         setorigin(portal, o);
174         return 1;
175 }
176
177 void Portal_Touch()
178 {
179         if(trace_fraction < 1)
180                 return; // only handle TouchAreaGrid ones (only these can teleport)
181                 // for some unknown reason, this also gets collisions from SV_Impact sometimes
182         if(!self.enemy)
183         {
184                 objerror("Portal_Touch called for [unconnected] portal\n");
185                 return;
186         }
187         if(other.classname == "porto")
188         {
189                 if(other.portal_id == self.portal_id)
190                         return;
191         }
192         if(time < self.portal_activatetime)
193                 if(other == self.owner)
194                 {
195                         self.portal_activatetime = time + 0.1;
196                         return;
197                 }
198         if(other != self.owner)
199                 if(other.classname == "player")
200                         if(IS_INDEPENDENT_PLAYER(other) || IS_INDEPENDENT_PLAYER(self.owner))
201                                 return; // cannot go through someone else's portal
202         fixedmakevectors(self.angles);
203         if((other.origin - self.origin) * v_forward < 0)
204                 return;
205         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
206         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
207         {
208                 // can't teleport this
209                 return;
210         }
211
212         if(Portal_TeleportPlayer(self, other))
213                 if(other.classname == "porto")
214                         if(other.effects & EF_RED)
215                                 other.effects += EF_BLUE - EF_RED;
216 }
217
218 void Portal_MakeBrokenPortal(entity portal)
219 {
220         portal.solid = SOLID_NOT;
221         portal.touch = SUB_Null;
222         portal.effects = 0;
223         //portal.colormod = '1 1 1';
224         portal.nextthink = 0;
225         portal.takedamage = DAMAGE_NO;
226 }
227
228 void Portal_MakeWaitingPortal(entity portal)
229 {
230         portal.solid = SOLID_NOT;
231         portal.touch = SUB_Null;
232         portal.effects = EF_ADDITIVE;
233         portal.colormod = '1 1 1';
234         portal.nextthink = 0;
235         portal.takedamage = DAMAGE_YES;
236 }
237
238 void Portal_MakeInPortal(entity portal)
239 {
240         portal.solid = SOLID_TRIGGER;
241         portal.touch = Portal_Touch;
242         portal.effects = EF_RED;
243         portal.colormod = '1 0 0';
244         portal.nextthink = time;
245         portal.takedamage = DAMAGE_NO;
246 }
247
248 void Portal_MakeOutPortal(entity portal)
249 {
250         portal.solid = SOLID_NOT;
251         portal.touch = SUB_Null;
252         portal.effects = EF_STARDUST | EF_BLUE;
253         portal.colormod = '0 0 1';
254         portal.nextthink = 0;
255         portal.takedamage = DAMAGE_YES;
256 }
257
258 void Portal_Disconnect(entity teleporter, entity destination)
259 {
260         teleporter.enemy = world;
261         destination.enemy = world;
262         Portal_MakeBrokenPortal(teleporter);
263         Portal_MakeBrokenPortal(destination);
264 }
265
266 void Portal_Connect(entity teleporter, entity destination)
267 {
268         float id;
269
270         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
271
272 #ifdef DEBUG
273         {
274                 // let's verify the transform
275                 vector in_f, in_r, in_u;
276                 vector out_f, out_r, out_u;
277                 fixedmakevectors(teleporter.angles);
278                 in_f = v_forward;
279                 in_r = v_right;
280                 in_u = v_up;
281                 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
282                 fixedmakevectors(destination.angles);
283                 out_f = v_forward;
284                 out_r = v_right;
285                 out_u = v_up;
286                 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
287                 // INTENDED TRANSFORM:
288                 //   in_f -> -out_f
289                 //   in_r -> -out_r
290                 //   in_u -> +out_u
291                 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
292                 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
293                 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
294
295                 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
296                 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
297         }
298 #endif
299
300         teleporter.enemy = destination;
301         destination.enemy = teleporter;
302         Portal_MakeInPortal(teleporter);
303         Portal_MakeOutPortal(destination);
304         teleporter.fade_time = time + 15;
305         destination.fade_time = time + 15;
306         teleporter.portal_wants_to_vanish = 0;
307         destination.portal_wants_to_vanish = 0;
308 }
309
310 void Portal_Remove(entity portal, float killed)
311 {
312         entity e;
313         e = portal.enemy;
314
315         if(e)
316         {
317                 Portal_Disconnect(portal, e);
318                 Portal_Remove(e, killed);
319         }
320
321         if(portal == portal.owner.portal_in)
322                 portal.owner.portal_in = world;
323         if(portal == portal.owner.portal_out)
324                 portal.owner.portal_out = world;
325         portal.owner = world;
326
327         // makes the portal vanish
328         if(killed)
329         {
330                 fixedmakevectors(portal.angles);
331                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
332                 remove(portal);
333         }
334         else
335         {
336                 Portal_MakeBrokenPortal(portal);
337                 SUB_SetFade(portal, time, 0.5);
338         }
339 }
340
341 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
342 {
343         if(deathtype == DEATH_TELEFRAG)
344                 return;
345         if(attacker != self.owner)
346                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
347                         return;
348         self.health -= damage;
349         if(self.health < 0)
350         {
351                 Portal_Remove(self, 1);
352         }
353 }
354
355 void Portal_Think()
356 {
357         entity e, o;
358
359         if(self.solid != SOLID_TRIGGER)
360                 error("Portal_Think called for a portal that should not be thinking");
361
362         o = self.owner;
363         self.solid = SOLID_BBOX;
364         self.owner = world;
365         FOR_EACH_PLAYER(e)
366         {
367                 if(time < self.portal_activatetime)
368                         if(e == o)
369                                 continue;
370                 if(e != o)
371                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
372                                 continue; // cannot go through someone else's portal
373                 // if e would hit the portal in a frame...
374                 // already teleport him
375                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
376                 if(trace_ent == self)
377                         Portal_TeleportPlayer(self, e);
378         }
379         self.solid = SOLID_TRIGGER;
380         self.owner = o;
381
382         self.nextthink = time;
383
384         if(time > self.fade_time)
385                 Portal_Remove(self, 0);
386 }
387
388 float Portal_Customize()
389 {
390         if(other.classname == "spectator")
391                 other = other.enemy;
392         if(other == self.owner)
393                 return TRUE;
394         if(IS_INDEPENDENT_PLAYER(other))
395                 return FALSE;
396         if(IS_INDEPENDENT_PLAYER(self.owner))
397                 return FALSE;
398         return TRUE;
399 }
400
401 // cleanup:
402 //   when creating in-portal:
403 //     disconnect
404 //     clear existing in-portal
405 //     set as in-portal
406 //     connect
407 //   when creating out-portal:
408 //     disconnect
409 //     clear existing out-portal
410 //     set as out-portal
411 //   when player dies:
412 //     disconnect portals
413 //     clear both portals
414 //   after timeout of in-portal:
415 //     disconnect portals
416 //     clear both portals
417 //   TODO: ensure only one portal shot at once
418 float Portal_SetInPortal(entity own, entity portal)
419 {
420         if(own.portal_in)
421         {
422                 if(own.portal_out)
423                         Portal_Disconnect(own.portal_in, own.portal_out);
424                 Portal_Remove(own.portal_in, 0);
425         }
426         own.portal_in = portal;
427         if(own.portal_out)
428         {
429                 own.portal_out.portal_id = portal.portal_id;
430                 Portal_Connect(own.portal_in, own.portal_out);
431         }
432         return 2;
433 }
434 float Portal_SetOutPortal(entity own, entity portal)
435 {
436         if(own.portal_out)
437         {
438                 if(own.portal_in)
439                         Portal_Disconnect(own.portal_in, own.portal_out);
440                 Portal_Remove(own.portal_out, 0);
441         }
442         own.portal_out = portal;
443         if(own.portal_in)
444         {
445                 own.portal_in.portal_id = portal.portal_id;
446                 Portal_Connect(own.portal_in, own.portal_out);
447         }
448         return 1;
449 }
450 void Portal_ClearAll(entity own)
451 {
452         if(own.portal_in)
453                 Portal_Remove(own.portal_in, 0);
454         if(own.portal_out)
455                 Portal_Remove(own.portal_out, 0);
456 }
457 void Portal_ClearWithID(entity own, float id)
458 {
459         if(own.portal_in)
460                 if(own.portal_in.portal_id == id)
461                 {
462                         if(own.portal_out)
463                                 Portal_Disconnect(own.portal_in, own.portal_out);
464                         Portal_Remove(own.portal_in, 0);
465                 }
466         if(own.portal_out)
467                 if(own.portal_out.portal_id == id)
468                 {
469                         if(own.portal_in)
470                                 Portal_Disconnect(own.portal_in, own.portal_out);
471                         Portal_Remove(own.portal_out, 0);
472                 }
473 }
474
475 entity Portal_Spawn(entity own, vector org, vector ang)
476 {
477         entity portal;
478
479         fixedmakevectors(ang);
480         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
481                 return world;
482
483         portal = spawn();
484         portal.classname = "portal";
485         portal.owner = own;
486         portal.origin = org;
487         portal.angles = ang;
488         portal.think = Portal_Think;
489         portal.nextthink = 0;
490         portal.fade_time = time + 15;
491         portal.portal_activatetime = time + 0.1;
492         portal.event_damage = Portal_Damage;
493         portal.health = 300;
494         setmodel(portal, "models/portal.md3");
495         portal.customizeentityforclient = Portal_Customize;
496
497         if(!Portal_FindSafeOrigin(portal))
498         {
499                 remove(portal);
500                 return world;
501         }
502
503         setsize(portal, '-48 -48 -48', '48 48 48');
504         Portal_MakeWaitingPortal(portal);
505
506         return portal;
507 }
508
509 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
510 {
511         entity portal;
512         vector ang;
513         vector org;
514
515         if(trace_ent.movetype == MOVETYPE_WALK)
516         {
517                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
518                 trace_plane_normal = '0 0 1';
519                 dir = -1 * dir; // create telefrag portals the other way round
520         }
521
522         org = trace_endpos;
523         ang = vectoangles2(trace_plane_normal, dir);
524         fixedmakevectors(ang);
525
526         portal = Portal_Spawn(own, org, ang);
527         if(!portal)
528         {
529                 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
530                         Portal_ClearAll(own);
531                 return 0;
532         }
533
534         portal.portal_id = portal_id_val;
535         Portal_SetInPortal(own, portal);
536         sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
537
538         return 1;
539 }
540
541 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
542 {
543         entity portal;
544         vector ang;
545         vector org;
546
547         if(trace_ent.movetype == MOVETYPE_WALK)
548         {
549                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
550                 trace_plane_normal = '0 0 1';
551                 dir = -1 * dir; // create telefrag portals the other way round
552         }
553
554         org = trace_endpos;
555         ang = vectoangles2(trace_plane_normal, dir);
556         fixedmakevectors(ang);
557
558         portal = Portal_Spawn(own, org, ang);
559         if(!portal)
560         {
561                 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
562                         Portal_ClearAll(own);
563                 return 0;
564         }
565
566         portal.portal_id = portal_id_val;
567         Portal_SetOutPortal(own, portal);
568         sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
569
570         return 1;
571 }