1 #include "gamemode_lms.qh"
3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
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;
13 float LMS_NewPlayerLives()
16 fl = autocvar_fraglimit;
20 // first player has left the game for dying too much? Nobody else can get in.
21 if(lms_lowest_lives < 1)
24 if(!autocvar_g_lms_join_anytime)
25 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
28 return bound(1, lms_lowest_lives, fl);
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
36 int WinningCondition_LMS()
38 entity first_player = NULL;
39 int total_players = 0;
40 FOREACH_CLIENT(IS_PLAYER(it), {
48 if (total_players > 1)
50 // two or more active players - continue with the game
54 // exactly one player?
57 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
59 if (LMS_NewPlayerLives())
61 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
67 // and assign him his first place
68 GameRules_scoring_add(first_player, LMS_RANK, 1);
78 // nobody is playing at all...
79 if (LMS_NewPlayerLives())
81 // wait for players...
85 // SNAFU (maybe a draw game?)
87 LOG_TRACE("No players, ending game.");
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);
97 if(WinningConditionHelper_winner)
98 WinningConditionHelper_winner.winning = true;
99 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
100 return WINNING_NEVER;
102 // Top two have different scores? Way to go for our beloved TIMELIMIT!
107 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
109 lms_lowest_lives = 999;
112 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
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);
122 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
124 entity player = M_ARGV(0, entity);
126 if(player.frags == FRAGS_SPECTATOR)
127 TRANSMUTE(Observer, player);
130 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
131 if(tl < lms_lowest_lives)
132 lms_lowest_lives = tl;
134 TRANSMUTE(Observer, player);
136 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
140 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
142 entity player = M_ARGV(0, entity);
146 if(player.frags == FRAGS_SPECTATOR)
148 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
150 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
156 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
158 entity frag_target = M_ARGV(2, entity);
160 frag_target.respawn_flags |= RESPAWN_FORCE;
163 void lms_RemovePlayer(entity player)
165 static int quitters = 0;
166 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
170 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
171 if (player.lms_spectate_warning != 2)
173 if(IS_BOT_CLIENT(player))
175 player.frags = FRAGS_LMS_LOSER;
176 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
180 lms_lowest_lives = 999;
181 FOREACH_CLIENT(true, {
182 if (it.frags == FRAGS_LMS_LOSER)
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;
189 else if (it.frags != FRAGS_SPECTATOR)
191 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
192 if(tl < lms_lowest_lives)
193 lms_lowest_lives = tl;
196 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
199 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
202 player.frags = FRAGS_LMS_LOSER;
203 TRANSMUTE(Observer, player);
205 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
206 lms_lowest_lives = 0; // end the game now!
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);
213 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
216 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
218 entity player = M_ARGV(0, entity);
220 lms_RemovePlayer(player);
223 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
225 entity player = M_ARGV(0, entity);
227 lms_RemovePlayer(player);
228 return true; // prevent team reset
231 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
233 entity player = M_ARGV(0, entity);
235 TRANSMUTE(Player, player);
236 campaign_bots_may_start = true;
238 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
240 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
241 player.frags = FRAGS_SPECTATOR;
245 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
247 entity player = M_ARGV(0, entity);
249 if(player.deadflag == DEAD_DYING)
250 player.deadflag = DEAD_RESPAWNING;
253 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
255 if(autocvar_g_lms_regenerate)
260 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
266 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
268 entity frag_target = M_ARGV(1, entity);
273 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
274 if(tl < lms_lowest_lives)
275 lms_lowest_lives = tl;
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);
286 M_ARGV(2, float) = 0; // frag score
291 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
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");
304 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
306 // don't clear player score
310 MUTATOR_HOOKFUNCTION(lms, FilterItem)
312 entity item = M_ARGV(0, entity);
314 if(autocvar_g_lms_extra_lives)
315 if(item.itemdef == ITEM_ExtraLife)
321 void lms_extralife(entity this)
323 StartItem(this, ITEM_ExtraLife);
326 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
328 if (!autocvar_g_powerups) return false;
329 if (!autocvar_g_lms_extra_lives) return false;
331 entity ent = M_ARGV(0, entity);
333 // Can't use .itemdef here
334 if (ent.classname != "item_health_mega") return false;
337 setthink(e, lms_extralife);
339 e.nextthink = time + 0.1;
340 e.spawnflags = ent.spawnflags;
341 e.noalign = ent.noalign;
342 setorigin(e, ent.origin);
347 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
349 entity item = M_ARGV(0, entity);
350 entity toucher = M_ARGV(1, entity);
352 if(item.itemdef == ITEM_ExtraLife)
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;
359 return MUT_ITEMTOUCH_CONTINUE;
362 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
364 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
365 ++M_ARGV(0, int); // activerealplayers
366 ++M_ARGV(1, int); // realplayers
372 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
374 entity player = M_ARGV(0, entity);
376 if(warmup_stage || player.lms_spectate_warning)
378 // for the forfeit message...
379 player.lms_spectate_warning = 2;
383 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
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");
388 return MUT_SPECCMD_RETURN;
390 return MUT_SPECCMD_CONTINUE;
393 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
395 M_ARGV(0, float) = WinningCondition_LMS();
399 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
401 M_ARGV(2, bool) = true; // all weapons
404 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
409 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
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
416 void lms_Initialize()
418 lms_lowest_lives = 9999;