]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/portals.qc
mall stupid mod: the Portar (compile with -DPORTAR for testing)
[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_NOMONSTERS, player);
99         if(trace_startsolid)
100                 error("'safe' teleport location is not safe!");
101         safe = trace_endpos;
102         tracebox(safe, PL_MIN, PL_MAX, to, MOVE_NOMONSTERS, player);
103         if(trace_startsolid)
104                 error("trace_endpos in solid!");
105         to = trace_endpos;
106
107         if(player.classname == "player")
108         {
109                 ang = Portal_Transform_Multiply(transform, player.v_angle);
110                 ang_z = player.angles_z;
111         }
112         else
113         {
114                 ang = Portal_Transform_Multiply(transform, player.angles);
115         }
116
117         print("previous velocity: ", vtos(player.velocity), "\n");
118         print("previous origin: ", vtos(player.origin), "\n");
119         TeleportPlayer(teleporter, player, to, ang, Portal_Transform_Apply(transform, player.velocity));
120         print("new velocity: ", vtos(player.velocity), "\n");
121         print("new origin: ", vtos(player.origin), "\n");
122 }
123
124 float Portal_FindSafeOrigin(entity portal)
125 {
126         vector o;
127         o = portal.origin;
128         portal.mins = PL_MIN - '8 8 8';
129         portal.maxs = PL_MAX + '8 8 8';
130         if(!move_out_of_solid(portal))
131         {
132                 print("NO SAFE ORIGIN\n");
133                 return 0;
134         }
135         portal.portal_safe_origin = portal.origin;
136         setorigin(portal, o);
137         return 1;
138 }
139
140 void Portal_Touch()
141 {
142         if(time < self.portal_activatetime)
143                 return;
144         makevectors(self.angles);
145         if((other.origin - self.origin) * v_forward < 0)
146                 return;
147         if(other.mins_x < PL_MIN_x || other.mins_y < PL_MIN_y || other.mins_z < PL_MIN_z
148         || other.maxs_x > PL_MAX_x || other.maxs_y > PL_MAX_y || other.maxs_z > PL_MAX_z)
149         {
150                 // can't teleport this
151                 return;
152         }
153         Portal_TeleportPlayer(self, other);
154
155         // reset fade counter
156         self.portal_wants_to_vanish = 0;
157         self.nextthink = time + 60;
158 }
159
160 void Portal_MakeBrokenPortal(entity portal)
161 {
162         portal.solid = SOLID_NOT;
163         portal.touch = SUB_Null;
164         portal.effects = 0;
165         portal.colormod = '1 1 1';
166 }
167
168 void Portal_MakeInPortal(entity portal)
169 {
170         portal.solid = SOLID_TRIGGER;
171         portal.touch = Portal_Touch;
172         portal.effects = EF_RED;
173         portal.colormod = '1 0 0';
174 }
175
176 void Portal_MakeOutPortal(entity portal)
177 {
178         portal.solid = SOLID_NOT;
179         portal.touch = SUB_Null;
180         portal.effects = EF_STARDUST;
181         portal.colormod = '0 0 1';
182 }
183
184 void Portal_Disconnect(entity teleporter, entity destination)
185 {
186         teleporter.enemy = world;
187         destination.enemy = world;
188         Portal_MakeBrokenPortal(teleporter);
189         Portal_MakeBrokenPortal(destination);
190 }
191
192 void Portal_Connect(entity teleporter, entity destination)
193 {
194         teleporter.portal_transform = Portal_Transform_Divide(Portal_Transform_TurnDirection(destination.angles), teleporter.angles);
195         teleporter.enemy = destination;
196         destination.enemy = teleporter;
197         Portal_MakeInPortal(teleporter);
198         Portal_MakeOutPortal(destination);
199         teleporter.nextthink = time + 60;
200         destination.nextthink = time + 60;
201         teleporter.portal_wants_to_vanish = 0;
202         destination.portal_wants_to_vanish = 0;
203 }
204
205 void Portal_Vanish()
206 {
207         self.portal_wants_to_vanish = 1;
208
209         if(self.enemy)
210                 if(!self.enemy.portal_wants_to_vanish)
211                         return;
212
213         SUB_SetFade(self, time + 1, 1);
214         if(self.enemy)
215         {
216                 SUB_SetFade(self.enemy, time + 1, 1);
217                 Portal_Disconnect(self, self.enemy);
218         }
219 }
220
221 void Portal_RequestVanish(entity portal)
222 {
223         entity oldself;
224         oldself = self;
225         self = portal;
226         if(self.enemy)
227                 self.enemy.portal_wants_to_vanish = 1;
228         Portal_Vanish();
229         self = oldself;
230 }
231
232 entity Portal_Spawn(entity own, vector org, vector ang)
233 {
234         entity portal;
235         portal = spawn();
236         portal.classname = "portal";
237         portal.owner = own;
238         portal.origin = org;
239         portal.angles = ang;
240         portal.think = Portal_Vanish;
241         portal.nextthink = time + 60;
242         portal.portal_activatetime = time + 0.1;
243         setmodel(portal, "models/items/g_h100.md3");
244
245         if(!Portal_FindSafeOrigin(portal))
246         {
247                 remove(portal);
248                 return world;
249         }
250
251         setsize(portal, '-32 -32 -32', '32 32 32');
252
253         return portal;
254 }
255
256 float Portal_SpawnInPortalAtTrace(entity own, vector dir, float portal_id_val)
257 {
258         if(own.portal_in)
259         {
260                 if(own.portal_out)
261                 {
262                         Portal_Disconnect(own.portal_in, own.portal_out);
263                         Portal_RequestVanish(own.portal_out);
264                 }
265                 own.portal_out = own.portal_in;
266                 own.portal_in = world;
267                 own.portal_out.portal_id = portal_id_val;
268         }
269         else if(own.portal_out)
270         {
271                 print("this DID happen, no idea why (1)\n");
272                 Portal_RequestVanish(own.portal_out);
273                 own.portal_out = world;
274         }
275                 
276         print("Plane normal: ", vtos(trace_plane_normal), "\n");
277         print("Portal angles: ", vtos(fixedvectoangles(trace_plane_normal)), "\n");
278         own.portal_in = Portal_Spawn(own, trace_endpos + trace_plane_normal, fixedvectoangles2(trace_plane_normal, dir));
279         if(!own.portal_in)
280                 return 0;
281         own.portal_in.portal_id = portal_id_val;
282
283         if(own.portal_out)
284                 Portal_Connect(own.portal_in, own.portal_out);
285
286         return 1;
287 }
288
289 float Portal_SpawnOutPortalAtTrace(entity own, vector dir, float portal_id_val)
290 {
291         if(!own.portal_in)
292                 return 0; // sorry
293
294         if(own.portal_in.portal_id != portal_id_val)
295                 return 0; // we need MATCHING portals
296
297         if(own.portal_out)
298         {
299                 Portal_Disconnect(own.portal_in, own.portal_out);
300                 Portal_RequestVanish(own.portal_out);
301                 own.portal_out = world;
302         }
303                 
304         print("Plane normal: ", vtos(trace_plane_normal), "\n");
305         print("Portal angles: ", vtos(fixedvectoangles(trace_plane_normal)), "\n");
306         own.portal_out = Portal_Spawn(own, trace_endpos + trace_plane_normal, fixedvectoangles2(trace_plane_normal, dir));
307         if(!own.portal_out)
308                 return 0;
309         own.portal_out.portal_id = portal_id_val;
310
311         Portal_Connect(own.portal_in, own.portal_out);
312         print("Portal transform: ", vtos(own.portal_in.portal_transform), "\n");
313
314         return 1;
315 }