RynthSuite/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.RareCorpses.cs

155 lines
8.6 KiB
C#

// ============================================================================
// RynthSuite - RynthAiPlugin.RareCorpses.cs
// Loot Only Rare Corpses (2026-11-22: "we should know whether it's a rare corpse... vtank does
// somehow"). Asked for on Discord for rare farming on Conquest: Rares loot mode opened every corpse,
// and kills per hour went down. The setting was kept only so profiles round-trip, read by nothing.
//
// How a corpse says it holds a rare: ACE (Corpse.TryGenerateRare, the same in DreamWeave's fork)
// appends " This corpse generated a rare item!" to the corpse's long description, the text whose
// "Killed by <name>." the ownership rules already read once the corpse is identified (the looter
// asks for that ID for every corpse in range). It also broadcasts "<killer> has discovered the
// <rare>!", but the description is on the corpse, so it holds for a corpse seen after a relog too.
//
// With the setting on, a corpse is opened only when its description has that sentence. One whose
// description hasn't arrived yet waits for it, as Mine Only does. Your own death corpse
// (Recover My Corpse) is decided before this, so it is always recovered.
//
// The sentence is the same for every rare tier (ACE writes it before the corpse enters the world),
// so tier never decides anything here. A waiting corpse is never claimed, so it never got the target's
// ID re-asks: since 2026-11-29 one in loot range is asked again (CorpseOpenController
// RareCorpseIdWaitInRange), or a dropped request left a rare corpse closed for good.
//
// No rare is left on a corpse (2026-11-29), in every loot mode:
// * a rare is always kept, ahead of the loot profile (ClassifyLootItem), and needs no ID when its
// rare icon shows it (ItemNeedsAppraisalForLoot);
// * it is taken first on a corpse, and may use the slots kept free for weapon swaps;
// * a corpse that generated a rare gets past Stop Looting When Pack Full while one slot is free,
// and is claimed ahead of other corpses (your own death corpse still comes first);
// * an opened corpse with a rare still on it is never marked done quietly: it is set aside and
// looted again (RareLeftMaxRetries times), then a chat line names the rare to take by hand.
// ============================================================================
using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.Plugin.RynthAi.Loot;
namespace RynthCore.Plugin.RynthAi;
public sealed partial class RynthAiPlugin
{
/// <summary>The sentence ACE adds to a corpse that generated a rare (Corpse.TryGenerateRare).</summary>
internal const string RareCorpseText = "This corpse generated a rare item";
/// <summary>Times an opened corpse with a rare still on it is looted again before the chat line.</summary>
internal const int RareLeftMaxRetries = 3;
/// <summary>How long such a corpse is set aside before it is looted again.</summary>
internal const long RareLeftRetryMs = 5_000;
/// <summary>Per corpse: times it was set aside for a rare still on it.</summary>
private readonly Dictionary<int, int> _rareLeftRetries = new();
/// <summary>The corpse's long description says it generated a rare.</summary>
internal static bool IsRareCorpse(string? longDesc)
=> !string.IsNullOrEmpty(longDesc) && longDesc.IndexOf(RareCorpseText, StringComparison.OrdinalIgnoreCase) >= 0;
/// <summary>Why Loot Only Rare Corpses leaves a corpse closed, or null when it is a rare corpse.</summary>
internal static string? RareCorpseRefusal(string? longDesc)
{
if (string.IsNullOrWhiteSpace(longDesc))
return "Loot Only Rare Corpses: waiting for its description (the ID) to see whether it generated a rare.";
return IsRareCorpse(longDesc) ? null : "Loot Only Rare Corpses: it didn't generate a rare.";
}
/// <summary>A rare still in container <paramref name="corpseId"/> (its contents as the client last listed
/// them), or null. Chests are not asked: they regenerate and are never marked done.</summary>
private WorldObject? RareStillOn(int corpseId)
{
if (_objectCache == null || corpseId == 0 || IsChestLoot(corpseId))
return null;
foreach (WorldObject item in _objectCache.GetContainedItems(corpseId))
if (RareItems.IsRare(Host, item, out _) && !ObjectGoneFromClient(item.Id))
return item;
return null;
}
private string CorpseNameFor(int corpseId)
=> _objectCache?[corpseId]?.Name is { Length: > 0 } n ? n : $"corpse 0x{(uint)corpseId:X8}";
/// <summary>
/// Called where an opened corpse would be marked done. With a rare still on it (not your own death
/// corpse, which has its own retries), it is set aside for <see cref="RareLeftRetryMs"/> and looted
/// again instead, and true is returned; the caller then skips the completed mark. After
/// <see cref="RareLeftMaxRetries"/> such tries it says so in chat, naming the rare, and returns false.
/// </summary>
private bool SetAsideForRareLeft(int corpseId, long now)
{
WorldObject? rare = RareStillOn(corpseId);
if (rare == null || IsOwnDeathCorpseId(_dashboard?.Settings, corpseId))
{
_rareLeftRetries.Remove(corpseId);
return false;
}
string corpseName = CorpseNameFor(corpseId);
string rareName = string.IsNullOrWhiteSpace(rare.Name) ? $"0x{(uint)rare.Id:X8}" : rare.Name;
int tries = _rareLeftRetries.TryGetValue(corpseId, out int n) ? n + 1 : 1;
if (tries > RareLeftMaxRetries)
{
_rareLeftRetries.Remove(corpseId);
string why = IsPackFull() ? " No pack has a free slot." : string.Empty;
ChatLine($"[RynthAi] A rare is still on {corpseName}: {rareName}. Looted {RareLeftMaxRetries} more times and it stayed.{why} Take it by hand.");
LootDiag($"[RynthAi] Rare left: {rareName} 0x{(uint)rare.Id:X8} on 0x{(uint)corpseId:X8} after {RareLeftMaxRetries} retries; marking the corpse done.");
return false;
}
_rareLeftRetries[corpseId] = tries;
_corpseCooldownUntil[corpseId] = now + RareLeftRetryMs;
ChatLine($"[RynthAi] A rare is still on {corpseName}: {rareName}. Looting it again in {RareLeftRetryMs / 1000} s (try {tries} of {RareLeftMaxRetries}).");
LootDiag($"[RynthAi] Rare left: {rareName} 0x{(uint)rare.Id:X8} on 0x{(uint)corpseId:X8}; set aside {RareLeftRetryMs} ms, try {tries} of {RareLeftMaxRetries}.");
return true;
}
/// <summary>A corpse that generated a rare was written off without being opened: said in chat.</summary>
private void SayRareCorpseUnopened(int corpseId)
{
string longDesc = _objectCache?[corpseId]?.Values(StringValueKey.LongDesc, string.Empty) ?? string.Empty;
if (!IsRareCorpse(longDesc) || IsChestLoot(corpseId))
return;
ChatLine($"[RynthAi] {CorpseNameFor(corpseId)} generated a rare, but it could not be opened. Take it by hand.");
}
/// <summary>A corpse in loot range whose description says it generated a rare and that the loot rules
/// would open (not done, not set aside): the nearest one.</summary>
private bool TryFindRareCorpseInRange(double maxMeters, out WorldObject? corpse)
{
corpse = null;
if (_objectCache == null || _playerId == 0)
return false;
long now = CorpseNowMs;
int playerId = unchecked((int)_playerId);
double best = double.MaxValue;
foreach (WorldObject candidate in _objectCache.GetLandscapeObjects())
{
if (candidate.ObjectClass != AcObjectClass.Corpse || _completedCorpses.ContainsKey(candidate.Id))
continue;
if (_corpseCooldownUntil.TryGetValue(candidate.Id, out long until) && until > now)
continue;
if (!IsRareCorpse(candidate.Values(StringValueKey.LongDesc, string.Empty)))
continue;
double dist = _objectCache.Distance(playerId, candidate.Id);
if (dist > maxMeters || dist >= best)
continue;
if (!EvaluateCorpseLootDecision(candidate).ShouldLoot)
continue;
corpse = candidate;
best = dist;
}
return corpse != null;
}
/// <summary>Stop Looting When Pack Full lets a rare corpse through while any pack has a slot: the rare
/// goes first and the reserve check ends the corpse after it (as your own corpse is let through).</summary>
private bool RareCorpseBypassesPackGate(LegacyUiSettings settings, double maxMeters)
=> !IsPackFull() && TryFindRareCorpseInRange(maxMeters, out _);
}