Merge branch 'master' into terencehill/music_player
[xonotic/xonotic-data.pk3dir.git] / qcsrc / warpzonelib / client.qc
1 void WarpZone_Fade_PreDraw()
2 {
3         vector org;
4         org = getpropertyvec(VF_ORIGIN);
5         if(
6 #ifdef COMPAT_XON060_DONTCRASH_CHECKPVS
7                 cvar_string("g_xonoticversion") != "0.5.0" &&
8                 cvar_string("g_xonoticversion") != "0.6.0" &&
9 #endif
10                 !checkpvs(org, self)) // this makes sense as long as we don't support recursive warpzones
11                 self.alpha = 0;
12         else if(self.warpzone_fadestart)
13                 self.alpha = bound(0, (self.warpzone_fadeend - vlen(org - self.origin - 0.5 * (self.mins + self.maxs))) / (self.warpzone_fadeend - self.warpzone_fadestart), 1);
14         else
15                 self.alpha = 1;
16         //printf("%v <-> %v\n", view_origin, self.origin + 0.5 * (self.mins + self.maxs));
17         if(self.alpha <= 0)
18                 self.drawmask = 0;
19         else
20                 self.drawmask = MASK_NORMAL;
21 }
22
23 void WarpZone_Read(float isnew)
24 {
25         float f;
26
27         warpzone_warpzones_exist = 1;
28         if (!self.enemy)
29         {
30                 self.enemy = spawn();
31                 self.enemy.classname = "warpzone_from";
32         }
33         self.classname = "trigger_warpzone";
34
35         f = ReadByte();
36         self.warpzone_isboxy = (f & 1);
37         if(f & 4)
38         {
39                 self.origin_x = ReadCoord();
40                 self.origin_y = ReadCoord();
41                 self.origin_z = ReadCoord();
42         }
43         else
44                 self.origin = '0 0 0';
45         self.modelindex = ReadShort();
46         self.mins_x = ReadCoord();
47         self.mins_y = ReadCoord();
48         self.mins_z = ReadCoord();
49         self.maxs_x = ReadCoord();
50         self.maxs_y = ReadCoord();
51         self.maxs_z = ReadCoord();
52         self.scale = ReadByte() / 16;
53         self.enemy.oldorigin_x = ReadCoord();
54         self.enemy.oldorigin_y = ReadCoord();
55         self.enemy.oldorigin_z = ReadCoord();
56         self.enemy.avelocity_x = ReadCoord();
57         self.enemy.avelocity_y = ReadCoord();
58         self.enemy.avelocity_z = ReadCoord();
59         self.oldorigin_x = ReadCoord();
60         self.oldorigin_y = ReadCoord();
61         self.oldorigin_z = ReadCoord();
62         self.avelocity_x = ReadCoord();
63         self.avelocity_y = ReadCoord();
64         self.avelocity_z = ReadCoord();
65
66         if(f & 2)
67         {
68                 self.warpzone_fadestart = ReadShort();
69                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
70         }
71         else
72         {
73                 self.warpzone_fadestart = 0;
74                 self.warpzone_fadeend = 0;
75         }
76
77         // common stuff
78         WarpZone_SetUp(self, self.enemy.oldorigin, self.enemy.avelocity, self.oldorigin, self.avelocity);
79
80         // link me
81         //setmodel(self, self.model);
82         setorigin(self, self.origin);
83         setsize(self, self.mins, self.maxs);
84
85         // how to draw
86         // engine currently wants this
87         self.predraw = WarpZone_Fade_PreDraw;
88 }
89
90 void WarpZone_Camera_Read(float isnew)
91 {
92         float f;
93         warpzone_cameras_exist = 1;
94         self.classname = "func_warpzone_camera";
95
96         f = ReadByte();
97         if(f & 4)
98         {
99                 self.origin_x = ReadCoord();
100                 self.origin_y = ReadCoord();
101                 self.origin_z = ReadCoord();
102         }
103         else
104                 self.origin = '0 0 0';
105         self.modelindex = ReadShort();
106         self.mins_x = ReadCoord();
107         self.mins_y = ReadCoord();
108         self.mins_z = ReadCoord();
109         self.maxs_x = ReadCoord();
110         self.maxs_y = ReadCoord();
111         self.maxs_z = ReadCoord();
112         self.scale = ReadByte() / 16;
113         self.oldorigin_x = ReadCoord();
114         self.oldorigin_y = ReadCoord();
115         self.oldorigin_z = ReadCoord();
116         self.avelocity_x = ReadCoord();
117         self.avelocity_y = ReadCoord();
118         self.avelocity_z = ReadCoord();
119
120         if(f & 2)
121         {
122                 self.warpzone_fadestart = ReadShort();
123                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
124         }
125         else
126         {
127                 self.warpzone_fadestart = 0;
128                 self.warpzone_fadeend = 0;
129         }
130
131         // common stuff
132         WarpZone_Camera_SetUp(self, self.oldorigin, self.avelocity);
133
134         // engine currently wants this
135         self.drawmask = MASK_NORMAL;
136
137         // link me
138         //setmodel(self, self.model);
139         setorigin(self, self.origin);
140         setsize(self, self.mins, self.maxs);
141
142         // how to draw
143         // engine currently wants this
144         self.predraw = WarpZone_Fade_PreDraw;
145 }
146
147 void CL_RotateMoves(vector ang) = #638;
148 void WarpZone_Teleported_Read(float isnew)
149 {
150         vector v;
151         self.classname = "warpzone_teleported";
152         v_x = ReadCoord();
153         v_y = ReadCoord();
154         v_z = ReadCoord();
155         if(!isnew)
156                 return;
157         self.warpzone_transform = v;
158         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(self, getpropertyvec(VF_CL_VIEWANGLES)));
159         if(checkextension("DP_CSQC_ROTATEMOVES"))
160                 CL_RotateMoves(v);
161                 //CL_RotateMoves('0 90 0');
162 }
163
164 float warpzone_fixingview;
165 float warpzone_fixingview_drawexteriormodel;
166 float autocvar_chase_active;
167
168 void WarpZone_View_Outside()
169 {
170         if(!warpzone_fixingview)
171                 return;
172         warpzone_fixingview = 0;
173         cvar_set("r_drawexteriormodel", ftos(warpzone_fixingview_drawexteriormodel));
174 }
175
176 void WarpZone_View_Inside()
177 {
178         if(autocvar_chase_active)
179         {
180                 WarpZone_View_Outside();
181                 return;
182         }
183         if(warpzone_fixingview)
184                 return;
185         warpzone_fixingview = 1;
186         warpzone_fixingview_drawexteriormodel = cvar("r_drawexteriormodel");
187         cvar_set("r_drawexteriormodel", "0");
188 }
189
190 vector WarpZone_FixNearClip(vector o, vector c0, vector c1, vector c2, vector c3)
191 {
192         vector mi, ma;
193         entity e;
194         float pd;
195
196         mi_x = min(o_x, c0_x, c1_x, c2_x, c3_x);
197         ma_x = max(o_x, c0_x, c1_x, c2_x, c3_x);
198         mi_y = min(o_y, c0_y, c1_y, c2_y, c3_y);
199         ma_y = max(o_y, c0_y, c1_y, c2_y, c3_y);
200         mi_z = min(o_z, c0_z, c1_z, c2_z, c3_z);
201         ma_z = max(o_z, c0_z, c1_z, c2_z, c3_z);
202
203         e = WarpZone_Find(mi, ma);
204         if(e)
205         {
206                 if(WarpZone_PlaneDist(e, o) < 0)
207                         return '0 0 0';
208                         // can't really be, though, but if it is, this is not my warpzone, but a random different one in the same mins/maxs
209                 pd = min(
210                                 WarpZone_PlaneDist(e, c0),
211                                 WarpZone_PlaneDist(e, c1),
212                                 WarpZone_PlaneDist(e, c2),
213                                 WarpZone_PlaneDist(e, c3)
214                         );
215                 if(pd < 0)
216                         return e.warpzone_forward * -pd;
217         }
218
219         return '0 0 0';
220 }
221
222 void WarpZone_FixPMove()
223 {
224         entity e;
225         e = WarpZone_Find(pmove_org, pmove_org);
226         if(e)
227         {
228                 pmove_org = WarpZone_TransformOrigin(e, pmove_org);
229                 input_angles = WarpZone_TransformVAngles(e, input_angles);
230         }
231 }
232
233 #ifndef KEEP_ROLL
234 var float autocvar_cl_rollkillspeed = 10;
235 #endif
236 void WarpZone_FixView()
237 {
238         entity e;
239         vector org, ang, nearclip, corner0, corner1, corner2, corner3, o;
240         float f;
241
242         warpzone_save_view_origin = org = getpropertyvec(VF_ORIGIN);
243         warpzone_save_view_angles = ang = getpropertyvec(VF_ANGLES);
244 #ifdef WORKAROUND_XON010
245         float dirty;
246         dirty = checkextension("DP_CSQC_ROTATEMOVES");
247 #endif
248
249         e = WarpZone_Find(org, org);
250         if(e)
251         {
252                 org = WarpZone_TransformOrigin(e, org);
253                 ang = WarpZone_TransformVAngles(e, ang);
254 #ifdef WORKAROUND_XON010
255                 dirty = 1;
256 #endif
257                 WarpZone_View_Inside();
258         }
259         else
260                 WarpZone_View_Outside();
261
262 #ifndef KEEP_ROLL
263         float rick;
264         if(autocvar_cl_rollkillspeed)
265                 f = max(0, (1 - frametime * autocvar_cl_rollkillspeed));
266         else
267                 f = 0;
268
269         rick = getproperty(VF_CL_VIEWANGLES_Z);
270         rick *= f;
271         setproperty(VF_CL_VIEWANGLES_Z, rick);
272
273 #ifdef WORKAROUND_XON010
274         if(ang_z > 1 || ang_z < -1)
275                 dirty = 1;
276 #endif
277         ang_z *= f;
278 #endif
279
280 #ifdef WORKAROUND_XON010
281         if(dirty)
282         {
283 #endif
284         setproperty(VF_ORIGIN, org);
285         setproperty(VF_ANGLES, ang);
286 #ifdef WORKAROUND_XON010
287         }
288 #endif
289
290         nearclip = '0 0 1' * (cvar("r_nearclip") * 1.125);
291         corner0 = cs_unproject('0 0 0' + nearclip);
292         corner1 = cs_unproject('1 0 0' * cvar("vid_conwidth") + nearclip);
293         corner2 = cs_unproject('0 1 0' * cvar("vid_conheight") + nearclip);
294         corner3 = cs_unproject('1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight") + nearclip);
295         o = WarpZone_FixNearClip(org, corner0, corner1, corner2, corner3);
296         if(o != '0 0 0')
297                 setproperty(VF_ORIGIN, org + o);
298 }
299
300 void WarpZone_Init()
301 {
302 }
303
304 void WarpZone_Shutdown()
305 {
306         WarpZone_View_Outside();
307 }