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