]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Added round number in countdown
[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 (intermission_running)
12         {
13                 round_handler_Reset(0);
14                 round_handler_Remove();
15                 return;
16         }
17
18         if (time < game_starttime)
19         {
20                 round_handler_Reset(game_starttime);
21                 return;
22         }
23
24         game_stopped = false;
25
26         if (this.wait)
27         {
28                 this.wait = false;
29                 this.cnt = this.count + 1;  // init countdown
30                 round_starttime = time + this.count;
31                 reset_map(true, false);
32         }
33
34         if (this.cnt > 0)  // countdown running
35         {
36                 if (this.canRoundStart() && !(autocvar_g_campaign && !campaign_bots_may_start))
37                 {
38                         if (this.cnt == this.count + 1) {
39                                 round_starttime = time + this.count;
40                                 ++round_count;
41                         }
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                                 if (this.roundStart) this.roundStart();
49                                 return;
50                         }
51                         this.cnt = this.cnt - 1;
52                 }
53                 else
54                 {
55                         round_handler_Reset(0);
56                         round_starttime = -1; // can't start
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         round_limit = the_round_timelimit;
83 }
84
85 // NOTE: this is only needed because if round_handler spawns at time 1
86 // game_starttime isn't initialized yet
87 void round_handler_FirstThink(entity this)
88 {
89         round_starttime = max(time, game_starttime) + this.count;
90         setthink(this, round_handler_Think);
91         this.nextthink = max(time, game_starttime);
92 }
93
94 void round_handler_Spawn(bool() canRoundStart_func, bool() canRoundEnd_func, void() roundStart_func)
95 {
96         if (round_handler)
97         {
98                 backtrace("Can't spawn round_handler again!");
99                 return;
100         }
101         entity this = round_handler = new_pure(round_handler);
102
103         setthink(this, round_handler_FirstThink);
104         this.canRoundStart = canRoundStart_func;
105         this.canRoundEnd = canRoundEnd_func;
106         this.roundStart = roundStart_func;
107         this.wait = false;
108         round_handler_Init(5, 5, 180);
109         this.nextthink = time;
110 }
111
112 void round_handler_Reset(float next_think)
113 {
114         entity this = round_handler;
115         this.wait = false;
116         if (this.count)
117                 if (this.cnt < this.count + 1) this.cnt = this.count + 1;
118         this.nextthink = next_think;
119         if (next_think)
120                 round_starttime = next_think + this.count;
121 }
122
123 void round_handler_Remove()
124 {
125         delete(round_handler);
126         round_handler = NULL;
127 }