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;
40 FOREACH_CLIENT(IS_PLAYER(it), {
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, ReadLevelCvars)
135 sv_ready_restart_after_countdown = 0;
138 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
140 entity player = M_ARGV(0, entity);
142 if(player.frags == FRAGS_SPECTATOR)
143 TRANSMUTE(Observer, player);
146 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
147 if(tl < lms_lowest_lives)
148 lms_lowest_lives = tl;
150 TRANSMUTE(Observer, player);
152 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
156 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
158 entity player = M_ARGV(0, entity);
162 if(player.frags == FRAGS_SPECTATOR)
164 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
166 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
172 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
174 entity frag_target = M_ARGV(2, entity);
176 frag_target.respawn_flags |= RESPAWN_FORCE;
179 void lms_RemovePlayer(entity player)
181 static int quitters = 0;
182 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
186 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
187 if (player.lms_spectate_warning != 2)
189 if(IS_BOT_CLIENT(player))
191 player.frags = FRAGS_LMS_LOSER;
192 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
196 lms_lowest_lives = 999;
197 FOREACH_CLIENT(true, {
198 if (it.frags == FRAGS_LMS_LOSER)
200 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
201 if (it_rank > player_rank && it_rank <= 256)
202 GameRules_scoring_add(it, LMS_RANK, -1);
203 lms_lowest_lives = 0;
205 else if (it.frags != FRAGS_SPECTATOR)
207 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
208 if(tl < lms_lowest_lives)
209 lms_lowest_lives = tl;
212 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
215 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
218 player.frags = FRAGS_LMS_LOSER;
219 TRANSMUTE(Observer, player);
221 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
222 lms_lowest_lives = 0; // end the game now!
225 if(CS(player).killcount != FRAGS_SPECTATOR)
226 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
227 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
229 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
232 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
234 entity player = M_ARGV(0, entity);
236 lms_RemovePlayer(player);
239 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
241 entity player = M_ARGV(0, entity);
243 if (!IS_PLAYER(player))
246 lms_RemovePlayer(player);
247 return true; // prevent team reset
250 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
252 entity player = M_ARGV(0, entity);
254 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
256 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
257 player.frags = FRAGS_SPECTATOR;
261 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
263 if(autocvar_g_campaign)
268 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
270 entity player = M_ARGV(0, entity);
272 if(player.deadflag == DEAD_DYING)
273 player.deadflag = DEAD_RESPAWNING;
276 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
278 if(autocvar_g_lms_regenerate)
283 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
289 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
291 entity frag_target = M_ARGV(1, entity);
296 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
297 if(tl < lms_lowest_lives)
298 lms_lowest_lives = tl;
302 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
303 if(IS_BOT_CLIENT(frag_target))
304 bot_clear(frag_target);
305 frag_target.frags = FRAGS_LMS_LOSER;
306 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
309 M_ARGV(2, float) = 0; // frag score
314 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
316 start_items &= ~IT_UNLIMITED_AMMO;
317 start_health = warmup_start_health = cvar("g_lms_start_health");
318 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
319 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
320 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
321 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
322 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
323 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
324 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
327 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
329 // don't clear player score
333 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
335 entity definition = M_ARGV(0, entity);
337 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
344 void lms_extralife(entity this)
346 StartItem(this, ITEM_ExtraLife);
349 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
351 if (!autocvar_g_powerups) return false;
352 if (!autocvar_g_lms_extra_lives) return false;
354 entity ent = M_ARGV(0, entity);
356 // Can't use .itemdef here
357 if (ent.classname != "item_health_mega") return false;
360 setthink(e, lms_extralife);
362 e.nextthink = time + 0.1;
363 e.spawnflags = ent.spawnflags;
364 e.noalign = ent.noalign;
365 setorigin(e, ent.origin);
370 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
372 entity item = M_ARGV(0, entity);
373 entity toucher = M_ARGV(1, entity);
375 if(item.itemdef == ITEM_ExtraLife)
377 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
378 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
379 return MUT_ITEMTOUCH_PICKUP;
382 return MUT_ITEMTOUCH_CONTINUE;
385 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
387 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
388 ++M_ARGV(0, int); // activerealplayers
389 ++M_ARGV(1, int); // realplayers
395 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
397 entity player = M_ARGV(0, entity);
399 if(warmup_stage || player.lms_spectate_warning)
401 // for the forfeit message...
402 player.lms_spectate_warning = 2;
406 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
408 player.lms_spectate_warning = 1;
409 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");
411 return MUT_SPECCMD_RETURN;
413 return MUT_SPECCMD_CONTINUE;
416 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
418 M_ARGV(0, float) = WinningCondition_LMS();
422 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
424 M_ARGV(2, bool) = true; // all weapons
427 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
432 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
435 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
436 return true; // allow writing to this field in intermission as it is needed for newly joining players
439 void lms_Initialize()
441 lms_lowest_lives = 9999;