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!
132 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
134 lms_lowest_lives = 999;
137 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
139 FOREACH_CLIENT(true, {
140 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
142 // players who forfeited (rank >= 256) become spectators
143 if (it.lms_spectate_warning == 2)
144 it.frags = FRAGS_SPECTATOR;
146 it.frags = FRAGS_PLAYER;
149 CS(it).killcount = 0;
151 it.lms_spectate_warning = 0;
152 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
153 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
155 if (it.frags != FRAGS_PLAYER)
158 TRANSMUTE(Player, it);
159 PutClientInServer(it);
160 if (it.waypointsprite_attachedforcarrier)
161 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
165 // FIXME add support for sv_ready_restart_after_countdown
166 // that is find a way to respawn/reset players IN GAME without setting lives to 0
167 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
170 sv_ready_restart_after_countdown = 0;
173 // returns true if player is added to the game
174 bool lms_AddPlayer(entity player)
176 if (!player.lmsplayer)
178 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
181 player.lmsplayer = 2; // temp value indicating player has just joined the game (but not spawned yet)
183 if (warmup_stage || time <= game_starttime)
185 if(player.lms_spectate_warning)
187 player.lms_spectate_warning = 0;
188 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
189 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
191 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
196 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
198 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
205 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
207 entity player = M_ARGV(0, entity);
208 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
210 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
211 TRANSMUTE(Observer, player);
215 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
217 entity player = M_ARGV(0, entity);
219 if (warmup_stage || time < game_starttime)
222 if (player.lmsplayer == 2) // just joined the game
224 // spawn player with the same amount of health / armor
225 // as the least healthy player with the least number of lives
226 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
227 float min_health = start_health;
228 float min_armorvalue = start_armorvalue;
229 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
230 if (GetResource(it, RES_HEALTH) < min_health)
231 min_health = GetResource(it, RES_HEALTH);
232 if (GetResource(it, RES_ARMOR) < min_armorvalue)
233 min_armorvalue = GetResource(it, RES_ARMOR);
235 if (min_health != start_health)
236 SetResource(player, RES_HEALTH, max(1, min_health));
237 if (min_armorvalue != start_armorvalue)
238 SetResource(player, RES_ARMOR, min_armorvalue);
239 player.lmsplayer = 1;
243 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
245 entity player = M_ARGV(0, entity);
247 if (warmup_stage || lms_AddPlayer(player))
253 void lms_RemovePlayer(entity player)
255 if (warmup_stage || time < game_starttime)
258 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
261 if (player.lms_spectate_warning < 2)
263 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
265 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
268 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
272 int min_forfeiter_rank = 665; // different from 666
273 FOREACH_CLIENT(true, {
274 // update rank of other players that were eliminated
275 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
277 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
278 if (it_rank > player_rank && it_rank <= 256)
279 GameRules_scoring_add(it, LMS_RANK, -1);
280 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
281 min_forfeiter_rank = it_rank - 1;
283 else if (it.frags != FRAGS_SPECTATOR)
285 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
286 if(tl < lms_lowest_lives)
287 lms_lowest_lives = tl;
290 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
292 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
293 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
294 TRANSMUTE(Observer, player);
298 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
300 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
301 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
303 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
307 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
309 entity player = M_ARGV(0, entity);
311 // no further message other than the disconnect message
312 player.lms_spectate_warning = 3;
314 lms_RemovePlayer(player);
315 player.lmsplayer = 0;
318 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
320 entity player = M_ARGV(0, entity);
321 bool is_forced = M_ARGV(1, bool);
323 if (!IS_PLAYER(player))
326 if (warmup_stage || time <= game_starttime)
328 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
329 player.frags = FRAGS_SPECTATOR;
330 TRANSMUTE(Observer, player);
331 player.lmsplayer = 0;
336 player.lms_spectate_warning = 2;
337 if (!GameRules_scoring_add(player, LMS_RANK, 0))
338 lms_RemovePlayer(player);
340 return true; // prevent team reset
343 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
345 entity player = M_ARGV(0, entity);
346 player.frags = FRAGS_SPECTATOR;
349 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
351 entity player = M_ARGV(0, entity);
353 if(player.deadflag == DEAD_DYING)
354 player.deadflag = DEAD_RESPAWNING;
357 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
359 if(autocvar_g_lms_regenerate)
364 MUTATOR_HOOKFUNCTION(lms, PlayerPowerups)
366 entity player = M_ARGV(0, entity);
367 if (player.waypointsprite_attachedforcarrier)
368 player.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
370 player.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
373 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
379 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
381 if (!autocvar_g_lms_dynamic_vampire)
384 entity frag_attacker = M_ARGV(1, entity);
385 entity frag_target = M_ARGV(2, entity);
386 float frag_damage = M_ARGV(4, float);
388 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
389 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
391 float vampire_factor = 0;
393 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
394 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
395 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
398 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
399 if (vampire_factor > 0)
401 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
402 SetResourceExplicit(frag_attacker, RES_HEALTH,
403 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
408 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
412 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
414 float leader_time = autocvar_g_lms_leader_wp_time;
415 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
416 float wp_time = this.owner.lms_wp_time;
417 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
423 void lms_UpdateWaypoints()
427 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
428 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
429 if (lives > max_lives)
434 int second_max_lives = 0;
435 int pl_cnt_with_max_lives = 0;
436 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
437 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
438 if (lives == max_lives)
439 pl_cnt_with_max_lives++;
440 else if (lives > second_max_lives)
441 second_max_lives = lives;
444 int lives_diff = autocvar_g_lms_leader_wp_lives;
445 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
446 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
447 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
448 if (lives == max_lives)
450 if (!it.waypointsprite_attachedforcarrier)
452 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
453 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
454 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
455 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
456 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
457 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
460 it.lms_wp_time = time;
464 if (it.waypointsprite_attachedforcarrier)
465 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
470 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
471 if (it.waypointsprite_attachedforcarrier)
472 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
477 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
479 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
480 lms_UpdateWaypoints();
483 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
485 entity player = M_ARGV(0, entity);
486 player.respawn_flags |= RESPAWN_FORCE;
488 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
491 player.respawn_flags = RESPAWN_SILENT;
492 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
493 player.respawn_time = time + 2;
497 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
502 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
503 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
504 if (lives > max_lives)
509 // min delay with only 2 players
510 if (pl_cnt == 1) // player wasn't counted
513 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
514 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
518 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
520 entity frag_target = M_ARGV(1, entity);
522 if (!warmup_stage && time > game_starttime)
525 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
526 if(tl < lms_lowest_lives)
527 lms_lowest_lives = tl;
531 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
534 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
535 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
538 M_ARGV(2, float) = 0; // frag score
543 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
545 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
546 start_health = warmup_start_health = cvar("g_lms_start_health");
547 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
548 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
549 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
550 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
551 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
552 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
553 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
556 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
558 // don't clear player score
562 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
564 entity definition = M_ARGV(0, entity);
566 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
573 void lms_extralife(entity this)
575 StartItem(this, ITEM_ExtraLife);
578 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
580 if (MUTATOR_RETURNVALUE) return false;
581 if (!autocvar_g_powerups) return false;
582 if (!autocvar_g_lms_extra_lives) return false;
584 entity ent = M_ARGV(0, entity);
586 // Can't use .itemdef here
587 if (ent.classname != "item_health_mega") return false;
590 setthink(e, lms_extralife);
592 e.nextthink = time + 0.1;
593 e.spawnflags = ent.spawnflags;
594 e.noalign = ent.noalign;
595 setorigin(e, ent.origin);
600 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
602 if(MUTATOR_RETURNVALUE) return false;
604 entity item = M_ARGV(0, entity);
605 entity toucher = M_ARGV(1, entity);
607 if(item.itemdef == ITEM_ExtraLife)
609 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
610 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
611 return MUT_ITEMTOUCH_PICKUP;
614 return MUT_ITEMTOUCH_CONTINUE;
617 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
619 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
620 if (it.lmsplayer && it.lms_spectate_warning < 2)
621 ++M_ARGV(0, int); // activerealplayers
622 ++M_ARGV(1, int); // realplayers
628 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
630 entity player = M_ARGV(0, entity);
632 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
634 // for the forfeit message...
635 player.lms_spectate_warning = 2;
639 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
641 player.lms_spectate_warning = 1;
642 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");
644 return MUT_SPECCMD_RETURN;
646 return MUT_SPECCMD_CONTINUE;
649 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
651 M_ARGV(0, float) = WinningCondition_LMS();
655 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
657 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
658 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
661 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
663 entity player = M_ARGV(0, entity);
665 return boolean(player.lmsplayer);
668 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
671 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
672 return true; // allow writing to this field in intermission as it is needed for newly joining players
675 void lms_Initialize()
677 lms_lowest_lives = 999;