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