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