smooth crosshair effects by Taoki
[divverent/nexuiz.git] / data / qcsrc / client / View.qc
1 #define spider_rocket_icon "gfx/spiderbot/rocket_ico.tga"
2 #define spider_rocket_targ "gfx/spiderbot/target.tga"
3 #define SPIDER_CROSS "textures/spiderbot/cross.tga"
4 #define rkt_size 32
5 #define rld_size_x 256
6 #define rld_size_y 16
7
8 entity porto;
9 vector polyline[16];
10 float trace_dphitcontents;
11 float trace_networkentity;
12 float Q3SURFACEFLAG_SLICK = 2; // low friction surface
13 float DPCONTENTS_SOLID = 1; // blocks player movement
14 float DPCONTENTS_BODY = 32; // blocks player movement
15 float DPCONTENTS_CORPSE = 64; // blocks player movement
16 float DPCONTENTS_PLAYERCLIP = 256; // blocks player movement
17 void Porto_Draw()
18 {
19         vector p, dir, ang, q, nextdir;
20         float idx, portal_number, portal1_idx;
21
22         if(activeweapon != WEP_PORTO || spectatee_status || gametype == GAME_NEXBALL)
23                 return;
24         if(intermission == 1)
25                 return;
26         if(intermission == 2)
27                 return;
28         if (getstati(STAT_HEALTH) <= 0)
29                 return;
30
31         dir = view_forward;
32
33         if(angles_held_status)
34         {
35                 makevectors(angles_held);
36                 dir = v_forward;
37         }
38
39         p = view_origin;
40
41         polyline[0] = p;
42         idx = 1;
43         portal_number = 0;
44         nextdir = dir;
45
46         for(;;)
47         {
48                 dir = nextdir;
49                 traceline(p, p + 65536 * dir, TRUE, porto);
50                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
51                         return;
52                 nextdir = dir - 2 * (dir * trace_plane_normal) * trace_plane_normal; // mirror dir at trace_plane_normal
53                 p = trace_endpos;
54                 polyline[idx] = p;
55                 ++idx;
56                 if(idx >= 16)
57                         return;
58                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
59                         continue;
60                 ++portal_number;
61                 ang = vectoangles2(trace_plane_normal, dir);
62                 ang_x = -ang_x;
63                 makevectors(ang);
64                 if(!CheckWireframeBox(porto, p - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))
65                         return;
66                 if(portal_number == 1)
67                         portal1_idx = idx;
68                 if(portal_number >= 2)
69                         break;
70         }
71
72         while(idx >= 2)
73         {
74                 p = polyline[idx-2];
75                 q = polyline[idx-1];
76                 if(idx == 2)
77                         p = p - view_up * 16;
78                 if(idx-1 >= portal1_idx)
79                 {
80                         Draw_CylindricLine(p, q, 4, "", 1, 0, '0 0 1', 0.5, DRAWFLAG_NORMAL);
81                 }
82                 else
83                 {
84                         Draw_CylindricLine(p, q, 4, "", 1, 0, '1 0 0', 0.5, DRAWFLAG_NORMAL);
85                 }
86                 --idx;
87         }
88 }
89
90 /**
91  * Checks whether the server initiated a map restart (stat_game_starttime changed)
92  *
93  * TODO: Use a better solution where a common shared entitiy is used that contains
94  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
95  * and STAT_FRAGLIMIT to be auto-sent)
96  */
97 void CheckForGamestartChange() {
98         float startTime;
99         startTime = getstatf(STAT_GAMESTARTTIME);
100         if (previous_game_starttime != startTime) {
101                 if ((time + 5.0) < startTime) {
102                         //if connecting to server while restart was active don't always play prepareforbattle
103                         sound(self, CHAN_VOICE, "announcer/robotic/prepareforbattle.wav", VOL_BASEVOICE, ATTN_NONE);
104                 }
105                 if (time < startTime) {
106                         restartAnnouncer = spawn();
107                         restartAnnouncer.think = restartAnnouncer_Think;
108                         restartAnnouncer.nextthink = startTime - floor(startTime - time); //synchronize nextthink to startTime
109                 }
110         }
111         previous_game_starttime = startTime;
112 }
113
114 void Porto_Init()
115 {
116         porto = spawn();
117         porto.classname = "porto";
118         porto.draw = Porto_Draw;
119         porto.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
120 }
121
122 float drawtime;
123
124 float tan(float x)
125 {
126         return sin(x) / cos(x);
127 }
128 float atan2(float y, float x)
129 {
130         vector v;
131         v = '1 0 0' * x + '0 1 0' * y;
132         v = vectoangles(v);
133         return v_y * 0.01745329251994329576;
134 }
135
136 vector GetCurrentFov(float fov)
137 {
138         float zoomsensitivity, zoomspeed, zoomfactor, zoomdir;
139
140         zoomsensitivity = cvar("cl_zoomsensitivity");
141         zoomfactor = cvar("cl_zoomfactor");
142         if(zoomfactor < 1 || zoomfactor > 16)
143                 zoomfactor = 2.5;
144         zoomspeed = cvar("cl_zoomspeed");
145         if(zoomspeed >= 0)
146                 if(zoomspeed < 0.5 || zoomspeed > 16)
147                         zoomspeed = 3.5;
148
149         zoomdir = button_zoom;
150         if(getstati(STAT_ACTIVEWEAPON) == WEP_NEX) // do NOT use switchweapon here
151                 zoomdir += button_attack2;
152         if(spectatee_status > 0 || isdemo())
153         {
154                 if(spectatorbutton_zoom)
155                         zoomdir = 0 + !zoomdir;
156                         // do not even THINK about removing this 0
157                         // _I_ know what I am doing
158                         // fteqcc does not
159         }
160
161         if(zoomdir)
162                 zoomin_effect = 0;
163
164         if(zoomin_effect || camera_active)
165         {
166                 current_viewzoom = min(1, current_viewzoom + drawframetime);
167         }
168         else
169         {
170                 if(zoomspeed < 0) // instant zoom
171                 {
172                         if(zoomdir)
173                                 current_viewzoom = 1 / zoomfactor;
174                         else
175                                 current_viewzoom = 1;
176                 }
177                 else
178                 {
179                         if(zoomdir)
180                                 current_viewzoom = 1 / bound(1, 1 / current_viewzoom + drawframetime * zoomspeed * (zoomfactor - 1), zoomfactor);
181                         else
182                                 current_viewzoom = bound(1 / zoomfactor, current_viewzoom + drawframetime * zoomspeed * (1 - 1 / zoomfactor), 1);
183                 }
184         }
185
186         if(almost_equals(current_viewzoom, 1))
187                 current_zoomfraction = 0;
188         else if(almost_equals(current_viewzoom, 1/zoomfactor))
189                 current_zoomfraction = 1;
190         else
191                 current_zoomfraction = (current_viewzoom - 1) / (1/zoomfactor - 1);
192
193         if(zoomsensitivity < 1)
194                 setsensitivityscale(pow(current_viewzoom, 1 - zoomsensitivity));
195         else
196                 setsensitivityscale(1);
197
198         float frustumx, frustumy, fovx, fovy;
199         frustumy = tan(fov * 0.00872664625997164788) * 0.75 * current_viewzoom;
200         frustumx = frustumy * vid_width / vid_height / vid_pixelheight;
201         fovx = atan2(frustumx, 1) / 0.00872664625997164788;
202         fovy = atan2(frustumy, 1) / 0.00872664625997164788;
203
204         return '1 0 0' * fovx + '0 1 0' * fovy;
205 }
206
207 // this function must match W_SetupShot!
208 float zoomscript_caught;
209
210 vector wcross_origin;
211 float wcross_sizefloat, wcross_alpha;
212 entity trueaim;
213 entity trueaim_rifle;
214
215 #define SHOTTYPE_HITTEAM 1
216 #define SHOTTYPE_HITOBSTRUCTION 2
217 #define SHOTTYPE_HITWORLD 3
218 #define SHOTTYPE_HITENEMY 4
219
220 void TrueAim_Init()
221 {
222         trueaim = spawn();
223         trueaim.classname = "trueaim";
224         trueaim.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
225         trueaim_rifle = spawn();
226         trueaim_rifle.classname = "trueaim_rifle";
227         trueaim_rifle.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
228 }
229
230 float EnemyHitCheck()
231 {
232         float t;
233         wcross_origin = project_3d_to_2d(trace_endpos);
234         wcross_origin_z = 0;
235         if(trace_networkentity < 1)
236                 return SHOTTYPE_HITWORLD;
237         if(trace_networkentity > maxclients)
238                 return SHOTTYPE_HITWORLD;
239         t = GetPlayerColor(trace_networkentity - 1);
240         if(teamplay)
241         if(t == myteam)
242                 return SHOTTYPE_HITTEAM;
243         if(t == COLOR_SPECTATOR)
244                 return SHOTTYPE_HITWORLD;
245         return SHOTTYPE_HITENEMY;
246 }
247
248 float TrueAimCheck()
249 {
250         float nudge = 1; // added to traceline target and subtracted from result
251         vector vecs, trueaimpoint, w_shotorg;
252         vector mi, ma, dv;
253         float shottype;
254
255         mi = ma = '0 0 0';
256
257         switch(activeweapon)
258         {
259                 case WEP_TUBA: // no aim
260                 case WEP_PORTO: // shoots from eye
261                 case WEP_HOOK: // no trueaim
262                 case WEP_GRENADE_LAUNCHER: // toss curve
263                         return SHOTTYPE_HITWORLD;
264                 case WEP_CAMPINGRIFLE:
265                         if(zoomscript_caught)
266                         {
267                                 tracebox(view_origin, '0 0 0', '0 0 0', view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, trueaim);
268                                 return EnemyHitCheck();
269                         }
270                         break;
271                 case WEP_ROCKET_LAUNCHER: // projectile has a size!
272                         mi = '-3 -3 -3';
273                         ma = '3 3 3';
274                         break;
275                 case WEP_FIREBALL: // projectile has a size!
276                         mi = '-16 -16 -16';
277                         ma = '16 16 16';
278                         break;
279                 case WEP_ELECTRO: // projectile has a size!
280                         mi = '0 0 -3';
281                         ma = '0 0 -3';
282                         break;
283         }
284
285         vecs = decompressShotOrigin(getstati(STAT_SHOTORG));
286
287         if(activeweapon == WEP_CAMPINGRIFLE)
288                 traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, trueaim_rifle);
289         else
290                 traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, trueaim);
291         trueaimpoint = trace_endpos;
292
293         if(vecs_x > 0)
294                 vecs_y = -vecs_y;
295         else
296                 vecs = '0 0 0';
297
298         dv = view_right * vecs_y + view_up * vecs_z;
299         w_shotorg = view_origin + dv;
300
301         // now move the vecs forward as much as requested if possible
302         tracebox(w_shotorg, mi, ma, w_shotorg + view_forward * (vecs_x + nudge), MOVE_NORMAL, trueaim); // FIXME this MOVE_NORMAL part will misbehave a little in csqc
303         w_shotorg = trace_endpos - view_forward * nudge;
304
305         tracebox(w_shotorg, mi, ma, trueaimpoint, MOVE_NORMAL, trueaim);
306         shottype = EnemyHitCheck();
307         if(shottype != SHOTTYPE_HITWORLD)
308                 return shottype;
309         
310 #if 0
311         // FIXME WHY DOES THIS NOT WORK FOR THE ROCKET LAUNCHER?
312         // or rather, I know why, but see no fix
313         if(vlen(trace_endpos - trueaimpoint) > vlen(ma) + vlen(mi) + 1)
314                 // 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
315                 return SHOTTYPE_HITOBSTRUCTION;
316 #endif
317         
318         return SHOTTYPE_HITWORLD;
319 }
320
321 void CSQC_common_hud(void);
322
323 void CSQC_kh_hud(void);
324 void CSQC_ctf_hud(void);
325 void PostInit(void);
326 void CSQC_Demo_Camera();
327 float Sbar_WouldDrawScoreboard ();
328 float view_set;
329 float camera_mode;
330 string NextFrameCommand;
331 void CSQC_spider_HUD();
332 void CSQC_UpdateView(float w, float h)
333 {
334         entity e;
335         float fov;
336         float f, i, j;
337         vector v;
338
339         dprint_load();
340         WaypointSprite_Load();
341
342         if(spectatee_status)
343                 myteam = GetPlayerColor(spectatee_status - 1);
344         else
345                 myteam = GetPlayerColor(player_localentnum - 1);
346
347         ticrate = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
348
349         // Render the Scene
350         if(!intermission || !view_set)
351         {
352                 view_origin = pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT);
353                 view_angles = input_angles;
354                 makevectors(view_angles);
355                 view_forward = v_forward;
356                 view_right = v_right;
357                 view_up = v_up;
358                 view_set = 1;
359         }
360
361         vid_width = w;
362         vid_height = h;
363
364 #ifdef BLURTEST
365         if(time > blurtest_time0 && time < blurtest_time1)
366         {
367                 float r, t;
368
369                 t = (time - blurtest_time0) / (blurtest_time1 - blurtest_time0);
370                 r = t * blurtest_radius;
371                 f = 1 / pow(t, blurtest_power) - 1;
372
373                 cvar_set("r_glsl_postprocess", "1");
374                 cvar_set("r_glsl_postprocess_uservec1", strcat(ftos(r), " ", ftos(f), " 0 0"));
375         }
376         else
377         {
378                 cvar_set("r_glsl_postprocess", "0");
379                 cvar_set("r_glsl_postprocess_uservec1", "0 0 0 0");
380         }
381 #endif
382
383         Fog_Force();
384
385         drawframetime = max(0.000001, time - drawtime);
386         drawtime = time;
387
388         // watch for gametype changes here...
389         // in ParseStuffCMD the cmd isn't executed yet :/
390         // might even be better to add the gametype to TE_CSQC_INIT...?
391         if(!postinit)
392                 PostInit();
393
394         if(intermission && !isdemo() && !(calledhooks & HOOK_END))
395         if(calledhooks & HOOK_START)
396         {
397                 localcmd("\ncl_hook_gameend;");
398                 calledhooks |= HOOK_END;
399         }
400
401         CheckForGamestartChange();
402         maptimeAnnouncer();
403
404         fov = cvar("fov");
405         if(button_zoom || fov <= 59.5)
406         {
407                 if(!zoomscript_caught)
408                 {
409                         localcmd("+button4\n");
410                         zoomscript_caught = 1;
411                         ignore_plus_zoom += 1;
412                 }
413         }
414         else
415         {
416                 if(zoomscript_caught)
417                 {
418                         localcmd("-button4\n");
419                         zoomscript_caught = 0;
420                         ignore_minus_zoom += 1;
421                 }
422         }
423
424         sbar_alpha_fg = cvar("sbar_alpha_fg" ) * (1 - cvar("_menu_alpha"));
425         sbar_currentammo = cvar("sbar_showcurrentammo");
426         sbar_hudselector = cvar("sbar_hudselector");
427         sbar_hud_accuracy = cvar("sbar_hud_accuracy");
428         ColorTranslateMode = cvar("cl_stripcolorcodes");
429         activeweapon = getstati(STAT_SWITCHWEAPON);
430         f = cvar("teamplay");
431         if(f != teamplay)
432         {
433                 teamplay = f;
434                 Sbar_InitScores();
435         }
436
437         if(last_weapon != activeweapon) {
438                 weapontime = time;
439                 last_weapon = activeweapon;
440         }
441
442         // ALWAYS Clear Current Scene First
443         R_ClearScene();
444
445         // Assign Standard Viewflags
446         // Draw the World (and sky)
447         R_SetView(VF_DRAWWORLD, 1);
448
449         // Set the console size vars
450         vid_conwidth = cvar("vid_conwidth");
451         vid_conheight = cvar("vid_conheight");
452         vid_pixelheight = cvar("vid_pixelheight");
453
454         R_SetView(VF_FOV, GetCurrentFov(fov));
455
456         // Camera for demo playback
457         if(camera_active)
458         {
459                 if(cvar("camera_enable"))
460                         CSQC_Demo_Camera();
461                 else
462                 {
463                         cvar_set("chase_active", ftos(chase_active_backup));
464                         cvar_set("cl_demo_mousegrab", "0");
465                         camera_active = FALSE;
466                 }
467         }
468 #ifdef CAMERATEST
469         else if(cvar("camera_enable"))
470 #else
471         else if(cvar("camera_enable") && isdemo())
472 #endif
473         {
474                 // Enable required Darkplaces cvars
475                 chase_active_backup = cvar("chase_active");
476                 cvar_set("chase_active", "2");
477                 cvar_set("cl_demo_mousegrab", "1");
478                 camera_active = TRUE;
479                 camera_mode = FALSE;
480         }
481
482         // Draw the Crosshair
483         float scoreboard_active;
484         scoreboard_active = Sbar_WouldDrawScoreboard();
485         R_SetView(VF_DRAWCROSSHAIR, 0); //Make sure engine crosshairs are always hidden
486
487         // Draw the Engine Status Bar (the default Quake HUD)
488         R_SetView(VF_DRAWENGINESBAR, 0);
489
490         // fetch this one only once per frame
491         sbar_showbinds = cvar("sbar_showbinds");
492         sbar_showbinds_limit = cvar("sbar_showbinds_limit");
493
494         // Update the mouse position
495         /*
496         mousepos_x = vid_conwidth;
497         mousepos_y = vid_conheight;
498         mousepos = mousepos*0.5 + getmousepos();
499         */
500
501         e = self;
502         for(self = world; (self = nextent(self)); )
503                 if(self.draw)
504                         self.draw();
505         self = e;
506
507         R_AddEntities(MASK_NORMAL | MASK_ENGINE | MASK_ENGINEVIEWMODELS);
508         R_RenderScene();
509
510         // now switch to 2D drawing mode by calling a 2D drawing function
511         // then polygon drawing will draw as 2D stuff, and NOT get queued until the
512         // next R_RenderScene call
513         drawstring('0 0 0', "", '1 1 0', '1 1 1', 0, 0);
514
515         // Draw the mouse cursor
516         // NOTE: drawpic must happen after R_RenderScene for some reason
517         //drawpic(getmousepos(), "gfx/cursor.tga", '11 14 0', '1 1 1', 1, 0);
518         //drawstring('50 50', ftos(game), '10 10 0', '1 1 1', 1, 0);
519         //self = edict_num(player_localnum);
520         //drawstring('0 0', vtos(pmove_org), '8 8 0', '1 1 1', 1, 0);
521         //drawstring('0 8', strcat("ORG: ", vtos(self.origin), " state: ", ftos(self.ctf_state), " HP: ", ftos(self.health)), '8 8 0', '1 1 1', 1, 0);
522         // as long as the ctf part isn't in, this is useless
523         if(menu_visible)
524                 menu_show();
525
526         /*if(gametype == GAME_CTF)
527         {
528                 ctf_view();
529         } else */
530
531         // draw 2D entities
532         e = self;
533         for(self = world; (self = nextent(self)); )
534                 if(self.draw2d)
535                         self.draw2d();
536         self = e;
537
538         // draw radar
539         if(
540                 ons_showmap
541                 ||
542                 (
543                         !scoreboard_active
544                         &&
545                         cvar_string("cl_teamradar") != "0"
546                         &&
547                         (
548                                 cvar("cl_teamradar") == 2
549                                 ||
550                                 teamplay
551                         )
552                 )
553         )
554                 teamradar_view();
555
556         // draw sbar
557         if(cvar("r_letterbox") == 0) {
558                 if (cvar("cl_showpressedkeys")) { // draw pressed keys when spectating and playing
559                         if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2)
560                                 Sbar_DrawPressedKeys();
561                 }
562                 
563                 if (cvar("cl_showspeed"))
564                         Sbar_ShowSpeed();
565                 if (cvar("cl_showacceleration"))
566                         Sbar_ShowAcceleration();
567                 
568                 Sbar_DrawCenterPrint(); // draw centerprint messages even if viewsize >= 120
569         }
570
571     float hud;
572     hud = getstati(STAT_HUD);
573     if(hud == HUD_SPIDERBOT)
574     {
575         vector sz;
576         CSQC_spider_HUD();
577         sz = drawgetimagesize(SPIDER_CROSS);
578         sz_x *= cvar_or("cl_vehicle_spiderbot_cross_size",1);
579         sz_y *= cvar_or("cl_vehicle_spiderbot_cross_size",1);
580         drawpic('0.5 0 0' * (vid_conwidth - sz_x) + '0 0.5 0' * (vid_conheight - sz_y), SPIDER_CROSS, sz, '1 1 1', cvar_or("cl_vehicle_spiderbot_cross_alpha",0.6), DRAWFLAG_NORMAL);
581     }
582     else
583     {
584         if(cvar("r_letterbox") == 0)
585             if(cvar("viewsize") < 120)
586                 CSQC_common_hud();
587
588         // crosshair goes VERY LAST
589         if(!scoreboard_active && !ons_showmap && !camera_active) {
590                         // TrueAim check
591                         float shottype;
592                         float bullets, ring_scale;
593                         // wcross_origin = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
594                         wcross_origin = project_3d_to_2d(view_origin + MAX_SHOT_DISTANCE * view_forward);
595                         wcross_origin_z = 0;
596             if(cvar("crosshair_hittest"))
597                         {
598                                 vector wcross_oldorigin;
599                                 wcross_oldorigin = wcross_origin;
600                 shottype = TrueAimCheck();
601                                 if(shottype == SHOTTYPE_HITWORLD)
602                                 {
603                                         v = wcross_origin - wcross_oldorigin;
604                                         v_x /= vid_conwidth;
605                                         v_y /= vid_conheight;
606                                         if(vlen(v) > 0.01)
607                                                 shottype = SHOTTYPE_HITOBSTRUCTION;
608                                 }
609                                 if(!cvar("crosshair_hittest_showimpact"))
610                                         wcross_origin = wcross_oldorigin;
611                         }
612             else
613                 shottype = SHOTTYPE_HITWORLD;
614
615             string wcross_style;
616             wcross_style = cvar_string("crosshair");
617
618             if (wcross_style != "0") {
619                 vector wcross_color, wcross_size;
620                 string wcross_wep, wcross_name;
621                 float wcross_alpha_initial, wcross_alpha_old, wcross_sizefloat_initial;
622
623                 wcross_color_x = cvar("crosshair_color_red");
624                 wcross_color_y = cvar("crosshair_color_green");
625                 wcross_color_z = cvar("crosshair_color_blue");
626                 wcross_alpha_initial = cvar("crosshair_color_alpha") * (1-cvar("_menu_alpha"));
627                 wcross_sizefloat_initial = cvar("crosshair_size") * (1-cvar("_menu_alpha"));
628                 if (cvar("crosshair_per_weapon")) {
629                     e = get_weaponinfo(activeweapon);
630                     if (e && e.netname != "")
631                     {
632                         wcross_wep = e.netname;
633                         wcross_style = cvar_string(strcat("crosshair_", wcross_wep));
634                         if(wcross_style == "")
635                             wcross_style = e.netname;
636
637                         if(!cvar("crosshair_color_override"))
638                         {
639                             wcross_color_x = cvar(strcat("crosshair_", wcross_wep, "_color_red"));
640                             wcross_color_y = cvar(strcat("crosshair_", wcross_wep, "_color_green"));
641                             wcross_color_z = cvar(strcat("crosshair_", wcross_wep, "_color_blue"));
642                         }
643
644                         wcross_alpha_initial *= cvar(strcat("crosshair_", wcross_wep, "_color_alpha"));
645                         wcross_sizefloat_initial *= cvar(strcat("crosshair_", wcross_wep, "_size"));
646                     }
647                 }
648
649                 wcross_name = strcat("gfx/crosshair", wcross_style);
650
651                                 if(shottype == SHOTTYPE_HITENEMY)
652                                         wcross_sizefloat_initial *= cvar("crosshair_hittest"); // is not queried if hittest is 0
653                                 if(shottype == SHOTTYPE_HITTEAM)
654                                         wcross_sizefloat_initial /= cvar("crosshair_hittest"); // is not queried if hittest is 0
655
656                                 if(cvar("crosshair_effect_speed") <= 0 || 1-cvar("_menu_alpha") != 1)
657                                         wcross_sizefloat = wcross_sizefloat_initial;
658                                 else if(wcross_sizefloat < (wcross_sizefloat_initial - cvar("crosshair_effect_speed")))
659                                         wcross_sizefloat += cvar("crosshair_effect_speed");
660                                 else if(wcross_sizefloat > (wcross_sizefloat_initial + cvar("crosshair_effect_speed")))
661                                         wcross_sizefloat -= cvar("crosshair_effect_speed");
662                                 else
663                                         wcross_sizefloat = wcross_sizefloat_initial;
664
665                 wcross_size = drawgetimagesize(wcross_name);
666                 wcross_size_x *= wcross_sizefloat;
667                 wcross_size_y *= wcross_sizefloat;
668                                 
669                                 // ring around crosshair representing bullets left in camping rifle clip
670                                 if (activeweapon == WEP_CAMPINGRIFLE)
671                                 {
672                                         ring_scale = cvar("crosshair_campingrifle_ring_size");
673                                         bullets = bound(0, getstati(STAT_BULLETS_LOADED), 8);
674                                 }
675                                 else
676                                         bullets = 0;
677                                 
678                 if(shottype == SHOTTYPE_HITTEAM || (shottype == SHOTTYPE_HITOBSTRUCTION && cvar("crosshair_hittest_blur") && !cvar("chase_active")))
679                 {
680                                         wcross_alpha_old = wcross_alpha;
681                     wcross_alpha = wcross_alpha_initial * 0.04 * 0.75;
682                     for(i = -2; i <= 2; ++i)
683                         for(j = -2; j <= 2; ++j)
684                                                 {
685                                                         if (bullets)
686                                                                 drawpic(wcross_origin - '0.5 0 0' * (wcross_size_x * ring_scale + i) - '0 0.5 0' * (wcross_size_y * ring_scale + j), strcat("gfx/hud/rifle_ring_", ftos(bullets)), wcross_size * ring_scale, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
687                             drawpic(wcross_origin - '0.5 0 0' * (wcross_size_x + i) - '0 0.5 0' * (wcross_size_y + j), wcross_name, wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
688                                                 }
689                                         wcross_alpha = wcross_alpha_old;
690                 }
691                 else
692                 {
693                                         if(cvar("crosshair_effect_speed") <= 0 || 1-cvar("_menu_alpha") != 1)
694                                                 wcross_alpha = wcross_alpha_initial;
695                                         else if(wcross_alpha < (wcross_alpha_initial - cvar("crosshair_effect_speed") * 1.5))
696                                                 wcross_alpha += cvar("crosshair_effect_speed") * 1.5;
697                                         else if(wcross_alpha > (wcross_alpha_initial + cvar("crosshair_effect_speed") * 1.5))
698                                                 wcross_alpha -= cvar("crosshair_effect_speed") * 1.5;
699                                         else
700                                                 wcross_alpha = wcross_alpha_initial;
701                                         if (bullets)
702                                                 drawpic(wcross_origin - '0.5 0 0' * (wcross_size_x * ring_scale) - '0 0.5 0' * (wcross_size_y * ring_scale), strcat("gfx/hud/rifle_ring_", ftos(bullets)), wcross_size * ring_scale, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
703                     drawpic(wcross_origin - '0.5 0 0' * (wcross_size_x) - '0 0.5 0' * ( wcross_size_y), wcross_name, wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
704                 }
705             }
706         }
707     }
708
709         if(NextFrameCommand)
710         {
711                 localcmd("\n", NextFrameCommand, "\n");
712                 NextFrameCommand = string_null;
713         }
714
715         // we must do this check AFTER a frame was rendered, or it won't work
716         if(cs_project_is_b0rked == 0)
717         {
718                 string w0, h0;
719                 w0 = cvar_string("vid_conwidth");
720                 h0 = cvar_string("vid_conheight");
721                 //R_SetView(VF_VIEWPORT, '0 0 0', '640 480 0');
722                 //R_SetView(VF_FOV, '90 90 0');
723                 R_SetView(VF_ORIGIN, '0 0 0');
724                 R_SetView(VF_ANGLES, '0 0 0');
725                 R_SetView(VF_PERSPECTIVE, 1);
726                 makevectors('0 0 0');
727                 vector v1, v2;
728                 cvar_set("vid_conwidth", "800");
729                 cvar_set("vid_conheight", "600");
730                 v1 = cs_project(v_forward);
731                 cvar_set("vid_conwidth", "640");
732                 cvar_set("vid_conheight", "480");
733                 v2 = cs_project(v_forward);
734                 if(v1 == v2)
735                         cs_project_is_b0rked = 1;
736                 else
737                         cs_project_is_b0rked = -1;
738                 cvar_set("vid_conwidth", w0);
739                 cvar_set("vid_conheight", h0);
740         }
741
742         // be safe against triggerbots until everyone has the fixed engine
743         // this call is meant to overwrite the trace globals by something
744         // unsuspicious
745         traceline('0 0 0', '0 0 0', MOVE_WORLDONLY, world);
746 }
747
748 void Sbar_Draw();
749 void CSQC_spider_HUD()
750 {
751     float rockets,reload,heat,hp,shield,i;
752     vector p,pp;
753
754     rockets = getstati(STAT_SPIDERBOT_ROCKETS);
755     heat    = min(getstatf(STAT_SPIDERBOT_HEAT),1);
756     reload  = min(getstatf(STAT_SPIDERBOT_RELOAD),1);
757     hp      = min(getstatf(STAT_SPIDERBOT_HEALTH),1);
758     shield  = min(getstatf(STAT_SPIDERBOT_SHIELD),1);
759
760     // Draw health bar
761     p = '0.5 0 0' * (vid_conwidth - (rkt_size * 8));
762     p = p + '0 1 0' * vid_conheight - '0 32 0';
763     //pp = ('0 1 0' * hp) + ('1 0 0' * (1-hp));
764     drawfill(p, '256 0 0' * shield + '0 8 0' , '0.5 0.5 1', 0.75, DRAWFLAG_NORMAL);
765     p_y += 8;
766     drawfill(p, '256 0 0' * hp + '0 8 0' , '0 1 0', 0.75, DRAWFLAG_NORMAL);
767     p_x += 256 * hp;
768     drawfill(p, '256 0 0' * (1-hp) + '0 8 0' , '0 0 0', 0.75, DRAWFLAG_NORMAL);
769
770     // Draw minigun heat indicator
771     p = '0.5 0 0' * (vid_conwidth - 256);
772     p = p + '0 1 0' * vid_conheight - '0 34  0';
773     drawfill(p, '256 0 0' * (1-heat) + '0 2 0' ,'0 0 1', 0.5, DRAWFLAG_NORMAL);
774     p_x += 256 * (1-heat);
775     drawfill(p, '256 0 0' * heat  + '0 2 0' , '1 0 0', 0.5, DRAWFLAG_NORMAL);
776
777     // Draw rocket icons for loaded/empty tubes.
778     pp = '0.5 0 0' * (vid_conwidth - (rkt_size * 8));
779     pp += '0 1 0' * vid_conheight - '0 64 0';
780     for(i = 0; i < 8; ++i)
781     {
782         p = pp + '1 0 0' * (rkt_size * i);
783         if(rockets == 8)
784         {
785             if(floor(reload * 8) == i)
786             {
787                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '1 0 0' + '0 1 0' * ((reload*8)-i), 0.75 , DRAWFLAG_NORMAL);
788             }
789             else if(i < reload * 8)
790                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '1 1 0', 0.75 , DRAWFLAG_NORMAL);
791             else
792                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0.5 0.5 0.5', 0.75, DRAWFLAG_NORMAL);
793         }
794         else
795         {
796             if(i < rockets)
797                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0 0 0', 0.25, DRAWFLAG_NORMAL);
798             else
799                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0 1 0' * reload, 0.75, DRAWFLAG_NORMAL);
800         }
801     }
802
803         if (sb_showscores)
804         {
805                 Sbar_DrawScoreboard();
806                 Sbar_DrawCenterPrint();
807         }
808
809 }
810 void CSQC_common_hud(void)
811 {
812
813         // Sbar_SortFrags(); done in Sbar_Draw
814     float hud;
815     hud = getstati(STAT_HUD);
816
817     //hud = 10;
818     switch(hud)
819     {
820         case HUD_NORMAL:
821             Sbar_Draw();
822             break;
823
824         case HUD_SPIDERBOT:
825             CSQC_spider_HUD();
826             break;
827     }
828
829         if (getstati(STAT_HEALTH) <= 0)
830                 wcross_sizefloat = wcross_alpha = 0;
831 }
832
833
834 // following vectors must be global to allow seamless switching between camera modes
835 vector camera_offset, current_camera_offset, mouse_angles, current_angles, current_origin, current_position;
836 void CSQC_Demo_Camera()
837 {
838         float speed, attenuation, dimensions;
839         vector tmp, delta;
840
841         if( cvar("camera_reset") || !camera_mode )
842         {
843                 camera_offset = '0 0 0';
844                 current_angles = '0 0 0';
845                 camera_direction = '0 0 0';
846                 camera_offset_z += 30;
847                 camera_offset_x += 30 * -cos(current_angles_y * DEG2RAD);
848                 camera_offset_y += 30 * -sin(current_angles_y * DEG2RAD);
849                 current_origin = view_origin;
850                 current_camera_offset  = camera_offset;
851                 cvar_set("camera_reset", "0");
852                 camera_mode = CAMERA_CHASE;
853         }
854
855         // Camera angles
856         if( camera_roll )
857                 mouse_angles_z += camera_roll * cvar("camera_speed_roll");
858
859         if(cvar("camera_look_player"))
860         {
861                 local vector dir;
862                 local float n;
863
864                 dir = normalize(view_origin - current_position);
865                 n = mouse_angles_z;
866                 mouse_angles = vectoangles(dir);
867                 mouse_angles_x = mouse_angles_x * -1;
868                 mouse_angles_z = n;
869         }
870         else
871         {
872                 tmp = getmousepos() * 0.1;
873                 if(vlen(tmp)>cvar("camera_mouse_treshold"))
874                 {
875                         mouse_angles_x += tmp_y * cos(mouse_angles_z * DEG2RAD) + (tmp_x * sin(mouse_angles_z * DEG2RAD));
876                         mouse_angles_y -= tmp_x * cos(mouse_angles_z * DEG2RAD) + (tmp_y * -sin(mouse_angles_z * DEG2RAD));
877                 }
878         }
879
880         while (mouse_angles_x < -180) mouse_angles_x = mouse_angles_x + 360;
881         while (mouse_angles_x > 180) mouse_angles_x = mouse_angles_x - 360;
882         while (mouse_angles_y < -180) mouse_angles_y = mouse_angles_y + 360;
883         while (mouse_angles_y > 180) mouse_angles_y = mouse_angles_y - 360;
884
885         // Fix difference when angles don't have the same sign
886         delta = '0 0 0';
887         if(mouse_angles_y < -60 && current_angles_y > 60)
888                 delta = '0 360 0';
889         if(mouse_angles_y > 60 && current_angles_y < -60)
890                 delta = '0 -360 0';
891
892         if(cvar("camera_look_player"))
893                 attenuation = cvar("camera_look_attenuation");
894         else
895                 attenuation = cvar("camera_speed_attenuation");
896
897         attenuation = 1 / max(1, attenuation);
898         current_angles += (mouse_angles - current_angles + delta) * attenuation;
899
900         while (current_angles_x < -180) current_angles_x = current_angles_x + 360;
901         while (current_angles_x > 180) current_angles_x = current_angles_x - 360;
902         while (current_angles_y < -180) current_angles_y = current_angles_y + 360;
903         while (current_angles_y > 180) current_angles_y = current_angles_y - 360;
904
905         // Camera position
906         tmp = '0 0 0';
907         dimensions = 0;
908
909         if( camera_direction_x )
910         {
911                 tmp_x = camera_direction_x * cos(current_angles_y * DEG2RAD);
912                 tmp_y = camera_direction_x * sin(current_angles_y * DEG2RAD);
913                 if( cvar("camera_forward_follows") && !cvar("camera_look_player") )
914                         tmp_z = camera_direction_x * -sin(current_angles_x * DEG2RAD);
915                 ++dimensions;
916         }
917
918         if( camera_direction_y )
919         {
920                 tmp_x += camera_direction_y * -sin(current_angles_y * DEG2RAD);
921                 tmp_y += camera_direction_y * cos(current_angles_y * DEG2RAD) * cos(current_angles_z * DEG2RAD);
922                 tmp_z += camera_direction_y * sin(current_angles_z * DEG2RAD);
923                 ++dimensions;
924         }
925
926         if( camera_direction_z )
927         {
928                 tmp_z += camera_direction_z * cos(current_angles_z * DEG2RAD);
929                 ++dimensions;
930         }
931
932         if(cvar("camera_free"))
933                 speed = cvar("camera_speed_free");
934         else
935                 speed = cvar("camera_speed_chase");
936
937         if(dimensions)
938         {
939                 speed = speed * sqrt(1 / dimensions);
940                 camera_offset += tmp * speed;
941         }
942
943         current_camera_offset += (camera_offset - current_camera_offset) * attenuation;
944
945         // Camera modes
946         if( cvar("camera_free") )
947         {
948                 if ( camera_mode == CAMERA_CHASE )
949                 {
950                         current_camera_offset = current_origin + current_camera_offset;
951                         camera_offset = current_origin + camera_offset;
952                 }
953
954                 camera_mode = CAMERA_FREE;
955                 current_position = current_camera_offset;
956         }
957         else
958         {
959                 if ( camera_mode == CAMERA_FREE )
960                 {
961                         current_origin = view_origin;
962                         camera_offset = camera_offset - current_origin;
963                         current_camera_offset = current_camera_offset - current_origin;
964                 }
965
966                 camera_mode = CAMERA_CHASE;
967
968                 if(cvar("camera_chase_smoothly"))
969                         current_origin += (view_origin - current_origin) * attenuation;
970                 else
971                         current_origin = view_origin;
972
973                 current_position = current_origin + current_camera_offset;
974         }
975
976         R_SetView(VF_ANGLES, current_angles);
977         R_SetView(VF_ORIGIN, current_position);
978 }