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