Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
ammo.qc
Go to the documentation of this file.
1#include "ammo.qh"
2
3#include <client/draw.qh>
5#include <client/view.qh>
8#include <common/wepent.qh>
9
10// Ammo (#1)
11
12void HUD_Ammo_Export(int fh)
13{
14 // allow saving cvars that aesthetically change the panel into hud skin files
15 HUD_Write_Cvar("hud_panel_ammo_onlycurrent");
16 HUD_Write_Cvar("hud_panel_ammo_noncurrent_alpha");
17 HUD_Write_Cvar("hud_panel_ammo_noncurrent_scale");
18 HUD_Write_Cvar("hud_panel_ammo_iconalign");
19 HUD_Write_Cvar("hud_panel_ammo_progressbar");
20 HUD_Write_Cvar("hud_panel_ammo_progressbar_name");
21 HUD_Write_Cvar("hud_panel_ammo_progressbar_xoffset");
22 HUD_Write_Cvar("hud_panel_ammo_text");
23}
24
34
35void DrawAmmoItem(vector myPos, vector mySize, Resource ammoType, bool isCurrent, bool isInfinite)
36{
37 TC(bool, isCurrent); TC(bool, isInfinite);
38 if (ammoType == RES_NONE)
39 return;
40
41 // Initialize variables
42
43 int ammo;
45 {
46 isCurrent = (ammoType == RES_ROCKETS); // Rockets always current
47 ammo = 60;
48 }
49 else
50 ammo = getstati(GetAmmoStat(ammoType));
51
52 if (!isCurrent)
53 {
55 myPos += (mySize - mySize * scale) * 0.5;
56 mySize *= scale;
57 }
58
59 vector iconPos, textPos;
61 {
62 iconPos = myPos + eX * 2 * mySize.y;
63 textPos = myPos;
64 }
65 else
66 {
67 iconPos = myPos;
68 textPos = myPos + eX * mySize.y;
69 }
70
71 bool isShadowed = (ammo <= 0 && !isCurrent && !isInfinite);
72
73 vector iconColor = isShadowed ? '0 0 0' : '1 1 1';
74 vector textColor;
75 if (isInfinite) textColor = '0.2 0.95 0';
76 else if (isShadowed) textColor = '0 0 0';
77 else if (ammo < 10) textColor = '0.8 0.04 0';
78 else textColor = '1 1 1';
79
80 float alpha;
81 if (isCurrent)
83 else if (isShadowed)
85 else
87
88 string text = isInfinite ? "\xE2\x88\x9E" : ftos(ammo); // Use infinity symbol (U+221E)
89
90 // Draw item
91
92 if (isCurrent)
93 drawpic_aspect_skin(myPos, "ammo_current_bg", mySize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
94
97
99 drawstring_aspect(textPos, text, eX * (2/3) * mySize.x + eY * mySize.y, textColor * 0.6 + ammoType.m_color * 0.4, alpha, DRAWFLAG_NORMAL);
100
101 drawpic_aspect_skin(iconPos, ammoType.m_icon, '1 1 0' * mySize.y, iconColor, alpha, DRAWFLAG_NORMAL);
102}
103
107
109{
110 if (hud != HUD_NORMAL)
111 return;
113 {
115 return;
116 if (STAT(HEALTH) <= 0 && autocvar_hud_panel_ammo_hide_ondeath)
117 return;
118 }
119
121
123
124 vector pos = panel_pos;
125 vector mySize = panel_size;
126
129 else
133 {
134 pos += '1 1 0' * panel_bg_padding;
135 mySize -= '2 2 0' * panel_bg_padding;
136 }
137
138 int rows = 0, columns, row, column;
139 float nade_cnt = STAT(NADE_BONUS), nade_score = STAT(NADE_BONUS_SCORE);
140 bool draw_nades = (nade_cnt > 0 || nade_score > 0);
141 float nade_statuschange_elapsedtime;
142 int total_ammo_count;
143
144 vector ammo_size;
146 total_ammo_count = 1;
147 else
148 total_ammo_count = AMMO_COUNT;
149
150 if (draw_nades)
151 {
152 ++total_ammo_count;
153 if (nade_cnt != nade_prevframe)
154 {
157 nade_prevframe = nade_cnt;
158 }
159 }
160 else
162
163 rows = HUD_GetRowCount(total_ammo_count, mySize, 3);
164 columns = ceil(total_ammo_count / rows);
165 ammo_size = vec2(mySize.x / columns, mySize.y / rows);
166
167 vector offset = '0 0 0';
168 float newSize;
169 if (ammo_size.x / ammo_size.y > 3)
170 {
171 newSize = 3 * ammo_size.y;
172 offset.x = ammo_size.x - newSize;
173 pos.x += offset.x * 0.5;
174 ammo_size.x = newSize;
175 }
176 else
177 {
178 newSize = 1/3 * ammo_size.x;
179 offset.y = ammo_size.y - newSize;
180 pos.y += offset.y * 0.5;
181 ammo_size.y = newSize;
182 }
183
184 entity wepent = viewmodels[0]; // TODO: unhardcode
185
186 Weapon wep = wepent.switchweapon;
187 int i;
188 bool infinite_ammo = (STAT(ITEMS) & IT_UNLIMITED_AMMO);
189 row = column = 0;
191 {
193 DrawAmmoItem(pos, ammo_size, RES_ROCKETS, true, false);
194 else
196 pos,
197 ammo_size,
198 wep.ammo_type,
199 true,
200 infinite_ammo
201 );
202
203 ++row;
204 if (row >= rows)
205 {
206 row = 0;
207 ++column;
208 }
209 }
210 else
211 {
212 row = column = 0;
213 IL_EACH(default_order_resources, it.instanceOfAmmoResource && !it.m_hidden,
214 {
215 DrawAmmoItem(
216 pos + vec2(column * (ammo_size.x + offset.x), row * (ammo_size.y + offset.y)),
217 ammo_size,
218 it,
219 (wep.ammo_type == it),
220 infinite_ammo
221 );
222
223 ++row;
224 if (row >= rows)
225 {
226 row = 0;
227 ++column;
228 }
229 });
230 }
231
232 if (draw_nades)
233 {
234 nade_statuschange_elapsedtime = time - nade_statuschange_time;
235
236 float f = bound(0, nade_statuschange_elapsedtime * 2, 1);
237 DrawAmmoNades(pos + vec2(column * (ammo_size.x + offset.x), row * (ammo_size.y + offset.y)), ammo_size, nade_prevstatus < nade_cnt && nade_cnt != 0 && f < 1, f);
238 }
239
241}
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void DrawAmmoNades(vector myPos, vector mySize, bool draw_expanding, float expand_time)
Definition cl_nades.qc:81
string m_icon
Definition resources.qh:32
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:44
Resource ammo_type
M: ammotype : main ammo type.
Definition weapon.qh:51
void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag)
Definition draw.qc:110
#define drawpic_aspect_skin(pos, pic, sz, color, theAlpha, drawflag)
Definition draw.qh:78
#define draw_beginBoldFont()
Definition draw.qh:4
#define draw_endBoldFont()
Definition draw.qh:5
int nade_prevstatus
Definition ammo.qc:104
void HUD_Ammo_Export(int fh)
Definition ammo.qc:12
void HUD_Ammo()
Definition ammo.qc:108
void DrawAmmoItem(vector myPos, vector mySize, Resource ammoType, bool isCurrent, bool isInfinite)
Definition ammo.qc:35
void DrawNadeProgressBar(vector myPos, vector mySize, float progress, vector color)
Definition ammo.qc:25
int nade_prevframe
Definition ammo.qc:105
float nade_statuschange_time
Definition ammo.qc:106
bool autocvar_hud_panel_ammo
Definition ammo.qh:4
bool autocvar_hud_panel_ammo_text
Definition ammo.qh:15
bool autocvar_hud_panel_ammo_progressbar
Definition ammo.qh:12
bool autocvar_hud_panel_ammo_hide_ondeath
Definition ammo.qh:6
bool autocvar_hud_panel_ammo_dynamichud
Definition ammo.qh:5
bool autocvar_hud_panel_ammo_onlycurrent
Definition ammo.qh:9
int autocvar_hud_panel_ammo_maxammo
Definition ammo.qh:8
bool autocvar_hud_panel_ammo_iconalign
Definition ammo.qh:7
string autocvar_hud_panel_ammo_progressbar_name
Definition ammo.qh:13
float autocvar_hud_panel_ammo_noncurrent_alpha
Definition ammo.qh:10
float autocvar_hud_panel_ammo_noncurrent_scale
Definition ammo.qh:11
float autocvar_hud_panel_ammo_progressbar_xoffset
Definition ammo.qh:14
float alpha
Definition items.qc:13
const int AMMO_COUNT
Definition items.qh:3
int spectatee_status
the -1 disables HUD panels before CSQC receives necessary data
Definition main.qh:197
int hud
Definition main.qh:173
const int IT_UNLIMITED_AMMO
Definition item.qh:23
const int HUD_NORMAL
Definition constants.qh:47
const float DRAWFLAG_NORMAL
float time
vector color
Definition dynlight.qc:15
void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
Definition hud.qc:269
void HUD_Panel_LoadCvars()
Definition hud.qc:215
float HUD_GetRowCount(int item_count, vector size, float item_aspect)
Definition hud.qc:165
void HUD_Scale_Enable()
Definition hud.qc:91
void HUD_Scale_Disable()
Definition hud.qc:84
float autocvar_hud_progressbar_alpha
Definition hud.qh:204
vector panel_size
Definition hud.qh:163
float panel_fg_alpha
Definition hud.qh:169
float panel_bg_padding
Definition hud.qh:174
#define HUD_Panel_DrawBg()
Definition hud.qh:55
vector panel_pos
Definition hud.qh:162
#define HUD_Write_Cvar(cvar)
Definition hud_config.qh:40
bool autocvar__hud_configure
Definition hud_config.qh:3
#define IL_EACH(this, cond, body)
#define TC(T, sym)
Definition _all.inc:82
#define STAT(...)
Definition stats.qh:82
float ceil(float f)
float bound(float min, float value, float max)
string ftos(float f)
float scale
Definition projectile.qc:14
IntrusiveList default_order_resources
Definition resources.qh:42
vector
Definition self.qh:92
float ammo
Definition sv_turrets.qh:43
const vector eY
Definition vector.qh:45
const vector eX
Definition vector.qh:44
#define vec2(...)
Definition vector.qh:90
entity viewmodels[MAX_WEAPONSLOTS]
Definition view.qh:108
int GetAmmoStat(Resource ammotype)
Definition all.qc:210