d46595e8718bde0a2007fb276a6e54ab6b49d7ca
[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                 Announcer_ClearTitle();
136                 if(announcer_countdown)
137                 {
138                         centerprint_Kill(ORDINAL(CPID_ROUND));
139                         if(announcer_countdown)
140                         {
141                                 delete(announcer_countdown);
142                                 announcer_countdown = NULL;
143                         }
144                 }
145                 return;
146         }
147
148         if(announcer_countdown && gametype.m_1v1)
149                 Announcer_Duel();
150
151         if(previous_game_starttime != startTime)
152         {
153                 if(time < startTime)
154                 {
155                         if (!announcer_countdown)
156                         {
157                                 announcer_countdown = new(announcer_countdown);
158                                 setthink(announcer_countdown, Announcer_Countdown);
159                         }
160
161                         if(!warmup_stage && time < STAT(GAMESTARTTIME))
162                         {
163                                 if (gametype.m_1v1)
164                                         Announcer_Duel();
165                                 else
166                                         centerprint_SetTitle(strcat("^BG", MapInfo_Type_ToText(gametype))); // Show game type as title
167
168                                 if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
169                                         Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
170                         }
171
172                         announcer_countdown.nextthink = startTime - floor(startTime - time + 0.5); //synchronize nextthink to startTime
173                 }
174         }
175
176         previous_game_starttime = startTime;
177 }
178
179 #define ANNOUNCER_CHECKMINUTE(minute) MACRO_BEGIN \
180         if(announcer_##minute##min) { \
181                 if(timeleft > minute * 60) \
182                         announcer_##minute##min = false; \
183         } else { \
184                 if(timeleft < minute * 60 && timeleft > minute * 60 - 1) { \
185                         announcer_##minute##min = true; \
186                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_##minute); \
187                 } \
188         } \
189 MACRO_END
190
191 void Announcer_Time()
192 {
193         static bool warmup_stage_prev;
194
195         if(intermission)
196                 return;
197
198         if (warmup_stage != warmup_stage_prev)
199         {
200                 announcer_5min = announcer_1min = false;
201                 warmup_stage_prev = warmup_stage;
202                 return;
203         }
204
205         float starttime = STAT(GAMESTARTTIME);
206         if(time < starttime)
207         {
208                 announcer_5min = announcer_1min = false;
209                 return;
210         }
211
212         float timeleft;
213         if(warmup_stage)
214         {
215                 float warmup_timelimit = STAT(WARMUP_TIMELIMIT);
216                 if(warmup_timelimit > 0)
217                         timeleft = max(0, warmup_timelimit - time);
218                 else
219                         timeleft = 0;
220         }
221         else
222                 timeleft = max(0, STAT(TIMELIMIT) * 60 + starttime - time);
223
224         if(autocvar_cl_announcer_maptime >= 2)
225                 ANNOUNCER_CHECKMINUTE(5);
226
227         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
228                 ANNOUNCER_CHECKMINUTE(1);
229 }
230
231 void Announcer()
232 {
233         // announcer code sets gametype name as centerprint title
234         if(!gametype)
235                 return;
236         Announcer_Gamestart();
237         Announcer_Time();
238 }