]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/round_handler.qc
Merge branch 'master' into Mario/ons_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / round_handler.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qh"
5     #include "../dpdefs/dpextensions.qh"
6     #include "../common/util.qh"
7     #include "defs.qh"
8     #include "round_handler.qh"
9 #endif
10
11 void round_handler_Think()
12 {
13         float f;
14
15         if(time < game_starttime)
16         {
17                 round_handler_Reset(game_starttime);
18                 return;
19         }
20
21         if(gameover)
22         {
23                 round_handler_Reset(0);
24                 round_handler_Remove();
25                 return;
26         }
27
28         if(self.wait)
29         {
30                 self.wait = false;
31                 self.cnt = self.count + 1; // init countdown
32                 round_starttime = time + self.count;
33                 reset_map(true);
34         }
35
36         if(self.cnt > 0) // countdown running
37         {
38                 if(self.canRoundStart())
39                 {
40                         if(self.cnt == self.count + 1)
41                                 round_starttime = time + self.count;
42                         f = self.cnt - 1;
43                         if(f == 0)
44                         {
45                                 self.cnt = 0;
46                                 self.round_endtime = (self.round_timelimit) ? time + self.round_timelimit : 0;
47                                 self.nextthink = time;
48                                 if(self.roundStart)
49                                         self.roundStart();
50                                 return;
51                         }
52                         self.cnt = self.cnt - 1;
53                 }
54                 else
55                 {
56                         round_handler_Reset(0);
57                 }
58                 self.nextthink = time + 1; // canRoundStart every second
59         }
60         else
61         {
62                 if(self.canRoundEnd())
63                 {
64                         // schedule a new round
65                         self.wait = true;
66                         self.nextthink = time + self.delay;
67                 }
68                 else
69                 {
70                         self.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         round_handler.delay = (the_delay > 0) ? the_delay : 0;
78         round_handler.count = fabs(floor(the_count));
79         round_handler.cnt = round_handler.count + 1;
80         round_handler.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 // gamestarttime isn't initialized yet
85 void round_handler_FirstThink()
86 {
87         round_starttime = max(time, game_starttime) + round_handler.count;
88         round_handler.think = round_handler_Think;
89         round_handler.nextthink = max(time, game_starttime);
90 }
91
92 void round_handler_Spawn(float() canRoundStart_func, float() canRoundEnd_func, void() roundStart_func)
93 {
94         if(round_handler)
95         {
96                 backtrace("Can't spawn round_handler again!");
97                 return;
98         }
99         round_handler = spawn();
100         round_handler.classname = "round_handler";
101
102         round_handler.think = round_handler_FirstThink;
103         round_handler.canRoundStart = canRoundStart_func;
104         round_handler.canRoundEnd = canRoundEnd_func;
105         round_handler.roundStart = roundStart_func;
106         round_handler.wait = false;
107         round_handler_Init(5, 5, 180);
108         round_handler.nextthink = time;
109 }
110
111 void round_handler_Reset(float next_think)
112 {
113         round_handler.wait = false;
114         if(round_handler.count)
115         if(round_handler.cnt < round_handler.count + 1)
116                 round_handler.cnt = round_handler.count + 1;
117         round_handler.nextthink = next_think;
118         round_starttime = (next_think) ? (next_think + round_handler.count) : -1;
119 }
120
121 void round_handler_Remove()
122 {
123         remove(round_handler);
124         round_handler = world;
125 }
126