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_regenerate;
13 int autocvar_g_lms_leader_wp_lives;
14 float autocvar_g_lms_leader_wp_max_relative;
15 float autocvar_g_lms_leader_wp_time;
16 float autocvar_g_lms_leader_wp_time_repeat;
17 float autocvar_g_lms_dynamic_respawn_delay;
18 float autocvar_g_lms_dynamic_respawn_delay_base;
19 float autocvar_g_lms_dynamic_respawn_delay_increase;
20 bool autocvar_g_lms_dynamic_vampire;
21 float autocvar_g_lms_dynamic_vampire_factor_base;
22 float autocvar_g_lms_dynamic_vampire_factor_increase;
23 float autocvar_g_lms_dynamic_vampire_factor_max;
24 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
29 int LMS_NewPlayerLives()
31 int fl = floor(autocvar_fraglimit);
32 if(fl == 0 || fl > 999)
35 // first player has left the game for dying too much? Nobody else can get in.
36 if(lms_lowest_lives < 1)
39 if(!autocvar_g_lms_join_anytime)
40 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
43 return bound(1, lms_lowest_lives, fl);
48 // LMS winning condition: game terminates if and only if there's at most one
49 // one player who's living lives. Top two scores being equal cancels the time
51 int WinningCondition_LMS()
53 if (warmup_stage || time <= game_starttime)
56 entity first_player = NULL;
58 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
68 // two or more active players - continue with the game
70 if (autocvar_g_campaign)
72 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
73 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
75 return WINNING_YES; // human player lost, game over
82 // exactly one player?
85 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
87 if (LMS_NewPlayerLives())
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 // wait for players...
110 // SNAFU (maybe a draw game?)
112 LOG_TRACE("No players, ending game.");
117 // When we get here, we have at least two players who are actually LIVING,
118 // now check if the top two players have equal score.
119 WinningConditionHelper(NULL);
122 if(WinningConditionHelper_winner)
123 WinningConditionHelper_winner.winning = true;
124 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
125 return WINNING_NEVER;
127 // Top two have different scores? Way to go for our beloved TIMELIMIT!
131 // runs on waypoints which are attached to leaders, updates once per frame
132 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view)
136 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
138 float leader_time = autocvar_g_lms_leader_wp_time;
139 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
140 if (time % leader_repeat_time > leader_time)
146 void lms_UpdateLeaders()
150 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
151 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
152 if (lives > max_lives)
157 int second_max_lives = 0;
158 int pl_cnt_with_max_lives = 0;
159 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
160 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
161 if (lives == max_lives)
162 pl_cnt_with_max_lives++;
163 else if (lives > second_max_lives)
164 second_max_lives = lives;
167 int lives_diff = autocvar_g_lms_leader_wp_lives;
168 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
169 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
170 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
171 if (lives == max_lives)
174 it.lms_leader = true;
178 it.lms_leader = false;
182 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
183 if (it.waypointsprite_attachedforcarrier)
184 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
185 it.lms_leader = false;
190 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
192 lms_lowest_lives = 999;
195 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
197 FOREACH_CLIENT(true, {
198 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
200 // players who forfeited (rank >= 256) become spectators
201 if (it.lms_spectate_warning == 2)
202 it.frags = FRAGS_SPECTATOR;
204 it.frags = FRAGS_PLAYER;
207 CS(it).killcount = 0;
209 it.lms_spectate_warning = 0;
210 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
211 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
213 if (it.frags != FRAGS_PLAYER)
216 TRANSMUTE(Player, it);
217 PutClientInServer(it);
218 it.lms_leader = false;
219 if (it.waypointsprite_attachedforcarrier)
220 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
224 // FIXME add support for sv_ready_restart_after_countdown
225 // that is find a way to respawn/reset players IN GAME without setting lives to 0
226 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
229 sv_ready_restart_after_countdown = 0;
232 // returns true if player is added to the game
233 bool lms_AddPlayer(entity player)
235 if (!player.lmsplayer)
237 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
240 player.lmsplayer = 2; // temp value indicating player has just joined the game (but not spawned yet)
242 if (warmup_stage || time <= game_starttime)
244 if(player.lms_spectate_warning)
246 player.lms_spectate_warning = 0;
247 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
248 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
250 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
255 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
257 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
264 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
266 entity player = M_ARGV(0, entity);
267 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
269 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
270 TRANSMUTE(Observer, player);
274 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
276 entity player = M_ARGV(0, entity);
278 if (warmup_stage || time < game_starttime)
281 if (player.lmsplayer == 2) // just joined the game
283 // spawn player with the same amount of health / armor
284 // as the least healthy player with the least number of lives
285 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
286 float min_health = start_health;
287 float min_armorvalue = start_armorvalue;
288 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
289 if (GetResource(it, RES_HEALTH) < min_health)
290 min_health = GetResource(it, RES_HEALTH);
291 if (GetResource(it, RES_ARMOR) < min_armorvalue)
292 min_armorvalue = GetResource(it, RES_ARMOR);
294 if (min_health != start_health)
295 SetResource(player, RES_HEALTH, max(1, min_health));
296 if (min_armorvalue != start_armorvalue)
297 SetResource(player, RES_ARMOR, min_armorvalue);
298 player.lmsplayer = 1;
302 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
304 entity player = M_ARGV(0, entity);
306 if (warmup_stage || lms_AddPlayer(player))
312 void lms_RemovePlayer(entity player)
314 if (warmup_stage || time < game_starttime)
317 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
320 if (player.lms_spectate_warning < 2)
322 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
324 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
327 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
331 int min_forfeiter_rank = 665; // different from 666
332 FOREACH_CLIENT(true, {
333 // update rank of other players that were eliminated
334 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
336 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
337 if (it_rank > player_rank && it_rank <= 256)
338 GameRules_scoring_add(it, LMS_RANK, -1);
339 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
340 min_forfeiter_rank = it_rank - 1;
342 else if (it.frags != FRAGS_SPECTATOR)
344 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
345 if(tl < lms_lowest_lives)
346 lms_lowest_lives = tl;
349 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
351 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
352 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
353 TRANSMUTE(Observer, player);
355 if (autocvar_g_lms_leader_wp_lives > 0)
359 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
361 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
362 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
364 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
368 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
370 entity player = M_ARGV(0, entity);
372 // no further message other than the disconnect message
373 player.lms_spectate_warning = 3;
375 lms_RemovePlayer(player);
376 player.lmsplayer = 0;
379 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
381 entity player = M_ARGV(0, entity);
382 bool is_forced = M_ARGV(1, bool);
384 if (!IS_PLAYER(player))
387 if (warmup_stage || time <= game_starttime)
389 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
390 player.frags = FRAGS_SPECTATOR;
391 TRANSMUTE(Observer, player);
392 player.lmsplayer = 0;
397 player.lms_spectate_warning = 2;
398 if (!GameRules_scoring_add(player, LMS_RANK, 0))
399 lms_RemovePlayer(player);
401 return true; // prevent team reset
404 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
406 entity player = M_ARGV(0, entity);
407 player.frags = FRAGS_SPECTATOR;
411 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
413 entity player = M_ARGV(0, entity);
415 // recycled REDALIVE to avoid adding a dedicated stat
416 STAT(REDALIVE, player) = lms_leaders;
418 if(player.deadflag == DEAD_DYING)
419 player.deadflag = DEAD_RESPAWNING;
422 MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
425 FOREACH_CLIENT(true, {
426 if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
430 if (!it.waypointsprite_attachedforcarrier)
432 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
433 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
434 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
435 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
436 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
437 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
444 if (it.waypointsprite_attachedforcarrier)
445 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
450 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
452 if(autocvar_g_lms_regenerate)
457 MUTATOR_HOOKFUNCTION(lms, PlayerPowerups)
459 entity player = M_ARGV(0, entity);
460 if (player.waypointsprite_attachedforcarrier)
461 player.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
463 player.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
466 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
472 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
474 if (!autocvar_g_lms_dynamic_vampire)
477 entity frag_attacker = M_ARGV(1, entity);
478 entity frag_target = M_ARGV(2, entity);
479 float frag_damage = M_ARGV(4, float);
481 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
482 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
484 float vampire_factor = 0;
486 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
487 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
488 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
491 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
492 if (vampire_factor > 0)
494 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
495 SetResourceExplicit(frag_attacker, RES_HEALTH,
496 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
501 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
503 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
507 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
509 entity player = M_ARGV(0, entity);
510 player.respawn_flags |= RESPAWN_FORCE;
512 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
515 player.respawn_flags = RESPAWN_SILENT;
516 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
517 player.respawn_time = time + 2;
521 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
526 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
527 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
528 if (lives > max_lives)
533 // min delay with only 2 players
534 if (pl_cnt == 1) // player wasn't counted
537 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
538 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
542 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
544 entity frag_target = M_ARGV(1, entity);
546 if (!warmup_stage && time > game_starttime)
549 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
550 if(tl < lms_lowest_lives)
551 lms_lowest_lives = tl;
555 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
558 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
559 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
562 M_ARGV(2, float) = 0; // frag score
567 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
569 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
570 start_health = warmup_start_health = cvar("g_lms_start_health");
571 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
572 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
573 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
574 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
575 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
576 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
577 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
580 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
582 // don't clear player score
586 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
588 entity definition = M_ARGV(0, entity);
590 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
597 void lms_extralife(entity this)
599 StartItem(this, ITEM_ExtraLife);
602 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
604 if (MUTATOR_RETURNVALUE) return false;
605 if (!autocvar_g_powerups) return false;
606 if (!autocvar_g_lms_extra_lives) return false;
608 entity ent = M_ARGV(0, entity);
610 // Can't use .itemdef here
611 if (ent.classname != "item_health_mega") return false;
614 setthink(e, lms_extralife);
616 e.nextthink = time + 0.1;
617 e.spawnflags = ent.spawnflags;
618 e.noalign = ent.noalign;
619 setorigin(e, ent.origin);
624 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
626 if(MUTATOR_RETURNVALUE) return false;
628 entity item = M_ARGV(0, entity);
629 entity toucher = M_ARGV(1, entity);
631 if(item.itemdef == ITEM_ExtraLife)
633 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
634 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
635 return MUT_ITEMTOUCH_PICKUP;
638 return MUT_ITEMTOUCH_CONTINUE;
641 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
643 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
644 if (it.lmsplayer && it.lms_spectate_warning < 2)
645 ++M_ARGV(0, int); // activerealplayers
646 ++M_ARGV(1, int); // realplayers
652 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
654 entity player = M_ARGV(0, entity);
656 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
658 // for the forfeit message...
659 player.lms_spectate_warning = 2;
663 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
665 player.lms_spectate_warning = 1;
666 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");
668 return MUT_SPECCMD_RETURN;
670 return MUT_SPECCMD_CONTINUE;
673 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
675 M_ARGV(0, float) = WinningCondition_LMS();
679 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
681 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
682 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
685 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
687 entity player = M_ARGV(0, entity);
689 return boolean(player.lmsplayer);
692 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
695 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
696 return true; // allow writing to this field in intermission as it is needed for newly joining players
699 void lms_Initialize()
701 lms_lowest_lives = 999;