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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
#pragma once
#include "StarIterator.hpp"
#include "StarEntityMap.hpp"
#include "StarWorldTiles.hpp"
#include "StarBlocksAlongLine.hpp"
#include "StarBiome.hpp"
#include "StarSky.hpp"
#include "StarWorldTemplate.hpp"
#include "StarLiquidsDatabase.hpp"
#include "StarCellularLighting.hpp"
#include "StarRoot.hpp"
#include "StarMaterialDatabase.hpp"
#include "StarAssets.hpp"
#include "StarJsonExtra.hpp"
#include "StarTileModification.hpp"
namespace Star {
namespace WorldImpl {
template <typename TileSectorArray>
bool tileIsOccupied(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, bool includeEphemeral, bool checkCollision = false);
template <typename TileSectorArray>
CollisionKind tileCollisionKind(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const& entityMap,
Vec2I const& pos);
template <typename TileSectorArray>
bool rectTileCollision(shared_ptr<TileSectorArray> const& tileSectorArray, RectI const& region, bool solidCollision);
template <typename TileSectorArray>
bool lineTileCollision(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray, Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet);
template <typename TileSectorArray>
Maybe<pair<Vec2F, Vec2I>> lineTileCollisionPoint(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray, Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet);
template <typename TileSectorArray>
List<Vec2I> collidingTilesAlongLine(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray,
Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet, size_t maxSize, bool includeEdges);
inline TileDamageParameters tileDamageParameters(WorldTile* tile, TileLayer layer, TileDamage const& tileDamage);
template <typename TileSectorArray>
bool damageWouldDestroy(shared_ptr<TileSectorArray> const& tileSectorArray, Vec2I pos, TileLayer layer, TileDamage const& tileDamage);
template <typename GetTileFunction>
bool canPlaceMaterial(EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, MaterialId material, bool allowEntityOverlap, bool allowTileOverlap, GetTileFunction& getTile);
// returns true if this material could be placed if in the same batch other
// tiles can be placed
// that connect to it
template <typename GetTileFunction>
bool perhapsCanPlaceMaterial(EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, MaterialId material, bool allowEntityOverlap, bool allowTileOverlap, GetTileFunction& getTile);
template <typename GetTileFunction>
bool canPlaceMaterialColorVariant(Vec2I const& pos, TileLayer layer, MaterialColorVariant color, GetTileFunction& getTile);
template <typename GetTileFunction>
bool canPlaceMod(Vec2I const& pos, TileLayer layer, ModId mod, GetTileFunction& getTile);
template <typename GetTileFunction>
pair<bool, bool> validateTileModification(EntityMapPtr const& entityMap, Vec2I const& pos, TileModification const& modification, bool allowEntityOverlap, GetTileFunction& getTile);
bool validateTileReplacement(TileModification const& modification);
// Split modification list into good and bad
template <typename GetTileFunction>
pair<TileModificationList, TileModificationList> splitTileModifications(EntityMapPtr const& entityMap, TileModificationList const& modificationList,
bool allowEntityOverlap, GetTileFunction& getTile, function<bool(Vec2I pos, TileModification modification)> extraCheck = {});
template <typename TileSectorArray>
float windLevel(shared_ptr<TileSectorArray> const& tileSectorArray, Vec2F const& position, float weatherWindLevel);
template <typename TileSectorArray>
float temperature(shared_ptr<TileSectorArray> const& tileSectorArray, WorldTemplateConstPtr const& worldTemplate,
SkyConstPtr const& sky, Vec2F const& pos);
template <typename TileSectorArray>
bool breathable(World const* world, shared_ptr<TileSectorArray> const& tileSectorArray, WorldTemplateConstPtr const& worldTemplate, Vec2F const& pos);
template <typename TileSectorArray>
float lightLevel(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const& entityMap, WorldGeometry const& worldGeometry,
WorldTemplateConstPtr const& worldTemplate, SkyConstPtr const& sky, CellularLightIntensityCalculator& lighting, Vec2F pos);
InteractiveEntityPtr getInteractiveInRange(WorldGeometry const& geometry, EntityMapPtr const& entityMap,
Vec2F const& targetPosition, Vec2F const& sourcePosition, float maxRange);
bool isTileEntityInRange(WorldGeometry const& geometry, EntityMapPtr const& entityMap,
EntityId targetEntity, Vec2F const& sourcePosition, float maxRange);
template <typename TileSectorArray>
bool canReachEntity(WorldGeometry const& geometry, shared_ptr<TileSectorArray> const& tileSectorArray,
EntityMapPtr const& entityMap, Vec2F const& sourcePosition, float maxRange, EntityId targetEntity, bool preferInteractive = true);
template <typename TileSectorArray>
bool tileIsOccupied(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, bool includeEphemeral, bool checkCollision) {
auto& tile = tileSectorArray->tile(pos);
if (layer == TileLayer::Foreground)
return (checkCollision ? tile.getCollision() >= CollisionKind::Dynamic : tile.foreground != EmptyMaterialId) || entityMap->tileIsOccupied(pos, includeEphemeral);
else
return tile.background != EmptyMaterialId;
}
template <typename TileSectorArray>
CollisionKind tileCollisionKind(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const&,
Vec2I const& pos) {
return tileSectorArray->tile(pos).getCollision();
}
template <typename TileSectorArray>
bool rectTileCollision(shared_ptr<TileSectorArray> const& tileSectorArray, RectI const& region, CollisionSet const& collisionSet) {
return tileSectorArray->tileSatisfies(region, [&collisionSet](Vec2I const&, typename TileSectorArray::Tile const& tile) {
return isColliding(tile.getCollision(), collisionSet);
});
}
template <typename TileSectorArray>
bool lineTileCollision(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray,
Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet) {
return !forBlocksAlongLine<float>(begin, worldGeometry.diff(end, begin), [=](int x, int y) -> bool {
return !tileSectorArray->tile({x, y}).isColliding(collisionSet);
});
}
template <typename TileSectorArray>
Maybe<pair<Vec2F, Vec2I>> lineTileCollisionPoint(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray,
Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet) {
Maybe<Vec2I> collidingBlock;
auto clear = forBlocksAlongLine<float>(begin, worldGeometry.diff(end, begin), [=, &collidingBlock](int x, int y) -> bool {
if (tileSectorArray->tile({x, y}).isColliding(collisionSet)) {
collidingBlock = Vec2I(x, y);
return false;
} else {
return true;
}
});
if (clear)
return {}; // no colliding blocks along the line
Vec2F direction = worldGeometry.diff(end, begin).normalized();
Vec2F blockPosition = Vec2F(*collidingBlock);
// find the position of intersecting sides
if (direction[0] < 0) blockPosition[0] = blockPosition[0] + 1;
if (direction[1] < 0) blockPosition[1] = blockPosition[1] + 1;
Vec2F blockDistance = worldGeometry.diff(blockPosition, begin);
// exclude edges which are in the opposite direction of the line
if (direction[0] * blockDistance[0] < 0) blockDistance[0] = 0.0f;
if (direction[1] * blockDistance[1] < 0) blockDistance[1] = 0.0f;
// line length per block in each axis, don't divide by 0
float deltaDistX = direction[0] == 0 ? 0.0f : 1.0f / abs(direction[0]);
float deltaDistY = direction[1] == 0 ? 0.0f : 1.0f / abs(direction[1]);
// distance to each edge of the intersect
Vec2F intersectDist = Vec2F(abs(blockDistance[0]) * deltaDistX, abs(blockDistance[1]) * deltaDistY);
Vec2I normal = Vec2I(intersectDist[0] > intersectDist[1] ? 1 : 0, intersectDist[1] > intersectDist[0] ? 1 : 0);
normal[0] *= copysign(1.0f, direction[0]);
normal[1] *= copysign(1.0f, direction[1]);
Vec2F position = begin + (direction * max(intersectDist[0], intersectDist[1]));
// HACK: Keeps the end position from being right on a tile border, making line check
// *from* this end position from colliding with the first tile
if (intersectDist[0] > intersectDist[1])
position[0] = blockPosition[0] + (normal[0] * -0.0001f);
else
position[1] = blockPosition[1] + (normal[1] * -0.0001f);
return pair<Vec2F, Vec2I>(position, normal);
}
template <typename TileSectorArray>
inline LiquidLevel liquidLevel(shared_ptr<TileSectorArray> const& tileSectorArray, RectF const& region) {
if (region.isEmpty())
return LiquidLevel();
RectI sampleRect = RectI::integral(region);
// This is not entirely accurate, even though all liquid types in the
// region count towards the grand total liquid percentage, only the most
// common liquid is reported.
float totalSpace = 0;
Map<LiquidId, float> totals;
tileSectorArray->tileEach(sampleRect, [®ion, &totalSpace, &totals](Vec2I const& pos, typename TileSectorArray::Tile const& tile) {
float blockIncidence = RectF(pos[0], pos[1], pos[0] + 1, pos[1] + 1).overlap(region).volume();
totalSpace += blockIncidence;
auto liquidLevel = tile.liquid;
if (liquidLevel.liquid != EmptyLiquidId)
totals[liquidLevel.liquid] += min(1.0f, liquidLevel.level) * blockIncidence;
});
float totalLiquidLevel = 0.0f;
float maximumLiquidLevel = 0.0f;
LiquidId maximumLiquidId = EmptyLiquidId;
for (auto const& p : totals) {
totalLiquidLevel += p.second;
if (p.second > maximumLiquidLevel) {
maximumLiquidLevel = p.second;
maximumLiquidId = p.first;
}
}
return LiquidLevel(maximumLiquidId, totalLiquidLevel / totalSpace);
}
template <typename TileSectorArray>
List<Vec2I> collidingTilesAlongLine(WorldGeometry const& worldGeometry, shared_ptr<TileSectorArray> const& tileSectorArray,
Vec2F const& begin, Vec2F const& end, CollisionSet const& collisionSet, size_t maxSize, bool includeEdges) {
List<Vec2I> res;
forBlocksAlongLine<float>(begin, worldGeometry.diff(end, begin), [&](int x, int y) {
if (res.size() >= maxSize)
return false;
if (tileSectorArray->tile({x, y}).isColliding(collisionSet))
res.append({x, y});
return true;
});
if (includeEdges)
return res;
SMutableIterator<List<Vec2I>> tileIt{res};
while (tileIt.hasNext()) {
auto tileRect = RectF::withSize(Vec2F(worldGeometry.xwrap(tileIt.next())), Vec2F::filled(1.0f));
Line2F line{worldGeometry.xwrap(begin), worldGeometry.xwrap(end)};
auto lineSet = worldGeometry.splitLine(line);
if (any(lineSet, [&](Line2F const& line) { return tileRect.edgeIntersection(line).glances; }))
tileIt.remove();
}
return res;
}
inline TileDamageParameters tileDamageParameters(WorldTile* tile, TileLayer layer, TileDamage const& tileDamage) {
bool foreground = layer == TileLayer::Foreground;
auto materialDatabase = Root::singleton().materialDatabase();
auto target = foreground ? tile->foreground : tile->background;
auto mod = foreground ? tile->foregroundMod : tile->backgroundMod;
if (!isRealMod(mod))
return materialDatabase->materialDamageParameters(target);
if (tileDamageIsPenetrating(tileDamage.type))
return materialDatabase->materialDamageParameters(target);
if (materialDatabase->modBreaksWithTile(mod))
return materialDatabase->modDamageParameters(mod).sum(materialDatabase->materialDamageParameters(target));
return materialDatabase->modDamageParameters(mod);
}
template <typename TileSectorArray>
bool damageWouldDestroy(shared_ptr<TileSectorArray> const& tileSectorArray, Vec2I pos, TileLayer layer, TileDamage const& tileDamage) {
if (auto tile = tileSectorArray->modifyTile(pos)) {
auto damageParameters = tileDamageParameters(tile, layer, tileDamage);
float percentageDelta = damageParameters.damageDone(tileDamage) / damageParameters.totalHealth();
auto damage = layer == TileLayer::Foreground ? tile->foregroundDamage : tile->backgroundDamage;
return percentageDelta + damage.damagePercentage() >= 1.0f;
}
return false;
}
template <typename GetTileFunction>
bool canPlaceMaterial(EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, MaterialId material, bool allowEntityOverlap, bool allowTileOverlap, GetTileFunction& getTile) {
auto materialDatabase = Root::singleton().materialDatabase();
if (!isRealMaterial(material))
return false;
auto isAdjacentToConnectable = [&](Vec2I const& pos, int distance, bool foreground) {
if (pos.y() - distance < 0)
return true;
int maxY = pos.y() + distance + 1;
int maxX = pos.x() + distance + 1;
for (int y = pos.y() - distance; y != maxY; ++y) {
Vec2I tPos = { 0, y };
for (int x = pos.x() - distance; x != maxX; ++x) {
tPos[0] = x;
if (tPos != pos) {
const auto& tile = getTile(tPos);
if (isConnectableMaterial(foreground ? tile.foreground : tile.background))
return true;
}
}
}
return false;
};
if (!materialDatabase->canPlaceInLayer(material, layer))
return false;
auto& tile = getTile(pos);
if (layer == TileLayer::Background) {
if (tile.background != EmptyMaterialId && tile.background != ObjectPlatformMaterialId)
return false;
// Can attach background blocks to other background blocks, *or* the
// foreground block in front of it.
if (!isAdjacentToConnectable(pos, 1, false) && !isConnectableMaterial(tile.foreground))
return false;
} else {
if (tile.foreground != EmptyMaterialId && tile.foreground != ObjectPlatformMaterialId)
return false;
if (!allowTileOverlap && entityMap->tileIsOccupied(pos))
return false;
if (!allowEntityOverlap && entityMap->spaceIsOccupied(RectF::withSize(Vec2F(pos), Vec2F(0.999f, 0.999f))))
return false;
if (!isAdjacentToConnectable(pos, 1, true) && !isConnectableMaterial(tile.background))
return false;
}
return true;
}
template <typename GetTileFunction>
bool perhapsCanPlaceMaterial(EntityMapPtr const& entityMap,
Vec2I const& pos, TileLayer layer, MaterialId material, bool allowEntityOverlap, bool allowTileOverlap, GetTileFunction& getTile) {
auto materialDatabase = Root::singleton().materialDatabase();
if (!isRealMaterial(material))
return false;
if (!materialDatabase->canPlaceInLayer(material, layer))
return false;
const auto& tile = getTile(pos);
if (layer == TileLayer::Background) {
if (tile.background != EmptyMaterialId && tile.background != ObjectPlatformMaterialId)
return false;
} else {
if (tile.foreground != EmptyMaterialId && tile.foreground != ObjectPlatformMaterialId)
return false;
if (!allowTileOverlap && entityMap->tileIsOccupied(pos))
return false;
if (!allowEntityOverlap && entityMap->spaceIsOccupied(RectF::withSize(Vec2F(pos), Vec2F(0.999f, 0.999f))))
return false;
}
return true;
}
template <typename GetTileFunction>
bool canPlaceMaterialColorVariant(Vec2I const& pos, TileLayer layer, MaterialColorVariant color, GetTileFunction& getTile) {
auto materialDatabase = Root::singleton().materialDatabase();
auto& tile = getTile(pos);
auto mat = tile.material(layer);
auto existingColor = tile.materialColor(layer);
auto existingHue = layer == TileLayer::Foreground ? tile.foregroundHueShift : tile.backgroundHueShift;
return existingHue != 0 || (existingColor != color && materialDatabase->isMultiColor(mat));
}
template <typename GetTileFunction>
bool canPlaceMod(Vec2I const& pos, TileLayer layer, ModId mod, GetTileFunction& getTile) {
if (!isRealMod(mod))
return false;
auto materialDatabase = Root::singleton().materialDatabase();
auto mat = getTile(pos).material(layer);
auto existingMod = getTile(pos).mod(layer);
return existingMod != mod && materialDatabase->supportsMod(mat, mod);
}
template <typename GetTileFunction>
pair<bool, bool> validateTileModification(EntityMapPtr const& entityMap, Vec2I const& pos, TileModification const& modification, bool allowEntityOverlap, GetTileFunction& getTile) {
bool good = false;
bool perhaps = false;
if (auto placeMaterial = modification.ptr<PlaceMaterial>()) {
bool allowTileOverlap = placeMaterial->collisionOverride != TileCollisionOverride::None && collisionKindFromOverride(placeMaterial->collisionOverride) < CollisionKind::Dynamic;
perhaps = WorldImpl::perhapsCanPlaceMaterial(entityMap, pos, placeMaterial->layer, placeMaterial->material, allowEntityOverlap, allowTileOverlap, getTile);
if (perhaps)
good = WorldImpl::canPlaceMaterial(entityMap, pos, placeMaterial->layer, placeMaterial->material, allowEntityOverlap, allowTileOverlap, getTile);
} else if (auto placeMod = modification.ptr<PlaceMod>()) {
good = WorldImpl::canPlaceMod(pos, placeMod->layer, placeMod->mod, getTile);
} else if (auto placeMaterialColor = modification.ptr<PlaceMaterialColor>()) {
good = WorldImpl::canPlaceMaterialColorVariant(pos, placeMaterialColor->layer, placeMaterialColor->color, getTile);
} else if (modification.is<PlaceLiquid>()) {
good = getTile(pos).collision == CollisionKind::None;
} else {
good = false;
}
return { good, perhaps };
}
inline bool validateTileReplacement(TileModification const& modification) {
if (auto placeMaterial = modification.ptr<PlaceMaterial>()) {
if (!isRealMaterial(placeMaterial->material))
return false;
auto materialDatabase = Root::singleton().materialDatabase();
if (!materialDatabase->canPlaceInLayer(placeMaterial->material, placeMaterial->layer))
return false;
return true;
}
return false;
}
template <typename GetTileFunction>
pair<TileModificationList, TileModificationList> splitTileModifications(EntityMapPtr const& entityMap, TileModificationList const& modificationList,
bool allowEntityOverlap, GetTileFunction& getTile, function<bool(Vec2I pos, TileModification modification)> extraCheck) {
TileModificationList success;
TileModificationList unknown;
TileModificationList failures;
for (auto const& pair : modificationList) {
bool good = false, perhaps = false;
if (!extraCheck || extraCheck(pair.first, pair.second))
std::tie(good, perhaps) = validateTileModification(entityMap, pair.first, pair.second, allowEntityOverlap, getTile);
if (good)
success.append(pair);
else if (perhaps)
unknown.append(pair);
else
failures.append(pair);
}
if (!success.empty())
success.appendAll(unknown);
else
failures.appendAll(unknown);
return {success, failures};
}
template <typename TileSectorArray>
float windLevel(shared_ptr<TileSectorArray> const& tileSectorArray, Vec2F const& position, float weatherWindLevel) {
auto const& tile = tileSectorArray->tile(Vec2I::floor(position));
if (tile.material(TileLayer::Background) != EmptyMaterialId)
return 0.0f;
if (tile.material(TileLayer::Foreground) != EmptyMaterialId)
return 0.0f;
return weatherWindLevel;
}
template <typename TileSectorArray>
bool breathable(World const* world, shared_ptr<TileSectorArray> const& tileSectorArray, HashMap<DungeonId, bool> const& breathableMap,
WorldTemplateConstPtr const& worldTemplate, Vec2F const& pos) {
Vec2I ipos = Vec2I::floor(pos);
float remainder = pos[1] - ipos[1];
auto materialDatabase = Root::singleton().materialDatabase();
auto liquidsDatabase = Root::singleton().liquidsDatabase();
auto tile = tileSectorArray->tile(ipos);
bool environmentBreathable = breathableMap.maybe(tile.dungeonId).value(worldTemplate->breathable(ipos[0], ipos[1]));
bool liquidBreathable = remainder >= tile.liquid.level;
bool foregroundBreathable = tile.getCollision() != CollisionKind::Block || !world->pointCollision(pos);
return environmentBreathable && foregroundBreathable && liquidBreathable;
}
template <typename TileSectorArray>
float lightLevel(shared_ptr<TileSectorArray> const& tileSectorArray, EntityMapPtr const& entityMap, WorldGeometry const& worldGeometry,
WorldTemplateConstPtr const& worldTemplate, SkyConstPtr const& sky, CellularLightIntensityCalculator& lighting, Vec2F pos) {
if (pos[1] < 0 || pos[1] >= worldGeometry.height())
return 0;
// tileEach can't handle rects that are WAY out of range.
pos = worldGeometry.xwrap(pos);
Vec3F environmentLight = sky->environmentLight().toRgbF();
float undergroundLevel = worldTemplate->undergroundLevel();
auto materialDatabase = Root::singleton().materialDatabase();
auto liquidsDatabase = Root::singleton().liquidsDatabase();
lighting.begin(pos);
// Each column in tileEvalColumns is guaranteed to be no larger than the
// sector size.
CellularLightIntensityCalculator::Cell lightingCellColumn[WorldSectorSize];
tileSectorArray->tileEvalColumns(lighting.calculationRegion(), [&](Vec2I const& pos, typename TileSectorArray::Tile const* column, size_t ySize) {
for (size_t y = 0; y < ySize; ++y) {
auto& tile = column[y];
auto& cell = lightingCellColumn[y];
bool backgroundTransparent = materialDatabase->backgroundLightTransparent(tile.background);
bool foregroundTransparent = materialDatabase->foregroundLightTransparent(tile.foreground)
&& tile.getCollision() != CollisionKind::Dynamic;
cell = {materialDatabase->radiantLight(tile.foreground, tile.foregroundMod).sum() / 3.0f, !foregroundTransparent};
cell.light += liquidsDatabase->radiantLight(tile.liquid).sum() / 3.0f;
if (foregroundTransparent) {
cell.light += materialDatabase->radiantLight(tile.background, tile.backgroundMod).sum() / 3.0f;
if (backgroundTransparent && pos[1] > undergroundLevel)
cell.light += environmentLight.sum() / 3.0f;
}
}
lighting.setCellColumn(pos, lightingCellColumn, ySize);
});
for (auto const& entity : entityMap->entityQuery(RectF(lighting.calculationRegion()))) {
for (auto const& light : entity->lightSources()) {
Vec2F position = worldGeometry.nearestTo(Vec2F(lighting.calculationRegion().min()), light.position);
if (light.type == LightType::Spread)
lighting.addSpreadLight(position, light.color.sum() / 3.0f);
else
lighting.addPointLight(position, light.color.sum() / 3.0f, light.pointBeam, light.beamAngle, light.beamAmbience);
}
}
return lighting.calculate();
}
inline InteractiveEntityPtr getInteractiveInRange(WorldGeometry const& geometry, EntityMapPtr const& entityMap,
Vec2F const& targetPosition, Vec2F const& sourcePosition, float maxRange) {
if (auto entity = entityMap->interactiveEntityNear(targetPosition, maxRange)) {
if (auto tileEntity = as<TileEntity>(entity)) {
if (isTileEntityInRange(geometry, entityMap, tileEntity->entityId(), sourcePosition, maxRange))
return entity;
} else {
if (geometry.diffToNearestCoordInBox(entity->interactiveBoundBox().translated(entity->position()), sourcePosition) .magnitude() <= maxRange)
return entity;
}
}
return {};
}
inline bool isTileEntityInRange(WorldGeometry const& geometry, EntityMapPtr const& entityMap, EntityId targetEntity,
Vec2F const& sourcePosition, float maxRange) {
if (auto entity = entityMap->get<TileEntity>(targetEntity)) {
// If any of the entity spaces is within interaction range, then assume the
// whole entity is in interaction range
for (auto space : entity->spaces()) {
if (geometry.diff(entity->position() + centerOfTile(space), sourcePosition).magnitude() <= maxRange)
return true;
}
}
return false;
}
template <typename TileSectorArray>
bool canReachEntity(WorldGeometry const& geometry, shared_ptr<TileSectorArray> const& tileSectorArray,
EntityMapPtr const& entityMap, Vec2F const& sourcePosition, float maxRange, EntityId targetEntity, bool preferInteractive) {
auto entity = entityMap->entity(targetEntity);
if (!entity)
return false;
// exclude the final tile from the collision check since many targets will be collidable tile entities, e.g. doors
auto const canReachTile = [&](Vec2F const& end) -> bool {
Vec2I const& endTile = Vec2I::floor(end);
return forBlocksAlongLine<float>(sourcePosition, geometry.diff(end, sourcePosition), [=](int x, int y) -> bool {
Vec2I diff = geometry.diff(endTile, Vec2I(x, y));
if (diff[0] == 0 && diff[1] == 0)
return true;
return !tileSectorArray->tile({x, y}).isColliding(DefaultCollisionSet);
});
};
if (auto tileEntity = as<TileEntity>(entity)) {
for (auto space : preferInteractive ? tileEntity->interactiveSpaces() : tileEntity->spaces()) {
auto spacePosition = entity->position() + centerOfTile(space);
if (geometry.diff(spacePosition, sourcePosition).magnitude() <= maxRange) {
if (canReachTile(spacePosition))
return true;
}
}
} else if (preferInteractive && is<InteractiveEntity>(entity)) {
auto interactiveEntity = as<InteractiveEntity>(entity);
auto entityBounds = interactiveEntity->interactiveBoundBox().translated(entity->position());
if (geometry.rectContains(entityBounds, sourcePosition))
return true;
if (!geometry.rectIntersectsCircle(entityBounds, sourcePosition, maxRange))
return false;
return
!lineTileCollision(geometry, tileSectorArray, sourcePosition, entityBounds.nearestCoordTo(sourcePosition), DefaultCollisionSet) ||
!lineTileCollision(geometry, tileSectorArray, sourcePosition, Vec2F(entityBounds.xMin(), entityBounds.yMin()), DefaultCollisionSet) ||
!lineTileCollision(geometry, tileSectorArray, sourcePosition, Vec2F(entityBounds.xMin(), entityBounds.yMax()), DefaultCollisionSet) ||
!lineTileCollision(geometry, tileSectorArray, sourcePosition, Vec2F(entityBounds.xMax(), entityBounds.yMax()), DefaultCollisionSet) ||
!lineTileCollision(geometry, tileSectorArray, sourcePosition, Vec2F(entityBounds.xMax(), entityBounds.yMin()), DefaultCollisionSet);
} else {
if (geometry.diff(entity->position(), sourcePosition).magnitude() <= maxRange)
return !lineTileCollision(geometry, tileSectorArray, sourcePosition, entity->position(), DefaultCollisionSet);
}
return false;
}
}
}
|