]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/announcer.qc
Merge branch 'z411/centerprint_fix' into 'master'
[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         float offset = stringwidth(pl2_name, true, hud_fontsize) - stringwidth(pl1_name, true, hud_fontsize) - 1;
46         centerprint_SetTitle(sprintf("^BG%s^BG  %s  %s", pl1_name, _("vs"), pl2_name), offset / 2);
47 }
48
49 void Announcer_ClearTitle()
50 {
51         strfree(prev_pl1_name);
52         strfree(prev_pl2_name);
53         centerprint_ClearTitle();
54 }
55
56 bool prev_inround;
57 void Announcer_Countdown(entity this)
58 {
59         float starttime = STAT(GAMESTARTTIME);
60         float roundstarttime = STAT(ROUNDSTARTTIME);
61         if(roundstarttime == -1)
62         {
63                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
64                 delete(this);
65                 announcer_countdown = NULL;
66                 Announcer_ClearTitle();
67                 return;
68         }
69
70         bool inround = (roundstarttime && time >= starttime);
71         float countdown = (inround ? roundstarttime - time : starttime - time);
72         float countdown_rounded = floor(0.5 + countdown);
73
74         if(countdown <= 0) // countdown has finished, starttime is now
75         {
76                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_BEGIN);
77                 Local_Notification(MSG_MULTI, MULTI_COUNTDOWN_BEGIN);
78                 delete(this);
79                 announcer_countdown = NULL;
80                 Announcer_ClearTitle();
81                 return;
82         }
83         else // countdown is still going
84         {
85                 if(inround)
86                 {
87                         if(!prev_inround) Announcer_ClearTitle(); // clear title if we just started the match
88                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTART, STAT(ROUNDS_PLAYED) + 1, countdown_rounded);
89                         Notification annce_num = Announcer_PickNumber(CNT_ROUNDSTART, countdown_rounded);
90                         if(annce_num != NULL)
91                                 Local_Notification(MSG_ANNCE, annce_num);
92                         this.nextthink = (roundstarttime - (countdown - 1));
93                 }
94                 else
95                 {
96                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_GAMESTART, countdown_rounded);
97                         Notification annce_num = Announcer_PickNumber(CNT_GAMESTART, countdown_rounded);
98                         if(!roundstarttime && annce_num != NULL) // Don't announce game start in round based modes
99                                 Local_Notification(MSG_ANNCE, annce_num);
100                         this.nextthink = (starttime - (countdown - 1));
101                 }
102         }
103
104         prev_inround = inround;
105 }
106
107 /**
108  * Checks whether the server initiated a map restart (stat_game_starttime changed)
109  *
110  * TODO: Use a better solution where a common shared entitiy is used that contains
111  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
112  * and STAT_FRAGLIMIT to be auto-sent)
113  */
114 float previous_game_starttime;
115 void Announcer_Gamestart()
116 {
117         float startTime = STAT(GAMESTARTTIME);
118         float roundstarttime = STAT(ROUNDSTARTTIME);
119         if(roundstarttime > startTime)
120                 startTime = roundstarttime;
121         if(intermission)
122         {
123                 if(announcer_countdown)
124                 {
125                         centerprint_Kill(ORDINAL(CPID_ROUND));
126                         if(announcer_countdown)
127                         {
128                                 delete(announcer_countdown);
129                                 announcer_countdown = NULL;
130                         }
131                 }
132                 return;
133         }
134
135         if(announcer_countdown && gametype.m_1v1)
136                 Announcer_Duel();
137
138         if(previous_game_starttime != startTime)
139         {
140                 if(time < startTime)
141                 {
142                         if (!announcer_countdown)
143                         {
144                                 announcer_countdown = new(announcer_countdown);
145                                 setthink(announcer_countdown, Announcer_Countdown);
146                         }
147
148                         if(!warmup_stage && time < STAT(GAMESTARTTIME))
149                         {
150                                 if (gametype.m_1v1)
151                                         Announcer_Duel();
152                                 else
153                                         centerprint_SetTitle(strcat("^BG", MapInfo_Type_ToText(gametype)), 0); // Show game type as title
154
155                                 if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
156                                         Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
157                         }
158
159                         announcer_countdown.nextthink = startTime - floor(startTime - time + 0.5); //synchronize nextthink to startTime
160                 }
161         }
162
163         previous_game_starttime = startTime;
164 }
165
166 #define ANNOUNCER_CHECKMINUTE(minute) MACRO_BEGIN \
167         if(announcer_##minute##min) { \
168                 if(timeleft > minute * 60) \
169                         announcer_##minute##min = false; \
170         } else { \
171                 if(timeleft < minute * 60 && timeleft > minute * 60 - 1) { \
172                         announcer_##minute##min = true; \
173                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_##minute); \
174                 } \
175         } \
176 MACRO_END
177
178 void Announcer_Time()
179 {
180         static bool warmup_stage_prev;
181
182         if(intermission)
183                 return;
184
185         if (warmup_stage != warmup_stage_prev)
186         {
187                 announcer_5min = announcer_1min = false;
188                 warmup_stage_prev = warmup_stage;
189                 return;
190         }
191
192         float starttime = STAT(GAMESTARTTIME);
193         if(time < starttime)
194         {
195                 announcer_5min = announcer_1min = false;
196                 return;
197         }
198
199         float timeleft;
200         if(warmup_stage)
201         {
202                 float warmup_timelimit = STAT(WARMUP_TIMELIMIT);
203                 if(warmup_timelimit > 0)
204                         timeleft = max(0, warmup_timelimit - time);
205                 else
206                         timeleft = 0;
207         }
208         else
209                 timeleft = max(0, STAT(TIMELIMIT) * 60 + starttime - time);
210
211         if(autocvar_cl_announcer_maptime >= 2)
212                 ANNOUNCER_CHECKMINUTE(5);
213
214         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
215                 ANNOUNCER_CHECKMINUTE(1);
216 }
217
218 void Announcer()
219 {
220         Announcer_Gamestart();
221         Announcer_Time();
222 }