diff --git a/assets/images/vaults/house1.png b/assets/images/vaults/house1.png new file mode 100644 index 0000000..39f02f7 Binary files /dev/null and b/assets/images/vaults/house1.png differ diff --git a/lib/algorithms/regionalize.dart b/lib/algorithms/regionalize.dart new file mode 100644 index 0000000..9369dee --- /dev/null +++ b/lib/algorithms/regionalize.dart @@ -0,0 +1,66 @@ +import 'dart:math' as math; + +class Region { + final math.Rectangle rect; + final Set<(int, int)> points; + + Region(this.rect, this.points); + + static fromNonEmptySet(Set<(int, int)> s) { + assert(s.isNotEmpty); + int xMin = s.map((xy) => xy.$1).reduce(math.min); + int yMin = s.map((xy) => xy.$2).reduce(math.min); + int xMax = s.map((xy) => xy.$1).reduce(math.max); + int yMax = s.map((xy) => xy.$2).reduce(math.max); + var rect = math.Rectangle.fromPoints( + math.Point(xMin, yMin), math.Point(xMax, yMax)); + Region(rect, s); + } +} + +List regionalize( + math.Rectangle rect, bool Function(int, int) isAccessible) { + int nextRegion = 0; + Map<(int, int), int> regions = {}; + + void floodfill(int x, int y, region) { + if (!rect.containsPoint(math.Point(x, y))) { + return; + } + if (regions[(x, y)] != null) { + return; + } + if (!isAccessible(x, y)) { + return; + } + + regions[(x, y)] = region; + floodfill(x - 1, y, region); + floodfill(x + 1, y, region); + floodfill(x, y - 1, region); + floodfill(x, y + 1, region); + } + + // TODO: This can be done more efficiently with a union/find data structure + // But this is an easy implementation to understand + for (var y = rect.top; y < rect.bottom; y++) { + for (var x = rect.left; x < rect.right; x++) { + if (regions[(x, y)] == null) { + floodfill(x, y, nextRegion); + nextRegion += 1; + } + } + } + return _toExplicit(regions); +} + +List _toExplicit(Map<(int, int), int> regions) { + List> pointsOut = [ + for (var i = 0; i < regions.length; i++) Set() + ]; + for (var MapEntry(key: (x, y), value: id_) in regions.entries) { + pointsOut[id_].add((x, y)); + } + + return [for (var s in pointsOut) Region.fromNonEmptySet(s)]; +} diff --git a/lib/gen/generator.dart b/lib/gen/generator.dart index 4e2f397..4d2465e 100644 --- a/lib/gen/generator.dart +++ b/lib/gen/generator.dart @@ -10,9 +10,12 @@ part 'requirement.dart'; part 'vault.dart'; part 'vaults.dart'; +const vaultTries = 10; + class Generator { final math.Random _random; final Vaults _vaults; + List _queue = []; Generator(this._random, this._vaults); @@ -32,7 +35,10 @@ class Generator { } Vault generate(Requirement requirement) { - // TODO: Pick a relevant vault from the vaults file if possible + Vault? suggested = _suggest(vaultTries, requirement); + if (suggested != null) { + return suggested; + } // First of all: randomize orientation // This way we only have to consider one kind of spilt @@ -78,4 +84,74 @@ class Generator { return v; } + + Vault? _suggest(int tries, Requirement req) { + for (var i = 0; i < tries; i++) { + var sugg = _popSuggestion(); + if (sugg == null) { + return null; + } + + sugg = reorientVault(sugg, randomOrientation(_random)); + sugg = _tidy(sugg, req); + if (sugg != null) { + return sugg; + } + } + return null; + } + + Vault? _popSuggestion() { + if (_queue.isEmpty) { + _queue = _vaults.randomFlight(_random); + } + if (_queue.isEmpty) { + return null; + } + return _queue.removeLast(); + } + + Vault? _tidy(Vault vault, Requirement req) { + if (vault.vx > req.vx || vault.vy > req.vy) { + return null; + } + if (vault.vx < req.vx / 2 || vault.vy < req.vy / 2) { + return null; + } + + var rsd = req.smooth.directions; + bool mustFillX = + rsd.contains(Direction.left) && rsd.contains(Direction.right); + if (vault.vx != req.vx && mustFillX) { + return null; + } + bool mustFillY = + rsd.contains(Direction.left) && rsd.contains(Direction.right); + if (vault.vy != req.vy && mustFillY) { + return null; + } + + Vault full = Vault.blank(req.vx, req.vy, req.smooth, LevelTile.wall); + int vx = vault.vx; + int dx; + if (rsd.contains(Direction.left)) { + dx = 0; + } else if (rsd.contains(Direction.right)) { + dx = req.vx - vx; + } else { + dx = _random.nextInt(req.vx - vx); + } + + int vy = vault.vy; + int dy; + if (rsd.contains(Direction.up)) { + dy = 0; + } else if (rsd.contains(Direction.down)) { + dy = req.vy - vy; + } else { + dy = _random.nextInt(req.vx - vx); + } + full.blitFrom(vault, dx, dy); + return full; + } } diff --git a/lib/gen/vaults.dart b/lib/gen/vaults.dart index e8871f8..c5973e1 100644 --- a/lib/gen/vaults.dart +++ b/lib/gen/vaults.dart @@ -7,4 +7,12 @@ class Vaults { // TODO return Vaults(); } + + List randomFlight(math.Random rng) { + // TODO: There are many more efficient ways to do this! + List list2 = []; + list2.addAll(_primitive); + list2.shuffle(rng); + return list2; + } } diff --git a/pubspec.yaml b/pubspec.yaml index 674917d..5fdb915 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -62,6 +62,7 @@ flutter: assets: - assets/images/fonts/ - assets/images/wfc/ + - assets/images/vaults/ # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg