]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Merged master
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / round_handler.qc
1 #include "round_handler.qh"
2
3 #include "campaign.qh"
4 #include "command/vote.qh"
5 #include "../common/util.qh"
6
7 void round_handler_Think(entity this)
8 {
9         if (intermission_running)
10         {
11                 round_handler_Reset(0);
12                 round_handler_Remove();
13                 return;
14         }
15
16         if (time < game_starttime)
17         {
18                 round_handler_Reset(game_starttime);
19                 return;
20         }
21
22         game_stopped = false;
23
24         if (this.wait)
25         {
26                 this.wait = false;
27                 this.cnt = this.count + 1;  // init countdown
28                 round_starttime = time + this.count;
29                 reset_map(true);
30         }
31
32         if (this.cnt > 0)  // countdown running
33         {
34                 if (this.canRoundStart() && !(autocvar_g_campaign && !campaign_bots_may_start))
35                 {
36                         if (this.cnt == this.count + 1) round_starttime = time + this.count;
37                         int f = this.cnt - 1;
38                         if (f == 0)
39                         {
40                                 this.cnt = 0;
41                                 this.round_endtime = (this.round_timelimit) ? time + this.round_timelimit : 0;
42                                 this.nextthink = time;
43                                 if (this.roundStart) this.roundStart();
44                                 return;
45                         }
46                         this.cnt = this.cnt - 1;
47                 }
48                 else
49                 {
50                         round_handler_Reset(0);
51                 }
52                 this.nextthink = time + 1;  // canRoundStart every second
53         }
54         else
55         {
56                 if (this.canRoundEnd())
57                 {
58                         // schedule a new round
59                         this.wait = true;
60                         this.nextthink = time + this.delay;
61                 }
62                 else
63                 {
64                         this.nextthink = time;  // canRoundEnd every frame
65                 }
66         }
67 }
68
69 void round_handler_Init(float the_delay, float the_count, float the_round_timelimit)
70 {
71         entity this = round_handler;
72         this.delay = (the_delay > 0) ? the_delay : 0;
73         this.count = fabs(floor(the_count));
74         this.cnt = this.count + 1;
75         this.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 // game_starttime isn't initialized yet
80 void round_handler_FirstThink(entity this)
81 {
82         round_starttime = max(time, game_starttime) + this.count;
83         setthink(this, 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         setthink(this, 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         delete(round_handler);
118         round_handler = NULL;
119 }