]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/announcer.qc
109ee5ac1c25cb0e5529cadfd46a527f616a964f
[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
8 bool announcer_1min;
9 bool announcer_5min;
10 string AnnouncerOption()
11 {
12         string ret = autocvar_cl_announcer;
13         MUTATOR_CALLHOOK(AnnouncerOption, ret);
14         ret = M_ARGV(0, string);
15         return ret;
16 }
17
18 entity announcer_countdown;
19
20 void Announcer_Countdown(entity this)
21 {
22         float starttime = STAT(GAMESTARTTIME);
23         float roundstarttime = STAT(ROUNDSTARTTIME);
24         if(roundstarttime == -1)
25         {
26                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
27                 delete(this);
28                 announcer_countdown = NULL;
29                 return;
30         }
31         if(starttime <= time && roundstarttime != starttime) // game start time has passed
32                 announcer_5min = announcer_1min = false; // reset maptime announcers now as well
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(countdown <= 0) // countdown has finished, starttime is now
39         {
40                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_BEGIN);
41                 Local_Notification(MSG_MULTI, MULTI_COUNTDOWN_BEGIN);
42                 delete(this);
43                 announcer_countdown = NULL;
44                 return;
45         }
46         else // countdown is still going
47         {
48                 // if concomitant countdown to round start overrides countdown to game start
49                 if(inround)
50                 {
51                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTART, countdown_rounded);
52                         Notification annce_num = Announcer_PickNumber(CNT_ROUNDSTART, countdown_rounded);
53                         if(annce_num != NULL)
54                                 Local_Notification(MSG_ANNCE, annce_num);
55                         this.nextthink = (roundstarttime - (countdown - 1));
56                 }
57                 else
58                 {
59                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_GAMESTART, countdown_rounded);
60                         Notification annce_num = Announcer_PickNumber(CNT_GAMESTART, countdown_rounded);
61                         if(!roundstarttime && annce_num != NULL) // Don't announce game start in round based modes
62                                 Local_Notification(MSG_ANNCE, annce_num);
63                         this.nextthink = (starttime - (countdown - 1));
64                 }
65         }
66 }
67
68 /**
69  * Checks whether the server initiated a map restart (stat_game_starttime changed)
70  *
71  * TODO: Use a better solution where a common shared entitiy is used that contains
72  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
73  * and STAT_FRAGLIMIT to be auto-sent)
74  */
75  float previous_game_starttime;
76 void Announcer_Gamestart()
77 {
78         float startTime = STAT(GAMESTARTTIME);
79         float roundstarttime = STAT(ROUNDSTARTTIME);
80         if(roundstarttime > startTime)
81                 startTime = roundstarttime;
82         if(intermission)
83         {
84                 if(announcer_countdown)
85                 {
86                         centerprint_Kill(ORDINAL(CPID_ROUND));
87                         if(announcer_countdown)
88                         {
89                                 delete(announcer_countdown);
90                                 announcer_countdown = NULL;
91                         }
92                 }
93                 return;
94         }
95
96         if(previous_game_starttime != startTime)
97         {
98                 if(time < startTime)
99                 {
100                         if (!announcer_countdown)
101                         {
102                                 announcer_countdown = new(announcer_countdown);
103                                 setthink(announcer_countdown, Announcer_Countdown);
104                         }
105
106                         if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
107                         if(time > announcer_countdown.nextthink) // don't play it again if countdown was already going
108                                 Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
109
110                         announcer_countdown.nextthink = startTime - floor(startTime - time + 0.5); //synchronize nextthink to startTime
111                 }
112         }
113
114         previous_game_starttime = startTime;
115 }
116
117 #define ANNOUNCER_CHECKMINUTE(minute) MACRO_BEGIN \
118         if(announcer_##minute##min) { \
119                 if(timeleft > minute * 60) \
120                         announcer_##minute##min = false; \
121         } else { \
122                 if(timeleft < minute * 60 && timeleft > minute * 60 - 1) { \
123                         announcer_##minute##min = true; \
124                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_##minute); \
125                 } \
126         } \
127 MACRO_END
128
129 void Announcer_Time()
130 {
131         if(intermission)
132                 return;
133
134         float timeleft;
135         if(warmup_stage)
136         {
137                 float warmup_timelimit = STAT(WARMUP_TIMELIMIT);
138                 if(warmup_timelimit > 0)
139                         timeleft = max(0, warmup_timelimit - time);
140                 else
141                         timeleft = 0;
142         }
143         else
144                 timeleft = max(0, STAT(TIMELIMIT) * 60 + STAT(GAMESTARTTIME) - time);
145
146         if(autocvar_cl_announcer_maptime >= 2)
147                 ANNOUNCER_CHECKMINUTE(5);
148
149         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
150                 ANNOUNCER_CHECKMINUTE(1);
151 }
152
153 void Announcer()
154 {
155         Announcer_Gamestart();
156         Announcer_Time();
157 }