So far most of the bugs are solved but there's still some edge cases in the inventory dance.

This commit is contained in:
Zed A. Shaw 2025-06-22 23:54:50 -04:00
parent e0588847fa
commit 3c5021e4c9
6 changed files with 40 additions and 25 deletions

View file

@ -1,17 +1,15 @@
#include "inventory.hpp"
namespace inventory {
void Model::add(const Slot &in_slot, DinkyECS::Entity ent) {
if(by_entity.contains(ent)) {
// doing it this way so we can get the offending entity, otherwise it
// crashes on the by_entity.at when this test _passes_
dbc::sentinel(fmt::format("failed to add item to inventory, entity {} is already in the inventory slot {}", ent, by_entity.at(ent)));
}
bool Model::add(const Slot &in_slot, DinkyECS::Entity ent) {
invariant();
if(by_slot.contains(in_slot)) return false;
by_entity.insert_or_assign(ent, in_slot);
by_slot.insert_or_assign(in_slot, ent);
invariant();
return true;
}
Slot& Model::get(DinkyECS::Entity ent) {
@ -49,9 +47,6 @@ namespace inventory {
}
void Model::invariant() {
dbc::check(by_slot.size() == by_entity.size(), "by_slot and by_entity have differing sizes");
// std::unordered_map<DinkyECS::Entity, Slot> find_dupes;
for(auto& [slot, ent] : by_slot) {
dbc::check(by_entity.at(ent) == slot,
fmt::format("mismatched slot {} in by_slot doesn't match entity {}", slot, ent));
@ -61,5 +56,18 @@ namespace inventory {
dbc::check(by_slot.at(slot) == ent,
fmt::format("mismatched entity {} in by_entity doesn't match entity {}", ent, slot));
}
dbc::check(by_slot.size() == by_entity.size(), "by_slot and by_entity have differing sizes");
}
void Model::dump() {
invariant();
fmt::println("INVENTORY has {} slots, sizes equal? {}, contents:",
by_entity.size(), by_entity.size() == by_slot.size());
for(auto [slot, ent] : by_slot) {
fmt::println("slot={}, ent={}, both={}, equal={}",
slot, ent, by_entity.contains(ent), by_entity.at(ent) == slot);
}
}
}