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

summaryrefslogtreecommitdiff
path: root/source/game/StarSkyParameters.cpp
blob: 3d540b86a5ed007f6fba65fbd7db99381036e63b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "StarSkyParameters.hpp"
#include "StarCelestialDatabase.hpp"
#include "StarCelestialGraphics.hpp"
#include "StarCasting.hpp"
#include "StarJsonExtra.hpp"
#include "StarDataStreamExtra.hpp"

namespace Star {

SkyParameters::SkyParameters() : seed(), skyType(SkyType::Barren), skyColoring(makeRight(Color::Black)), settings(JsonObject()) {}

SkyParameters::SkyParameters(CelestialCoordinate const& coordinate, CelestialDatabasePtr const& celestialDatabase)
  : SkyParameters() {
  if (!coordinate || coordinate.isSystem())
    return;
  auto params = celestialDatabase->parameters(coordinate);
  if (!params)
    return;
  auto systemParams = celestialDatabase->parameters(coordinate.system());
  seed = staticRandomU64(params->seed(), "SkySeed");

  // Gather up all the CelestialParameters and scales for all the celestial
  // objects to draw in the sky, we should draw the parent planet if we are a
  // satellite, as well as all the other satellites.
  auto selfCoordinate = params->coordinate();
  if (selfCoordinate.isSatelliteBody()) {
    if (auto planet = celestialDatabase->parameters(selfCoordinate.parent())) {
      Vec2F pos;
      pos[0] = staticRandomFloat(params->seed(), planet->seed(), "x");
      pos[1] = staticRandomFloat(params->seed(), planet->seed(), "y");

      // My parent's parent is no one.
      nearbyPlanet = {{CelestialGraphics::drawWorld(*planet, {}), pos}};
    }
  }

  for (auto satelliteCoordinate : celestialDatabase->children(selfCoordinate.planet())) {
    if (satelliteCoordinate != selfCoordinate) {
      if (auto satellite = celestialDatabase->parameters(satelliteCoordinate)) {
        Vec2F pos;
        pos[0] = staticRandomFloat(params->seed(), satellite->seed(), "x");
        pos[1] = staticRandomFloat(params->seed(), satellite->seed(), "y");

        nearbyMoons.append(
            {CelestialGraphics::drawWorld(*satellite, celestialDatabase->parameters(satelliteCoordinate.parent())),
                pos});
      }
    }
  }

  horizonImages = CelestialGraphics::worldHorizonImages(*params);

  readVisitableParameters(params->visitableParameters());

  sunType = systemParams->getParameter("typeName").toString();
}

SkyParameters::SkyParameters(SkyParameters const& oldSkyParameters, VisitableWorldParametersConstPtr newVisitableParameters) : SkyParameters() {
  *this = oldSkyParameters;
  readVisitableParameters(newVisitableParameters);
}

SkyParameters::SkyParameters(Json const& config) : SkyParameters() {
  if (config.isNull())
    return;

  seed = config.getUInt("seed");
  dayLength = config.optFloat("dayLength");

  auto extractLayerData = [](Json const& v, uint64_t selfSeed) -> pair<List<pair<String, float>>, Vec2F> {
    Vec2F pos;

    if (v.contains("pos")) {
      pos = jsonToVec2F(v.get("pos"));
    } else if (v.contains("seed")) {
      pos[0] = staticRandomFloat(selfSeed, v.getUInt("seed"), "x");
      pos[1] = staticRandomFloat(selfSeed, v.getUInt("seed"), "y");
    }

    List<pair<String, float>> layers;
    for (auto const& l : v.get("layers").iterateArray())
      layers.append({l.getString("image"), l.getFloat("scale")});

    return {layers, pos};
  };

  if (config.contains("planet") && config.get("planet"))
    nearbyPlanet = extractLayerData(config.get("planet"), seed);

  if (config.contains("satellites"))
    nearbyMoons =
        jsonToList<pair<List<pair<String, float>>, Vec2F>>(config.get("satellites"), bind(extractLayerData, _1, seed));

  if (config.contains("horizonImages")) {
    horizonImages = jsonToList<pair<String, String>>(config.get("horizonImages"),
        [](Json const& v) -> pair<String, String> {
          return {v.getString("left"), v.getString("right")};
        });
  }

  horizonClouds = config.getBool("horizonClouds", true);

  skyType = SkyTypeNames.getLeft(config.getString("skyType", "barren"));
  if (auto colors = config.opt("skyColoring")) {
    skyColoring.setLeft(SkyColoring{*colors});
  } else if (auto ambientLightLevel = config.opt("ambientLightLevel")) {
    skyColoring.setRight(jsonToColor(*ambientLightLevel));
  } else {
    skyColoring.setRight(Color::Black);
  }

  spaceLevel = config.optFloat("spaceLevel");
  surfaceLevel = config.optFloat("surfaceLevel");

  sunType = config.getString("sunType", "");

  settings = config.get("settings", JsonObject());
}

Json SkyParameters::toJson() const {
  return JsonObject{
      {"seed", seed},
      {"dayLength", jsonFromMaybe<float>(dayLength)},
      {"planet",
          jsonFromMaybe<pair<List<pair<String, float>>, Vec2F>>(nearbyPlanet,
              [](pair<List<pair<String, float>>, Vec2F> p) -> Json {
                return JsonObject{
                    {"layers",
                        p.first.transformed([](pair<String, float> const& p) -> Json {
                          return JsonObject{{"image", p.first}, {"scale", p.second}};
                        })},
                    {"pos", jsonFromVec2F(p.second)},
                };
              })},
      {"satellites",
          jsonFromList<pair<List<pair<String, float>>, Vec2F>>(nearbyMoons,
              [](pair<List<pair<String, float>>, Vec2F> const& p) {
                return JsonObject{
                    {"layers",
                        p.first.transformed([](pair<String, float> const& p) -> Json {
                          return JsonObject{{"image", p.first}, {"scale", p.second}};
                        })},
                    {"pos", jsonFromVec2F(p.second)},
                };
              })},
      {"horizonImages",
          jsonFromList<pair<String, String>>(horizonImages,
              [](pair<String, String> p) {
                return JsonObject{
                    {"left", p.first}, {"right", p.second},
                };
              })},
      {"horizonClouds", horizonClouds},
      {"skyType", SkyTypeNames.getRight(skyType)},
      {"skyColoring", jsonFromMaybe<SkyColoring>(skyColoring.maybeLeft(), [](SkyColoring c) { return c.toJson(); })},
      {"ambientLightLevel", jsonFromMaybe<Color>(skyColoring.maybeRight(), [](Color c) { return jsonFromColor(c); })},
      {"spaceLevel", jsonFromMaybe<float>(spaceLevel)},
      {"surfaceLevel", jsonFromMaybe<float>(surfaceLevel)},
      {"sunType", sunType},
      {"settings", settings}
  };
}

void SkyParameters::read(DataStream& ds) {
  ds >> seed;
  ds >> dayLength;
  ds >> nearbyPlanet;
  ds >> nearbyMoons;
  ds >> horizonImages;
  ds >> horizonClouds;
  ds >> skyType;
  ds >> skyColoring;
  ds >> spaceLevel;
  ds >> surfaceLevel;
  ds >> sunType;
  if (ds.streamCompatibilityVersion() >= 3)
    ds >> settings;
  else
    settings = JsonObject();
}

void SkyParameters::write(DataStream& ds) const {
  ds << seed;
  ds << dayLength;
  ds << nearbyPlanet;
  ds << nearbyMoons;
  ds << horizonImages;
  ds << horizonClouds;
  ds << skyType;
  ds << skyColoring;
  ds << spaceLevel;
  ds << surfaceLevel;
  ds << sunType;
  if (ds.streamCompatibilityVersion() >= 3)
    ds << settings;
}

void SkyParameters::readVisitableParameters(VisitableWorldParametersConstPtr visitableParameters) {
  if (auto terrestrialParameters = as<TerrestrialWorldParameters>(visitableParameters)) {
    dayLength = terrestrialParameters->dayLength;
    if (terrestrialParameters->airless) {
      skyType = SkyType::Atmosphereless;
      horizonClouds = false;
    } else {
      skyType = SkyType::Atmospheric;
      horizonClouds = true;
    }
    skyColoring.setLeft(terrestrialParameters->skyColoring);
    spaceLevel = terrestrialParameters->spaceLayer.layerMinHeight;
    surfaceLevel = terrestrialParameters->atmosphereLayer.layerMinHeight;
  } else {
    skyType = SkyType::Barren;
    horizonClouds = false;

    if (auto asteroidsParameters = as<AsteroidsWorldParameters>(visitableParameters)) {
      skyColoring.setRight(asteroidsParameters->ambientLightLevel);
    } else if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(visitableParameters)) {
      skyColoring.setRight(floatingDungeonParameters->ambientLightLevel);
    } else {
      skyColoring.setRight(Color::Black);
    }
  }
}

DataStream& operator>>(DataStream& ds, SkyParameters& sky) {
  sky.read(ds);
  return ds;
}

DataStream& operator<<(DataStream& ds, SkyParameters const& sky) {
  sky.write(ds);
  return ds;
}

}