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