Cleanup
[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 NET_HANDLE(ENT_CLIENT_WARPZONE, bool isnew)
29 {
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         return true;
91 }
92
93 NET_HANDLE(ENT_CLIENT_WARPZONE_CAMERA, bool isnew)
94 {
95         warpzone_cameras_exist = 1;
96         self.classname = "func_warpzone_camera";
97
98         int f = ReadByte();
99         if(f & 4)
100         {
101                 self.origin_x = ReadCoord();
102                 self.origin_y = ReadCoord();
103                 self.origin_z = ReadCoord();
104         }
105         else
106                 self.origin = '0 0 0';
107         self.modelindex = ReadShort();
108         self.mins_x = ReadCoord();
109         self.mins_y = ReadCoord();
110         self.mins_z = ReadCoord();
111         self.maxs_x = ReadCoord();
112         self.maxs_y = ReadCoord();
113         self.maxs_z = ReadCoord();
114         self.scale = ReadByte() / 16;
115         self.oldorigin_x = ReadCoord();
116         self.oldorigin_y = ReadCoord();
117         self.oldorigin_z = ReadCoord();
118         self.avelocity_x = ReadCoord();
119         self.avelocity_y = ReadCoord();
120         self.avelocity_z = ReadCoord();
121
122         if(f & 2)
123         {
124                 self.warpzone_fadestart = ReadShort();
125                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
126         }
127         else
128         {
129                 self.warpzone_fadestart = 0;
130                 self.warpzone_fadeend = 0;
131         }
132
133         // common stuff
134         WarpZone_Camera_SetUp(self, self.oldorigin, self.avelocity);
135
136         // engine currently wants this
137         self.drawmask = MASK_NORMAL;
138
139         // link me
140         //setmodel(self, self.model);
141         setorigin(self, self.origin);
142         setsize(self, self.mins, self.maxs);
143
144         // how to draw
145         // engine currently wants this
146         self.predraw = WarpZone_Fade_PreDraw;
147         return true;
148 }
149
150 void CL_RotateMoves(vector ang) = #638;
151 NET_HANDLE(ENT_CLIENT_WARPZONE_TELEPORTED, bool isnew)
152 {
153         self.classname = "warpzone_teleported";
154         vector v;
155         v.x = ReadCoord();
156         v.y = ReadCoord();
157         v.z = ReadCoord();
158         return = true;
159         if (!isnew) return;
160         self.warpzone_transform = v;
161         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(self, getpropertyvec(VF_CL_VIEWANGLES)));
162         if(checkextension("DP_CSQC_ROTATEMOVES"))
163                 CL_RotateMoves(v);
164                 //CL_RotateMoves('0 90 0');
165 }
166
167 float warpzone_fixingview;
168 float warpzone_fixingview_drawexteriormodel;
169
170 void WarpZone_View_Outside()
171 {
172         if(!warpzone_fixingview)
173                 return;
174         warpzone_fixingview = 0;
175         cvar_set("r_drawexteriormodel", ftos(warpzone_fixingview_drawexteriormodel));
176 }
177
178 void WarpZone_View_Inside()
179 {
180         if(autocvar_chase_active)
181         {
182                 WarpZone_View_Outside();
183                 return;
184         }
185         if(warpzone_fixingview)
186                 return;
187         warpzone_fixingview = 1;
188         warpzone_fixingview_drawexteriormodel = cvar("r_drawexteriormodel");
189         cvar_set("r_drawexteriormodel", "0");
190 }
191
192 vector WarpZone_FixNearClip(vector o, vector c0, vector c1, vector c2, vector c3)
193 {
194         vector mi, ma;
195         entity e;
196         float pd;
197
198         mi.x = min(o.x, c0_x, c1_x, c2_x, c3_x);
199         ma.x = max(o.x, c0_x, c1_x, c2_x, c3_x);
200         mi.y = min(o.y, c0_y, c1_y, c2_y, c3_y);
201         ma.y = max(o.y, c0_y, c1_y, c2_y, c3_y);
202         mi.z = min(o.z, c0_z, c1_z, c2_z, c3_z);
203         ma.z = max(o.z, c0_z, c1_z, c2_z, c3_z);
204
205         e = WarpZone_Find(mi, ma);
206         if(e)
207         {
208                 if(WarpZone_PlaneDist(e, o) < 0)
209                         return '0 0 0';
210                         // can't really be, though, but if it is, this is not my warpzone, but a random different one in the same mins/maxs
211                 pd = min(
212                                 WarpZone_PlaneDist(e, c0),
213                                 WarpZone_PlaneDist(e, c1),
214                                 WarpZone_PlaneDist(e, c2),
215                                 WarpZone_PlaneDist(e, c3)
216                         );
217                 if(pd < 0)
218                         return e.warpzone_forward * -pd;
219         }
220
221         return '0 0 0';
222 }
223
224 void WarpZone_FixPMove()
225 {
226         entity e;
227         e = WarpZone_Find(pmove_org, pmove_org);
228         if(e)
229         {
230                 pmove_org = WarpZone_TransformOrigin(e, pmove_org);
231                 input_angles = WarpZone_TransformVAngles(e, input_angles);
232         }
233 }
234
235 #ifndef KEEP_ROLL
236 float autocvar_cl_rollkillspeed = 10;
237 #endif
238 void WarpZone_FixView()
239 {
240         entity e;
241         vector org, ang, nearclip, corner0, corner1, corner2, corner3, o;
242         float f;
243
244         warpzone_save_view_origin = org = getpropertyvec(VF_ORIGIN);
245         warpzone_save_view_angles = ang = getpropertyvec(VF_ANGLES);
246
247         e = WarpZone_Find(org, org);
248         if(e)
249         {
250                 org = WarpZone_TransformOrigin(e, org);
251                 ang = WarpZone_TransformVAngles(e, ang);
252                 WarpZone_View_Inside();
253         }
254         else
255                 WarpZone_View_Outside();
256
257 #ifndef KEEP_ROLL
258         float rick;
259         if(autocvar_cl_rollkillspeed)
260                 f = max(0, (1 - frametime * autocvar_cl_rollkillspeed));
261         else
262                 f = 0;
263
264         rick = getproperty(VF_CL_VIEWANGLES_Z);
265         rick *= f;
266         setproperty(VF_CL_VIEWANGLES_Z, rick);
267         ang.z *= f;
268 #endif
269
270         setproperty(VF_ORIGIN, org);
271         setproperty(VF_ANGLES, ang);
272
273         nearclip = '0 0 1' * (cvar("r_nearclip") * 1.125);
274         corner0 = cs_unproject('0 0 0' + nearclip);
275         corner1 = cs_unproject('1 0 0' * cvar("vid_conwidth") + nearclip);
276         corner2 = cs_unproject('0 1 0' * cvar("vid_conheight") + nearclip);
277         corner3 = cs_unproject('1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight") + nearclip);
278         o = WarpZone_FixNearClip(org, corner0, corner1, corner2, corner3);
279         if(o != '0 0 0')
280                 setproperty(VF_ORIGIN, org + o);
281 }
282
283 void WarpZone_Shutdown()
284 {
285         WarpZone_View_Outside();
286 }