7f8b0cdc837258a7bb5547d984afbcd960430baa
[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(entity this, entity toucher);
29 NET_HANDLE(ENT_CLIENT_WARPZONE, bool isnew)
30 {
31         if(!warpzone_warpzones_exist)
32                 cvar_settemp("r_water", "1"); // HACK for DarkPlaces: always enable reflections when a map has warpzones
33         warpzone_warpzones_exist = 1;
34         if (!this.enemy)
35         {
36                 this.enemy = new(warpzone_from);
37         }
38         this.classname = "trigger_warpzone";
39
40         if(isnew)
41                 IL_PUSH(g_warpzones, this);
42
43         int f = ReadByte();
44         this.warpzone_isboxy = (f & 1);
45         if(f & 4)
46         {
47                 this.origin = ReadVector();
48         }
49         else
50                 this.origin = '0 0 0';
51         this.modelindex = ReadShort();
52         this.mins = ReadVector();
53         this.maxs = ReadVector();
54         this.scale = ReadByte() / 16;
55         this.enemy.oldorigin = ReadVector();
56         this.enemy.avelocity = ReadVector();
57         this.oldorigin = ReadVector();
58         this.avelocity = ReadVector();
59
60         if(f & 2)
61         {
62                 this.warpzone_fadestart = ReadShort();
63                 this.warpzone_fadeend = max(this.warpzone_fadestart + 1, ReadShort());
64         }
65         else
66         {
67                 this.warpzone_fadestart = 0;
68                 this.warpzone_fadeend = 0;
69         }
70
71         // common stuff
72         WarpZone_SetUp(this, this.enemy.oldorigin, this.enemy.avelocity, this.oldorigin, this.avelocity);
73
74         // link me
75         //setmodel(this, this.model);
76         setorigin(this, this.origin);
77         setsize(this, this.mins, this.maxs);
78
79         // how to draw
80         // engine currently wants this
81         setpredraw(this, WarpZone_Fade_PreDraw);
82
83         //settouch(this, WarpZone_Touch);
84         return true;
85 }
86
87 NET_HANDLE(ENT_CLIENT_WARPZONE_CAMERA, bool isnew)
88 {
89         if(!warpzone_cameras_exist)
90                 cvar_settemp("r_water", "1"); // HACK for DarkPlaces: always enable reflections when a map has cameras
91         warpzone_cameras_exist = 1;
92         this.classname = "func_warpzone_camera";
93
94         int f = ReadByte();
95         if(f & 4)
96         {
97                 this.origin = ReadVector();
98         }
99         else
100                 this.origin = '0 0 0';
101         this.modelindex = ReadShort();
102         this.mins = ReadVector();
103         this.maxs = ReadVector();
104         this.scale = ReadByte() / 16;
105         this.oldorigin = ReadVector();
106         this.avelocity = ReadVector();
107
108         if(f & 2)
109         {
110                 this.warpzone_fadestart = ReadShort();
111                 this.warpzone_fadeend = max(this.warpzone_fadestart + 1, ReadShort());
112         }
113         else
114         {
115                 this.warpzone_fadestart = 0;
116                 this.warpzone_fadeend = 0;
117         }
118
119         // common stuff
120         WarpZone_Camera_SetUp(this, this.oldorigin, this.avelocity);
121
122         // engine currently wants this
123         this.drawmask = MASK_NORMAL;
124
125         // link me
126         //setmodel(this, this.model);
127         setorigin(this, this.origin);
128         setsize(this, this.mins, this.maxs);
129
130         // how to draw
131         // engine currently wants this
132         setpredraw(this, WarpZone_Fade_PreDraw);
133         return true;
134 }
135
136 void CL_RotateMoves(vector ang) = #638;
137 NET_HANDLE(ENT_CLIENT_WARPZONE_TELEPORTED, bool isnew)
138 {
139         this.classname = "warpzone_teleported";
140         vector v = ReadVector();
141         return = true;
142         if (!isnew) return;
143         this.warpzone_transform = v;
144         setproperty(VF_CL_VIEWANGLES, WarpZone_TransformVAngles(this, getpropertyvec(VF_CL_VIEWANGLES)));
145         if(checkextension("DP_CSQC_ROTATEMOVES"))
146                 CL_RotateMoves(v);
147                 //CL_RotateMoves('0 90 0');
148 }
149
150 float warpzone_fixingview;
151 float warpzone_fixingview_drawexteriormodel;
152
153 void WarpZone_View_Outside()
154 {
155         if(!warpzone_fixingview)
156                 return;
157         warpzone_fixingview = 0;
158         cvar_set("r_drawexteriormodel", ftos(warpzone_fixingview_drawexteriormodel));
159 }
160
161 void WarpZone_View_Inside()
162 {
163         if(autocvar_chase_active)
164         {
165                 WarpZone_View_Outside();
166                 return;
167         }
168         if(warpzone_fixingview)
169                 return;
170         warpzone_fixingview = 1;
171         warpzone_fixingview_drawexteriormodel = cvar("r_drawexteriormodel");
172         cvar_set("r_drawexteriormodel", "0");
173 }
174
175 vector WarpZone_FixNearClip(vector o, vector c0, vector c1, vector c2, vector c3)
176 {
177         vector mi, ma;
178         entity e;
179         float pd;
180
181         mi.x = min(o.x, c0_x, c1_x, c2_x, c3_x);
182         ma.x = max(o.x, c0_x, c1_x, c2_x, c3_x);
183         mi.y = min(o.y, c0_y, c1_y, c2_y, c3_y);
184         ma.y = max(o.y, c0_y, c1_y, c2_y, c3_y);
185         mi.z = min(o.z, c0_z, c1_z, c2_z, c3_z);
186         ma.z = max(o.z, c0_z, c1_z, c2_z, c3_z);
187
188         e = WarpZone_Find(mi, ma);
189         if(e)
190         {
191                 if(WarpZone_PlaneDist(e, o) < 0)
192                         return '0 0 0';
193                         // can't really be, though, but if it is, this is not my warpzone, but a random different one in the same mins/maxs
194                 pd = min(
195                                 WarpZone_PlaneDist(e, c0),
196                                 WarpZone_PlaneDist(e, c1),
197                                 WarpZone_PlaneDist(e, c2),
198                                 WarpZone_PlaneDist(e, c3)
199                         );
200                 if(pd < 0)
201                         return e.warpzone_forward * -pd;
202         }
203
204         return '0 0 0';
205 }
206
207 void WarpZone_FixPMove()
208 {
209         entity e = WarpZone_Find(pmove_org, pmove_org);
210         if(e)
211         {
212                 pmove_org = WarpZone_TransformOrigin(e, pmove_org);
213                 input_angles = WarpZone_TransformVAngles(e, input_angles);
214         }
215 }
216
217 #ifndef KEEP_ROLL
218 float autocvar_cl_rollkillspeed = 10;
219 #endif
220 void WarpZone_FixView()
221 {
222         entity e;
223         vector org, ang, nearclip, corner0, corner1, corner2, corner3, o;
224
225         warpzone_save_view_origin = org = getpropertyvec(VF_ORIGIN);
226         warpzone_save_view_angles = ang = getpropertyvec(VF_ANGLES);
227
228         e = WarpZone_Find(org, org);
229         if(e)
230         {
231                 org = WarpZone_TransformOrigin(e, org);
232                 ang = WarpZone_TransformVAngles(e, ang);
233                 WarpZone_View_Inside();
234         }
235         else
236                 WarpZone_View_Outside();
237
238 #ifndef KEEP_ROLL
239         float rick;
240         float f;
241         static float rollkill;
242         if (STAT(HEALTH) > 0 || STAT(HEALTH) == -666 || STAT(HEALTH) == -2342)
243         {
244                 f = 0;
245                 // reset roll when passing through a warpzone that change player's roll angle
246                 if(autocvar_cl_rollkillspeed)
247                         f = max(0, (1 - frametime * autocvar_cl_rollkillspeed));
248                 if(rollkill)
249                         rollkill = 0;
250         }
251         else
252         {
253                 f = 1;
254                 // roll the view when killed (v_deathtilt)
255                 if(autocvar_cl_rollkillspeed)
256                 {
257                         rollkill += frametime * autocvar_cl_rollkillspeed;
258                         f = min(1, rollkill);
259                 }
260                 else if(rollkill)
261                         rollkill = 0;
262         }
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 }