]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
more Portar fixes
[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_id;
5 .float portal_activatetime;
6
7 .entity portal_in, portal_out;
8
9 vector fixedvectoangles(vector v)
10 {
11         vector a;
12         a = vectoangles(v);
13         a_x = -a_x;
14         return a;
15 }
16
17 vector fixedvectoangles2(vector v, vector w)
18 {
19         vector a;
20         a = vectoangles(v, w);
21         a_x = -a_x;
22         return a;
23 }
24
25 vector Portal_Transform_Apply(vector transform, vector v)
26 {
27         makevectors(transform);
28         return v_forward * v_x
29              + v_right   * (-v_y)
30                  + v_up      * v_z;
31 }
32
33 vector Portal_Transform_Multiply(vector t1, vector t2)
34 {
35         vector m_forward, m_up;
36         makevectors(t2); m_forward = v_forward; m_up = v_up;
37         m_forward = Portal_Transform_Apply(t1, m_forward);
38         m_up = Portal_Transform_Apply(t1, m_up);
39         return fixedvectoangles2(m_forward, m_up);
40 }
41
42 vector Portal_Transform_Invert(vector transform)
43 {
44         vector i_forward, i_up;
45         makevectors(transform);
46         // we want angles that turn v_forward into '1 0 0', v_right into '0 1 0' and v_up into '0 0 1'
47         // but these are orthogonal unit vectors!
48         // so to invert, we can simply vectoangles the TRANSPOSED matrix
49         // TODO is this always -transform?
50         i_forward_x = v_forward_x;
51         i_forward_y = -v_right_x;
52         i_forward_z = v_up_x;
53         i_up_x = v_forward_z;
54         i_up_y = -v_right_z;
55         i_up_z = v_up_z;
56 #ifdef DEBUG
57         vector v;
58         v = fixedvectoangles2(i_forward, i_up);
59         print("Transform: ", vtos(transform), "\n");
60         print("Inverted: ", vtos(v), "\n");
61         print("Verify: ", vtos(Portal_Transform_Multiply(v, transform)), "\n");
62         makevectors(Portal_Transform_Multiply(v, transform));
63         print("Verify: ", vtos(v_forward), "\n");
64         print("Verify: ", vtos(v_right), "\n");
65         print("Verify: ", vtos(v_up), "\n");
66 #endif
67         return fixedvectoangles2(i_forward, i_up);
68 }
69
70 vector Portal_Transform_TurnDirection(vector transform)
71 {
72         vector t_angles;
73         t_angles_x = -transform_x;
74         t_angles_y = mod(transform_y + 180, 360);
75         t_angles_z = -transform_z;
76         return t_angles;
77 }
78
79 vector Portal_Transform_Divide(vector to_transform, vector from_transform)
80 {
81         return Portal_Transform_Multiply(to_transform, Portal_Transform_Invert(from_transform));
82 }
83
84 void Portal_TeleportPlayer(entity teleporter, entity player)
85 {
86         vector from, to, safe, step, transform, ang;
87         from = teleporter.origin;
88         to = teleporter.enemy.origin;
89         transform = teleporter.portal_transform;
90
91         to = to + Portal_Transform_Apply(teleporter.portal_transform, player.origin - from);
92         // this now is INSIDE the plane... can't use that
93
94         // shift it out
95         makevectors(teleporter.enemy.angles);
96         safe = teleporter.enemy.portal_safe_origin; // a valid player origin
97         step = to + ((safe - to) * v_forward) * v_forward;
98         tracebox(safe, PL_MIN, PL_MAX, step, MOVE_WORLDONLY, player);
99         if(trace_startsolid)
100         {
101                 bprint("'safe' teleport location is not safe!\n");
102                 // FAIL TODO why does this happen?
103                 return;
104         }
105         safe = trace_endpos;
106         tracebox(safe, PL_MIN, PL_MAX, to, MOVE_WORLDONLY, player);
107         if(trace_startsolid)
108                 error("trace_endpos in solid!");
109         to = trace_endpos;
110
111         if(player.classname == "player")
112         {
113                 ang = Portal_Transform_Multiply(transform, player.v_angle);
114                 ang_z = player.angles_z;
115         }
116         else
117         {
118                 ang = Portal_Transform_Multiply(transform, player.angles);
119         }
120
121         print("previous velocity: ", vtos(player.velocity), "\n");
122         print("previous origin: ", vtos(player.origin), "\n");
123         TeleportPlayer(teleporter, player, to, ang, Portal_Transform_Apply(transform, player.velocity));
124         print("new velocity: ", vtos(player.velocity), "\n");
125         print("new origin: ", vtos(player.origin), "\n");
126 }
127
128 float Portal_FindSafeOrigin(entity portal)
129 {
130         vector o;
131         o = portal.origin;
132         portal.mins = PL_MIN - '8 8 8';
133         portal.maxs = PL_MAX + '8 8 8';
134         if(!move_out_of_solid(portal))
135         {
136                 print("NO SAFE ORIGIN\n");
137                 return 0;
138         }
139         portal.portal_safe_origin = portal.origin;
140         setorigin(portal, o);
141         return 1;
142 }
143
144 void Portal_Touch()
145 {
146         if(time < self.portal_activatetime)
147                 return;
148         makevectors(self.angles);
149         if((other.origin - self.origin) * v_forward < 0)
150                 return;
151         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
152         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
153         {
154                 // can't teleport this
155                 return;
156         }
157         Portal_TeleportPlayer(self, other);
158
159         // reset fade counter
160         self.portal_wants_to_vanish = 0;
161         self.nextthink = time + 60;
162 }
163
164 void Portal_MakeBrokenPortal(entity portal)
165 {
166         portal.solid = SOLID_NOT;
167         portal.touch = SUB_Null;
168         portal.effects = 0;
169         portal.colormod = '1 1 1';
170 }
171
172 void Portal_MakeInPortal(entity portal)
173 {
174         portal.solid = SOLID_TRIGGER;
175         portal.touch = Portal_Touch;
176         portal.effects = EF_RED;
177         portal.colormod = '1 0 0';
178 }
179
180 void Portal_MakeOutPortal(entity portal)
181 {
182         portal.solid = SOLID_NOT;
183         portal.touch = SUB_Null;
184         portal.effects = EF_STARDUST;
185         portal.colormod = '0 0 1';
186 }
187
188 void Portal_Disconnect(entity teleporter, entity destination)
189 {
190         teleporter.enemy = world;
191         destination.enemy = world;
192         Portal_MakeBrokenPortal(teleporter);
193         Portal_MakeBrokenPortal(destination);
194 }
195
196 void Portal_Connect(entity teleporter, entity destination)
197 {
198         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
199         teleporter.enemy = destination;
200         destination.enemy = teleporter;
201         Portal_MakeInPortal(teleporter);
202         Portal_MakeOutPortal(destination);
203         teleporter.nextthink = time + 60;
204         destination.nextthink = time + 60;
205         teleporter.portal_wants_to_vanish = 0;
206         destination.portal_wants_to_vanish = 0;
207 }
208
209 void Portal_Vanish()
210 {
211         self.portal_wants_to_vanish = 1;
212
213         if(self.enemy)
214                 if(!self.enemy.portal_wants_to_vanish)
215                         return;
216
217         SUB_SetFade(self, time + 1, 1);
218         if(self.enemy)
219         {
220                 SUB_SetFade(self.enemy, time + 1, 1);
221                 Portal_Disconnect(self, self.enemy);
222         }
223 }
224
225 void Portal_RequestVanish(entity portal)
226 {
227         entity oldself;
228         oldself = self;
229         self = portal;
230         if(self.enemy)
231                 self.enemy.portal_wants_to_vanish = 1;
232         Portal_Vanish();
233         self = oldself;
234 }
235
236 entity Portal_Spawn(entity own, vector org, vector ang)
237 {
238         entity portal;
239         portal = spawn();
240         portal.classname = "portal";
241         portal.owner = own;
242         portal.origin = org;
243         portal.angles = ang;
244         portal.think = Portal_Vanish;
245         portal.nextthink = time + 60;
246         portal.portal_activatetime = time + 0.1;
247         setmodel(portal, "models/items/g_h100.md3");
248
249         if(!Portal_FindSafeOrigin(portal))
250         {
251                 remove(portal);
252                 return world;
253         }
254
255         setsize(portal, '-64 -64 -64', '64 64 64');
256
257         return portal;
258 }
259
260 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
261 {
262         if(trace_ent.classname == "player")
263         {
264                 print("hit a player, adjusting...\n");
265                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
266                 trace_plane_normal = '0 0 1';
267         }
268
269         if(own.portal_in)
270         {
271                 if(own.portal_out)
272                 {
273                         Portal_Disconnect(own.portal_in, own.portal_out);
274                         Portal_RequestVanish(own.portal_out);
275                 }
276                 own.portal_out = own.portal_in;
277                 own.portal_in = world;
278                 own.portal_out.portal_id = portal_id_val;
279         }
280         else if(own.portal_out)
281         {
282                 print("this DID happen, no idea why (1)\n");
283                 Portal_RequestVanish(own.portal_out);
284                 own.portal_out = world;
285         }
286                 
287         print("Plane normal: ", vtos(trace_plane_normal), "\n");
288         print("Portal angles: ", vtos(fixedvectoangles(trace_plane_normal)), "\n");
289         own.portal_in = Portal_Spawn(own, trace_endpos + trace_plane_normal, fixedvectoangles2(trace_plane_normal, dir));
290         if(!own.portal_in)
291                 return 0;
292         own.portal_in.portal_id = portal_id_val;
293
294         if(own.portal_out)
295                 Portal_Connect(own.portal_in, own.portal_out);
296
297         return 1;
298 }
299
300 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
301 {
302         if(trace_ent.classname == "player")
303         {
304                 print("hit a player, adjusting...\n");
305                 trace_endpos = trace_ent.origin + '0 0 1' * PL_MIN_z;
306                 trace_plane_normal = '0 0 1';
307         }
308
309         if(!own.portal_in)
310                 return 0; // sorry
311
312         if(own.portal_in.portal_id != portal_id_val)
313                 return 0; // we need MATCHING portals
314
315         if(own.portal_out)
316         {
317                 Portal_Disconnect(own.portal_in, own.portal_out);
318                 Portal_RequestVanish(own.portal_out);
319                 own.portal_out = world;
320         }
321                 
322         print("Plane normal: ", vtos(trace_plane_normal), "\n");
323         print("Portal angles: ", vtos(fixedvectoangles(trace_plane_normal)), "\n");
324         own.portal_out = Portal_Spawn(own, trace_endpos + trace_plane_normal, fixedvectoangles2(trace_plane_normal, dir));
325         if(!own.portal_out)
326                 return 0;
327         own.portal_out.portal_id = portal_id_val;
328
329         Portal_Connect(own.portal_in, own.portal_out);
330         print("Portal transform: ", vtos(own.portal_in.portal_transform), "\n");
331
332         return 1;
333 }