]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_arena.qc
Add Arena to the mutator system, making use of round_handler. Also add support for...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_arena.qc
1 void Spawnqueue_Insert(entity e)
2 {
3         if(e.spawnqueue_in)
4                 return;
5         dprint(strcat("Into queue: ", e.netname, "\n"));
6         e.spawnqueue_in = TRUE;
7         e.spawnqueue_prev = spawnqueue_last;
8         e.spawnqueue_next = world;
9         if(spawnqueue_last)
10                 spawnqueue_last.spawnqueue_next = e;
11         spawnqueue_last = e;
12         if(!spawnqueue_first)
13                 spawnqueue_first = e;
14 }
15
16 void Spawnqueue_Remove(entity e)
17 {
18         if(!e.spawnqueue_in)
19                 return;
20         dprint(strcat("Out of queue: ", e.netname, "\n"));
21         e.spawnqueue_in = FALSE;
22         if(e == spawnqueue_first)
23                 spawnqueue_first = e.spawnqueue_next;
24         if(e == spawnqueue_last)
25                 spawnqueue_last = e.spawnqueue_prev;
26         if(e.spawnqueue_prev)
27                 e.spawnqueue_prev.spawnqueue_next = e.spawnqueue_next;
28         if(e.spawnqueue_next)
29                 e.spawnqueue_next.spawnqueue_prev = e.spawnqueue_prev;
30         e.spawnqueue_next = world;
31         e.spawnqueue_prev = world;
32 }
33
34 void Spawnqueue_Unmark(entity e)
35 {
36         if(!e.spawned)
37                 return;
38         e.spawned = FALSE;
39         numspawned = numspawned - 1;
40 }
41
42 void Spawnqueue_Mark(entity e)
43 {
44         if(e.spawned)
45                 return;
46         e.spawned = TRUE;
47         numspawned = numspawned + 1;
48 }
49
50 float Arena_CheckWinner()
51 {
52         entity e;
53
54         // FIXME do this only once (on round start)
55         FOR_EACH_PLAYER(e)
56                 e.player_blocked = 0;
57
58         if(round_handler_GetTimeLeft() <= 0)
59         {
60                 FOR_EACH_REALCLIENT(e)
61                         centerprint(e, "Round over, there's no winner");
62                 bprint("Round over, there's no winner\n");
63                 return 1;
64         }
65
66         if(numspawned > 1)
67                 return 0;
68
69         entity champion;
70         champion = world;
71         FOR_EACH_CLIENT(e)
72         {
73                 if(e.spawned && e.classname == "player")
74                         champion = e;
75         }
76
77         if(champion)
78         {
79                 FOR_EACH_REALCLIENT(e)
80                         centerprint(e, strcat("The Champion is ", champion.netname));
81                 bprint("The Champion is ", champion.netname, "\n");
82                 UpdateFrags(champion, +1);
83         }
84         else
85         {
86                 FOR_EACH_REALCLIENT(e)
87                         centerprint(e, "Round tied");
88                 bprint("Round tied\n");
89         }
90         return 1;
91 }
92 void Arena_AddChallengers()
93 {
94         entity e;
95         e = self;
96         while(numspawned < maxspawned && spawnqueue_first)
97         {
98                 self = spawnqueue_first;
99
100                 bprint ("^4", self.netname, "^4 is the next challenger\n");
101
102                 Spawnqueue_Remove(self);
103                 Spawnqueue_Mark(self);
104
105                 self.classname = "player";
106                 PutClientInServer();
107         }
108         self = e;
109 }
110
111 float Arena_CheckPlayers()
112 {
113         Arena_AddChallengers();
114
115         if(numspawned >= 2)
116                 return 1;
117
118         return 0;
119 }
120
121 MUTATOR_HOOKFUNCTION(arena_ClientDisconnect)
122 {
123         Spawnqueue_Unmark(self);
124         Spawnqueue_Remove(self);
125         return 1;
126 }
127
128 MUTATOR_HOOKFUNCTION(arena_reset_map_players)
129 {
130         FOR_EACH_CLIENT(self)
131         {
132                 if(self.spawned)
133                 {
134                         PutClientInServer();
135                         self.player_blocked = 1;
136                 }
137                 else
138                         PutObserverInServer();
139         }
140         return 1;
141 }
142
143 MUTATOR_HOOKFUNCTION(arena_MakePlayerObserver)
144 {
145         self.frags = FRAGS_PLAYER;
146         if(self.version_mismatch)
147         {
148                 Spawnqueue_Unmark(self);
149                 Spawnqueue_Remove(self);
150         }
151         else
152                 Spawnqueue_Insert(self);
153         return 1;
154 }
155
156 MUTATOR_HOOKFUNCTION(arena_PutClientInServer)
157 {
158         if(!self.spawned)
159                 self.classname = "observer";
160         return 1;
161 }
162
163 MUTATOR_HOOKFUNCTION(arena_PlayerSpawn)
164 {
165         Spawnqueue_Remove(self);
166         Spawnqueue_Mark(self);
167         if(arena_roundbased)
168                 self.player_blocked = 1;
169         return 1;
170 }
171
172 MUTATOR_HOOKFUNCTION(arena_PlayerPreThink)
173 {
174         self.stat_respawn_time = 0;
175
176         // put dead players in the spawn queue
177         if(arena_roundbased)
178         if(self.deadflag && time - self.death_time >= 1.5)
179                 PutClientInServer();
180
181         return 1;
182 }
183
184 MUTATOR_HOOKFUNCTION(arena_ForbidPlayerScore_Clear)
185 {
186         return 1;
187 }
188
189 MUTATOR_HOOKFUNCTION(arena_GiveFragsForKill)
190 {
191         if(arena_roundbased)
192                 frag_score = 0;
193         return 1;
194 }
195
196 MUTATOR_HOOKFUNCTION(arena_PlayerDies)
197 {
198         Spawnqueue_Unmark(self);
199         return 1;
200 }
201
202 MUTATOR_HOOKFUNCTION(arena_SV_StartFrame)
203 {
204         if(arena_roundbased) return 1;
205         if(time <= game_starttime) return 1;
206         if(gameover) return 1;
207
208         Arena_AddChallengers();
209         return 1;
210 }
211
212 void arena_Initialize()
213 {
214         maxspawned = max(2, autocvar_g_arena_maxspawned);
215         arena_roundbased = autocvar_g_arena_roundbased;
216         if(arena_roundbased)
217                 round_handler_Spawn(Arena_CheckPlayers, Arena_CheckWinner, 5, autocvar_g_arena_warmup, autocvar_g_arena_round_timelimit);
218 }
219
220 MUTATOR_DEFINITION(gamemode_arena)
221 {
222         MUTATOR_HOOK(ClientDisconnect, arena_ClientDisconnect, CBC_ORDER_ANY);
223         MUTATOR_HOOK(reset_map_players, arena_reset_map_players, CBC_ORDER_ANY);
224         MUTATOR_HOOK(MakePlayerObserver, arena_MakePlayerObserver, CBC_ORDER_ANY);
225         MUTATOR_HOOK(PutClientInServer, arena_PutClientInServer, CBC_ORDER_ANY);
226         MUTATOR_HOOK(PlayerSpawn, arena_PlayerSpawn, CBC_ORDER_ANY);
227         MUTATOR_HOOK(PlayerPreThink, arena_PlayerPreThink, CBC_ORDER_ANY);
228         MUTATOR_HOOK(ForbidPlayerScore_Clear, arena_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
229         MUTATOR_HOOK(GiveFragsForKill, arena_GiveFragsForKill, CBC_ORDER_ANY);
230         MUTATOR_HOOK(PlayerDies, arena_PlayerDies, CBC_ORDER_ANY);
231         MUTATOR_HOOK(SV_StartFrame, arena_SV_StartFrame, CBC_ORDER_ANY);
232
233         MUTATOR_ONADD
234         {
235                 if(time > 1) // game loads at time 1
236                         error("This is a game type and it cannot be added at runtime.");
237                 arena_Initialize();
238         }
239
240         MUTATOR_ONREMOVE
241         {
242                 error("This is a game type and it cannot be removed at runtime.");
243         }
244
245         return 0;
246 }