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