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 int autocvar_g_lms_leader_lives_diff = 2;
16 float autocvar_g_lms_leader_minpercent = 0.5;
17 float autocvar_g_lms_leader_wp_interval = 25;
18 float autocvar_g_lms_leader_wp_interval_jitter = 10;
19 float autocvar_g_lms_leader_wp_time = 5;
20 float autocvar_g_lms_dynamic_respawn_delay = 1;
21 float autocvar_g_lms_dynamic_respawn_delay_base = 2;
22 float autocvar_g_lms_dynamic_respawn_delay_increase = 3;
23 float autocvar_g_lms_dynamic_respawn_delay_max = 20;
24 bool autocvar_g_lms_dynamic_vampire = 1;
25 float autocvar_g_lms_dynamic_vampire_factor_base = 0.1;
26 float autocvar_g_lms_dynamic_vampire_factor_increase = 0.1;
27 float autocvar_g_lms_dynamic_vampire_factor_max = 0.5;
28 int autocvar_g_lms_dynamic_vampire_min_lives_diff = 2;
32 float lms_visible_leaders_time;
33 bool lms_visible_leaders = true; // triggers lms_visible_leaders_time update in the first frame
34 bool lms_visible_leaders_prev;
35 bool autocvar_g_lms_rot;
38 int LMS_NewPlayerLives()
40 int fl = floor(autocvar_fraglimit);
41 if(fl == 0 || fl > 999)
44 // first player has left the game for dying too much? Nobody else can get in.
45 if(lms_lowest_lives < 1)
48 if(!autocvar_g_lms_join_anytime)
49 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
52 return bound(1, lms_lowest_lives, fl);
57 // LMS winning condition: game terminates if and only if there's at most one
58 // one player who's living lives. Top two scores being equal cancels the time
60 int WinningCondition_LMS()
62 if (warmup_stage || time <= game_starttime)
65 entity first_player = NULL;
68 FOREACH_CLIENT(true, {
69 if (IS_PLAYER(it) && it.frags == FRAGS_PLAYER)
75 else if (GameRules_scoring_add(it, LMS_RANK, 0))
83 // two or more active players - continue with the game
85 if (autocvar_g_campaign && campaign_bots_may_start)
87 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
88 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
90 return WINNING_YES; // human player lost, game over
97 // exactly one player?
101 if (LMS_NewPlayerLives())
103 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
105 GameRules_scoring_add(first_player, LMS_RANK, 1);
106 first_player.winning = 1;
109 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
115 // and assign him his first place
116 GameRules_scoring_add(first_player, LMS_RANK, 1);
117 first_player.winning = 1;
124 // nobody is playing at all...
125 if (LMS_NewPlayerLives())
127 if (totalplayed && game_starttime > 0 && time > game_starttime + autocvar_g_lms_forfeit_min_match_time) // give players time to join
132 // wait for players...
136 // SNAFU (maybe a draw game?)
138 LOG_TRACE("No players, ending game.");
143 // When we get here, we have at least two players who are actually LIVING,
144 // now check if the top two players have equal score.
145 WinningConditionHelper(NULL);
148 if(WinningConditionHelper_winner)
149 WinningConditionHelper_winner.winning = true;
150 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
151 return WINNING_NEVER;
153 // Top two have different scores? Way to go for our beloved TIMELIMIT!
157 // runs on waypoints which are attached to leaders, updates once per frame
158 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view)
162 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
164 if (!lms_visible_leaders)
170 int lms_leaders_lives_diff;
171 void lms_UpdateLeaders()
175 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
176 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
177 if (lives > max_lives)
182 int second_max_lives = 0;
183 int pl_cnt_with_max_lives = 0;
184 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
185 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
186 if (lives == max_lives)
187 pl_cnt_with_max_lives++;
188 else if (lives > second_max_lives)
189 second_max_lives = lives;
192 lms_leaders_lives_diff = max_lives - second_max_lives;
194 int lives_diff = autocvar_g_lms_leader_lives_diff;
195 if (lms_leaders_lives_diff >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_minpercent)
196 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
197 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
198 if (lives == max_lives)
201 it.lms_leader = true;
205 it.lms_leader = false;
209 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
210 if (it.waypointsprite_attachedforcarrier)
211 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
212 it.lms_leader = false;
217 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
219 lms_lowest_lives = 999;
222 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
224 FOREACH_CLIENT(true, {
225 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
226 it.frags = FRAGS_PLAYER;
228 CS(it).killcount = 0;
229 INGAME_STATUS_CLEAR(it);
230 it.lms_spectate = false;
231 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
232 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
234 if (it.frags != FRAGS_PLAYER)
237 TRANSMUTE(Player, it);
238 PutClientInServer(it);
239 it.lms_leader = false;
240 if (it.waypointsprite_attachedforcarrier)
241 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
245 // FIXME add support for sv_ready_restart_after_countdown
246 // that is find a way to respawn/reset players IN GAME without setting lives to 0
247 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
250 sv_ready_restart_after_countdown = 0;
253 // returns true if player is added to the game
254 bool lms_AddPlayer(entity player)
258 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
261 if (time < game_starttime)
262 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
264 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
266 if (warmup_stage || time <= game_starttime)
268 player.lms_spectate = false;
269 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
270 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
272 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
276 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
278 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
285 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
287 entity player = M_ARGV(0, entity);
288 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
290 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
291 TRANSMUTE(Observer, player);
295 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
297 entity player = M_ARGV(0, entity);
299 if (warmup_stage || time < game_starttime)
302 if (INGAME_JOINING(player))
304 // spawn player with the same amount of health / armor
305 // as the least healthy player with the least number of lives
306 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
307 float min_health = start_health;
308 float min_armorvalue = start_armorvalue;
309 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
310 if (GetResource(it, RES_HEALTH) < min_health)
311 min_health = GetResource(it, RES_HEALTH);
312 if (GetResource(it, RES_ARMOR) < min_armorvalue)
313 min_armorvalue = GetResource(it, RES_ARMOR);
315 if (min_health != start_health)
316 SetResource(player, RES_HEALTH, max(1, min_health));
317 if (min_armorvalue != start_armorvalue)
318 SetResource(player, RES_ARMOR, min_armorvalue);
319 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
323 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
325 entity player = M_ARGV(0, entity);
327 if (warmup_stage || lms_AddPlayer(player))
333 void lms_RemovePlayer(entity player)
335 if (warmup_stage || time < game_starttime)
338 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
341 if (!player.lms_spectate)
343 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
345 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
348 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
350 else if (INGAME(player))
352 FOREACH_CLIENT(it != player, {
353 // update rank of other players
354 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
355 GameRules_scoring_add(it, LMS_RANK, -1);
357 int rank = GameRules_scoring_add(player, LMS_RANK, 0);
358 GameRules_scoring_add(player, LMS_RANK, -rank);
360 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
361 player.frags = FRAGS_SPECTATOR;
362 TRANSMUTE(Observer, player);
363 INGAME_STATUS_CLEAR(player);
364 player.lms_spectate = false;
365 CS(player).killcount = FRAGS_SPECTATOR;
367 if (autocvar_g_lms_leader_lives_diff > 0)
371 if (CS(player).killcount != FRAGS_SPECTATOR)
373 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0)
374 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
378 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
380 entity player = M_ARGV(0, entity);
382 player.lms_spectate = true;
384 lms_RemovePlayer(player);
385 INGAME_STATUS_CLEAR(player);
388 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
390 entity player = M_ARGV(0, entity);
391 bool is_forced = M_ARGV(1, bool);
393 if (!IS_PLAYER(player))
396 if (warmup_stage || time <= game_starttime)
398 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
399 player.frags = FRAGS_SPECTATOR;
400 TRANSMUTE(Observer, player);
401 INGAME_STATUS_CLEAR(player);
405 if (is_forced || player.killindicator_teamchange == -2) // player is forced or wants to spectate
406 player.lms_spectate = true;
407 if (!GameRules_scoring_add(player, LMS_RANK, 0))
408 lms_RemovePlayer(player);
410 return true; // prevent team reset
413 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
415 entity player = M_ARGV(0, entity);
416 player.frags = FRAGS_SPECTATOR;
419 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
421 entity player = M_ARGV(0, entity);
423 // recycled REDALIVE and BLUEALIVE to avoid adding a dedicated stat
424 STAT(REDALIVE, player) = lms_leaders;
425 STAT(BLUEALIVE, player) = lms_leaders_lives_diff;
427 if(player.deadflag == DEAD_DYING)
428 player.deadflag = DEAD_RESPAWNING;
431 MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
433 float leader_time = autocvar_g_lms_leader_wp_time;
434 float leader_interval = leader_time + autocvar_g_lms_leader_wp_interval;
435 lms_visible_leaders_prev = lms_visible_leaders;
436 lms_visible_leaders = (time > lms_visible_leaders_time && time < lms_visible_leaders_time + leader_time);
437 if (lms_visible_leaders_prev && !lms_visible_leaders)
438 lms_visible_leaders_time = time + leader_interval + random() * autocvar_g_lms_leader_wp_interval_jitter;
441 FOREACH_CLIENT(true, {
442 STAT(OBJECTIVE_STATUS, it) = lms_visible_leaders;
443 if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
447 if (!it.waypointsprite_attachedforcarrier)
449 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
450 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
451 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
452 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
453 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
454 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
456 if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
457 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_LEADER);
460 else // if (!it.lms_leader)
462 if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
464 if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
465 Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_OTHER);
467 if (it.waypointsprite_attachedforcarrier)
468 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
474 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
476 if(!autocvar_g_lms_regenerate)
477 M_ARGV(2, float) = 0;
478 if(!autocvar_g_lms_rot)
479 M_ARGV(3, float) = 0;
480 return (!autocvar_g_lms_regenerate && !autocvar_g_lms_rot);
483 MUTATOR_HOOKFUNCTION(lms, PlayerPowerups)
485 entity player = M_ARGV(0, entity);
486 if (player.waypointsprite_attachedforcarrier)
487 player.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
489 player.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
492 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
498 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
500 if (!autocvar_g_lms_dynamic_vampire)
503 entity frag_attacker = M_ARGV(1, entity);
504 entity frag_target = M_ARGV(2, entity);
505 float frag_damage = M_ARGV(4, float);
507 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
508 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
510 float vampire_factor = 0;
512 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
513 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
514 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
517 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
518 if (vampire_factor > 0)
520 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
521 SetResourceExplicit(frag_attacker, RES_HEALTH,
522 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
527 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
529 if (!warmup_stage && autocvar_g_lms_leader_lives_diff > 0)
533 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
535 entity player = M_ARGV(0, entity);
536 player.respawn_flags |= RESPAWN_FORCE;
538 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
541 player.respawn_flags = RESPAWN_SILENT;
542 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
543 player.respawn_time = time + 2;
547 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
552 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
553 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
554 if (lives > max_lives)
559 // min delay with only 2 players
560 if (pl_cnt == 1) // player wasn't counted
563 float dlay = autocvar_g_lms_dynamic_respawn_delay_base +
564 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
565 player.respawn_time = time + min(autocvar_g_lms_dynamic_respawn_delay_max, dlay);
569 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
571 entity frag_target = M_ARGV(1, entity);
573 if (!warmup_stage && time > game_starttime)
576 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
577 if(tl < lms_lowest_lives)
578 lms_lowest_lives = tl;
582 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
585 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
586 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
589 M_ARGV(2, float) = 0; // frag score
594 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
596 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
597 if(!cvar("g_use_ammunition"))
598 start_items |= IT_UNLIMITED_AMMO;
600 start_health = warmup_start_health = cvar("g_lms_start_health");
601 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
602 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
603 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
604 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
605 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
606 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
607 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
610 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
612 // don't clear player score
616 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
618 if (autocvar_g_lms_items)
621 entity definition = M_ARGV(0, entity);
623 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
627 return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
630 void lms_extralife(entity this)
632 StartItem(this, ITEM_ExtraLife);
635 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
637 if (MUTATOR_RETURNVALUE) return false;
638 if (!autocvar_g_powerups) return false;
639 if (!autocvar_g_lms_extra_lives) return false;
641 entity ent = M_ARGV(0, entity);
643 // Can't use .itemdef here
644 if (ent.classname != "item_health_mega") return false;
647 setthink(e, lms_extralife);
649 e.nextthink = time + 0.1;
650 e.spawnflags = ent.spawnflags;
651 e.noalign = ent.noalign;
652 setorigin(e, ent.origin);
657 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
659 if(MUTATOR_RETURNVALUE) return false;
661 entity item = M_ARGV(0, entity);
662 entity toucher = M_ARGV(1, entity);
664 if(item.itemdef == ITEM_ExtraLife)
666 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
667 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
668 return MUT_ITEMTOUCH_PICKUP;
671 return MUT_ITEMTOUCH_CONTINUE;
674 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
676 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
678 ++M_ARGV(0, int); // activerealplayers
679 ++M_ARGV(1, int); // realplayers
685 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
687 entity player = M_ARGV(0, entity);
688 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
689 return MUT_SPECCMD_CONTINUE;
690 // ranked players (out of game) can no longer become real spectators
691 return MUT_SPECCMD_RETURN;
694 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
696 M_ARGV(0, float) = WinningCondition_LMS();
700 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
702 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
703 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
706 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
709 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
710 return true; // allow writing to this field in intermission as it is needed for newly joining players
713 void lms_Initialize()
715 lms_lowest_lives = 999;