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