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