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 float autocvar_g_lms_forfeit_min_match_time;
11 bool autocvar_g_lms_join_anytime;
12 int autocvar_g_lms_last_join;
13 bool autocvar_g_lms_items;
14 bool autocvar_g_lms_regenerate;
15 bool autocvar_g_lms_rot;
18 int LMS_NewPlayerLives()
20 int fl = floor(autocvar_fraglimit);
21 if(fl == 0 || fl > 999)
24 // first player has left the game for dying too much? Nobody else can get in.
25 if(lms_lowest_lives < 1)
28 if(!autocvar_g_lms_join_anytime)
29 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
32 return bound(1, lms_lowest_lives, fl);
37 // LMS winning condition: game terminates if and only if there's at most one
38 // one player who's living lives. Top two scores being equal cancels the time
40 int WinningCondition_LMS()
42 if (warmup_stage || time <= game_starttime)
45 entity first_player = NULL;
48 FOREACH_CLIENT(true, {
49 if (IS_PLAYER(it) && it.frags == FRAGS_PLAYER)
55 else if (GameRules_scoring_add(it, LMS_RANK, 0))
63 // two or more active players - continue with the game
65 if (autocvar_g_campaign && campaign_bots_may_start)
67 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
68 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
70 return WINNING_YES; // human player lost, game over
77 // exactly one player?
80 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
82 if (LMS_NewPlayerLives())
84 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
86 GameRules_scoring_add(first_player, LMS_RANK, 1);
89 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
95 // and assign him his first place
96 GameRules_scoring_add(first_player, LMS_RANK, 1);
103 // nobody is playing at all...
104 if (LMS_NewPlayerLives())
106 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
111 // wait for players...
115 // SNAFU (maybe a draw game?)
117 LOG_TRACE("No players, ending game.");
122 // When we get here, we have at least two players who are actually LIVING,
123 // now check if the top two players have equal score.
124 WinningConditionHelper(NULL);
127 if(WinningConditionHelper_winner)
128 WinningConditionHelper_winner.winning = true;
129 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
130 return WINNING_NEVER;
132 // Top two have different scores? Way to go for our beloved TIMELIMIT!
137 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
139 lms_lowest_lives = 999;
142 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
144 FOREACH_CLIENT(true, {
145 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
147 // players who forfeited (rank >= 256) become spectators
148 if (it.lms_spectate_warning == 2)
149 it.frags = FRAGS_SPECTATOR;
151 it.frags = FRAGS_PLAYER;
154 CS(it).killcount = 0;
155 INGAME_STATUS_CLEAR(it);
156 it.lms_spectate_warning = 0;
157 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
158 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
160 if (it.frags != FRAGS_PLAYER)
163 TRANSMUTE(Player, it);
164 PutClientInServer(it);
168 // FIXME add support for sv_ready_restart_after_countdown
169 // that is find a way to respawn/reset players IN GAME without setting lives to 0
170 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
173 sv_ready_restart_after_countdown = 0;
176 // returns true if player is added to the game
177 bool lms_AddPlayer(entity player)
181 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
184 if (time < game_starttime)
185 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
187 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
189 if (warmup_stage || time <= game_starttime)
191 if(player.lms_spectate_warning)
193 player.lms_spectate_warning = 0;
194 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
195 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
197 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
202 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
204 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
211 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
213 entity player = M_ARGV(0, entity);
214 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
216 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
217 TRANSMUTE(Observer, player);
221 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
223 entity player = M_ARGV(0, entity);
225 if (warmup_stage || time < game_starttime)
228 if (INGAME_JOINING(player))
230 // spawn player with the same amount of health / armor
231 // as the least healthy player with the least number of lives
232 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
233 float min_health = start_health;
234 float min_armorvalue = start_armorvalue;
235 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
236 if (GetResource(it, RES_HEALTH) < min_health)
237 min_health = GetResource(it, RES_HEALTH);
238 if (GetResource(it, RES_ARMOR) < min_armorvalue)
239 min_armorvalue = GetResource(it, RES_ARMOR);
241 if (min_health != start_health)
242 SetResource(player, RES_HEALTH, max(1, min_health));
243 if (min_armorvalue != start_armorvalue)
244 SetResource(player, RES_ARMOR, min_armorvalue);
245 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
249 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
251 entity player = M_ARGV(0, entity);
253 if (warmup_stage || lms_AddPlayer(player))
259 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
261 entity frag_target = M_ARGV(2, entity);
263 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
266 frag_target.respawn_flags = RESPAWN_SILENT;
267 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
268 frag_target.respawn_time = time + 2;
270 frag_target.respawn_flags |= RESPAWN_FORCE;
273 void lms_RemovePlayer(entity player)
275 if (warmup_stage || time < game_starttime)
278 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
281 if (player.lms_spectate_warning < 2)
283 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
285 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
288 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
290 else if (INGAME(player))
292 int min_forfeiter_rank = 665; // different from 666
293 FOREACH_CLIENT(it != player, {
294 // update rank of other players that were eliminated
295 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
297 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
298 if (it_rank > player_rank && it_rank <= 256)
299 GameRules_scoring_add(it, LMS_RANK, -1);
300 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
301 min_forfeiter_rank = it_rank - 1;
303 else if (it.frags != FRAGS_SPECTATOR)
305 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
306 if(tl < lms_lowest_lives)
307 lms_lowest_lives = tl;
310 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
312 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
313 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
314 TRANSMUTE(Observer, player);
318 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
320 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
321 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
323 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
327 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
329 entity player = M_ARGV(0, entity);
331 // no further message other than the disconnect message
332 player.lms_spectate_warning = 3;
334 lms_RemovePlayer(player);
335 INGAME_STATUS_CLEAR(player);
338 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
340 entity player = M_ARGV(0, entity);
341 bool is_forced = M_ARGV(1, bool);
343 if (!IS_PLAYER(player))
346 if (warmup_stage || time <= game_starttime)
348 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
349 player.frags = FRAGS_SPECTATOR;
350 TRANSMUTE(Observer, player);
351 INGAME_STATUS_CLEAR(player);
356 player.lms_spectate_warning = 2;
357 if (!GameRules_scoring_add(player, LMS_RANK, 0))
358 lms_RemovePlayer(player);
360 return true; // prevent team reset
363 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
365 entity player = M_ARGV(0, entity);
366 player.frags = FRAGS_SPECTATOR;
369 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
371 entity player = M_ARGV(0, entity);
373 if(player.deadflag == DEAD_DYING)
374 player.deadflag = DEAD_RESPAWNING;
377 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
379 if(!autocvar_g_lms_regenerate)
380 M_ARGV(2, float) = 0;
381 if(!autocvar_g_lms_rot)
382 M_ARGV(3, float) = 0;
383 return (!autocvar_g_lms_regenerate && !autocvar_g_lms_rot);
386 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
392 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
394 entity frag_target = M_ARGV(1, entity);
396 if (!warmup_stage && time > game_starttime)
399 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
400 if(tl < lms_lowest_lives)
401 lms_lowest_lives = tl;
405 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
408 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
409 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
412 M_ARGV(2, float) = 0; // frag score
417 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
419 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
420 if(!cvar("g_use_ammunition"))
421 start_items |= IT_UNLIMITED_AMMO;
423 start_health = warmup_start_health = cvar("g_lms_start_health");
424 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
425 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
426 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
427 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
428 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
429 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
430 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
433 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
435 // don't clear player score
439 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
441 if (autocvar_g_lms_items)
444 entity definition = M_ARGV(0, entity);
446 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
450 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
453 void lms_extralife(entity this)
455 StartItem(this, ITEM_ExtraLife);
458 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
460 if (MUTATOR_RETURNVALUE) return false;
461 if (!autocvar_g_powerups) return false;
462 if (!autocvar_g_lms_extra_lives) return false;
464 entity ent = M_ARGV(0, entity);
466 // Can't use .itemdef here
467 if (ent.classname != "item_health_mega") return false;
470 setthink(e, lms_extralife);
472 e.nextthink = time + 0.1;
473 e.spawnflags = ent.spawnflags;
474 e.noalign = ent.noalign;
475 setorigin(e, ent.origin);
480 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
482 if(MUTATOR_RETURNVALUE) return false;
484 entity item = M_ARGV(0, entity);
485 entity toucher = M_ARGV(1, entity);
487 if(item.itemdef == ITEM_ExtraLife)
489 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
490 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
491 return MUT_ITEMTOUCH_PICKUP;
494 return MUT_ITEMTOUCH_CONTINUE;
497 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
499 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
500 if (INGAME(it) && it.lms_spectate_warning < 2)
501 ++M_ARGV(0, int); // activerealplayers
502 ++M_ARGV(1, int); // realplayers
508 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
510 entity player = M_ARGV(0, entity);
512 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
514 // for the forfeit message...
515 player.lms_spectate_warning = 2;
519 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
521 player.lms_spectate_warning = 1;
522 sprint(player, "^1WARNING:^7 you can't rejoin this match after spectating. Use the same command again to spectate anyway.\n");
523 Send_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CENTER_LMS_SPECWARN);
525 return MUT_SPECCMD_RETURN;
527 return MUT_SPECCMD_CONTINUE;
530 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
532 M_ARGV(0, float) = WinningCondition_LMS();
536 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
538 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
539 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
542 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
545 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
546 return true; // allow writing to this field in intermission as it is needed for newly joining players
549 void lms_Initialize()
551 lms_lowest_lives = 999;