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