sots-engine/src/game/design/design.cpp

84 lines
2.3 KiB
C++

#include "game/design/design.h"
#include <algorithm>
namespace game::design {
std::string_view slot_name(Slot s) {
switch (s) {
case Slot::Command: return "command";
case Slot::Mission: return "mission";
case Slot::Engine: return "engine";
case Slot::Reserved3: return "slot3";
case Slot::Reserved4: return "slot4";
}
return "?";
}
std::optional<Slot> parse_slot(std::string_view s) {
for (Slot k : kUsedSlots)
if (data::iequals(slot_name(k), s)) return k;
return std::nullopt;
}
std::string WeaponRef::describe() const {
switch (kind) {
case Kind::Empty: return "(none)";
case Kind::Id: return "#" + std::to_string(id);
case Kind::File:
case Kind::Stem: return text;
}
return "?";
}
std::string SectionUse::describe() const {
if (section_id != 0) return "#" + std::to_string(section_id);
return section.empty() ? "(empty)" : section;
}
data::Species Design::effective_race() const {
if (race != data::Species::Unknown) return race;
for (const SectionUse& u : slots)
if (!u.empty()) return u.species;
return data::Species::Unknown;
}
std::string_view level_name(Level l) {
switch (l) {
case Level::Error: return "error";
case Level::Warn: return "warn";
case Level::Info: return "info";
}
return "?";
}
std::string Violation::str() const {
std::string s(level_name(level));
for (char& c : s) c = static_cast<char>(c >= 'a' && c <= 'z' ? c - 32 : c);
while (s.size() < 5) s += ' ';
s += ' ';
s += rule;
if (slot) {
s += " [";
s += slot_name(*slot);
if (bank) s += " bank " + std::to_string(*bank);
s += ']';
}
s += ": ";
s += message;
return s;
}
bool has_errors(const std::vector<Violation>& v) {
return std::any_of(v.begin(), v.end(), [](const Violation& x) { return x.level == Level::Error; });
}
std::vector<std::string> rules_at(const std::vector<Violation>& v, Level level) {
std::vector<std::string> out;
for (const Violation& x : v)
if (x.level == level && std::find(out.begin(), out.end(), x.rule) == out.end()) out.push_back(x.rule);
std::sort(out.begin(), out.end());
return out;
}
} // namespace game::design