Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
powerups.qc
Go to the documentation of this file.
1#include "powerups.qh"
2
3#include <client/draw.qh>
5#include <common/util.qh>
6
7// Powerups (#2)
8
10{
11 // allow saving cvars that aesthetically change the panel into hud skin files
12 HUD_Write_Cvar("hud_panel_powerups_iconalign");
13 HUD_Write_Cvar("hud_panel_powerups_baralign");
14 HUD_Write_Cvar("hud_panel_powerups_progressbar");
15 HUD_Write_Cvar("hud_panel_powerups_text");
16}
17
18// Powerup item fields (reusing existing fields)
19.string message; // Human readable name
20.string netname; // Icon name
21.vector colormod; // Color
22.float count; // Time left
23.float lifetime; // Maximum time
24.float cnt; // Infinite timer
25
28
30{
31 entity item;
32 for (item = powerupItems; item; item = item.chain)
33 item.count = 0;
34
36}
37
38void addPowerupItem(string name, string icon, vector color, float currentTime, float lifeTime, bool isInfinite)
39{
40 if (!powerupItems)
42
43 entity item;
44 for (item = powerupItems; item.count; item = item.chain)
45 if (!item.chain)
46 item.chain = spawn();
47
48 item.message = name;
49 item.netname = icon;
50 item.colormod = color;
51 item.count = currentTime;
52 item.lifetime = lifeTime;
53 item.cnt = isInfinite;
54
56}
57
58int getPowerupItemAlign(int align, int column, int row, int columns, int rows, bool isVertical)
59{
60 TC(int, align); TC(int, column); TC(int, row); TC(int, columns); TC(int, rows); TC(bool, isVertical);
61 if (align < 2)
62 return align;
63
64 bool isTop = isVertical && rows > 1 && row == 0;
65 bool isBottom = isVertical && rows > 1 && row == rows - 1;
66 bool isLeft = !isVertical && columns > 1 && column == 0;
67 bool isRight = !isVertical && columns > 1 && column == columns - 1;
68
69 if (isTop || isLeft) return (align == 2) ? 1 : 0;
70 if (isBottom || isRight) return (align == 2) ? 0 : 1;
71
72 return 2;
73}
74
76{
77 // Initialize items
79 {
81 return;
83 return;
84 }
85
86 // Add items to linked list
88
89 MUTATOR_CALLHOOK(HUD_Powerups_add);
90
92 return;
93
94 // Draw panel background
96
99 else
102
103 // Set drawing area
104 vector pos = panel_pos;
106 bool isVertical = size.y > size.x;
107
109 {
110 pos += '1 1 0' * panel_bg_padding;
111 size -= '2 2 0' * panel_bg_padding;
112 }
113
114 // Find best partitioning of the drawing area
115 const float DESIRED_ASPECT = 6;
116 float aspect = 0, a;
117 int columns = 0, c;
118 int rows = 0, r;
119 int i = 1;
120
121 do
122 {
123 c = floor(powerupItemsCount / i);
124 r = ceil(powerupItemsCount / c);
125 a = isVertical
126 ? (size.y / r) / (size.x / c)
127 : (size.x / c) / (size.y / r);
128
129 if (i == 1 || fabs(DESIRED_ASPECT - a) < fabs(DESIRED_ASPECT - aspect))
130 {
131 aspect = a;
132 columns = c;
133 rows = r;
134 }
135 }
136 while (++i <= powerupItemsCount);
137
138 // Prevent single items from getting too wide
139 if (powerupItemsCount == 1 && aspect > DESIRED_ASPECT)
140 {
141 if (isVertical)
142 {
143 size.y *= 0.5;
144 pos.y += size.y * 0.5;
145 }
146 else
147 {
148 size.x *= 0.5;
149 pos.x += size.x * 0.5;
150 }
151 }
152
153 // Draw items from linked list
154 vector itemPos = pos;
155 vector itemSize = vec2(size.x / columns, size.y / rows);
156 vector textColor = '1 1 1';
157
158 int fullSeconds = 0;
159 int align = 0;
160 int column = 0;
161 int row = 0;
162
164 for (entity item = powerupItems; item.count; item = item.chain)
165 {
166 itemPos = vec2(pos.x + column * itemSize.x, pos.y + row * itemSize.y);
167
168 // Draw progressbar
170 {
171 align = getPowerupItemAlign(autocvar_hud_panel_powerups_baralign, column, row, columns, rows, isVertical);
172 HUD_Panel_DrawProgressBar(itemPos, itemSize, "progressbar", item.count / item.lifetime, isVertical, align, item.colormod, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
173 }
174
175 // Draw icon and text
177 {
178 align = getPowerupItemAlign(autocvar_hud_panel_powerups_iconalign, column, row, columns, rows, isVertical);
179 fullSeconds = ceil(item.count);
180 textColor = '0.6 0.6 0.6' + (item.colormod * 0.4);
181
182 if (item.cnt)
183 DrawNumIcon(itemPos, itemSize, fullSeconds, item.netname, isVertical, true, align, textColor, panel_fg_alpha);
184 else
185 {
186 if (item.count > 1)
187 DrawNumIcon(itemPos, itemSize, fullSeconds, item.netname, isVertical, false, align, textColor, panel_fg_alpha);
188 if (item.count <= 5)
189 DrawNumIcon_expanding(itemPos, itemSize, fullSeconds, item.netname, isVertical, false, align, textColor, panel_fg_alpha, bound(0, (fullSeconds - item.count) * 2, 1));
190 }
191 }
192
193 // Determine next section
194 if (isVertical)
195 {
196 if (++column >= columns)
197 {
198 column = 0;
199 ++row;
200 }
201 }
202 else
203 {
204 if (++row >= rows)
205 {
206 row = 0;
207 ++column;
208 }
209 }
210 }
212}
#define MUTATOR_CALLHOOK(id,...)
Definition base.qh:143
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
#define draw_beginBoldFont()
Definition draw.qh:4
#define draw_endBoldFont()
Definition draw.qh:5
entity powerupItems
Definition powerups.qc:26
string netname
Definition powerups.qc:20
void addPowerupItem(string name, string icon, vector color, float currentTime, float lifeTime, bool isInfinite)
Definition powerups.qc:38
float lifetime
Definition powerups.qc:23
int getPowerupItemAlign(int align, int column, int row, int columns, int rows, bool isVertical)
Definition powerups.qc:58
void resetPowerupItems()
Definition powerups.qc:29
float cnt
Definition powerups.qc:24
void HUD_Powerups_Export(int fh)
Definition powerups.qc:9
string message
Definition powerups.qc:19
vector colormod
Definition powerups.qc:21
int powerupItemsCount
Definition powerups.qc:27
void HUD_Powerups()
Definition powerups.qc:75
float count
Definition powerups.qc:22
bool autocvar_hud_panel_powerups_dynamichud
Definition powerups.qh:6
bool autocvar_hud_panel_powerups_text
Definition powerups.qh:10
int autocvar_hud_panel_powerups_baralign
Definition powerups.qh:5
bool autocvar_hud_panel_powerups_hide_ondeath
Definition powerups.qh:7
int autocvar_hud_panel_powerups_iconalign
Definition powerups.qh:8
bool autocvar_hud_panel_powerups_progressbar
Definition powerups.qh:9
bool autocvar_hud_panel_powerups
Definition powerups.qh:4
int spectatee_status
the -1 disables HUD panels before CSQC receives necessary data
Definition main.qh:197
const float DRAWFLAG_NORMAL
vector size
#define spawn
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
void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, bool isInfinite, int icon_right_align, vector color, float theAlpha)
Definition hud.qc:474
void HUD_Scale_Enable()
Definition hud.qc:91
void HUD_Scale_Disable()
Definition hud.qc:84
void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, bool isInfinite, int icon_right_align, vector color, float theAlpha, float fadelerp)
Definition hud.qc:393
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 TC(T, sym)
Definition _all.inc:82
#define STAT(...)
Definition stats.qh:82
string name
Definition menu.qh:30
float ceil(float f)
float bound(float min, float value, float max)
float fabs(float f)
float floor(float f)
vector
Definition self.qh:92
#define vec2(...)
Definition vector.qh:90