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