]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/announcer.qc
Merge branch 'master' into z411/new_centerprint
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / announcer.qc
1 #include "announcer.qh"
2
3 #include <client/hud/panel/centerprint.qh>
4 #include <client/mutators/_mod.qh>
5 #include <common/notifications/all.qh>
6 #include <common/stats.qh>
7 #include <common/mapinfo.qh>
8
9 bool announcer_1min;
10 bool announcer_5min;
11 string AnnouncerOption()
12 {
13         string ret = autocvar_cl_announcer;
14         MUTATOR_CALLHOOK(AnnouncerOption, ret);
15         ret = M_ARGV(0, string);
16         return ret;
17 }
18
19 entity announcer_countdown;
20
21 void Announcer_Countdown(entity this)
22 {
23         float starttime = STAT(GAMESTARTTIME);
24         float roundstarttime = STAT(ROUNDSTARTTIME);
25         if(roundstarttime == -1)
26         {
27                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
28                 delete(this);
29                 announcer_countdown = NULL;
30                 centerprint_ClearTitle();
31                 return;
32         }
33
34         bool inround = (roundstarttime && time >= starttime);
35         float countdown = (inround ? roundstarttime - time : starttime - time);
36         float countdown_rounded = floor(0.5 + countdown);
37
38         if(time >= starttime) centerprint_ClearTitle();
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                                 centerprint_SetTitle(strcat("^BG", MapInfo_Type_ToText(gametype))); // Set centerprint title
111                                 if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
112                                         Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
113                         }
114
115                         announcer_countdown.nextthink = startTime - floor(startTime - time + 0.5); //synchronize nextthink to startTime
116                 }
117         }
118
119         previous_game_starttime = startTime;
120 }
121
122 #define ANNOUNCER_CHECKMINUTE(minute) MACRO_BEGIN \
123         if(announcer_##minute##min) { \
124                 if(timeleft > minute * 60) \
125                         announcer_##minute##min = false; \
126         } else { \
127                 if(timeleft < minute * 60 && timeleft > minute * 60 - 1) { \
128                         announcer_##minute##min = true; \
129                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_##minute); \
130                 } \
131         } \
132 MACRO_END
133
134 void Announcer_Time()
135 {
136         static bool warmup_stage_prev;
137
138         if(intermission)
139                 return;
140
141         if (warmup_stage != warmup_stage_prev)
142         {
143                 announcer_5min = announcer_1min = false;
144                 warmup_stage_prev = warmup_stage;
145                 return;
146         }
147
148         float starttime = STAT(GAMESTARTTIME);
149         if(time < starttime)
150         {
151                 announcer_5min = announcer_1min = false;
152                 return;
153         }
154
155         float timeleft;
156         if(warmup_stage)
157         {
158                 float warmup_timelimit = STAT(WARMUP_TIMELIMIT);
159                 if(warmup_timelimit > 0)
160                         timeleft = max(0, warmup_timelimit - time);
161                 else
162                         timeleft = 0;
163         }
164         else
165                 timeleft = max(0, STAT(TIMELIMIT) * 60 + starttime - time);
166
167         if(autocvar_cl_announcer_maptime >= 2)
168                 ANNOUNCER_CHECKMINUTE(5);
169
170         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
171                 ANNOUNCER_CHECKMINUTE(1);
172 }
173
174 void Announcer()
175 {
176         Announcer_Gamestart();
177         Announcer_Time();
178 }