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