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?
81 if (LMS_NewPlayerLives())
83 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
85 GameRules_scoring_add(first_player, LMS_RANK, 1);
86 first_player.winning = 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);
97 first_player.winning = 1;
104 // nobody is playing at all...
105 if (LMS_NewPlayerLives())
107 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
112 // wait for players...
116 // SNAFU (maybe a draw game?)
118 LOG_TRACE("No players, ending game.");
123 // When we get here, we have at least two players who are actually LIVING,
124 // now check if the top two players have equal score.
125 WinningConditionHelper(NULL);
128 if(WinningConditionHelper_winner)
129 WinningConditionHelper_winner.winning = true;
130 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
131 return WINNING_NEVER;
133 // Top two have different scores? Way to go for our beloved TIMELIMIT!
138 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
140 lms_lowest_lives = 999;
143 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
145 FOREACH_CLIENT(true, {
146 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
148 // players who forfeited (rank >= 256) become spectators
149 if (it.lms_spectate_warning == 2)
150 it.frags = FRAGS_SPECTATOR;
152 it.frags = FRAGS_PLAYER;
155 CS(it).killcount = 0;
156 INGAME_STATUS_CLEAR(it);
157 it.lms_spectate_warning = 0;
158 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
159 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
161 if (it.frags != FRAGS_PLAYER)
164 TRANSMUTE(Player, it);
165 PutClientInServer(it);
169 // FIXME add support for sv_ready_restart_after_countdown
170 // that is find a way to respawn/reset players IN GAME without setting lives to 0
171 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
174 sv_ready_restart_after_countdown = 0;
177 // returns true if player is added to the game
178 bool lms_AddPlayer(entity player)
182 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
185 if (time < game_starttime)
186 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
188 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
190 if (warmup_stage || time <= game_starttime)
192 if(player.lms_spectate_warning)
194 player.lms_spectate_warning = 0;
195 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
196 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
198 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
203 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
205 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
212 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
214 entity player = M_ARGV(0, entity);
215 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
217 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
218 TRANSMUTE(Observer, player);
222 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
224 entity player = M_ARGV(0, entity);
226 if (warmup_stage || time < game_starttime)
229 if (INGAME_JOINING(player))
231 // spawn player with the same amount of health / armor
232 // as the least healthy player with the least number of lives
233 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
234 float min_health = start_health;
235 float min_armorvalue = start_armorvalue;
236 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
237 if (GetResource(it, RES_HEALTH) < min_health)
238 min_health = GetResource(it, RES_HEALTH);
239 if (GetResource(it, RES_ARMOR) < min_armorvalue)
240 min_armorvalue = GetResource(it, RES_ARMOR);
242 if (min_health != start_health)
243 SetResource(player, RES_HEALTH, max(1, min_health));
244 if (min_armorvalue != start_armorvalue)
245 SetResource(player, RES_ARMOR, min_armorvalue);
246 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
250 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
252 entity player = M_ARGV(0, entity);
254 if (warmup_stage || lms_AddPlayer(player))
260 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
262 entity frag_target = M_ARGV(2, entity);
264 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
267 frag_target.respawn_flags = RESPAWN_SILENT;
268 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
269 frag_target.respawn_time = time + 2;
271 frag_target.respawn_flags |= RESPAWN_FORCE;
274 void lms_RemovePlayer(entity player)
276 if (warmup_stage || time < game_starttime)
279 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
282 if (player.lms_spectate_warning < 2)
284 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
286 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
289 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
291 else if (INGAME(player))
293 int min_forfeiter_rank = 665; // different from 666
294 FOREACH_CLIENT(it != player, {
295 // update rank of other players that were eliminated
296 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
298 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
299 if (it_rank > player_rank && it_rank <= 256)
300 GameRules_scoring_add(it, LMS_RANK, -1);
301 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
302 min_forfeiter_rank = it_rank - 1;
304 else if (it.frags != FRAGS_SPECTATOR)
306 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
307 if(tl < lms_lowest_lives)
308 lms_lowest_lives = tl;
311 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
313 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
314 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
315 TRANSMUTE(Observer, player);
319 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
321 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
322 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
324 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
328 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
330 entity player = M_ARGV(0, entity);
332 // no further message other than the disconnect message
333 player.lms_spectate_warning = 3;
335 lms_RemovePlayer(player);
336 INGAME_STATUS_CLEAR(player);
339 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
341 entity player = M_ARGV(0, entity);
342 bool is_forced = M_ARGV(1, bool);
344 if (!IS_PLAYER(player))
347 if (warmup_stage || time <= game_starttime)
349 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
350 player.frags = FRAGS_SPECTATOR;
351 TRANSMUTE(Observer, player);
352 INGAME_STATUS_CLEAR(player);
357 player.lms_spectate_warning = 2;
358 if (!GameRules_scoring_add(player, LMS_RANK, 0))
359 lms_RemovePlayer(player);
361 return true; // prevent team reset
364 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
366 entity player = M_ARGV(0, entity);
367 player.frags = FRAGS_SPECTATOR;
370 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
372 entity player = M_ARGV(0, entity);
374 if(player.deadflag == DEAD_DYING)
375 player.deadflag = DEAD_RESPAWNING;
378 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
380 if(!autocvar_g_lms_regenerate)
381 M_ARGV(2, float) = 0;
382 if(!autocvar_g_lms_rot)
383 M_ARGV(3, float) = 0;
384 return (!autocvar_g_lms_regenerate && !autocvar_g_lms_rot);
387 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
393 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
395 entity frag_target = M_ARGV(1, entity);
397 if (!warmup_stage && time > game_starttime)
400 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
401 if(tl < lms_lowest_lives)
402 lms_lowest_lives = tl;
406 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
409 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
410 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
413 M_ARGV(2, float) = 0; // frag score
418 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
420 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
421 if(!cvar("g_use_ammunition"))
422 start_items |= IT_UNLIMITED_AMMO;
424 start_health = warmup_start_health = cvar("g_lms_start_health");
425 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
426 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
427 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
428 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
429 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
430 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
431 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
434 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
436 // don't clear player score
440 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
442 if (autocvar_g_lms_items)
445 entity definition = M_ARGV(0, entity);
447 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
451 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
454 void lms_extralife(entity this)
456 StartItem(this, ITEM_ExtraLife);
459 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
461 if (MUTATOR_RETURNVALUE) return false;
462 if (!autocvar_g_powerups) return false;
463 if (!autocvar_g_lms_extra_lives) return false;
465 entity ent = M_ARGV(0, entity);
467 // Can't use .itemdef here
468 if (ent.classname != "item_health_mega") return false;
471 setthink(e, lms_extralife);
473 e.nextthink = time + 0.1;
474 e.spawnflags = ent.spawnflags;
475 e.noalign = ent.noalign;
476 setorigin(e, ent.origin);
481 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
483 if(MUTATOR_RETURNVALUE) return false;
485 entity item = M_ARGV(0, entity);
486 entity toucher = M_ARGV(1, entity);
488 if(item.itemdef == ITEM_ExtraLife)
490 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
491 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
492 return MUT_ITEMTOUCH_PICKUP;
495 return MUT_ITEMTOUCH_CONTINUE;
498 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
500 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
501 if (INGAME(it) && it.lms_spectate_warning < 2)
502 ++M_ARGV(0, int); // activerealplayers
503 ++M_ARGV(1, int); // realplayers
509 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
511 entity player = M_ARGV(0, entity);
513 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
515 // for the forfeit message...
516 player.lms_spectate_warning = 2;
520 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
522 player.lms_spectate_warning = 1;
523 sprint(player, "^1WARNING:^7 you can't rejoin this match after spectating. Use the same command again to spectate anyway.\n");
524 Send_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CENTER_LMS_SPECWARN);
526 return MUT_SPECCMD_RETURN;
528 return MUT_SPECCMD_CONTINUE;
531 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
533 M_ARGV(0, float) = WinningCondition_LMS();
537 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
539 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
540 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
543 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
546 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
547 return true; // allow writing to this field in intermission as it is needed for newly joining players
550 void lms_Initialize()
552 lms_lowest_lives = 999;