Skip to content

Share: healing infected, sharing ammo, infection pack always overused #3

Description

@Luti2000

Hello!

Since I don't know other ways to contact I write here, sorry about that. I'm relatively new on the platform and I can't push a new branch since i dont have permission for your repository
I like the mod very much, i started fixing some things during my free time starting with the share logic that really annoyed me, there are still some problems, but still better than nothing.

I added basic persistence, fixed the ammopack share calculation, and healing when player is infected.
Infection pack is still overused but it usually is not that annoying

Persistence is not perfect since it won't detect new node so config has to be deleted first if new node is added.

Thanks a lot for the SmartControl, makes my solo runs much better!

Here are the changes I made for myself 3 months ago:
ShareActionPatch.cs:

case (uint)ResourceIDs.MediPack:
    if (candidateAgent.Damage.GetHealthRel() + ammoutCanGivePercent < threshold) // Would you have less than threshHold after heals?
    {
        candidateScore = threshold - candidateAgent.Damage.GetHealthRel() - candidateAgent.Damage.Infection; // more damaged -> higher score
    }
    break;
case (uint)ResourceIDs.AmmoPack:
    if (candidateAmmoStorage.StandardAmmo.AmmoInPack + 90 <= 460 * threshold &&
        candidateAmmoStorage.SpecialAmmo.AmmoInPack + 90 <= 230 * threshold) //ammopack gives 90 to weapons max to main is 460 and special is 230 --- from wiki
    {
        candidateScore = threshold - (candidateAmmoStorage.StandardAmmo.RelInPack + candidateAmmoStorage.SpecialAmmo.RelInPack) / 2f;
    }
    break;
case (uint)ResourceIDs.DisinfectPack:
    //if (candidateAgent.Damage.Infection * threshold > ammoutCanGivePercent)
    if (__instance.m_bot.IsPositionInfected(candidateAgent.Position) > 0.001f) //Are they taking infection damage?
        break;
    if (candidateAgent.Damage.Infection > lowerThreshold) // Would we overheal?
        candidateScore = candidateAgent.Damage.Infection - lowerThreshold;
    //candidateScore = candidateAgent.Damage.Infection * threshold; // score is how infected they are
    break;
case (uint)ResourceIDs.ToolPack:
    if (candidateAgent.NeedToolAmmo() && candidateAmmoStorage.ClassAmmo.RelInPack + ammoutCanGivePercent < threshold) // would we overflow tool ammo?
    {
        candidateScore = threshold - candidateAmmoStorage.ClassAmmo.RelInPack; // how much ammo are they missing?
    }
    break;
default:
    break;

Added to the OverrideTree.cs

using System.Text.Json;
--- at netSender == 0
if (netSender == 0)
    Persistence.Write(identifier, node.GetNodeTreeString(), value);

under --- var node = new Node(key, parent, value, condition, defaultValue, hasDefaultValue);
if (Persistence.Enabled)
{
    var saved = Persistence.Read(identifier, node.GetNodeTreeString(), value);
    if (saved.hasValue)
        node.Value = saved.value;
}


//then new class
internal static class Persistence
{
    // for debug purposes that everything is detected
    private const string Missing = "__SLIDES_BOT_CONTROL_MISSING__";

    internal static bool Enabled => ZiMain.PersistentConfig != null;

    internal static (bool hasValue, T value) Read<T>(string tree, string path, T fallback)
    {
        try
        {
            var key = Sanitize(path);
            var entry = ZiMain.PersistentConfig.Bind(tree, key, Missing, "Persisted Slides Bot Control setting");
            if (entry.Value == Missing)
                return (false, fallback);

            var value = JsonSerializer.Deserialize<T>(entry.Value);
            return (true, value);
        }
        catch (Exception e)
        {
            ZiMain.log?.LogWarning($"Failed to load persisted setting {tree}/{path}: {e.Message}");
            return (false, fallback);
        }
    }

    internal static void Write<T>(string tree, string path, T value)
    {
        try
        {
            var key = Sanitize(path);
            var entry = ZiMain.PersistentConfig.Bind(tree, key, Missing, "Persisted Slides Bot Control setting");
            entry.Value = JsonSerializer.Serialize(value);
            ZiMain.PersistentConfig.Save();
        }
        catch (Exception e)
        {
            ZiMain.log?.LogWarning($"Failed to save persisted setting {tree}/{path}: {e.Message}");
        }
    }

    private static string Sanitize(string path) => path.Replace('/', '.');
}

ZiMain.cs:

under --- public class ZiMain : BasePlugin
public static ConfigFile PersistentConfig { get; private set; }
To Load():
PersistentConfig = Config;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions