Snapshot for release 2026.10.30.1 (internal 50ae062)

This commit is contained in:
Tom Bohar 2026-10-31 06:01:30 -04:00
commit 76aca938b6
52 changed files with 6242 additions and 60 deletions

View file

@ -0,0 +1,252 @@
// ============================================================================
// RynthDefective - Quantity/QtyWorld.cs
// What the quantity box (QuantityController) needs from the game, as an
// interface so the tests drive it offline (RynthDefective.Tests), and the
// engine host's side of it. Plugin pump thread only.
//
// Every call is behind its Has* check: splitting (SplitStackInternal, as old
// as the plugin API), giving with a count (GiveObjectTo, v62), vendor trading
// (v67), the server's refusals (v70) and the pack column's order (v80) are
// each used only where the engine has them. Nothing here needs more than the
// live release's API 92, and the Selection window itself needs v90.
// ============================================================================
using System;
using System.Collections.Generic;
using RynthCore.PluginSdk;
namespace RynthCore.Plugin.RynthDefective.Quantity;
internal enum QtyPackKind { Main, Side, Open }
/// <summary>A place a split can go: the main pack, a side pack, or the container you have open.</summary>
internal readonly record struct QtyPack(uint Id, string Name, int Capacity, int Count, QtyPackKind Kind)
{
/// <summary>Free item slots; -1 = unknown (the server decides).</summary>
public int Free => Capacity > 0 && Count >= 0 ? Math.Max(0, Capacity - Count) : -1;
/// <summary>In a sentence: "the main pack", "Sack", "Chest".</summary>
public string Where => Kind == QtyPackKind.Main ? "the main pack" : Name;
/// <summary>On a button: "Main pack", "Sack", "Chest (open)".</summary>
public string Label => Kind switch
{
QtyPackKind.Main => "Main pack",
QtyPackKind.Open => Name + " (open)",
_ => Name,
};
}
internal interface IQtyWorld
{
long NowMs { get; }
uint PlayerId { get; }
uint SelectedId { get; }
/// <summary>AC's selection before this one (0 = none or unknown).</summary>
uint PreviousSelectedId { get; }
/// <summary>The chest or corpse you have open (0 = none).</summary>
uint OpenContainerId { get; }
bool TryGetName(uint id, out string name);
/// <summary>The client knows the object.</summary>
bool Exists(uint id);
/// <summary>Someone to give to: a player or an NPC (a creature that isn't a monster).</summary>
bool CanReceive(uint id);
/// <summary>Where it is: its container and its wielder (0 = none). False = the client doesn't know it.</summary>
bool TryGetOwner(uint id, out uint container, out uint wielder);
/// <summary>Its stack size; 0 = unknown.</summary>
int StackOf(uint id);
/// <summary>The main pack first, then the side packs (the pack column's order), then the open container.</summary>
IReadOnlyList<QtyPack> Packs();
bool CanSplit { get; }
/// <summary>Splits <paramref name="n"/> off the stack into <paramref name="into"/> (a new stack there).</summary>
bool Split(uint id, uint into, int n);
bool CanGive { get; }
/// <summary>Gives <paramref name="n"/> of it (0 = the whole of it) to a player or NPC.</summary>
bool Give(uint id, uint to, int n);
/// <summary>Counts the server's refusals (0 when the engine can't say): note it before a send, compare after.</summary>
int RefusalSeq { get; }
bool LastRefusal(out uint error, out uint eventType, out uint objectId);
bool CanTrade { get; }
/// <summary>The vendor open now (false = none).</summary>
bool TryGetVendor(out VendorInfo info);
IReadOnlyList<VendorItem> VendorItems();
/// <summary>Asks the engine to buy; the request id, or 0 when it refused at once (the trade status says why).</summary>
uint Buy(uint vendorId, uint itemId, int n);
bool TryGetTradeStatus(out VendorTradeStatus status);
/// <summary>Free container slots (for packs and foci bought); -1 = unknown.</summary>
int FreeContainerSlots();
/// <summary>Burden you can still carry (three times your capacity, as ACE); -1 = unknown.</summary>
long FreeBurden();
}
/// <summary>The engine host's side (RynthCoreHost). Plugin pump thread only.</summary>
internal sealed class HostQtyWorld : IQtyWorld
{
// STypeInt
private const uint EncumbranceVal = 5, ItemsCapacity = 6, ContainersCapacity = 7, StackSize = 12, EncumbranceCapacity = 96;
private const uint ItemTypeCreature = 0x10, ItemTypeContainer = 0x200;
// PublicWeenieDesc bitfield (ObjectDescriptionFlag).
private const uint BitPlayer = 0x8, BitAttackable = 0x10;
private const int MainPackDefault = 102, ContainerSlotsDefault = 7;
private readonly RynthCoreHost _h;
private uint[] _contents = new uint[256];
private uint[] _order = new uint[16];
private uint _vendorId, _vendorGeneration;
private VendorItem[] _vendorItems = Array.Empty<VendorItem>();
public HostQtyWorld(RynthCoreHost host) => _h = host;
public long NowMs => Environment.TickCount64;
public uint PlayerId => _h.HasGetPlayerId ? _h.GetPlayerId() : 0;
public uint SelectedId => _h.HasGetSelectedItemId ? _h.GetSelectedItemId() : 0;
public uint PreviousSelectedId => _h.HasGetPreviousSelectedItemId ? _h.GetPreviousSelectedItemId() : 0;
public uint OpenContainerId => _h.HasGetGroundContainerId ? _h.GetGroundContainerId() : 0;
public bool TryGetName(uint id, out string name)
{
name = string.Empty;
if (id == 0 || !_h.TryGetObjectName(id, out string n) || string.IsNullOrWhiteSpace(n)) return false;
name = n.Trim();
return true;
}
public bool Exists(uint id) => id != 0 && _h.TryGetItemType(id, out _);
/// <summary>
/// A creature that is a player (description bit Player 0x8) or isn't attackable (Attackable 0x10: a
/// monster): a monster selected in a fight never becomes the give target. Without the description
/// bits (an older engine) any creature; the server refuses a give it doesn't take.
/// </summary>
public bool CanReceive(uint id)
{
if (id == 0 || !_h.TryGetItemType(id, out uint t) || (t & ItemTypeCreature) == 0) return false;
if (!_h.HasGetObjectBitfield || !_h.TryGetObjectBitfield(id, out uint bits)) return true;
return (bits & BitPlayer) != 0 || (bits & BitAttackable) == 0;
}
public bool TryGetOwner(uint id, out uint container, out uint wielder)
{
container = wielder = 0;
if (id == 0) return false;
if (_h.HasGetObjectOwnershipInfo && _h.TryGetObjectOwnershipInfo(id, out container, out wielder, out _)) return true;
return false;
}
public int StackOf(uint id) => id != 0 && _h.TryGetObjectIntProperty(id, StackSize, out int s) && s > 0 ? s : 0;
private int Int(uint id, uint stype) => _h.TryGetObjectIntProperty(id, stype, out int v) ? v : -1;
public IReadOnlyList<QtyPack> Packs()
{
var packs = new List<QtyPack>(8);
uint player = PlayerId;
if (player == 0) return packs;
int mainCap = Int(player, ItemsCapacity);
packs.Add(new QtyPack(player, "Main pack", mainCap > 0 ? mainCap : MainPackDefault, Count(player), QtyPackKind.Main));
// The side packs, in the pack column's order when the engine knows it (v80), else as the client lists them.
var ids = new List<uint>(8);
if (_h.HasContainerOrder && _h.TryGetContainerOrder(player, true, ref _order, out int ordered) && ordered > 0)
for (int i = 0; i < ordered && i < _order.Length; i++) ids.Add(_order[i]);
else if (_h.HasGetContainerContents)
{
int n = _h.GetContainerContents(player, _contents);
if (n > _contents.Length && n <= 4096) { _contents = new uint[n]; n = _h.GetContainerContents(player, _contents); }
for (int i = 0; i < n && i < _contents.Length; i++) ids.Add(_contents[i]);
}
var names = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (uint id in ids)
{
if (id == 0 || !_h.TryGetItemType(id, out uint type) || (type & ItemTypeContainer) == 0) continue;
int cap = Int(id, ItemsCapacity);
if (cap <= 0) continue; // a focus holds no items
if (TryGetOwner(id, out uint c, out uint w) && (c != player || w != 0)) continue;
string name = TryGetName(id, out string nm) ? nm : "Pack";
names[name] = names.TryGetValue(name, out int seen) ? seen + 1 : 1;
if (names[name] > 1) name += " " + names[name];
packs.Add(new QtyPack(id, name, cap, Count(id), QtyPackKind.Side));
}
uint open = OpenContainerId;
if (open != 0 && open != player && !packs.Exists(p => p.Id == open))
{
string name = TryGetName(open, out string nm) ? nm : "The open container";
packs.Add(new QtyPack(open, name, Int(open, ItemsCapacity), Count(open), QtyPackKind.Open));
}
return packs;
}
private int Count(uint container) => _h.HasGetNumContainedItems ? _h.GetNumContainedItems(container) : -1;
public bool CanSplit => _h.HasSplitStackInternal;
// Slot 0 with a count below the stack: the client's StackableSplitToContainer, as the engine's own
// Inventory panel splits (InventoryModel). The new stack takes a free slot of the target.
public bool Split(uint id, uint into, int n) => CanSplit && n >= 1 && _h.SplitStackInternal(id, into, 0, n);
public bool CanGive => _h.HasGiveObjectTo;
public bool Give(uint id, uint to, int n) => CanGive && _h.GiveObjectTo(id, to, Math.Max(0, n));
public int RefusalSeq => _h.HasGetLastWeenieError && _h.TryGetLastWeenieError(out int seq, out _, out _, out _) ? seq : 0;
public bool LastRefusal(out uint error, out uint eventType, out uint objectId)
{
error = eventType = objectId = 0;
return _h.HasGetLastWeenieError && _h.TryGetLastWeenieError(out _, out error, out eventType, out objectId);
}
public bool CanTrade => _h.HasVendorTrade;
public bool TryGetVendor(out VendorInfo info)
{
info = null!;
return CanTrade && _h.TryGetVendorInfo(out info) && info.VendorId != 0;
}
/// <summary>The open vendor's list, read again only when the vendor or its list (Generation) changed.</summary>
public IReadOnlyList<VendorItem> VendorItems()
{
if (!TryGetVendor(out VendorInfo info))
{
_vendorId = 0;
_vendorItems = Array.Empty<VendorItem>();
return _vendorItems;
}
if (info.VendorId != _vendorId || info.Generation != _vendorGeneration || _vendorItems.Length == 0)
{
_vendorItems = _h.GetVendorItems();
_vendorId = info.VendorId;
_vendorGeneration = info.Generation;
}
return _vendorItems;
}
public uint Buy(uint vendorId, uint itemId, int n) => CanTrade ? _h.VendorBuy(itemId, n, vendorId) : 0;
public bool TryGetTradeStatus(out VendorTradeStatus status)
{
status = null!;
return CanTrade && _h.TryGetVendorTradeStatus(out status);
}
public int FreeContainerSlots()
{
uint player = PlayerId;
if (player == 0 || !_h.HasGetNumContainedContainers) return -1;
int n = _h.GetNumContainedContainers(player);
if (n < 0) return -1;
int cap = Int(player, ContainersCapacity);
return Math.Max(0, (cap > 0 ? cap : ContainerSlotsDefault) - n);
}
public long FreeBurden()
{
uint player = PlayerId;
if (player == 0) return -1;
int carried = Int(player, EncumbranceVal), cap = Int(player, EncumbranceCapacity);
if (carried < 0 || cap <= 0) return -1; // unknown: the engine checks with its own reading
return Math.Max(0, 3L * cap - carried);
}
}

