Remove some of that useless code kids are always talking about
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / announcer.qc
1 float announcer_1min;
2 float announcer_5min;
3 void Announcer_Countdown() 
4 {
5         float starttime = getstatf(STAT_GAMESTARTTIME);
6         float roundstarttime = getstatf(STAT_ROUNDSTARTTIME);
7         if(roundstarttime == -1)
8         {
9                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTOP);
10                 remove(self);
11                 return;
12         }
13         if(roundstarttime >= starttime)
14                 starttime = roundstarttime;
15         if(starttime <= time && roundstarttime != starttime) // game start time has passed
16                 announcer_5min = announcer_1min = FALSE; // reset maptime announcers now as well
17
18         float countdown = (starttime - time);
19         float countdown_rounded = floor(0.5 + countdown);
20
21         if(countdown <= 0) // countdown has finished, starttime is now
22         {
23                 Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_BEGIN); 
24                 Local_Notification(MSG_MULTI, MULTI_COUNTDOWN_BEGIN); 
25                 remove(self);
26                 return;
27         }
28         else // countdown is still going
29         {
30                 if(roundstarttime == starttime)
31                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_ROUNDSTART, countdown_rounded);
32                 else
33                         Local_Notification(MSG_CENTER, CENTER_COUNTDOWN_GAMESTART, countdown_rounded);
34
35                 switch(countdown_rounded)
36                 {
37                         case 1: Local_Notification(MSG_ANNCE, ANNCE_NUM_1); break;
38                         case 2: Local_Notification(MSG_ANNCE, ANNCE_NUM_2); break;
39                         case 3: Local_Notification(MSG_ANNCE, ANNCE_NUM_3); break;
40                 }
41
42                 self.nextthink = (starttime - (countdown - 1));
43         }
44 }
45
46 /**
47  * Checks whether the server initiated a map restart (stat_game_starttime changed)
48  *
49  * TODO: Use a better solution where a common shared entitiy is used that contains
50  * timelimit, fraglimit and game_starttime! Requires engine changes (remove STAT_TIMELIMIT
51  * and STAT_FRAGLIMIT to be auto-sent)
52  */
53  float previous_game_starttime;
54 void Announcer_Gamestart() 
55 {
56         float startTime = getstatf(STAT_GAMESTARTTIME);
57         float roundstarttime = getstatf(STAT_ROUNDSTARTTIME);
58         if(roundstarttime > startTime)
59                 startTime = roundstarttime;
60
61         if(previous_game_starttime != startTime) 
62         {
63                 if((time + 5.0) < startTime) // if connecting to server while restart was active don't always play prepareforbattle
64                         Local_Notification(MSG_ANNCE, ANNCE_PREPARE);
65
66                 if(time < startTime) 
67                 {
68                         entity e = find(world, classname, "announcer_countdown");
69                         if not(e)
70                         {
71                                 e = spawn();
72                                 e.classname = "announcer_countdown";
73                                 e.think = Announcer_Countdown;
74                         }
75                         e.nextthink = startTime - floor(startTime - time); //synchronize nextthink to startTime
76                 }
77         }
78         
79         previous_game_starttime = startTime;
80 }
81
82
83 // Plays the 1 minute or 5 minutes (of maptime) remaining sound, if client wants it
84 void Announcer_Time() 
85 {
86         float timelimit = getstatf(STAT_TIMELIMIT);
87         float timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
88         float warmup_timeleft = 0;
89         
90         if(warmup_stage) 
91                 if(autocvar_g_warmup_limit > 0)
92                         warmup_timeleft = max(0, autocvar_g_warmup_limit + getstatf(STAT_GAMESTARTTIME) - time); 
93
94         // 5 minute check
95         if(autocvar_cl_announcer_maptime >= 2) 
96         {
97                 // make sure that after connect (and e.g. 4 minutes left) we will not get a wrong sound
98                 if(announcer_5min)
99                 {
100                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timeleft > 300) 
101                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft > 300))
102                                         announcer_5min = FALSE;
103                 }
104                 else 
105                 {
106                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timelimit > 0 && timeleft < 300 && timeleft > 299) 
107                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft < 300 && warmup_timeleft > 299))
108                         {
109                                 //if we're in warmup mode, check whether there's a warmup timelimit
110                                 if not(autocvar_g_warmup_limit == -1 && warmup_stage) 
111                                 {
112                                         announcer_5min = TRUE;
113                                         Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_5);
114                                 }
115                         }
116                 }
117         }
118
119         // 1 minute check
120         if((autocvar_cl_announcer_maptime == 1) || (autocvar_cl_announcer_maptime == 3))
121         {
122                 if (announcer_1min)
123                 {
124                         if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timeleft > 60) 
125                                 || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft > 60))
126                                         announcer_1min = FALSE;
127                 }
128                 else if(((!warmup_stage || autocvar_g_warmup_limit == 0) && timelimit > 0 && timeleft < 60) 
129                         || (warmup_stage && autocvar_g_warmup_limit > 0 && warmup_timeleft < 60))
130                 {
131                         // if we're in warmup mode, check whether there's a warmup timelimit
132                         if not(autocvar_g_warmup_limit == -1 && warmup_stage) 
133                         {
134                                 announcer_1min = TRUE;
135                                 Local_Notification(MSG_ANNCE, ANNCE_REMAINING_MIN_1);
136                         }
137                 }
138         }
139 }
140
141 void Announcer()
142 {
143         Announcer_Gamestart();
144         Announcer_Time();
145 }