3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6 #include <server/world.qh>
7 #include <server/items/items.qh>
9 int autocvar_g_lms_extra_lives;
10 bool autocvar_g_lms_join_anytime;
11 int autocvar_g_lms_last_join;
12 bool autocvar_g_lms_items;
13 bool autocvar_g_lms_regenerate;
14 bool autocvar_g_lms_rot;
17 int LMS_NewPlayerLives()
19 int fl = floor(autocvar_fraglimit);
20 if(fl == 0 || fl > 999)
23 // first player has left the game for dying too much? Nobody else can get in.
24 if(lms_lowest_lives < 1)
27 if(!autocvar_g_lms_join_anytime)
28 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
31 return bound(1, lms_lowest_lives, fl);
36 // LMS winning condition: game terminates if and only if there's at most one
37 // one player who's living lives. Top two scores being equal cancels the time
39 int WinningCondition_LMS()
41 if (warmup_stage || time <= game_starttime)
44 entity first_player = NULL;
46 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
56 // two or more active players - continue with the game
58 if (autocvar_g_campaign && campaign_bots_may_start)
60 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
61 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
63 return WINNING_YES; // human player lost, game over
70 // exactly one player?
73 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
75 if (LMS_NewPlayerLives())
77 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
83 // and assign him his first place
84 GameRules_scoring_add(first_player, LMS_RANK, 1);
91 // nobody is playing at all...
92 if (LMS_NewPlayerLives())
94 // wait for players...
98 // SNAFU (maybe a draw game?)
100 LOG_TRACE("No players, ending game.");
105 // When we get here, we have at least two players who are actually LIVING,
106 // now check if the top two players have equal score.
107 WinningConditionHelper(NULL);
110 if(WinningConditionHelper_winner)
111 WinningConditionHelper_winner.winning = true;
112 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
113 return WINNING_NEVER;
115 // Top two have different scores? Way to go for our beloved TIMELIMIT!
120 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
122 lms_lowest_lives = 999;
125 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
127 FOREACH_CLIENT(true, {
128 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
130 // players who forfeited (rank >= 256) become spectators
131 if (it.lms_spectate_warning == 2)
132 it.frags = FRAGS_SPECTATOR;
134 it.frags = FRAGS_PLAYER;
137 CS(it).killcount = 0;
138 INGAME_STATUS_CLEAR(it);
139 it.lms_spectate_warning = 0;
140 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
141 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
143 if (it.frags != FRAGS_PLAYER)
146 TRANSMUTE(Player, it);
147 PutClientInServer(it);
151 // FIXME add support for sv_ready_restart_after_countdown
152 // that is find a way to respawn/reset players IN GAME without setting lives to 0
153 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
156 sv_ready_restart_after_countdown = 0;
159 // returns true if player is added to the game
160 bool lms_AddPlayer(entity player)
164 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
167 if (time < game_starttime)
168 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
170 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
172 if (warmup_stage || time <= game_starttime)
174 if(player.lms_spectate_warning)
176 player.lms_spectate_warning = 0;
177 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
178 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
180 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
185 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
187 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
194 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
196 entity player = M_ARGV(0, entity);
197 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
199 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
200 TRANSMUTE(Observer, player);
204 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
206 entity player = M_ARGV(0, entity);
208 if (warmup_stage || time < game_starttime)
211 if (INGAME_JOINING(player))
213 // spawn player with the same amount of health / armor
214 // as the least healthy player with the least number of lives
215 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
216 float min_health = start_health;
217 float min_armorvalue = start_armorvalue;
218 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
219 if (GetResource(it, RES_HEALTH) < min_health)
220 min_health = GetResource(it, RES_HEALTH);
221 if (GetResource(it, RES_ARMOR) < min_armorvalue)
222 min_armorvalue = GetResource(it, RES_ARMOR);
224 if (min_health != start_health)
225 SetResource(player, RES_HEALTH, max(1, min_health));
226 if (min_armorvalue != start_armorvalue)
227 SetResource(player, RES_ARMOR, min_armorvalue);
228 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
232 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
234 entity player = M_ARGV(0, entity);
236 if (warmup_stage || lms_AddPlayer(player))
242 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
244 entity frag_target = M_ARGV(2, entity);
246 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
249 frag_target.respawn_flags = RESPAWN_SILENT;
250 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
251 frag_target.respawn_time = time + 2;
253 frag_target.respawn_flags |= RESPAWN_FORCE;
256 void lms_RemovePlayer(entity player)
258 if (warmup_stage || time < game_starttime)
261 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
264 if (player.lms_spectate_warning < 2)
266 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
268 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
271 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
273 else if (INGAME(player))
275 int min_forfeiter_rank = 665; // different from 666
276 FOREACH_CLIENT(it != player, {
277 // update rank of other players that were eliminated
278 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
280 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
281 if (it_rank > player_rank && it_rank <= 256)
282 GameRules_scoring_add(it, LMS_RANK, -1);
283 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
284 min_forfeiter_rank = it_rank - 1;
286 else if (it.frags != FRAGS_SPECTATOR)
288 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
289 if(tl < lms_lowest_lives)
290 lms_lowest_lives = tl;
293 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
295 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
296 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
297 TRANSMUTE(Observer, player);
301 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
303 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
304 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
306 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
310 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
312 entity player = M_ARGV(0, entity);
314 // no further message other than the disconnect message
315 player.lms_spectate_warning = 3;
317 lms_RemovePlayer(player);
318 INGAME_STATUS_CLEAR(player);
321 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
323 entity player = M_ARGV(0, entity);
324 bool is_forced = M_ARGV(1, bool);
326 if (!IS_PLAYER(player))
329 if (warmup_stage || time <= game_starttime)
331 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
332 player.frags = FRAGS_SPECTATOR;
333 TRANSMUTE(Observer, player);
334 INGAME_STATUS_CLEAR(player);
339 player.lms_spectate_warning = 2;
340 if (!GameRules_scoring_add(player, LMS_RANK, 0))
341 lms_RemovePlayer(player);
343 return true; // prevent team reset
346 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
348 entity player = M_ARGV(0, entity);
349 player.frags = FRAGS_SPECTATOR;
352 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
354 entity player = M_ARGV(0, entity);
356 if(player.deadflag == DEAD_DYING)
357 player.deadflag = DEAD_RESPAWNING;
360 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
362 if(!autocvar_g_lms_regenerate)
363 M_ARGV(2, float) = 0;
364 if(!autocvar_g_lms_rot)
365 M_ARGV(3, float) = 0;
366 return (!autocvar_g_lms_regenerate && !autocvar_g_lms_rot);
369 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
375 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
377 entity frag_target = M_ARGV(1, entity);
379 if (!warmup_stage && time > game_starttime)
382 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
383 if(tl < lms_lowest_lives)
384 lms_lowest_lives = tl;
388 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
391 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
392 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
395 M_ARGV(2, float) = 0; // frag score
400 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
402 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
403 if(!cvar("g_use_ammunition"))
404 start_items |= IT_UNLIMITED_AMMO;
406 start_health = warmup_start_health = cvar("g_lms_start_health");
407 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
408 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
409 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
410 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
411 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
412 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
413 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
416 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
418 // don't clear player score
422 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
424 if (autocvar_g_lms_items)
427 entity definition = M_ARGV(0, entity);
429 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
433 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
436 void lms_extralife(entity this)
438 StartItem(this, ITEM_ExtraLife);
441 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
443 if (MUTATOR_RETURNVALUE) return false;
444 if (!autocvar_g_powerups) return false;
445 if (!autocvar_g_lms_extra_lives) return false;
447 entity ent = M_ARGV(0, entity);
449 // Can't use .itemdef here
450 if (ent.classname != "item_health_mega") return false;
453 setthink(e, lms_extralife);
455 e.nextthink = time + 0.1;
456 e.spawnflags = ent.spawnflags;
457 e.noalign = ent.noalign;
458 setorigin(e, ent.origin);
463 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
465 if(MUTATOR_RETURNVALUE) return false;
467 entity item = M_ARGV(0, entity);
468 entity toucher = M_ARGV(1, entity);
470 if(item.itemdef == ITEM_ExtraLife)
472 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
473 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
474 return MUT_ITEMTOUCH_PICKUP;
477 return MUT_ITEMTOUCH_CONTINUE;
480 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
482 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
483 if (INGAME(it) && it.lms_spectate_warning < 2)
484 ++M_ARGV(0, int); // activerealplayers
485 ++M_ARGV(1, int); // realplayers
491 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
493 entity player = M_ARGV(0, entity);
495 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
497 // for the forfeit message...
498 player.lms_spectate_warning = 2;
502 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
504 player.lms_spectate_warning = 1;
505 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");
507 return MUT_SPECCMD_RETURN;
509 return MUT_SPECCMD_CONTINUE;
512 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
514 M_ARGV(0, float) = WinningCondition_LMS();
518 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
520 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
521 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
524 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
527 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
528 return true; // allow writing to this field in intermission as it is needed for newly joining players
531 void lms_Initialize()
533 lms_lowest_lives = 999;