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