Makefile: use `-I.`
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / announcer.qc
1 #include "announcer.qh"
2
3 #include "mutators/events.qh"
4
5 #include <common/notifications.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 = ret_string;
15         return ret;
16 }
17
18 entity announcer_countdown;
19
20 void Announcer_Countdown()
21 {
22         SELFPARAM();
23         float starttime = STAT(GAMESTARTTIME);
24         float roundstarttime = STAT(ROUNDSTARTTIME);
25         if(roundstarttime == -1)
26         {
27                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
28                 remove(this);
29                 announcer_countdown = NULL;
30                 return;
31         }
32         if(roundstarttime >= starttime)
33                 starttime = roundstarttime;
34         if(starttime <= time && roundstarttime != starttime) // game start time has passed
35                 announcer_5min = announcer_1min = false; // reset maptime announcers now as well
36
37         float countdown = (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                 remove(this);
45                 announcer_countdown = NULL;
46                 return;
47         }
48         else // countdown is still going
49         {
50                 // if concomitant countdown to round start overrides countdown to game start
51                 if(roundstarttime == starttime)
52                 {
53                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTART, countdown_rounded);
54                         Local_Notification(MSG_ANNCE, Announcer_PickNumber(CNT_ROUNDSTART, countdown_rounded));
55                 }
56                 else
57                 {
58                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_GAMESTART, countdown_rounded);
59                         Local_Notification(MSG_ANNCE, Announcer_PickNumber(CNT_GAMESTART, countdown_rounded));
60                 }
61
62                 this.nextthink = (starttime - (countdown - 1));
63         }
64 }
65
66 /**
67  * Checks whether the server initiated a map restart (stat_game_starttime changed)
68  *
69  * TODO: Use a better solution where a common shared entitiy is used that contains
70  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
71  * and STAT_FRAGLIMIT to be auto-sent)
72  */
73  float previous_game_starttime;
74 void Announcer_Gamestart()
75 {
76         float startTime = STAT(GAMESTARTTIME);
77         float roundstarttime = STAT(ROUNDSTARTTIME);
78         if(roundstarttime > startTime)
79                 startTime = roundstarttime;
80
81         if(previous_game_starttime != startTime)
82         {
83                 if(time < startTime)
84                 {
85                         if (!announcer_countdown)
86                         {
87                                 announcer_countdown = new(announcer_countdown);
88                                 announcer_countdown.think = Announcer_Countdown;
89                         }
90
91                         if(time + 5.0 < startTime) // if connecting to server while restart was active don't always play prepareforbattle
92                         if(time > announcer_countdown.nextthink) // don't play it again if countdown was already going
93                                 Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
94
95                         announcer_countdown.nextthink = startTime - floor(startTime - time); //synchronize nextthink to startTime
96                 }
97         }
98
99         previous_game_starttime = startTime;
100 }
101
102
103 // Plays the 1 minute or 5 minutes (of maptime) remaining sound, if client wants it
104 void Announcer_Time()
105 {
106         float timelimit = STAT(TIMELIMIT);
107         float timeleft = max(0, timelimit * 60 + STAT(GAMESTARTTIME) - time);
108         float warmup_timeleft = 0;
109
110         if(warmup_stage)
111                 if(autocvar_g_warmup_limit > 0)
112                         warmup_timeleft = max(0, autocvar_g_warmup_limit + STAT(GAMESTARTTIME) - time);
113
114         // 5 minute check
115         if(autocvar_cl_announcer_maptime >= 2)
116         {
117                 // make sure that after connect (and e.g. 4 minutes left) we will not get a wrong sound
118                 if(announcer_5min)
119                 {
120                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timeleft > 300)
121                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft > 300))
122                                         announcer_5min = false;
123                 }
124                 else
125                 {
126                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timelimit > 0 && timeleft < 300 && timeleft > 299)
127                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft < 300 && warmup_timeleft > 299))
128                         {
129                                 //if we're in warmup mode, check whether there's a warmup timelimit
130                                 if(!(autocvar_g_warmup_limit == -1 && warmup_stage))
131                                 {
132                                         announcer_5min = true;
133                                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_5);
134                                 }
135                         }
136                 }
137         }
138
139         // 1 minute check
140         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
141         {
142                 if (announcer_1min)
143                 {
144                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timeleft > 60)
145                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft > 60))
146                                         announcer_1min = false;
147                 }
148                 else if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timelimit > 0 && timeleft < 60)
149                         || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft < 60))
150                 {
151                         // if we're in warmup mode, check whether there's a warmup timelimit
152                         if(!(autocvar_g_warmup_limit == -1 && warmup_stage))
153                         {
154                                 announcer_1min = true;
155                                 Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_1);
156                         }
157                 }
158         }
159 }
160
161 void Announcer()
162 {
163         Announcer_Gamestart();
164         Announcer_Time();
165 }