Compare commits

...

2 Commits

Author SHA1 Message Date
b5466919e6 Use vaults if we have them 2023-09-20 19:30:03 -07:00
0a600fd930 Break up the vaults code into multiple files 2023-09-20 18:09:08 -07:00
11 changed files with 446 additions and 249 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

@ -29,11 +29,10 @@ void main() async {
int seed = 0;
while (true) {
Vault output = vaults.generateBoxed(
math.Random(seed),
Vault output = Generator(math.Random(seed), vaults).generateBoxed(
Requirement(
16,
16,
32,
24,
DirectionSet({
Direction.up,
Direction.left,
@ -42,8 +41,8 @@ void main() async {
})));
var w = output.vx;
var h = output.vy;
for (var y = 0; y < w; y++) {
for (var x = 0; x < h; x++) {
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
var cursor = at(x * 2, y * 2).big();
switch (output.tiles[x + y * w]) {
case LevelTile.floor:

8
lib/gen/direction.dart Normal file
View File

@ -0,0 +1,8 @@
part of 'generator.dart';
enum Direction {
up,
left,
down,
right,
}

View File

@ -0,0 +1,65 @@
part of "generator.dart";
// TODO: There are many more efficient ways to do this
class DirectionSet {
final Set<Direction> directions;
DirectionSet(this.directions);
DirectionSet flip() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.up);
case Direction.left:
ds2.directions.add(Direction.right);
case Direction.down:
ds2.directions.add(Direction.down);
case Direction.right:
ds2.directions.add(Direction.left);
}
}
return ds2;
}
DirectionSet rotateLeft() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.left);
case Direction.left:
ds2.directions.add(Direction.down);
case Direction.down:
ds2.directions.add(Direction.right);
case Direction.right:
ds2.directions.add(Direction.up);
}
}
return ds2;
}
DirectionSet rotateRight() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.right);
case Direction.right:
ds2.directions.add(Direction.down);
case Direction.down:
ds2.directions.add(Direction.left);
case Direction.left:
ds2.directions.add(Direction.up);
}
}
return ds2;
}
DirectionSet clone() {
var ds2 = DirectionSet({});
ds2.directions.addAll(directions);
return ds2;
}
}

View File

