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