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
52 if (autocvar_g_campaign)
54 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
55 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
57 return WINNING_YES; // human player lost, game over
64 // exactly one player?
67 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
69 if (LMS_NewPlayerLives())
71 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
77 // and assign him his first place
78 GameRules_scoring_add(first_player, LMS_RANK, 1);
88 // nobody is playing at all...
89 if (LMS_NewPlayerLives())
91 // wait for players...
95 // SNAFU (maybe a draw game?)
97 LOG_TRACE("No players, ending game.");
102 // When we get here, we have at least two players who are actually LIVING,
103 // now check if the top two players have equal score.
104 WinningConditionHelper(NULL);
107 if(WinningConditionHelper_winner)
108 WinningConditionHelper_winner.winning = true;
109 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
110 return WINNING_NEVER;
112 // Top two have different scores? Way to go for our beloved TIMELIMIT!
117 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
119 lms_lowest_lives = 999;
122 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
124 FOREACH_CLIENT(true, {
125 TRANSMUTE(Player, it);
126 it.frags = FRAGS_PLAYER;
127 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
128 PutClientInServer(it);
132 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
134 entity player = M_ARGV(0, entity);
136 if(player.frags == FRAGS_SPECTATOR)
137 TRANSMUTE(Observer, player);
140 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
141 if(tl < lms_lowest_lives)
142 lms_lowest_lives = tl;
144 TRANSMUTE(Observer, player);
146 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
150 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
152 entity player = M_ARGV(0, entity);
156 if(player.frags == FRAGS_SPECTATOR)
158 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
160 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
166 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
168 entity frag_target = M_ARGV(2, entity);
170 frag_target.respawn_flags |= RESPAWN_FORCE;
173 void lms_RemovePlayer(entity player)
175 static int quitters = 0;
176 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
180 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
181 if (player.lms_spectate_warning != 2)
183 if(IS_BOT_CLIENT(player))
185 player.frags = FRAGS_LMS_LOSER;
186 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
190 lms_lowest_lives = 999;
191 FOREACH_CLIENT(true, {
192 if (it.frags == FRAGS_LMS_LOSER)
194 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
195 if (it_rank > player_rank && it_rank <= 256)
196 GameRules_scoring_add(it, LMS_RANK, -1);
197 lms_lowest_lives = 0;
199 else if (it.frags != FRAGS_SPECTATOR)
201 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
202 if(tl < lms_lowest_lives)
203 lms_lowest_lives = tl;
206 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
209 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
212 player.frags = FRAGS_LMS_LOSER;
213 TRANSMUTE(Observer, player);
215 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
216 lms_lowest_lives = 0; // end the game now!
219 if(CS(player).killcount != FRAGS_SPECTATOR)
220 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
221 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
223 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
226 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
228 entity player = M_ARGV(0, entity);
230 lms_RemovePlayer(player);
233 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
235 entity player = M_ARGV(0, entity);
237 lms_RemovePlayer(player);
238 return true; // prevent team reset
241 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
243 entity player = M_ARGV(0, entity);
245 TRANSMUTE(Player, player);
246 campaign_bots_may_start = true;
248 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
250 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
251 player.frags = FRAGS_SPECTATOR;
255 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
257 entity player = M_ARGV(0, entity);
259 if(player.deadflag == DEAD_DYING)
260 player.deadflag = DEAD_RESPAWNING;
263 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
265 if(autocvar_g_lms_regenerate)
270 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
276 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
278 entity frag_target = M_ARGV(1, entity);
283 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
284 if(tl < lms_lowest_lives)
285 lms_lowest_lives = tl;
289 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
290 if(IS_BOT_CLIENT(frag_target))
291 bot_clear(frag_target);
292 frag_target.frags = FRAGS_LMS_LOSER;
293 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
296 M_ARGV(2, float) = 0; // frag score
301 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
303 start_items &= ~IT_UNLIMITED_AMMO;
304 start_health = warmup_start_health = cvar("g_lms_start_health");
305 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
306 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
307 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
308 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
309 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
310 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
311 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
314 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
316 // don't clear player score
320 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
322 entity definition = M_ARGV(0, entity);
324 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
331 void lms_extralife(entity this)
333 StartItem(this, ITEM_ExtraLife);
336 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
338 if (!autocvar_g_powerups) return false;
339 if (!autocvar_g_lms_extra_lives) return false;
341 entity ent = M_ARGV(0, entity);
343 // Can't use .itemdef here
344 if (ent.classname != "item_health_mega") return false;
347 setthink(e, lms_extralife);
349 e.nextthink = time + 0.1;
350 e.spawnflags = ent.spawnflags;
351 e.noalign = ent.noalign;
352 setorigin(e, ent.origin);
357 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
359 entity item = M_ARGV(0, entity);
360 entity toucher = M_ARGV(1, entity);
362 if(item.itemdef == ITEM_ExtraLife)
364 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
365 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
366 return MUT_ITEMTOUCH_PICKUP;
369 return MUT_ITEMTOUCH_CONTINUE;
372 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
374 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
375 ++M_ARGV(0, int); // activerealplayers
376 ++M_ARGV(1, int); // realplayers
382 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
384 entity player = M_ARGV(0, entity);
386 if(warmup_stage || player.lms_spectate_warning)
388 // for the forfeit message...
389 player.lms_spectate_warning = 2;
393 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
395 player.lms_spectate_warning = 1;
396 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");
398 return MUT_SPECCMD_RETURN;
400 return MUT_SPECCMD_CONTINUE;
403 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
405 M_ARGV(0, float) = WinningCondition_LMS();
409 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
411 M_ARGV(2, bool) = true; // all weapons
414 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
419 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
422 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
423 return true; // allow writing to this field in intermission as it is needed for newly joining players
426 void lms_Initialize()
428 lms_lowest_lives = 9999;