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