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, ForbidThrowCurrentWeapon)
370 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
372 if (!autocvar_g_lms_dynamic_vampire)
375 entity frag_attacker = M_ARGV(1, entity);
376 entity frag_target = M_ARGV(2, entity);
377 float frag_damage = M_ARGV(4, float);
379 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
380 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
382 float vampire_factor = 0;
384 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
385 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
386 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
389 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
390 if (vampire_factor > 0)
392 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
393 SetResourceExplicit(frag_attacker, RES_HEALTH,
394 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
399 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
403 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
405 float leader_time = autocvar_g_lms_leader_wp_time;
406 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
407 float wp_time = this.owner.lms_wp_time;
408 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
414 void lms_UpdateWaypoints()
418 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
419 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
420 if (lives > max_lives)
425 int second_max_lives = 0;
426 int pl_cnt_with_max_lives = 0;
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)
430 pl_cnt_with_max_lives++;
431 else if (lives > second_max_lives)
432 second_max_lives = lives;
435 int lives_diff = autocvar_g_lms_leader_wp_lives;
436 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
437 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
438 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
439 if (lives == max_lives)
441 if (!it.waypointsprite_attachedforcarrier)
443 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
444 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
445 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
446 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
447 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
448 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
451 it.lms_wp_time = time;
455 if (it.waypointsprite_attachedforcarrier)
456 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
461 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
462 if (it.waypointsprite_attachedforcarrier)
463 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
468 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
470 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
471 lms_UpdateWaypoints();
474 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
476 entity player = M_ARGV(0, entity);
477 player.respawn_flags |= RESPAWN_FORCE;
479 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
482 player.respawn_flags = RESPAWN_SILENT;
483 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
484 player.respawn_time = time + 2;
488 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
493 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
494 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
495 if (lives > max_lives)
500 // min delay with only 2 players
501 if (pl_cnt == 1) // player wasn't counted
504 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
505 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
509 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
511 entity frag_target = M_ARGV(1, entity);
513 if (!warmup_stage && time > game_starttime)
516 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
517 if(tl < lms_lowest_lives)
518 lms_lowest_lives = tl;
522 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
525 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
526 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
529 M_ARGV(2, float) = 0; // frag score
534 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
536 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
537 start_health = warmup_start_health = cvar("g_lms_start_health");
538 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
539 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
540 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
541 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
542 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
543 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
544 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
547 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
549 // don't clear player score
553 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
555 entity definition = M_ARGV(0, entity);
557 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
564 void lms_extralife(entity this)
566 StartItem(this, ITEM_ExtraLife);
569 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
571 if (MUTATOR_RETURNVALUE) return false;
572 if (!autocvar_g_powerups) return false;
573 if (!autocvar_g_lms_extra_lives) return false;
575 entity ent = M_ARGV(0, entity);
577 // Can't use .itemdef here
578 if (ent.classname != "item_health_mega") return false;
581 setthink(e, lms_extralife);
583 e.nextthink = time + 0.1;
584 e.spawnflags = ent.spawnflags;
585 e.noalign = ent.noalign;
586 setorigin(e, ent.origin);
591 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
593 if(MUTATOR_RETURNVALUE) return false;
595 entity item = M_ARGV(0, entity);
596 entity toucher = M_ARGV(1, entity);
598 if(item.itemdef == ITEM_ExtraLife)
600 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
601 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
602 return MUT_ITEMTOUCH_PICKUP;
605 return MUT_ITEMTOUCH_CONTINUE;
608 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
610 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
611 if (it.lmsplayer && it.lms_spectate_warning < 2)
612 ++M_ARGV(0, int); // activerealplayers
613 ++M_ARGV(1, int); // realplayers
619 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
621 entity player = M_ARGV(0, entity);
623 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
625 // for the forfeit message...
626 player.lms_spectate_warning = 2;
630 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
632 player.lms_spectate_warning = 1;
633 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");
635 return MUT_SPECCMD_RETURN;
637 return MUT_SPECCMD_CONTINUE;
640 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
642 M_ARGV(0, float) = WinningCondition_LMS();
646 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
648 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
649 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
652 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
654 entity player = M_ARGV(0, entity);
656 return boolean(player.lmsplayer);
659 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
662 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
663 return true; // allow writing to this field in intermission as it is needed for newly joining players
666 void lms_Initialize()
668 lms_lowest_lives = 999;