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