]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/View.qc
sync crosshair effects to weapon switch delay
[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_prev, wcross_alpha_prev, wcross_blur_prev;
212 float wcross_sizefloat_goal_prev, wcross_alpha_goal_prev, wcross_blur_goal_prev;
213 float wcross_lastchangetime;
214 entity trueaim;
215 entity trueaim_rifle;
216
217 #define SHOTTYPE_HITTEAM 1
218 #define SHOTTYPE_HITOBSTRUCTION 2
219 #define SHOTTYPE_HITWORLD 3
220 #define SHOTTYPE_HITENEMY 4
221
222 void TrueAim_Init()
223 {
224         trueaim = spawn();
225         trueaim.classname = "trueaim";
226         trueaim.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
227         trueaim_rifle = spawn();
228         trueaim_rifle.classname = "trueaim_rifle";
229         trueaim_rifle.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
230 }
231
232 float EnemyHitCheck()
233 {
234         float t;
235         wcross_origin = project_3d_to_2d(trace_endpos);
236         wcross_origin_z = 0;
237         if(trace_networkentity < 1)
238                 return SHOTTYPE_HITWORLD;
239         if(trace_networkentity > maxclients)
240                 return SHOTTYPE_HITWORLD;
241         t = GetPlayerColor(trace_networkentity - 1);
242         if(teamplay)
243                 if(t == myteam)
244                         return SHOTTYPE_HITTEAM;
245         if(t == COLOR_SPECTATOR)
246                 return SHOTTYPE_HITWORLD;
247         return SHOTTYPE_HITENEMY;
248 }
249
250 float TrueAimCheck()
251 {
252         float nudge = 1; // added to traceline target and subtracted from result
253         vector vecs, trueaimpoint, w_shotorg;
254         vector mi, ma, dv;
255         float shottype;
256
257         mi = ma = '0 0 0';
258
259         switch(activeweapon)
260         {
261                 case WEP_TUBA: // no aim
262                 case WEP_PORTO: // shoots from eye
263                 case WEP_HOOK: // no trueaim
264                 case WEP_GRENADE_LAUNCHER: // toss curve
265                         return SHOTTYPE_HITWORLD;
266                 case WEP_CAMPINGRIFLE:
267                         if(zoomscript_caught)
268                         {
269                                 tracebox(view_origin, '0 0 0', '0 0 0', view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, trueaim);
270                                 return EnemyHitCheck();
271                         }
272                         break;
273                 case WEP_ROCKET_LAUNCHER: // projectile has a size!
274                         mi = '-3 -3 -3';
275                         ma = '3 3 3';
276                         break;
277                 case WEP_FIREBALL: // projectile has a size!
278                         mi = '-16 -16 -16';
279                         ma = '16 16 16';
280                         break;
281                 case WEP_ELECTRO: // projectile has a size!
282                         mi = '0 0 -3';
283                         ma = '0 0 -3';
284                         break;
285         }
286
287         vecs = decompressShotOrigin(getstati(STAT_SHOTORG));
288
289         if(activeweapon == WEP_CAMPINGRIFLE)
290                 traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, trueaim_rifle);
291         else
292                 traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, trueaim);
293         trueaimpoint = trace_endpos;
294
295         if(vecs_x > 0)
296                 vecs_y = -vecs_y;
297         else
298                 vecs = '0 0 0';
299
300         dv = view_right * vecs_y + view_up * vecs_z;
301         w_shotorg = view_origin + dv;
302
303         // now move the vecs forward as much as requested if possible
304         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
305         w_shotorg = trace_endpos - view_forward * nudge;
306
307         tracebox(w_shotorg, mi, ma, trueaimpoint, MOVE_NORMAL, trueaim);
308         shottype = EnemyHitCheck();
309         if(shottype != SHOTTYPE_HITWORLD)
310                 return shottype;
311
312 #if 0
313         // FIXME WHY DOES THIS NOT WORK FOR THE ROCKET LAUNCHER?
314         // or rather, I know why, but see no fix
315         if(vlen(trace_endpos - trueaimpoint) > vlen(ma) + vlen(mi) + 1)
316                 // 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
317                 return SHOTTYPE_HITOBSTRUCTION;
318 #endif
319
320         return SHOTTYPE_HITWORLD;
321 }
322
323 void CSQC_common_hud(void);
324
325 void CSQC_kh_hud(void);
326 void CSQC_ctf_hud(void);
327 void PostInit(void);
328 void CSQC_Demo_Camera();
329 float Sbar_WouldDrawScoreboard ();
330 float view_set;
331 float camera_mode;
332 string NextFrameCommand;
333 void CSQC_spider_HUD();
334 void CSQC_UpdateView(float w, float h)
335 {
336         entity e;
337         float fov;
338         float f, i, j;
339         vector v;
340
341         dprint_load();
342         WaypointSprite_Load();
343
344         if(spectatee_status)
345                 myteam = GetPlayerColor(spectatee_status - 1);
346         else
347                 myteam = GetPlayerColor(player_localentnum - 1);
348
349         ticrate = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
350
351         // Render the Scene
352         if(!intermission || !view_set)
353         {
354                 view_origin = pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT);
355                 view_angles = input_angles;
356                 makevectors(view_angles);
357                 view_forward = v_forward;
358                 view_right = v_right;
359                 view_up = v_up;
360                 view_set = 1;
361         }
362
363         vid_width = w;
364         vid_height = h;
365
366 #ifdef BLURTEST
367         if(time > blurtest_time0 && time < blurtest_time1)
368         {
369                 float r, t;
370
371                 t = (time - blurtest_time0) / (blurtest_time1 - blurtest_time0);
372                 r = t * blurtest_radius;
373                 f = 1 / pow(t, blurtest_power) - 1;
374
375                 cvar_set("r_glsl_postprocess", "1");
376                 cvar_set("r_glsl_postprocess_uservec1", strcat(ftos(r), " ", ftos(f), " 0 0"));
377         }
378         else
379         {
380                 cvar_set("r_glsl_postprocess", "0");
381                 cvar_set("r_glsl_postprocess_uservec1", "0 0 0 0");
382         }
383 #endif
384
385         Fog_Force();
386
387         drawframetime = max(0.000001, time - drawtime);
388         drawtime = time;
389
390         // watch for gametype changes here...
391         // in ParseStuffCMD the cmd isn't executed yet :/
392         // might even be better to add the gametype to TE_CSQC_INIT...?
393         if(!postinit)
394                 PostInit();
395
396         if(intermission && !isdemo() && !(calledhooks & HOOK_END))
397                 if(calledhooks & HOOK_START)
398                 {
399                         localcmd("\ncl_hook_gameend;");
400                         calledhooks |= HOOK_END;
401                 }
402
403         CheckForGamestartChange();
404         maptimeAnnouncer();
405
406         fov = cvar("fov");
407         if(button_zoom || fov <= 59.5)
408         {
409                 if(!zoomscript_caught)
410                 {
411                         localcmd("+button4\n");
412                         zoomscript_caught = 1;
413                         ignore_plus_zoom += 1;
414                 }
415         }
416         else
417         {
418                 if(zoomscript_caught)
419                 {
420                         localcmd("-button4\n");
421                         zoomscript_caught = 0;
422                         ignore_minus_zoom += 1;
423                 }
424         }
425
426         sbar_alpha_fg = cvar("sbar_alpha_fg" ) * (1 - cvar("_menu_alpha"));
427         sbar_currentammo = cvar("sbar_showcurrentammo");
428         sbar_hudselector = cvar("sbar_hudselector");
429         sbar_hud_accuracy = cvar("sbar_hud_accuracy");
430         ColorTranslateMode = cvar("cl_stripcolorcodes");
431         activeweapon = getstati(STAT_SWITCHWEAPON);
432         f = cvar("teamplay");
433         if(f != teamplay)
434         {
435                 teamplay = f;
436                 Sbar_InitScores();
437         }
438
439         if(last_weapon != activeweapon) {
440                 weapontime = time;
441                 last_weapon = activeweapon;
442         }
443
444         // ALWAYS Clear Current Scene First
445         R_ClearScene();
446
447         // Assign Standard Viewflags
448         // Draw the World (and sky)
449         R_SetView(VF_DRAWWORLD, 1);
450
451         // Set the console size vars
452         vid_conwidth = cvar("vid_conwidth");
453         vid_conheight = cvar("vid_conheight");
454         vid_pixelheight = cvar("vid_pixelheight");
455
456         R_SetView(VF_FOV, GetCurrentFov(fov));
457
458         // Camera for demo playback
459         if(camera_active)
460         {
461                 if(cvar("camera_enable"))
462                         CSQC_Demo_Camera();
463                 else
464                 {
465                         cvar_set("chase_active", ftos(chase_active_backup));
466                         cvar_set("cl_demo_mousegrab", "0");
467                         camera_active = FALSE;
468                 }
469         }
470 #ifdef CAMERATEST
471         else if(cvar("camera_enable"))
472 #else
473         else if(cvar("camera_enable") && isdemo())
474 #endif
475         {
476                 // Enable required Darkplaces cvars
477                 chase_active_backup = cvar("chase_active");
478                 cvar_set("chase_active", "2");
479                 cvar_set("cl_demo_mousegrab", "1");
480                 camera_active = TRUE;
481                 camera_mode = FALSE;
482         }
483
484         // Draw the Crosshair
485         float scoreboard_active;
486         scoreboard_active = Sbar_WouldDrawScoreboard();
487         R_SetView(VF_DRAWCROSSHAIR, 0); //Make sure engine crosshairs are always hidden
488
489         // Draw the Engine Status Bar (the default Quake HUD)
490         R_SetView(VF_DRAWENGINESBAR, 0);
491
492         // fetch this one only once per frame
493         sbar_showbinds = cvar("sbar_showbinds");
494         sbar_showbinds_limit = cvar("sbar_showbinds_limit");
495
496         // Update the mouse position
497         /*
498            mousepos_x = vid_conwidth;
499            mousepos_y = vid_conheight;
500            mousepos = mousepos*0.5 + getmousepos();
501          */
502
503         e = self;
504         for(self = world; (self = nextent(self)); )
505                 if(self.draw)
506                         self.draw();
507         self = e;
508
509         R_AddEntities(MASK_NORMAL | MASK_ENGINE | MASK_ENGINEVIEWMODELS);
510         R_RenderScene();
511
512         // now switch to 2D drawing mode by calling a 2D drawing function
513         // then polygon drawing will draw as 2D stuff, and NOT get queued until the
514         // next R_RenderScene call
515         drawstring('0 0 0', "", '1 1 0', '1 1 1', 0, 0);
516
517         // Draw the mouse cursor
518         // NOTE: drawpic must happen after R_RenderScene for some reason
519         //drawpic(getmousepos(), "gfx/cursor.tga", '11 14 0', '1 1 1', 1, 0);
520         //drawstring('50 50', ftos(game), '10 10 0', '1 1 1', 1, 0);
521         //self = edict_num(player_localnum);
522         //drawstring('0 0', vtos(pmove_org), '8 8 0', '1 1 1', 1, 0);
523         //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);
524         // as long as the ctf part isn't in, this is useless
525         if(menu_visible)
526                 menu_show();
527
528         /*if(gametype == GAME_CTF)
529           {
530           ctf_view();
531           } else */
532
533         // draw 2D entities
534         e = self;
535         for(self = world; (self = nextent(self)); )
536                 if(self.draw2d)
537                         self.draw2d();
538         self = e;
539
540         // draw radar
541         if(
542                         ons_showmap
543                         ||
544                         (
545                          !scoreboard_active
546                          &&
547                          cvar_string("cl_teamradar") != "0"
548                          &&
549                          (
550                           cvar("cl_teamradar") == 2
551                           ||
552                           teamplay
553                          )
554                         )
555           )
556                 teamradar_view();
557
558         // draw sbar
559         if(cvar("r_letterbox") == 0) {
560                 if (cvar("cl_showpressedkeys")) { // draw pressed keys when spectating and playing
561                         if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2)
562                                 Sbar_DrawPressedKeys();
563                 }
564
565                 if (cvar("cl_showspeed"))
566                         Sbar_ShowSpeed();
567                 if (cvar("cl_showacceleration"))
568                         Sbar_ShowAcceleration();
569
570                 Sbar_DrawCenterPrint(); // draw centerprint messages even if viewsize >= 120
571         }
572
573         float hud;
574         hud = getstati(STAT_HUD);
575         if(hud == HUD_SPIDERBOT)
576         {
577                 vector sz;
578                 CSQC_spider_HUD();
579                 sz = drawgetimagesize(SPIDER_CROSS);
580                 sz_x *= cvar_or("cl_vehicle_spiderbot_cross_size",1);
581                 sz_y *= cvar_or("cl_vehicle_spiderbot_cross_size",1);
582                 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);
583         }
584         else
585         {
586                 if(cvar("r_letterbox") == 0)
587                         if(cvar("viewsize") < 120)
588                                 CSQC_common_hud();
589
590                 // crosshair goes VERY LAST
591                 if(!scoreboard_active && !ons_showmap && !camera_active) {
592                         // TrueAim check
593                         float shottype;
594                         float bullets, ring_scale;
595                         // wcross_origin = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
596                         wcross_origin = project_3d_to_2d(view_origin + MAX_SHOT_DISTANCE * view_forward);
597                         wcross_origin_z = 0;
598                         if(cvar("crosshair_hittest"))
599                         {
600                                 vector wcross_oldorigin;
601                                 wcross_oldorigin = wcross_origin;
602                                 shottype = TrueAimCheck();
603                                 if(shottype == SHOTTYPE_HITWORLD)
604                                 {
605                                         v = wcross_origin - wcross_oldorigin;
606                                         v_x /= vid_conwidth;
607                                         v_y /= vid_conheight;
608                                         if(vlen(v) > 0.01)
609                                                 shottype = SHOTTYPE_HITOBSTRUCTION;
610                                 }
611                                 if(!cvar("crosshair_hittest_showimpact"))
612                                         wcross_origin = wcross_oldorigin;
613                         }
614                         else
615                                 shottype = SHOTTYPE_HITWORLD;
616
617                         string wcross_style;
618                         wcross_style = cvar_string("crosshair");
619
620                         if (wcross_style != "0") {
621                                 vector wcross_color, wcross_size;
622                                 string wcross_wep, wcross_name;
623                                 float wcross_alpha, wcross_sizefloat, wcross_blur;
624
625                                 wcross_color_x = cvar("crosshair_color_red");
626                                 wcross_color_y = cvar("crosshair_color_green");
627                                 wcross_color_z = cvar("crosshair_color_blue");
628                                 wcross_alpha = cvar("crosshair_color_alpha");
629                                 wcross_sizefloat = cvar("crosshair_size");
630                                 if (cvar("crosshair_per_weapon")) {
631                                         e = get_weaponinfo(activeweapon);
632                                         if (e && e.netname != "")
633                                         {
634                                                 wcross_wep = e.netname;
635                                                 wcross_style = cvar_string(strcat("crosshair_", wcross_wep));
636                                                 if(wcross_style == "")
637                                                         wcross_style = e.netname;
638
639                                                 if(!cvar("crosshair_color_override"))
640                                                 {
641                                                         wcross_color_x = cvar(strcat("crosshair_", wcross_wep, "_color_red"));
642                                                         wcross_color_y = cvar(strcat("crosshair_", wcross_wep, "_color_green"));
643                                                         wcross_color_z = cvar(strcat("crosshair_", wcross_wep, "_color_blue"));
644                                                 }
645
646                                                 wcross_alpha *= cvar(strcat("crosshair_", wcross_wep, "_color_alpha"));
647                                                 wcross_sizefloat *= cvar(strcat("crosshair_", wcross_wep, "_size"));
648                                         }
649                                 }
650
651                                 wcross_name = strcat("gfx/crosshair", wcross_style);
652
653                                 if(shottype == SHOTTYPE_HITENEMY)
654                                         wcross_sizefloat *= cvar("crosshair_hittest"); // is not queried if hittest is 0
655                                 if(shottype == SHOTTYPE_HITTEAM)
656                                         wcross_sizefloat /= cvar("crosshair_hittest"); // is not queried if hittest is 0
657                                 if(shottype == SHOTTYPE_HITTEAM || (shottype == SHOTTYPE_HITOBSTRUCTION && cvar("crosshair_hittest_blur") && !cvar("chase_active")))
658                                 {
659                                         wcross_alpha *= 0.75;
660                                         wcross_blur = 1;
661                                 }
662                                 else
663                                         wcross_blur = 0;
664
665                                 if(wcross_blur != wcross_blur_goal_prev || wcross_sizefloat != wcross_sizefloat_goal_prev || wcross_alpha != wcross_alpha_goal_prev)
666                                 {
667                                         f = cvar("crosshair_effect_speed");
668                                         if(f < 0)
669                                                 wcross_lastchangetime = time - f * 2 * g_weaponswitchdelay;
670                                         else
671                                                 wcross_lastchangetime = time + f;
672                                 }
673
674                                 wcross_sizefloat_goal_prev = wcross_sizefloat;
675                                 wcross_alpha_goal_prev = wcross_alpha;
676                                 wcross_blur_goal_prev = wcross_blur;
677
678                                 // *_prev is at time-frametime
679                                 // * is at wcross_lastchangetime+f
680                                 // what do we have at time?
681                                 if(time < wcross_lastchangetime)
682                                 {
683                                         f = frametime / (wcross_lastchangetime - time + frametime);
684                                         wcross_sizefloat = f * wcross_sizefloat + (1 - f) * wcross_sizefloat_prev;
685                                         wcross_alpha = f * wcross_alpha + (1 - f) * wcross_alpha_prev;
686                                         //wcross_blur = (frametime / f) * wcross_blur + (1 - frametime / f) * wcross_blur_prev;
687                                 }
688
689                                 wcross_sizefloat_prev = wcross_sizefloat;
690                                 wcross_alpha_prev = wcross_alpha;
691                                 wcross_blur_prev = wcross_blur;
692
693                                 wcross_sizefloat *= 1 - cvar("_menu_alpha");
694                                 wcross_alpha *= 1 - cvar("_menu_alpha");
695
696                                 wcross_size = drawgetimagesize(wcross_name);
697                                 wcross_size_x *= wcross_sizefloat;
698                                 wcross_size_y *= wcross_sizefloat;
699
700                                 // ring around crosshair representing bullets left in camping rifle clip
701                                 if (activeweapon == WEP_CAMPINGRIFLE)
702                                 {
703                                         ring_scale = cvar("crosshair_campingrifle_ring_size");
704                                         bullets = bound(0, getstati(STAT_BULLETS_LOADED), 8);
705                                 }
706                                 else
707                                         bullets = 0;
708
709                                 if(wcross_blur > 0)
710                                 {
711                                         wcross_alpha = wcross_alpha * 0.04;
712                                         for(i = -2; i <= 2; ++i)
713                                                 for(j = -2; j <= 2; ++j)
714                                                 {
715                                                         if (bullets)
716                                                                 drawpic(wcross_origin - ('0.5 0 0' * (wcross_size_x * ring_scale + i * wcross_blur) + '0 0.5 0' * (wcross_size_y * ring_scale + j * wcross_blur)), strcat("gfx/hud/rifle_ring_", ftos(bullets)), wcross_size * ring_scale, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
717                                                         drawpic(wcross_origin - ('0.5 0 0' * (wcross_size_x + i * wcross_blur) + '0 0.5 0' * (wcross_size_y + j * wcross_blur)), wcross_name, wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);
718                                                 }
719                                 }
720                                 else
721                                 {
722                                         if (bullets)
723                                                 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);
724                                         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);
725                                 }
726                         }
727                 }
728                 else
729                 {
730                         wcross_sizefloat_prev = 0;
731                         wcross_alpha_prev = 0;
732                         wcross_blur_prev = 0;
733                         wcross_sizefloat_goal_prev = 0;
734                         wcross_alpha_goal_prev = 0;
735                         wcross_blur_goal_prev = 0;
736                 }
737         }
738
739         if(NextFrameCommand)
740         {
741                 localcmd("\n", NextFrameCommand, "\n");
742                 NextFrameCommand = string_null;
743         }
744
745         // we must do this check AFTER a frame was rendered, or it won't work
746         if(cs_project_is_b0rked == 0)
747         {
748                 string w0, h0;
749                 w0 = cvar_string("vid_conwidth");
750                 h0 = cvar_string("vid_conheight");
751                 //R_SetView(VF_VIEWPORT, '0 0 0', '640 480 0');
752                 //R_SetView(VF_FOV, '90 90 0');
753                 R_SetView(VF_ORIGIN, '0 0 0');
754                 R_SetView(VF_ANGLES, '0 0 0');
755                 R_SetView(VF_PERSPECTIVE, 1);
756                 makevectors('0 0 0');
757                 vector v1, v2;
758                 cvar_set("vid_conwidth", "800");
759                 cvar_set("vid_conheight", "600");
760                 v1 = cs_project(v_forward);
761                 cvar_set("vid_conwidth", "640");
762                 cvar_set("vid_conheight", "480");
763                 v2 = cs_project(v_forward);
764                 if(v1 == v2)
765                         cs_project_is_b0rked = 1;
766                 else
767                         cs_project_is_b0rked = -1;
768                 cvar_set("vid_conwidth", w0);
769                 cvar_set("vid_conheight", h0);
770         }
771
772         // be safe against triggerbots until everyone has the fixed engine
773         // this call is meant to overwrite the trace globals by something
774         // unsuspicious
775         traceline('0 0 0', '0 0 0', MOVE_WORLDONLY, world);
776 }
777
778 void Sbar_Draw();
779 void CSQC_spider_HUD()
780 {
781         float rockets,reload,heat,hp,shield,i;
782         vector p,pp;
783
784         rockets = getstati(STAT_SPIDERBOT_ROCKETS);
785         heat    = min(getstatf(STAT_SPIDERBOT_HEAT),1);
786         reload  = min(getstatf(STAT_SPIDERBOT_RELOAD),1);
787         hp      = min(getstatf(STAT_SPIDERBOT_HEALTH),1);
788         shield  = min(getstatf(STAT_SPIDERBOT_SHIELD),1);
789
790         // Draw health bar
791         p = '0.5 0 0' * (vid_conwidth - (rkt_size * 8));
792         p = p + '0 1 0' * vid_conheight - '0 32 0';
793         //pp = ('0 1 0' * hp) + ('1 0 0' * (1-hp));
794         drawfill(p, '256 0 0' * shield + '0 8 0' , '0.5 0.5 1', 0.75, DRAWFLAG_NORMAL);
795         p_y += 8;
796         drawfill(p, '256 0 0' * hp + '0 8 0' , '0 1 0', 0.75, DRAWFLAG_NORMAL);
797         p_x += 256 * hp;
798         drawfill(p, '256 0 0' * (1-hp) + '0 8 0' , '0 0 0', 0.75, DRAWFLAG_NORMAL);
799
800         // Draw minigun heat indicator
801         p = '0.5 0 0' * (vid_conwidth - 256);
802         p = p + '0 1 0' * vid_conheight - '0 34  0';
803         drawfill(p, '256 0 0' * (1-heat) + '0 2 0' ,'0 0 1', 0.5, DRAWFLAG_NORMAL);
804         p_x += 256 * (1-heat);
805         drawfill(p, '256 0 0' * heat  + '0 2 0' , '1 0 0', 0.5, DRAWFLAG_NORMAL);
806
807         // Draw rocket icons for loaded/empty tubes.
808         pp = '0.5 0 0' * (vid_conwidth - (rkt_size * 8));
809         pp += '0 1 0' * vid_conheight - '0 64 0';
810         for(i = 0; i < 8; ++i)
811         {
812                 p = pp + '1 0 0' * (rkt_size * i);
813                 if(rockets == 8)
814                 {
815                         if(floor(reload * 8) == i)
816                         {
817                                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '1 0 0' + '0 1 0' * ((reload*8)-i), 0.75 , DRAWFLAG_NORMAL);
818                         }
819                         else if(i < reload * 8)
820                                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '1 1 0', 0.75 , DRAWFLAG_NORMAL);
821                         else
822                                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0.5 0.5 0.5', 0.75, DRAWFLAG_NORMAL);
823                 }
824                 else
825                 {
826                         if(i < rockets)
827                                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0 0 0', 0.25, DRAWFLAG_NORMAL);
828                         else
829                                 drawpic(p, spider_rocket_icon, '1 1 0' * rkt_size, '0 1 0' * reload, 0.75, DRAWFLAG_NORMAL);
830                 }
831         }
832
833         if (sb_showscores)
834         {
835                 Sbar_DrawScoreboard();
836                 Sbar_DrawCenterPrint();
837         }
838
839 }
840 void CSQC_common_hud(void)
841 {
842         // Sbar_SortFrags(); done in Sbar_Draw
843         float hud;
844         hud = getstati(STAT_HUD);
845
846         //hud = 10;
847         switch(hud)
848         {
849                 case HUD_NORMAL:
850                         Sbar_Draw();
851                         break;
852
853                 case HUD_SPIDERBOT:
854                         CSQC_spider_HUD();
855                         break;
856         }
857 }
858
859
860 // following vectors must be global to allow seamless switching between camera modes
861 vector camera_offset, current_camera_offset, mouse_angles, current_angles, current_origin, current_position;
862 void CSQC_Demo_Camera()
863 {
864         float speed, attenuation, dimensions;
865         vector tmp, delta;
866
867         if( cvar("camera_reset") || !camera_mode )
868         {
869                 camera_offset = '0 0 0';
870                 current_angles = '0 0 0';
871                 camera_direction = '0 0 0';
872                 camera_offset_z += 30;
873                 camera_offset_x += 30 * -cos(current_angles_y * DEG2RAD);
874                 camera_offset_y += 30 * -sin(current_angles_y * DEG2RAD);
875                 current_origin = view_origin;
876                 current_camera_offset  = camera_offset;
877                 cvar_set("camera_reset", "0");
878                 camera_mode = CAMERA_CHASE;
879         }
880
881         // Camera angles
882         if( camera_roll )
883                 mouse_angles_z += camera_roll * cvar("camera_speed_roll");
884
885         if(cvar("camera_look_player"))
886         {
887                 local vector dir;
888                 local float n;
889
890                 dir = normalize(view_origin - current_position);
891                 n = mouse_angles_z;
892                 mouse_angles = vectoangles(dir);
893                 mouse_angles_x = mouse_angles_x * -1;
894                 mouse_angles_z = n;
895         }
896         else
897         {
898                 tmp = getmousepos() * 0.1;
899                 if(vlen(tmp)>cvar("camera_mouse_treshold"))
900                 {
901                         mouse_angles_x += tmp_y * cos(mouse_angles_z * DEG2RAD) + (tmp_x * sin(mouse_angles_z * DEG2RAD));
902                         mouse_angles_y -= tmp_x * cos(mouse_angles_z * DEG2RAD) + (tmp_y * -sin(mouse_angles_z * DEG2RAD));
903                 }
904         }
905
906         while (mouse_angles_x < -180) mouse_angles_x = mouse_angles_x + 360;
907         while (mouse_angles_x > 180) mouse_angles_x = mouse_angles_x - 360;
908         while (mouse_angles_y < -180) mouse_angles_y = mouse_angles_y + 360;
909         while (mouse_angles_y > 180) mouse_angles_y = mouse_angles_y - 360;
910
911         // Fix difference when angles don't have the same sign
912         delta = '0 0 0';
913         if(mouse_angles_y < -60 && current_angles_y > 60)
914                 delta = '0 360 0';
915         if(mouse_angles_y > 60 && current_angles_y < -60)
916                 delta = '0 -360 0';
917
918         if(cvar("camera_look_player"))
919                 attenuation = cvar("camera_look_attenuation");
920         else
921                 attenuation = cvar("camera_speed_attenuation");
922
923         attenuation = 1 / max(1, attenuation);
924         current_angles += (mouse_angles - current_angles + delta) * attenuation;
925
926         while (current_angles_x < -180) current_angles_x = current_angles_x + 360;
927         while (current_angles_x > 180) current_angles_x = current_angles_x - 360;
928         while (current_angles_y < -180) current_angles_y = current_angles_y + 360;
929         while (current_angles_y > 180) current_angles_y = current_angles_y - 360;
930
931         // Camera position
932         tmp = '0 0 0';
933         dimensions = 0;
934
935         if( camera_direction_x )
936         {
937                 tmp_x = camera_direction_x * cos(current_angles_y * DEG2RAD);
938                 tmp_y = camera_direction_x * sin(current_angles_y * DEG2RAD);
939                 if( cvar("camera_forward_follows") && !cvar("camera_look_player") )
940                         tmp_z = camera_direction_x * -sin(current_angles_x * DEG2RAD);
941                 ++dimensions;
942         }
943
944         if( camera_direction_y )
945         {
946                 tmp_x += camera_direction_y * -sin(current_angles_y * DEG2RAD);
947                 tmp_y += camera_direction_y * cos(current_angles_y * DEG2RAD) * cos(current_angles_z * DEG2RAD);
948                 tmp_z += camera_direction_y * sin(current_angles_z * DEG2RAD);
949                 ++dimensions;
950         }
951
952         if( camera_direction_z )
953         {
954                 tmp_z += camera_direction_z * cos(current_angles_z * DEG2RAD);
955                 ++dimensions;
956         }
957
958         if(cvar("camera_free"))
959                 speed = cvar("camera_speed_free");
960         else
961                 speed = cvar("camera_speed_chase");
962
963         if(dimensions)
964         {
965                 speed = speed * sqrt(1 / dimensions);
966                 camera_offset += tmp * speed;
967         }
968
969         current_camera_offset += (camera_offset - current_camera_offset) * attenuation;
970
971         // Camera modes
972         if( cvar("camera_free") )
973         {
974                 if ( camera_mode == CAMERA_CHASE )
975                 {
976                         current_camera_offset = current_origin + current_camera_offset;
977                         camera_offset = current_origin + camera_offset;
978                 }
979
980                 camera_mode = CAMERA_FREE;
981                 current_position = current_camera_offset;
982         }
983         else
984         {
985                 if ( camera_mode == CAMERA_FREE )
986                 {
987                         current_origin = view_origin;
988                         camera_offset = camera_offset - current_origin;
989                         current_camera_offset = current_camera_offset - current_origin;
990                 }
991
992                 camera_mode = CAMERA_CHASE;
993
994                 if(cvar("camera_chase_smoothly"))
995                         current_origin += (view_origin - current_origin) * attenuation;
996                 else
997                         current_origin = view_origin;
998
999                 current_position = current_origin + current_camera_offset;
1000         }
1001
1002         R_SetView(VF_ANGLES, current_angles);
1003         R_SetView(VF_ORIGIN, current_position);
1004 }