49216057c447f115009ddc82c2f13da67cb3ee40
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / warpzone / client.qc
1 #include "client.qh"
2 #include "common.qh"
3
4 #if defined(CSQC)
5         #include "../../client/autocvars.qh"
6         #include "../csqcmodel/cl_model.qh"
7 #elif defined(MENUQC)
8 #elif defined(SVQC)
9 #endif
10
11 void WarpZone_Fade_PreDraw()
12 {SELFPARAM();
13         vector org;
14         org = getpropertyvec(VF_ORIGIN);
15         if(!checkpvs(org, self)) // this makes sense as long as we don't support recursive warpzones
16                 self.alpha = 0;
17         else if(self.warpzone_fadestart)
18                 self.alpha = bound(0, (self.warpzone_fadeend - vlen(org - self.origin - 0.5 * (self.mins + self.maxs))) / (self.warpzone_fadeend - self.warpzone_fadestart), 1);
19         else
20                 self.alpha = 1;
21         //printf("%v <-> %v\n", view_origin, self.origin + 0.5 * (self.mins + self.maxs));
22         if(self.alpha <= 0)
23                 self.drawmask = 0;
24         else
25                 self.drawmask = MASK_NORMAL;
26 }
27
28 void WarpZone_Read(float isnew)
29 {SELFPARAM();
30         warpzone_warpzones_exist = 1;
31         if (!self.enemy)
32         {
33                 self.enemy = new(warpzone_from);
34         }
35         self.classname = "trigger_warpzone";
36
37         int f = ReadByte();
38         self.warpzone_isboxy = (f & 1);
39         if(f & 4)
40         {
41                 self.origin_x = ReadCoord();
42                 self.origin_y = ReadCoord();
43                 self.origin_z = ReadCoord();
44         }
45         else
46                 self.origin = '0 0 0';
47         self.modelindex = ReadShort();
48         self.mins_x = ReadCoord();
49         self.mins_y = ReadCoord();
50         self.mins_z = ReadCoord();
51         self.maxs_x = ReadCoord();
52         self.maxs_y = ReadCoord();
53         self.maxs_z = ReadCoord();
54         self.scale = ReadByte() / 16;
55         self.enemy.oldorigin_x = ReadCoord();
56         self.enemy.oldorigin_y = ReadCoord();
57         self.enemy.oldorigin_z = ReadCoord();
58         self.enemy.avelocity_x = ReadCoord();
59         self.enemy.avelocity_y = ReadCoord();
60         self.enemy.avelocity_z = ReadCoord();
61         self.oldorigin_x = ReadCoord();
62         self.oldorigin_y = ReadCoord();
63         self.oldorigin_z = ReadCoord();
64         self.avelocity_x = ReadCoord();
65         self.avelocity_y = ReadCoord();
66         self.avelocity_z = ReadCoord();
67
68         if(f & 2)
69         {
70                 self.warpzone_fadestart = ReadShort();
71                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
72         }
73         else
74         {
75                 self.warpzone_fadestart = 0;
76                 self.warpzone_fadeend = 0;
77         }
78
79         // common stuff
80         WarpZone_SetUp(self, self.enemy.oldorigin, self.enemy.avelocity, self.oldorigin, self.avelocity);
81
82         // link me
83         //setmodel(self, self.model);
84         setorigin(self, self.origin);
85         setsize(self, self.mins, self.maxs);
86
87         // how to draw
88         // engine currently wants this
89         self.predraw = WarpZone_Fade_PreDraw;
90 }
91
92 void WarpZone_Camera_Read(float isnew)
93 {SELFPARAM();
94         warpzone_cameras_exist = 1;
95         self.classname = "func_warpzone_camera";
96
97         int f = ReadByte();
98         if(f & 4)
99         {
100                 self.origin_x = ReadCoord();
101                 self.origin_y = ReadCoord();
102                 self.origin_z = ReadCoord();
103         }
104         else
105                 self.origin = '0 0 0';
106         self.modelindex = ReadShort();
107         self.mins_x = ReadCoord();
108         self.mins_y = ReadCoord();
109         self.mins_z = ReadCoord();
110         self.maxs_x = ReadCoord();
111         self.maxs_y = ReadCoord();
112         self.maxs_z = ReadCoord();
113         self.scale = ReadByte() / 16;
114         self.oldorigin_x = ReadCoord();
115         self.oldorigin_y = ReadCoord();
116         self.oldorigin_z = ReadCoord();
117         self.avelocity_x = ReadCoord();
118         self.avelocity_y = ReadCoord();
119         self.avelocity_z = ReadCoord();
120
121         if(f & 2)
122         {
123                 self.warpzone_fadestart = ReadShort();
124                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
125         }
126         else
127         {
128                 self.warpzone_fadestart = 0;
129                 self.warpzone_fadeend = 0;
130         }
131
132         // common stuff
133         WarpZone_Camera_SetUp(self, self.oldorigin, self.avelocity);
134
135         // engine currently wants this
136         self.drawmask = MASK_NORMAL;
137
138         // link me
139         //setmodel(self, self.model);
140         setorigin(self, self.origin);
141         setsize(self, self.mins, self.maxs);
142
143         // how to draw
144         // engine currently wants this
145         self.predraw = WarpZone_Fade_PreDraw;
146 }
147
148 void CL_RotateMoves(vector ang) = #638;
149 void WarpZone_Teleported_Read(float isnew)
150 {SELFPARAM();
151         self.classname = "warpzone_teleported";
152         vector v;
153         v.x = ReadCoord();
154         v.y = ReadCoord();
155         v.z = ReadCoord();
156         if(!isnew)
157                 return;
158         self.warpzone_transform = v;
159         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(self, getpropertyvec(VF_CL_VIEWANGLES)));
160         if(checkextension("DP_CSQC_ROTATEMOVES"))
161                 CL_RotateMoves(v);
162                 //CL_RotateMoves('0 90 0');
163 }
164
165 float warpzone_fixingview;
166 float warpzone_fixingview_drawexteriormodel;
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 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
245         e = WarpZone_Find(org, org);
246         if(e)
247         {
248                 org = WarpZone_TransformOrigin(e, org);
249                 ang = WarpZone_TransformVAngles(e, ang);
250                 WarpZone_View_Inside();
251         }
252         else
253                 WarpZone_View_Outside();
254
255 #ifndef KEEP_ROLL
256         float rick;
257         if(autocvar_cl_rollkillspeed)
258                 f = max(0, (1 - frametime * autocvar_cl_rollkillspeed));
259         else
260                 f = 0;
261
262         rick = getproperty(VF_CL_VIEWANGLES_Z);
263         rick *= f;
264         setproperty(VF_CL_VIEWANGLES_Z, rick);
265         ang.z *= f;
266 #endif
267
268         setproperty(VF_ORIGIN, org);
269         setproperty(VF_ANGLES, ang);
270
271         nearclip = '0 0 1' * (cvar("r_nearclip") * 1.125);
272         corner0 = cs_unproject('0 0 0' + nearclip);
273         corner1 = cs_unproject('1 0 0' * cvar("vid_conwidth") + nearclip);
274         corner2 = cs_unproject('0 1 0' * cvar("vid_conheight") + nearclip);
275         corner3 = cs_unproject('1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight") + nearclip);
276         o = WarpZone_FixNearClip(org, corner0, corner1, corner2, corner3);
277         if(o != '0 0 0')
278                 setproperty(VF_ORIGIN, org + o);
279 }
280
281 void WarpZone_Init()
282 {
283 }
284
285 void WarpZone_Shutdown()
286 {
287         WarpZone_View_Outside();
288 }