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;
16 int LMS_NewPlayerLives()
18 int fl = floor(autocvar_fraglimit);
19 if(fl == 0 || fl > 999)
22 // first player has left the game for dying too much? Nobody else can get in.
23 if(lms_lowest_lives < 1)
26 if(!autocvar_g_lms_join_anytime)
27 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
30 return bound(1, lms_lowest_lives, fl);
35 // LMS winning condition: game terminates if and only if there's at most one
36 // one player who's living lives. Top two scores being equal cancels the time
38 int WinningCondition_LMS()
40 if (warmup_stage || time <= game_starttime)
43 entity first_player = NULL;
45 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
55 // two or more active players - continue with the game
57 if (autocvar_g_campaign)
59 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
60 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
62 return WINNING_YES; // human player lost, game over
69 // exactly one player?
72 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
74 if (LMS_NewPlayerLives())
76 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
82 // and assign him his first place
83 GameRules_scoring_add(first_player, LMS_RANK, 1);
90 // nobody is playing at all...
91 if (LMS_NewPlayerLives())
93 // wait for players...
97 // SNAFU (maybe a draw game?)
99 LOG_TRACE("No players, ending game.");
104 // When we get here, we have at least two players who are actually LIVING,
105 // now check if the top two players have equal score.
106 WinningConditionHelper(NULL);
109 if(WinningConditionHelper_winner)
110 WinningConditionHelper_winner.winning = true;
111 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
112 return WINNING_NEVER;
114 // Top two have different scores? Way to go for our beloved TIMELIMIT!
119 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
121 lms_lowest_lives = 999;
124 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
126 FOREACH_CLIENT(true, {
127 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
129 // players who forfeited (rank >= 256) become spectators
130 if (it.lms_spectate_warning == 2)
131 it.frags = FRAGS_SPECTATOR;
133 it.frags = FRAGS_PLAYER;
136 CS(it).killcount = 0;
137 INGAME_STATUS_CLEAR(it);
138 it.lms_spectate_warning = 0;
139 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
140 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
142 if (it.frags != FRAGS_PLAYER)
145 TRANSMUTE(Player, it);
146 PutClientInServer(it);
150 // FIXME add support for sv_ready_restart_after_countdown
151 // that is find a way to respawn/reset players IN GAME without setting lives to 0
152 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
155 sv_ready_restart_after_countdown = 0;
158 // returns true if player is added to the game
159 bool lms_AddPlayer(entity player)
163 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
166 if (time < game_starttime)
167 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
169 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
171 if (warmup_stage || time <= game_starttime)
173 if(player.lms_spectate_warning)
175 player.lms_spectate_warning = 0;
176 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
177 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
179 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
184 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
186 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
193 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
195 entity player = M_ARGV(0, entity);
196 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
198 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
199 TRANSMUTE(Observer, player);
203 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
205 entity player = M_ARGV(0, entity);
207 if (warmup_stage || time < game_starttime)
210 if (INGAME_JOINING(player))
212 // spawn player with the same amount of health / armor
213 // as the least healthy player with the least number of lives
214 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
215 float min_health = start_health;
216 float min_armorvalue = start_armorvalue;
217 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
218 if (GetResource(it, RES_HEALTH) < min_health)
219 min_health = GetResource(it, RES_HEALTH);
220 if (GetResource(it, RES_ARMOR) < min_armorvalue)
221 min_armorvalue = GetResource(it, RES_ARMOR);
223 if (min_health != start_health)
224 SetResource(player, RES_HEALTH, max(1, min_health));
225 if (min_armorvalue != start_armorvalue)
226 SetResource(player, RES_ARMOR, min_armorvalue);
227 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
231 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
233 entity player = M_ARGV(0, entity);
235 if (warmup_stage || lms_AddPlayer(player))
241 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
243 entity frag_target = M_ARGV(2, entity);
245 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
248 frag_target.respawn_flags = RESPAWN_SILENT;
249 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
250 frag_target.respawn_time = time + 2;
252 frag_target.respawn_flags |= RESPAWN_FORCE;
255 void lms_RemovePlayer(entity player)
257 if (warmup_stage || time < game_starttime)
260 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
263 if (player.lms_spectate_warning < 2)
265 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
267 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
270 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
272 else if (INGAME(player))
274 int min_forfeiter_rank = 665; // different from 666
275 FOREACH_CLIENT(it != player, {
276 // update rank of other players that were eliminated
277 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
279 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
280 if (it_rank > player_rank && it_rank <= 256)
281 GameRules_scoring_add(it, LMS_RANK, -1);
282 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
283 min_forfeiter_rank = it_rank - 1;
285 else if (it.frags != FRAGS_SPECTATOR)
287 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
288 if(tl < lms_lowest_lives)
289 lms_lowest_lives = tl;
292 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
294 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
295 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
296 TRANSMUTE(Observer, player);
300 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
302 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
303 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
305 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
309 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
311 entity player = M_ARGV(0, entity);
313 // no further message other than the disconnect message
314 player.lms_spectate_warning = 3;
316 lms_RemovePlayer(player);
317 INGAME_STATUS_CLEAR(player);
320 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
322 entity player = M_ARGV(0, entity);
323 bool is_forced = M_ARGV(1, bool);
325 if (!IS_PLAYER(player))
328 if (warmup_stage || time <= game_starttime)
330 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
331 player.frags = FRAGS_SPECTATOR;
332 TRANSMUTE(Observer, player);
333 INGAME_STATUS_CLEAR(player);
338 player.lms_spectate_warning = 2;
339 if (!GameRules_scoring_add(player, LMS_RANK, 0))
340 lms_RemovePlayer(player);
342 return true; // prevent team reset
345 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
347 entity player = M_ARGV(0, entity);
348 player.frags = FRAGS_SPECTATOR;
351 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
353 entity player = M_ARGV(0, entity);
355 if(player.deadflag == DEAD_DYING)
356 player.deadflag = DEAD_RESPAWNING;
359 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
361 if(autocvar_g_lms_regenerate)
366 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
372 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
374 entity frag_target = M_ARGV(1, entity);
376 if (!warmup_stage && time > game_starttime)
379 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
380 if(tl < lms_lowest_lives)
381 lms_lowest_lives = tl;
385 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
388 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
389 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
392 M_ARGV(2, float) = 0; // frag score
397 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
399 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
400 if(!cvar("g_use_ammunition"))
401 start_items |= IT_UNLIMITED_AMMO;
403 start_health = warmup_start_health = cvar("g_lms_start_health");
404 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
405 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
406 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
407 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
408 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
409 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
410 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
413 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
415 // don't clear player score
419 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
421 if (autocvar_g_lms_items)
424 entity definition = M_ARGV(0, entity);
426 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
430 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
433 void lms_extralife(entity this)
435 StartItem(this, ITEM_ExtraLife);
438 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
440 if (MUTATOR_RETURNVALUE) return false;
441 if (!autocvar_g_powerups) return false;
442 if (!autocvar_g_lms_extra_lives) return false;
444 entity ent = M_ARGV(0, entity);
446 // Can't use .itemdef here
447 if (ent.classname != "item_health_mega") return false;
450 setthink(e, lms_extralife);
452 e.nextthink = time + 0.1;
453 e.spawnflags = ent.spawnflags;
454 e.noalign = ent.noalign;
455 setorigin(e, ent.origin);
460 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
462 if(MUTATOR_RETURNVALUE) return false;
464 entity item = M_ARGV(0, entity);
465 entity toucher = M_ARGV(1, entity);
467 if(item.itemdef == ITEM_ExtraLife)
469 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
470 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
471 return MUT_ITEMTOUCH_PICKUP;
474 return MUT_ITEMTOUCH_CONTINUE;
477 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
479 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
480 if (INGAME(it) && it.lms_spectate_warning < 2)
481 ++M_ARGV(0, int); // activerealplayers
482 ++M_ARGV(1, int); // realplayers
488 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
490 entity player = M_ARGV(0, entity);
492 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
494 // for the forfeit message...
495 player.lms_spectate_warning = 2;
499 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
501 player.lms_spectate_warning = 1;
502 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");
504 return MUT_SPECCMD_RETURN;
506 return MUT_SPECCMD_CONTINUE;
509 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
511 M_ARGV(0, float) = WinningCondition_LMS();
515 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
517 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
518 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
521 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
524 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
525 return true; // allow writing to this field in intermission as it is needed for newly joining players
528 void lms_Initialize()
530 lms_lowest_lives = 999;