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