View file

@ -0,0 +1,552 @@
// ============================================================================
// RynthDefective - Quantity/QuantityController.cs
// The quantity box (2026-10-30): one number, and what it does to what is
// selected. Asked for by a player: "to select a portion of a stack, or buy
// multiple of an item from a vendor, I need a place to type in the number".
//
// a stack you carry Split N into the main pack, a side pack or the
// (or in the chest you have container you have open; Give N to the player
// open) or NPC you selected last
// an item in a vendor's list Buy N, with the price, your pyreals, the room
// in your packs and your burden checked first
//
// The Selection window shows the box under what is selected (QuantityRow);
// /rdef qty, split, give and buy do the same from chat, hotkeys and metas.
// Nothing here needs the engine's Inventory panel or RynthInventory.
//
// One action at a time. Each one answers once: at once when it can't go
// (why, in the player's words), or when the game answers (the stack got
// smaller, the item left your packs, the vendor's trade finished) or doesn't
// (a refusal from the server, or no answer in time).
//
// Threads: everything on the plugin pump, except Value and Into, which the
// Selection window sets from the render thread (single ints).
// ============================================================================
using System;
using System.Collections.Generic;
using System.Threading;
using RynthCore.Plugin.RynthDefective.Model;
using RynthCore.PluginSdk;
namespace RynthCore.Plugin.RynthDefective.Quantity;
internal enum QtyMode { None, Single, Stack, Vendor }
/// <summary>Where the selected item is, for a split or a give.</summary>
internal enum QtyWhere { Unknown, Carried, Wielded, Open, Elsewhere }
/// <summary>What is selected, as the box sees it.</summary>
internal sealed record QtySubject(QtyMode Mode, uint Id, string Name, int Stack, QtyWhere Where)
{
public static readonly QtySubject Nothing = new(QtyMode.None, 0, string.Empty, 0, QtyWhere.Unknown);
/// <summary>A vendor's item: its price and what you have.</summary>
public VendorQuote? Quote { get; init; }
public uint VendorId { get; init; }
/// <summary>The most you can buy now (vendor items).</summary>
public int MaxBuy { get; init; }
/// <summary>What the box's "max" is of: the whole stack, or the most you can buy; 0 = nothing.</summary>
public int BoxMax => Mode switch
{
QtyMode.Stack => Stack,
QtyMode.Vendor => MaxBuy,
_ => 0,
};
}
/// <summary>The box as the Selection window draws it (built on the tick).</summary>
internal sealed record QuantityView(QtySubject Subject, QtyPack[] Targets, int TargetIndex, uint GiveTo, string GiveName,
bool Busy, bool CanSplit, bool CanGive)
{
public QtyPack? Target => TargetIndex >= 0 && TargetIndex < Targets.Length ? Targets[TargetIndex] : null;
}
/// <summary>The last thing the box did, for the Selection window's line.</summary>
internal sealed record QtyStatus(string Text, bool Ok, long AtMs);
internal sealed class QuantityController
{
/// <summary>How long a split, a give (the server may walk you to the NPC first) and a buy wait for the game.</summary>
public const long SplitAnswerMs = 6000, GiveAnswerMs = 20_000, BuyAnswerMs = 15_000;
/// <summary>A refusal that doesn't name the item is ours if nothing changed this long after it.</summary>
public const long RefusalGraceMs = 1500;
/// <summary>The last player or NPC selected stays the give target this long.</summary>
public const long GiveTargetMs = 10 * 60 * 1000;
public const long StatusShownMs = 8000;
/// <summary>InventoryServerSaveFailed: the server refused a move, split or give of this item.</summary>
private const uint InventorySaveFailed = 0x00A0;
private int _value = 1;
private uint _into;
private volatile QtyStatus? _status;
private uint _lastSelection, _giveId;
private string _giveName = string.Empty;
private long _giveAtMs;
private Pending? _pending;
/// <summary>Every answer (ok, text): the plugin writes it in chat.</summary>
public Action<bool, string> Report = (_, _) => { };
/// <summary>The box's number (1 .. MaxAmount). Any thread.</summary>
public int Value
{
get => Volatile.Read(ref _value);
set => Volatile.Write(ref _value, Math.Clamp(value, 1, QuantityRules.MaxAmount));
}
/// <summary>The pack a split goes into (its id); 0 or a pack that's gone = the main pack. Any thread.</summary>
public uint Into
{
get => Volatile.Read(ref _into);
set => Volatile.Write(ref _into, value);
}
public bool Busy => _pending != null;
/// <summary>The last answer while it is fresh. Any thread.</summary>
public QtyStatus? StatusAt(long nowMs)
{
QtyStatus? s = _status;
return s != null && nowMs - s.AtMs < StatusShownMs ? s : null;
}
public void Reset()
{
_pending = null;
_status = null;
_giveId = _lastSelection = 0;
_giveName = string.Empty;
Into = 0;
}
private void Say(IQtyWorld w, bool ok, string text)
{
_status = new QtyStatus(text, ok, w.NowMs);
Report(ok, text);
}
// ── What is selected ────────────────────────────────────────────────────
public QtySubject Analyze(IQtyWorld w)
{
uint sel = w.SelectedId;
if (sel == 0) return QtySubject.Nothing;
string name = w.TryGetName(sel, out string n) ? n : $"0x{sel:X8}";
if (w.CanTrade && w.TryGetVendor(out VendorInfo info))
{
foreach (VendorItem it in w.VendorItems())
{
if (it.ObjectId != sel) continue;
VendorQuote q = Quote(w, info, it);
return new QtySubject(QtyMode.Vendor, sel, it.Name.Length > 0 ? it.Name : name, Math.Max(1, it.StackSize), QtyWhere.Elsewhere)
{
Quote = q,
VendorId = info.VendorId,
MaxBuy = q.MaxBuy(),
};
}
}
int stack = w.StackOf(sel);
return new QtySubject(stack >= 2 ? QtyMode.Stack : QtyMode.Single, sel, name, Math.Max(1, stack), WhereIs(w, sel));
}
private static QtyWhere WhereIs(IQtyWorld w, uint id)
{
uint player = w.PlayerId;
if (player == 0 || !w.TryGetOwner(id, out uint c, out uint wielder)) return QtyWhere.Unknown;
if (wielder != 0 && wielder == player) return QtyWhere.Wielded;
if (c == player) return QtyWhere.Carried;
if (c != 0 && w.TryGetOwner(c, out uint outer, out uint outerWielder) && outer == player && outerWielder == 0) return QtyWhere.Carried;
if (c != 0 && c == w.OpenContainerId) return QtyWhere.Open;
return QtyWhere.Elsewhere;
}
private static bool Owned(IQtyWorld w, uint id)
{
QtyWhere where = WhereIs(w, id);
return where is QtyWhere.Carried or QtyWhere.Wielded;
}
private static VendorQuote Quote(IQtyWorld w, VendorInfo info, VendorItem item)
{
int free = 0;
foreach (QtyPack p in w.Packs())
{
if (p.Kind == QtyPackKind.Open) continue;
if (p.Free < 0) { free = -1; break; }
free += p.Free;
}
return VendorQuote.From(info, item, free, w.FreeContainerSlots(), w.FreeBurden());
}
/// <summary>
/// The Selection window's box: for a vendor's item, or a stack you carry, wield (it says to take it
/// off) or have in the open container. Not for a stack on the ground or in someone else's hands. Plugin tick.
/// </summary>
public QuantityView? View(IQtyWorld w)
{
QtySubject s = Analyze(w);
if (s.Mode is not (QtyMode.Stack or QtyMode.Vendor)) return null;
if (s.Mode == QtyMode.Stack && s.Where is not (QtyWhere.Carried or QtyWhere.Wielded or QtyWhere.Open)) return null;
QtyPack[] targets = s.Mode == QtyMode.Stack ? new List<QtyPack>(w.Packs()).ToArray() : Array.Empty<QtyPack>();
int index = 0;
uint into = Into;
for (int i = 0; i < targets.Length; i++) if (targets[i].Id == into) { index = i; break; }
TryGiveTarget(w, s.Id, out uint to, out string toName);
return new QuantityView(s, targets, targets.Length == 0 ? -1 : index, to, toName, Busy, w.CanSplit, w.CanGive);
}
// ── The give target: the last player or NPC selected ─────────────────────
/// <summary>Notes a newly selected player or NPC (not you, not a monster) as the give target. Plugin tick, a few times a second.</summary>
public void NoteSelection(IQtyWorld w)
{
uint sel = w.SelectedId;
if (sel == _lastSelection) return;
_lastSelection = sel;
if (sel == 0 || sel == w.PlayerId || !w.CanReceive(sel)) return;
_giveId = sel;
_giveName = w.TryGetName(sel, out string n) ? n : $"0x{sel:X8}";
_giveAtMs = w.NowMs;
}
/// <summary>Who a give goes to: the last player or NPC selected (within GiveTargetMs and still there), else AC's previous selection.</summary>
public bool TryGiveTarget(IQtyWorld w, uint item, out uint to, out string name)
{
uint player = w.PlayerId;
if (_giveId != 0 && _giveId != item && w.NowMs - _giveAtMs < GiveTargetMs && w.Exists(_giveId))
{
to = _giveId;
name = _giveName;
return true;
}
uint prev = w.PreviousSelectedId;
if (prev != 0 && prev != item && prev != player && w.CanReceive(prev))
{
to = prev;
name = w.TryGetName(prev, out string n) ? n : $"0x{prev:X8}";
return true;
}
to = 0;
name = string.Empty;
return false;
}
// ── /rdef qty ───────────────────────────────────────────────────────────
/// <summary>Sets the box (null: says what it is). "max" and "half" are of what is selected.</summary>
public void SetBox(IQtyWorld w, QtyAmount? amount)
{
QtySubject s = Analyze(w);
if (amount is { } a)
{
if (a.Word != QtyWord.Number && s.BoxMax < 1)
{
Say(w, false, s.Mode == QtyMode.Vendor ? s.Quote?.Refusal(1) ?? QtyReplies.MaxNeedsSelection : QtyReplies.MaxNeedsSelection);
return;
}
Value = QuantityRules.ForBox(a, s.BoxMax);
}
Say(w, true, $"Quantity {QuantityRules.N(Value)}{About(s)}.");
}
private string About(QtySubject s) => s.Mode switch
{
QtyMode.Stack => $" (the selected {s.Name}: a stack of {QuantityRules.N(s.Stack)}; a split takes up to {QuantityRules.N(s.Stack - 1)})",
QtyMode.Vendor when s.Quote != null => $" (the vendor's {s.Name}: {s.Quote.PriceLine(Value)}; you can buy {QuantityRules.N(s.MaxBuy)})",
_ => string.Empty,
};
// ── Split ───────────────────────────────────────────────────────────────
/// <summary>
/// Splits the box's number (or <paramref name="amount"/>) off the selected stack into
/// <paramref name="into"/> (a pack's name; empty = the pack chosen in the window, else the main pack).
/// </summary>
public void Split(IQtyWorld w, QtyAmount? amount, string into)
{
if (_pending != null) { Say(w, false, QtyReplies.Busy); return; }
if (!w.CanSplit) { Say(w, false, QtyReplies.NoSplitApi); return; }
QtySubject s = Analyze(w);
string? why = s.Mode switch
{
QtyMode.None => QtyReplies.NothingSelected,
QtyMode.Vendor => QtyReplies.VendorsItem(s.Name),
QtyMode.Single => QtyReplies.NotAStack(s.Name),
_ => s.Where switch
{
QtyWhere.Wielded => QtyReplies.Wielded(s.Name),
QtyWhere.Carried or QtyWhere.Open => null,
_ => QtyReplies.NotReachable(s.Name),
},
};
if (why != null) { Say(w, false, why); return; }
IReadOnlyList<QtyPack> packs = w.Packs();
if (!TryPickTarget(packs, into, out QtyPack target, out why)) { Say(w, false, why); return; }
if (target.Free == 0) { Say(w, false, QtyReplies.PackFull(target.Where)); return; }
int n = QuantityRules.ForSplit(amount ?? QtyAmount.Of(Value), s.Stack);
var p = new Pending
{
Kind = PendingKind.Split, Id = s.Id, Name = s.Name, N = n, Where = target.Where,
StackBefore = s.Stack, RefusalSeq = w.RefusalSeq, AtMs = w.NowMs,
};
if (!w.Split(s.Id, target.Id, n)) { Say(w, false, QtyReplies.NotSent("split")); return; }
_pending = p;
}
/// <summary>The split's pack: the one named, else the one chosen in the window (if it's still there), else the main pack.</summary>
internal bool TryPickTarget(IReadOnlyList<QtyPack> packs, string typed, out QtyPack target, out string why)
{
target = default;
why = string.Empty;
if (packs.Count == 0) { why = "Your packs can't be read yet: try again in a moment."; return false; }
string t = (typed ?? string.Empty).Trim();
if (t.Length == 0)
{
uint into = Into;
target = packs[0];
foreach (QtyPack p in packs) if (p.Id == into) { target = p; break; }
return true;
}
string low = t.ToLowerInvariant();
if (low is "main" or "main pack" or "mainpack" or "pack" or "me" or "inventory")
{
target = packs[0];
return true;
}
if (low is "open" or "chest" or "container" or "the chest" or "the open container")
{
foreach (QtyPack p in packs) if (p.Kind == QtyPackKind.Open) { target = p; return true; }
why = "No chest or other container is open: open it first, or split into one of your packs.";
return false;
}
// The pack's own name: the whole of it, then its start, then any part of it.
foreach (QtyPack p in packs) if (p.Name.Equals(t, StringComparison.OrdinalIgnoreCase) || p.Label.Equals(t, StringComparison.OrdinalIgnoreCase)) { target = p; return true; }
foreach (QtyPack p in packs) if (p.Name.StartsWith(t, StringComparison.OrdinalIgnoreCase)) { target = p; return true; }
foreach (QtyPack p in packs) if (p.Name.Contains(t, StringComparison.OrdinalIgnoreCase)) { target = p; return true; }
var names = new List<string>(packs.Count);
foreach (QtyPack p in packs) names.Add(p.Label);
why = QtyReplies.NoSuchPack(t, string.Join(", ", names));
return false;
}
// ── Give ────────────────────────────────────────────────────────────────
/// <summary>Gives the box's number (or <paramref name="amount"/>) of the selected stack to the last player or NPC selected.</summary>
public void Give(IQtyWorld w, QtyAmount? amount)
{
if (_pending != null) { Say(w, false, QtyReplies.Busy); return; }
if (!w.CanGive) { Say(w, false, QtyReplies.NoGiveApi); return; }
QtySubject s = Analyze(w);
string? why = s.Mode switch
{
QtyMode.None => QtyReplies.NothingSelected,
QtyMode.Vendor => QtyReplies.VendorsItem(s.Name),
_ => s.Where switch
{
QtyWhere.Wielded => QtyReplies.Wielded(s.Name),
QtyWhere.Carried => null,
_ => QtyReplies.NotCarried(s.Name),
},
};
if (why != null) { Say(w, false, why); return; }
if (!TryGiveTarget(w, s.Id, out uint to, out string toName)) { Say(w, false, QtyReplies.NoGiveTarget); return; }
int n = QuantityRules.ForGive(amount ?? QtyAmount.Of(Value), s.Stack);
var p = new Pending
{
Kind = PendingKind.Give, Id = s.Id, Name = s.Name, N = n, Where = toName,
StackBefore = s.Stack, RefusalSeq = w.RefusalSeq, AtMs = w.NowMs,
};
// 0 = the whole of it (the give the client sends for a drag of the whole stack).
if (!w.Give(s.Id, to, n >= s.Stack ? 0 : n)) { Say(w, false, QtyReplies.NotSent("give")); return; }
_pending = p;
}
// ── Buy ─────────────────────────────────────────────────────────────────
/// <summary>Buys the box's number (or <paramref name="amount"/>) of the selected vendor item, or the one named.</summary>
public void Buy(IQtyWorld w, QtyAmount? amount, string itemName)
{
if (_pending != null) { Say(w, false, QtyReplies.Busy); return; }
if (!w.CanTrade) { Say(w, false, QtyReplies.NoTradeApi); return; }
if (!w.TryGetVendor(out VendorInfo info)) { Say(w, false, QtyReplies.NoVendor); return; }
if (!info.TradingAvailable) { Say(w, false, QtyReplies.VendorUnreadable); return; }
if (info.TradeInFlight) { Say(w, false, QtyReplies.VendorBusy); return; }
IReadOnlyList<VendorItem> items = w.VendorItems();
VendorItem? item;
string name = (itemName ?? string.Empty).Trim();
if (name.Length > 0)
{
item = FindVendorItem(items, name, out string? why);
if (item == null) { Say(w, false, why!); return; }
}
else
{
item = null;
uint sel = w.SelectedId;
foreach (VendorItem it in items) if (it.ObjectId == sel && sel != 0) { item = it; break; }
if (item == null) { Say(w, false, QtyReplies.NoVendorItem); return; }
}
VendorQuote q = Quote(w, info, item);
int n = amount is { } a ? QuantityRules.ForBuy(a, q.MaxBuy()) : Value;
if (n < 1) { Say(w, false, q.Refusal(1) ?? $"You can't buy any {item.Name} now."); return; }
string? refusal = q.Refusal(n);
if (refusal != null) { Say(w, false, refusal); return; }
uint request = w.Buy(info.VendorId, item.ObjectId, n);
if (request == 0)
{
Say(w, false, QtyReplies.VendorRefusal(w.TryGetTradeStatus(out VendorTradeStatus st) ? st.Message : null));
return;
}
_pending = new Pending
{
Kind = PendingKind.Buy, Id = item.ObjectId, Name = item.Name, N = n, RequestId = request,
Cost = q.Cost(n), Currency = q.Currency, AtMs = w.NowMs,
};
}
/// <summary>A vendor's item by name: the whole name, else the one whose name starts so, else the one that holds it. Several = ask for more.</summary>
internal static VendorItem? FindVendorItem(IReadOnlyList<VendorItem> items, string name, out string? why)
{
why = null;
foreach (VendorItem it in items) if (it.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) return it;
foreach (bool starts in new[] { true, false })
{
VendorItem? found = null;
var names = new List<string>();
foreach (VendorItem it in items)
{
bool match = starts ? it.Name.StartsWith(name, StringComparison.OrdinalIgnoreCase) : it.Name.Contains(name, StringComparison.OrdinalIgnoreCase);
if (!match) continue;
if (!names.Exists(x => x.Equals(it.Name, StringComparison.OrdinalIgnoreCase))) names.Add(it.Name);
found ??= it;
}
if (names.Count == 1) return found;
if (names.Count > 1)
{
why = $"'{name}' could be {string.Join(", ", names.GetRange(0, Math.Min(5, names.Count)))}{(names.Count > 5 ? ", ..." : "")}: type more of the name.";
return null;
}
}
why = $"The vendor doesn't sell '{name}'.";
return null;
}
// ── Waiting for the game ────────────────────────────────────────────────
private enum PendingKind { Split, Give, Buy }
private sealed class Pending
{
public PendingKind Kind;
public uint Id;
public string Name = string.Empty;
public int N;
/// <summary>Split: the pack ("the main pack"); give: who to.</summary>
public string Where = string.Empty;
public int StackBefore;
public int RefusalSeq;
public long AtMs;
public long RefusedAtMs;
public uint Error;
public bool RefusalNamesItem;
public uint RequestId;
public long Cost;
public string Currency = string.Empty;
}
/// <summary>Plugin tick: the give target, and the answer to the action in flight.</summary>
public void Tick(IQtyWorld w)
{
NoteSelection(w);
Pending? p = _pending;
if (p == null) return;
long now = w.NowMs;
switch (p.Kind)
{
case PendingKind.Split:
{
int stack = w.StackOf(p.Id);
if (stack > 0 && stack < p.StackBefore) Finish(w, true, QtyReplies.Splitting(p.N, p.Name, p.Where));
else if (RefusedNow(w, p, now)) Finish(w, false, QtyReplies.Refused("split", p.Error));
else if (now - p.AtMs >= SplitAnswerMs) Finish(w, false, QtyReplies.NoAnswer("split"));
break;
}
case PendingKind.Give:
{
int stack = w.StackOf(p.Id);
bool left = !w.Exists(p.Id) || !Owned(w, p.Id);
if (left || (stack > 0 && stack < p.StackBefore)) Finish(w, true, QtyReplies.Gave(p.N, p.Name, p.Where));
else if (RefusedNow(w, p, now)) Finish(w, false, QtyReplies.Refused("give", p.Error));
else if (now - p.AtMs >= GiveAnswerMs) Finish(w, false, QtyReplies.NoAnswer("give"));
break;
}
case PendingKind.Buy:
WatchBuy(w, p, now);
break;
}
}
private void WatchBuy(IQtyWorld w, Pending p, long now)
{
if (w.TryGetTradeStatus(out VendorTradeStatus s))
{
if (s.RequestId == p.RequestId)
{
if (s.State == VendorTradeState.Refused) { Finish(w, false, QtyReplies.VendorRefusal(s.Message)); return; }
if (s.State == VendorTradeState.Done)
{
if (s.Result == VendorTradeResult.Accepted)
Finish(w, true, QtyReplies.Bought(p.N, p.Name, s.Estimate > 0 ? s.Estimate : p.Cost, p.Currency));
else if (s.Result == VendorTradeResult.TimedOut)
Finish(w, false, $"No answer from the vendor about {QuantityRules.N(p.N)} {p.Name}: check your pack and {p.Currency} before trying again.");
else
Finish(w, false, $"The vendor didn't sell {QuantityRules.N(p.N)} {p.Name}: the server said no (money, room, or too busy).");
return;
}
}
else if (s.RequestId > p.RequestId)
{
// Another plugin's trade came after ours: the engine keeps only the newest one's status.
Finish(w, true, $"Asked the vendor for {QuantityRules.N(p.N)} {p.Name}; another trade followed it, so check your pack.");
return;
}
}
if (now - p.AtMs >= BuyAnswerMs)
Finish(w, false, $"No answer about {QuantityRules.N(p.N)} {p.Name}: check your pack and {p.Currency} before trying again.");
}
/// <summary>
/// The server refused it: a refusal naming the item (InventoryServerSaveFailed) at once, any other
/// new refusal once nothing changed for RefusalGraceMs after it (a fight's own errors pass by).
/// </summary>
private static bool RefusedNow(IQtyWorld w, Pending p, long now)
{
int seq = w.RefusalSeq;
if (p.RefusedAtMs == 0 && seq != 0 && seq != p.RefusalSeq && w.LastRefusal(out uint error, out uint eventType, out uint obj))
{
p.RefusalSeq = seq;
bool names = eventType == InventorySaveFailed && obj != 0;
if (names && obj != p.Id) return false; // another item's
p.RefusedAtMs = now;
p.Error = error;
p.RefusalNamesItem = names;
}
return p.RefusedAtMs != 0 && (p.RefusalNamesItem || now - p.RefusedAtMs >= RefusalGraceMs);
}
private void Finish(IQtyWorld w, bool ok, string text)
{
_pending = null;
Say(w, ok, text);
}
}