]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
hopefully fix portal crash
[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         if(other.owner != self.owner)
203                 if(other.owner.classname == "player")
204                         if(IS_INDEPENDENT_PLAYER(other.owner) || IS_INDEPENDENT_PLAYER(self.owner))
205                                 return; // cannot go through someone else's portal
206         fixedmakevectors(self.angles);
207         if((other.origin - self.origin) * v_forward < 0)
208                 return;
209         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
210         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
211         {
212                 // can't teleport this
213                 return;
214         }
215
216         if(Portal_TeleportPlayer(self, other))
217                 if(other.classname == "porto")
218                         if(other.effects & EF_RED)
219                                 other.effects += EF_BLUE - EF_RED;
220 }
221
222 void Portal_Think();
223 void Portal_MakeBrokenPortal(entity portal)
224 {
225         portal.solid = SOLID_NOT;
226         portal.touch = SUB_Null;
227         portal.think = SUB_Null;
228         portal.effects = 0;
229         //portal.colormod = '1 1 1';
230         portal.nextthink = 0;
231         portal.takedamage = DAMAGE_NO;
232 }
233
234 void Portal_MakeWaitingPortal(entity portal)
235 {
236         portal.solid = SOLID_NOT;
237         portal.touch = SUB_Null;
238         portal.think = SUB_Null;
239         portal.effects = EF_ADDITIVE;
240         portal.colormod = '1 1 1';
241         portal.nextthink = 0;
242         portal.takedamage = DAMAGE_YES;
243 }
244
245 void Portal_MakeInPortal(entity portal)
246 {
247         portal.solid = SOLID_TRIGGER;
248         portal.touch = Portal_Touch;
249         portal.think = Portal_Think;
250         portal.effects = EF_RED;
251         portal.colormod = '1 0 0';
252         portal.nextthink = time;
253         portal.takedamage = DAMAGE_NO;
254 }
255
256 void Portal_MakeOutPortal(entity portal)
257 {
258         portal.solid = SOLID_NOT;
259         portal.touch = SUB_Null;
260         portal.think = SUB_Null;
261         portal.effects = EF_STARDUST | EF_BLUE;
262         portal.colormod = '0 0 1';
263         portal.nextthink = 0;
264         portal.takedamage = DAMAGE_YES;
265 }
266
267 void Portal_Disconnect(entity teleporter, entity destination)
268 {
269         teleporter.enemy = world;
270         destination.enemy = world;
271         Portal_MakeBrokenPortal(teleporter);
272         Portal_MakeBrokenPortal(destination);
273 }
274
275 void Portal_Connect(entity teleporter, entity destination)
276 {
277         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
278
279 #ifdef DEBUG
280         {
281                 // let's verify the transform
282                 vector in_f, in_r, in_u;
283                 vector out_f, out_r, out_u;
284                 fixedmakevectors(teleporter.angles);
285                 in_f = v_forward;
286                 in_r = v_right;
287                 in_u = v_up;
288                 print("teleporter: ", vtos(in_f), " ", vtos(in_r), " ", vtos(in_u), "\n");
289                 fixedmakevectors(destination.angles);
290                 out_f = v_forward;
291                 out_r = v_right;
292                 out_u = v_up;
293                 print("dest: ", vtos(out_f), " ", vtos(out_r), " ", vtos(out_u), "\n");
294                 // INTENDED TRANSFORM:
295                 //   in_f -> -out_f
296                 //   in_r -> -out_r
297                 //   in_u -> +out_u
298                 print("FORWARD: ", vtos(in_f), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_f)), ", should be", vtos(-1 * out_f), "\n");
299                 print("RIGHT: ", vtos(in_r), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_r)), ", should be", vtos(-1 * out_r), "\n");
300                 print("UP: ", vtos(in_u), " -> ", vtos(Portal_Transform_Apply(teleporter.portal_transform, in_u)), ", should be", vtos(out_u), "\n");
301
302                 te_lightning3(world, teleporter.origin, teleporter.origin + in_r * 1000);
303                 te_lightning3(world, destination.origin, destination.origin + out_r * 1000);
304         }
305 #endif
306
307         teleporter.enemy = destination;
308         destination.enemy = teleporter;
309         Portal_MakeInPortal(teleporter);
310         Portal_MakeOutPortal(destination);
311         teleporter.fade_time = time + 15;
312         destination.fade_time = time + 15;
313         teleporter.portal_wants_to_vanish = 0;
314         destination.portal_wants_to_vanish = 0;
315 }
316
317 void Portal_Remove(entity portal, float killed)
318 {
319         entity e;
320         e = portal.enemy;
321
322         if(e)
323         {
324                 Portal_Disconnect(portal, e);
325                 Portal_Remove(e, killed);
326         }
327
328         if(portal == portal.owner.portal_in)
329                 portal.owner.portal_in = world;
330         if(portal == portal.owner.portal_out)
331                 portal.owner.portal_out = world;
332         portal.owner = world;
333
334         // makes the portal vanish
335         if(killed)
336         {
337                 fixedmakevectors(portal.angles);
338                 pointparticles(particleeffectnum("rocket_explode"), portal.origin + v_forward * 16, v_forward * 1024, 4);
339                 remove(portal);
340         }
341         else
342         {
343                 Portal_MakeBrokenPortal(portal);
344                 SUB_SetFade(portal, time, 0.5);
345         }
346 }
347
348 void Portal_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
349 {
350         if(deathtype == DEATH_TELEFRAG)
351                 return;
352         if(attacker != self.owner)
353                 if(IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(self.owner))
354                         return;
355         self.health -= damage;
356         if(self.health < 0)
357         {
358                 Portal_Remove(self, 1);
359         }
360 }
361
362 void Portal_Think()
363 {
364         entity e, o;
365
366         if(self.solid != SOLID_TRIGGER)
367                 error("Portal_Think called for a portal that should not be thinking");
368
369         o = self.owner;
370         self.solid = SOLID_BBOX;
371         self.owner = world;
372         FOR_EACH_PLAYER(e)
373         {
374                 if(time < self.portal_activatetime)
375                         if(e == o)
376                                 continue;
377                 if(e != o)
378                         if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o))
379                                 continue; // cannot go through someone else's portal
380                 // if e would hit the portal in a frame...
381                 // already teleport him
382                 tracebox(e.origin, e.mins, e.maxs, e.origin + e.velocity * 2 * frametime, MOVE_NORMAL, e);
383                 if(trace_ent == self)
384                         Portal_TeleportPlayer(self, e);
385         }
386         self.solid = SOLID_TRIGGER;
387         self.owner = o;
388
389         self.nextthink = time;
390
391         if(time > self.fade_time)
392                 Portal_Remove(self, 0);
393 }
394
395 float Portal_Customize()
396 {
397         if(other.classname == "spectator")
398                 other = other.enemy;
399         if(other == self.owner)
400                 return TRUE;
401         if(IS_INDEPENDENT_PLAYER(other))
402                 return FALSE;
403         if(IS_INDEPENDENT_PLAYER(self.owner))
404                 return FALSE;
405         return TRUE;
406 }
407
408 // cleanup:
409 //   when creating in-portal:
410 //     disconnect
411 //     clear existing in-portal
412 //     set as in-portal
413 //     connect
414 //   when creating out-portal:
415 //     disconnect
416 //     clear existing out-portal
417 //     set as out-portal
418 //   when player dies:
419 //     disconnect portals
420 //     clear both portals
421 //   after timeout of in-portal:
422 //     disconnect portals
423 //     clear both portals
424 //   TODO: ensure only one portal shot at once
425 float Portal_SetInPortal(entity own, entity portal)
426 {
427         if(own.portal_in)
428         {
429                 if(own.portal_out)
430                         Portal_Disconnect(own.portal_in, own.portal_out);
431                 Portal_Remove(own.portal_in, 0);
432         }
433         own.portal_in = portal;
434         if(own.portal_out)
435         {
436                 own.portal_out.portal_id = portal.portal_id;
437                 Portal_Connect(own.portal_in, own.portal_out);
438         }
439         return 2;
440 }
441 float Portal_SetOutPortal(entity own, entity portal)
442 {
443         if(own.portal_out)
444         {
445                 if(own.portal_in)
446                         Portal_Disconnect(own.portal_in, own.portal_out);
447                 Portal_Remove(own.portal_out, 0);
448         }
449         own.portal_out = portal;
450         if(own.portal_in)
451         {
452                 own.portal_in.portal_id = portal.portal_id;
453                 Portal_Connect(own.portal_in, own.portal_out);
454         }
455         return 1;
456 }
457 void Portal_ClearAll(entity own)
458 {
459         if(own.portal_in)
460                 Portal_Remove(own.portal_in, 0);
461         if(own.portal_out)
462                 Portal_Remove(own.portal_out, 0);
463 }
464 void Portal_ClearWithID(entity own, float id)
465 {
466         if(own.portal_in)
467                 if(own.portal_in.portal_id == id)
468                 {
469                         if(own.portal_out)
470                                 Portal_Disconnect(own.portal_in, own.portal_out);
471                         Portal_Remove(own.portal_in, 0);
472                 }
473         if(own.portal_out)
474                 if(own.portal_out.portal_id == id)
475                 {
476                         if(own.portal_in)
477                                 Portal_Disconnect(own.portal_in, own.portal_out);
478                         Portal_Remove(own.portal_out, 0);
479                 }
480 }
481
482 entity Portal_Spawn(entity own, vector org, vector ang)
483 {
484         entity portal;
485
486         fixedmakevectors(ang);
487         if(!CheckWireframeBox(own, org - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
488                 return world;
489
490         portal = spawn();
491         portal.classname = "portal";
492         portal.owner = own;
493         portal.origin = org;
494         portal.angles = ang;
495         portal.think = Portal_Think;
496         portal.nextthink = 0;
497         portal.fade_time = time + 15;
498         portal.portal_activatetime = time + 0.1;
499         portal.event_damage = Portal_Damage;
500         portal.health = 300;
501         setmodel(portal, "models/portal.md3");
502         portal.customizeentityforclient = Portal_Customize;
503
504         if(!Portal_FindSafeOrigin(portal))
505         {
506                 remove(portal);
507                 return world;
508         }
509
510         setsize(portal, '-48 -48 -48', '48 48 48');
511         Portal_MakeWaitingPortal(portal);
512
513         return portal;
514 }
515
516 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
517 {
518         entity portal;
519         vector ang;
520         vector org;
521
522         if(trace_ent.movetype == MOVETYPE_WALK)
523         {
524                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
525                 trace_plane_normal = '0 0 1';
526                 dir = -1 * dir; // create telefrag portals the other way round
527         }
528
529         org = trace_endpos;
530         ang = vectoangles2(trace_plane_normal, dir);
531         fixedmakevectors(ang);
532
533         portal = Portal_Spawn(own, org, ang);
534         if(!portal)
535         {
536                 if(!self.portal_out || self.portal_out.portal_id == portal_id_val)
537                         Portal_ClearAll(own);
538                 return 0;
539         }
540
541         portal.portal_id = portal_id_val;
542         Portal_SetInPortal(own, portal);
543         sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
544
545         return 1;
546 }
547
548 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
549 {
550         entity portal;
551         vector ang;
552         vector org;
553
554         if(trace_ent.movetype == MOVETYPE_WALK)
555         {
556                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
557                 trace_plane_normal = '0 0 1';
558                 dir = -1 * dir; // create telefrag portals the other way round
559         }
560
561         org = trace_endpos;
562         ang = vectoangles2(trace_plane_normal, dir);
563         fixedmakevectors(ang);
564
565         portal = Portal_Spawn(own, org, ang);
566         if(!portal)
567         {
568                 if(!self.portal_in || self.portal_in.portal_id == portal_id_val)
569                         Portal_ClearAll(own);
570                 return 0;
571         }
572
573         portal.portal_id = portal_id_val;
574         Portal_SetOutPortal(own, portal);
575         sound(portal, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
576
577         return 1;
578 }