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