]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/powerups.qc
client: pass compilation units test
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / powerups.qc
1 #include "powerups.qh"
2
3 #include <common/items/all.qc>
4
5 // Powerups (#2)
6
7 // Powerup item fields (reusing existing fields)
8 .string message;  // Human readable name
9 .string netname;  // Icon name
10 .vector colormod; // Color
11 .float count;     // Time left
12 .float lifetime;  // Maximum time
13
14 entity powerupItems;
15 int powerupItemsCount;
16
17 void resetPowerupItems()
18 {
19         entity item;
20         for(item = powerupItems; item; item = item.chain)
21                 item.count = 0;
22
23         powerupItemsCount = 0;
24 }
25
26 void addPowerupItem(string name, string icon, vector color, float currentTime, float lifeTime)
27 {
28         if(!powerupItems)
29                 powerupItems = spawn();
30
31         entity item;
32         for(item = powerupItems; item.count; item = item.chain)
33                 if(!item.chain)
34                         item.chain = spawn();
35
36         item.message  = name;
37         item.netname  = icon;
38         item.colormod = color;
39         item.count    = currentTime;
40         item.lifetime = lifeTime;
41
42         ++powerupItemsCount;
43 }
44
45 int getPowerupItemAlign(int align, int column, int row, int columns, int rows, bool isVertical)
46 {
47         if(align < 2)
48                 return align;
49
50         bool isTop    =  isVertical && rows > 1 && row == 0;
51         bool isBottom =  isVertical && rows > 1 && row == rows-1;
52         bool isLeft   = !isVertical && columns > 1 && column == 0;
53         bool isRight  = !isVertical && columns > 1 && column == columns-1;
54
55         if(isTop    || isLeft)  return (align == 2) ? 1 : 0;
56         if(isBottom || isRight) return (align == 2) ? 0 : 1;
57
58         return 2;
59 }
60
61 void HUD_Powerups()
62 {
63         int allItems = STAT(ITEMS);
64         int allBuffs = STAT(BUFFS);
65         int strengthTime, shieldTime, superTime;
66
67         // Initialize items
68         if(!autocvar__hud_configure)
69         {
70                 if(!autocvar_hud_panel_powerups) return;
71                 if(spectatee_status == -1) return;
72                 if(STAT(HEALTH) <= 0) return;
73                 if(!(allItems & (ITEM_Strength.m_itemid | ITEM_Shield.m_itemid | IT_SUPERWEAPON)) && !allBuffs) return;
74
75                 strengthTime = bound(0, STAT(STRENGTH_FINISHED) - time, 99);
76                 shieldTime = bound(0, STAT(INVINCIBLE_FINISHED) - time, 99);
77                 superTime = bound(0, STAT(SUPERWEAPONS_FINISHED) - time, 99);
78
79                 if(allItems & IT_UNLIMITED_SUPERWEAPONS)
80                         superTime = 99;
81
82                 // Prevent stuff to show up on mismatch that will be fixed next frame
83                 if(!(allItems & IT_SUPERWEAPON))
84                         superTime = 0;
85         }
86         else
87         {
88                 strengthTime = 15;
89                 shieldTime = 27;
90                 superTime = 13;
91                 allBuffs = 0;
92         }
93
94         // Add items to linked list
95         resetPowerupItems();
96
97         if(strengthTime)
98                 addPowerupItem("Strength", "strength", autocvar_hud_progressbar_strength_color, strengthTime, 30);
99         if(shieldTime)
100                 addPowerupItem("Shield", "shield", autocvar_hud_progressbar_shield_color, shieldTime, 30);
101         if(superTime)
102                 addPowerupItem("Superweapons", "superweapons", autocvar_hud_progressbar_superweapons_color, superTime, 30);
103
104         MUTATOR_CALLHOOK(HUD_Powerups_add);
105
106         if(!powerupItemsCount)
107                 return;
108
109         // Draw panel background
110         HUD_Panel_UpdateCvars();
111         HUD_Panel_DrawBg(1);
112
113         // Set drawing area
114         vector pos = panel_pos;
115         vector size = panel_size;
116         bool isVertical = size.y > size.x;
117
118         if(panel_bg_padding)
119         {
120                 pos += '1 1 0' * panel_bg_padding;
121                 size -= '2 2 0' * panel_bg_padding;
122         }
123
124         // Find best partitioning of the drawing area
125         const float DESIRED_ASPECT = 6;
126         float aspect = 0, a;
127         int columns = 0, c;
128         int rows = 0, r;
129         int i = 1;
130
131         do
132         {
133                 c = floor(powerupItemsCount / i);
134                 r = ceil(powerupItemsCount / c);
135                 a = isVertical ? (size.y/r) / (size.x/c) : (size.x/c) / (size.y/r);
136
137                 if(i == 1 || fabs(DESIRED_ASPECT - a) < fabs(DESIRED_ASPECT - aspect))
138                 {
139                         aspect = a;
140                         columns = c;
141                         rows = r;
142                 }
143         }
144         while(++i <= powerupItemsCount);
145
146         // Prevent single items from getting too wide
147         if(powerupItemsCount == 1 && aspect > DESIRED_ASPECT)
148         {
149                 if(isVertical)
150                 {
151                         size.y *= 0.5;
152                         pos.y += size.y * 0.5;
153                 }
154                 else
155                 {
156                         size.x *= 0.5;
157                         pos.x += size.x * 0.5;
158                 }
159         }
160
161         // Draw items from linked list
162         vector itemPos = pos;
163         vector itemSize = eX * (size.x / columns) + eY * (size.y / rows);
164         vector textColor = '1 1 1';
165
166         int fullSeconds = 0;
167         int align = 0;
168         int column = 0;
169         int row = 0;
170
171         draw_beginBoldFont();
172         for(entity item = powerupItems; item.count; item = item.chain)
173         {
174                 itemPos = eX * (pos.x + column * itemSize.x) + eY * (pos.y + row * itemSize.y);
175
176                 // Draw progressbar
177                 if(autocvar_hud_panel_powerups_progressbar)
178                 {
179                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_baralign, column, row, columns, rows, isVertical);
180                         HUD_Panel_DrawProgressBar(itemPos, itemSize, "progressbar", item.count / item.lifetime, isVertical, align, item.colormod, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
181                 }
182
183                 // Draw icon and text
184                 if(autocvar_hud_panel_powerups_text)
185                 {
186                         align = getPowerupItemAlign(autocvar_hud_panel_powerups_iconalign, column, row, columns, rows, isVertical);
187                         fullSeconds = ceil(item.count);
188                         textColor = '0.6 0.6 0.6' + (item.colormod * 0.4);
189
190                         if(item.count > 1)
191                                 DrawNumIcon(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha);
192                         if(item.count <= 5)
193                                 DrawNumIcon_expanding(itemPos, itemSize, fullSeconds, item.netname, isVertical, align, textColor, panel_fg_alpha, bound(0, (fullSeconds - item.count) / 0.5, 1));
194                 }
195
196                 // Determine next section
197                 if(isVertical)
198                 {
199                         if(++column >= columns)
200                         {
201                                 column = 0;
202                                 ++row;
203                         }
204                 }
205                 else
206                 {
207                         if(++row >= rows)
208                         {
209                                 row = 0;
210                                 ++column;
211                         }
212                 }
213         }
214         draw_endBoldFont();
215 }