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

summaryrefslogtreecommitdiff
path: root/source/game/StarTileDrawer.cpp
blob: 15e88f39e3579cba3ac12c14d45f343a8f04ebbd (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "StarTileDrawer.hpp"
#include "StarLexicalCast.hpp"
#include "StarJsonExtra.hpp"
#include "StarXXHash.hpp"
#include "StarMaterialDatabase.hpp"
#include "StarLiquidsDatabase.hpp"
#include "StarAssets.hpp"
#include "StarRoot.hpp"

namespace Star {

RenderTile TileDrawer::DefaultRenderTile{
    NullMaterialId,
    NoModId,
    NullMaterialId,
    NoModId,
    0,
    0,
    DefaultMaterialColorVariant,
    TileDamageType::Protected,
    0,
    0,
    0,
    DefaultMaterialColorVariant,
    TileDamageType::Protected,
    0,
    EmptyLiquidId,
    0
};

TileDrawer* TileDrawer::s_singleton;

TileDrawer* TileDrawer::singletonPtr() {
  return s_singleton;
}

TileDrawer& TileDrawer::singleton() {
  if (!s_singleton)
    throw StarException("TileDrawer::singleton() called with no TileDrawer instance available");
  else
    return *s_singleton;
}

TileDrawer::TileDrawer() {
  auto assets = Root::singleton().assets();

  m_backgroundLayerColor = jsonToColor(assets->json("/rendering.config:backgroundLayerColor")).toRgba();
  m_foregroundLayerColor = jsonToColor(assets->json("/rendering.config:foregroundLayerColor")).toRgba();
  m_liquidDrawLevels = jsonToVec2F(assets->json("/rendering.config:liquidDrawLevels"));
  s_singleton = this;
}

TileDrawer::~TileDrawer() {
  if (s_singleton == this)
    s_singleton = nullptr;
}

bool TileDrawer::produceTerrainDrawables(Drawables& drawables,
  TerrainLayer terrainLayer, Vec2I const& pos, WorldRenderData const& renderData, float scale, Vec2I offset, Maybe<TerrainLayer> variantLayer) {
  auto& root = Root::singleton();
  auto assets = Root::singleton().assets();
  auto materialDatabase = root.materialDatabase();

  RenderTile const& tile = getRenderTile(renderData, pos);

  MaterialId material = EmptyMaterialId;
  MaterialHue materialHue = 0;
  MaterialColorVariant colorVariant = 0;
  ModId mod = NoModId;
  MaterialHue modHue = 0;
  float damageLevel = 0.0f;
  TileDamageType damageType = TileDamageType::Protected;
  Color color;

  bool occlude = false;

  if (terrainLayer == TerrainLayer::Background) {
    material = tile.background;
    materialHue = tile.backgroundHueShift;
    colorVariant = tile.backgroundColorVariant;
    mod = tile.backgroundMod;
    modHue = tile.backgroundModHueShift;
    damageLevel = byteToFloat(tile.backgroundDamageLevel);
    damageType = tile.backgroundDamageType;
    color = Color::rgba(m_backgroundLayerColor);
  } else {
    material = tile.foreground;
    materialHue = tile.foregroundHueShift;
    colorVariant = tile.foregroundColorVariant;
    mod = tile.foregroundMod;
    modHue = tile.foregroundModHueShift;
    damageLevel = byteToFloat(tile.foregroundDamageLevel);
    damageType = tile.foregroundDamageType;
    color = Color::rgba(m_foregroundLayerColor);
  }

  // render non-block colliding things in the midground
  bool isBlock = BlockCollisionSet.contains(materialDatabase->materialCollisionKind(material));
  if ((isBlock && terrainLayer == TerrainLayer::Midground) || (!isBlock && terrainLayer == TerrainLayer::Foreground))
    return false;

  auto getPieceImage = [](MaterialRenderPieceConstPtr const& piece, Box<float, 2> const& box, MaterialHue hue, Directives const* directives) -> AssetPath {
    String path = (hue == 0)
      ? strf("{}?crop={};{};{};{}", piece->texture, box.xMin(), box.yMin(), box.xMax(), box.yMax())
      : strf("{}?crop={};{};{};{}?hueshift={}", piece->texture, box.xMin(), box.yMin(), box.xMax(), box.yMax(), materialHueToDegrees(hue));

    AssetPath image(path);
    if (directives)
      image.directives += *directives;

    return image;
  };

  auto materialRenderProfile = materialDatabase->materialRenderProfile(material);
  auto modRenderProfile = materialDatabase->modRenderProfile(mod);

  if (materialRenderProfile) {
    occlude = materialRenderProfile->occludesBehind;
    auto materialColorVariant = materialRenderProfile->colorVariants > 0 ? colorVariant % materialRenderProfile->colorVariants : 0;
    uint32_t variance = staticRandomU32(renderData.geometry.xwrap(pos[0]) + offset[0], pos[1] + offset[1], (int)variantLayer.value(terrainLayer), "main");
    auto& drawList = drawables[materialZLevel(materialRenderProfile->zLevel, material, materialHue, materialColorVariant)];

    MaterialPieceResultList pieces;
    determineMatchingPieces(pieces, &occlude, materialDatabase, materialRenderProfile->mainMatchList, renderData, pos,
        terrainLayer == TerrainLayer::Background ? TileLayer::Background : TileLayer::Foreground, false);
    Directives const* directives = materialRenderProfile->colorDirectives.empty()
      ? nullptr
      : &materialRenderProfile->colorDirectives.wrap(materialColorVariant);
    for (auto const& piecePair : pieces) {
      auto variant = piecePair.first->variants.ptr(materialColorVariant);
      if (!variant) variant = piecePair.first->variants.ptr(0);
      if (!variant) continue;
      auto& textureCoords = variant->wrap(variance);
      auto image = getPieceImage(piecePair.first, textureCoords, materialHue, directives);
      drawList.emplace_back(Drawable::makeImage(image, scale, false, piecePair.second * scale + Vec2F(pos), color));
    }
  }

  if (modRenderProfile) {
    auto modColorVariant = modRenderProfile->colorVariants > 0 ? colorVariant % modRenderProfile->colorVariants : 0;
    uint32_t variance = staticRandomU32(renderData.geometry.xwrap(pos[0]), pos[1], (int)variantLayer.value(terrainLayer), "mod");
    auto& drawList = drawables[modZLevel(modRenderProfile->zLevel, mod, modHue, modColorVariant)];

    MaterialPieceResultList pieces;
    determineMatchingPieces(pieces, &occlude, materialDatabase, modRenderProfile->mainMatchList, renderData, pos,
        terrainLayer == TerrainLayer::Background ? TileLayer::Background : TileLayer::Foreground, true);
    Directives const* directives = modRenderProfile->colorDirectives.empty()
      ? nullptr
      : &modRenderProfile->colorDirectives.wrap(modColorVariant);
    for (auto const& piecePair : pieces) {
      auto variant = piecePair.first->variants.ptr(modColorVariant);
      if (!variant) variant = piecePair.first->variants.ptr(0);
      if (!variant) continue;
      auto& textureCoords = variant->wrap(variance);
      auto image = getPieceImage(piecePair.first, textureCoords, modHue, directives);
      drawList.emplace_back(Drawable::makeImage(image, scale, false, piecePair.second * scale + Vec2F(pos), color));
    }
  }

  if (materialRenderProfile && damageLevel > 0 && isBlock) {
    auto& drawList = drawables[damageZLevel()];
    auto const& crackingImage = materialRenderProfile->damageImage(damageLevel, damageType);

    drawList.emplace_back(Drawable::makeImage(crackingImage.first, scale, false, crackingImage.second * scale + Vec2F(pos), color));
  }

  return occlude;
}

WorldRenderData& TileDrawer::renderData() {
  return m_tempRenderData;
}

MutexLocker TileDrawer::lockRenderData() {
  return MutexLocker(m_tempRenderDataMutex);
}

RenderTile const& TileDrawer::getRenderTile(WorldRenderData const& renderData, Vec2I const& worldPos) {
  Vec2I arrayPos = renderData.geometry.diff(worldPos, renderData.tileMinPosition);

  Vec2I size = Vec2I(renderData.tiles.size());
  if (arrayPos[0] >= 0 && arrayPos[1] >= 0 && arrayPos[0] < size[0] && arrayPos[1] < size[1])
    return renderData.tiles(Vec2S(arrayPos));

  return DefaultRenderTile;
}

TileDrawer::QuadZLevel TileDrawer::materialZLevel(uint32_t zLevel, MaterialId material, MaterialHue hue, MaterialColorVariant colorVariant) {
  QuadZLevel quadZLevel = 0;
  quadZLevel |= (uint64_t)colorVariant;
  quadZLevel |= (uint64_t)hue << 8;
  quadZLevel |= (uint64_t)material << 16;
  quadZLevel |= (uint64_t)zLevel << 32;
  return quadZLevel;
}

TileDrawer::QuadZLevel TileDrawer::modZLevel(uint32_t zLevel, ModId mod, MaterialHue hue, MaterialColorVariant colorVariant) {
  QuadZLevel quadZLevel = 0;
  quadZLevel |= (uint64_t)colorVariant;
  quadZLevel |= (uint64_t)hue << 8;
  quadZLevel |= (uint64_t)mod << 16;
  quadZLevel |= (uint64_t)zLevel << 32;
  quadZLevel |= (uint64_t)1 << 63;
  return quadZLevel;
}

TileDrawer::QuadZLevel TileDrawer::damageZLevel() {
  return (uint64_t)(-1);
}

bool TileDrawer::determineMatchingPieces(MaterialPieceResultList& resultList, bool* occlude, MaterialDatabaseConstPtr const& materialDb, MaterialRenderMatchList const& matchList,
    WorldRenderData const& renderData, Vec2I const& basePos, TileLayer layer, bool isMod) {
  RenderTile const& tile = getRenderTile(renderData, basePos);

  auto matchSetMatches = [&](MaterialRenderMatchConstPtr const& match) -> bool {
    if (match->requiredLayer && *match->requiredLayer != layer)
      return false;

    if (match->matchPoints.empty())
      return true;

    bool matchValid = match->matchJoin == MaterialJoinType::All;
    for (auto const& matchPoint : match->matchPoints) {
      auto const& neighborTile = getRenderTile(renderData, basePos + matchPoint.position);

      bool neighborShadowing = false;
      if (layer == TileLayer::Background) {
        if (auto profile = materialDb->materialRenderProfile(neighborTile.foreground))
          neighborShadowing = !profile->foregroundLightTransparent;
      }

      MaterialHue baseHue = layer == TileLayer::Foreground ? tile.foregroundHueShift : tile.backgroundHueShift;
      MaterialHue neighborHue = layer == TileLayer::Foreground ? neighborTile.foregroundHueShift : neighborTile.backgroundHueShift;
      MaterialHue baseModHue = layer == TileLayer::Foreground ? tile.foregroundModHueShift : tile.backgroundModHueShift;
      MaterialHue neighborModHue = layer == TileLayer::Foreground ? neighborTile.foregroundModHueShift : neighborTile.backgroundModHueShift;
      MaterialId baseMaterial = layer == TileLayer::Foreground ? tile.foreground : tile.background;
      MaterialId neighborMaterial = layer == TileLayer::Foreground ? neighborTile.foreground : neighborTile.background;
      ModId baseMod = layer == TileLayer::Foreground ? tile.foregroundMod : tile.backgroundMod;
      ModId neighborMod = layer == TileLayer::Foreground ? neighborTile.foregroundMod : neighborTile.backgroundMod;

      bool rulesValid = matchPoint.rule->join == MaterialJoinType::All;
      for (auto const& ruleEntry : matchPoint.rule->entries) {
        bool valid = true;
        if (isMod) {
          if (ruleEntry.rule.is<MaterialRule::RuleEmpty>()) {
            valid = neighborMod == NoModId;
          } else if (ruleEntry.rule.is<MaterialRule::RuleConnects>()) {
            valid = isConnectableMaterial(neighborMaterial);
          } else if (ruleEntry.rule.is<MaterialRule::RuleShadows>()) {
            valid = neighborShadowing;
          } else if (auto equalsSelf = ruleEntry.rule.ptr<MaterialRule::RuleEqualsSelf>()) {
            valid = neighborMod == baseMod;
            if (equalsSelf->matchHue)
              valid = valid && baseModHue == neighborModHue;
          } else if (auto equalsId = ruleEntry.rule.ptr<MaterialRule::RuleEqualsId>()) {
            valid = neighborMod == equalsId->id;
          } else if (auto propertyEquals = ruleEntry.rule.ptr<MaterialRule::RulePropertyEquals>()) {
            if (auto profile = materialDb->modRenderProfile(neighborMod))
              valid = profile->ruleProperties.get(propertyEquals->propertyName, Json()) == propertyEquals->compare;
            else
              valid = false;
          }
        } else {
          if (ruleEntry.rule.is<MaterialRule::RuleEmpty>()) {
            valid = neighborMaterial == EmptyMaterialId;
          } else if (ruleEntry.rule.is<MaterialRule::RuleConnects>()) {
            valid = isConnectableMaterial(neighborMaterial);
          } else if (ruleEntry.rule.is<MaterialRule::RuleShadows>()) {
            valid = neighborShadowing;
          } else if (auto equalsSelf = ruleEntry.rule.ptr<MaterialRule::RuleEqualsSelf>()) {
            valid = neighborMaterial == baseMaterial;
            if (equalsSelf->matchHue)
              valid = valid && baseHue == neighborHue;
          } else if (auto equalsId = ruleEntry.rule.ptr<MaterialRule::RuleEqualsId>()) {
            valid = neighborMaterial == equalsId->id;
          } else if (auto propertyEquals = ruleEntry.rule.ptr<MaterialRule::RulePropertyEquals>()) {
            if (auto profile = materialDb->materialRenderProfile(neighborMaterial))
              valid = profile->ruleProperties.get(propertyEquals->propertyName) == propertyEquals->compare;
            else
              valid = false;
          }
        }
        if (ruleEntry.inverse)
          valid = !valid;

        if (matchPoint.rule->join == MaterialJoinType::All) {
          rulesValid = valid && rulesValid;
          if (!rulesValid)
            break;
        } else {
          rulesValid = valid || rulesValid;
        }
      }

      if (match->matchJoin == MaterialJoinType::All) {
        matchValid = matchValid && rulesValid;
        if (!matchValid)
          return matchValid;
      } else {
        matchValid = matchValid || rulesValid;
      }
    }
    return matchValid;
  };

  bool subMatchResult = false;
  for (auto const& match : matchList) {
    if (matchSetMatches(match)) {
      if (match->occlude)
        *occlude = match->occlude.get();

      subMatchResult = true;

      for (auto const& piecePair : match->resultingPieces)
        resultList.append({piecePair.first, piecePair.second});

      if (determineMatchingPieces(resultList, occlude, materialDb, match->subMatches, renderData, basePos, layer, isMod) && match->haltOnSubMatch)
        break;

      if (match->haltOnMatch)
        break;
    }
  }

  return subMatchResult;
}

}