|
|
@ -1,17 +1,15 @@ |
|
|
|
#include "inventory.hpp" |
|
|
|
#include "inventory.hpp" |
|
|
|
|
|
|
|
|
|
|
|
namespace inventory { |
|
|
|
namespace inventory { |
|
|
|
void Model::add(const Slot &in_slot, DinkyECS::Entity ent) { |
|
|
|
bool Model::add(const Slot &in_slot, DinkyECS::Entity ent) { |
|
|
|
if(by_entity.contains(ent)) { |
|
|
|
invariant(); |
|
|
|
// doing it this way so we can get the offending entity, otherwise it
|
|
|
|
|
|
|
|
// crashes on the by_entity.at when this test _passes_
|
|
|
|
if(by_slot.contains(in_slot)) return false; |
|
|
|
dbc::sentinel(fmt::format("failed to add item to inventory, entity {} is already in the inventory slot {}", ent, by_entity.at(ent))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
by_entity.insert_or_assign(ent, in_slot); |
|
|
|
by_entity.insert_or_assign(ent, in_slot); |
|
|
|
by_slot.insert_or_assign(in_slot, ent); |
|
|
|
by_slot.insert_or_assign(in_slot, ent); |
|
|
|
|
|
|
|
|
|
|
|
invariant(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Slot& Model::get(DinkyECS::Entity ent) { |
|
|
|
Slot& Model::get(DinkyECS::Entity ent) { |
|
|
@ -49,9 +47,6 @@ namespace inventory { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Model::invariant() { |
|
|
|
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) { |
|
|
|
for(auto& [slot, ent] : by_slot) { |
|
|
|
dbc::check(by_entity.at(ent) == slot, |
|
|
|
dbc::check(by_entity.at(ent) == slot, |
|
|
|
fmt::format("mismatched slot {} in by_slot doesn't match entity {}", slot, ent)); |
|
|
|
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, |
|
|
|
dbc::check(by_slot.at(slot) == ent, |
|
|
|
fmt::format("mismatched entity {} in by_entity doesn't match entity {}", ent, slot)); |
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|