189 lines
8.1 KiB
C++
189 lines
8.1 KiB
C++
#pragma once
|
|
|
|
#include "equipment_set_store.hpp"
|
|
#include "inventory_translation.hpp"
|
|
#include "gateway/turtle_entity_profile.hpp"
|
|
#include <chrono>
|
|
|
|
namespace gateway::equipment_sets {
|
|
using Clock = std::chrono::steady_clock;
|
|
struct Position { uint8_t bag, slot; Guid128 item; bool bank = false; };
|
|
using Carried = std::vector<Position>;
|
|
|
|
inline Carried accessible(const NativeInventoryTranslation& inv, bool bank_open) {
|
|
if (inv.player_fields.empty()) throw Error("owned inventory is pending");
|
|
const auto& bindings = inv.owned_bindings();
|
|
Carried result;
|
|
std::set<Guid128> seen;
|
|
auto add = [&](uint8_t bag, uint8_t slot, bool bank = false) {
|
|
auto pos = inv.position(bag, slot);
|
|
Guid128 item{};
|
|
if (pos.item) {
|
|
item = bindings.at(pos.item);
|
|
if (!seen.insert(item).second) throw Error("owned slot update incomplete");
|
|
}
|
|
result.push_back({pos.bag, pos.slot, item, bank});
|
|
};
|
|
for (uint8_t slot = 0; slot < Slots; ++slot) add(255, slot);
|
|
for (uint8_t slot = 35; slot < 51; ++slot) add(255, slot);
|
|
if (bank_open)
|
|
for (uint8_t slot = 0; slot < 24; ++slot) add(63, slot, true);
|
|
auto add_contents = [&](uint8_t bag, bool bank) {
|
|
auto id = inv.position(255, bag).item;
|
|
if (!id) return;
|
|
const auto& fields = inv.items.at(id).fields;
|
|
auto count = fields.find(turtle_entity_fields::CONTAINER_FIELD_NUM_SLOTS);
|
|
if (count == fields.end() || count->second > 36) throw Error("owned bag size unavailable");
|
|
for (unsigned slot = 0; slot < count->second; ++slot) add(bag, uint8_t(slot), bank);
|
|
};
|
|
for (uint8_t bag = 30; bag < 34; ++bag) add_contents(bag, false);
|
|
if (bank_open)
|
|
for (uint8_t bag = 64; bag < 70; ++bag) add_contents(bag, true);
|
|
return result;
|
|
}
|
|
|
|
struct Step {
|
|
LegacyPacket source;
|
|
size_t slot;
|
|
Guid128 expected, removed;
|
|
size_t source_position = SIZE_MAX;
|
|
bool from_bank = false, autostore = false;
|
|
};
|
|
struct Plan { std::optional<Step> first; uint32_t error = 0; };
|
|
// Preflight the entire set. Place requested items before reserving bag space
|
|
// for empty equipment slots, so cycles and a full backpack can still succeed.
|
|
inline Plan plan(const Set& s, Carried positions) {
|
|
std::optional<Step> first;
|
|
std::set<Guid128> requested;
|
|
for (size_t i = 0; i < Slots; ++i) {
|
|
auto wanted = s.pieces[i];
|
|
if ((s.ignore & (1u << i)) || wanted == Guid128{}) continue;
|
|
if (!requested.insert(wanted).second) return {{}, 1};
|
|
if (positions[i].item == wanted) continue;
|
|
auto from = std::ranges::find(positions, wanted, &Position::item);
|
|
if (from == positions.end()) return {{}, 1};
|
|
auto n = size_t(from - positions.begin());
|
|
if (n < Slots && (s.ignore & (1u << n))) return {{}, 1};
|
|
auto& to = positions[i];
|
|
if (!first) {
|
|
const auto displaced = to.item;
|
|
LegacyPacket p = from->bag == 255 && !from->bank
|
|
? LegacyPacket{0x10d, {from->slot, to.slot}}
|
|
: LegacyPacket{0x10c, {to.bag, to.slot, from->bag, from->slot}};
|
|
first = Step{std::move(p), i, wanted, displaced, n, from->bank, false};
|
|
}
|
|
std::swap(to.item, from->item);
|
|
}
|
|
for (size_t i = 0; i < Slots; ++i) {
|
|
if ((s.ignore & (1u << i)) || s.pieces[i] != Guid128{} || positions[i].item == Guid128{}) continue;
|
|
auto free = std::find_if(positions.begin() + Slots, positions.end(),
|
|
[](const Position& p) { return !p.bank && p.item == Guid128{}; });
|
|
if (free == positions.end()) return {{}, 4};
|
|
if (!first) first = Step{{0x10b, {255, uint8_t(i), 0}}, i, {}, positions[i].item,
|
|
i, false, true};
|
|
std::swap(free->item, positions[i].item);
|
|
}
|
|
return {first, 0};
|
|
}
|
|
struct Manager {
|
|
bool loaded = false, save_logged = false;
|
|
std::deque<LegacyPacket> replies;
|
|
struct Pending {
|
|
Set set;
|
|
std::optional<Step> next, waiting;
|
|
Clock::time_point deadline;
|
|
size_t sent = 0;
|
|
};
|
|
std::optional<Pending> pending;
|
|
void result(uint64_t guid, uint32_t reason) {
|
|
Bytes body;
|
|
le(body, reason, 4);
|
|
le(body, guid, 8);
|
|
replies.push_back({Result, std::move(body)});
|
|
}
|
|
void finish(uint32_t reason) {
|
|
result(pending->set.guid, reason);
|
|
pending.reset();
|
|
}
|
|
void expire(Clock::time_point now) {
|
|
if (pending && now >= pending->deadline) finish(1);
|
|
}
|
|
void advance(const Carried& inventory, Clock::time_point now) {
|
|
expire(now);
|
|
if (!pending) return;
|
|
if (auto& step = pending->waiting) {
|
|
if (step->slot >= inventory.size()) { finish(1); return; }
|
|
if (step->autostore) {
|
|
if (inventory[step->slot].item != step->expected ||
|
|
std::none_of(inventory.begin() + Slots, inventory.end(),
|
|
[&](const auto& p) { return !p.bank && p.item == step->removed; })) return;
|
|
} else {
|
|
if (step->source_position >= inventory.size()) { finish(1); return; }
|
|
if (inventory[step->slot].item != step->expected ||
|
|
inventory[step->source_position].item != step->removed) return;
|
|
}
|
|
step.reset();
|
|
}
|
|
auto next = plan(pending->set, inventory);
|
|
if (next.error || !next.first) { finish(next.error); return; }
|
|
if (pending->sent >= Slots * 2) { finish(1); return; }
|
|
pending->next = std::move(next.first);
|
|
}
|
|
void sent(Clock::time_point now) {
|
|
pending->waiting = std::move(pending->next);
|
|
pending->next.reset();
|
|
++pending->sent;
|
|
pending->deadline = now + std::chrono::seconds(8);
|
|
}
|
|
void handle(uint32_t op, std::span<const uint8_t> body, const std::filesystem::path& file,
|
|
const Carried& inventory, Clock::time_point now) {
|
|
if (!loaded || replies.size() >= 30) throw Error("equipment manager unavailable");
|
|
Reader r(body);
|
|
if (!save_logged && (op == Save || op == Delete) && body.size() > sizeof(uint64_t)) {
|
|
std::fprintf(stderr, "GWEquipmentSet save candidate opcode=%08x bytes=%zu\n", op, body.size());
|
|
save_logged = true;
|
|
}
|
|
if (op == Save) {
|
|
Set s = read_set(r);
|
|
r.end();
|
|
if (s.spec && (*s.spec < 0 || *s.spec >= 4)) throw Error("invalid equipment specialization");
|
|
for (size_t i = 0; i < Slots; ++i) {
|
|
if (s.ignore & (1u << i)) s.pieces[i] = {};
|
|
else if (s.pieces[i] != inventory[i].item) throw Error("equipment save differs from owned gear");
|
|
}
|
|
auto saved = save(file, std::move(s));
|
|
Bytes ack;
|
|
le(ack, 0, 4);
|
|
le(ack, saved.index, 4);
|
|
le(ack, saved.guid, 8);
|
|
replies.push_back({SetId, std::move(ack)});
|
|
} else if (op == Delete || op == Assign) {
|
|
uint64_t guid = r.le(8);
|
|
int32_t spec = op == Assign ? int32_t(r.le(4)) : -1;
|
|
r.end();
|
|
auto updated = change(file, guid, op == Delete, spec);
|
|
if (op == Delete && pending && pending->set.guid == guid) finish(1);
|
|
replies.push_back({Load, updated.packet()});
|
|
} else if (op == Use) {
|
|
auto header = r.le(1);
|
|
r.take(2 * (header >> 6));
|
|
std::array<Guid128, Slots> pieces;
|
|
for (auto& item : pieces) { item = piece(r); r.take(2); }
|
|
uint64_t guid = r.le(8);
|
|
r.end();
|
|
auto catalogue = load(file);
|
|
auto set = catalogue.find(guid);
|
|
if (pending || !set) { result(guid, 1); return; }
|
|
for (size_t i = 0; i < Slots; ++i) {
|
|
if (!(set->ignore & (1u << i)) && pieces[i] != set->pieces[i]) {
|
|
result(guid, 1);
|
|
return;
|
|
}
|
|
}
|
|
pending = Pending{*set, {}, {}, now + std::chrono::seconds(8)};
|
|
advance(inventory, now);
|
|
} else throw Error("unrecognized equipment request");
|
|
}
|
|
};
|
|
} // namespace gateway::equipment_sets
|