]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/modicons.qc
Add CTF stalemate indicator to HUD
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / modicons.qc
1 #include "modicons.qh"
2
3 #include <common/mapinfo.qh>
4 #include <common/ent_cs.qh>
5 #include <server/mutators/mutator/gamemode_ctf.qh> // TODO: remove
6
7 // Mod icons panel (#10)
8
9 bool mod_active; // is there any active mod icon?
10
11 void DrawCAItem(vector myPos, vector mySize, float aspect_ratio, int layout, int i)
12 {
13     TC(int, layout); TC(int, i);
14         int stat = -1;
15         string pic = "";
16         vector color = '0 0 0';
17         switch(i)
18         {
19                 case 0:
20                         stat = STAT(REDALIVE);
21                         pic = "player_red.tga";
22                         color = '1 0 0';
23                         break;
24                 case 1:
25                         stat = STAT(BLUEALIVE);
26                         pic = "player_blue.tga";
27                         color = '0 0 1';
28                         break;
29                 case 2:
30                         stat = STAT(YELLOWALIVE);
31                         pic = "player_yellow.tga";
32                         color = '1 1 0';
33                         break;
34                 default:
35                 case 3:
36                         stat = STAT(PINKALIVE);
37                         pic = "player_pink.tga";
38                         color = '1 0 1';
39                         break;
40         }
41
42         if(mySize.x/mySize.y > aspect_ratio)
43         {
44                 i = aspect_ratio * mySize.y;
45                 myPos.x = myPos.x + (mySize.x - i) / 2;
46                 mySize.x = i;
47         }
48         else
49         {
50                 i = 1/aspect_ratio * mySize.x;
51                 myPos.y = myPos.y + (mySize.y - i) / 2;
52                 mySize.y = i;
53         }
54
55         if(layout)
56         {
57                 drawpic_aspect_skin(myPos, pic, eX * 0.7 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
58                 drawstring_aspect(myPos + eX * 0.7 * mySize.x, ftos(stat), eX * 0.3 * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
59         }
60         else
61                 drawstring_aspect(myPos, ftos(stat), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
62 }
63
64 // Clan Arena and Freeze Tag HUD modicons
65 void HUD_Mod_CA(vector myPos, vector mySize)
66 {
67         mod_active = 1; // required in each mod function that always shows something
68
69         int layout;
70         if(gametype == MAPINFO_TYPE_CA)
71                 layout = autocvar_hud_panel_modicons_ca_layout;
72         else //if(gametype == MAPINFO_TYPE_FREEZETAG)
73                 layout = autocvar_hud_panel_modicons_freezetag_layout;
74         int rows, columns;
75         float aspect_ratio;
76         aspect_ratio = (layout) ? 2 : 1;
77         rows = HUD_GetRowCount(team_count, mySize, aspect_ratio);
78         columns = ceil(team_count/rows);
79
80         int i;
81         float row = 0, column = 0;
82         vector pos, itemSize;
83         itemSize = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
84         for(i=0; i<team_count; ++i)
85         {
86                 pos = myPos + eX * column * itemSize.x + eY * row * itemSize.y;
87
88                 DrawCAItem(pos, itemSize, aspect_ratio, layout, i);
89
90                 ++row;
91                 if(row >= rows)
92                 {
93                         row = 0;
94                         ++column;
95                 }
96         }
97 }
98
99 // CTF HUD modicon section
100 int redflag_prevframe, blueflag_prevframe, yellowflag_prevframe, pinkflag_prevframe, neutralflag_prevframe; // status during previous frame
101 int redflag_prevstatus, blueflag_prevstatus, yellowflag_prevstatus, pinkflag_prevstatus, neutralflag_prevstatus; // last remembered status
102 float redflag_statuschange_time, blueflag_statuschange_time, yellowflag_statuschange_time, pinkflag_statuschange_time, neutralflag_statuschange_time; // time when the status changed
103
104 void HUD_Mod_CTF_Reset()
105 {
106         redflag_prevstatus = blueflag_prevstatus = yellowflag_prevstatus = pinkflag_prevstatus = neutralflag_prevstatus = 0;
107         redflag_prevframe = blueflag_prevframe = yellowflag_prevframe = pinkflag_prevframe = neutralflag_prevframe = 0;
108         redflag_statuschange_time = blueflag_statuschange_time = yellowflag_statuschange_time = pinkflag_statuschange_time = neutralflag_statuschange_time = 0;
109 }
110
111 void HUD_Mod_CTF(vector pos, vector mySize)
112 {
113         vector redflag_pos, blueflag_pos, yellowflag_pos, pinkflag_pos, neutralflag_pos;
114         vector flag_size;
115         float f; // every function should have that
116
117         int redflag, blueflag, yellowflag, pinkflag, neutralflag; // current status
118         float redflag_statuschange_elapsedtime = 0, blueflag_statuschange_elapsedtime = 0, yellowflag_statuschange_elapsedtime = 0, pinkflag_statuschange_elapsedtime = 0, neutralflag_statuschange_elapsedtime = 0; // time since the status changed
119         bool ctf_oneflag; // one-flag CTF mode enabled/disabled
120         int stat_items = STAT(CTF_FLAGSTATUS);
121         float fs, fs2, fs3, size1, size2;
122         vector e1, e2;
123
124         redflag = (stat_items/CTF_RED_FLAG_TAKEN) & 3;
125         blueflag = (stat_items/CTF_BLUE_FLAG_TAKEN) & 3;
126         yellowflag = (stat_items/CTF_YELLOW_FLAG_TAKEN) & 3;
127         pinkflag = (stat_items/CTF_PINK_FLAG_TAKEN) & 3;
128         neutralflag = (stat_items/CTF_NEUTRAL_FLAG_TAKEN) & 3;
129
130         ctf_oneflag = (stat_items & CTF_FLAG_NEUTRAL);
131
132         mod_active = (redflag || blueflag || yellowflag || pinkflag || neutralflag || (stat_items & CTF_SHIELDED));
133
134         if (autocvar__hud_configure) {
135                 redflag = 1;
136                 blueflag = 2;
137                 if (team_count >= 3)
138                         yellowflag = 2;
139                 if (team_count >= 4)
140                         pinkflag = 3;
141                 ctf_oneflag = neutralflag = 0; // disable neutral flag in hud editor?
142         }
143
144         // when status CHANGES, set old status into prevstatus and current status into status
145         #define X(team) MACRO_BEGIN {                                                                                                   \
146                 if (team##flag != team##flag_prevframe) {                                                                       \
147                 team##flag_statuschange_time = time;                                                                    \
148                 team##flag_prevstatus = team##flag_prevframe;                                                   \
149                 team##flag_prevframe = team##flag;                                                                              \
150         }                                                                                                                                                       \
151         team##flag_statuschange_elapsedtime = time - team##flag_statuschange_time;      \
152     } MACRO_END
153         X(red);
154         X(blue);
155         X(yellow);
156         X(pink);
157         X(neutral);
158         #undef X
159
160         const float BLINK_FACTOR = 0.15;
161         const float BLINK_BASE = 0.85;
162         // note:
163         //   RMS = sqrt(BLINK_BASE^2 + 0.5 * BLINK_FACTOR^2)
164         // thus
165         //   BLINK_BASE = sqrt(RMS^2 - 0.5 * BLINK_FACTOR^2)
166         // ensure RMS == 1
167         const float BLINK_FREQ = 5; // circle frequency, = 2*pi*frequency in hertz
168
169         #define X(team, cond) \
170         string team##_icon = string_null, team##_icon_prevstatus = string_null; \
171         int team##_alpha, team##_alpha_prevstatus; \
172         team##_alpha = team##_alpha_prevstatus = 1; \
173         MACRO_BEGIN { \
174                 switch (team##flag) { \
175                         case 1: team##_icon = "flag_" #team "_taken"; break; \
176                         case 2: team##_icon = "flag_" #team "_lost"; break; \
177                         case 3: team##_icon = "flag_" #team "_carrying"; team##_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break; \
178                         default: \
179                                 if ((stat_items & CTF_SHIELDED) && (cond)) { \
180                                         team##_icon = "flag_" #team "_shielded"; \
181                                 } else { \
182                                         team##_icon = string_null; \
183                                 } \
184                                 break; \
185                 } \
186                 switch (team##flag_prevstatus) { \
187                         case 1: team##_icon_prevstatus = "flag_" #team "_taken"; break; \
188                         case 2: team##_icon_prevstatus = "flag_" #team "_lost"; break; \
189                         case 3: team##_icon_prevstatus = "flag_" #team "_carrying"; team##_alpha_prevstatus = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ); break; \
190                         default: \
191                                 if (team##flag == 3) { \
192                                         team##_icon_prevstatus = "flag_" #team "_carrying"; /* make it more visible */\
193                                 } else if((stat_items & CTF_SHIELDED) && (cond)) { \
194                                         team##_icon_prevstatus = "flag_" #team "_shielded"; \
195                                 } else { \
196                                         team##_icon_prevstatus = string_null; \
197                                 } \
198                                 break; \
199                 } \
200         } MACRO_END
201         X(red, myteam != NUM_TEAM_1);
202         X(blue, myteam != NUM_TEAM_2);
203         X(yellow, myteam != NUM_TEAM_3 && team_count >= 3);
204         X(pink, myteam != NUM_TEAM_4 && team_count >= 4);
205         X(neutral, ctf_oneflag);
206         #undef X
207
208         if (ctf_oneflag) {
209                 // hacky, but these aren't needed
210                 red_icon = red_icon_prevstatus = blue_icon = blue_icon_prevstatus = yellow_icon = yellow_icon_prevstatus = pink_icon = pink_icon_prevstatus = string_null;
211                 fs = fs2 = fs3 = 1;
212         } else switch (team_count) {
213                 default:
214                 case 2: fs = 0.5; fs2 = 0.5; fs3 = 0.5; break;
215                 case 3: fs = 1; fs2 = 0.35; fs3 = 0.35; break;
216                 case 4: fs = 0.75; fs2 = 0.25; fs3 = 0.5; break;
217         }
218
219         if (mySize_x > mySize_y) {
220                 size1 = mySize_x;
221                 size2 = mySize_y;
222                 e1 = eX;
223                 e2 = eY;
224         } else {
225                 size1 = mySize_y;
226                 size2 = mySize_x;
227                 e1 = eY;
228                 e2 = eX;
229         }
230
231         switch (myteam) {
232                 default:
233                 case NUM_TEAM_1: {
234                         redflag_pos = pos;
235                         blueflag_pos = pos + eX * fs2 * size1;
236                         yellowflag_pos = pos - eX * fs2 * size1;
237                         pinkflag_pos = pos + eX * fs3 * size1;
238                         break;
239                 }
240                 case NUM_TEAM_2: {
241                         redflag_pos = pos + eX * fs2 * size1;
242                         blueflag_pos = pos;
243                         yellowflag_pos = pos - eX * fs2 * size1;
244                         pinkflag_pos = pos + eX * fs3 * size1;
245                         break;
246                 }
247                 case NUM_TEAM_3: {
248                         redflag_pos = pos + eX * fs3 * size1;
249                         blueflag_pos = pos - eX * fs2 * size1;
250                         yellowflag_pos = pos;
251                         pinkflag_pos = pos + eX * fs2 * size1;
252                         break;
253                 }
254                 case NUM_TEAM_4: {
255                         redflag_pos = pos - eX * fs2 * size1;
256                         blueflag_pos = pos + eX * fs3 * size1;
257                         yellowflag_pos = pos + eX * fs2 * size1;
258                         pinkflag_pos = pos;
259                         break;
260                 }
261         }
262         neutralflag_pos = pos;
263         flag_size = e1 * fs * size1 + e2 * size2;
264
265         #define X(team) MACRO_BEGIN { \
266                 f = bound(0, team##flag_statuschange_elapsedtime * 2, 1); \
267                 if (team##_icon && 0) /* TODO Replace 0 with stalemate condition */ \
268                         drawpic_aspect_skin(team##flag_pos, "flag_stalemate", flag_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL); \
269                 if (team##_icon_prevstatus && f < 1) \
270                         drawpic_aspect_skin_expanding(team##flag_pos, team##_icon_prevstatus, flag_size, '1 1 1', panel_fg_alpha * team##_alpha_prevstatus, DRAWFLAG_NORMAL, f); \
271                 if (team##_icon) \
272                         drawpic_aspect_skin(team##flag_pos, team##_icon, flag_size, '1 1 1', panel_fg_alpha * team##_alpha * f, DRAWFLAG_NORMAL); \
273         } MACRO_END
274         X(red);
275         X(blue);
276         X(yellow);
277         X(pink);
278         X(neutral);
279         #undef X
280 }
281
282 // Keyhunt HUD modicon section
283 vector KH_SLOTS[4];
284
285 void HUD_Mod_KH(vector pos, vector mySize)
286 {
287         mod_active = 1; // keyhunt should never hide the mod icons panel
288
289         // Read current state
290
291         int state = STAT(KH_KEYS);
292         int i, key_state;
293         int all_keys, team1_keys, team2_keys, team3_keys, team4_keys, dropped_keys, carrying_keys;
294         all_keys = team1_keys = team2_keys = team3_keys = team4_keys = dropped_keys = carrying_keys = 0;
295
296         for(i = 0; i < 4; ++i)
297         {
298                 key_state = (bitshift(state, i * -5) & 31) - 1;
299
300                 if(key_state == -1)
301                         continue;
302
303                 if(key_state == 30)
304                 {
305                         ++carrying_keys;
306                         key_state = myteam;
307                 }
308
309                 switch(key_state)
310                 {
311                         case NUM_TEAM_1: ++team1_keys; break;
312                         case NUM_TEAM_2: ++team2_keys; break;
313                         case NUM_TEAM_3: ++team3_keys; break;
314                         case NUM_TEAM_4: ++team4_keys; break;
315                         case 29: ++dropped_keys; break;
316                 }
317
318                 ++all_keys;
319         }
320
321         // Calculate slot measurements
322
323         vector slot_size;
324
325         if(all_keys == 4 && mySize.x * 0.5 < mySize.y && mySize.y * 0.5 < mySize.x)
326         {
327                 // Quadratic arrangement
328                 slot_size = eX * mySize.x * 0.5 + eY * mySize.y * 0.5;
329                 KH_SLOTS[0] = pos;
330                 KH_SLOTS[1] = pos + eX * slot_size.x;
331                 KH_SLOTS[2] = pos + eY * slot_size.y;
332                 KH_SLOTS[3] = pos + eX * slot_size.x + eY * slot_size.y;
333         }
334         else
335         {
336                 if(mySize.x > mySize.y)
337                 {
338                         // Horizontal arrangement
339                         slot_size = eX * mySize.x / all_keys + eY * mySize.y;
340                         for(i = 0; i < all_keys; ++i)
341                                 KH_SLOTS[i] = pos + eX * slot_size.x * i;
342                 }
343                 else
344                 {
345                         // Vertical arrangement
346                         slot_size = eX * mySize.x + eY * mySize.y / all_keys;
347                         for(i = 0; i < all_keys; ++i)
348                                 KH_SLOTS[i] = pos + eY * slot_size.y * i;
349                 }
350         }
351
352         // Make icons blink in case of RUN HERE
353
354         float blink = 0.6 + sin(2*M_PI*time) / 2.5; // Oscillate between 0.2 and 1
355         float alpha;
356         alpha = 1;
357
358         if(carrying_keys)
359                 switch(myteam)
360                 {
361                         case NUM_TEAM_1: if(team1_keys == all_keys) alpha = blink; break;
362                         case NUM_TEAM_2: if(team2_keys == all_keys) alpha = blink; break;
363                         case NUM_TEAM_3: if(team3_keys == all_keys) alpha = blink; break;
364                         case NUM_TEAM_4: if(team4_keys == all_keys) alpha = blink; break;
365                 }
366
367         // Draw icons
368
369         i = 0;
370
371         while(team1_keys--)
372                 if(myteam == NUM_TEAM_1 && carrying_keys)
373                 {
374                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_red_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
375                         --carrying_keys;
376                 }
377                 else
378                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_red_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
379
380         while(team2_keys--)
381                 if(myteam == NUM_TEAM_2 && carrying_keys)
382                 {
383                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_blue_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
384                         --carrying_keys;
385                 }
386                 else
387                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_blue_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
388
389         while(team3_keys--)
390                 if(myteam == NUM_TEAM_3 && carrying_keys)
391                 {
392                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_yellow_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
393                         --carrying_keys;
394                 }
395                 else
396                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_yellow_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
397
398         while(team4_keys--)
399                 if(myteam == NUM_TEAM_4 && carrying_keys)
400                 {
401                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_pink_carrying", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
402                         --carrying_keys;
403                 }
404                 else
405                         drawpic_aspect_skin(KH_SLOTS[i++], "kh_pink_taken", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
406
407         while(dropped_keys--)
408                 drawpic_aspect_skin(KH_SLOTS[i++], "kh_dropped", slot_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
409 }
410
411 // Keepaway HUD mod icon
412 int kaball_prevstatus; // last remembered status
413 float kaball_statuschange_time; // time when the status changed
414
415 // we don't need to reset for keepaway since it immediately
416 // autocorrects prevstatus as to if the player has the ball or not
417
418 void HUD_Mod_Keepaway(vector pos, vector mySize)
419 {
420         mod_active = 1; // keepaway should always show the mod HUD
421
422         float BLINK_FACTOR = 0.15;
423         float BLINK_BASE = 0.85;
424         float BLINK_FREQ = 5;
425         float kaball_alpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
426
427         int stat_items = STAT(ITEMS);
428         int kaball = (stat_items/IT_KEY1) & 1;
429
430         if(kaball != kaball_prevstatus)
431         {
432                 kaball_statuschange_time = time;
433                 kaball_prevstatus = kaball;
434         }
435
436         vector kaball_pos, kaball_size;
437
438         if(mySize.x > mySize.y) {
439                 kaball_pos = pos + eX * 0.25 * mySize.x;
440                 kaball_size = eX * 0.5 * mySize.x + eY * mySize.y;
441         } else {
442                 kaball_pos = pos + eY * 0.25 * mySize.y;
443                 kaball_size = eY * 0.5 * mySize.y + eX * mySize.x;
444         }
445
446         float kaball_statuschange_elapsedtime = time - kaball_statuschange_time;
447         float f = bound(0, kaball_statuschange_elapsedtime*2, 1);
448
449         if(kaball_prevstatus && f < 1)
450                 drawpic_aspect_skin_expanding(kaball_pos, "keepawayball_carrying", kaball_size, '1 1 1', panel_fg_alpha * kaball_alpha, DRAWFLAG_NORMAL, f);
451
452         if(kaball)
453                 drawpic_aspect_skin(pos, "keepawayball_carrying", eX * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha * kaball_alpha * f, DRAWFLAG_NORMAL);
454 }
455
456
457 // Nexball HUD mod icon
458 void HUD_Mod_NexBall(vector pos, vector mySize)
459 {
460         float nb_pb_starttime, dt, p;
461         int stat_items;
462
463         stat_items = STAT(ITEMS);
464         nb_pb_starttime = STAT(NB_METERSTART);
465
466         if (stat_items & IT_KEY1)
467                 mod_active = 1;
468         else
469                 mod_active = 0;
470
471         //Manage the progress bar if any
472         if (nb_pb_starttime > 0)
473         {
474                 dt = (time - nb_pb_starttime) % nb_pb_period;
475                 // one period of positive triangle
476                 p = 2 * dt / nb_pb_period;
477                 if (p > 1)
478                         p = 2 - p;
479
480                 HUD_Panel_DrawProgressBar(pos, mySize, "progressbar", p, (mySize.x <= mySize.y), 0, autocvar_hud_progressbar_nexball_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
481         }
482
483         if (stat_items & IT_KEY1)
484                 drawpic_aspect_skin(pos, "nexball_carrying", eX * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
485 }
486
487 // Race/CTS HUD mod icons
488 float crecordtime_prev; // last remembered crecordtime
489 float crecordtime_change_time; // time when crecordtime last changed
490 float srecordtime_prev; // last remembered srecordtime
491 float srecordtime_change_time; // time when srecordtime last changed
492
493 float race_status_time;
494 int race_status_prev;
495 string race_status_name_prev;
496
497 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
498 int race_CheckName(string net_name)
499 {
500         int i;
501         for (i=RANKINGS_CNT-1;i>=0;--i)
502                 if(grecordholder[i] == net_name)
503                         return i+1;
504         return 0;
505 }
506
507 void race_showTime(string text, vector pos, vector timeText_ofs, float theTime, vector textSize, float f)
508 {
509         drawstring_aspect(pos, text, textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
510         drawstring_aspect(pos + timeText_ofs, TIME_ENCODED_TOSTRING(theTime), textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
511         if (f < 1) {
512                 drawstring_aspect_expanding(pos, text, textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
513                 drawstring_aspect_expanding(pos + timeText_ofs, TIME_ENCODED_TOSTRING(theTime), textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
514         }
515 }
516
517 void HUD_Mod_Race(vector pos, vector mySize)
518 {
519         mod_active = 1; // race should never hide the mod icons panel
520         entity me;
521         me = playerslots[player_localnum];
522         float score;
523         score = me.(scores[ps_primary]);
524
525         if(!(scores_flags[ps_primary] & SFL_TIME) || teamplay) // race/cts record display on HUD
526                 return; // no records in the actual race
527
528         // clientside personal record
529         string rr;
530         if(gametype == MAPINFO_TYPE_CTS)
531                 rr = CTS_RECORD;
532         else
533                 rr = RACE_RECORD;
534         float t = stof(db_get(ClientProgsDB, strcat(shortmapname, rr, "time")));
535
536         if(score && (score < t || !t)) {
537                 db_put(ClientProgsDB, strcat(shortmapname, rr, "time"), ftos(score));
538                 if(autocvar_cl_autodemo_delete_keeprecords)
539                 {
540                         float f = autocvar_cl_autodemo_delete;
541                         f &= ~1;
542                         cvar_set("cl_autodemo_delete", ftos(f)); // don't delete demo with new record!
543                 }
544         }
545
546         if(t != crecordtime_prev) {
547                 crecordtime_prev = t;
548                 crecordtime_change_time = time;
549         }
550
551         vector textPos, medalPos;
552         float squareSize;
553         if(mySize.x > mySize.y) {
554                 // text on left side
555                 squareSize = min(mySize.y, mySize.x/2);
556                 textPos = pos + eX * 0.5 * max(0, mySize.x/2 - squareSize) + eY * 0.5 * (mySize.y - squareSize);
557                 medalPos = pos + eX * 0.5 * max(0, mySize.x/2 - squareSize) + eX * 0.5 * mySize.x + eY * 0.5 * (mySize.y - squareSize);
558         } else {
559                 // text on top
560                 squareSize = min(mySize.x, mySize.y/2);
561                 textPos = pos + eY * 0.5 * max(0, mySize.y/2 - squareSize) + eX * 0.5 * (mySize.x - squareSize);
562                 medalPos = pos + eY * 0.5 * max(0, mySize.y/2 - squareSize) + eY * 0.5 * mySize.y + eX * 0.5 * (mySize.x - squareSize);
563         }
564         vector textSize = eX * squareSize + eY * 0.25 * squareSize;
565
566         race_showTime(_("Personal best"), textPos, eY * 0.25 * squareSize, t, textSize, time - crecordtime_change_time);
567
568         // server record
569         t = race_server_record;
570         if(t != srecordtime_prev) {
571                 srecordtime_prev = t;
572                 srecordtime_change_time = time;
573         }
574
575         textPos += eY * 0.5 * squareSize;
576         race_showTime(_("Server best"), textPos, eY * 0.25 * squareSize, t, textSize, time - srecordtime_change_time);
577
578         if (race_status != race_status_prev || race_status_name != race_status_name_prev) {
579                 race_status_time = time + 5;
580                 race_status_prev = race_status;
581                 if (race_status_name_prev)
582                         strunzone(race_status_name_prev);
583                 race_status_name_prev = strzone(race_status_name);
584         }
585
586         // race "awards"
587         float a;
588         a = bound(0, race_status_time - time, 1);
589
590         string s;
591         s = textShortenToWidth(race_status_name, squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
592
593         float rank;
594         if(race_status > 0)
595                 rank = race_CheckName(race_status_name);
596         else
597                 rank = 0;
598         string rankname;
599         rankname = count_ordinal(rank);
600
601         vector namepos;
602         namepos = medalPos + '0 0.8 0' * squareSize;
603         vector rankpos;
604         rankpos = medalPos + '0 0.15 0' * squareSize;
605
606         if(race_status == 0)
607                 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
608         else if(race_status == 1) {
609                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newtime", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
610                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
611                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
612         } else if(race_status == 2) {
613                 if(race_status_name == entcs_GetName(player_localnum) || !race_myrank || race_myrank < rank)
614                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankgreen", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
615                 else
616                         drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankyellow", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
617                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
618                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
619         } else if(race_status == 3) {
620                 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrecordserver", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
621                 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
622                 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
623         }
624
625         if (race_status_time - time <= 0) {
626                 race_status_prev = -1;
627                 race_status = -1;
628                 if(race_status_name)
629                         strunzone(race_status_name);
630                 race_status_name = string_null;
631                 if(race_status_name_prev)
632                         strunzone(race_status_name_prev);
633                 race_status_name_prev = string_null;
634         }
635 }
636
637 void DrawDomItem(vector myPos, vector mySize, float aspect_ratio, int layout, int i)
638 {
639     TC(int, layout); TC(int, i);
640         float stat = -1;
641         string pic = "";
642         vector color = '0 0 0';
643         switch(i)
644         {
645                 case 0:
646                         stat = STAT(DOM_PPS_RED);
647                         pic = "dom_icon_red";
648                         color = '1 0 0';
649                         break;
650                 case 1:
651                         stat = STAT(DOM_PPS_BLUE);
652                         pic = "dom_icon_blue";
653                         color = '0 0 1';
654                         break;
655                 case 2:
656                         stat = STAT(DOM_PPS_YELLOW);
657                         pic = "dom_icon_yellow";
658                         color = '1 1 0';
659                         break;
660                 default:
661                 case 3:
662                         stat = STAT(DOM_PPS_PINK);
663                         pic = "dom_icon_pink";
664                         color = '1 0 1';
665                         break;
666         }
667         float pps_ratio = stat / STAT(DOM_TOTAL_PPS);
668
669         if(mySize.x/mySize.y > aspect_ratio)
670         {
671                 i = aspect_ratio * mySize.y;
672                 myPos.x = myPos.x + (mySize.x - i) / 2;
673                 mySize.x = i;
674         }
675         else
676         {
677                 i = 1/aspect_ratio * mySize.x;
678                 myPos.y = myPos.y + (mySize.y - i) / 2;
679                 mySize.y = i;
680         }
681
682         if (layout) // show text too
683         {
684                 //draw the text
685                 color *= 0.5 + pps_ratio * (1 - 0.5); // half saturated color at min, full saturated at max
686                 if (layout == 2) // average pps
687                         drawstring_aspect(myPos + eX * mySize.y, ftos_decimals(stat, 2), eX * (2/3) * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
688                 else // percentage of average pps
689                         drawstring_aspect(myPos + eX * mySize.y, strcat( ftos(floor(pps_ratio*100 + 0.5)), "%" ), eX * (2/3) * mySize.x + eY * mySize.y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
690         }
691
692         //draw the icon
693         drawpic_aspect_skin(myPos, pic, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
694         if (stat > 0)
695         {
696                 drawsetcliparea(myPos.x, myPos.y + mySize.y * (1 - pps_ratio), mySize.y, mySize.y * pps_ratio);
697                 drawpic_aspect_skin(myPos, strcat(pic, "-highlighted"), '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
698                 drawresetcliparea();
699         }
700 }
701
702 void HUD_Mod_Dom(vector myPos, vector mySize)
703 {
704         mod_active = 1; // required in each mod function that always shows something
705
706         int layout = autocvar_hud_panel_modicons_dom_layout;
707         int rows, columns;
708         float aspect_ratio;
709         aspect_ratio = (layout) ? 3 : 1;
710         rows = HUD_GetRowCount(team_count, mySize, aspect_ratio);
711         columns = ceil(team_count/rows);
712
713         int i;
714         float row = 0, column = 0;
715         vector pos, itemSize;
716         itemSize = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
717         for(i=0; i<team_count; ++i)
718         {
719                 pos = myPos + eX * column * itemSize.x + eY * row * itemSize.y;
720
721                 DrawDomItem(pos, itemSize, aspect_ratio, layout, i);
722
723                 ++row;
724                 if(row >= rows)
725                 {
726                         row = 0;
727                         ++column;
728                 }
729         }
730 }
731
732 void HUD_ModIcons_SetFunc()
733 {
734         switch(gametype)
735         {
736                 case MAPINFO_TYPE_KEYHUNT:              HUD_ModIcons_GameType = HUD_Mod_KH; break;
737                 case MAPINFO_TYPE_CTF:                  HUD_ModIcons_GameType = HUD_Mod_CTF; break;
738                 case MAPINFO_TYPE_NEXBALL:              HUD_ModIcons_GameType = HUD_Mod_NexBall; break;
739                 case MAPINFO_TYPE_CTS:
740                 case MAPINFO_TYPE_RACE:         HUD_ModIcons_GameType = HUD_Mod_Race; break;
741                 case MAPINFO_TYPE_CA:
742                 case MAPINFO_TYPE_FREEZETAG:    HUD_ModIcons_GameType = HUD_Mod_CA; break;
743                 case MAPINFO_TYPE_DOMINATION:   HUD_ModIcons_GameType = HUD_Mod_Dom; break;
744                 case MAPINFO_TYPE_KEEPAWAY:     HUD_ModIcons_GameType = HUD_Mod_Keepaway; break;
745         }
746 }
747
748 int mod_prev; // previous state of mod_active to check for a change
749 float mod_alpha;
750 float mod_change; // "time" when mod_active changed
751
752 void HUD_ModIcons()
753 {
754         if(!autocvar__hud_configure)
755         {
756                 if(!autocvar_hud_panel_modicons) return;
757                 if(!HUD_ModIcons_GameType) return;
758         }
759
760         HUD_Panel_UpdateCvars();
761
762         draw_beginBoldFont();
763
764         if(mod_active != mod_prev) {
765                 mod_change = time;
766                 mod_prev = mod_active;
767         }
768
769         if(mod_active || autocvar__hud_configure)
770                 mod_alpha = bound(0, (time - mod_change) * 2, 1);
771         else
772                 mod_alpha = bound(0, 1 - (time - mod_change) * 2, 1);
773
774         if (autocvar_hud_panel_modicons_dynamichud)
775                 HUD_Scale_Enable();
776         else
777                 HUD_Scale_Disable();
778         if(mod_alpha)
779                 HUD_Panel_DrawBg(mod_alpha);
780
781         if(panel_bg_padding)
782         {
783                 panel_pos += '1 1 0' * panel_bg_padding;
784                 panel_size -= '2 2 0' * panel_bg_padding;
785         }
786
787         if(autocvar__hud_configure)
788                 HUD_Mod_CTF(panel_pos, panel_size);
789         else
790                 HUD_ModIcons_GameType(panel_pos, panel_size);
791
792         draw_endBoldFont();
793 }