MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / StatCollection

Method StatCollection

source/game/StarStatCollection.cpp:8–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6namespace Star {
7
8StatCollection::StatCollection(Json const& config) {
9 for (auto const& stat : config.getObject("stats", {}))
10 m_stats.addStat(stat.first, stat.second.getFloat("baseValue", 0.0));
11
12 for (auto const& resource : config.getObject("resources", {})) {
13 auto statOrValue = [&resource](String const& statName, String const& valueName, MVariant<String, float> def = {}) -> MVariant<String, float> {
14 if (auto maxStat = resource.second.optString(statName))
15 return *maxStat;
16 else if (auto maxValue = resource.second.optFloat(valueName))
17 return *maxValue;
18 else
19 return def;
20 };
21
22 MVariant<String, float> resourceMax = statOrValue("maxStat", "maxValue");
23 MVariant<String, float> resourceDelta = statOrValue("deltaStat", "deltaValue");
24 m_stats.addResource(resource.first, resourceMax, resourceDelta);
25
26 if (auto initialValue = resource.second.optFloat("initialValue")) {
27 m_stats.setResourceValue(resource.first, *initialValue);
28 m_defaultResourceValues[resource.first] = makeLeft(*initialValue);
29 } else if (auto percentage = resource.second.optFloat("initialPercentage")) {
30 m_stats.setResourcePercentage(resource.first, *percentage);
31 m_defaultResourceValues[resource.first] = makeRight(*percentage);
32 } else {
33 if (m_stats.resourceMax(resource.first)) {
34 m_stats.setResourcePercentage(resource.first, 1.0f);
35 m_defaultResourceValues[resource.first] = makeRight(1.0f);
36 } else {
37 m_stats.setResourceValue(resource.first, 0.0f);
38 m_defaultResourceValues[resource.first] = makeLeft(0.0f);
39 }
40 }
41 }
42
43 addNetElement(&m_statModifiersNetState);
44
45 // Sort resource names alphabetically to ensure the same order on master and
46 // slaves.
47 for (auto const& resource : m_stats.resourceNames().sorted()) {
48 auto& resourceNetState = m_resourceValuesNetStates[resource];
49 addNetElement(&resourceNetState);
50 addNetElement(&m_resourceLockedNetStates[resource]);
51 }
52}
53
54StringList StatCollection::statNames() const {
55 return m_stats.effectiveStatNames();

Callers

nothing calls this directly

Calls 13

makeLeftFunction · 0.85
makeRightFunction · 0.85
addStatMethod · 0.80
getFloatMethod · 0.80
optStringMethod · 0.80
optFloatMethod · 0.80
addResourceMethod · 0.80
setResourceValueMethod · 0.80
getObjectMethod · 0.45
setResourcePercentageMethod · 0.45
resourceMaxMethod · 0.45
sortedMethod · 0.45

Tested by

no test coverage detected