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