@ -3,57 +3,56 @@ import 'dart:math' as math;
import 'package:dartterm/world/level.dart';
enum Direction {
up,
left,
down,
right,
}
part 'direction.dart';
part 'direction_set.dart';
part 'orientation.dart';
part 'requirement.dart';
part 'vault.dart';
part 'vaults.dart';
class Vaults {
List<Vault> _primitive = [];
const vaultTries = 10;
static Future<Vaults> load(String filename) async {
// TODO
return Vaults();
}
class Generator {
final math.Random _random;
final Vaults _vaults;
List<Vault> _queue = [];
Vault generateBoxed(math.Random random, Requirement req) {
Generator(this._random, this._vaults);
Vault generateBoxed(Requirement req) {
var vx = req.vx;
var vy = req.vy;
var tiles = [
for (var y = 0; y < vy; y++)
for (var x = 0; x < vx; x++) LevelTile.wall
];
var v = Vault(tiles, vx, vy, req.smooth);
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
if (req.vx < 2 || req.vy < 2) {
return v;
}
var req2 = Requirement(vx - 2, vy - 2, req.smooth);
var inner = generate(random, req2);
var inner = generate(req2);
v.blitFrom(inner, 1, 1);
return v;
}
Vault generate(math.Random random, Requirement requirement) {
// TODO: Pick a relevant vault from the vaults file if possible
//
Vault generate(Requirement requirement) {
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
var orientation = randomOrientation(random);
var orientation = randomOrientation(_random);
// Try to make vx the long axis if possible
var req2 = requirement.unReorient(orientation);
var req2 = unReorientRequirement(requirement, orientation);
if (req2.vy > (req2.vx - 2) * 3 / 2) {
orientation = (orientation + 2) % 8; // rotate once more
}
req2 = requirement.unReorient(orientation);
req2 = unReorientRequirement(requirement, orientation);
var out2 = _generate(random, req2);
var out1 = out2.reorient(orientation);
var out2 = _generate(req2);
var out1 = reorientVault(out2, orientation);
log("$orientation ${requirement.vx} ${requirement.vy} ${req2.vx} ${req2.vy} ${out2.vx} ${out2.vy} ${out1.vx} ${out1.vy}");
assert(out1.vx == requirement.vx);
@ -61,248 +60,98 @@ class Vaults {
return out1;
}
Vault _generate(math.Random random, Requirement req) {
Vault _generate(Requirement req) {
var vx = req.vx;
var vy = req.vy;
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
var tiles = [
for (var y = 0; y < vy; y++)
for (var x = 0; x < vx; x++) LevelTile.wall
];
var v = Vault(tiles, vx, vy, req.smooth);
if (vx < 3 || vx * vy < 10) {
if (vx < 5 || vx * vy < 10) {
v.clear(LevelTile.floor);
} else {
// pick a split point
var splitVx = random.nextInt(vx - 2) + 1;
var splitVx = _random.nextInt(vx - 4) + 2;
var reqLeft = Requirement(splitVx, vy, req.smooth.clone());
reqLeft.smooth.directions.add(Direction.right);
var reqRight = Requirement((vx - splitVx - 1), vy, req.smooth.clone());
reqRight.smooth.directions.add(Direction.left);
var vaultLeft = generate(random, reqLeft);
var vaultRight = generate(random, reqRight);
var vaultLeft = generate(reqLeft);
var vaultRight = generate(reqRight);
v.blitFrom(vaultLeft, 0, 0);
v.blitFrom(vaultRight, splitVx + 1, 0);
}
return v;
}
Vault? _suggest(int tries, Requirement req) {
for (var i = 0; i < tries; i++) {
var sugg = _popSuggestion();
if (sugg == null) {
return null;
}
// TODO: There are many more efficient ways to do this
class DirectionSet {
final Set<Direction> directions;
DirectionSet(this.directions);
DirectionSet flip() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.up);
case Direction.left:
ds2.directions.add(Direction.right);
case Direction.down:
ds2.directions.add(Direction.down);
case Direction.right:
ds2.directions.add(Direction.left);
sugg = reorientVault(sugg, randomOrientation(_random));
sugg = _tidy(sugg, req);
if (sugg != null) {
return sugg;
}
}
return ds2;
return null;
}
DirectionSet rotateLeft() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.left);
case Direction.left:
ds2.directions.add(Direction.down);
case Direction.down:
ds2.directions.add(Direction.right);
case Direction.right:
ds2.directions.add(Direction.up);
Vault? _popSuggestion() {
if (_queue.isEmpty) {
_queue = _vaults.randomFlight(_random);
}
if (_queue.isEmpty) {
return null;
}
return ds2;
return _queue.removeLast();
}
DirectionSet rotateRight() {
var ds2 = DirectionSet({});
for (var i in directions) {
switch (i) {
case Direction.up:
ds2.directions.add(Direction.right);
case Direction.right:
ds2.directions.add(Direction.down);
case Direction.down:
ds2.directions.add(Direction.left);
case Direction.left:
ds2.directions.add(Direction.up);
Vault? _tidy(Vault vault, Requirement req) {
if (vault.vx > req.vx || vault.vy > req.vy) {
return null;
}
}
return ds2;
if (vault.vx < req.vx / 2 || vault.vy < req.vy / 2) {
return null;
}
DirectionSet clone() {
var ds2 = DirectionSet({});
ds2.directions.addAll(directions);
return ds2;
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;
}
class Requirement {
final int vx, vy;
final DirectionSet smooth;
Requirement(this.vx, this.vy, this.smooth);
Requirement flip() {
return Requirement(vx, vy, smooth.flip());
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);
}
Requirement rotateLeft() {
return Requirement(vy, vx, smooth.rotateLeft());
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);
}
Requirement rotateRight() {
return Requirement(vy, vx, smooth.rotateRight());
}
Requirement unReorient(int r) {
assert(r >= 0 && r < 8);
Requirement o = this;
if (r % 2 == 1) {
o = o.flip();
r -= 1;
}
while (r >= 2) {
o = o.rotateLeft();
r -= 2;
}
return o;
full.blitFrom(vault, dx, dy);
return full;
}
}
class Vault {
final List<LevelTile> tiles;
final int vx, vy;
final DirectionSet smooth;
Vault(this.tiles, this.vx, this.vy, this.smooth) {
assert(tiles.length == vx * vy);
}
// TODO: We should be assessing this based on whether the pattern in the input
// PNG had right-angled borders on this side, not based on the literal
// presence or absence of walls
//
// In other words, this is wrong.
static Vault fromVaultData(List<LevelTile> tiles, int vx, int vy) {
assert(tiles.length == vx * vy);
var smooth = {
Direction.up,
Direction.left,
Direction.down,
Direction.right
};
for (var x = 0; x < vx; x++) {
if (tiles[x + 0 * vx] == LevelTile.wall) {
smooth.remove(Direction.up);
break;
}
}
for (var x = 0; x < vx; x++) {
if (tiles[x + (vy - 1) * vx] == LevelTile.wall) {
smooth.remove(Direction.down);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[0 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.left);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[vx - 1 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.right);
break;
}
}
return Vault(tiles, vx, vy, DirectionSet(smooth));
}
void clear(LevelTile lt) {
for (var y = 0; y < vy; y++) {
for (var x = 0; x < vx; x++) {
tiles[y * vx + x] = lt;
}
}
}
void blitFrom(Vault other, int dx, int dy) {
assert(dx >= 0);
assert(dy >= 0);
assert(dx + other.vx <= vx);
assert(dy + other.vy <= vy);
for (var x = 0; x < other.vx; x++) {
for (var y = 0; y < other.vy; y++) {
tiles[(y + dy) * vx + x + dx] = other.tiles[y * other.vx + x];
}
}
}
Vault flip() {
List<LevelTile> tiles2 = [
for (var y = 0; y < vy; y++)
for (var x = vx - 1; x >= 0; x--) tiles[y * vx + x]
];
return Vault(tiles2, vx, vy, smooth.flip());
}
// TODO: Actually test this logic.
// This worked in Python, so it might even be right!
Vault rotateRight() {
List<LevelTile> tiles2 = [
for (var x = 0; x < vx; x++)
for (var y = 0; y < vy; y++) tiles[(vy - 1 - y) * vx + x]
];
return Vault(tiles2, vy, vx, smooth.rotateRight());
}
Vault rotateLeft() {
List<LevelTile> tiles2 = [
for (var x = vx - 1; x >= 0; x++)
for (var y = vy - 1; y >= 0; y++) tiles[y * vx + (vx - 1 - x)]
];
return Vault(tiles2, vy, vx, smooth.rotateLeft());
}
Vault reorient(int r) {
assert(r >= 0 && r < 8);
Vault o = this;
while (r >= 2) {
o = o.rotateRight();
r -= 2;
}
if (r == 1) {
o = o.flip();
r -= 1;
}
return o;
}
}
int randomOrientation(math.Random random) {
return random.nextInt(8);
}

33
lib/gen/orientation.dart Normal file
View File

@ -0,0 +1,33 @@
part of 'generator.dart';
int randomOrientation(math.Random random) {
return random.nextInt(8);
}
Vault reorientVault(Vault o, int r) {
assert(r >= 0 && r < 8);
while (r >= 2) {
o = o.rotateRight();
r -= 2;
}
if (r == 1) {
o = o.flip();
r -= 1;
}
return o;
}
Requirement unReorientRequirement(Requirement o, int r) {
assert(r >= 0 && r < 8);
if (r % 2 == 1) {
o = o.flip();
r -= 1;
}
while (r >= 2) {
o = o.rotateLeft();
r -= 2;
}
return o;
}

34
lib/gen/requirement.dart Normal file
View File

@ -0,0 +1,34 @@
part of 'generator.dart';
class Requirement {
final int vx, vy;
final DirectionSet smooth;
Requirement(this.vx, this.vy, this.smooth);
Requirement flip() {
return Requirement(vx, vy, smooth.flip());
}
Requirement rotateLeft() {
return Requirement(vy, vx, smooth.rotateLeft());
}
Requirement rotateRight() {
return Requirement(vy, vx, smooth.rotateRight());
}
Requirement unReorient(int r) {
assert(r >= 0 && r < 8);
Requirement o = this;
if (r % 2 == 1) {
o = o.flip();
r -= 1;
}
while (r >= 2) {
o = o.rotateLeft();
r -= 2;
}
return o;
}
}

124
lib/gen/vault.dart Normal file
View File

@ -0,0 +1,124 @@
part of 'generator.dart';
class Vault {
final int vx, vy;
final DirectionSet smooth;
final List<LevelTile> tiles;
Vault(this.vx, this.vy, this.smooth, this.tiles) {
assert(tiles.length == vx * vy);
}
// TODO: We should be assessing this based on whether the pattern in the input
// PNG had right-angled borders on this side, not based on the literal
// presence or absence of walls
//
// In other words, this is wrong.
static Vault fromVaultData(int vx, int vy, List<LevelTile> tiles) {
assert(tiles.length == vx * vy);
var smooth = {
Direction.up,
Direction.left,
Direction.down,
Direction.right
};
for (var x = 0; x < vx; x++) {
if (tiles[x + 0 * vx] == LevelTile.wall) {
smooth.remove(Direction.up);
break;
}
}
for (var x = 0; x < vx; x++) {
if (tiles[x + (vy - 1) * vx] == LevelTile.wall) {
smooth.remove(Direction.down);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[0 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.left);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[vx - 1 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.right);
break;
}
}
return Vault(vx, vy, DirectionSet(smooth), tiles);
}
void clear(LevelTile lt) {
for (var y = 0; y < vy; y++) {
for (var x = 0; x < vx; x++) {
tiles[y * vx + x] = lt;
}
}
}
void blitFrom(Vault other, int dx, int dy) {
assert(dx >= 0);
assert(dy >= 0);
assert(dx + other.vx <= vx);
assert(dy + other.vy <= vy);
for (var x = 0; x < other.vx; x++) {
for (var y = 0; y < other.vy; y++) {
tiles[(y + dy) * vx + x + dx] = other.tiles[y * other.vx + x];
}
}
}
Vault flip() {
List<LevelTile> tiles2 = [
for (var y = 0; y < vy; y++)
for (var x = vx - 1; x >= 0; x--) tiles[y * vx + x]
];
return Vault(vx, vy, smooth.flip(), tiles2);
}
// TODO: Actually test this logic.
// This worked in Python, so it might even be right!
Vault rotateRight() {
List<LevelTile> tiles2 = [
for (var x = 0; x < vx; x++)
for (var y = 0; y < vy; y++) tiles[(vy - 1 - y) * vx + x]
];
return Vault(vy, vx, smooth.rotateRight(), tiles2);
}
Vault rotateLeft() {
List<LevelTile> tiles2 = [
for (var x = vx - 1; x >= 0; x++)
for (var y = vy - 1; y >= 0; y++) tiles[y * vx + (vx - 1 - x)]
];
return Vault(vy, vx, smooth.rotateLeft(), tiles2);
}
Vault reorient(int r) {
assert(r >= 0 && r < 8);
Vault o = this;
while (r >= 2) {
o = o.rotateRight();
r -= 2;
}
if (r == 1) {
o = o.flip();
r -= 1;
}
return o;
}
static Vault blank(int vx, int vy, DirectionSet smooth, LevelTile lt) {
var tiles = [
for (var y = 0; y < vy; y++)
for (var x = 0; x < vx; x++) lt
];
return Vault(vx, vy, smooth, tiles);
}
}

18
lib/gen/vaults.dart Normal file
View File

@ -0,0 +1,18 @@
part of 'generator.dart';
class Vaults {
final List<Vault> _primitive = [];
static Future<Vaults> load(String filename) async {
// TODO
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/images/fonts/
- assets/images/wfc/
- assets/images/vaults/
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg