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