]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Fix other round countdown issues by improving workaround for uninitialized game_start...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / round_handler.qc
1 void round_handler_Think()
2 {
3         float f;
4
5         if(gameover)
6         {
7                 round_handler_Reset(0);
8                 round_handler_Remove();
9                 return;
10         }
11
12         if(self.wait)
13         {
14                 self.wait = FALSE;
15                 self.cnt = self.count + 1; // init countdown
16                 round_starttime = time + self.count;
17                 reset_map(TRUE);
18         }
19
20         if(self.cnt > 0) // countdown running
21         {
22                 if(self.canRoundStart())
23                 {
24                         if(self.cnt == self.count + 1)
25                                 round_starttime = time + self.count;
26                         f = self.cnt - 1;
27                         if(f == 0)
28                         {
29                                 self.cnt = 0;
30                                 self.round_endtime = time + self.round_timelimit;
31                                 self.nextthink = time;
32                                 if(self.roundStart)
33                                         self.roundStart();
34                                 return;
35                         }
36                         self.cnt = self.cnt - 1;
37                 }
38                 else
39                 {
40                         round_handler_Reset(0);
41                 }
42                 self.nextthink = time + 1; // canRoundStart every second
43         }
44         else
45         {
46                 if(self.canRoundEnd())
47                 {
48                         // schedule a new round
49                         self.wait = TRUE;
50                         self.nextthink = time + self.delay;
51                 }
52                 else
53                 {
54                         self.nextthink = time; // canRoundEnd every frame
55                 }
56         }
57 }
58
59 void round_handler_Init(float the_delay, float the_count, float the_round_timelimit)
60 {
61         round_handler.delay = (the_delay > 0) ? the_delay : 0;
62         round_handler.count = fabs(floor(the_count));
63         round_handler.cnt = round_handler.count + 1;
64         round_handler.round_timelimit = max(10, the_round_timelimit);
65 }
66
67 // NOTE: this is only needed because if round_handler spawns at time 1
68 // gamestarttime isn't initialized yet
69 void round_handler_FirstThink()
70 {
71         round_starttime = max(time, game_starttime) + round_handler.count;
72         round_handler.think = round_handler_Think;
73         round_handler.nextthink = max(time, game_starttime);
74 }
75
76 void round_handler_Spawn(float() canRoundStart_func, float() canRoundEnd_func, void() roundStart_func)
77 {
78         if(round_handler)
79         {
80                 backtrace("Can't spawn round_handler again!");
81                 return;
82         }
83         round_handler = spawn();
84         round_handler.classname = "round_handler";
85
86         round_handler.think = round_handler_FirstThink;
87         round_handler.canRoundStart = canRoundStart_func;
88         round_handler.canRoundEnd = canRoundEnd_func;
89         round_handler.roundStart = roundStart_func;
90         round_handler.wait = FALSE;
91         round_handler_Init(5, 5, 180);
92         round_handler.nextthink = time;
93 }
94
95 void round_handler_Reset(float next_think)
96 {
97         round_handler.wait = FALSE;
98         if(round_handler.count)
99         if(round_handler.cnt < round_handler.count + 1)
100                 round_handler.cnt = round_handler.count + 1;
101         round_handler.nextthink = next_think;
102         round_starttime = (next_think) ? (next_think + round_handler.count) : -1;
103 }
104
105 void round_handler_Remove()
106 {
107         remove(round_handler);
108         round_handler = world;
109 }
110