]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
Simplify / clean up WinningCondition_LMS
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_lms.qc
1 #include "gamemode_lms.qh"
2
3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6
7 int autocvar_g_lms_extra_lives;
8 bool autocvar_g_lms_join_anytime;
9 int autocvar_g_lms_last_join;
10 bool autocvar_g_lms_regenerate;
11
12 // main functions
13 float LMS_NewPlayerLives()
14 {
15         float fl;
16         fl = autocvar_fraglimit;
17         if(fl == 0)
18                 fl = 999;
19
20         // first player has left the game for dying too much? Nobody else can get in.
21         if(lms_lowest_lives < 1)
22                 return 0;
23
24         if(!autocvar_g_lms_join_anytime)
25                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
26                         return 0;
27
28         return bound(1, lms_lowest_lives, fl);
29 }
30
31 void ClearWinners();
32
33 // LMS winning condition: game terminates if and only if there's at most one
34 // one player who's living lives. Top two scores being equal cancels the time
35 // limit.
36 int WinningCondition_LMS()
37 {
38         entity first_player = NULL;
39         int total_players = 0;
40         FOREACH_CLIENT(IS_PLAYER(it), {
41                 if (!total_players)
42                         first_player = it;
43                 ++total_players;
44         });
45
46         if (total_players)
47         {
48                 if (total_players > 1)
49                 {
50                         // two or more active players - continue with the game
51                 }
52                 else
53                 {
54                         // exactly one player?
55
56                         ClearWinners();
57                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
58
59                         if (LMS_NewPlayerLives())
60                         {
61                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
62                                 return WINNING_NO;
63                         }
64                         else
65                         {
66                                 // a winner!
67                                 // and assign him his first place
68                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
69                                 if(warmup_stage)
70                                         return WINNING_NO;
71                                 else
72                                         return WINNING_YES;
73                         }
74                 }
75         }
76         else
77         {
78                 // nobody is playing at all...
79                 if (LMS_NewPlayerLives())
80                 {
81                         // wait for players...
82                 }
83                 else
84                 {
85                         // SNAFU (maybe a draw game?)
86                         ClearWinners();
87                         LOG_TRACE("No players, ending game.");
88                         return WINNING_YES;
89                 }
90         }
91
92         // When we get here, we have at least two players who are actually LIVING,
93         // now check if the top two players have equal score.
94         WinningConditionHelper(NULL);
95
96         ClearWinners();
97         if(WinningConditionHelper_winner)
98                 WinningConditionHelper_winner.winning = true;
99         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
100                 return WINNING_NEVER;
101
102         // Top two have different scores? Way to go for our beloved TIMELIMIT!
103         return WINNING_NO;
104 }
105
106 // mutator hooks
107 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
108 {
109         lms_lowest_lives = 999;
110 }
111
112 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
113 {
114         FOREACH_CLIENT(true, {
115                 TRANSMUTE(Player, it);
116                 it.frags = FRAGS_PLAYER;
117                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
118                 PutClientInServer(it);
119         });
120 }
121
122 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
123 {
124         entity player = M_ARGV(0, entity);
125
126         if(player.frags == FRAGS_SPECTATOR)
127                 TRANSMUTE(Observer, player);
128         else
129         {
130                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
131                 if(tl < lms_lowest_lives)
132                         lms_lowest_lives = tl;
133                 if(tl <= 0)
134                         TRANSMUTE(Observer, player);
135                 if(warmup_stage)
136                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
137         }
138 }
139
140 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
141 {
142         entity player = M_ARGV(0, entity);
143
144         if(warmup_stage)
145                 return false;
146         if(player.frags == FRAGS_SPECTATOR)
147                 return true;
148         if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
149         {
150                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
151                 return true;
152         }
153         return false;
154 }
155
156 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
157 {
158         entity frag_target = M_ARGV(2, entity);
159
160         frag_target.respawn_flags |= RESPAWN_FORCE;
161 }
162
163 void lms_RemovePlayer(entity player)
164 {
165         static int quitters = 0;
166         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
167         if (!player_rank)
168         {
169                 int pl_cnt = 0;
170                 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
171                 if (player.lms_spectate_warning != 2)
172                 {
173                         if(IS_BOT_CLIENT(player))
174                                 bot_clear(player);
175                         player.frags = FRAGS_LMS_LOSER;
176                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
177                 }
178                 else
179                 {
180                         lms_lowest_lives = 999;
181                         FOREACH_CLIENT(true, {
182                                 if (it.frags == FRAGS_LMS_LOSER)
183                                 {
184                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
185                                         if (it_rank > player_rank && it_rank <= 256)
186                                                 GameRules_scoring_add(it, LMS_RANK, -1);
187                                         lms_lowest_lives = 0;
188                                 }
189                                 else if (it.frags != FRAGS_SPECTATOR)
190                                 {
191                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
192                                         if(tl < lms_lowest_lives)
193                                                 lms_lowest_lives = tl;
194                                 }
195                         });
196                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
197                         if(!warmup_stage)
198                         {
199                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
200                                 ++quitters;
201                         }
202                         player.frags = FRAGS_LMS_LOSER;
203                         TRANSMUTE(Observer, player);
204                 }
205                 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
206                         lms_lowest_lives = 0; // end the game now!
207         }
208
209         if(CS(player).killcount != FRAGS_SPECTATOR)
210                 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
211                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
212                 else
213                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
214 }
215
216 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
217 {
218         entity player = M_ARGV(0, entity);
219
220         lms_RemovePlayer(player);
221 }
222
223 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
224 {
225     entity player = M_ARGV(0, entity);
226
227         lms_RemovePlayer(player);
228         return true;  // prevent team reset
229 }
230
231 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
232 {
233         entity player = M_ARGV(0, entity);
234
235         TRANSMUTE(Player, player);
236         campaign_bots_may_start = true;
237
238         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
239         {
240                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
241                 player.frags = FRAGS_SPECTATOR;
242         }
243 }
244
245 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
246 {
247         entity player = M_ARGV(0, entity);
248
249         if(player.deadflag == DEAD_DYING)
250                 player.deadflag = DEAD_RESPAWNING;
251 }
252
253 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
254 {
255         if(autocvar_g_lms_regenerate)
256                 return false;
257         return true;
258 }
259
260 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
261 {
262         // forbode!
263         return true;
264 }
265
266 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
267 {
268         entity frag_target = M_ARGV(1, entity);
269
270         if (!warmup_stage)
271         {
272                 // remove a life
273                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
274                 if(tl < lms_lowest_lives)
275                         lms_lowest_lives = tl;
276                 if(tl <= 0)
277                 {
278                         int pl_cnt = 0;
279                         FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
280                         if(IS_BOT_CLIENT(frag_target))
281                                 bot_clear(frag_target);
282                         frag_target.frags = FRAGS_LMS_LOSER;
283                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
284                 }
285         }
286         M_ARGV(2, float) = 0; // frag score
287
288         return true;
289 }
290
291 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
292 {
293         start_items &= ~IT_UNLIMITED_AMMO;
294         start_health       = warmup_start_health       = cvar("g_lms_start_health");
295         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
296         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
297         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
298         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
299         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
300         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
301         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
302 }
303
304 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
305 {
306         // don't clear player score
307         return true;
308 }
309
310 MUTATOR_HOOKFUNCTION(lms, FilterItem)
311 {
312         entity item = M_ARGV(0, entity);
313
314         if(autocvar_g_lms_extra_lives)
315         if(item.itemdef == ITEM_ExtraLife)
316                 return false;
317
318         return true;
319 }
320
321 void lms_extralife(entity this)
322 {
323         StartItem(this, ITEM_ExtraLife);
324 }
325
326 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
327 {
328         if (!autocvar_g_powerups) return false;
329         if (!autocvar_g_lms_extra_lives) return false;
330
331         entity ent = M_ARGV(0, entity);
332
333         // Can't use .itemdef here
334         if (ent.classname != "item_health_mega") return false;
335
336         entity e = spawn();
337         setthink(e, lms_extralife);
338
339         e.nextthink = time + 0.1;
340         e.spawnflags = ent.spawnflags;
341         e.noalign = ent.noalign;
342         setorigin(e, ent.origin);
343
344         return true;
345 }
346
347 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
348 {
349         entity item = M_ARGV(0, entity);
350         entity toucher = M_ARGV(1, entity);
351
352         if(item.itemdef == ITEM_ExtraLife)
353         {
354                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
355                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
356                 return MUT_ITEMTOUCH_PICKUP;
357         }
358
359         return MUT_ITEMTOUCH_CONTINUE;
360 }
361
362 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
363 {
364         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
365                 ++M_ARGV(0, int); // activerealplayers
366                 ++M_ARGV(1, int); // realplayers
367         });
368
369         return true;
370 }
371
372 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
373 {
374     entity player = M_ARGV(0, entity);
375
376         if(warmup_stage || player.lms_spectate_warning)
377         {
378                 // for the forfeit message...
379                 player.lms_spectate_warning = 2;
380         }
381         else
382         {
383                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
384                 {
385                         player.lms_spectate_warning = 1;
386                         sprint(player, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
387                 }
388                 return MUT_SPECCMD_RETURN;
389         }
390         return MUT_SPECCMD_CONTINUE;
391 }
392
393 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
394 {
395         M_ARGV(0, float) = WinningCondition_LMS();
396         return true;
397 }
398
399 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
400 {
401         M_ARGV(2, bool) = true; // all weapons
402 }
403
404 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
405 {
406         return true;
407 }
408
409 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
410 {
411         if(game_stopped)
412         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
413                 return true; // allow writing to this field in intermission as it is needed for newly joining players
414 }
415
416 void lms_Initialize()
417 {
418         lms_lowest_lives = 9999;
419 }