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;
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)
161 if (!player.lmsplayer)
163 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
166 player.lmsplayer = 2; // temp value indicating player has just joined the game (but not spawned yet)
168 if (warmup_stage || time <= game_starttime)
170 if(player.lms_spectate_warning)
172 player.lms_spectate_warning = 0;
173 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
174 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
176 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
181 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
183 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
190 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
192 entity player = M_ARGV(0, entity);
193 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
195 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
196 TRANSMUTE(Observer, player);
200 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
202 entity player = M_ARGV(0, entity);
204 if (warmup_stage || time < game_starttime)
207 if (player.lmsplayer == 2) // just joined the game
209 // spawn player with the same amount of health / armor
210 // as the least healthy player with the least number of lives
211 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
212 float min_health = start_health;
213 float min_armorvalue = start_armorvalue;
214 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
215 if (GetResource(it, RES_HEALTH) < min_health)
216 min_health = GetResource(it, RES_HEALTH);
217 if (GetResource(it, RES_ARMOR) < min_armorvalue)
218 min_armorvalue = GetResource(it, RES_ARMOR);
220 if (min_health != start_health)
221 SetResource(player, RES_HEALTH, max(1, min_health));
222 if (min_armorvalue != start_armorvalue)
223 SetResource(player, RES_ARMOR, min_armorvalue);
224 player.lmsplayer = 1;
228 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
230 entity player = M_ARGV(0, entity);
232 if (warmup_stage || lms_AddPlayer(player))
238 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
240 entity frag_target = M_ARGV(2, entity);
242 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
245 frag_target.respawn_flags = RESPAWN_SILENT;
246 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
247 frag_target.respawn_time = time + 2;
249 frag_target.respawn_flags |= RESPAWN_FORCE;
252 void lms_RemovePlayer(entity player)
254 if (warmup_stage || time < game_starttime)
257 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
260 if (player.lms_spectate_warning < 2)
262 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
264 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
267 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
271 int min_forfeiter_rank = 665; // different from 666
272 FOREACH_CLIENT(true, {
273 // update rank of other players that were eliminated
274 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
276 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
277 if (it_rank > player_rank && it_rank <= 256)
278 GameRules_scoring_add(it, LMS_RANK, -1);
279 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
280 min_forfeiter_rank = it_rank - 1;
282 else if (it.frags != FRAGS_SPECTATOR)
284 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
285 if(tl < lms_lowest_lives)
286 lms_lowest_lives = tl;
289 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
291 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
292 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
293 TRANSMUTE(Observer, player);
297 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
299 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
300 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
302 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
306 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
308 entity player = M_ARGV(0, entity);
310 // no further message other than the disconnect message
311 player.lms_spectate_warning = 3;
313 lms_RemovePlayer(player);
314 player.lmsplayer = 0;
317 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
319 entity player = M_ARGV(0, entity);
320 bool is_forced = M_ARGV(1, bool);
322 if (!IS_PLAYER(player))
325 if (warmup_stage || time <= game_starttime)
327 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
328 player.frags = FRAGS_SPECTATOR;
329 TRANSMUTE(Observer, player);
330 player.lmsplayer = 0;
335 player.lms_spectate_warning = 2;
336 if (!GameRules_scoring_add(player, LMS_RANK, 0))
337 lms_RemovePlayer(player);
339 return true; // prevent team reset
342 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
344 entity player = M_ARGV(0, entity);
345 player.frags = FRAGS_SPECTATOR;
348 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
350 entity player = M_ARGV(0, entity);
352 if(player.deadflag == DEAD_DYING)
353 player.deadflag = DEAD_RESPAWNING;
356 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
358 if(autocvar_g_lms_regenerate)
363 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
369 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
371 entity frag_target = M_ARGV(1, entity);
373 if (!warmup_stage && time > game_starttime)
376 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
377 if(tl < lms_lowest_lives)
378 lms_lowest_lives = tl;
382 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
385 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
386 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
389 M_ARGV(2, float) = 0; // frag score
394 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
396 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
397 if(!cvar("g_use_ammunition"))
398 start_items |= IT_UNLIMITED_AMMO;
400 start_health = warmup_start_health = cvar("g_lms_start_health");
401 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
402 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
403 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
404 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
405 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
406 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
407 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
410 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
412 // don't clear player score
416 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
418 if (autocvar_g_lms_items)
421 entity definition = M_ARGV(0, entity);
423 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
427 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
430 void lms_extralife(entity this)
432 StartItem(this, ITEM_ExtraLife);
435 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
437 if (MUTATOR_RETURNVALUE) return false;
438 if (!autocvar_g_powerups) return false;
439 if (!autocvar_g_lms_extra_lives) return false;
441 entity ent = M_ARGV(0, entity);
443 // Can't use .itemdef here
444 if (ent.classname != "item_health_mega") return false;
447 setthink(e, lms_extralife);
449 e.nextthink = time + 0.1;
450 e.spawnflags = ent.spawnflags;
451 e.noalign = ent.noalign;
452 setorigin(e, ent.origin);
457 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
459 if(MUTATOR_RETURNVALUE) return false;
461 entity item = M_ARGV(0, entity);
462 entity toucher = M_ARGV(1, entity);
464 if(item.itemdef == ITEM_ExtraLife)
466 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
467 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
468 return MUT_ITEMTOUCH_PICKUP;
471 return MUT_ITEMTOUCH_CONTINUE;
474 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
476 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
477 if (it.lmsplayer && it.lms_spectate_warning < 2)
478 ++M_ARGV(0, int); // activerealplayers
479 ++M_ARGV(1, int); // realplayers
485 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
487 entity player = M_ARGV(0, entity);
489 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
491 // for the forfeit message...
492 player.lms_spectate_warning = 2;
496 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
498 player.lms_spectate_warning = 1;
499 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");
501 return MUT_SPECCMD_RETURN;
503 return MUT_SPECCMD_CONTINUE;
506 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
508 M_ARGV(0, float) = WinningCondition_LMS();
512 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
514 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
515 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
518 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
520 entity player = M_ARGV(0, entity);
522 return boolean(player.lmsplayer);
525 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
528 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
529 return true; // allow writing to this field in intermission as it is needed for newly joining players
532 void lms_Initialize()
534 lms_lowest_lives = 999;