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

summaryrefslogtreecommitdiff
path: root/source/game/terrain/StarIslandSurfaceSelector.cpp
blob: 21e9f2270b0ab7e2133409c1d4b956dbeb36e6b5 (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
#include "StarIslandSurfaceSelector.hpp"
#include "StarGameTypes.hpp"
#include "StarRandom.hpp"
#include "StarJsonExtra.hpp"

namespace Star {

char const* const IslandSurfaceSelector::Name = "islandSurface";

IslandSurfaceSelector::IslandSurfaceSelector(Json const& config, TerrainSelectorParameters const& parameters)
  : TerrainSelector(Name, config, parameters) {
  layerBaseHeight = parameters.baseHeight;
  worldWidth = parameters.worldWidth;

  islandElevation = config.getFloat("islandElevation");
  islandTaperPoint = config.getFloat("islandTaperPoint");

  islandHeight = PerlinF(config.getObject("islandHeight"), staticRandomU64(parameters.seed, parameters.baseHeight, "islandHeight"));
  islandDepth = PerlinF(config.getObject("islandDepth"), staticRandomU64(parameters.seed, parameters.baseHeight, "islandDepth"));
  islandDecision = PerlinF(config.getObject("islandDecision"), staticRandomU64(parameters.seed, parameters.baseHeight, "islandDecision"));
}

IslandColumn IslandSurfaceSelector::generateColumn(int x) const {
  float noiseAngle = 2 * Constants::pi * x / worldWidth;
  float noiseX = (std::cos(noiseAngle) * worldWidth) / (2 * Constants::pi);
  float noiseY = (std::sin(noiseAngle) * worldWidth) / (2 * Constants::pi);

  IslandColumn newCol;
  float thisIslandDecision = islandDecision.get(noiseX, noiseY);
  if (thisIslandDecision > 0) {
    float taperFactor = thisIslandDecision < islandTaperPoint ? std::sin((0.5 * Constants::pi * thisIslandDecision) / islandTaperPoint) : 1.0f;
    newCol.topLevel = islandElevation + layerBaseHeight + taperFactor * islandHeight.get(noiseX, noiseY);
    newCol.bottomLevel = islandElevation + layerBaseHeight - taperFactor * islandDepth.get(noiseX, noiseY);
  } else {
    newCol.topLevel = layerBaseHeight;
    newCol.bottomLevel = layerBaseHeight;
  }

  return newCol;
}

float IslandSurfaceSelector::get(int x, int y) const {
  auto col = columnCache.get(x, [=](int x) {
      return IslandSurfaceSelector::generateColumn(x);
    });

  return (col.topLevel - col.bottomLevel) / 2 - abs((col.topLevel + col.bottomLevel) / 2 - y);
}

}