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