]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/view.qc
Fix weaponmodel effects passing through warpzones
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / view.qc
1
2 #include "announcer.qh"
3 #include "hud/all.qh"
4 #include "mapvoting.qh"
5 #include "scoreboard.qh"
6 #include "shownames.qh"
7 #include "quickmenu.qh"
8
9 #include "mutators/events.qh"
10
11 #include "../common/anim.qh"
12 #include "../common/constants.qh"
13 #include "../common/debug.qh"
14 #include "../common/mapinfo.qh"
15 #include <common/gamemodes/all.qh>
16 #include "../common/physics/player.qh"
17 #include "../common/stats.qh"
18 #include "../common/triggers/target/music.qh"
19 #include "../common/teams.qh"
20
21 #include <common/vehicles/all.qh>
22 #include <common/weapons/all.qh>
23 #include "../common/viewloc.qh"
24 #include "../common/minigames/cl_minigames.qh"
25 #include "../common/minigames/cl_minigames_hud.qh"
26
27 #include "../lib/csqcmodel/cl_player.qh"
28
29 #include "../lib/warpzone/client.qh"
30 #include "../lib/warpzone/common.qh"
31
32 #define EFMASK_CHEAP (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NODRAW | EF_NOSHADOW | EF_SELECTABLE | EF_TELEPORT_BIT)
33
34 float autocvar_cl_viewmodel_scale;
35
36 bool autocvar_cl_bobmodel;
37 float autocvar_cl_bobmodel_speed;
38 float autocvar_cl_bobmodel_side;
39 float autocvar_cl_bobmodel_up;
40
41 float autocvar_cl_followmodel;
42 float autocvar_cl_followmodel_speed = 0.3;
43 float autocvar_cl_followmodel_limit = 1000;
44 float autocvar_cl_followmodel_highpass1 = 0.05;
45 float autocvar_cl_followmodel_highpass = 0.05;
46 float autocvar_cl_followmodel_lowpass = 0.03;
47
48 float autocvar_cl_leanmodel;
49 float autocvar_cl_leanmodel_speed = 0.3;
50 float autocvar_cl_leanmodel_limit = 1000;
51 float autocvar_cl_leanmodel_highpass1 = 0.2;
52 float autocvar_cl_leanmodel_highpass = 0.2;
53 float autocvar_cl_leanmodel_lowpass = 0.05;
54
55 #define avg_factor(avg_time) (1 - exp(-frametime / max(0.001, avg_time)))
56 #define lowpass(value, frac, ref_store, ret) MACRO_BEGIN \
57 { \
58         ret = ref_store = ref_store * (1 - frac) + (value) * frac; \
59 } MACRO_END
60
61 #define lowpass_limited(value, frac, limit, ref_store, ret) MACRO_BEGIN \
62 { \
63         float __ignore; lowpass(value, frac, ref_store, __ignore); \
64         ret = ref_store = bound((value) - (limit), ref_store, (value) + (limit)); \
65 } MACRO_END
66
67 #define highpass(value, frac, ref_store, ret) MACRO_BEGIN \
68 { \
69         float __f = 0; lowpass(value, frac, ref_store, __f); \
70         ret = (value) - __f; \
71 } MACRO_END
72
73 #define highpass_limited(value, frac, limit, ref_store, ret) MACRO_BEGIN \
74 { \
75         float __f = 0; lowpass_limited(value, frac, limit, ref_store, __f); \
76         ret = (value) - __f; \
77 } MACRO_END
78
79 #define lowpass2(value, frac, ref_store, ref_out) MACRO_BEGIN \
80 { \
81         lowpass(value.x, frac, ref_store.x, ref_out.x); \
82         lowpass(value.y, frac, ref_store.y, ref_out.y); \
83 } MACRO_END
84
85 #define lowpass2_limited(value, frac, limit, ref_store, ref_out) MACRO_BEGIN \
86 { \
87         lowpass_limited(value.x, frac, limit, ref_store.x, ref_out.x); \
88         lowpass_limited(value.y, frac, limit, ref_store.y, ref_out.y); \
89 } MACRO_END
90
91 #define highpass2(value, frac, ref_store, ref_out) MACRO_BEGIN \
92 { \
93         highpass(value.x, frac, ref_store.x, ref_out.x); \
94         highpass(value.y, frac, ref_store.y, ref_out.y); \
95 } MACRO_END
96
97 #define highpass2_limited(value, frac, limit, ref_store, ref_out) MACRO_BEGIN \
98 { \
99         highpass_limited(value.x, frac, limit, ref_store.x, ref_out.x); \
100         highpass_limited(value.y, frac, limit, ref_store.y, ref_out.y); \
101 } MACRO_END
102
103 #define lowpass3(value, frac, ref_store, ref_out) MACRO_BEGIN \
104 { \
105         lowpass(value.x, frac, ref_store.x, ref_out.x); \
106         lowpass(value.y, frac, ref_store.y, ref_out.y); \
107         lowpass(value.z, frac, ref_store.z, ref_out.z); \
108 } MACRO_END
109
110 #define highpass3(value, frac, ref_store, ref_out) MACRO_BEGIN \
111 { \
112         highpass(value.x, frac, ref_store.x, ref_out.x); \
113         highpass(value.y, frac, ref_store.y, ref_out.y); \
114         highpass(value.z, frac, ref_store.z, ref_out.z); \
115 } MACRO_END
116
117 #define highpass3_limited(value, frac, limit, ref_store, ref_out) MACRO_BEGIN \
118 { \
119         highpass_limited(value.x, frac, limit, ref_store.x, ref_out.x); \
120         highpass_limited(value.y, frac, limit, ref_store.y, ref_out.y); \
121         highpass_limited(value.z, frac, limit, ref_store.z, ref_out.z); \
122 } MACRO_END
123
124 #define lowpass3_limited(value, frac, limit, ref_store, ref_out) MACRO_BEGIN \
125 { \
126         lowpass_limited(value.x, frac, limit, ref_store.x, ref_out.x); \
127         lowpass_limited(value.y, frac, limit, ref_store.y, ref_out.y); \
128         lowpass_limited(value.z, frac, limit, ref_store.z, ref_out.z); \
129 } MACRO_END
130
131 void viewmodel_animate(entity this)
132 {
133         static float prevtime;
134         float frametime = (time - prevtime);
135         prevtime = time;
136
137         if (autocvar_chase_active) return;
138         if (STAT(HEALTH) <= 0) return;
139
140         entity view = CSQCModel_server2csqc(player_localentnum - 1);
141
142         bool clonground = !(view.anim_implicit_state & ANIMIMPLICITSTATE_INAIR);
143         static bool oldonground;
144         static float hitgroundtime;
145         static float lastongroundtime;
146         if (clonground)
147         {
148                 float f = time; // cl.movecmd[0].time
149                 if (!oldonground)
150                         hitgroundtime = f;
151                 lastongroundtime = f;
152         }
153         oldonground = clonground;
154
155
156         bool teleported = view.csqcmodel_teleported;
157
158         float frac;
159         if(autocvar_cl_followmodel)
160         {
161                 vector gunorg = '0 0 0';
162                 static vector vel_average;
163                 static vector gunorg_prev = '0 0 0';
164                 static vector gunorg_adjustment_highpass;
165                 static vector gunorg_adjustment_lowpass;
166
167                 vector vel;
168                 vector forward, right = '0 0 0', up = '0 0 0';
169                 MAKEVECTORS(makevectors, view_angles, forward, right, up);
170                 vel.x = view.velocity * forward;
171                 vel.y = view.velocity * right * -1;
172                 vel.z = view.velocity * up;
173                 frac = avg_factor(autocvar_cl_followmodel_highpass1);
174                 lowpass3_limited(vel, frac, autocvar_cl_followmodel_limit, vel_average, gunorg);
175
176                 gunorg *= -autocvar_cl_followmodel_speed * 0.042;
177
178                 // perform highpass/lowpass on the adjustment vectors (turning velocity into acceleration!)
179                 // trick: we must do the lowpass LAST, so the lowpass vector IS the final vector!
180                 frac = avg_factor(autocvar_cl_followmodel_highpass);
181                 highpass3(gunorg, frac, gunorg_adjustment_highpass, gunorg);
182                 frac = avg_factor(autocvar_cl_followmodel_lowpass);
183                 lowpass3(gunorg, frac, gunorg_adjustment_lowpass, gunorg);
184
185                 this.origin += gunorg;
186         }
187
188         if(autocvar_cl_leanmodel)
189         {
190                 vector gunangles = '0 0 0';
191                 static vector gunangles_prev = '0 0 0';
192                 static vector gunangles_highpass = '0 0 0';
193                 static vector gunangles_adjustment_highpass;
194                 static vector gunangles_adjustment_lowpass;
195
196                 if (teleported)
197                         gunangles_prev = view_angles;
198
199                 // in the highpass, we _store_ the DIFFERENCE to the actual view angles...
200                 gunangles_highpass += gunangles_prev;
201                 PITCH(gunangles_highpass) += 360 * floor((PITCH(view_angles) - PITCH(gunangles_highpass)) / 360 + 0.5);
202                 YAW(gunangles_highpass) += 360 * floor((YAW(view_angles) - YAW(gunangles_highpass)) / 360 + 0.5);
203                 ROLL(gunangles_highpass) += 360 * floor((ROLL(view_angles) - ROLL(gunangles_highpass)) / 360 + 0.5);
204                 frac = avg_factor(autocvar_cl_leanmodel_highpass1);
205                 highpass2_limited(view_angles, frac, autocvar_cl_leanmodel_limit, gunangles_highpass, gunangles);
206                 gunangles_prev = view_angles;
207                 gunangles_highpass -= gunangles_prev;
208
209                 PITCH(gunangles) *= -autocvar_cl_leanmodel_speed;
210                 YAW(gunangles) *= -autocvar_cl_leanmodel_speed;
211
212                 // we assume here: PITCH = 0, YAW = 1, ROLL = 2
213                 frac = avg_factor(autocvar_cl_leanmodel_highpass);
214                 highpass2(gunangles, frac, gunangles_adjustment_highpass, gunangles);
215                 frac = avg_factor(autocvar_cl_leanmodel_lowpass);
216                 lowpass2(gunangles, frac, gunangles_adjustment_lowpass, gunangles);
217
218                 gunangles.x = -gunangles.x; // pitch was inverted, now that actually matters
219                 this.angles += gunangles;
220         }
221
222         float xyspeed = bound(0, vlen(vec2(view.velocity)), 400);
223
224         // vertical view bobbing code
225         // TODO: cl_bob
226
227         // horizontal view bobbing code
228         // TODO: cl_bob2
229
230         // fall bobbing code
231         // causes the view to swing down and back up when touching the ground
232         // TODO: cl_bobfall
233
234         // gun model bobbing code
235         if (autocvar_cl_bobmodel)
236         {
237                 // calculate for swinging gun model
238                 // the gun bobs when running on the ground, but doesn't bob when you're in the air.
239                 // Sajt: I tried to smooth out the transitions between bob and no bob, which works
240                 // for the most part, but for some reason when you go through a message trigger or
241                 // pick up an item or anything like that it will momentarily jolt the gun.
242                 float bspeed;
243                 float t = 1;
244                 float s = time * autocvar_cl_bobmodel_speed;
245                 if (clonground)
246                 {
247                         if (time - hitgroundtime < 0.2)
248                         {
249                                 // just hit the ground, speed the bob back up over the next 0.2 seconds
250                                 t = time - hitgroundtime;
251                                 t = bound(0, t, 0.2);
252                                 t *= 5;
253                         }
254                 }
255                 else
256                 {
257                         // recently left the ground, slow the bob down over the next 0.2 seconds
258                         t = time - lastongroundtime;
259                         t = 0.2 - bound(0, t, 0.2);
260                         t *= 5;
261                 }
262                 bspeed = xyspeed * 0.01;
263                 vector gunorg = '0 0 0';
264                 gunorg.y += bspeed * autocvar_cl_bobmodel_side * autocvar_cl_viewmodel_scale * sin(s) * t;
265                 gunorg.z += bspeed * autocvar_cl_bobmodel_up * autocvar_cl_viewmodel_scale * cos(s * 2) * t;
266
267                 this.origin += gunorg;
268         }
269 }
270
271 .vector viewmodel_origin, viewmodel_angles;
272 .float weapon_nextthink;
273 .float weapon_eta_last;
274 .float weapon_switchdelay;
275
276 void viewmodel_draw(entity this)
277 {
278         if(!activeweapon)
279                 return;
280         int mask = (intermission || (STAT(HEALTH) <= 0) || autocvar_chase_active) ? 0 : MASK_NORMAL;
281         float a = this.alpha;
282         static bool wasinvehicle;
283         bool invehicle = player_localentnum > maxclients;
284         if (invehicle) a = -1;
285         else if (wasinvehicle) a = 1;
286         wasinvehicle = invehicle;
287         Weapon wep = activeweapon;
288         int c = stof(getplayerkeyvalue(current_player, "colors"));
289         vector g = weaponentity_glowmod(wep, c);
290         entity me = CSQCModel_server2csqc(player_localentnum - 1);
291         int fx = ((me.csqcmodel_effects & EFMASK_CHEAP)
292                 | EF_NODEPTHTEST)
293                 &~ (EF_FULLBRIGHT); // can mask team color, so get rid of it
294         for (entity e = this; e; e = e.weaponchild)
295         {
296                 e.drawmask = mask;
297                 e.alpha = a;
298                 e.colormap = 256 + c;  // colormap == 0 is black, c == 0 is white
299                 e.glowmod = g;
300                 e.csqcmodel_effects = fx;
301                 CSQCModel_Effects_Apply(e);
302         }
303         {
304                 static string name_last;
305                 string name = wep.mdl;
306                 if (name != name_last)
307                 {
308                         name_last = name;
309                         CL_WeaponEntity_SetModel(this, name);
310                         this.viewmodel_origin = this.origin;
311                         this.viewmodel_angles = this.angles;
312                 }
313                 anim_update(this);
314                 if (!this.animstate_override)
315                         anim_set(this, this.anim_idle, true, false, false);
316         }
317         float f = 0; // 0..1; 0: fully active
318         float eta = (this.weapon_nextthink - time) / STAT(WEAPONRATEFACTOR);
319         if (eta <= 0) f = this.weapon_eta_last;
320         else switch (this.state)
321         {
322                 case WS_RAISE:
323                 {
324                         f = eta / max(eta, this.weapon_switchdelay);
325                         break;
326                 }
327                 case WS_DROP:
328                 {
329                         f = 1 - eta / max(eta, this.weapon_switchdelay);
330                         break;
331                 }
332                 case WS_CLEAR:
333                 {
334                         f = 1;
335                         break;
336                 }
337         }
338         this.weapon_eta_last = f;
339         this.origin = this.viewmodel_origin;
340         this.angles = this.viewmodel_angles;
341         this.angles_x = (-90 * f * f);
342         viewmodel_animate(this);
343         setorigin(this, this.origin);
344 }
345
346 entity viewmodel;
347 STATIC_INIT(viewmodel) {
348     viewmodel = new(viewmodel);
349     make_pure(viewmodel);
350         viewmodel.draw = viewmodel_draw;
351 }
352
353 entity porto;
354 vector polyline[16];
355 void Porto_Draw(entity this)
356 {
357         vector p, dir, ang, q, nextdir;
358         float portal_number, portal1_idx;
359
360         if(activeweapon != WEP_PORTO || spectatee_status || gametype == MAPINFO_TYPE_NEXBALL)
361                 return;
362         if(WEP_CVAR(porto, secondary))
363                 return;
364         if(intermission == 1)
365                 return;
366         if(intermission == 2)
367                 return;
368         if (STAT(HEALTH) <= 0)
369                 return;
370
371         dir = view_forward;
372
373         if(angles_held_status)
374         {
375                 makevectors(angles_held);
376                 dir = v_forward;
377         }
378
379         p = view_origin;
380
381         polyline[0] = p;
382         int idx = 1;
383         portal_number = 0;
384         nextdir = dir;
385
386         for (;;)
387         {
388                 dir = nextdir;
389                 traceline(p, p + 65536 * dir, true, porto);
390                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
391                         return;
392                 nextdir = dir - 2 * (dir * trace_plane_normal) * trace_plane_normal; // mirror dir at trace_plane_normal
393                 p = trace_endpos;
394                 polyline[idx] = p;
395                 ++idx;
396                 if(idx >= 16)
397                         return;
398                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
399                         continue;
400                 ++portal_number;
401                 ang = vectoangles2(trace_plane_normal, dir);
402                 ang.x = -ang.x;
403                 makevectors(ang);
404                 if(!CheckWireframeBox(porto, p - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
405                         return;
406                 if(portal_number == 1)
407                 {
408                         portal1_idx = idx;
409                         if(portal_number >= 2)
410                                 break;
411                 }
412         }
413
414         while(idx >= 2)
415         {
416                 p = polyline[idx-2];
417                 q = polyline[idx-1];
418                 if(idx == 2)
419                         p = p - view_up * 16;
420                 if(idx-1 >= portal1_idx)
421                 {
422                         Draw_CylindricLine(p, q, 4, "", 1, 0, '0 0 1', 0.5, DRAWFLAG_NORMAL, view_origin);
423                 }
424                 else
425                 {
426                         Draw_CylindricLine(p, q, 4, "", 1, 0, '1 0 0', 0.5, DRAWFLAG_NORMAL, view_origin);
427                 }
428                 --idx;
429         }
430 }
431
432 void Porto_Init()
433 {
434         porto = new(porto);
435         make_pure(porto);
436         porto.draw = Porto_Draw;
437         porto.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
438 }
439
440 float drawtime;
441 float avgspeed;
442 vector GetCurrentFov(float fov)
443 {
444         float zoomsensitivity, zoomspeed, zoomfactor, zoomdir;
445         float velocityzoom, curspeed;
446         vector v;
447
448         zoomsensitivity = autocvar_cl_zoomsensitivity;
449         zoomfactor = autocvar_cl_zoomfactor;
450         if(zoomfactor < 1 || zoomfactor > 30)
451                 zoomfactor = 2.5;
452         zoomspeed = autocvar_cl_zoomspeed;
453         if(zoomspeed >= 0)
454         if(zoomspeed < 0.5 || zoomspeed > 16)
455                         zoomspeed = 3.5;
456
457         zoomdir = button_zoom;
458         if(hud == HUD_NORMAL)
459         if(switchweapon == activeweapon)
460         if((activeweapon == WEP_VORTEX && !WEP_CVAR(vortex, secondary)) || (activeweapon == WEP_RIFLE && !WEP_CVAR(rifle, secondary))) // do NOT use switchweapon here
461                 zoomdir += button_attack2;
462         if(spectatee_status > 0 || isdemo())
463         {
464                 if(spectatorbutton_zoom)
465                 {
466                         if(zoomdir)
467                                 zoomdir = 0;
468                         else
469                                 zoomdir = 1;
470                 }
471                 // fteqcc failed twice here already, don't optimize this
472         }
473
474         if(zoomdir) { zoomin_effect = 0; }
475
476         if(camera_active)
477         {
478                 current_viewzoom = min(1, current_viewzoom + drawframetime);
479         }
480         else if(autocvar_cl_spawnzoom && zoomin_effect)
481         {
482                 float spawnzoomfactor = bound(1, autocvar_cl_spawnzoom_factor, 30);
483
484                 current_viewzoom += (autocvar_cl_spawnzoom_speed * (spawnzoomfactor - current_viewzoom) * drawframetime);
485                 current_viewzoom = bound(1 / spawnzoomfactor, current_viewzoom, 1);
486                 if(current_viewzoom == 1) { zoomin_effect = 0; }
487         }
488         else
489         {
490                 if(zoomspeed < 0) // instant zoom
491                 {
492                         if(zoomdir)
493                                 current_viewzoom = 1 / zoomfactor;
494                         else
495                                 current_viewzoom = 1;
496                 }
497                 else
498                 {
499                         if(zoomdir)
500                                 current_viewzoom = 1 / bound(1, 1 / current_viewzoom + drawframetime * zoomspeed * (zoomfactor - 1), zoomfactor);
501                         else
502                                 current_viewzoom = bound(1 / zoomfactor, current_viewzoom + drawframetime * zoomspeed * (1 - 1 / zoomfactor), 1);
503                 }
504         }
505
506         if(almost_equals(current_viewzoom, 1))
507                 current_zoomfraction = 0;
508         else if(almost_equals(current_viewzoom, 1/zoomfactor))
509                 current_zoomfraction = 1;
510         else
511                 current_zoomfraction = (current_viewzoom - 1) / (1/zoomfactor - 1);
512
513         if(zoomsensitivity < 1)
514                 setsensitivityscale(pow(current_viewzoom, 1 - zoomsensitivity));
515         else
516                 setsensitivityscale(1);
517
518         if(autocvar_cl_velocityzoom_enabled && autocvar_cl_velocityzoom_type) // _type = 0 disables velocity zoom too
519         {
520                 if(intermission) { curspeed = 0; }
521                 else
522                 {
523
524                         makevectors(view_angles);
525                         v = pmove_vel;
526                         if(csqcplayer)
527                                 v = csqcplayer.velocity;
528
529                         switch(autocvar_cl_velocityzoom_type)
530                         {
531                                 case 3: curspeed = max(0, v_forward * v); break;
532                                 case 2: curspeed = (v_forward * v); break;
533                                 case 1: default: curspeed = vlen(v); break;
534                         }
535                 }
536
537                 velocityzoom = bound(0, drawframetime / max(0.000000001, autocvar_cl_velocityzoom_time), 1); // speed at which the zoom adapts to player velocity
538                 avgspeed = avgspeed * (1 - velocityzoom) + (curspeed / autocvar_cl_velocityzoom_speed) * velocityzoom;
539                 velocityzoom = exp(float2range11(avgspeed * -autocvar_cl_velocityzoom_factor / 1) * 1);
540
541                 //print(ftos(avgspeed), " avgspeed, ", ftos(curspeed), " curspeed, ", ftos(velocityzoom), " return\n"); // for debugging
542         }
543         else
544                 velocityzoom = 1;
545
546         float frustumx, frustumy, fovx, fovy;
547         frustumy = tan(fov * M_PI / 360.0) * 0.75 * current_viewzoom * velocityzoom;
548         frustumx = frustumy * vid_width / vid_height / vid_pixelheight;
549         fovx = atan2(frustumx, 1) / M_PI * 360.0;
550         fovy = atan2(frustumy, 1) / M_PI * 360.0;
551
552         return '1 0 0' * fovx + '0 1 0' * fovy;
553 }
554
555 vector GetViewLocationFOV(float fov)
556 {
557         float frustumy = tan(fov * M_PI / 360.0) * 0.75;
558         float frustumx = frustumy * vid_width / vid_height / vid_pixelheight;
559         float fovx = atan2(frustumx, 1) / M_PI * 360.0;
560         float fovy = atan2(frustumy, 1) / M_PI * 360.0;
561         return '1 0 0' * fovx + '0 1 0' * fovy;
562 }
563
564 vector GetOrthoviewFOV(vector ov_worldmin, vector ov_worldmax, vector ov_mid, vector ov_org)
565 {
566         float fovx, fovy;
567         float width = (ov_worldmax.x - ov_worldmin.x);
568         float height = (ov_worldmax.y - ov_worldmin.y);
569         float distance_to_middle_of_world = vlen(ov_mid - ov_org);
570         fovx = atan2(width/2, distance_to_middle_of_world) / M_PI * 360.0;
571         fovy = atan2(height/2, distance_to_middle_of_world) / M_PI * 360.0;
572         return '1 0 0' * fovx + '0 1 0' * fovy;
573 }
574
575 // this function must match W_SetupShot!
576 float zoomscript_caught;
577
578 vector wcross_origin;
579 float wcross_scale_prev, wcross_alpha_prev;
580 vector wcross_color_prev;
581 float wcross_scale_goal_prev, wcross_alpha_goal_prev;
582 vector wcross_color_goal_prev;
583 float wcross_changedonetime;
584
585 string wcross_name_goal_prev, wcross_name_goal_prev_prev;
586 float wcross_resolution_goal_prev, wcross_resolution_goal_prev_prev;
587 float wcross_name_changestarttime, wcross_name_changedonetime;
588 float wcross_name_alpha_goal_prev, wcross_name_alpha_goal_prev_prev;
589
590 float wcross_ring_prev;
591
592 entity trueaim;
593 entity trueaim_rifle;
594
595 const float SHOTTYPE_HITTEAM = 1;
596 const float SHOTTYPE_HITOBSTRUCTION = 2;
597 const float SHOTTYPE_HITWORLD = 3;
598 const float SHOTTYPE_HITENEMY = 4;
599
600 void TrueAim_Init()
601 {
602         trueaim = new(trueaim);
603         make_pure(trueaim);
604         trueaim.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
605         trueaim_rifle = new(trueaim_rifle);
606         make_pure(trueaim_rifle);
607         trueaim_rifle.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
608 }
609
610 float EnemyHitCheck()
611 {
612         float t, n;
613         wcross_origin = project_3d_to_2d(trace_endpos);
614         wcross_origin.z = 0;
615         if(trace_ent)
616                 n = trace_ent.entnum;
617         else
618                 n = trace_networkentity;
619         if(n < 1)
620                 return SHOTTYPE_HITWORLD;
621         if(n > maxclients)
622                 return SHOTTYPE_HITWORLD;
623         t = entcs_GetTeam(n - 1);
624         if(teamplay)
625                 if(t == myteam)
626                         return SHOTTYPE_HITTEAM;
627         if(t == NUM_SPECTATOR)
628                 return SHOTTYPE_HITWORLD;
629         return SHOTTYPE_HITENEMY;
630 }
631
632 float TrueAimCheck()
633 {
634         float nudge = 1; // added to traceline target and subtracted from result TOOD(divVerent): do we still need this? Doesn't the engine do this now for us?
635         vector vecs, trueaimpoint, w_shotorg;
636         vector mi, ma, dv;
637         float shottype;
638         entity ta;
639         float mv;
640
641         mi = ma = '0 0 0';
642         ta = trueaim;
643         mv = MOVE_NOMONSTERS;
644
645         switch(activeweapon) // WEAPONTODO
646         {
647                 case WEP_TUBA: // no aim
648                 case WEP_PORTO: // shoots from eye
649                 case WEP_NEXBALL: // shoots from eye
650                 case WEP_HOOK: // no trueaim
651                 case WEP_MORTAR: // toss curve
652                         return SHOTTYPE_HITWORLD;
653                 case WEP_VORTEX:
654                 case WEP_VAPORIZER:
655                         mv = MOVE_NORMAL;
656                         break;
657                 case WEP_RIFLE:
658                         ta = trueaim_rifle;
659                         mv = MOVE_NORMAL;
660                         if(zoomscript_caught)
661                         {
662                                 tracebox(view_origin, '0 0 0', '0 0 0', view_origin + view_forward * MAX_SHOT_DISTANCE, mv, ta);
663                                 return EnemyHitCheck();
664                         }
665                         break;
666                 case WEP_DEVASTATOR: // projectile has a size!
667                         mi = '-3 -3 -3';
668                         ma = '3 3 3';
669                         break;
670                 case WEP_FIREBALL: // projectile has a size!
671                         mi = '-16 -16 -16';
672                         ma = '16 16 16';
673                         break;
674                 case WEP_SEEKER: // projectile has a size!
675                         mi = '-2 -2 -2';
676                         ma = '2 2 2';
677                         break;
678                 case WEP_ELECTRO: // projectile has a size!
679                         mi = '0 0 -3';
680                         ma = '0 0 -3';
681                         break;
682         }
683
684         vector traceorigin = entcs_receiver(player_localentnum - 1).origin + (eZ * STAT(VIEWHEIGHT));
685
686         vecs = decompressShotOrigin(STAT(SHOTORG));
687
688         traceline(traceorigin, traceorigin + view_forward * MAX_SHOT_DISTANCE, mv, ta);
689         trueaimpoint = trace_endpos;
690
691         if(vdist((trueaimpoint - traceorigin), <, g_trueaim_minrange))
692                 trueaimpoint = traceorigin + view_forward * g_trueaim_minrange;
693
694         if(vecs.x > 0)
695                 vecs.y = -vecs.y;
696         else
697                 vecs = '0 0 0';
698
699         dv = view_right * vecs.y + view_up * vecs.z;
700         w_shotorg = traceorigin + dv;
701
702         // now move the vecs forward as much as requested if possible
703         tracebox(w_shotorg, mi, ma, w_shotorg + view_forward * (vecs.x + nudge), MOVE_NORMAL, ta); // FIXME this MOVE_NORMAL part will misbehave a little in csqc
704         w_shotorg = trace_endpos - view_forward * nudge;
705
706         tracebox(w_shotorg, mi, ma, trueaimpoint, MOVE_NORMAL, ta);
707         shottype = EnemyHitCheck();
708         if(shottype != SHOTTYPE_HITWORLD)
709                 return shottype;
710
711 #if 0
712         // FIXME WHY DOES THIS NOT WORK FOR THE ROCKET LAUNCHER?
713         // or rather, I know why, but see no fix
714         if(vlen(trace_endpos - trueaimpoint) > vlen(ma) + vlen(mi) + 1)
715                 // yes, this is an ugly hack... but it seems good enough to find out whether the test hits the same place as the initial trace
716                 return SHOTTYPE_HITOBSTRUCTION;
717 #endif
718
719         return SHOTTYPE_HITWORLD;
720 }
721
722 void PostInit();
723 void CSQC_Demo_Camera();
724 float HUD_WouldDrawScoreboard();
725 float camera_mode;
726 const float CAMERA_FREE = 1;
727 const float CAMERA_CHASE = 2;
728 float reticle_type;
729 string reticle_image;
730 string NextFrameCommand;
731
732 vector freeze_org, freeze_ang;
733 entity nightvision_noise, nightvision_noise2;
734
735 const float MAX_TIME_DIFF = 5;
736 float pickup_crosshair_time, pickup_crosshair_size;
737 float hitindication_crosshair_size;
738 float use_vortex_chargepool;
739
740 float myhealth, myhealth_prev;
741 float myhealth_flash;
742
743 float old_blurradius, old_bluralpha;
744 float old_sharpen_intensity;
745
746 vector myhealth_gentlergb;
747
748 float contentavgalpha, liquidalpha_prev;
749 vector liquidcolor_prev;
750
751 float eventchase_current_distance;
752 float eventchase_running;
753 bool WantEventchase(entity this)
754 {
755         if(autocvar_cl_orthoview)
756                 return false;
757         if(intermission)
758                 return true;
759         if(this.viewloc)
760                 return true;
761         if(spectatee_status >= 0)
762         {
763                 if(hud != HUD_NORMAL && (autocvar_cl_eventchase_vehicle || spectatee_status > 0))
764                         return true;
765                 if(MUTATOR_CALLHOOK(WantEventchase, this))
766                         return true;
767                 if(autocvar_cl_eventchase_nexball && gametype == MAPINFO_TYPE_NEXBALL && !(WepSet_GetFromStat() & WEPSET(NEXBALL)))
768                         return true;
769                 if(autocvar_cl_eventchase_death && (STAT(HEALTH) <= 0))
770                 {
771                         if(autocvar_cl_eventchase_death == 2)
772                         {
773                                 // don't stop eventchase once it's started (even if velocity changes afterwards)
774                                 if(this.velocity == '0 0 0' || eventchase_running)
775                                         return true;
776                         }
777                         else return true;
778                 }
779         }
780         return false;
781 }
782
783 void HUD_Crosshair_Vehicle()
784 {
785         if(hud != HUD_BUMBLEBEE_GUN)
786         {
787                 Vehicle info = Vehicles_from(hud);
788                 info.vr_crosshair(info);
789         }
790 }
791
792 vector damage_blurpostprocess, content_blurpostprocess;
793
794 float unaccounted_damage = 0;
795 void UpdateDamage()
796 {
797         // accumulate damage with each stat update
798         static float damage_total_prev = 0;
799         float damage_total = STAT(DAMAGE_DEALT_TOTAL);
800         float unaccounted_damage_new = COMPARE_INCREASING(damage_total, damage_total_prev);
801         damage_total_prev = damage_total;
802
803         static float damage_dealt_time_prev = 0;
804         float damage_dealt_time = STAT(HIT_TIME);
805         if (damage_dealt_time != damage_dealt_time_prev)
806         {
807                 unaccounted_damage += unaccounted_damage_new;
808                 LOG_TRACE("dmg total: ", ftos(unaccounted_damage), " (+", ftos(unaccounted_damage_new), ")", "\n");
809         }
810         damage_dealt_time_prev = damage_dealt_time;
811
812         // prevent hitsound when switching spectatee
813         static float spectatee_status_prev = 0;
814         if (spectatee_status != spectatee_status_prev)
815                 unaccounted_damage = 0;
816         spectatee_status_prev = spectatee_status;
817 }
818
819 void HitSound()
820 {
821         // varying sound pitch
822
823         static float hitsound_time_prev = 0;
824         // HACK: the only way to get the arc to sound consistent with pitch shift is to ignore cl_hitsound_antispam_time
825         float arc_hack = activeweapon == WEP_ARC && autocvar_cl_hitsound >= 2;
826         if (arc_hack || COMPARE_INCREASING(time, hitsound_time_prev) > autocvar_cl_hitsound_antispam_time)
827         {
828                 if (autocvar_cl_hitsound && unaccounted_damage)
829                 {
830                         // customizable gradient function that crosses (0,a), (c,1) and asymptotically approaches b
831                         float a = autocvar_cl_hitsound_max_pitch;
832                         float b = autocvar_cl_hitsound_min_pitch;
833                         float c = autocvar_cl_hitsound_nom_damage;
834                         float x = unaccounted_damage;
835                         float pitch_shift = (b*x*(a-1) + a*c*(1-b)) / (x*(a-1) + c*(1-b));
836
837                         // if sound variation is disabled, set pitch_shift to 1
838                         if (autocvar_cl_hitsound == 1)
839                                 pitch_shift = 1;
840
841                         // if pitch shift is reversed, mirror in (max-min)/2 + min
842                         if (autocvar_cl_hitsound == 3)
843                         {
844                                 float mirror_value = (a-b)/2 + b;
845                                 pitch_shift = mirror_value + (mirror_value - pitch_shift);
846                         }
847
848                         LOG_TRACE("dmg total (dmg): ", ftos(unaccounted_damage), " , pitch shift: ", ftos(pitch_shift), "\n");
849
850                         // todo: avoid very long and very short sounds from wave stretching using different sound files? seems unnecessary
851                         // todo: normalize sound pressure levels? seems unnecessary
852
853                         sound7(world, CH_INFO, SND(HIT), VOL_BASE, ATTN_NONE, pitch_shift * 100, 0);
854                 }
855                 unaccounted_damage = 0;
856                 hitsound_time_prev = time;
857         }
858
859         static float typehit_time_prev = 0;
860         float typehit_time = STAT(TYPEHIT_TIME);
861         if (COMPARE_INCREASING(typehit_time, typehit_time_prev) > autocvar_cl_hitsound_antispam_time)
862         {
863                 sound(world, CH_INFO, SND_TYPEHIT, VOL_BASE, ATTN_NONE);
864                 typehit_time_prev = typehit_time;
865         }
866 }
867
868 void HUD_Crosshair()
869 {SELFPARAM();
870         static float rainbow_last_flicker;
871         static vector rainbow_prev_color;
872         entity e = this;
873         float f, i, j;
874         vector v;
875         if(!scoreboard_active && !camera_active && intermission != 2 &&
876                 spectatee_status != -1 && !csqcplayer.viewloc &&
877                 !HUD_MinigameMenu_IsOpened() )
878         {
879                 if (!autocvar_crosshair_enabled) // main toggle for crosshair rendering
880                         return;
881
882                 if (hud != HUD_NORMAL)
883                 {
884                         HUD_Crosshair_Vehicle();
885                         return;
886                 }
887
888                 string wcross_style;
889                 float wcross_alpha, wcross_resolution;
890                 wcross_style = autocvar_crosshair;
891                 if (wcross_style == "0")
892                         return;
893                 wcross_resolution = autocvar_crosshair_size;
894                 if (wcross_resolution == 0)
895                         return;
896                 wcross_alpha = autocvar_crosshair_alpha;
897                 if (wcross_alpha == 0)
898                         return;
899
900                 // TrueAim check
901                 float shottype;
902
903                 // wcross_origin = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
904                 wcross_origin = project_3d_to_2d(view_origin + MAX_SHOT_DISTANCE * view_forward);
905                 wcross_origin.z = 0;
906                 if(autocvar_crosshair_hittest)
907                 {
908                         vector wcross_oldorigin;
909                         wcross_oldorigin = wcross_origin;
910                         shottype = TrueAimCheck();
911                         if(shottype == SHOTTYPE_HITWORLD)
912                         {
913                                 v = wcross_origin - wcross_oldorigin;
914                                 v.x /= vid_conwidth;
915                                 v.y /= vid_conheight;
916                                 if(vlen(v) > 0.01)
917                                         shottype = SHOTTYPE_HITOBSTRUCTION;
918                         }
919                         if(!autocvar_crosshair_hittest_showimpact)
920                                 wcross_origin = wcross_oldorigin;
921                 }
922                 else
923                         shottype = SHOTTYPE_HITWORLD;
924
925                 vector wcross_color = '0 0 0', wcross_size = '0 0 0';
926                 string wcross_name = "";
927                 float wcross_scale, wcross_blur;
928
929                 if(autocvar_crosshair_per_weapon || (autocvar_crosshair_color_special == 1))
930                 {
931                         e = switchingweapon;
932                         if(e)
933                         {
934                                 if(autocvar_crosshair_per_weapon)
935                                 {
936                                         // WEAPONTODO: access these through some general settings (with non-balance config settings)
937                                         //wcross_resolution *= cvar(strcat("crosshair_", wcross_wep, "_size"));
938                                         //if (wcross_resolution == 0)
939                                                 //return;
940
941                                         //wcross_style = cvar_string(strcat("crosshair_", wcross_wep));
942                                         wcross_resolution *= e.w_crosshair_size;
943                                         wcross_name = e.w_crosshair;
944                                 }
945                         }
946                 }
947
948                 if(wcross_name == "")
949                         wcross_name = strcat("gfx/crosshair", wcross_style);
950
951                 // MAIN CROSSHAIR COLOR DECISION
952                 switch(autocvar_crosshair_color_special)
953                 {
954                         case 1: // crosshair_color_per_weapon
955                         {
956                                 if(e)
957                                 {
958                                         wcross_color = e.wpcolor;
959                                         break;
960                                 }
961                                 else { goto normalcolor; }
962                         }
963
964                         case 2: // crosshair_color_by_health
965                         {
966                                 float x = STAT(HEALTH);
967
968                                 //x = red
969                                 //y = green
970                                 //z = blue
971
972                                 wcross_color.z = 0;
973
974                                 if(x > 200)
975                                 {
976                                         wcross_color.x = 0;
977                                         wcross_color.y = 1;
978                                 }
979                                 else if(x > 150)
980                                 {
981                                         wcross_color.x = 0.4 - (x-150)*0.02 * 0.4;
982                                         wcross_color.y = 0.9 + (x-150)*0.02 * 0.1;
983                                 }
984                                 else if(x > 100)
985                                 {
986                                         wcross_color.x = 1 - (x-100)*0.02 * 0.6;
987                                         wcross_color.y = 1 - (x-100)*0.02 * 0.1;
988                                         wcross_color.z = 1 - (x-100)*0.02;
989                                 }
990                                 else if(x > 50)
991                                 {
992                                         wcross_color.x = 1;
993                                         wcross_color.y = 1;
994                                         wcross_color.z = 0.2 + (x-50)*0.02 * 0.8;
995                                 }
996                                 else if(x > 20)
997                                 {
998                                         wcross_color.x = 1;
999                                         wcross_color.y = (x-20)*90/27/100;
1000                                         wcross_color.z = (x-20)*90/27/100 * 0.2;
1001                                 }
1002                                 else
1003                                 {
1004                                         wcross_color.x = 1;
1005                                         wcross_color.y = 0;
1006                                 }
1007                                 break;
1008                         }
1009
1010                         case 3: // crosshair_color_rainbow
1011                         {
1012                                 if(time >= rainbow_last_flicker)
1013                                 {
1014                                         rainbow_prev_color = randomvec() * autocvar_crosshair_color_special_rainbow_brightness;
1015                                         rainbow_last_flicker = time + autocvar_crosshair_color_special_rainbow_delay;
1016                                 }
1017                                 wcross_color = rainbow_prev_color;
1018                                 break;
1019                         }
1020                         :normalcolor
1021                         default: { wcross_color = stov(autocvar_crosshair_color); break; }
1022                 }
1023
1024                 if(autocvar_crosshair_effect_scalefade)
1025                 {
1026                         wcross_scale = wcross_resolution;
1027                         wcross_resolution = 1;
1028                 }
1029                 else
1030                 {
1031                         wcross_scale = 1;
1032                 }
1033
1034                 if(autocvar_crosshair_pickup)
1035                 {
1036                         float stat_pickup_time = STAT(LAST_PICKUP);
1037
1038                         if(pickup_crosshair_time < stat_pickup_time)
1039                         {
1040                                 if(time - stat_pickup_time < MAX_TIME_DIFF) // don't trigger the animation if it's too old
1041                                         pickup_crosshair_size = 1;
1042
1043                                 pickup_crosshair_time = stat_pickup_time;
1044                         }
1045
1046                         if(pickup_crosshair_size > 0)
1047                                 pickup_crosshair_size -= autocvar_crosshair_pickup_speed * frametime;
1048                         else
1049                                 pickup_crosshair_size = 0;
1050
1051                         wcross_scale += sin(pickup_crosshair_size) * autocvar_crosshair_pickup;
1052                 }
1053
1054                 // todo: make crosshair hit indication dependent on damage dealt
1055                 if(autocvar_crosshair_hitindication)
1056                 {
1057                         vector hitindication_color = ((autocvar_crosshair_color_special == 1) ? stov(autocvar_crosshair_hitindication_per_weapon_color) : stov(autocvar_crosshair_hitindication_color));
1058
1059                         if(unaccounted_damage)
1060                         {
1061                                 hitindication_crosshair_size = 1;
1062                         }
1063
1064                         if(hitindication_crosshair_size > 0)
1065                                 hitindication_crosshair_size -= autocvar_crosshair_hitindication_speed * frametime;
1066                         else
1067                                 hitindication_crosshair_size = 0;
1068
1069                         wcross_scale += sin(hitindication_crosshair_size) * autocvar_crosshair_hitindication;
1070                         wcross_color.x += sin(hitindication_crosshair_size) * hitindication_color.x;
1071                         wcross_color.y += sin(hitindication_crosshair_size) * hitindication_color.y;
1072                         wcross_color.z += sin(hitindication_crosshair_size) * hitindication_color.z;
1073                 }
1074
1075                 if(shottype == SHOTTYPE_HITENEMY)
1076                         wcross_scale *= autocvar_crosshair_hittest; // is not queried if hittest is 0
1077                 if(shottype == SHOTTYPE_HITTEAM)
1078                         wcross_scale /= autocvar_crosshair_hittest; // is not queried if hittest is 0
1079
1080                 f = fabs(autocvar_crosshair_effect_time);
1081                 if(wcross_scale != wcross_scale_goal_prev || wcross_alpha != wcross_alpha_goal_prev || wcross_color != wcross_color_goal_prev)
1082                 {
1083                         wcross_changedonetime = time + f;
1084                 }
1085                 if(wcross_name != wcross_name_goal_prev || wcross_resolution != wcross_resolution_goal_prev)
1086                 {
1087                         wcross_name_changestarttime = time;
1088                         wcross_name_changedonetime = time + f;
1089                         if(wcross_name_goal_prev_prev)
1090                                 strunzone(wcross_name_goal_prev_prev);
1091                         wcross_name_goal_prev_prev = wcross_name_goal_prev;
1092                         wcross_name_goal_prev = strzone(wcross_name);
1093                         wcross_name_alpha_goal_prev_prev = wcross_name_alpha_goal_prev;
1094                         wcross_resolution_goal_prev_prev = wcross_resolution_goal_prev;
1095                         wcross_resolution_goal_prev = wcross_resolution;
1096                 }
1097
1098                 wcross_scale_goal_prev = wcross_scale;
1099                 wcross_alpha_goal_prev = wcross_alpha;
1100                 wcross_color_goal_prev = wcross_color;
1101
1102                 if(spectatee_status == -1 && shottype == SHOTTYPE_HITTEAM || (shottype == SHOTTYPE_HITOBSTRUCTION && autocvar_crosshair_hittest_blur && !autocvar_chase_active))
1103                 {
1104                         wcross_blur = 1;
1105                         wcross_alpha *= 0.75;
1106                 }
1107                 else
1108                         wcross_blur = 0;
1109                 // *_prev is at time-frametime
1110                 // * is at wcross_changedonetime+f
1111                 // what do we have at time?
1112                 if(time < wcross_changedonetime)
1113                 {
1114                         f = frametime / (wcross_changedonetime - time + frametime);
1115                         wcross_scale = f * wcross_scale + (1 - f) * wcross_scale_prev;
1116                         wcross_alpha = f * wcross_alpha + (1 - f) * wcross_alpha_prev;
1117                         wcross_color = f * wcross_color + (1 - f) * wcross_color_prev;
1118                 }
1119
1120                 wcross_scale_prev = wcross_scale;
1121                 wcross_alpha_prev = wcross_alpha;
1122                 wcross_color_prev = wcross_color;
1123
1124                 MUTATOR_CALLHOOK(UpdateCrosshair);
1125
1126                 wcross_scale *= 1 - autocvar__menu_alpha;
1127                 wcross_alpha *= 1 - autocvar__menu_alpha;
1128                 wcross_size = draw_getimagesize(wcross_name) * wcross_scale;
1129
1130                 if(wcross_scale >= 0.001 && wcross_alpha >= 0.001)
1131                 {
1132                         // crosshair rings for weapon stats
1133                         if (autocvar_crosshair_ring || autocvar_crosshair_ring_reload)
1134                         {
1135                                 // declarations and stats
1136                                 float ring_value = 0, ring_scale = 0, ring_alpha = 0, ring_inner_value = 0, ring_inner_alpha = 0;
1137                                 string ring_image = string_null, ring_inner_image = string_null;
1138                                 vector ring_rgb = '0 0 0', ring_inner_rgb = '0 0 0';
1139
1140                                 ring_scale = autocvar_crosshair_ring_size;
1141
1142                                 float weapon_clipload, weapon_clipsize;
1143                                 weapon_clipload = STAT(WEAPON_CLIPLOAD);
1144                                 weapon_clipsize = STAT(WEAPON_CLIPSIZE);
1145
1146                                 float ok_ammo_charge, ok_ammo_chargepool;
1147                                 ok_ammo_charge = STAT(OK_AMMO_CHARGE);
1148                                 ok_ammo_chargepool = STAT(OK_AMMO_CHARGEPOOL);
1149
1150                                 float vortex_charge, vortex_chargepool;
1151                                 vortex_charge = STAT(VORTEX_CHARGE);
1152                                 vortex_chargepool = STAT(VORTEX_CHARGEPOOL);
1153
1154                                 float arc_heat = STAT(ARC_HEAT);
1155
1156                                 if(vortex_charge_movingavg == 0) // this should only happen if we have just loaded up the game
1157                                         vortex_charge_movingavg = vortex_charge;
1158
1159
1160                                 // handle the values
1161                                 if (autocvar_crosshair_ring && activeweapon == WEP_VORTEX && vortex_charge && autocvar_crosshair_ring_vortex) // ring around crosshair representing velocity-dependent damage for the vortex
1162                                 {
1163                                         if (vortex_chargepool || use_vortex_chargepool) {
1164                                                 use_vortex_chargepool = 1;
1165                                                 ring_inner_value = vortex_chargepool;
1166                                         } else {
1167                                                 vortex_charge_movingavg = (1 - autocvar_crosshair_ring_vortex_currentcharge_movingavg_rate) * vortex_charge_movingavg + autocvar_crosshair_ring_vortex_currentcharge_movingavg_rate * vortex_charge;
1168                                                 ring_inner_value = bound(0, autocvar_crosshair_ring_vortex_currentcharge_scale * (vortex_charge - vortex_charge_movingavg), 1);
1169                                         }
1170
1171                                         ring_inner_alpha = autocvar_crosshair_ring_vortex_inner_alpha;
1172                                         ring_inner_rgb = eX * autocvar_crosshair_ring_vortex_inner_color_red + eY * autocvar_crosshair_ring_vortex_inner_color_green + eZ * autocvar_crosshair_ring_vortex_inner_color_blue;
1173                                         ring_inner_image = "gfx/crosshair_ring_inner.tga";
1174
1175                                         // draw the outer ring to show the current charge of the weapon
1176                                         ring_value = vortex_charge;
1177                                         ring_alpha = autocvar_crosshair_ring_vortex_alpha;
1178                                         ring_rgb = wcross_color;
1179                                         ring_image = "gfx/crosshair_ring_nexgun.tga";
1180                                 }
1181                                 else if (autocvar_crosshair_ring && activeweapon == WEP_MINE_LAYER && WEP_CVAR(minelayer, limit) && autocvar_crosshair_ring_minelayer)
1182                                 {
1183                                         ring_value = bound(0, STAT(LAYED_MINES) / WEP_CVAR(minelayer, limit), 1); // if you later need to use the count of bullets in another place, then add a float for it. For now, no need to.
1184                                         ring_alpha = autocvar_crosshair_ring_minelayer_alpha;
1185                                         ring_rgb = wcross_color;
1186                                         ring_image = "gfx/crosshair_ring.tga";
1187                                 }
1188                                 else if (activeweapon == WEP_HAGAR && STAT(HAGAR_LOAD) && autocvar_crosshair_ring_hagar)
1189                                 {
1190                                         ring_value = bound(0, STAT(HAGAR_LOAD) / WEP_CVAR_SEC(hagar, load_max), 1);
1191                                         ring_alpha = autocvar_crosshair_ring_hagar_alpha;
1192                                         ring_rgb = wcross_color;
1193                                         ring_image = "gfx/crosshair_ring.tga";
1194                                 }
1195                                 else if (ok_ammo_charge)
1196                                 {
1197                                         ring_value = ok_ammo_chargepool;
1198                                         ring_alpha = autocvar_crosshair_ring_reload_alpha;
1199                                         ring_rgb = wcross_color;
1200                                         ring_image = "gfx/crosshair_ring.tga";
1201                                 }
1202                                 else if(autocvar_crosshair_ring_reload && weapon_clipsize) // forces there to be only an ammo ring
1203                                 {
1204                                         ring_value = bound(0, weapon_clipload / weapon_clipsize, 1);
1205                                         ring_scale = autocvar_crosshair_ring_reload_size;
1206                                         ring_alpha = autocvar_crosshair_ring_reload_alpha;
1207                                         ring_rgb = wcross_color;
1208
1209                                         // Note: This is to stop Taoki from complaining that the image doesn't match all potential balances.
1210                                         // if a new image for another weapon is added, add the code (and its respective file/value) here
1211                                         if ((activeweapon == WEP_RIFLE) && (weapon_clipsize == 80))
1212                                                 ring_image = "gfx/crosshair_ring_rifle.tga";
1213                                         else
1214                                                 ring_image = "gfx/crosshair_ring.tga";
1215                                 }
1216                                 else if ( autocvar_crosshair_ring && autocvar_crosshair_ring_arc && arc_heat && activeweapon == WEP_ARC )
1217                                 {
1218                                         ring_value = arc_heat;
1219                                         ring_alpha = (1-arc_heat)*autocvar_crosshair_ring_arc_cold_alpha +
1220                                                 arc_heat*autocvar_crosshair_ring_arc_hot_alpha;
1221                                         ring_rgb = (1-arc_heat)*wcross_color + arc_heat*autocvar_crosshair_ring_arc_hot_color;
1222                                         ring_image = "gfx/crosshair_ring.tga";
1223                                 }
1224
1225                                 // if in weapon switch animation, fade ring out/in
1226                                 if(autocvar_crosshair_effect_time > 0)
1227                                 {
1228                                         f = (time - wcross_name_changestarttime) / autocvar_crosshair_effect_time;
1229                                         if (f >= 1)
1230                                         {
1231                                                 wcross_ring_prev = ((ring_image) ? true : false);
1232                                         }
1233
1234                                         if(wcross_ring_prev)
1235                                         {
1236                                                 if(f < 1)
1237                                                         ring_alpha *= fabs(1 - bound(0, f, 1));
1238                                         }
1239                                         else
1240                                         {
1241                                                 if(f < 1)
1242                                                         ring_alpha *= bound(0, f, 1);
1243                                         }
1244                                 }
1245
1246                                 if (autocvar_crosshair_ring_inner && ring_inner_value) // lets draw a ring inside a ring so you can ring while you ring
1247                                         DrawCircleClippedPic(wcross_origin, wcross_size.x * ring_scale, ring_inner_image, ring_inner_value, ring_inner_rgb, wcross_alpha * ring_inner_alpha, DRAWFLAG_ADDITIVE);
1248
1249                                 if (ring_value)
1250                                         DrawCircleClippedPic(wcross_origin, wcross_size.x * ring_scale, ring_image, ring_value, ring_rgb, wcross_alpha * ring_alpha, DRAWFLAG_ADDITIVE);
1251                         }
1252
1253 #define CROSSHAIR_DO_BLUR(M,sz,wcross_name,wcross_alpha) \
1254                         MACRO_BEGIN { \
1255                                 if(wcross_blur > 0) \
1256                                 { \
1257                                         for(i = -2; i <= 2; ++i) \
1258                                         for(j = -2; j <= 2; ++j) \
1259                                         M(i,j,sz,wcross_name,wcross_alpha*0.04); \
1260                                 } \
1261                                 else \
1262                                 { \
1263                                         M(0,0,sz,wcross_name,wcross_alpha); \
1264                                 } \
1265                         } MACRO_END
1266
1267 #define CROSSHAIR_DRAW_SINGLE(i,j,sz,wcross_name,wcross_alpha) \
1268                         drawpic(wcross_origin - ('0.5 0 0' * (sz * wcross_size.x + i * wcross_blur) + '0 0.5 0' * (sz * wcross_size.y + j * wcross_blur)), wcross_name, sz * wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL)
1269
1270 #define CROSSHAIR_DRAW(sz,wcross_name,wcross_alpha) \
1271                         CROSSHAIR_DO_BLUR(CROSSHAIR_DRAW_SINGLE,sz,wcross_name,wcross_alpha)
1272
1273                         if(time < wcross_name_changedonetime && wcross_name != wcross_name_goal_prev_prev && wcross_name_goal_prev_prev)
1274                         {
1275                                 f = (wcross_name_changedonetime - time) / (wcross_name_changedonetime - wcross_name_changestarttime);
1276                                 wcross_size = draw_getimagesize(wcross_name_goal_prev_prev) * wcross_scale;
1277                                 CROSSHAIR_DRAW(wcross_resolution_goal_prev_prev, wcross_name_goal_prev_prev, wcross_alpha * f * wcross_name_alpha_goal_prev_prev);
1278                                 f = 1 - f;
1279                         }
1280                         else
1281                         {
1282                                 f = 1;
1283                         }
1284                         wcross_name_alpha_goal_prev = f;
1285
1286                         wcross_size = draw_getimagesize(wcross_name) * wcross_scale;
1287                         CROSSHAIR_DRAW(wcross_resolution, wcross_name, wcross_alpha * f);
1288
1289                         if(autocvar_crosshair_dot)
1290                         {
1291                                 vector wcross_color_old;
1292                                 wcross_color_old = wcross_color;
1293
1294                                 if((autocvar_crosshair_dot_color_custom) && (autocvar_crosshair_dot_color != "0"))
1295                                         wcross_color = stov(autocvar_crosshair_dot_color);
1296
1297                                 CROSSHAIR_DRAW(wcross_resolution * autocvar_crosshair_dot_size, "gfx/crosshairdot.tga", f * autocvar_crosshair_dot_alpha);
1298                                 // FIXME why don't we use wcross_alpha here?
1299                                 wcross_color = wcross_color_old;
1300                         }
1301                 }
1302         }
1303         else
1304         {
1305                 wcross_scale_prev = 0;
1306                 wcross_alpha_prev = 0;
1307                 wcross_scale_goal_prev = 0;
1308                 wcross_alpha_goal_prev = 0;
1309                 wcross_changedonetime = 0;
1310                 if(wcross_name_goal_prev)
1311                         strunzone(wcross_name_goal_prev);
1312                 wcross_name_goal_prev = string_null;
1313                 if(wcross_name_goal_prev_prev)
1314                         strunzone(wcross_name_goal_prev_prev);
1315                 wcross_name_goal_prev_prev = string_null;
1316                 wcross_name_changestarttime = 0;
1317                 wcross_name_changedonetime = 0;
1318                 wcross_name_alpha_goal_prev = 0;
1319                 wcross_name_alpha_goal_prev_prev = 0;
1320                 wcross_resolution_goal_prev = 0;
1321                 wcross_resolution_goal_prev_prev = 0;
1322         }
1323 }
1324
1325 void HUD_Draw()
1326 {
1327         vector rgb = '0 0 0';
1328         float a = 1;
1329         if (MUTATOR_CALLHOOK(HUD_Draw_overlay))
1330         {
1331                 rgb = MUTATOR_ARGV(0, vector);
1332                 a = MUTATOR_ARGV(0, float);
1333         }
1334         else if(STAT(FROZEN))
1335         {
1336                 rgb = ((STAT(REVIVE_PROGRESS)) ? ('0.25 0.90 1' + ('1 0 0' * STAT(REVIVE_PROGRESS)) + ('0 1 1' * STAT(REVIVE_PROGRESS) * -1)) : '0.25 0.90 1');
1337         }
1338         drawfill('0 0 0', eX * vid_conwidth + eY * vid_conheight, rgb, autocvar_hud_colorflash_alpha * a, DRAWFLAG_ADDITIVE);
1339         if(!intermission)
1340         if(STAT(NADE_TIMER) && autocvar_cl_nade_timer) // give nade top priority, as it's a matter of life and death
1341         {
1342                 DrawCircleClippedPic(eX * 0.5 * vid_conwidth + eY * 0.6 * vid_conheight, 0.1 * vid_conheight, "gfx/crosshair_ring.tga", STAT(NADE_TIMER), '0.25 0.90 1' + ('1 0 0' * STAT(NADE_TIMER)) - ('0 1 1' * STAT(NADE_TIMER)), autocvar_hud_colorflash_alpha, DRAWFLAG_ADDITIVE);
1343                 drawstring_aspect(eY * 0.64 * vid_conheight, ((autocvar_cl_nade_timer == 2) ? _("Nade timer") : ""), eX * vid_conwidth + eY * 0.025 * vid_conheight, '1 1 1', 1, DRAWFLAG_NORMAL);
1344         }
1345         else if(STAT(REVIVE_PROGRESS))
1346         {
1347                 DrawCircleClippedPic(eX * 0.5 * vid_conwidth + eY * 0.6 * vid_conheight, 0.1 * vid_conheight, "gfx/crosshair_ring.tga", STAT(REVIVE_PROGRESS), '0.25 0.90 1', autocvar_hud_colorflash_alpha, DRAWFLAG_ADDITIVE);
1348                 drawstring_aspect(eY * 0.64 * vid_conheight, _("Revival progress"), eX * vid_conwidth + eY * 0.025 * vid_conheight, '1 1 1', 1, DRAWFLAG_NORMAL);
1349         }
1350
1351         if(autocvar_r_letterbox == 0)
1352                 if(autocvar_viewsize < 120)
1353                 {
1354                         if(!(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS))
1355                                 Accuracy_LoadLevels();
1356
1357                         HUD_Main();
1358                         HUD_DrawScoreboard();
1359                 }
1360
1361         // crosshair goes VERY LAST
1362         UpdateDamage();
1363         HUD_Crosshair();
1364         HitSound();
1365 }
1366
1367 bool ov_enabled;
1368 float oldr_nearclip;
1369 float oldr_farclip_base;
1370 float oldr_farclip_world;
1371 float oldr_novis;
1372 float oldr_useportalculling;
1373 float oldr_useinfinitefarclip;
1374
1375 void cl_notice_run();
1376 float prev_myteam;
1377 int lasthud;
1378 float vh_notice_time;
1379 void WaypointSprite_Load();
1380 void CSQC_UpdateView(float w, float h)
1381 {SELFPARAM();
1382         entity e;
1383         float fov;
1384         float f;
1385         vector vf_size, vf_min;
1386         float a;
1387
1388         execute_next_frame();
1389
1390         ++framecount;
1391
1392         stats_get();
1393         hud = STAT(HUD);
1394
1395         if(hud != HUD_NORMAL && lasthud == HUD_NORMAL)
1396                 vh_notice_time = time + autocvar_cl_vehicles_notify_time;
1397
1398         lasthud = hud;
1399
1400         if(autocvar__hud_showbinds_reload) // menu can set this one
1401         {
1402                 db_close(binddb);
1403                 binddb = db_create();
1404                 cvar_set("_hud_showbinds_reload", "0");
1405         }
1406
1407         if(checkextension("DP_CSQC_MINFPS_QUALITY"))
1408                 view_quality = getproperty(VF_MINFPS_QUALITY);
1409         else
1410                 view_quality = 1;
1411
1412         button_attack2 = PHYS_INPUT_BUTTON_ATCK2(this);
1413         button_zoom = PHYS_INPUT_BUTTON_ZOOM(this);
1414
1415         vf_size = getpropertyvec(VF_SIZE);
1416         vf_min = getpropertyvec(VF_MIN);
1417         vid_width = vf_size.x;
1418         vid_height = vf_size.y;
1419
1420         vector reticle_pos = '0 0 0', reticle_size = '0 0 0';
1421         vector splash_pos = '0 0 0', splash_size = '0 0 0';
1422
1423         WaypointSprite_Load();
1424
1425         CSQCPlayer_SetCamera();
1426
1427         if(player_localentnum <= maxclients) // is it a client?
1428                 current_player = player_localentnum - 1;
1429         else // then player_localentnum is the vehicle I'm driving
1430                 current_player = player_localnum;
1431         myteam = entcs_GetTeam(current_player);
1432
1433         if(myteam != prev_myteam)
1434         {
1435                 myteamcolors = colormapPaletteColor(myteam, 1);
1436                 FOREACH(hud_panels, true, LAMBDA(it.update_time = time));
1437                 prev_myteam = myteam;
1438         }
1439
1440         ticrate = STAT(MOVEVARS_TICRATE) * STAT(MOVEVARS_TIMESCALE);
1441
1442         float is_dead = (STAT(HEALTH) <= 0);
1443
1444         // FIXME do we need this hack?
1445         if(isdemo())
1446         {
1447                 // in demos, input_buttons do not work
1448                 button_zoom = (autocvar__togglezoom == "-");
1449         }
1450         else if(button_zoom
1451                 && autocvar_cl_unpress_zoom_on_death
1452                 && (spectatee_status >= 0)
1453                 && (is_dead || intermission))
1454         {
1455                 // no zoom while dead or in intermission please
1456                 localcmd("-zoom\n");
1457                 button_zoom = false;
1458         }
1459
1460         // event chase camera
1461         if(autocvar_chase_active <= 0) // greater than 0 means it's enabled manually, and this code is skipped
1462         {
1463                 float vehicle_chase = (hud != HUD_NORMAL && (autocvar_cl_eventchase_vehicle || spectatee_status > 0));
1464                 float ons_roundlost = (gametype == MAPINFO_TYPE_ONSLAUGHT && STAT(ROUNDLOST));
1465                 entity gen = world;
1466
1467                 float vehicle_viewdist = 0;
1468                 vector vehicle_viewofs = '0 0 0';
1469
1470                 if(vehicle_chase)
1471                 {
1472                         if(hud != HUD_BUMBLEBEE_GUN)
1473                         {
1474                                 Vehicle info = Vehicles_from(hud);
1475                                 vehicle_viewdist = info.height;
1476                                 vehicle_viewofs = info.view_ofs;
1477                         }
1478                 }
1479
1480                 if(ons_roundlost)
1481                 {
1482                         FOREACH_ENTITY_CLASS("onslaught_generator", it.health <= 0, LAMBDA(
1483                                 gen = it;
1484                                 break;
1485                         ));
1486                         if(!gen)
1487                                 ons_roundlost = false; // don't enforce the 3rd person camera if there is no dead generator to show
1488                 }
1489                 if(WantEventchase(self) || (!autocvar_cl_orthoview && ons_roundlost))
1490                 {
1491                         eventchase_running = true;
1492
1493                         entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1));
1494                         if(!local_player)
1495                                 local_player = self; // fall back!
1496
1497                         // make special vector since we can't use view_origin (It is one frame old as of this code, it gets set later with the results this code makes.)
1498                         vector current_view_origin = (csqcplayer ? csqcplayer.origin : pmove_org);
1499                         if(ons_roundlost) { current_view_origin = gen.origin; }
1500
1501                         // detect maximum viewoffset and use it
1502                         vector view_offset = autocvar_cl_eventchase_viewoffset;
1503                         if(vehicle_chase)
1504                         {
1505                                 if(vehicle_viewofs)
1506                                         view_offset = vehicle_viewofs;
1507                                 else
1508                                         view_offset = autocvar_cl_eventchase_vehicle_viewoffset;
1509                         }
1510                         if(ons_roundlost) { view_offset = autocvar_cl_eventchase_generator_viewoffset; }
1511
1512                         if(view_offset)
1513                         {
1514                                 WarpZone_TraceLine(current_view_origin, current_view_origin + view_offset + ('0 0 1' * autocvar_cl_eventchase_maxs.z), MOVE_WORLDONLY, self);
1515                                 if(trace_fraction == 1) { current_view_origin += view_offset; }
1516                                 else { current_view_origin.z += max(0, (trace_endpos.z - current_view_origin.z) - autocvar_cl_eventchase_maxs.z); }
1517                         }
1518
1519                         // We must enable chase_active to get a third person view (weapon viewmodel hidden and own player model showing).
1520                         // Ideally, there should be another way to enable third person cameras, such as through setproperty()
1521                         // -1 enables chase_active while marking it as set by this code, and not by the user (which would be 1)
1522                         if(!autocvar_chase_active) { cvar_set("chase_active", "-1"); }
1523
1524                         // make the camera smooth back
1525                         float chase_distance = autocvar_cl_eventchase_distance;
1526                         if(vehicle_chase)
1527                         {
1528                                 if(vehicle_viewofs)
1529                                         chase_distance = vehicle_viewdist;
1530                                 else
1531                                         chase_distance = autocvar_cl_eventchase_vehicle_distance;
1532                         }
1533                         if(ons_roundlost) { chase_distance = autocvar_cl_eventchase_generator_distance; }
1534
1535                         if(autocvar_cl_eventchase_speed && eventchase_current_distance < chase_distance)
1536                                 eventchase_current_distance += autocvar_cl_eventchase_speed * (chase_distance - eventchase_current_distance) * frametime; // slow down the further we get
1537                         else if(eventchase_current_distance != chase_distance)
1538                                 eventchase_current_distance = chase_distance;
1539
1540                         makevectors(view_angles);
1541
1542                         vector eventchase_target_origin = (current_view_origin - (v_forward * eventchase_current_distance));
1543                         WarpZone_TraceBox(current_view_origin, autocvar_cl_eventchase_mins, autocvar_cl_eventchase_maxs, eventchase_target_origin, MOVE_WORLDONLY, self);
1544
1545                         // If the boxtrace fails, revert back to line tracing.
1546                         if(!local_player.viewloc)
1547                         if(trace_startsolid)
1548                         {
1549                                 eventchase_target_origin = (current_view_origin - (v_forward * eventchase_current_distance));
1550                                 WarpZone_TraceLine(current_view_origin, eventchase_target_origin, MOVE_WORLDONLY, self);
1551                                 setproperty(VF_ORIGIN, (trace_endpos - (v_forward * autocvar_cl_eventchase_mins.z)));
1552                         }
1553                         else { setproperty(VF_ORIGIN, trace_endpos); }
1554
1555                         if(!local_player.viewloc)
1556                                 setproperty(VF_ANGLES, WarpZone_TransformVAngles(WarpZone_trace_transform, view_angles));
1557                 }
1558                 else if(autocvar_chase_active < 0) // time to disable chase_active if it was set by this code
1559                 {
1560                         eventchase_running = false;
1561                         cvar_set("chase_active", "0");
1562                         eventchase_current_distance = 0; // start from 0 next time
1563                 }
1564         }
1565         // workaround for camera stuck between player's legs when using chase_active 1
1566         // because the engine stops updating the chase_active camera when the game ends
1567         else if(intermission)
1568         {
1569                 cvar_settemp("chase_active", "-1");
1570                 eventchase_current_distance = 0;
1571         }
1572
1573         // do lockview after event chase camera so that it still applies whenever necessary.
1574         if(autocvar_cl_lockview || (!autocvar_hud_cursormode && (autocvar__hud_configure && spectatee_status <= 0 || intermission > 1 || QuickMenu_IsOpened())))
1575         {
1576                 setproperty(VF_ORIGIN, freeze_org);
1577                 setproperty(VF_ANGLES, freeze_ang);
1578         }
1579         else
1580         {
1581                 freeze_org = getpropertyvec(VF_ORIGIN);
1582                 freeze_ang = getpropertyvec(VF_ANGLES);
1583         }
1584
1585         WarpZone_FixView();
1586         //WarpZone_FixPMove();
1587
1588         vector ov_org = '0 0 0';
1589         vector ov_mid = '0 0 0';
1590         vector ov_worldmin = '0 0 0';
1591         vector ov_worldmax = '0 0 0';
1592         if(autocvar_cl_orthoview)
1593         {
1594                 ov_worldmin = mi_picmin;
1595                 ov_worldmax = mi_picmax;
1596
1597                 float ov_width = (ov_worldmax.x - ov_worldmin.x);
1598                 float ov_height = (ov_worldmax.y - ov_worldmin.y);
1599                 float ov_distance = (max(vid_width, vid_height) * max(ov_width, ov_height));
1600
1601                 ov_mid = ((ov_worldmax + ov_worldmin) * 0.5);
1602                 ov_org = vec3(ov_mid.x, ov_mid.y, (ov_mid.z + ov_distance));
1603
1604                 float ov_nearest = vlen(ov_org - vec3(
1605                         bound(ov_worldmin.x, ov_org.x, ov_worldmax.x),
1606                         bound(ov_worldmin.y, ov_org.y, ov_worldmax.y),
1607                         bound(ov_worldmin.z, ov_org.z, ov_worldmax.z)
1608                 ));
1609
1610                 float ov_furthest = 0;
1611                 float dist = 0;
1612
1613                 if((dist = vdist((vec3(ov_worldmin.x, ov_worldmin.y, ov_worldmin.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1614                 if((dist = vdist((vec3(ov_worldmax.x, ov_worldmin.y, ov_worldmin.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1615                 if((dist = vdist((vec3(ov_worldmin.x, ov_worldmax.y, ov_worldmin.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1616                 if((dist = vdist((vec3(ov_worldmin.x, ov_worldmin.y, ov_worldmax.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1617                 if((dist = vdist((vec3(ov_worldmax.x, ov_worldmax.y, ov_worldmin.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1618                 if((dist = vdist((vec3(ov_worldmin.x, ov_worldmax.y, ov_worldmax.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1619                 if((dist = vdist((vec3(ov_worldmax.x, ov_worldmin.y, ov_worldmax.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1620                 if((dist = vdist((vec3(ov_worldmax.x, ov_worldmax.y, ov_worldmax.z) - ov_org), >, ov_furthest))) { ov_furthest = dist; }
1621
1622                 if(!ov_enabled)
1623                 {
1624                         oldr_nearclip = cvar("r_nearclip");
1625                         oldr_farclip_base = cvar("r_farclip_base");
1626                         oldr_farclip_world = cvar("r_farclip_world");
1627                         oldr_novis = cvar("r_novis");
1628                         oldr_useportalculling = cvar("r_useportalculling");
1629                         oldr_useinfinitefarclip = cvar("r_useinfinitefarclip");
1630                 }
1631
1632                 cvar_settemp("r_nearclip", ftos(ov_nearest));
1633                 cvar_settemp("r_farclip_base", ftos(ov_furthest));
1634                 cvar_settemp("r_farclip_world", "0");
1635                 cvar_settemp("r_novis", "1");
1636                 cvar_settemp("r_useportalculling", "0");
1637                 cvar_settemp("r_useinfinitefarclip", "0");
1638
1639                 setproperty(VF_ORIGIN, ov_org);
1640                 setproperty(VF_ANGLES, '90 0 0');
1641
1642                 ov_enabled = true;
1643
1644                 #if 0
1645                 LOG_INFOF("OrthoView: org = %s, angles = %s, distance = %f, nearest = %f, furthest = %f\n",
1646                         vtos(ov_org),
1647                         vtos(getpropertyvec(VF_ANGLES)),
1648                         ov_distance,
1649                         ov_nearest,
1650                         ov_furthest);
1651                 #endif
1652         }
1653         else
1654         {
1655                 if(ov_enabled)
1656                 {
1657                         cvar_set("r_nearclip", ftos(oldr_nearclip));
1658                         cvar_set("r_farclip_base", ftos(oldr_farclip_base));
1659                         cvar_set("r_farclip_world", ftos(oldr_farclip_world));
1660                         cvar_set("r_novis", ftos(oldr_novis));
1661                         cvar_set("r_useportalculling", ftos(oldr_useportalculling));
1662                         cvar_set("r_useinfinitefarclip", ftos(oldr_useinfinitefarclip));
1663                 }
1664                 ov_enabled = false;
1665         }
1666
1667         // run viewmodel_draw before updating view_angles to the angles calculated by WarpZone_FixView
1668         // viewmodel_draw needs to use the view_angles set by the engine on every CSQC_UpdateView call
1669         FOREACH_ENTITY(it.draw, LAMBDA(it.draw(it)));
1670
1671         // Render the Scene
1672         view_origin = getpropertyvec(VF_ORIGIN);
1673         view_angles = getpropertyvec(VF_ANGLES);
1674         makevectors(view_angles);
1675         view_forward = v_forward;
1676         view_right = v_right;
1677         view_up = v_up;
1678
1679 #ifdef BLURTEST
1680         if(time > blurtest_time0 && time < blurtest_time1)
1681         {
1682                 float r, t;
1683
1684                 t = (time - blurtest_time0) / (blurtest_time1 - blurtest_time0);
1685                 r = t * blurtest_radius;
1686                 f = 1 / pow(t, blurtest_power) - 1;
1687
1688                 cvar_set("r_glsl_postprocess", "1");
1689                 cvar_set("r_glsl_postprocess_uservec1", strcat(ftos(r), " ", ftos(f), " 0 0"));
1690         }
1691         else
1692         {
1693                 cvar_set("r_glsl_postprocess", "0");
1694                 cvar_set("r_glsl_postprocess_uservec1", "0 0 0 0");
1695         }
1696 #endif
1697
1698         TargetMusic_Advance();
1699         Fog_Force();
1700
1701         if(drawtime == 0)
1702                 drawframetime = 0.01666667; // when we don't know fps yet, we assume 60fps
1703         else
1704                 drawframetime = bound(0.000001, time - drawtime, 1);
1705         drawtime = time;
1706
1707         // watch for gametype changes here...
1708         // in ParseStuffCMD the cmd isn't executed yet :/
1709         // might even be better to add the gametype to TE_CSQC_INIT...?
1710         if(!postinit)
1711                 PostInit();
1712
1713         if(intermission && !isdemo() && !(calledhooks & HOOK_END))
1714         {
1715                 if(calledhooks & HOOK_START)
1716                 {
1717                         localcmd("\ncl_hook_gameend\n");
1718                         calledhooks |= HOOK_END;
1719                 }
1720         }
1721
1722         Announcer();
1723
1724         fov = autocvar_fov;
1725         if(fov <= 59.5)
1726         {
1727                 if(!zoomscript_caught)
1728                 {
1729                         localcmd("+button9\n");
1730                         zoomscript_caught = 1;
1731                 }
1732         }
1733         else
1734         {
1735                 if(zoomscript_caught)
1736                 {
1737                         localcmd("-button9\n");
1738                         zoomscript_caught = 0;
1739                 }
1740         }
1741
1742         ColorTranslateMode = autocvar_cl_stripcolorcodes;
1743
1744         // currently switching-to weapon (for crosshair)
1745         switchingweapon = Weapons_from(STAT(SWITCHINGWEAPON));
1746
1747         // actually active weapon (for zoom)
1748         activeweapon = Weapons_from(STAT(ACTIVEWEAPON));
1749
1750         switchweapon = Weapons_from(STAT(SWITCHWEAPON));
1751
1752         f = (serverflags & SERVERFLAG_TEAMPLAY);
1753         if(f != teamplay)
1754         {
1755                 teamplay = f;
1756                 HUD_InitScores();
1757         }
1758
1759         if(last_switchweapon != switchweapon)
1760         {
1761                 weapontime = time;
1762                 last_switchweapon = switchweapon;
1763                 if(button_zoom && autocvar_cl_unpress_zoom_on_weapon_switch)
1764                 {
1765                         localcmd("-zoom\n");
1766                         button_zoom = false;
1767                 }
1768                 if(autocvar_cl_unpress_attack_on_weapon_switch)
1769                 {
1770                         localcmd("-fire\n");
1771                         localcmd("-fire2\n");
1772                         button_attack2 = false;
1773                 }
1774         }
1775         if(last_activeweapon != activeweapon)
1776         {
1777                 last_activeweapon = activeweapon;
1778
1779                 e = activeweapon;
1780                 if(e.netname != "")
1781                         localcmd(strcat("\ncl_hook_activeweapon ", e.netname), "\n");
1782                 else
1783                         localcmd("\ncl_hook_activeweapon none\n");
1784         }
1785
1786         // ALWAYS Clear Current Scene First
1787         clearscene();
1788
1789         setproperty(VF_ORIGIN, view_origin);
1790         setproperty(VF_ANGLES, view_angles);
1791
1792         // FIXME engine bug? VF_SIZE and VF_MIN are not restored to sensible values by this
1793         setproperty(VF_SIZE, vf_size);
1794         setproperty(VF_MIN, vf_min);
1795
1796         // Assign Standard Viewflags
1797         // Draw the World (and sky)
1798         setproperty(VF_DRAWWORLD, 1);
1799
1800         // Set the console size vars
1801         vid_conwidth = autocvar_vid_conwidth;
1802         vid_conheight = autocvar_vid_conheight;
1803         vid_pixelheight = autocvar_vid_pixelheight;
1804
1805         if(autocvar_cl_orthoview) { setproperty(VF_FOV, GetOrthoviewFOV(ov_worldmin, ov_worldmax, ov_mid, ov_org)); }
1806         else if(csqcplayer.viewloc) { setproperty(VF_FOV, GetViewLocationFOV(110)); } // enforce 110 fov, so things dont look odd
1807         else { setproperty(VF_FOV, GetCurrentFov(fov)); }
1808
1809         // Camera for demo playback
1810         if(camera_active)
1811         {
1812                 if(autocvar_camera_enable)
1813                         CSQC_Demo_Camera();
1814                 else
1815                 {
1816                         cvar_set("chase_active", ftos(chase_active_backup));
1817                         cvar_set("cl_demo_mousegrab", "0");
1818                         camera_active = false;
1819                 }
1820         }
1821         else
1822         {
1823 #ifdef CAMERATEST
1824                 if(autocvar_camera_enable)
1825 #else
1826                 if(autocvar_camera_enable && isdemo())
1827 #endif
1828                 {
1829                         // Enable required Darkplaces cvars
1830                         chase_active_backup = autocvar_chase_active;
1831                         cvar_set("chase_active", "2");
1832                         cvar_set("cl_demo_mousegrab", "1");
1833                         camera_active = true;
1834                         camera_mode = false;
1835                 }
1836         }
1837
1838         // Draw the Crosshair
1839         setproperty(VF_DRAWCROSSHAIR, 0); //Make sure engine crosshairs are always hidden
1840
1841         // Draw the Engine Status Bar (the default Quake HUD)
1842         setproperty(VF_DRAWENGINESBAR, 0);
1843
1844         // Update the mouse position
1845         /*
1846            mousepos_x = vid_conwidth;
1847            mousepos_y = vid_conheight;
1848            mousepos = mousepos*0.5 + getmousepos();
1849          */
1850
1851         addentities(MASK_NORMAL | MASK_ENGINE | MASK_ENGINEVIEWMODELS);
1852         renderscene();
1853
1854         // now switch to 2D drawing mode by calling a 2D drawing function
1855         // then polygon drawing will draw as 2D stuff, and NOT get queued until the
1856         // next R_RenderScene call
1857         drawstring('0 0 0', "", '1 1 0', '1 1 1', 0, 0);
1858
1859         if(autocvar_r_fakelight >= 2 || autocvar_r_fullbright)
1860         if (!(serverflags & SERVERFLAG_ALLOW_FULLBRIGHT))
1861         {
1862                 // apply night vision effect
1863                 vector tc_00, tc_01, tc_10, tc_11;
1864                 vector rgb = '0 0 0';
1865
1866                 if(!nightvision_noise)
1867                 {
1868                         nightvision_noise = new(nightvision_noise);
1869                 }
1870                 if(!nightvision_noise2)
1871                 {
1872                         nightvision_noise2 = new(nightvision_noise2);
1873                 }
1874
1875                 // color tint in yellow
1876                 drawfill('0 0 0', autocvar_vid_conwidth * '1 0 0' + autocvar_vid_conheight * '0 1 0', '0.5 1 0.3', 1, DRAWFLAG_MODULATE);
1877
1878                 // draw BG
1879                 a = Noise_Pink(nightvision_noise, frametime * 1.5) * 0.05 + 0.15;
1880                 rgb = '1 1 1';
1881                 tc_00 = '0 0 0' + '0.2 0 0' * sin(time * 0.3) + '0 0.3 0' * cos(time * 0.7);
1882                 tc_01 = '0 2.25 0' + '0.6 0 0' * cos(time * 1.2) - '0 0.3 0' * sin(time * 2.2);
1883                 tc_10 = '1.5 0 0' - '0.2 0 0' * sin(time * 0.5) + '0 0.5 0' * cos(time * 1.7);
1884                 //tc_11 = '1 1 0' + '0.6 0 0' * sin(time * 0.6) + '0 0.3 0' * cos(time * 0.1);
1885                 tc_11 = tc_01 + tc_10 - tc_00;
1886                 R_BeginPolygon("gfx/nightvision-bg.tga", DRAWFLAG_ADDITIVE);
1887                 R_PolygonVertex('0 0 0', tc_00, rgb, a);
1888                 R_PolygonVertex(autocvar_vid_conwidth * '1 0 0', tc_10, rgb, a);
1889                 R_PolygonVertex(autocvar_vid_conwidth * '1 0 0' + autocvar_vid_conheight * '0 1 0', tc_11, rgb, a);
1890                 R_PolygonVertex(autocvar_vid_conheight * '0 1 0', tc_01, rgb, a);
1891                 R_EndPolygon();
1892
1893                 // draw FG
1894                 a = Noise_Pink(nightvision_noise2, frametime * 0.1) * 0.05 + 0.12;
1895                 rgb = '0.3 0.6 0.4' + '0.1 0.4 0.2' * Noise_White(nightvision_noise2, frametime);
1896                 tc_00 = '0 0 0' + '1 0 0' * Noise_White(nightvision_noise2, frametime) + '0 1 0' * Noise_White(nightvision_noise2, frametime);
1897                 tc_01 = tc_00 + '0 3 0' * (1 + Noise_White(nightvision_noise2, frametime) * 0.2);
1898                 tc_10 = tc_00 + '2 0 0' * (1 + Noise_White(nightvision_noise2, frametime) * 0.3);
1899                 tc_11 = tc_01 + tc_10 - tc_00;
1900                 R_BeginPolygon("gfx/nightvision-fg.tga", DRAWFLAG_ADDITIVE);
1901                 R_PolygonVertex('0 0 0', tc_00, rgb, a);
1902                 R_PolygonVertex(autocvar_vid_conwidth * '1 0 0', tc_10, rgb, a);
1903                 R_PolygonVertex(autocvar_vid_conwidth * '1 0 0' + autocvar_vid_conheight * '0 1 0', tc_11, rgb, a);
1904                 R_PolygonVertex(autocvar_vid_conheight * '0 1 0', tc_01, rgb, a);
1905                 R_EndPolygon();
1906         }
1907
1908         if(autocvar_cl_reticle)
1909         {
1910                 Weapon wep = activeweapon;
1911                 // Draw the aiming reticle for weapons that use it
1912                 // reticle_type is changed to the item we are zooming / aiming with, to decide which reticle to use
1913                 // It must be a persisted float for fading out to work properly (you let go of the zoom button for
1914                 // the view to go back to normal, so reticle_type would become 0 as we fade out)
1915                 if(spectatee_status || is_dead || hud != HUD_NORMAL)
1916                 {
1917                         // no zoom reticle while dead
1918                         reticle_type = 0;
1919                 }
1920                 else if(wep.wr_zoomreticle(wep) && autocvar_cl_reticle_weapon)
1921                 {
1922                         if(reticle_image != "") { reticle_type = 2; }
1923                         else { reticle_type = 0; }
1924                 }
1925                 else if(button_zoom || zoomscript_caught)
1926                 {
1927                         // normal zoom
1928                         reticle_type = 1;
1929                 }
1930
1931                 if(reticle_type)
1932                 {
1933                         if(autocvar_cl_reticle_stretch)
1934                         {
1935                                 reticle_size.x = vid_conwidth;
1936                                 reticle_size.y = vid_conheight;
1937                                 reticle_pos.x = 0;
1938                                 reticle_pos.y = 0;
1939                         }
1940                         else
1941                         {
1942                                 reticle_size.x = max(vid_conwidth, vid_conheight);
1943                                 reticle_size.y = max(vid_conwidth, vid_conheight);
1944                                 reticle_pos.x = (vid_conwidth - reticle_size.x) / 2;
1945                                 reticle_pos.y = (vid_conheight - reticle_size.y) / 2;
1946                         }
1947
1948                         if(zoomscript_caught)
1949                                 f = 1;
1950                         else
1951                                 f = current_zoomfraction;
1952
1953                         if(f)
1954                         {
1955                                 switch(reticle_type)
1956                                 {
1957                                         case 1: drawpic(reticle_pos, "gfx/reticle_normal", reticle_size, '1 1 1', f * autocvar_cl_reticle_normal_alpha, DRAWFLAG_NORMAL); break;
1958                                         case 2: drawpic(reticle_pos, reticle_image, reticle_size, '1 1 1', f * autocvar_cl_reticle_weapon_alpha, DRAWFLAG_NORMAL); break;
1959                                 }
1960                         }
1961                 }
1962         }
1963         else
1964         {
1965                 if(reticle_type != 0) { reticle_type = 0; }
1966         }
1967
1968
1969         // improved polyblend
1970         if(autocvar_hud_contents)
1971         {
1972                 float contentalpha_temp, incontent, liquidalpha, contentfadetime;
1973                 vector liquidcolor;
1974
1975                 switch(pointcontents(view_origin))
1976                 {
1977                         case CONTENT_WATER:
1978                                 liquidalpha = autocvar_hud_contents_water_alpha;
1979                                 liquidcolor = stov(autocvar_hud_contents_water_color);
1980                                 incontent = 1;
1981                                 break;
1982
1983                         case CONTENT_LAVA:
1984                                 liquidalpha = autocvar_hud_contents_lava_alpha;
1985                                 liquidcolor = stov(autocvar_hud_contents_lava_color);
1986                                 incontent = 1;
1987                                 break;
1988
1989                         case CONTENT_SLIME:
1990                                 liquidalpha = autocvar_hud_contents_slime_alpha;
1991                                 liquidcolor = stov(autocvar_hud_contents_slime_color);
1992                                 incontent = 1;
1993                                 break;
1994
1995                         default:
1996                                 liquidalpha = 0;
1997                                 liquidcolor = '0 0 0';
1998                                 incontent = 0;
1999                                 break;
2000                 }
2001
2002                 if(incontent) // fade in/out at different speeds so you can do e.g. instant fade when entering water and slow when leaving it.
2003                 { // also lets delcare previous values for blending properties, this way it isn't reset until after you have entered a different content
2004                         contentfadetime = autocvar_hud_contents_fadeintime;
2005                         liquidalpha_prev = liquidalpha;
2006                         liquidcolor_prev = liquidcolor;
2007                 }
2008                 else
2009                         contentfadetime = autocvar_hud_contents_fadeouttime;
2010
2011                 contentalpha_temp = bound(0, drawframetime / max(0.0001, contentfadetime), 1);
2012                 contentavgalpha = contentavgalpha * (1 - contentalpha_temp) + incontent * contentalpha_temp;
2013
2014                 if(contentavgalpha)
2015                         drawfill('0 0 0', eX * vid_conwidth + eY * vid_conheight, liquidcolor_prev, contentavgalpha * liquidalpha_prev, DRAWFLAG_NORMAL);
2016
2017                 if(autocvar_hud_postprocessing)
2018                 {
2019                         if(autocvar_hud_contents_blur && contentavgalpha)
2020                         {
2021                                 content_blurpostprocess.x = 1;
2022                                 content_blurpostprocess.y = contentavgalpha * autocvar_hud_contents_blur;
2023                                 content_blurpostprocess.z = contentavgalpha * autocvar_hud_contents_blur_alpha;
2024                         }
2025                         else
2026                         {
2027                                 content_blurpostprocess.x = 0;
2028                                 content_blurpostprocess.y = 0;
2029                                 content_blurpostprocess.z = 0;
2030                         }
2031                 }
2032         }
2033
2034         if(autocvar_hud_damage && !STAT(FROZEN))
2035         {
2036                 splash_size.x = max(vid_conwidth, vid_conheight);
2037                 splash_size.y = max(vid_conwidth, vid_conheight);
2038                 splash_pos.x = (vid_conwidth - splash_size.x) / 2;
2039                 splash_pos.y = (vid_conheight - splash_size.y) / 2;
2040
2041                 float myhealth_flash_temp;
2042                 myhealth = STAT(HEALTH);
2043
2044                 // fade out
2045                 myhealth_flash = max(0, myhealth_flash - autocvar_hud_damage_fade_rate * frametime);
2046                 // add new damage
2047                 myhealth_flash = bound(0, myhealth_flash + dmg_take * autocvar_hud_damage_factor, autocvar_hud_damage_maxalpha);
2048
2049                 float pain_threshold, pain_threshold_lower, pain_threshold_lower_health;
2050                 pain_threshold = autocvar_hud_damage_pain_threshold;
2051                 pain_threshold_lower = autocvar_hud_damage_pain_threshold_lower;
2052                 pain_threshold_lower_health = autocvar_hud_damage_pain_threshold_lower_health;
2053
2054                 if(pain_threshold_lower && myhealth < pain_threshold_lower_health)
2055                 {
2056                         pain_threshold = pain_threshold - max(autocvar_hud_damage_pain_threshold_pulsating_min, fabs(sin(M_PI * time / autocvar_hud_damage_pain_threshold_pulsating_period))) * pain_threshold_lower * (1 - max(0, myhealth)/pain_threshold_lower_health);
2057                 }
2058
2059                 myhealth_flash_temp = bound(0, myhealth_flash - pain_threshold, 1);
2060
2061                 if(myhealth_prev < 1)
2062                 {
2063                         if(myhealth >= 1)
2064                         {
2065                                 myhealth_flash = 0; // just spawned, clear the flash immediately
2066                                 myhealth_flash_temp = 0;
2067                         }
2068                         else
2069                         {
2070                                 myhealth_flash += autocvar_hud_damage_fade_rate * frametime; // dead
2071                         }
2072                 }
2073
2074                 if(spectatee_status == -1 || intermission)
2075                 {
2076                         myhealth_flash = 0; // observing, or match ended
2077                         myhealth_flash_temp = 0;
2078                 }
2079
2080                 myhealth_prev = myhealth;
2081
2082                 // IDEA: change damage color/picture based on player model for robot/alien species?
2083                 // pro: matches model better
2084                 // contra: it's not red because blood is red, but because red is an alarming color, so red should stay
2085                 // maybe different reddish pics?
2086                 if(autocvar_cl_gentle_damage || autocvar_cl_gentle)
2087                 {
2088                         if(autocvar_cl_gentle_damage == 2)
2089                         {
2090                                 if(myhealth_flash < pain_threshold) // only randomize when the flash is gone
2091                                         myhealth_gentlergb = eX * random() + eY * random() + eZ * random();
2092                         }
2093                         else
2094                                 myhealth_gentlergb = stov(autocvar_hud_damage_gentle_color);
2095
2096                         if(myhealth_flash_temp > 0)
2097                                 drawfill('0 0 0', eX * vid_conwidth + eY * vid_conheight, myhealth_gentlergb, autocvar_hud_damage_gentle_alpha_multiplier * bound(0, myhealth_flash_temp, 1) * autocvar_hud_damage, DRAWFLAG_NORMAL);
2098                 }
2099                 else if(myhealth_flash_temp > 0)
2100                         drawpic(splash_pos, "gfx/blood", splash_size, stov(autocvar_hud_damage_color), bound(0, myhealth_flash_temp, 1) * autocvar_hud_damage, DRAWFLAG_NORMAL);
2101
2102                 if(autocvar_hud_postprocessing) // we still need to set this anyway even when chase_active is set, this way it doesn't get stuck on.
2103                 {
2104                         if(autocvar_hud_damage_blur && myhealth_flash_temp)
2105                         {
2106                                 damage_blurpostprocess.x = 1;
2107                                 damage_blurpostprocess.y = bound(0, myhealth_flash_temp, 1) * autocvar_hud_damage_blur;
2108                                 damage_blurpostprocess.z = bound(0, myhealth_flash_temp, 1) * autocvar_hud_damage_blur_alpha;
2109                         }
2110                         else
2111                         {
2112                                 damage_blurpostprocess.x = 0;
2113                                 damage_blurpostprocess.y = 0;
2114                                 damage_blurpostprocess.z = 0;
2115                         }
2116                 }
2117         }
2118
2119         float e1 = (autocvar_hud_postprocessing_maxbluralpha != 0);
2120         float e2 = (autocvar_hud_powerup != 0);
2121         if(autocvar_hud_postprocessing && (e1 || e2)) // TODO: Remove this code and re-do the postprocess handling in the engine, where it properly belongs.
2122         {
2123                 // enable or disable rendering types if they are used or not
2124                 if(cvar("r_glsl_postprocess_uservec1_enable") != e1) { cvar_set("r_glsl_postprocess_uservec1_enable", ftos(e1)); }
2125                 if(cvar("r_glsl_postprocess_uservec2_enable") != e2) { cvar_set("r_glsl_postprocess_uservec2_enable", ftos(e2)); }
2126
2127                 // blur postprocess handling done first (used by hud_damage and hud_contents)
2128                 if((damage_blurpostprocess.x || content_blurpostprocess.x))
2129                 {
2130                         float blurradius = bound(0, damage_blurpostprocess.y + content_blurpostprocess.y, autocvar_hud_postprocessing_maxblurradius);
2131                         float bluralpha = bound(0, damage_blurpostprocess.z + content_blurpostprocess.z, autocvar_hud_postprocessing_maxbluralpha);
2132                         if(blurradius != old_blurradius || bluralpha != old_bluralpha) // reduce cvar_set spam as much as possible
2133                         {
2134                                 cvar_set("r_glsl_postprocess_uservec1", strcat(ftos(blurradius), " ", ftos(bluralpha), " 0 0"));
2135                                 old_blurradius = blurradius;
2136                                 old_bluralpha = bluralpha;
2137                         }
2138                 }
2139                 else if(cvar_string("r_glsl_postprocess_uservec1") != "0 0 0 0") // reduce cvar_set spam as much as possible
2140                 {
2141                         cvar_set("r_glsl_postprocess_uservec1", "0 0 0 0");
2142                         old_blurradius = 0;
2143                         old_bluralpha = 0;
2144                 }
2145
2146                 // edge detection postprocess handling done second (used by hud_powerup)
2147                 float sharpen_intensity = 0, strength_finished = STAT(STRENGTH_FINISHED), invincible_finished = STAT(INVINCIBLE_FINISHED);
2148                 if (strength_finished - time > 0) { sharpen_intensity += (strength_finished - time); }
2149                 if (invincible_finished - time > 0) { sharpen_intensity += (invincible_finished - time); }
2150
2151                 sharpen_intensity = bound(0, ((STAT(HEALTH) > 0) ? sharpen_intensity : 0), 5); // Check to see if player is alive (if not, set 0) - also bound to fade out starting at 5 seconds.
2152
2153                 if(autocvar_hud_powerup && sharpen_intensity > 0)
2154                 {
2155                         if(sharpen_intensity != old_sharpen_intensity) // reduce cvar_set spam as much as possible
2156                         {
2157                                 cvar_set("r_glsl_postprocess_uservec2", strcat(ftos((sharpen_intensity / 5) * autocvar_hud_powerup), " ", ftos(-sharpen_intensity * autocvar_hud_powerup), " 0 0"));
2158                                 old_sharpen_intensity = sharpen_intensity;
2159                         }
2160                 }
2161                 else if(cvar_string("r_glsl_postprocess_uservec2") != "0 0 0 0") // reduce cvar_set spam as much as possible
2162                 {
2163                         cvar_set("r_glsl_postprocess_uservec2", "0 0 0 0");
2164                         old_sharpen_intensity = 0;
2165                 }
2166
2167                 if(cvar("r_glsl_postprocess") == 0)
2168                         cvar_set("r_glsl_postprocess", "2");
2169         }
2170         else if(cvar("r_glsl_postprocess") == 2)
2171                 cvar_set("r_glsl_postprocess", "0");
2172
2173         /*if(gametype == MAPINFO_TYPE_CTF)
2174           {
2175           ctf_view();
2176           } else */
2177
2178         // draw 2D entities
2179         FOREACH_ENTITY(it.draw2d, LAMBDA(it.draw2d(it)));
2180         Draw_ShowNames_All();
2181         Debug_Draw();
2182
2183         scoreboard_active = HUD_WouldDrawScoreboard();
2184
2185         HUD_Draw();
2186
2187         if(NextFrameCommand)
2188         {
2189                 localcmd("\n", NextFrameCommand, "\n");
2190                 NextFrameCommand = string_null;
2191         }
2192
2193         // we must do this check AFTER a frame was rendered, or it won't work
2194         if(cs_project_is_b0rked == 0)
2195         {
2196                 string w0, h0;
2197                 w0 = ftos(autocvar_vid_conwidth);
2198                 h0 = ftos(autocvar_vid_conheight);
2199                 //setproperty(VF_VIEWPORT, '0 0 0', '640 480 0');
2200                 //setproperty(VF_FOV, '90 90 0');
2201                 setproperty(VF_ORIGIN, '0 0 0');
2202                 setproperty(VF_ANGLES, '0 0 0');
2203                 setproperty(VF_PERSPECTIVE, 1);
2204                 makevectors('0 0 0');
2205                 vector v1, v2;
2206                 cvar_set("vid_conwidth", "800");
2207                 cvar_set("vid_conheight", "600");
2208                 v1 = cs_project(v_forward);
2209                 cvar_set("vid_conwidth", "640");
2210                 cvar_set("vid_conheight", "480");
2211                 v2 = cs_project(v_forward);
2212                 if(v1 == v2)
2213                         cs_project_is_b0rked = 1;
2214                 else
2215                         cs_project_is_b0rked = -1;
2216                 cvar_set("vid_conwidth", w0);
2217                 cvar_set("vid_conheight", h0);
2218         }
2219
2220         if(autocvar__hud_configure)
2221                 HUD_Panel_Mouse();
2222         else if ( HUD_MinigameMenu_IsOpened() || minigame_isactive() )
2223                 HUD_Minigame_Mouse();
2224         else if(QuickMenu_IsOpened())
2225                 QuickMenu_Mouse();
2226         else
2227                 HUD_Radar_Mouse();
2228
2229         cl_notice_run();
2230         unpause_update();
2231         Net_Flush();
2232
2233         // let's reset the view back to normal for the end
2234         setproperty(VF_MIN, '0 0 0');
2235         setproperty(VF_SIZE, '1 0 0' * w + '0 1 0' * h);
2236 }
2237
2238
2239 // following vectors must be global to allow seamless switching between camera modes
2240 vector camera_offset, current_camera_offset, mouse_angles, current_angles, current_origin, current_position;
2241 void CSQC_Demo_Camera()
2242 {
2243         float speed, attenuation, dimensions;
2244         vector tmp, delta;
2245
2246         if( autocvar_camera_reset || !camera_mode )
2247         {
2248                 camera_offset = '0 0 0';
2249                 current_angles = '0 0 0';
2250                 camera_direction = '0 0 0';
2251                 camera_offset.z += 30;
2252                 camera_offset.x += 30 * -cos(current_angles.y * DEG2RAD);
2253                 camera_offset.y += 30 * -sin(current_angles.y * DEG2RAD);
2254                 current_origin = view_origin;
2255                 current_camera_offset  = camera_offset;
2256                 cvar_set("camera_reset", "0");
2257                 camera_mode = CAMERA_CHASE;
2258         }
2259
2260         // Camera angles
2261         if( camera_roll )
2262                 mouse_angles.z += camera_roll * autocvar_camera_speed_roll;
2263
2264         if(autocvar_camera_look_player)
2265         {
2266                 vector dir;
2267                 float n;
2268
2269                 dir = normalize(view_origin - current_position);
2270                 n = mouse_angles.z;
2271                 mouse_angles = vectoangles(dir);
2272                 mouse_angles.x = mouse_angles.x * -1;
2273                 mouse_angles.z = n;
2274         }
2275         else
2276         {
2277                 tmp = getmousepos() * 0.1;
2278                 if(vdist(tmp, >, autocvar_camera_mouse_threshold))
2279                 {
2280                         mouse_angles.x += tmp.y * cos(mouse_angles.z * DEG2RAD) + (tmp.x * sin(mouse_angles.z * DEG2RAD));
2281                         mouse_angles.y -= tmp.x * cos(mouse_angles.z * DEG2RAD) + (tmp.y * -sin(mouse_angles.z * DEG2RAD));
2282                 }
2283         }
2284
2285         while (mouse_angles.x < -180) mouse_angles.x = mouse_angles.x + 360;
2286         while (mouse_angles.x > 180) mouse_angles.x = mouse_angles.x - 360;
2287         while (mouse_angles.y < -180) mouse_angles.y = mouse_angles.y + 360;
2288         while (mouse_angles.y > 180) mouse_angles.y = mouse_angles.y - 360;
2289
2290         // Fix difference when angles don't have the same sign
2291         delta = '0 0 0';
2292         if(mouse_angles.y < -60 && current_angles.y > 60)
2293                 delta = '0 360 0';
2294         if(mouse_angles.y > 60 && current_angles.y < -60)
2295                 delta = '0 -360 0';
2296
2297         if(autocvar_camera_look_player)
2298                 attenuation = autocvar_camera_look_attenuation;
2299         else
2300                 attenuation = autocvar_camera_speed_attenuation;
2301
2302         attenuation = 1 / max(1, attenuation);
2303         current_angles += (mouse_angles - current_angles + delta) * attenuation;
2304
2305         while (current_angles.x < -180) current_angles.x = current_angles.x + 360;
2306         while (current_angles.x > 180) current_angles.x = current_angles.x - 360;
2307         while (current_angles.y < -180) current_angles.y = current_angles.y + 360;
2308         while (current_angles.y > 180) current_angles.y = current_angles.y - 360;
2309
2310         // Camera position
2311         tmp = '0 0 0';
2312         dimensions = 0;
2313
2314         if( camera_direction.x )
2315         {
2316                 tmp.x = camera_direction.x * cos(current_angles.y * DEG2RAD);
2317                 tmp.y = camera_direction.x * sin(current_angles.y * DEG2RAD);
2318                 if( autocvar_camera_forward_follows && !autocvar_camera_look_player )
2319                         tmp.z = camera_direction.x * -sin(current_angles.x * DEG2RAD);
2320                 ++dimensions;
2321         }
2322
2323         if( camera_direction.y )
2324         {
2325                 tmp.x += camera_direction.y * -sin(current_angles.y * DEG2RAD);
2326                 tmp.y += camera_direction.y * cos(current_angles.y * DEG2RAD) * cos(current_angles.z * DEG2RAD);
2327                 tmp.z += camera_direction.y * sin(current_angles.z * DEG2RAD);
2328                 ++dimensions;
2329         }
2330
2331         if( camera_direction.z )
2332         {
2333                 tmp.z += camera_direction.z * cos(current_angles.z * DEG2RAD);
2334                 ++dimensions;
2335         }
2336
2337         if(autocvar_camera_free)
2338                 speed = autocvar_camera_speed_free;
2339         else
2340                 speed = autocvar_camera_speed_chase;
2341
2342         if(dimensions)
2343         {
2344                 speed = speed * sqrt(1 / dimensions);
2345                 camera_offset += tmp * speed;
2346         }
2347
2348         current_camera_offset += (camera_offset - current_camera_offset) * attenuation;
2349
2350         // Camera modes
2351         if( autocvar_camera_free )
2352         {
2353                 if ( camera_mode == CAMERA_CHASE )
2354                 {
2355                         current_camera_offset = current_origin + current_camera_offset;
2356                         camera_offset = current_origin + camera_offset;
2357                 }
2358
2359                 camera_mode = CAMERA_FREE;
2360                 current_position = current_camera_offset;
2361         }
2362         else
2363         {
2364                 if ( camera_mode == CAMERA_FREE )
2365                 {
2366                         current_origin = view_origin;
2367                         camera_offset = camera_offset - current_origin;
2368                         current_camera_offset = current_camera_offset - current_origin;
2369                 }
2370
2371                 camera_mode = CAMERA_CHASE;
2372
2373                 if(autocvar_camera_chase_smoothly)
2374                         current_origin += (view_origin - current_origin) * attenuation;
2375                 else
2376                         current_origin = view_origin;
2377
2378                 current_position = current_origin + current_camera_offset;
2379         }
2380
2381         setproperty(VF_ANGLES, current_angles);
2382         setproperty(VF_ORIGIN, current_position);
2383 }