lib: move csqcmodellib and warpzonelib
[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 = spawn();
34                 self.enemy.classname = "warpzone_from";
35         }
36         self.classname = "trigger_warpzone";
37
38         int f = ReadByte();
39         self.warpzone_isboxy = (f & 1);
40         if(f & 4)
41         {
42                 self.origin_x = ReadCoord();
43                 self.origin_y = ReadCoord();
44                 self.origin_z = ReadCoord();
45         }
46         else
47                 self.origin = '0 0 0';
48         self.modelindex = ReadShort();
49         self.mins_x = ReadCoord();
50         self.mins_y = ReadCoord();
51         self.mins_z = ReadCoord();
52         self.maxs_x = ReadCoord();
53         self.maxs_y = ReadCoord();
54         self.maxs_z = ReadCoord();
55         self.scale = ReadByte() / 16;
56         self.enemy.oldorigin_x = ReadCoord();
57         self.enemy.oldorigin_y = ReadCoord();
58         self.enemy.oldorigin_z = ReadCoord();
59         self.enemy.avelocity_x = ReadCoord();
60         self.enemy.avelocity_y = ReadCoord();
61         self.enemy.avelocity_z = ReadCoord();
62         self.oldorigin_x = ReadCoord();
63         self.oldorigin_y = ReadCoord();
64         self.oldorigin_z = ReadCoord();
65         self.avelocity_x = ReadCoord();
66         self.avelocity_y = ReadCoord();
67         self.avelocity_z = ReadCoord();
68
69         if(f & 2)
70         {
71                 self.warpzone_fadestart = ReadShort();
72                 self.warpzone_fadeend = max(self.warpzone_fadestart + 1, ReadShort());
73         }
74         else
75         {
76                 self.warpzone_fadestart = 0;
77                 self.warpzone_fadeend = 0;
78         }
79
80         // common stuff
81         WarpZone_SetUp(self, self.enemy.oldorigin, self.enemy.avelocity, self.oldorigin, self.avelocity);
82
83         // link me
84         //setmodel(self, self.model);
85         setorigin(self, self.origin);
86         setsize(self, self.mins, self.maxs);
87
88         // how to draw
89         // engine currently wants this
90         self.predraw = WarpZone_Fade_PreDraw;
91 }
92
93 void WarpZone_Camera_Read(float isnew)
94 {SELFPARAM();
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 }
148
149 void CL_RotateMoves(vector ang) = #638;
150 void WarpZone_Teleported_Read(float isnew)
151 {SELFPARAM();
152         vector v;
153         self.classname = "warpzone_teleported";
154         v.x = ReadCoord();
155         v.y = ReadCoord();
156         v.z = ReadCoord();
157         if(!isnew)
158                 return;
159         self.warpzone_transform = v;
160         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(self, getpropertyvec(VF_CL_VIEWANGLES)));
161         if(checkextension("DP_CSQC_ROTATEMOVES"))
162                 CL_RotateMoves(v);
163                 //CL_RotateMoves('0 90 0');
164 }
165
166 float warpzone_fixingview;
167 float warpzone_fixingview_drawexteriormodel;
168
169 void WarpZone_View_Outside()
170 {
171         if(!warpzone_fixingview)
172                 return;
173         warpzone_fixingview = 0;
174         cvar_set("r_drawexteriormodel", ftos(warpzone_fixingview_drawexteriormodel));
175 }
176
177 void WarpZone_View_Inside()
178 {
179         if(autocvar_chase_active)
180         {
181                 WarpZone_View_Outside();
182                 return;
183         }
184         if(warpzone_fixingview)
185                 return;
186         warpzone_fixingview = 1;
187         warpzone_fixingview_drawexteriormodel = cvar("r_drawexteriormodel");
188         cvar_set("r_drawexteriormodel", "0");
189 }
190
191 vector WarpZone_FixNearClip(vector o, vector c0, vector c1, vector c2, vector c3)
192 {
193         vector mi, ma;
194         entity e;
195         float pd;
196
197         mi.x = min(o.x, c0_x, c1_x, c2_x, c3_x);
198         ma.x = max(o.x, c0_x, c1_x, c2_x, c3_x);
199         mi.y = min(o.y, c0_y, c1_y, c2_y, c3_y);
200         ma.y = max(o.y, c0_y, c1_y, c2_y, c3_y);
201         mi.z = min(o.z, c0_z, c1_z, c2_z, c3_z);
202         ma.z = max(o.z, c0_z, c1_z, c2_z, c3_z);
203
204         e = WarpZone_Find(mi, ma);
205         if(e)
206         {
207                 if(WarpZone_PlaneDist(e, o) < 0)
208                         return '0 0 0';
209                         // can't really be, though, but if it is, this is not my warpzone, but a random different one in the same mins/maxs
210                 pd = min(
211                                 WarpZone_PlaneDist(e, c0),
212                                 WarpZone_PlaneDist(e, c1),
213                                 WarpZone_PlaneDist(e, c2),
214                                 WarpZone_PlaneDist(e, c3)
215                         );
216                 if(pd < 0)
217                         return e.warpzone_forward * -pd;
218         }
219
220         return '0 0 0';
221 }
222
223 void WarpZone_FixPMove()
224 {
225         entity e;
226         e = WarpZone_Find(pmove_org, pmove_org);
227         if(e)
228         {
229                 pmove_org = WarpZone_TransformOrigin(e, pmove_org);
230                 input_angles = WarpZone_TransformVAngles(e, input_angles);
231         }
232 }
233
234 #ifndef KEEP_ROLL
235 float autocvar_cl_rollkillspeed = 10;
236 #endif
237 void WarpZone_FixView()
238 {
239         entity e;
240         vector org, ang, nearclip, corner0, corner1, corner2, corner3, o;
241         float f;
242
243         warpzone_save_view_origin = org = getpropertyvec(VF_ORIGIN);
244         warpzone_save_view_angles = ang = getpropertyvec(VF_ANGLES);
245
246         e = WarpZone_Find(org, org);
247         if(e)
248         {
249                 org = WarpZone_TransformOrigin(e, org);
250                 ang = WarpZone_TransformVAngles(e, ang);
251                 WarpZone_View_Inside();
252         }
253         else
254                 WarpZone_View_Outside();
255
256 #ifndef KEEP_ROLL
257         float rick;
258         if(autocvar_cl_rollkillspeed)
259                 f = max(0, (1 - frametime * autocvar_cl_rollkillspeed));
260         else
261                 f = 0;
262
263         rick = getproperty(VF_CL_VIEWANGLES_Z);
264         rick *= f;
265         setproperty(VF_CL_VIEWANGLES_Z, rick);
266         ang.z *= f;
267 #endif
268
269         setproperty(VF_ORIGIN, org);
270         setproperty(VF_ANGLES, ang);
271
272         nearclip = '0 0 1' * (cvar("r_nearclip") * 1.125);
273         corner0 = cs_unproject('0 0 0' + nearclip);
274         corner1 = cs_unproject('1 0 0' * cvar("vid_conwidth") + nearclip);
275         corner2 = cs_unproject('0 1 0' * cvar("vid_conheight") + nearclip);
276         corner3 = cs_unproject('1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight") + nearclip);
277         o = WarpZone_FixNearClip(org, corner0, corner1, corner2, corner3);
278         if(o != '0 0 0')
279                 setproperty(VF_ORIGIN, org + o);
280 }
281
282 void WarpZone_Init()
283 {
284 }
285
286 void WarpZone_Shutdown()
287 {
288         WarpZone_View_Outside();
289 }