make sure leadlimit is >= 0
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / healtharmor.qc
1 #include "healtharmor.qh"
2
3 #include <client/defs.qh>
4 #include <client/miscfunctions.qh>
5
6 #include <common/deathtypes/all.qh>
7
8 // Health/armor (#3)
9
10 void HUD_HealthArmor()
11 {
12         int armor, health, fuel;
13         if(!autocvar__hud_configure)
14         {
15                 if((!autocvar_hud_panel_healtharmor) || (spectatee_status == -1))
16                         return;
17                 if(hud != HUD_NORMAL) return;
18
19                 health = STAT(HEALTH);
20                 if(health <= 0)
21                 {
22                         health = 0;
23                         prev_health = -1;
24                         if(autocvar_hud_panel_healtharmor_hide_ondeath)
25                                 return;
26                 }
27                 armor = STAT(ARMOR);
28
29                 // code to check for spectatee_status changes is in ENT_CLIENT_CLIENTDATA
30                 // prev_p_health and prev_health can be set to -1 there
31
32                 if (prev_p_health == -1)
33                 {
34                         // no effect
35                         health_beforedamage = 0;
36                         armor_beforedamage = 0;
37                         health_damagetime = 0;
38                         armor_damagetime = 0;
39                         prev_health = health;
40                         prev_armor = armor;
41                         old_p_health = health;
42                         old_p_armor = armor;
43                         prev_p_health = health;
44                         prev_p_armor = armor;
45                 }
46                 else if (prev_health == -1)
47                 {
48                         //start the load effect
49                         health_damagetime = 0;
50                         armor_damagetime = 0;
51                         prev_health = 0;
52                         prev_armor = 0;
53                 }
54                 fuel = STAT(FUEL);
55         }
56         else
57         {
58                 health = 150;
59                 armor = 75;
60                 fuel = 20;
61         }
62
63         HUD_Panel_LoadCvars();
64
65         draw_beginBoldFont();
66
67         vector pos, mySize;
68         pos = panel_pos;
69         mySize = panel_size;
70
71         if (autocvar_hud_panel_healtharmor_dynamichud)
72                 HUD_Scale_Enable();
73         else
74                 HUD_Scale_Disable();
75         HUD_Panel_DrawBg();
76         if(panel_bg_padding)
77         {
78                 pos += '1 1 0' * panel_bg_padding;
79                 mySize -= '2 2 0' * panel_bg_padding;
80         }
81
82         int baralign = autocvar_hud_panel_healtharmor_baralign;
83         int iconalign = autocvar_hud_panel_healtharmor_iconalign;
84
85         int maxhealth = autocvar_hud_panel_healtharmor_maxhealth;
86         int maxarmor = autocvar_hud_panel_healtharmor_maxarmor;
87         if(autocvar_hud_panel_healtharmor_combined) // combined health and armor display
88         {
89                 vector v = healtharmor_maxdamage(health, armor, armorblockpercent, DEATH_WEAPON.m_id);
90                 float hp = floor(v.x + 1);
91
92                 float maxtotal = maxhealth + maxarmor;
93                 string biggercount;
94                 if(v.z) // NOT fully armored
95                 {
96                         biggercount = "health";
97                         if(autocvar_hud_panel_healtharmor_progressbar)
98                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_health, hp/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
99                         if(armor && autocvar_hud_panel_healtharmor_text)
100                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "armor", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha * armor / health, DRAWFLAG_NORMAL);
101                 }
102                 else
103                 {
104                         biggercount = "armor";
105                         if(autocvar_hud_panel_healtharmor_progressbar)
106                                 HUD_Panel_DrawProgressBar(pos, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, hp/maxtotal, 0, (baralign == 1 || baralign == 2), autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
107                         if(health && autocvar_hud_panel_healtharmor_text)
108                                 drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "health", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
109                 }
110                 if(autocvar_hud_panel_healtharmor_text)
111                         DrawNumIcon(pos, mySize, hp, biggercount, 0, iconalign, HUD_Get_Num_Color(hp, maxtotal), 1);
112
113                 if(fuel)
114                         HUD_Panel_DrawProgressBar(pos, vec2(mySize.x, 0.2 * mySize.y), "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
115         }
116         else
117         {
118                 float panel_ar = mySize.x/mySize.y;
119                 bool is_vertical = (panel_ar < 1);
120                 vector health_offset = '0 0 0', armor_offset = '0 0 0';
121                 if (panel_ar >= 4 || (panel_ar >= 1/4 && panel_ar < 1))
122                 {
123                         mySize.x *= 0.5;
124                         if (autocvar_hud_panel_healtharmor_flip)
125                                 health_offset.x = mySize.x;
126                         else
127                                 armor_offset.x = mySize.x;
128                 }
129                 else
130                 {
131                         mySize.y *= 0.5;
132                         if (autocvar_hud_panel_healtharmor_flip)
133                                 health_offset.y = mySize.y;
134                         else
135                                 armor_offset.y = mySize.y;
136                 }
137
138                 bool health_baralign, armor_baralign, fuel_baralign;
139                 bool health_iconalign, armor_iconalign;
140                 if (autocvar_hud_panel_healtharmor_flip)
141                 {
142                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
143                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
144                         fuel_baralign = health_baralign;
145                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
146                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
147                 }
148                 else
149                 {
150                         health_baralign = (autocvar_hud_panel_healtharmor_baralign == 2 || autocvar_hud_panel_healtharmor_baralign == 1);
151                         armor_baralign = (autocvar_hud_panel_healtharmor_baralign == 3 || autocvar_hud_panel_healtharmor_baralign == 1);
152                         fuel_baralign = armor_baralign;
153                         health_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 2 || autocvar_hud_panel_healtharmor_iconalign == 1);
154                         armor_iconalign = (autocvar_hud_panel_healtharmor_iconalign == 3 || autocvar_hud_panel_healtharmor_iconalign == 1);
155                 }
156
157                 //if(health)
158                 {
159                         if(autocvar_hud_panel_healtharmor_progressbar)
160                         {
161                                 float p_health, pain_health_alpha;
162                                 p_health = health;
163                                 pain_health_alpha = 1;
164                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
165                                 {
166                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
167                                         {
168                                                 if (fabs(prev_health - health) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
169                                                 {
170                                                         if (time - old_p_healthtime < 1)
171                                                                 old_p_health = prev_p_health;
172                                                         else
173                                                                 old_p_health = prev_health;
174                                                         old_p_healthtime = time;
175                                                 }
176                                                 if (time - old_p_healthtime < 1)
177                                                 {
178                                                         p_health += (old_p_health - health) * (1 - (time - old_p_healthtime));
179                                                         prev_p_health = p_health;
180                                                 }
181                                         }
182                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
183                                         {
184                                                 if (prev_health - health >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
185                                                 {
186                                                         if (time - health_damagetime >= 1)
187                                                                 health_beforedamage = prev_health;
188                                                         health_damagetime = time;
189                                                 }
190                                                 if (time - health_damagetime < 1)
191                                                 {
192                                                         float health_damagealpha = 1 - (time - health_damagetime)*(time - health_damagetime);
193                                                         HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, health_beforedamage/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * health_damagealpha, DRAWFLAG_NORMAL);
194                                                 }
195                                         }
196                                         prev_health = health;
197
198                                         if (health <= autocvar_hud_panel_healtharmor_progressbar_gfx_lowhealth)
199                                         {
200                                                 float BLINK_FACTOR = 0.15;
201                                                 float BLINK_BASE = 0.85;
202                                                 float BLINK_FREQ = 9;
203                                                 pain_health_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
204                                         }
205                                 }
206                                 HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
207                         }
208                         if(autocvar_hud_panel_healtharmor_text)
209                                 DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
210                 }
211
212                 //if(armor)
213                 {
214                         float p_armor = armor;
215                         if(autocvar_hud_panel_healtharmor_progressbar)
216                         {
217                                 if (autocvar_hud_panel_healtharmor_progressbar_gfx)
218                                 {
219                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_smooth > 0)
220                                         {
221                                                 if (fabs(prev_armor - armor) >= autocvar_hud_panel_healtharmor_progressbar_gfx_smooth)
222                                                 {
223                                                         if (time - old_p_armortime < 1)
224                                                                 old_p_armor = prev_p_armor;
225                                                         else
226                                                                 old_p_armor = prev_armor;
227                                                         old_p_armortime = time;
228                                                 }
229                                                 if (time - old_p_armortime < 1)
230                                                 {
231                                                         p_armor += (old_p_armor - armor) * (1 - (time - old_p_armortime));
232                                                         prev_p_armor = p_armor;
233                                                 }
234                                         }
235                                         if (autocvar_hud_panel_healtharmor_progressbar_gfx_damage > 0)
236                                         {
237                                                 if (prev_armor - armor >= autocvar_hud_panel_healtharmor_progressbar_gfx_damage)
238                                                 {
239                                                         if (time - armor_damagetime >= 1)
240                                                                 armor_beforedamage = prev_armor;
241                                                         armor_damagetime = time;
242                                                 }
243                                                 if (time - armor_damagetime < 1)
244                                                 {
245                                                         float armor_damagealpha = 1 - (time - armor_damagetime)*(time - armor_damagetime);
246                                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, armor_beforedamage/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * armor_damagealpha, DRAWFLAG_NORMAL);
247                                                 }
248                                         }
249                                         prev_armor = armor;
250                                 }
251                                 if(p_armor)
252                                         HUD_Panel_DrawProgressBar(pos + armor_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_armor, p_armor/maxarmor, is_vertical, armor_baralign, autocvar_hud_progressbar_armor_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
253                         }
254                         if(!autocvar_hud_panel_healtharmor_progressbar || p_armor)
255                         if(autocvar_hud_panel_healtharmor_text)
256                                 DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
257                 }
258
259                 if(fuel)
260                 {
261                         if (is_vertical)
262                                 mySize.x *= 0.2 / 2; //if vertical always halve x to not cover too much numbers with 3 digits
263                         else
264                                 mySize.y *= 0.2;
265                         if (panel_ar >= 4)
266                                 mySize.x *= 2; //restore full panel size
267                         else if (panel_ar < 1/4)
268                                 mySize.y *= 2; //restore full panel size
269                         HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", fuel/100, is_vertical, fuel_baralign, autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
270                 }
271         }
272
273         draw_endBoldFont();
274 }