]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Merge branch 'master' into TimePath/items
[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 {
8         SELFPARAM();
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 (this.wait)
24         {
25                 this.wait = false;
26                 this.cnt = this.count + 1;  // init countdown
27                 round_starttime = time + this.count;
28                 reset_map(true);
29         }
30
31         if (this.cnt > 0)  // countdown running
32         {
33                 if (this.canRoundStart())
34                 {
35                         if (this.cnt == this.count + 1) round_starttime = time + this.count;
36                         int f = this.cnt - 1;
37                         if (f == 0)
38                         {
39                                 this.cnt = 0;
40                                 this.round_endtime = (this.round_timelimit) ? time + this.round_timelimit : 0;
41                                 this.nextthink = time;
42                                 if (this.roundStart) this.roundStart();
43                                 return;
44                         }
45                         this.cnt = this.cnt - 1;
46                 }
47                 else
48                 {
49                         round_handler_Reset(0);
50                 }
51                 this.nextthink = time + 1;  // canRoundStart every second
52         }
53         else
54         {
55                 if (this.canRoundEnd())
56                 {
57                         // schedule a new round
58                         this.wait = true;
59                         this.nextthink = time + this.delay;
60                 }
61                 else
62                 {
63                         this.nextthink = time;  // canRoundEnd every frame
64                 }
65         }
66 }
67
68 void round_handler_Init(float the_delay, float the_count, float the_round_timelimit)
69 {
70         entity this = round_handler;
71         this.delay = (the_delay > 0) ? the_delay : 0;
72         this.count = fabs(floor(the_count));
73         this.cnt = this.count + 1;
74         this.round_timelimit = (the_round_timelimit > 0) ? the_round_timelimit : 0;
75 }
76
77 // NOTE: this is only needed because if round_handler spawns at time 1
78 // gamestarttime isn't initialized yet
79 void round_handler_FirstThink()
80 {
81         SELFPARAM();
82         round_starttime = max(time, game_starttime) + this.count;
83         this.think = round_handler_Think;
84         this.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         entity this = round_handler = new(round_handler);
95
96         this.think = round_handler_FirstThink;
97         this.canRoundStart = canRoundStart_func;
98         this.canRoundEnd = canRoundEnd_func;
99         this.roundStart = roundStart_func;
100         this.wait = false;
101         round_handler_Init(5, 5, 180);
102         this.nextthink = time;
103 }
104
105 void round_handler_Reset(float next_think)
106 {
107         entity this = round_handler;
108         this.wait = false;
109         if (this.count)
110                 if (this.cnt < this.count + 1) this.cnt = this.count + 1;
111         this.nextthink = next_think;
112         round_starttime = (next_think) ? (next_think + this.count) : -1;
113 }
114
115 void round_handler_Remove()
116 {
117         remove(round_handler);
118         round_handler = world;
119 }