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