]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
LMS: jitter waypoint repeat time to make it less predictable
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
index 95ecb8b8d6a61830123a7d3f4d8a953d5fbecb83..aa8cdc5381f364a0d61b7939686005f8416604eb 100644 (file)
@@ -10,20 +10,26 @@ int autocvar_g_lms_extra_lives;
 bool autocvar_g_lms_join_anytime;
 int autocvar_g_lms_last_join;
 bool autocvar_g_lms_regenerate;
-int autocvar_g_lms_leader_wp_lives;
-float autocvar_g_lms_leader_wp_max_relative;
-float autocvar_g_lms_leader_wp_time;
-float autocvar_g_lms_leader_wp_time_repeat;
-float autocvar_g_lms_dynamic_respawn_delay;
-float autocvar_g_lms_dynamic_respawn_delay_base;
-float autocvar_g_lms_dynamic_respawn_delay_increase;
-bool autocvar_g_lms_dynamic_vampire;
-float autocvar_g_lms_dynamic_vampire_factor_base;
-float autocvar_g_lms_dynamic_vampire_factor_increase;
-float autocvar_g_lms_dynamic_vampire_factor_max;
-int autocvar_g_lms_dynamic_vampire_min_lives_diff;
-
-.float lms_wp_time;
+int autocvar_g_lms_leader_wp_lives = 2;
+float autocvar_g_lms_leader_wp_max_relative = 0.5;
+float autocvar_g_lms_leader_wp_time = 5;
+float autocvar_g_lms_leader_wp_time_jitter = 10;
+float autocvar_g_lms_leader_wp_time_repeat = 25;
+float autocvar_g_lms_dynamic_respawn_delay = 1;
+float autocvar_g_lms_dynamic_respawn_delay_base = 2;
+float autocvar_g_lms_dynamic_respawn_delay_increase = 3;
+float autocvar_g_lms_dynamic_respawn_delay_max = 20;
+bool autocvar_g_lms_dynamic_vampire = 1;
+float autocvar_g_lms_dynamic_vampire_factor_base = 0.1;
+float autocvar_g_lms_dynamic_vampire_factor_increase = 0.1;
+float autocvar_g_lms_dynamic_vampire_factor_max = 0.5;
+int autocvar_g_lms_dynamic_vampire_min_lives_diff = 2;
+
+.float lms_leader;
+int lms_leaders;
+bool lms_visible_leaders;
+bool lms_visible_leaders_prev;
+float lms_leader_time_jitter;
 
 // main functions
 int LMS_NewPlayerLives()
@@ -128,6 +134,62 @@ int WinningCondition_LMS()
        return WINNING_NO;
 }
 
+// runs on waypoints which are attached to leaders, updates once per frame
+bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view)
+{
+       if(view.lms_leader)
+               if(IS_SPEC(player))
+                       return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
+
+       if (!lms_visible_leaders)
+               return false;
+
+       return true;
+}
+
+void lms_UpdateLeaders()
+{
+       int max_lives = 0;
+       int pl_cnt = 0;
+       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+               int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
+               if (lives > max_lives)
+                       max_lives = lives;
+               pl_cnt++;
+       });
+
+       int second_max_lives = 0;
+       int pl_cnt_with_max_lives = 0;
+       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+               int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
+               if (lives == max_lives)
+                       pl_cnt_with_max_lives++;
+               else if (lives > second_max_lives)
+                       second_max_lives = lives;
+       });
+
+       int lives_diff = autocvar_g_lms_leader_wp_lives;
+       if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
+               FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+                       int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
+                       if (lives == max_lives)
+                       {
+                               if (!it.lms_leader)
+                                       it.lms_leader = true;
+                       }
+                       else
+                       {
+                               it.lms_leader = false;
+                       }
+               });
+       else
+               FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
+                       if (it.waypointsprite_attachedforcarrier)
+                               WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
+                       it.lms_leader = false;
+               });
+}
+
 // mutator hooks
 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
 {
@@ -157,6 +219,7 @@ MUTATOR_HOOKFUNCTION(lms, reset_map_players)
 
                TRANSMUTE(Player, it);
                PutClientInServer(it);
+               it.lms_leader = false;
                if (it.waypointsprite_attachedforcarrier)
                        WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
        });
