Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarPlayerTech.cpp
blob: bf8053f4cb4c523e3ef706345d8cd7b157977a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "StarPlayerTech.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"

namespace Star {

PlayerTech::PlayerTech() {}

PlayerTech::PlayerTech(Json const& json) {
  m_availableTechs = jsonToStringSet(json.get("availableTechs"));
  m_enabledTechs = jsonToStringSet(json.get("enabledTechs"));
  auto techDatabase = Root::singleton().techDatabase();
  for (auto& p : json.getObject("equippedTechs")) {
    String techName = p.second.toString();
    if (techDatabase->contains(techName))
      m_equippedTechs.set(TechTypeNames.getLeft(p.first), techName);
    else
      Logger::warn("Unequipping unknown tech '{}' from slot '{}'", techName, p.first);
  }
}

Json PlayerTech::toJson() const {
  return JsonObject{
    {"availableTechs", jsonFromStringSet(m_availableTechs)},
    {"enabledTechs", jsonFromStringSet(m_enabledTechs)},
    {"equippedTechs", jsonFromMapK(m_equippedTechs, [](TechType t) {
        return TechTypeNames.getRight(t);
      })},
  };
}

bool PlayerTech::isAvailable(String const& techModule) const {
  return m_availableTechs.contains(techModule);
}

void PlayerTech::makeAvailable(String const& techModule) {
  m_availableTechs.add(techModule);
}

void PlayerTech::makeUnavailable(String const& techModule) {
  disable(techModule);
  m_availableTechs.remove(techModule);
}

bool PlayerTech::isEnabled(String const& techModule) const {
  return m_enabledTechs.contains(techModule);
}

void PlayerTech::enable(String const& techModule) {
  if (!m_availableTechs.contains(techModule))
    throw PlayerTechException::format("Enabling tech module '{}' when not available", techModule);
  m_enabledTechs.add(techModule);
}

void PlayerTech::disable(String const& techModule) {
  unequip(techModule);
  m_enabledTechs.remove(techModule);
}

bool PlayerTech::isEquipped(String const& techModule) const {
  for (auto t : m_equippedTechs.keys()) {
    if (m_equippedTechs.get(t) == techModule)
      return true;
  }
  return false;
}

void PlayerTech::equip(String const& techModule) {
  if (!m_enabledTechs.contains(techModule))
    throw PlayerTechException::format("Equipping tech module '{}' when not enabled", techModule);

  auto techDatabase = Root::singleton().techDatabase();
  m_equippedTechs[techDatabase->tech(techModule).type] = techModule;
}

void PlayerTech::unequip(String const& techModule) {
  for (auto t : m_equippedTechs.keys()) {
    if (m_equippedTechs.get(t) == techModule)
      m_equippedTechs.remove(t);
  }
}

StringSet const& PlayerTech::availableTechs() const {
  return m_availableTechs;
}

StringSet const& PlayerTech::enabledTechs() const {
  return m_enabledTechs;
}

HashMap<TechType, String> const& PlayerTech::equippedTechs() const {
  return m_equippedTechs;
}

StringList PlayerTech::techModules() const {
  return m_equippedTechs.values();
}

}