]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/announcer.qc
Call centerprint countdown only in the first tic, it will continue automatically...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / announcer.qc
1 #include "announcer.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/panel/centerprint.qh>
5 #include <client/hud/panel/scoreboard.qh>
6 #include <client/mutators/_mod.qh>
7 #include <common/notifications/all.qh>
8 #include <common/stats.qh>
9 #include <common/mapinfo.qh>
10 #include <common/ent_cs.qh>
11
12 bool announcer_1min;
13 bool announcer_5min;
14 string AnnouncerOption()
15 {
16         string ret = autocvar_cl_announcer;
17         MUTATOR_CALLHOOK(AnnouncerOption, ret);
18         ret = M_ARGV(0, string);
19         return ret;
20 }
21
22 entity announcer_countdown;
23
24 /**
25  * Displays duel title; updates it if the players in-game have changed.
26  */
27 string prev_pl1_name;
28 string prev_pl2_name;
29 void Announcer_Duel()
30 {
31         Scoreboard_UpdatePlayerTeams();
32
33         entity pl1 = players.sort_next;
34         entity pl2 = pl1.sort_next;
35         string pl1_name = (pl1 && pl1.team != NUM_SPECTATOR ? entcs_GetName(pl1.sv_entnum) : "???");
36         string pl2_name = (pl2 && pl2.team != NUM_SPECTATOR ? entcs_GetName(pl2.sv_entnum) : "???");
37
38         if(pl1_name == prev_pl1_name && pl2_name == prev_pl2_name)
39                 return; // Players haven't changed, stop here
40
41         strcpy(prev_pl1_name, pl1_name);
42         strcpy(prev_pl2_name, pl2_name);
43
44         // There are new duelers, update title
45         centerprint_SetDuelTitle(pl1_name, pl2_name, _("vs"));
46 }
47
48 void Announcer_ClearTitle()
49 {
50         strfree(prev_pl1_name);
51         strfree(prev_pl2_name);
52         centerprint_ClearTitle();
53 }
54
55 bool prev_inround;
56 float prev_starttime;
57 float prev_roundstarttime;
58 void Announcer_Countdown(entity this)
59 {
60         float starttime = STAT(GAMESTARTTIME);
61         float roundstarttime = STAT(ROUNDSTARTTIME);
62         if(roundstarttime == -1)
63         {
64                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
65                 delete(this);
66                 announcer_countdown = NULL;
67                 Announcer_ClearTitle();
68                 return;
69         }
70
71         bool inround = (roundstarttime && time >= starttime);
72         float countdown = (inround ? roundstarttime - time : starttime - time);
73         float countdown_rounded = floor(0.5 + countdown);
74
75         if (starttime != prev_starttime || roundstarttime != prev_roundstarttime || prev_inround != inround)
76                 this.skin = 0; // restart centerprint countdown
77
78         if(countdown <= 0) // countdown has finished, starttime is now
79         {
80                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_BEGIN);
81                 Local_Notification(MSG_MULTI, MULTI_COUNTDOWN_BEGIN);
82                 delete(this);
83                 announcer_countdown = NULL;
84                 Announcer_ClearTitle();
85                 return;
86         }
87         else // countdown is still going
88         {
89                 if(inround)
90                 {
91                         if(!prev_inround) Announcer_ClearTitle(); // clear title if we just started the match
92                         if (!this.skin) // first tic
93                                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTART, STAT(ROUNDS_PLAYED) + 1, countdown_rounded);
94                         Notification annce_num = Announcer_PickNumber(CNT_ROUNDSTART, countdown_rounded);
95                         if(annce_num != NULL)
96                                 Local_Notification(MSG_ANNCE, annce_num);
97                         this.nextthink = (roundstarttime - (countdown - 1));
98                 }
99                 else
100                 {
101                         if (!this.skin) // first tic
102                                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_GAMESTART, countdown_rounded);
103                         Notification annce_num = Announcer_PickNumber(CNT_GAMESTART, countdown_rounded);
104                         if(!roundstarttime && annce_num != NULL) // Don't announce game start in round based modes
105                                 Local_Notification(MSG_ANNCE, annce_num);
106                         this.nextthink = (starttime - (countdown - 1));
107                 }
108                 // Don't call centerprint countdown in the remaining tics, it will continue automatically.
109                 // It's an optimization but also fixes ^COUNT shown in the last tic because of high slowmo values (15+).
110                 // Hopefully it fixes ^COUNT occasionally shown in online servers, probably due to lags.
111                 this.skin = 1; // recycled field
112         }
113
114         prev_inround = inround;
115         prev_starttime = starttime;
116         prev_roundstarttime = roundstarttime;
117 }
118
119 /**
120  * Checks whether the server initiated a map restart (stat_game_starttime changed)
121  *
122  * TODO: Use a better solution where a common shared entitiy is used that contains
123  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
124  * and STAT_FRAGLIMIT to be auto-sent)
125  */
126 float previous_game_starttime;
127 void Announcer_Gamestart()
128 {
129         float startTime = STAT(GAMESTARTTIME);
130         float roundstarttime = STAT(ROUNDSTARTTIME);
131         if(roundstarttime > startTime)
132                 startTime = roundstarttime;
133         if(intermission)
134         {
135                 if(announcer_countdown)
136                 {
137                         centerprint_Kill(ORDINAL(CPID_ROUND));
138                         if(announcer_countdown)
139                         {
140                                 delete(announcer_countdown);
141                                 announcer_countdown = NULL;
142                         }
143                 }
144                 return;
145         }
146
147         if(announcer_countdown && gametype.m_1v1)
148                 Announcer_Duel();
149
150         if(previous_game_starttime != startTime)
151         {
152                 if(time < startTime)
153                 {
154                         if (!announcer_countdown)
155                         {
156                                 announcer_countdown = new(announcer_countdown);
157                                 setthink(announcer_countdown, Announcer_Countdown);
158                         }
159
160                         if(!warmup_stage && time < STAT(GAMESTARTTIME))
161                         {
162                                 if (gametype.m_1v1)
163                                         Announcer_Duel();
164                                 else
165                                         centerprint_SetTitle(strcat("^BG", MapInfo_Type_ToText(gametype))); // Show game type as title
166
167                                 if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
168                                         Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
169                         }
170
171                         announcer_countdown.nextthink = startTime - floor(startTime - time + 0.5); //synchronize nextthink to startTime
172                 }
173         }
174
175         previous_game_starttime = startTime;
176 }
177
178 #define ANNOUNCER_CHECKMINUTE(minute) MACRO_BEGIN \
179         if(announcer_##minute##min) { \
180                 if(timeleft > minute * 60) \
181                         announcer_##minute##min = false; \
182         } else { \
183                 if(timeleft < minute * 60 && timeleft > minute * 60 - 1) { \
184                         announcer_##minute##min = true; \
185                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_##minute); \
186                 } \
187         } \
188 MACRO_END
189
190 void Announcer_Time()
191 {
192         static bool warmup_stage_prev;
193
194         if(intermission)
195                 return;
196
197         if (warmup_stage != warmup_stage_prev)
198         {
199                 announcer_5min = announcer_1min = false;
200                 warmup_stage_prev = warmup_stage;
201                 return;
202         }
203
204         float starttime = STAT(GAMESTARTTIME);
205         if(time < starttime)
206         {
207                 announcer_5min = announcer_1min = false;
208                 return;
209         }
210
211         float timeleft;
212         if(warmup_stage)
213         {
214                 float warmup_timelimit = STAT(WARMUP_TIMELIMIT);
215                 if(warmup_timelimit > 0)
216                         timeleft = max(0, warmup_timelimit - time);
217                 else
218                         timeleft = 0;
219         }
220         else
221                 timeleft = max(0, STAT(TIMELIMIT) * 60 + starttime - time);
222
223         if(autocvar_cl_announcer_maptime >= 2)
224                 ANNOUNCER_CHECKMINUTE(5);
225
226         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
227                 ANNOUNCER_CHECKMINUTE(1);
228 }
229
230 void Announcer()
231 {
232         Announcer_Gamestart();
233         Announcer_Time();
234 }