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