@@ -293,6 +356,8 @@ void lms_RemovePlayer(entity player)
                        player.frags = FRAGS_PLAYER_OUT_OF_GAME;
                        TRANSMUTE(Observer, player);
                }
+               if (autocvar_g_lms_leader_wp_lives > 0)
+                       lms_UpdateLeaders();
        }
 
        if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
@@ -350,10 +415,57 @@ MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
 {
        entity player = M_ARGV(0, entity);
 
+       // recycled REDALIVE to avoid adding a dedicated stat
+       STAT(REDALIVE, player) = lms_leaders;
+
        if(player.deadflag == DEAD_DYING)
                player.deadflag = DEAD_RESPAWNING;
 }
 
+MUTATOR_HOOKFUNCTION(lms, SV_StartFrame)
+{
+       float leader_time = autocvar_g_lms_leader_wp_time;
+       float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
+       lms_visible_leaders_prev = lms_visible_leaders;
+       lms_visible_leaders = (time % leader_repeat_time < leader_time);
+       if (lms_visible_leaders_prev && !lms_visible_leaders)
+               lms_leader_time_jitter = random() * autocvar_g_lms_leader_wp_time_jitter;
+       leader_repeat_time += lms_leader_time_jitter;
+
+       lms_leaders = 0;
+       FOREACH_CLIENT(true, {
+               STAT(OBJECTIVE_STATUS, it) = lms_visible_leaders;
+               if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
+               {
+                       if (it.lms_leader)
+                       {
+                               if (!it.waypointsprite_attachedforcarrier)
+                               {
+                                       WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
+                                       it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
+                                       WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
+                                       vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
+                                       WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
+                                       WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
+                               }
+                               if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
+                                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_LEADER);
+                               lms_leaders++;
+                       }
+               }
+               else
+               {
+                       if (IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME)
+                       {
+                               if (!lms_visible_leaders_prev && lms_visible_leaders && IS_REAL_CLIENT(it))
+                                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_LMS_VISIBLE_OTHER);
+                       }
+                       if (it.waypointsprite_attachedforcarrier)
+                               WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
+               }
+       });
+}
+
 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
 {
        if(autocvar_g_lms_regenerate)
@@ -405,79 +517,10 @@ MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
        }
 }
 
-bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
-{
-       if(view.lms_wp_time)
-               if(IS_SPEC(player))
-                       return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
-
-       float leader_time = autocvar_g_lms_leader_wp_time;
-       float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
-       float wp_time = this.owner.lms_wp_time;
-       if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
-               return false;
-
-       return true;
-}
-
-void lms_UpdateWaypoints()
-{
-       int max_lives = 0;
-       int pl_cnt = 0;
-       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
-               int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
-               if (lives > max_lives)
-                       max_lives = lives;
-               pl_cnt++;
-       });
-
-       int second_max_lives = 0;
-       int pl_cnt_with_max_lives = 0;
-       FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
-               int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
-               if (lives == max_lives)
-                       pl_cnt_with_max_lives++;
-               else if (lives > second_max_lives)
-                       second_max_lives = lives;
-       });
-
-       int lives_diff = autocvar_g_lms_leader_wp_lives;
-       if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
-               FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
-                       int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
-                       if (lives == max_lives)
-                       {
-                               if (!it.waypointsprite_attachedforcarrier)
-                               {
-                                       WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
-                                       it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
-                                       WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
-                                       vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
-                                       WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
-                                       WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
-                               }
-                               if (!it.lms_wp_time)
-                                       it.lms_wp_time = time;
-                       }
-                       else
-                       {
-                               if (it.waypointsprite_attachedforcarrier)
-                                       WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
-                               it.lms_wp_time = 0;
-                       }
-               });
-       else
-               FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
-                       if (it.waypointsprite_attachedforcarrier)
-                               WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
-                       it.lms_wp_time = 0;
-               });
-}
-
 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
 {
        if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
-               lms_UpdateWaypoints();
+               lms_UpdateLeaders();
 }
 
 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
@@ -510,8 +553,9 @@ MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
        if (pl_cnt == 1) // player wasn't counted
                max_lives = 0;
 
-       player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
+       float dlay = autocvar_g_lms_dynamic_respawn_delay_base +
                autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
+       player.respawn_time = time + min(autocvar_g_lms_dynamic_respawn_delay_max, dlay);
        return true;
 }