]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Merge branch 'drjaska/mayhem' 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/world.qh>
8
9 void round_handler_Think(entity this)
10 {
11         if (game_timeout) { this.nextthink = time + 1; return; }
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(true);
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                                 this.cnt = 0;
45                                 this.round_endtime = (this.round_timelimit) ? time + this.round_timelimit : 0;
46                                 this.nextthink = time;
47                                 FOREACH_CLIENT(IS_PLAYER(it), { roundaccuracy_clear(it); });
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                 }
57                 this.nextthink = time + 1;  // canRoundStart every second
58         }
59         else
60         {
61                 if (this.canRoundEnd())
62                 {
63                         // schedule a new round
64                         this.wait = true;
65                         this.nextthink = time + this.delay;
66                 }
67                 else
68                 {
69                         this.nextthink = time;  // canRoundEnd every frame
70                 }
71         }
72 }
73
74 void round_handler_Init(float the_delay, float the_count, float the_round_timelimit)
75 {
76         entity this = round_handler;
77         this.delay = (the_delay > 0) ? the_delay : 0;
78         this.count = fabs(floor(the_count));
79         this.cnt = this.count + 1;
80         this.round_timelimit = (the_round_timelimit > 0) ? the_round_timelimit : 0;
81 }
82
83 // NOTE: this is only needed because if round_handler spawns at time 1
84 // game_starttime isn't initialized yet
85 void round_handler_FirstThink(entity this)
86 {
87         round_starttime = max(time, game_starttime) + this.count;
88         setthink(this, round_handler_Think);
89         this.nextthink = max(time, game_starttime);
90 }
91
92 void round_handler_Spawn(bool() canRoundStart_func, bool() canRoundEnd_func, void() roundStart_func)
93 {
94         if (round_handler)
95         {
96                 backtrace("Can't spawn round_handler again!");
97                 return;
98         }
99         entity this = round_handler = new_pure(round_handler);
100
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
110 void round_handler_Activate(bool active) {
111         if (round_handler) {
112                 entity this = round_handler;
113         
114                 this.isactive = active;
115                 if(active) {
116                         setthink(this, round_handler_FirstThink);
117                         this.nextthink = time;
118                 }
119         }
120 }
121
122 void round_handler_Reset(float next_think)
123 {
124         entity this = round_handler;
125         this.wait = false;
126         if (this.count)
127                 if (this.cnt < this.count + 1) this.cnt = this.count + 1;
128         this.nextthink = next_think;
129         round_starttime = (next_think) ? (next_think + this.count) : -1;
130 }
131
132 void round_handler_Remove()
133 {
134         delete(round_handler);
135         round_handler = NULL;
136 }