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

summaryrefslogtreecommitdiff
path: root/source/game/StarPlayerTypes.cpp
blob: 4f5d495df1958c59cfe354eee9645a022c01aa0e (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
#include "StarPlayerTypes.hpp"
#include "StarJsonExtra.hpp"

namespace Star {

EnumMap<PlayerMode> const PlayerModeNames{
  {PlayerMode::Casual, "casual"},
  {PlayerMode::Survival, "survival"},
  {PlayerMode::Hardcore, "hardcore"}
};

EnumMap<PlayerBusyState> const PlayerBusyStateNames{
  {PlayerBusyState::None, "none"},
  {PlayerBusyState::Chatting, "chatting"},
  {PlayerBusyState::Menu, "menu"}
};

PlayerModeConfig::PlayerModeConfig(Json config) {
  if (config.isNull())
    config = JsonObject();

  hunger = config.getBool("hunger", true);
  allowBeamUpUnderground = config.getBool("allowBeamUpUnderground", false);
  reviveCostPercentile = config.getFloat("reviveCostPercentile", 0.0f);
  auto deathDropItemTypesConfig = config.get("deathDropItemTypes", "none");
  if (deathDropItemTypesConfig.type() == Json::Type::Array)
    deathDropItemTypes.setRight(jsonToStringList(deathDropItemTypesConfig));
  else
    deathDropItemTypes.setLeft(deathDropItemTypesConfig.toString());
  permadeath = config.getBool("permadeath", false);
}

ShipUpgrades::ShipUpgrades(Json config) {
  if (config.isNull())
    config = JsonObject();

  shipLevel = config.getUInt("shipLevel", 0);
  maxFuel = config.getUInt("maxFuel", 0);
  crewSize = config.getUInt("crewSize", 0);
  fuelEfficiency = config.getFloat("fuelEfficiency", 1.0);
  shipSpeed = config.getUInt("shipSpeed", 0);
  capabilities.addAll(jsonToStringList(config.get("capabilities", JsonArray{})));
}

Json ShipUpgrades::toJson() const {
  return JsonObject{{"shipLevel", shipLevel},
      {"maxFuel", maxFuel},
      {"crewSize", crewSize},
      {"fuelEfficiency", fuelEfficiency},
      {"shipSpeed", shipSpeed},
      {"capabilities", capabilities.values().transformed(construct<Json>())}};
}

ShipUpgrades& ShipUpgrades::apply(Json const& upgrades) {
  shipLevel = max(shipLevel, (unsigned)upgrades.optUInt("shipLevel").value(shipLevel));
  maxFuel = upgrades.optUInt("maxFuel").value(maxFuel);
  crewSize = max(crewSize, (unsigned)upgrades.optUInt("crewSize").value(crewSize));
  fuelEfficiency = upgrades.optFloat("fuelEfficiency").value(fuelEfficiency);
  shipSpeed = upgrades.optFloat("shipSpeed").value(shipSpeed);
  if (upgrades.contains("capabilities"))
    capabilities.addAll(jsonToStringList(upgrades.get("capabilities", JsonArray{})));
  return *this;
}

bool ShipUpgrades::operator==(ShipUpgrades const& rhs) const {
  return tie(shipLevel, maxFuel, crewSize, fuelEfficiency, shipSpeed, capabilities)
      == tie(rhs.shipLevel, rhs.maxFuel, rhs.crewSize, rhs.fuelEfficiency, rhs.shipSpeed, rhs.capabilities);
}

}