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