]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/View.qc
fix a typo breaking the crosshair blurring
[divverent/nexuiz.git] / data / qcsrc / client / View.qc
1 entity porto;\r
2 vector polyline[16];\r
3 float trace_dphitcontents;\r
4 float Q3SURFACEFLAG_SLICK = 2; // low friction surface\r
5 float DPCONTENTS_SOLID = 1; // blocks player movement\r
6 float DPCONTENTS_BODY = 32; // blocks player movement\r
7 float DPCONTENTS_CORPSE = 64; // blocks player movement\r
8 float DPCONTENTS_PLAYERCLIP = 256; // blocks player movement\r
9 void Porto_Draw()\r
10 {\r
11         vector p, dir, ang, q, nextdir;\r
12         float idx, portal_number, portal1_idx;\r
13 \r
14         if(activeweapon != WEP_PORTO)\r
15                 return;\r
16 \r
17         dir = view_forward;\r
18 \r
19         if(angles_held_status)\r
20         {\r
21                 makevectors(angles_held);\r
22                 dir = v_forward;\r
23         }\r
24 \r
25         p = view_origin;\r
26 \r
27         polyline[0] = p;\r
28         idx = 1;\r
29         portal_number = 0;\r
30         nextdir = dir;\r
31 \r
32         for(;;)\r
33         {\r
34                 dir = nextdir;\r
35                 traceline(p, p + 65536 * dir, TRUE, porto);\r
36                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)\r
37                         return;\r
38                 nextdir = dir - 2 * (dir * trace_plane_normal) * trace_plane_normal; // mirror dir at trace_plane_normal\r
39                 p = trace_endpos;\r
40                 polyline[idx] = p;\r
41                 ++idx;\r
42                 if(idx >= 16)\r
43                         return;\r
44                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)\r
45                         continue;\r
46                 ++portal_number;\r
47                 ang = vectoangles2(trace_plane_normal, dir);\r
48                 ang_x = -ang_x;\r
49                 makevectors(ang);\r
50                 if(!CheckWireframeBox(porto, p - 48 * v_right - 48 * v_up + 16 * v_forward, 96 * v_right, 96 * v_up, 96 * v_forward))\r
51                         return;\r
52                 if(portal_number == 1)\r
53                         portal1_idx = idx;\r
54                 if(portal_number >= 2)\r
55                         break;\r
56         }\r
57 \r
58         while(idx >= 2)\r
59         {\r
60                 p = polyline[idx-2];\r
61                 q = polyline[idx-1];\r
62                 if(idx == 2)\r
63                         p = p - view_up * 16;\r
64                 if(idx-1 >= portal1_idx)\r
65                 {\r
66                         Draw_CylindricLine(p, q, 4, "", 1, 0, '0 0 1', 0.5, DRAWFLAG_NORMAL);\r
67                 }\r
68                 else\r
69                 {\r
70                         Draw_CylindricLine(p, q, 4, "", 1, 0, '1 0 0', 0.5, DRAWFLAG_NORMAL);\r
71                 }\r
72                 --idx;\r
73         }\r
74 }\r
75 \r
76 /**\r
77  * Checks whether the server initiated a map restart (stat_game_starttime changed)\r
78  *\r
79  * TODO: Use a better solution where a common shared entitiy is used that contains\r
80  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT\r
81  * and STAT_FRAGLIMIT to be auto-sent)\r
82  */\r
83 void CheckForGamestartChange() {\r
84         float startTime;\r
85         startTime = getstatf(STAT_GAMESTARTTIME);\r
86         if (previous_game_starttime != startTime) {\r
87                 if ((time + 5.0) < startTime) {\r
88                         //if connecting to server while restart was active don't always play prepareforbattle\r
89                         sound(self, CHAN_VOICE, "announcer/robotic/prepareforbattle.wav", VOL_BASEVOICE, ATTN_NONE);\r
90                 }\r
91                 if (time < startTime) {\r
92                         restartAnnouncer = spawn();\r
93                         restartAnnouncer.think = restartAnnouncer_Think;\r
94                         restartAnnouncer.nextthink = startTime - floor(startTime - time); //synchronize nextthink to startTime\r
95                 }\r
96         }\r
97         previous_game_starttime = startTime;\r
98 }\r
99 \r
100 void Porto_Init()\r
101 {\r
102         porto = spawn();\r
103         porto.classname = "porto";\r
104         porto.draw = Porto_Draw;\r
105         porto.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;\r
106 }\r
107 \r
108 float drawtime;\r
109 \r
110 float tan(float x)\r
111\r
112         return sin(x) / cos(x);\r
113 }\r
114 float atan2(float y, float x)\r
115 {\r
116         vector v;\r
117         v = '1 0 0' * x + '0 1 0' * y;\r
118         v = vectoangles(v);\r
119         return v_y * 0.01745329251994329576;\r
120 }\r
121 \r
122 vector GetCurrentFov(float fov)\r
123 {\r
124         float zoomsensitivity, zoomspeed, zoomfactor, zoomdir;\r
125 \r
126         zoomsensitivity = cvar("cl_zoomsensitivity");\r
127         zoomfactor = cvar("cl_zoomfactor");\r
128         if(zoomfactor < 1 || zoomfactor > 16)\r
129                 zoomfactor = 2.5;\r
130         zoomspeed = cvar("cl_zoomspeed");\r
131         if(zoomspeed >= 0)\r
132                 if(zoomspeed < 0.5 || zoomspeed > 16)\r
133                         zoomspeed = 3.5;\r
134 \r
135         zoomdir = button_zoom;\r
136         if(getstati(STAT_ACTIVEWEAPON) == WEP_NEX) // do NOT use switchweapon here\r
137                 zoomdir += button_attack2;\r
138         if(spectatee_status > 0 || isdemo())\r
139         {\r
140                 if(spectatorbutton_zoom)\r
141                         zoomdir = 0 + !zoomdir;\r
142                         // do not even THINK about removing this 0\r
143                         // _I_ know what I am doing\r
144                         // fteqcc does not\r
145         }\r
146 \r
147         if(zoomdir)\r
148                 zoomin_effect = 0;\r
149 \r
150         if(zoomin_effect || camera_active)\r
151         {\r
152                 current_viewzoom = min(1, current_viewzoom + drawframetime);\r
153         }\r
154         else\r
155         {\r
156                 if(zoomspeed < 0) // instant zoom\r
157                 {\r
158                         if(zoomdir)\r
159                                 current_viewzoom = 1 / zoomfactor;\r
160                         else\r
161                                 current_viewzoom = 1;\r
162                 }\r
163                 else\r
164                 {\r
165                         if(zoomdir)\r
166                                 current_viewzoom = 1 / bound(1, 1 / current_viewzoom + drawframetime * zoomspeed * (zoomfactor - 1), zoomfactor);\r
167                         else\r
168                                 current_viewzoom = bound(1 / zoomfactor, current_viewzoom + drawframetime * zoomspeed * (1 - 1 / zoomfactor), 1);\r
169                 }\r
170         }\r
171 \r
172         if(almost_equals(current_viewzoom, 1))\r
173                 current_zoomfraction = 0;\r
174         else if(almost_equals(current_viewzoom, 1/zoomfactor))\r
175                 current_zoomfraction = 1;\r
176         else\r
177                 current_zoomfraction = (current_viewzoom - 1) / (1/zoomfactor - 1);\r
178 \r
179         if(zoomsensitivity < 1)\r
180                 setsensitivityscale(pow(current_viewzoom, 1 - zoomsensitivity));\r
181         else\r
182                 setsensitivityscale(1);\r
183 \r
184         float frustumx, frustumy, fovx, fovy;\r
185         frustumy = tan(fov * 0.00872664625997164788) * 0.75 * current_viewzoom;\r
186         frustumx = frustumy * vid_width / vid_height / vid_pixelheight;\r
187         fovx = atan2(frustumx, 1) / 0.00872664625997164788;\r
188         fovy = atan2(frustumy, 1) / 0.00872664625997164788;\r
189 \r
190         return '1 0 0' * fovx + '0 1 0' * fovy;\r
191 }\r
192 \r
193 // this function must match W_SetupShot!\r
194 float zoomscript_caught;\r
195 entity trueaim;\r
196 void TrueAim_Init()\r
197 {\r
198         trueaim = spawn();\r
199         trueaim.classname = "trueaim";\r
200         trueaim.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;\r
201 }\r
202 \r
203 float TrueAimCheck()\r
204 {\r
205         float nudge = 1; // added to traceline target and subtracted from result\r
206         vector vecs, trueaimpoint, w_shotorg;\r
207         vector mi, ma;\r
208 \r
209         mi = ma = '0 0 0';\r
210 \r
211         switch(activeweapon)\r
212         {\r
213                 case WEP_PORTO: // shoots from eye\r
214                 case WEP_HOOK: // no trueaim\r
215                 case WEP_GRENADE_LAUNCHER: // toss curve\r
216                         return 1; \r
217                 case WEP_CAMPINGRIFLE:\r
218                         if(zoomscript_caught)\r
219                                 return 1; // shoots from eye when zoomed\r
220                 case WEP_ROCKET_LAUNCHER: // projectile has a size!\r
221                         mi = '-3 -3 -3';\r
222                         ma = '3 3 3';\r
223                         break;\r
224                 case WEP_SEEKER: // projectile has a size!\r
225                         mi = '-2 -2 -2';\r
226                         ma = '2 2 2';\r
227                         break;\r
228         }\r
229 \r
230         vecs = decompressShotOrigin(getstati(STAT_SHOTORG));\r
231 \r
232         traceline(view_origin, view_origin + view_forward * MAX_SHOT_DISTANCE, MOVE_NOMONSTERS, trueaim);\r
233         trueaimpoint = trace_endpos;\r
234 \r
235         if(vecs_x > 0)\r
236                 vecs_y = -vecs_y;\r
237         else\r
238                 vecs = '0 0 0';\r
239         \r
240         w_shotorg = view_origin + view_right * vecs_y + view_up * vecs_z;\r
241 \r
242         // now move the vecs forward as much as requested if possible\r
243         traceline(w_shotorg, w_shotorg + view_forward * (vecs_x + nudge), MOVE_NORMAL, trueaim); // FIXME this MOVE_NORMAL part will misbehave a little in csqc\r
244         w_shotorg = trace_endpos - view_forward * nudge;\r
245 \r
246         // now test whether we will actually hit the trueaimpoint...\r
247         tracebox(w_shotorg, mi, ma, trueaimpoint, MOVE_NOMONSTERS, trueaim);\r
248         return vlen(trace_endpos - trueaimpoint) <= vlen(ma - mi) + 1;\r
249                 // 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\r
250 }\r
251 \r
252 void CSQC_common_hud(void);\r
253 \r
254 void CSQC_kh_hud(void);\r
255 void CSQC_ctf_hud(void);\r
256 void PostInit(void);\r
257 void CSQC_Demo_Camera();\r
258 float Sbar_WouldDrawScoreboard ();\r
259 float view_set;\r
260 float camera_mode;\r
261 string NextFrameCommand;\r
262 void CSQC_UpdateView(float w, float h)\r
263 {\r
264         entity e;\r
265         float fov;\r
266         float f, i, j;\r
267 \r
268         dprint_load();\r
269         WaypointSprite_Load();\r
270 \r
271         ticrate = getstatf(STAT_SYS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);\r
272 \r
273         // Render the Scene\r
274         if(!intermission || !view_set)\r
275         {\r
276                 view_origin = pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT);\r
277                 view_angles = input_angles;\r
278                 makevectors(view_angles);\r
279                 view_forward = v_forward;\r
280                 view_right = v_right;\r
281                 view_up = v_up;\r
282                 view_set = 1;\r
283         }\r
284 \r
285         vid_width = w;\r
286         vid_height = h;\r
287 \r
288 #ifdef BLURTEST\r
289         if(time > blurtest_time0 && time < blurtest_time1)\r
290         {\r
291                 float r, t;\r
292 \r
293                 t = (time - blurtest_time0) / (blurtest_time1 - blurtest_time0);\r
294                 r = t * blurtest_radius;\r
295                 f = 1 / pow(t, blurtest_power) - 1;\r
296 \r
297                 cvar_set("r_glsl_postprocess", "1");\r
298                 cvar_set("r_glsl_postprocess_uservec1", strcat(ftos(r), " ", ftos(f), " 0 0"));\r
299         }\r
300         else\r
301         {\r
302                 cvar_set("r_glsl_postprocess", "0");\r
303                 cvar_set("r_glsl_postprocess_uservec1", "0 0 0 0");\r
304         }\r
305 #endif\r
306 \r
307         Fog_Force();\r
308 \r
309         drawframetime = max(0.000001, time - drawtime);\r
310         drawtime = time;\r
311 \r
312         // watch for gametype changes here...\r
313         // in ParseStuffCMD the cmd isn't executed yet :/\r
314         // might even be better to add the gametype to TE_CSQC_INIT...?\r
315         if(!postinit)\r
316                 PostInit();\r
317         \r
318         CheckForGamestartChange();\r
319 \r
320         fov = cvar("fov");\r
321         if(button_zoom || fov <= 59.5)\r
322         {\r
323                 if(!zoomscript_caught)\r
324                 {\r
325                         localcmd("+button4\n");\r
326                         zoomscript_caught = 1;\r
327                         ignore_plus_zoom += 1;\r
328                 }\r
329         }\r
330         else\r
331         {\r
332                 if(zoomscript_caught)\r
333                 {\r
334                         localcmd("-button4\n");\r
335                         zoomscript_caught = 0;\r
336                         ignore_minus_zoom += 1;\r
337                 }\r
338         }\r
339         \r
340         sbar_alpha_fg = cvar("sbar_alpha_fg" );\r
341         sbar_hudselector = cvar("sbar_hudselector");\r
342         ColorTranslateMode = cvar("cl_stripcolorcodes");\r
343         activeweapon = getstati(STAT_SWITCHWEAPON);\r
344         f = cvar("teamplay");\r
345         if(f != teamplay)\r
346         {\r
347                 teamplay = f;\r
348                 Sbar_InitScores();\r
349         }\r
350 \r
351         if(last_weapon != activeweapon) {\r
352                 weapontime = time;\r
353                 last_weapon = activeweapon;\r
354         }\r
355 \r
356         // ALWAYS Clear Current Scene First\r
357         R_ClearScene();\r
358 \r
359         // Assign Standard Viewflags\r
360         // Draw the World (and sky)\r
361         R_SetView(VF_DRAWWORLD, 1);\r
362 \r
363         // Set the console size vars\r
364         vid_conwidth = cvar("vid_conwidth");\r
365         vid_conheight = cvar("vid_conheight");\r
366         vid_pixelheight = cvar("vid_pixelheight");\r
367 \r
368         R_SetView(VF_FOV, GetCurrentFov(fov));\r
369 \r
370         // Camera for demo playback\r
371         if(camera_active)\r
372         {\r
373                 if(cvar("camera_enable"))\r
374                         CSQC_Demo_Camera();\r
375                 else\r
376                 {\r
377                         cvar_set("chase_active", ftos(chase_active_backup));\r
378                         cvar_set("cl_demo_mousegrab", "0");\r
379                         camera_active = FALSE;\r
380                 }\r
381         }\r
382 #ifdef CAMERATEST\r
383         else if(cvar("camera_enable"))\r
384 #else\r
385         else if(cvar("camera_enable") && isdemo())\r
386 #endif\r
387         {\r
388                 // Enable required Darkplaces cvars\r
389                 chase_active_backup = cvar("chase_active");\r
390                 cvar_set("chase_active", "2");          \r
391                 cvar_set("cl_demo_mousegrab", "1");\r
392                 camera_active = TRUE;\r
393                 camera_mode = FALSE;\r
394         }\r
395         \r
396         // Draw the Crosshair\r
397         float scoreboard_active;\r
398         scoreboard_active = Sbar_WouldDrawScoreboard();\r
399         R_SetView(VF_DRAWCROSSHAIR, 0); //Make sure engine crosshairs are always hidden\r
400         \r
401         // Draw the Engine Status Bar (the default Quake HUD)\r
402         R_SetView(VF_DRAWENGINESBAR, 0);\r
403 \r
404         // fetch this one only once per frame\r
405         sbar_showbinds = cvar("sbar_showbinds");\r
406         sbar_showbinds_limit = cvar("sbar_showbinds_limit");\r
407 \r
408         // Update the mouse position\r
409         /*\r
410         mousepos_x = vid_conwidth;\r
411         mousepos_y = vid_conheight;\r
412         mousepos = mousepos*0.5 + getmousepos();\r
413         */\r
414 \r
415         R_AddEntities(MASK_NORMAL | MASK_ENGINE | MASK_ENGINEVIEWMODELS);\r
416 \r
417         e = self;\r
418         for(self = world; (self = nextent(self)); )\r
419                 if(self.draw)\r
420                         self.draw();\r
421         self = e;\r
422         R_RenderScene();\r
423 \r
424         // now switch to 2D drawing mode by calling a 2D drawing function\r
425         // then polygon drawing will draw as 2D stuff, and NOT get queued until the\r
426         // next R_RenderScene call\r
427         drawstring('0 0 0', "", '1 1 0', '1 1 1', 0, 0);\r
428 \r
429         // Draw the mouse cursor\r
430         // NOTE: drawpic must happen after R_RenderScene for some reason\r
431         //drawpic(getmousepos(), "gfx/cursor.tga", '11 14 0', '1 1 1', 1, 0);\r
432         //drawstring('50 50', ftos(game), '10 10 0', '1 1 1', 1, 0);\r
433         //self = edict_num(player_localnum);\r
434         //drawstring('0 0', vtos(pmove_org), '8 8 0', '1 1 1', 1, 0);\r
435         //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);\r
436         // as long as the ctf part isn't in, this is useless\r
437         if(menu_visible)\r
438                 menu_show();\r
439         \r
440         /*if(gametype == GAME_CTF)\r
441         {\r
442                 ctf_view();\r
443         } else */\r
444 \r
445         // draw 2D entities\r
446         e = self;\r
447         for(self = world; (self = nextent(self)); )\r
448                 if(self.draw2d)\r
449                         self.draw2d();\r
450         self = e;\r
451         \r
452         // draw radar\r
453         if(\r
454                 ons_showmap\r
455                 ||\r
456                 (\r
457                         !scoreboard_active\r
458                         &&\r
459                         cvar_string("cl_teamradar") != "0"\r
460                         &&\r
461                         (\r
462                                 cvar("cl_teamradar") == 2\r
463                                 ||\r
464                                 teamplay\r
465                         )\r
466                 )\r
467         )\r
468                 teamradar_view();\r
469 \r
470         if (cvar("cl_showpressedkeys")) // draw pressed keys when spectating and playing\r
471         {\r
472                 if(spectatee_status > 0 || cvar("cl_showpressedkeys") >= 2)\r
473                         Sbar_DrawPressedKeys();\r
474         }\r
475 \r
476         // draw sbar\r
477         if(cvar("r_letterbox") == 0)\r
478         if(cvar("viewsize") < 120)\r
479                 CSQC_common_hud();\r
480 \r
481         // crosshair goes VERY LAST\r
482         if(!scoreboard_active && !ons_showmap && !camera_active) {\r
483                 // TrueAim check\r
484                 float goodshot;\r
485 \r
486                 goodshot = TrueAimCheck();\r
487 \r
488                 string wcross_style;\r
489                 wcross_style = cvar_string("crosshair");\r
490 \r
491                 if (wcross_style != "0") {\r
492                         vector wcross_color, wcross_size;\r
493                         string wcross_wep, wcross_name;\r
494                         float wcross_alpha, wcross_sizefloat;\r
495 \r
496                         wcross_color_x = cvar("crosshair_color_red");\r
497                         wcross_color_y = cvar("crosshair_color_green");\r
498                         wcross_color_z = cvar("crosshair_color_blue");\r
499                         wcross_alpha = cvar("crosshair_color_alpha");\r
500                         wcross_sizefloat = cvar("crosshair_size");\r
501                         if (cvar("crosshair_per_weapon")) {\r
502                                 e = get_weaponinfo(activeweapon);\r
503                                 if (e && e.netname != "")\r
504                                 {\r
505                                         wcross_wep = e.netname;\r
506                                         wcross_style = cvar_string(strcat("crosshair_", wcross_wep));\r
507                                         if(wcross_style == "")\r
508                                                 wcross_style = e.netname;\r
509 \r
510                                         if(!cvar("crosshair_color_override"))\r
511                                         {\r
512                                                 wcross_color_x = cvar(strcat("crosshair_", wcross_wep, "_color_red"));\r
513                                                 wcross_color_y = cvar(strcat("crosshair_", wcross_wep, "_color_green"));\r
514                                                 wcross_color_z = cvar(strcat("crosshair_", wcross_wep, "_color_blue"));\r
515                                         }\r
516 \r
517                                         wcross_alpha *= cvar(strcat("crosshair_", wcross_wep, "_color_alpha"));\r
518                                         wcross_sizefloat *= cvar(strcat("crosshair_", wcross_wep, "_size"));\r
519                                 }\r
520                         }\r
521 \r
522                         wcross_name = strcat("gfx/crosshair", wcross_style);\r
523                 \r
524                         wcross_size = drawgetimagesize(wcross_name);\r
525                         wcross_size_x *= wcross_sizefloat;\r
526                         wcross_size_y *= wcross_sizefloat;\r
527 \r
528                         if(goodshot || !cvar("crosshair_hittest"))\r
529                         {\r
530                                 drawpic('0.5 0 0' * (vid_conwidth - wcross_size_x) + '0 0.5 0' * (vid_conheight - wcross_size_y), wcross_name, wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);\r
531                         }\r
532                         else\r
533                         {\r
534                                 wcross_alpha *= 0.04 * 0.75;\r
535                                 for(i = -2; i <= 2; ++i)\r
536                                         for(j = -2; j <= 2; ++j)\r
537                                                 drawpic('0.5 0 0' * (vid_conwidth - wcross_size_x + i) + '0 0.5 0' * (vid_conheight - wcross_size_y + j), wcross_name, wcross_size, wcross_color, wcross_alpha, DRAWFLAG_NORMAL);\r
538                         }\r
539                 }\r
540         }\r
541 \r
542         if(NextFrameCommand)\r
543         {\r
544                 localcmd("\n", NextFrameCommand, "\n");\r
545                 NextFrameCommand = string_null;\r
546         }\r
547 }\r
548 \r
549 void Sbar_Draw();\r
550 void CSQC_common_hud(void)\r
551 {\r
552         // Sbar_SortFrags(); done in Sbar_Draw\r
553         Sbar_Draw();\r
554 }\r
555 \r
556 // KeyHunt HUD by victim\r
557 void CSQC_kh_hud(void)\r
558 {\r
559         // HUD 0 has the weapons on the right hand side - temporarily shown when needed\r
560         // HUD 1 has the weapons on the bottom - permanently\r
561 \r
562         // use the following two binds to check the icons move correctly\r
563         // bind g "toggle sbar_flagstatus_right; echo Menu right $sbar_flagstatus_right"  // move the icons\r
564         // bind h "toggle sbar_hudselector; echo HUD $sbar_hudselector"  // change the HUD\r
565 \r
566         float kh_keys, kh_keys_status, kh_teams_set;\r
567         float kh_margin_x, kh_margin_y, kh_key_box;\r
568         string kh_carrying, kh_outline;\r
569         vector red_pos, blue_pos, yellow_pos, pink_pos, kh_size;\r
570         vector red, blue, yellow, pink;\r
571 \r
572         kh_keys = getstati(STAT_KH_KEYS);\r
573         kh_keys_status = kh_keys / 256;\r
574         kh_teams_set = cvar("_teams_available");  // set in keyhunt.qc\r
575 \r
576         kh_margin_y = 8;\r
577         kh_margin_x = (cvar("sbar_flagstatus_right") * sbar_hudselector * (vid_conwidth - 67)) + 10;\r
578 //      sbar_flagstatus_right 0/1; sbar_hudselector 0/1; screen width - key width + margin\r
579 \r
580         red_pos_x = blue_pos_x = yellow_pos_x = pink_pos_x = kh_margin_x;\r
581 \r
582         kh_key_box = 120;\r
583 \r
584         pink_pos_y = kh_margin_y + 0;  // top\r
585         yellow_pos_y = kh_margin_y + kh_key_box;\r
586         blue_pos_y = kh_margin_y + kh_key_box * 2;\r
587         red_pos_y = kh_margin_y + kh_key_box * 3;  //bottom\r
588 \r
589         red = '1 0 0';\r
590         blue = '0 0 1';\r
591         yellow = '1 1 0';\r
592         pink = '1 0 1';\r
593 \r
594         kh_size = '0 0 0';  // don't resize the image\r
595 \r
596         kh_carrying = "gfx/sb_kh_full";\r
597         kh_outline = "gfx/sb_kh_outline";\r
598 \r
599 //      drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)\r
600 //      vector position = '0 0';  // 'x y' 0 0 (the origin) is the top left. X 0 - 799, Y 0 - 599\r
601 \r
602 //      vector size = '0 0';  // 'x y' changes the x & y dimensions. '0 0' gives the default pic size\r
603 //      vector rgb = '0 0 0';  // 'r g b' range 0 - 1\r
604 \r
605         if (kh_keys_status & 1)  // red\r
606                 drawpic (red_pos, kh_carrying, kh_size, red, 0.2, 0);  // show 20% alpha key\r
607         else\r
608                 drawpic (red_pos, kh_outline, kh_size, red, 0.4, 0);  // show key outline 40% alpha\r
609 \r
610         if (kh_keys & 1)\r
611                 drawpic (red_pos, kh_carrying, kh_size, red, 1.0, 0);  // show solid key 100% alpha\r
612 \r
613 \r
614         if (kh_keys_status & 2)  // blue\r
615                 drawpic (blue_pos, kh_carrying, kh_size, blue, 0.2, 0);\r
616         else\r
617                 drawpic (blue_pos, kh_outline, kh_size, blue, 0.4, 0);\r
618 \r
619         if (kh_keys & 2)\r
620                 drawpic (blue_pos, kh_carrying, kh_size, blue, 1.0, 0);\r
621 \r
622 \r
623         if (kh_teams_set & 4)  // yellow\r
624         {\r
625                 if (kh_keys_status & 4)\r
626                         drawpic (yellow_pos, kh_carrying, kh_size, yellow, 0.2, 0);\r
627                 else\r
628                         drawpic (yellow_pos, kh_outline, kh_size, yellow, 0.4, 0);\r
629 \r
630                 if (kh_keys & 4)\r
631                         drawpic (yellow_pos, kh_carrying, kh_size, yellow, 1.0, 0);\r
632         }\r
633 \r
634 \r
635         if (kh_teams_set & 8)  // pink\r
636         {\r
637                 if (kh_keys_status & 8)\r
638                         drawpic (pink_pos, kh_carrying, kh_size, pink, 0.2, 0);\r
639                 else\r
640                         drawpic (pink_pos, kh_outline, kh_size, pink, 0.4, 0);\r
641 \r
642                 if (kh_keys & 8)\r
643                         drawpic (pink_pos, kh_carrying, kh_size, pink, 1.0, 0);\r
644         }\r
645 \r
646 }\r
647 \r
648 // following vectors must be global to allow seamless switching between camera modes\r
649 vector camera_offset, current_camera_offset, mouse_angles, current_angles, current_origin, current_position;\r
650 void CSQC_Demo_Camera()\r
651 {\r
652         float speed, attenuation, dimensions;\r
653         vector tmp, delta;\r
654 \r
655         if( cvar("camera_reset") || !camera_mode )\r
656         {\r
657                 camera_offset = '0 0 0';\r
658                 current_angles = '0 0 0';\r
659                 camera_direction = '0 0 0';\r
660                 camera_offset_z += 30;\r
661                 camera_offset_x += 30 * -cos(current_angles_y * DEG2RAD);\r
662                 camera_offset_y += 30 * -sin(current_angles_y * DEG2RAD);\r
663                 current_origin = view_origin;\r
664                 current_camera_offset  = camera_offset;\r
665                 cvar_set("camera_reset", "0");\r
666                 camera_mode = CAMERA_CHASE;\r
667         }\r
668 \r
669         // Camera angles\r
670         if( camera_roll )\r
671                 mouse_angles_z += camera_roll * cvar("camera_speed_roll");\r
672 \r
673         if(cvar("camera_look_player"))\r
674         {\r
675                 local vector dir;\r
676                 local float n;\r
677 \r
678                 dir = normalize(view_origin - current_position);\r
679                 n = mouse_angles_z;\r
680                 mouse_angles = vectoangles(dir);\r
681                 mouse_angles_x = mouse_angles_x * -1;\r
682                 mouse_angles_z = n;\r
683         }\r
684         else\r
685         {\r
686                 tmp = getmousepos() * 0.1;\r
687                 if(vlen(tmp)>cvar("camera_mouse_treshold"))\r
688                 {\r
689                         mouse_angles_x += tmp_y * cos(mouse_angles_z * DEG2RAD) + (tmp_x * sin(mouse_angles_z * DEG2RAD));\r
690                         mouse_angles_y -= tmp_x * cos(mouse_angles_z * DEG2RAD) + (tmp_y * -sin(mouse_angles_z * DEG2RAD));\r
691                 }\r
692         }\r
693 \r
694         while (mouse_angles_x < -180) mouse_angles_x = mouse_angles_x + 360;\r
695         while (mouse_angles_x > 180) mouse_angles_x = mouse_angles_x - 360;\r
696         while (mouse_angles_y < -180) mouse_angles_y = mouse_angles_y + 360;\r
697         while (mouse_angles_y > 180) mouse_angles_y = mouse_angles_y - 360;\r
698 \r
699         // Fix difference when angles don't have the same sign\r
700         delta = '0 0 0';\r
701         if(mouse_angles_y < -60 && current_angles_y > 60)\r
702                 delta = '0 360 0';\r
703         if(mouse_angles_y > 60 && current_angles_y < -60)\r
704                 delta = '0 -360 0';\r
705 \r
706         if(cvar("camera_look_player"))\r
707                 attenuation = cvar("camera_look_attenuation");\r
708         else\r
709                 attenuation = cvar("camera_speed_attenuation");\r
710 \r
711         attenuation = 1 / max(1, attenuation);\r
712         current_angles += (mouse_angles - current_angles + delta) * attenuation;\r
713 \r
714         while (current_angles_x < -180) current_angles_x = current_angles_x + 360;\r
715         while (current_angles_x > 180) current_angles_x = current_angles_x - 360;\r
716         while (current_angles_y < -180) current_angles_y = current_angles_y + 360;\r
717         while (current_angles_y > 180) current_angles_y = current_angles_y - 360;\r
718 \r
719         // Camera position\r
720         tmp = '0 0 0';\r
721         dimensions = 0;\r
722 \r
723         if( camera_direction_x )\r
724         {\r
725                 tmp_x = camera_direction_x * cos(current_angles_y * DEG2RAD);\r
726                 tmp_y = camera_direction_x * sin(current_angles_y * DEG2RAD);\r
727                 if( cvar("camera_forward_follows") && !cvar("camera_look_player") )\r
728                         tmp_z = camera_direction_x * -sin(current_angles_x * DEG2RAD);\r
729                 ++dimensions;\r
730         }\r
731 \r
732         if( camera_direction_y )\r
733         {\r
734                 tmp_x += camera_direction_y * -sin(current_angles_y * DEG2RAD);\r
735                 tmp_y += camera_direction_y * cos(current_angles_y * DEG2RAD) * cos(current_angles_z * DEG2RAD);\r
736                 tmp_z += camera_direction_y * sin(current_angles_z * DEG2RAD);\r
737                 ++dimensions;\r
738         }\r
739 \r
740         if( camera_direction_z )\r
741         {\r
742                 tmp_z += camera_direction_z * cos(current_angles_z * DEG2RAD);\r
743                 ++dimensions;\r
744         }\r
745 \r
746         if(cvar("camera_free"))\r
747                 speed = cvar("camera_speed_free");\r
748         else\r
749                 speed = cvar("camera_speed_chase");\r
750 \r
751         if(dimensions)\r
752         {\r
753                 speed = speed * sqrt(1 / dimensions);\r
754                 camera_offset += tmp * speed;\r
755         }\r
756 \r
757         current_camera_offset += (camera_offset - current_camera_offset) * attenuation;\r
758 \r
759         // Camera modes\r
760         if( cvar("camera_free") )\r
761         {\r
762                 if ( camera_mode == CAMERA_CHASE )\r
763                 {\r
764                         current_camera_offset = current_origin + current_camera_offset;\r
765                         camera_offset = current_origin + camera_offset;\r
766                 }\r
767 \r
768                 camera_mode = CAMERA_FREE;\r
769                 current_position = current_camera_offset;\r
770         }\r
771         else\r
772         {\r
773                 if ( camera_mode == CAMERA_FREE )\r
774                 {\r
775                         current_origin = view_origin;\r
776                         camera_offset = camera_offset - current_origin;\r
777                         current_camera_offset = current_camera_offset - current_origin;\r
778                 }\r
779 \r
780                 camera_mode = CAMERA_CHASE;\r
781 \r
782                 if(cvar("camera_chase_smoothly"))\r
783                         current_origin += (view_origin - current_origin) * attenuation;\r
784                 else\r
785                         current_origin = view_origin;\r
786 \r
787                 current_position = current_origin + current_camera_offset;\r
788         }\r
789 \r
790         R_SetView(VF_ANGLES, current_angles);\r
791         R_SetView(VF_ORIGIN, current_position);\r
792 }\r