]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/crosshair.qc
Fix compilation units
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / crosshair.qc
1 #include "crosshair.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/panel/scoreboard.qh>
5 #include <client/view.qh>
6 #include <common/deathtypes/all.qh>
7 #include <common/ent_cs.qh>
8 #include <common/mapobjects/trigger/viewloc.qh>
9 #include <common/minigames/cl_minigames.qh>
10 #include <common/minigames/cl_minigames_hud.qh>
11 #include <common/mutators/mutator/overkill/oknex.qh>
12 #include <common/vehicles/all.qh>
13 #include <common/viewloc.qh>
14 #include <common/wepent.qh>
15 #include <lib/csqcmodel/cl_player.qh>
16 #include <lib/warpzone/common.qh>
17
18 float pickup_crosshair_time, pickup_crosshair_size;
19 float hitindication_crosshair_size;
20 float use_vortex_chargepool;
21
22 float reticle_type;
23
24 vector wcross_origin;
25 float wcross_scale_prev, wcross_alpha_prev;
26 vector wcross_color_prev;
27 float wcross_scale_goal_prev, wcross_alpha_goal_prev;
28 vector wcross_color_goal_prev;
29 float wcross_changedonetime;
30
31 string wcross_name_goal_prev, wcross_name_goal_prev_prev;
32 float wcross_resolution_goal_prev, wcross_resolution_goal_prev_prev;
33 float wcross_name_changestarttime, wcross_name_changedonetime;
34 float wcross_name_alpha_goal_prev, wcross_name_alpha_goal_prev_prev;
35
36 float wcross_ring_prev;
37
38 entity trueaim;
39 entity trueaim_rifle;
40
41 const float SHOTTYPE_HITTEAM = 1;
42 const float SHOTTYPE_HITOBSTRUCTION = 2;
43 const float SHOTTYPE_HITWORLD = 3;
44 const float SHOTTYPE_HITENEMY = 4;
45
46 void TrueAim_Init()
47 {
48         (trueaim = new_pure(trueaim)).dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
49         (trueaim_rifle = new_pure(trueaim_rifle)).dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
50 }
51
52 float EnemyHitCheck()
53 {
54         float t, n;
55         wcross_origin = project_3d_to_2d(trace_endpos);
56         wcross_origin.z = 0;
57         if(trace_ent)
58                 n = trace_ent.entnum;
59         else
60                 n = trace_networkentity;
61         if(n < 1)
62                 return SHOTTYPE_HITWORLD;
63         if(n > maxclients)
64                 return SHOTTYPE_HITWORLD;
65         t = entcs_GetTeam(n - 1);
66         if(teamplay)
67                 if(t == myteam)
68                         return SHOTTYPE_HITTEAM;
69         if(t == NUM_SPECTATOR)
70                 return SHOTTYPE_HITWORLD;
71         return SHOTTYPE_HITENEMY;
72 }
73
74 float TrueAimCheck(entity wepent)
75 {
76         if(wepent.activeweapon.spawnflags & WEP_FLAG_NOTRUEAIM)
77                 return SHOTTYPE_HITWORLD;
78
79         float nudge = 1; // added to traceline target and subtracted from result TOOD(divVerent): do we still need this? Doesn't the engine do this now for us?
80         vector vecs, trueaimpoint, w_shotorg;
81         vector mi, ma, dv;
82         float shottype;
83         entity ta;
84         float mv;
85
86         mi = ma = '0 0 0';
87         ta = trueaim;
88         mv = MOVE_NOMONSTERS;
89
90         switch(wepent.activeweapon) // WEAPONTODO
91         {
92                 case WEP_VORTEX:
93                 case WEP_OVERKILL_NEX:
94                 case WEP_VAPORIZER:
95                         mv = MOVE_NORMAL;
96                         break;
97                 case WEP_RIFLE:
98                         ta = trueaim_rifle;
99                         mv = MOVE_NORMAL;
100                         if(zoomscript_caught)
101                         {
102                                 tracebox(view_origin, '0 0 0', '0 0 0', view_origin + view_forward * max_shot_distance, mv, ta);
103                                 return EnemyHitCheck();
104                         }
105                         break;
106                 case WEP_DEVASTATOR: // projectile has a size!
107                         mi = '-3 -3 -3';
108                         ma = '3 3 3';
109                         break;
110                 case WEP_FIREBALL: // projectile has a size!
111                         mi = '-16 -16 -16';
112                         ma = '16 16 16';
113                         break;
114                 case WEP_SEEKER: // projectile has a size!
115                         mi = '-2 -2 -2';
116                         ma = '2 2 2';
117                         break;
118                 case WEP_ELECTRO: // projectile has a size!
119                         mi = '0 0 -3';
120                         ma = '0 0 -3';
121                         break;
122         }
123
124         vector traceorigin = entcs_receiver(player_localentnum - 1).origin + (eZ * STAT(VIEWHEIGHT));
125
126         vecs = decompressShotOrigin(STAT(SHOTORG));
127
128         traceline(traceorigin, traceorigin + view_forward * max_shot_distance, mv, ta);
129         trueaimpoint = trace_endpos;
130         // move trueaimpoint a little bit forward to make the final tracebox reliable
131         // since it sometimes doesn't reach a teammate by a hair
132         trueaimpoint += view_forward;
133
134         if(vdist((trueaimpoint - traceorigin), <, g_trueaim_minrange))
135                 trueaimpoint = traceorigin + view_forward * g_trueaim_minrange;
136
137         if(vecs.x > 0)
138                 vecs.y = -vecs.y;
139         else
140                 vecs = '0 0 0';
141
142         dv = view_right * vecs.y + view_up * vecs.z;
143         w_shotorg = traceorigin + dv;
144
145         // now move the vecs forward as much as requested if possible
146         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
147         w_shotorg = trace_endpos - view_forward * nudge;
148
149         tracebox(w_shotorg, mi, ma, trueaimpoint, MOVE_NORMAL, ta);
150         shottype = EnemyHitCheck();
151         if(shottype != SHOTTYPE_HITWORLD)
152                 return shottype;
153
154 #if 0
155         // FIXME WHY DOES THIS NOT WORK FOR THE ROCKET LAUNCHER?
156         // or rather, I know why, but see no fix
157         if(vlen(trace_endpos - trueaimpoint) > vlen(ma) + vlen(mi) + 1)
158                 // 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
159                 return SHOTTYPE_HITOBSTRUCTION;
160 #endif
161
162         return SHOTTYPE_HITWORLD;
163 }
164
165 void HUD_Crosshair_Vehicle(entity this)
166 {
167         if(hud != HUD_BUMBLEBEE_GUN)
168         {
169                 Vehicle info = REGISTRY_GET(Vehicles, hud);
170                 info.vr_crosshair(info, this);
171         }
172 }
173
174 vector crosshair_getcolor(entity this, float health_stat)
175 {
176         static float rainbow_last_flicker;
177         static vector rainbow_prev_color;
178         vector wcross_color = '0 0 0';
179         switch(autocvar_crosshair_color_special)
180         {
181                 case 1: // weapon color
182                 {
183                         if(this != WEP_Null && hud == HUD_NORMAL)
184                         {
185                                 wcross_color = this.wpcolor;
186                                 break;
187                         }
188                         else { goto normalcolor; }
189                 }
190
191                 case 2: // color based on health and armor
192                 {
193                         vector v = healtharmor_maxdamage(health_stat, STAT(ARMOR), armorblockpercent, DEATH_WEAPON.m_id);
194                         float health_and_armor = floor(v.x + 1);
195                         wcross_color = HUD_Get_Num_Color(health_and_armor, 200, false);
196                         break;
197                 }
198
199                 case 3: // rainbow/random color
200                 {
201                         if(time >= rainbow_last_flicker)
202                         {
203                                 rainbow_prev_color = randomvec() * autocvar_crosshair_color_special_rainbow_brightness;
204                                 rainbow_last_flicker = time + autocvar_crosshair_color_special_rainbow_delay;
205                         }
206                         wcross_color = rainbow_prev_color;
207                         break;
208                 }
209 LABEL(normalcolor)
210                 default: { wcross_color = stov(autocvar_crosshair_color); break; }
211         }
212
213         return wcross_color;
214 }
215
216 .entity tag_entity;
217 void HUD_Crosshair(entity this)
218 {
219         // reset player's alpha here upon death since forced scoreboard prevents running the crosshair_chase code
220         if(autocvar_chase_active > 0 && autocvar_crosshair_chase && STAT(HEALTH) <= 0 && csqcplayer)
221                 csqcplayer.alpha = csqcplayer.m_alpha;
222
223         if (autocvar_chase_active > 0 && autocvar_chase_front)
224         {
225                 csqcplayer.alpha = csqcplayer.m_alpha;
226                 return;
227         }
228
229         float f, i, j;
230         vector v;
231         if(!scoreboard_active && !camera_active && intermission != 2 && !STAT(GAME_STOPPED) && !autocvar_cl_lockview
232                 && spectatee_status != -1 && (!csqcplayer.viewloc || (!spectatee_status && (csqcplayer.viewloc.spawnflags & VIEWLOC_FREEAIM))) && !MUTATOR_CALLHOOK(DrawCrosshair)
233                 && !HUD_MinigameMenu_IsOpened())
234         {
235                 if (!autocvar_crosshair_enabled) // main toggle for crosshair rendering
236                         return;
237
238                 if (spectatee_status > 0 && STAT(CAMERA_SPECTATOR) == 2)
239                         return;
240
241                 if (hud != HUD_NORMAL)
242                 {
243                         HUD_Crosshair_Vehicle(this);
244                         return;
245                 }
246
247                 string wcross_style;
248                 float wcross_alpha, wcross_resolution;
249                 wcross_style = autocvar_crosshair;
250                 if (csqcplayer.viewloc && (csqcplayer.viewloc.spawnflags & VIEWLOC_FREEAIM) && autocvar_crosshair_2d != "")
251                         wcross_style = autocvar_crosshair_2d;
252                 if (wcross_style == "0")
253                         return;
254                 wcross_resolution = autocvar_crosshair_size;
255                 if (wcross_resolution == 0)
256                         return;
257                 wcross_alpha = autocvar_crosshair_alpha;
258                 if (wcross_alpha == 0)
259                         return;
260
261                 // TrueAim check
262                 float shottype;
263
264                 static int crosshair_chase_state = 0;
265
266                 // wcross_origin = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
267                 if(csqcplayer.viewloc && (csqcplayer.viewloc.spawnflags & VIEWLOC_FREEAIM))
268                         wcross_origin = viewloc_mousepos;
269                 else if(autocvar_chase_active > 0 && autocvar_crosshair_chase)
270                 {
271                         vector player_org = ((csqcplayer) ? csqcplayer.origin + csqcplayer.view_ofs : view_origin);
272                         float my_alpha = (!csqcplayer.m_alpha) ? 1 : csqcplayer.m_alpha;
273                         float chase_playeralpha = bound(0.001, autocvar_crosshair_chase_playeralpha, 1);
274                         if(csqcplayer && chase_playeralpha < 1 && my_alpha > chase_playeralpha)
275                         {
276                                 crosshair_chase_state = 2;
277                                 bool hit = false;
278                                 if (pointinsidebox(view_origin, csqcplayer.absmin, csqcplayer.absmax))
279                                         hit = true;
280                                 else
281                                 {
282                                         WarpZone_TraceLine(view_origin, view_origin + max_shot_distance * view_forward, MOVE_NORMAL, NULL);
283                                         if(trace_ent == csqcplayer)
284                                                 hit = true;
285                                 }
286                                 float prev_alpha = csqcplayer.alpha;
287                                 if(hit)
288                                         csqcplayer.alpha = max(csqcplayer.alpha - frametime * 5, chase_playeralpha);
289                                 else
290                                         csqcplayer.alpha = min(csqcplayer.alpha + frametime * 5, my_alpha);
291
292                                 if (csqcplayer.alpha != prev_alpha)
293                                 {
294                                         FOREACH_ENTITY_CLASS("ENT_CLIENT_MODEL", it.tag_entity == csqcplayer,
295                                         {
296                                                 it.alpha = csqcplayer.alpha;
297                                         });
298                                 }
299                         }
300                         traceline(player_org, player_org + max_shot_distance * view_forward, MOVE_WORLDONLY, NULL);
301                         wcross_origin = project_3d_to_2d(trace_endpos);
302                 }
303                 else
304                         wcross_origin = project_3d_to_2d(view_origin + max_shot_distance * view_forward);
305                 wcross_origin.z = 0;
306                 if (crosshair_chase_state == 2) // enabled (this frame)
307                         crosshair_chase_state = 1;
308                 else if (crosshair_chase_state == 1) // turned off in the previous frame
309                 {
310                         // reset player alpha only in this frame
311                         if (csqcplayer)
312                                 csqcplayer.alpha = csqcplayer.m_alpha;
313                         crosshair_chase_state = 0; // turned off and alpha reset
314                 }
315
316                 if(autocvar_crosshair_hittest)
317                 {
318                         vector wcross_oldorigin;
319                         entity thiswep = viewmodels[0]; // TODO: unhardcode
320                         wcross_oldorigin = wcross_origin;
321                         shottype = TrueAimCheck(thiswep);
322                         if(shottype == SHOTTYPE_HITWORLD)
323                         {
324                                 v = wcross_origin - wcross_oldorigin;
325                                 v.x /= vid_conwidth;
326                                 v.y /= vid_conheight;
327                                 if(vdist(v, >, 0.01))
328                                         shottype = SHOTTYPE_HITOBSTRUCTION;
329                         }
330                         if(!autocvar_crosshair_hittest_showimpact)
331                                 wcross_origin = wcross_oldorigin;
332                 }
333                 else
334                         shottype = SHOTTYPE_HITWORLD;
335
336                 vector wcross_color = '0 0 0', wcross_size = '0 0 0';
337                 string wcross_name = "";
338                 float wcross_scale, wcross_blur;
339
340                 entity e = WEP_Null;
341                 if(autocvar_crosshair_per_weapon || (autocvar_crosshair_color_special == 1))
342                 {
343                         entity wepent = viewmodels[0]; // TODO: unhardcode
344                         e = wepent.switchingweapon;
345                         if(e)
346                         {
347                                 if(autocvar_crosshair_per_weapon)
348                                 {
349                                         // WEAPONTODO: access these through some general settings (with non-balance config settings)
350                                         //wcross_resolution *= cvar(strcat("crosshair_", wcross_wep, "_size"));
351                                         //if (wcross_resolution == 0)
352                                                 //return;
353
354                                         //wcross_style = cvar_string(strcat("crosshair_", wcross_wep));
355                                         wcross_resolution *= e.w_crosshair_size;
356                                         wcross_name = e.w_crosshair;
357                                 }
358                         }
359                 }
360
361                 if(wcross_name == "")
362                         wcross_name = strcat("gfx/crosshair", wcross_style);
363
364                 // MAIN CROSSHAIR COLOR DECISION
365                 wcross_color = crosshair_getcolor(e, STAT(HEALTH));
366
367                 if(autocvar_crosshair_effect_scalefade)
368                 {
369                         wcross_scale = wcross_resolution;
370                         wcross_resolution = 1;
371                 }
372                 else
373                 {
374                         wcross_scale = 1;
375                 }
376
377                 if(autocvar_crosshair_pickup)
378                 {
379                         float stat_pickup_time = STAT(LAST_PICKUP);
380
381                         if(pickup_crosshair_time < stat_pickup_time)
382                         {
383                                 if(time - stat_pickup_time < 5) // don't trigger the animation if it's too old
384                                         pickup_crosshair_size = 1;
385
386                                 pickup_crosshair_time = stat_pickup_time;
387                         }
388
389                         if(pickup_crosshair_size > 0)
390                                 pickup_crosshair_size -= autocvar_crosshair_pickup_speed * frametime;
391                         else
392                                 pickup_crosshair_size = 0;
393
394                         wcross_scale += sin(pickup_crosshair_size) * autocvar_crosshair_pickup;
395                 }
396
397                 // todo: make crosshair hit indication dependent on damage dealt
398                 if(autocvar_crosshair_hitindication)
399                 {
400                         vector col = ((autocvar_crosshair_color_special == 1) ? stov(autocvar_crosshair_hitindication_per_weapon_color) : stov(autocvar_crosshair_hitindication_color));
401
402                         if(unaccounted_damage)
403                         {
404                                 hitindication_crosshair_size = 1;
405                         }
406
407                         if(hitindication_crosshair_size > 0)
408                                 hitindication_crosshair_size -= autocvar_crosshair_hitindication_speed * frametime;
409                         else
410                                 hitindication_crosshair_size = 0;
411
412                         wcross_scale += sin(hitindication_crosshair_size) * autocvar_crosshair_hitindication;
413                         wcross_color.x += sin(hitindication_crosshair_size) * col.x;
414                         wcross_color.y += sin(hitindication_crosshair_size) * col.y;
415                         wcross_color.z += sin(hitindication_crosshair_size) * col.z;
416                 }
417
418                 // no effects needed for targeting enemies, this can't possibly span all valid targets!
419                 // just show for teammates to give a sign that they're an invalid target
420                 //if(shottype == SHOTTYPE_HITENEMY)
421                         //wcross_scale *= autocvar_crosshair_hittest; // is not queried if hittest is 0
422                 if(shottype == SHOTTYPE_HITTEAM)
423                         wcross_scale /= autocvar_crosshair_hittest; // is not queried if hittest is 0
424
425                 f = fabs(autocvar_crosshair_effect_time);
426                 if(wcross_scale != wcross_scale_goal_prev || wcross_alpha != wcross_alpha_goal_prev || wcross_color != wcross_color_goal_prev)
427                 {
428                         wcross_changedonetime = time + f;
429                 }
430                 if(wcross_name != wcross_name_goal_prev || wcross_resolution != wcross_resolution_goal_prev)
431                 {
432                         wcross_name_changestarttime = time;
433                         wcross_name_changedonetime = time + f;
434                         if(wcross_name_goal_prev_prev)
435                                 strunzone(wcross_name_goal_prev_prev);
436                         wcross_name_goal_prev_prev = wcross_name_goal_prev;
437                         wcross_name_goal_prev = strzone(wcross_name);
438                         wcross_name_alpha_goal_prev_prev = wcross_name_alpha_goal_prev;
439                         wcross_resolution_goal_prev_prev = wcross_resolution_goal_prev;
440                         wcross_resolution_goal_prev = wcross_resolution;
441                 }
442
443                 wcross_scale_goal_prev = wcross_scale;
444                 wcross_alpha_goal_prev = wcross_alpha;
445                 wcross_color_goal_prev = wcross_color;
446
447                 if((shottype == SHOTTYPE_HITTEAM && autocvar_crosshair_hittest_blur_teammate)
448                         || (shottype == SHOTTYPE_HITOBSTRUCTION && autocvar_crosshair_hittest_blur_wall && !autocvar_chase_active))
449                 {
450                         wcross_blur = 1;
451                         wcross_alpha *= 0.75;
452                 }
453                 else
454                         wcross_blur = 0;
455                 // *_prev is at time-frametime
456                 // * is at wcross_changedonetime+f
457                 // what do we have at time?
458                 if(time < wcross_changedonetime)
459                 {
460                         f = frametime / (wcross_changedonetime - time + frametime);
461                         wcross_scale = f * wcross_scale + (1 - f) * wcross_scale_prev;
462                         wcross_alpha = f * wcross_alpha + (1 - f) * wcross_alpha_prev;
463                         wcross_color = f * wcross_color + (1 - f) * wcross_color_prev;
464                 }
465
466                 wcross_scale_prev = wcross_scale;
467                 wcross_alpha_prev = wcross_alpha;
468                 wcross_color_prev = wcross_color;
469
470                 MUTATOR_CALLHOOK(UpdateCrosshair);
471
472                 wcross_scale *= 1 - autocvar__menu_alpha;
473                 wcross_alpha *= 1 - autocvar__menu_alpha;
474                 wcross_size = draw_getimagesize(wcross_name) * wcross_scale;
475
476                 if(wcross_scale >= 0.001 && wcross_alpha >= 0.001)
477                 {
478                         // crosshair rings for weapon stats
479                         if (autocvar_crosshair_ring || autocvar_crosshair_ring_reload)
480                         {
481                                 // declarations and stats
482                                 float ring_value = 0, ring_scale = 0, ring_alpha = 0, ring_inner_value = 0, ring_inner_alpha = 0;
483                                 string ring_image = string_null, ring_inner_image = string_null;
484                                 vector ring_rgb = '0 0 0', ring_inner_rgb = '0 0 0';
485
486                                 ring_scale = autocvar_crosshair_ring_size;
487
488                                 entity wepent = viewmodels[0]; // TODO: unhardcode
489
490                                 int weapon_clipload = wepent.clip_load;
491                                 int weapon_clipsize = wepent.clip_size;
492
493                                 float arc_heat = wepent.arc_heat_percent;
494
495                                 if(vortex_charge_movingavg == 0) // this should only happen if we have just loaded up the game
496                                         vortex_charge_movingavg = wepent.vortex_charge;
497
498                                 float charge = 0;
499                                 float chargepool = 0;
500                                 bool ring_vortex_enabled = false;
501                                 if (autocvar_crosshair_ring && autocvar_crosshair_ring_vortex)
502                                 {
503                                         if (wepent.activeweapon == WEP_VORTEX)
504                                         {
505                                                 charge = wepent.vortex_charge;
506                                                 chargepool = wepent.vortex_chargepool_ammo;
507                                         }
508                                         else if (wepent.activeweapon == WEP_OVERKILL_NEX)
509                                         {
510                                                 charge = wepent.oknex_charge;
511                                                 chargepool = wepent.oknex_chargepool_ammo;
512                                         }
513                                         if (charge)
514                                                 ring_vortex_enabled = true;
515                                 }
516
517                                 if (ring_vortex_enabled)
518                                 {
519                                         if (chargepool || use_vortex_chargepool) {
520                                                 use_vortex_chargepool = 1;
521                                                 ring_inner_value = chargepool;
522                                         } else {
523                                                 float rate = autocvar_crosshair_ring_vortex_currentcharge_movingavg_rate;
524                                                 vortex_charge_movingavg = (1 - rate) * vortex_charge_movingavg + rate * charge;
525                                                 ring_inner_value = bound(0, autocvar_crosshair_ring_vortex_currentcharge_scale * (charge - vortex_charge_movingavg), 1);
526                                         }
527
528                                         ring_inner_alpha = autocvar_crosshair_ring_vortex_inner_alpha;
529                                         ring_inner_rgb = vec3(autocvar_crosshair_ring_vortex_inner_color_red, autocvar_crosshair_ring_vortex_inner_color_green, autocvar_crosshair_ring_vortex_inner_color_blue);
530                                         ring_inner_image = "gfx/crosshair_ring_inner.tga";
531
532                                         // draw the outer ring to show the current charge of the weapon
533                                         ring_value = charge;
534                                         ring_alpha = autocvar_crosshair_ring_vortex_alpha;
535                                         ring_rgb = wcross_color;
536                                         ring_image = "gfx/crosshair_ring_nexgun.tga";
537                                 }
538                                 else if (autocvar_crosshair_ring && wepent.activeweapon == WEP_MINE_LAYER && WEP_CVAR(minelayer, limit) && autocvar_crosshair_ring_minelayer)
539                                 {
540                                         ring_value = bound(0, wepent.minelayer_mines / WEP_CVAR(minelayer, limit), 1);
541                                         ring_alpha = autocvar_crosshair_ring_minelayer_alpha;
542                                         ring_rgb = wcross_color;
543                                         ring_image = "gfx/crosshair_ring.tga";
544                                 }
545                                 else if (wepent.activeweapon == WEP_HAGAR && wepent.hagar_load && autocvar_crosshair_ring_hagar)
546                                 {
547                                         ring_value = bound(0, wepent.hagar_load / WEP_CVAR_SEC(hagar, load_max), 1);
548                                         ring_alpha = autocvar_crosshair_ring_hagar_alpha;
549                                         ring_rgb = wcross_color;
550                                         ring_image = "gfx/crosshair_ring.tga";
551                                 }
552                                 else if(autocvar_crosshair_ring_reload && weapon_clipsize) // forces there to be only an ammo ring
553                                 {
554                                         ring_value = bound(0, weapon_clipload / weapon_clipsize, 1);
555                                         ring_scale = autocvar_crosshair_ring_reload_size;
556                                         ring_alpha = autocvar_crosshair_ring_reload_alpha;
557                                         ring_rgb = wcross_color;
558
559                                         // Note: This is to stop Taoki from complaining that the image doesn't match all potential balances.
560                                         // if a new image for another weapon is added, add the code (and its respective file/value) here
561                                         if ((wepent.activeweapon == WEP_RIFLE) && (weapon_clipsize == 80))
562                                                 ring_image = "gfx/crosshair_ring_rifle.tga";
563                                         else
564                                                 ring_image = "gfx/crosshair_ring.tga";
565                                 }
566                                 else if ( autocvar_crosshair_ring && autocvar_crosshair_ring_arc && arc_heat && wepent.activeweapon == WEP_ARC )
567                                 {
568                                         ring_value = arc_heat;
569                                         ring_alpha = (1-arc_heat)*autocvar_crosshair_ring_arc_cold_alpha +
570                                                 arc_heat*autocvar_crosshair_ring_arc_hot_alpha;
571                                         ring_rgb = (1-arc_heat)*wcross_color + arc_heat*autocvar_crosshair_ring_arc_hot_color;
572                                         ring_image = "gfx/crosshair_ring.tga";
573                                 }
574
575                                 // if in weapon switch animation, fade ring out/in
576                                 if(autocvar_crosshair_effect_time > 0)
577                                 {
578                                         f = (time - wcross_name_changestarttime) / autocvar_crosshair_effect_time;
579                                         if (f >= 1)
580                                         {
581                                                 wcross_ring_prev = ((ring_image) ? true : false);
582                                         }
583
584                                         if(wcross_ring_prev)
585                                         {
586                                                 if(f < 1)
587                                                         ring_alpha *= fabs(1 - bound(0, f, 1));
588                                         }
589                                         else
590                                         {
591                                                 if(f < 1)
592                                                         ring_alpha *= bound(0, f, 1);
593                                         }
594                                 }
595
596                                 if (autocvar_crosshair_ring_inner && ring_inner_value) // lets draw a ring inside a ring so you can ring while you ring
597                                         DrawCircleClippedPic(wcross_origin, wcross_size.x * wcross_resolution * ring_scale, ring_inner_image, ring_inner_value, ring_inner_rgb, wcross_alpha * ring_inner_alpha, DRAWFLAG_ADDITIVE);
598
599                                 if (ring_value)
600                                         DrawCircleClippedPic(wcross_origin, wcross_size.x * wcross_resolution * ring_scale, ring_image, ring_value, ring_rgb, wcross_alpha * ring_alpha, DRAWFLAG_ADDITIVE);
601                         }
602
603 #define CROSSHAIR_DO_BLUR(M,sz,wcross_name,wcross_alpha) \
604                         MACRO_BEGIN \
605                                 vector scaled_sz = sz * wcross_size; \
606                                 if(wcross_blur > 0) \
607                                 { \
608                                         for(i = -2; i <= 2; ++i) \
609                                         for(j = -2; j <= 2; ++j) \
610                                         M(i,j,sz,scaled_sz,wcross_name,wcross_alpha*0.04); \
611                                 } \
612                                 else \
613                                 { \
614                                         M(0,0,sz,scaled_sz,wcross_name,wcross_alpha); \
615                                 } \
616                         MACRO_END
617
618 #define CROSSHAIR_DRAW_SINGLE(i,j,sz,scaled_sz,wcross_name,wcross_alpha) \
619                         drawpic(wcross_origin - ('0.5 0 0' * (scaled_sz.x + i * wcross_blur) + '0 0.5 0' * (scaled_sz.y + j * wcross_blur)), wcross_name, scaled_sz, wcross_color, wcross_alpha, DRAWFLAG_NORMAL)
620
621 #define CROSSHAIR_DRAW(sz,wcross_name,wcross_alpha) \
622                         CROSSHAIR_DO_BLUR(CROSSHAIR_DRAW_SINGLE,sz,wcross_name,wcross_alpha)
623
624                         if(time < wcross_name_changedonetime && wcross_name != wcross_name_goal_prev_prev && wcross_name_goal_prev_prev)
625                         {
626                                 f = (wcross_name_changedonetime - time) / (wcross_name_changedonetime - wcross_name_changestarttime);
627                                 wcross_size = draw_getimagesize(wcross_name_goal_prev_prev) * wcross_scale;
628                                 CROSSHAIR_DRAW(wcross_resolution_goal_prev_prev, wcross_name_goal_prev_prev, wcross_alpha * f * wcross_name_alpha_goal_prev_prev);
629                                 f = 1 - f;
630                         }
631                         else
632                         {
633                                 f = 1;
634                         }
635                         wcross_name_alpha_goal_prev = f;
636
637                         wcross_size = draw_getimagesize(wcross_name) * wcross_scale;
638                         CROSSHAIR_DRAW(wcross_resolution, wcross_name, wcross_alpha * f);
639
640                         if(autocvar_crosshair_dot)
641                         {
642                                 vector wcross_color_old;
643                                 wcross_color_old = wcross_color;
644
645                                 if((autocvar_crosshair_dot_color_custom) && (autocvar_crosshair_dot_color != "0"))
646                                         wcross_color = stov(autocvar_crosshair_dot_color);
647
648                                 CROSSHAIR_DRAW(wcross_resolution * autocvar_crosshair_dot_size, "gfx/crosshairdot.tga", f * autocvar_crosshair_dot_alpha);
649                                 // FIXME why don't we use wcross_alpha here?
650                                 wcross_color = wcross_color_old;
651                         }
652                 }
653         }
654         else
655         {
656                 wcross_scale_prev = 0;
657                 wcross_alpha_prev = 0;
658                 wcross_scale_goal_prev = 0;
659                 wcross_alpha_goal_prev = 0;
660                 wcross_changedonetime = 0;
661                 strfree(wcross_name_goal_prev);
662                 strfree(wcross_name_goal_prev_prev);
663                 wcross_name_changestarttime = 0;
664                 wcross_name_changedonetime = 0;
665                 wcross_name_alpha_goal_prev = 0;
666                 wcross_name_alpha_goal_prev_prev = 0;
667                 wcross_resolution_goal_prev = 0;
668                 wcross_resolution_goal_prev_prev = 0;
669         }
670 }
671
672 void DrawReticle(entity this)
673 {
674         if(!autocvar_cl_reticle || MUTATOR_CALLHOOK(DrawReticle))
675         {
676                 reticle_type = 0;
677                 return;
678         }
679
680         float is_dead = (STAT(HEALTH) <= 0);
681         string reticle_image = string_null;
682         bool wep_zoomed = false;
683         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
684         {
685                 entity wepe = viewmodels[slot];
686                 Weapon wep = wepe.activeweapon;
687                 if(wep != WEP_Null && wep.wr_zoom)
688                 {
689                         bool do_zoom = wep.wr_zoom(wep, NULL);
690                         if(!reticle_image && wep.w_reticle && wep.w_reticle != "")
691                                 reticle_image = wep.w_reticle;
692                         wep_zoomed += do_zoom;
693                 }
694         }
695         // Draw the aiming reticle for weapons that use it
696         // reticle_type is changed to the item we are zooming / aiming with, to decide which reticle to use
697         // It must be a persisted float for fading out to work properly (you let go of the zoom button for
698         // the view to go back to normal, so reticle_type would become 0 as we fade out)
699         if(spectatee_status || is_dead || hud != HUD_NORMAL || this.viewloc)
700         {
701                 // no zoom reticle while dead
702                 reticle_type = 0;
703         }
704         else if(wep_zoomed && autocvar_cl_reticle_weapon)
705         {
706                 if(reticle_image) { reticle_type = 2; }
707                 else { reticle_type = 0; }
708         }
709         else if(button_zoom || zoomscript_caught)
710         {
711                 // normal zoom
712                 reticle_type = 1;
713         }
714
715         if(reticle_type)
716         {
717                 vector reticle_pos = '0 0 0', reticle_size = '0 0 0';
718                 if(autocvar_cl_reticle_stretch)
719                 {
720                         reticle_size.x = vid_conwidth;
721                         reticle_size.y = vid_conheight;
722                         reticle_pos.x = 0;
723                         reticle_pos.y = 0;
724                 }
725                 else
726                 {
727                         reticle_size.x = max(vid_conwidth, vid_conheight);
728                         reticle_size.y = max(vid_conwidth, vid_conheight);
729                         reticle_pos.x = (vid_conwidth - reticle_size.x) / 2;
730                         reticle_pos.y = (vid_conheight - reticle_size.y) / 2;
731                 }
732
733                 float f = (zoomscript_caught) ? 1 : current_zoomfraction;
734
735                 if(f)
736                 {
737                         switch(reticle_type)
738                         {
739                                 case 1: drawpic(reticle_pos, "gfx/reticle_normal", reticle_size, '1 1 1', f * autocvar_cl_reticle_normal_alpha, DRAWFLAG_NORMAL); break;
740                                 case 2: if(reticle_image) drawpic(reticle_pos, reticle_image, reticle_size, '1 1 1', f * autocvar_cl_reticle_weapon_alpha, DRAWFLAG_NORMAL); break;
741                         }
742                 }
743         }
744 }