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