]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/racetimer.qc
Merge branch 'master' into terencehill/glowmod_color_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / racetimer.qc
1 #include "racetimer.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/draw.qh>
5
6 // Race timer (#8)
7
8 void HUD_RaceTimer_Export(int fh)
9 {
10         // allow saving cvars that aesthetically change the panel into hud skin files
11 }
12
13 // return the string of the onscreen race timer
14 string MakeRaceString(int cp, float mytime, float theirtime, float othertime, float lapdelta, string theirname)
15 {
16         TC(int, cp);
17         string cpname, lapstr = "", timestr = "", col = "^7", othercol = "^7", othertimestr = "";
18         if(theirname == "" || !autocvar_cl_race_cptimes_showself)
19                 othertime = 0; // don't count personal time
20
21         if(theirtime == 0) // goal hit
22         {
23                 if(mytime > 0)
24                 {
25                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
26                         col = "^1";
27                 }
28                 else if(mytime == 0)
29                 {
30                         timestr = "+0.0";
31                         col = "^3";
32                 }
33                 else
34                 {
35                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
36                         col = "^2";
37                 }
38
39                 if(othertime > 0)
40                 {
41                         othertimestr = strcat("+", ftos_decimals(+othertime, TIME_DECIMALS));
42                         othercol = "^1";
43                 }
44                 else if(othertime == 0)
45                 {
46                         othertimestr = "+0.0";
47                         othercol = "^3";
48                 }
49                 else
50                 {
51                         othertimestr = strcat("-", ftos_decimals(-othertime, TIME_DECIMALS));
52                         othercol = "^2";
53                 }
54
55                 if(lapdelta > 0)
56                 {
57                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
58                         col = "^2";
59                 }
60                 else if(lapdelta < 0)
61                 {
62                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
63                         col = "^1";
64                 }
65         }
66         else if(theirtime > 0) // anticipation
67         {
68                 if(mytime >= theirtime)
69                         timestr = strcat("+", ftos_decimals(mytime - theirtime, TIME_DECIMALS));
70                 else
71                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(theirtime));
72                 col = "^3";
73                 if(mytime >= othertime)
74                         othertimestr = strcat("+", ftos_decimals(mytime - othertime, TIME_DECIMALS));
75                 else
76                         othertimestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(othertime));
77                 othercol = "^7";
78         }
79
80         if(cp == 254)
81                 cpname = _("Start line");
82         else if(cp == 255)
83                 cpname = _("Finish line");
84         else if(cp)
85                 cpname = sprintf(_("Intermediate %d"), cp);
86         else
87                 cpname = _("Finish line");
88
89         if(theirtime < 0)
90                 return strcat(col, cpname);
91         else if(theirname == "")
92                 return strcat(col, sprintf("%s (%s)", cpname, timestr));
93         else if(othertime)
94                 return strcat(col, sprintf("%s %s(%s)%s (%s %s)", cpname, othercol, othertimestr, col, timestr, strcat(ColorTranslateRGB(theirname), col, lapstr)));
95         else
96                 return strcat(col, sprintf("%s (%s %s)", cpname, timestr, strcat(ColorTranslateRGB(theirname), col, lapstr)));
97 }
98
99 void HUD_RaceTimer ()
100 {
101         if(!autocvar__hud_configure)
102         {
103                 if(!autocvar_hud_panel_racetimer) return;
104                 if(!MUTATOR_CALLHOOK(ShowRaceTimer)) return;
105                 if(spectatee_status == -1) return;
106         }
107
108         HUD_Panel_LoadCvars();
109
110         vector pos, mySize;
111         pos = panel_pos;
112         mySize = panel_size;
113
114         if (autocvar_hud_panel_racetimer_dynamichud)
115                 HUD_Scale_Enable();
116         else
117                 HUD_Scale_Disable();
118         HUD_Panel_DrawBg();
119         if(panel_bg_padding)
120         {
121                 pos += '1 1 0' * panel_bg_padding;
122                 mySize -= '2 2 0' * panel_bg_padding;
123         }
124
125         // always force 4:1 aspect
126         vector newSize = '0 0 0';
127         if(mySize.x/mySize.y > 4)
128         {
129                 newSize.x = 4 * mySize.y;
130                 newSize.y = mySize.y;
131
132                 pos.x = pos.x + (mySize.x - newSize.x) / 2;
133         }
134         else
135         {
136                 newSize.y = 1/4 * mySize.x;
137                 newSize.x = mySize.x;
138
139                 pos.y = pos.y + (mySize.y - newSize.y) / 2;
140         }
141         mySize = newSize;
142
143         float a, t;
144         string s, forcetime;
145         vector str_pos;
146
147         if(autocvar__hud_configure)
148         {
149                 s = "0:13:37";
150                 draw_beginBoldFont();
151                 str_pos = pos + eX * 0.5 * (mySize.x - stringwidth(s, false, '1 1 0' * 0.6 * mySize.y));
152                 drawstring(str_pos, s, '1 1 0' * 0.6 * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
153                 draw_endBoldFont();
154                 s = strcat("^1", sprintf(_("Intermediate %d"), 1), " (+15.42)");
155                 str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.6 * mySize.y);
156                 drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
157                 s = sprintf(_("PENALTY: %.1f (%s)"), 2, _("missing a checkpoint"));
158                 s = strcat("^1", s);
159                 str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.8 * mySize.y);
160                 drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha, DRAWFLAG_NORMAL);
161         }
162         else if(race_checkpointtime)
163         {
164                 a = bound(0, 2 - (time - race_checkpointtime), 1);
165                 s = "";
166                 forcetime = "";
167                 if(a > 0) // just hit a checkpoint?
168                 {
169                         if(race_checkpoint != 254)
170                         {
171                                 if(race_time && race_previousbesttime)
172                                         s = MakeRaceString(race_checkpoint, TIME_DECODE(race_time) - TIME_DECODE(race_previousbesttime), 0, ((race_mypreviousbesttime) ? TIME_DECODE(race_time) - TIME_DECODE(race_mypreviousbesttime) : 0), 0, race_previousbestname);
173                                 else
174                                         s = MakeRaceString(race_checkpoint, 0, -1, 0, 0, race_previousbestname);
175                                 if(race_time)
176                                         forcetime = TIME_ENCODED_TOSTRING(race_time);
177                         }
178                 }
179                 else
180                 {
181                         if(race_laptime && race_nextcheckpoint != 254)
182                         {
183                                 if(race_nextbesttime)
184                                 {
185                                         a = bound(0, 2 - ((race_laptime + TIME_DECODE(race_nextbesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1);
186                                         float a2 = ((race_mybesttime) ? bound(0, 2 - ((race_laptime + TIME_DECODE(race_mybesttime)) - (time + TIME_DECODE(race_penaltyaccumulator))), 1) : 0);
187                                         if(a > 0) // next one?
188                                                 s = MakeRaceString(race_nextcheckpoint, (time + TIME_DECODE(race_penaltyaccumulator)) - race_laptime, TIME_DECODE(race_nextbesttime), ((a2 > 0) ? TIME_DECODE(race_mybesttime) : 0), 0, race_nextbestname);
189                                 }
190                         }
191                 }
192
193                 if(s != "" && a > 0)
194                 {
195                         str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.6 * mySize.y);
196                         drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
197                 }
198
199                 if(race_penaltytime)
200                 {
201                         a = bound(0, 2 - (time - race_penaltyeventtime), 1);
202                         if(a > 0)
203                         {
204                                 s = sprintf(_("PENALTY: %.1f (%s)"), race_penaltytime * 0.1, race_penaltyreason);
205                                 s = strcat("^1", s);
206                                 str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.8 * mySize.y);
207                                 drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
208                         }
209                 }
210
211                 draw_beginBoldFont();
212
213                 if(forcetime != "")
214                 {
215                         a = bound(0, (time - race_checkpointtime) / 0.5, 1);
216                         str_pos = pos + eX * 0.5 * (mySize.x - stringwidth(forcetime, false, '1 1 0' * 0.6 * mySize.y));
217                         drawstring_expanding(str_pos, forcetime, '1 1 0' * 0.6 * mySize.y, '1 1 1', panel_fg_alpha, 0, a);
218                 }
219                 else
220                         a = 1;
221
222                 if(race_laptime && race_checkpoint != 255)
223                 {
224                         s = TIME_ENCODED_TOSTRING(TIME_ENCODE(time + TIME_DECODE(race_penaltyaccumulator) - race_laptime));
225                         str_pos = pos + eX * 0.5 * (mySize.x - stringwidth(s, false, '0.6 0.6 0' * mySize.y));
226                         drawstring(str_pos, s, '0.6 0.6 0' * mySize.y, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
227                 }
228
229                 draw_endBoldFont();
230         }
231         else
232         {
233                 if(race_mycheckpointtime)
234                 {
235                         a = bound(0, 2 - (time - race_mycheckpointtime), 1);
236                         s = MakeRaceString(race_mycheckpoint, TIME_DECODE(race_mycheckpointdelta), -(race_mycheckpointenemy == ""), 0, race_mycheckpointlapsdelta, race_mycheckpointenemy);
237                         str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.6 * mySize.y);
238                         drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
239                 }
240                 if(race_othercheckpointtime && race_othercheckpointenemy != "")
241                 {
242                         a = bound(0, 2 - (time - race_othercheckpointtime), 1);
243                         s = MakeRaceString(race_othercheckpoint, -TIME_DECODE(race_othercheckpointdelta), -(race_othercheckpointenemy == ""), 0, race_othercheckpointlapsdelta, race_othercheckpointenemy);
244                         str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.6 * mySize.y);
245                         drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
246                 }
247
248                 if(race_penaltytime && !race_penaltyaccumulator)
249                 {
250                         t = race_penaltytime * 0.1 + race_penaltyeventtime;
251                         a = bound(0, (1 + t - time), 1);
252                         if(a > 0)
253                         {
254                                 string col;
255                                 if(time < t)
256                                 {
257                                         t = (t - time) * 0.1;
258                                         col = "^1";
259                                 }
260                                 else
261                                 {
262                                         t = 0;
263                                         col = "^2";
264                                 }
265                                 s = sprintf(_("PENALTY: %.1f (%s)"), t, race_penaltyreason);
266                                 s = strcat(col, s);
267                                 str_pos = pos + vec2(0.5 * (mySize.x - stringwidth(s, true, '1 1 0' * 0.2 * mySize.y)), 0.6 * mySize.y);
268                                 drawcolorcodedstring(str_pos, s, '1 1 0' * 0.2 * mySize.y, panel_fg_alpha * a, DRAWFLAG_NORMAL);
269                         }
270                 }
271         }
272 }