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