Use vaults if we have them

This commit is contained in:
Pyrex 2023-09-20 19:30:03 -07:00
parent 0a600fd930
commit b5466919e6
5 changed files with 152 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 844 B

View File

@ -0,0 +1,66 @@
import 'dart:math' as math;
class Region {
final math.Rectangle<int> rect;
final Set<(int, int)> points;
Region(this.rect, this.points);
static fromNonEmptySet(Set<(int, int)> s) {
assert(s.isNotEmpty);
int xMin = s.map<int>((xy) => xy.$1).reduce(math.min);
int yMin = s.map<int>((xy) => xy.$2).reduce(math.min);
int xMax = s.map<int>((xy) => xy.$1).reduce(math.max);
int yMax = s.map<int>((xy) => xy.$2).reduce(math.max);
var rect = math.Rectangle.fromPoints(
math.Point(xMin, yMin), math.Point(xMax, yMax));
Region(rect, s);
}
}
List<Region> regionalize(
math.Rectangle<int> 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<Region> _toExplicit(Map<(int, int), int> regions) {
List<Set<(int, int)>> 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)];
}

View File

@ -10,9 +10,12 @@ part 'requirement.dart';
part 'vault.dart'; part 'vault.dart';
part 'vaults.dart'; part 'vaults.dart';
const vaultTries = 10;
class Generator { class Generator {
final math.Random _random; final math.Random _random;
final Vaults _vaults; final Vaults _vaults;
List<Vault> _queue = [];
Generator(this._random, this._vaults); Generator(this._random, this._vaults);
@ -32,7 +35,10 @@ class Generator {
} }
Vault generate(Requirement requirement) { 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 // First of all: randomize orientation
// This way we only have to consider one kind of spilt // This way we only have to consider one kind of spilt
@ -78,4 +84,74 @@ class Generator {
return v; 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;
}
} }

View File

@ -7,4 +7,12 @@ class Vaults {
// TODO // TODO
return Vaults(); return Vaults();
} }
List<Vault> randomFlight(math.Random rng) {
// TODO: There are many more efficient ways to do this!
List<Vault> list2 = [];
list2.addAll(_primitive);
list2.shuffle(rng);
return list2;
}
} }

View File

@ -62,6 +62,7 @@ flutter:
assets: assets:
- assets/images/fonts/ - assets/images/fonts/
- assets/images/wfc/ - assets/images/wfc/
- assets/images/vaults/
# - images/a_dot_burr.jpeg # - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg # - images/a_dot_ham.jpeg