Compare commits

..

No commits in common. "b5466919e64367e696c4ef79182eef90b959cec5" and "b91f3097f2eef9e09a469bddeab8c62b1bdf1d10" have entirely different histories.

11 changed files with 248 additions and 445 deletions

Binary file not shown.

Before

(image error) Size: 844 B

@ -1,66 +0,0 @@
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)];
}

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

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

@ -1,65 +0,0 @@
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;
}
}

@ -3,56 +3,57 @@ import 'dart:math' as math;
import 'package:dartterm/world/level.dart';
part 'direction.dart';
part 'direction_set.dart';
part 'orientation.dart';
part 'requirement.dart';
part 'vault.dart';
part 'vaults.dart';
enum Direction {
up,
left,
down,
right,
}
const vaultTries = 10;
class Vaults {
List<Vault> _primitive = [];
class Generator {
final math.Random _random;
final Vaults _vaults;
List<Vault> _queue = [];
static Future<Vaults> load(String filename) async {
// TODO
return Vaults();
}
Generator(this._random, this._vaults);
Vault generateBoxed(Requirement req) {
Vault generateBoxed(math.Random random, 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 (req.vx < 2 || req.vy < 2) {
return v;
}
var req2 = Requirement(vx - 2, vy - 2, req.smooth);
var inner = generate(req2);
var inner = generate(random, req2);
v.blitFrom(inner, 1, 1);
return v;
}
Vault generate(Requirement requirement) {
Vault? suggested = _suggest(vaultTries, requirement);
if (suggested != null) {
return suggested;
}
Vault generate(math.Random random, Requirement requirement) {
// TODO: Pick a relevant vault from the vaults file if possible
//
// 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 = unReorientRequirement(requirement, orientation);
var req2 = requirement.unReorient(orientation);
if (req2.vy > (req2.vx - 2) * 3 / 2) {
orientation = (orientation + 2) % 8; // rotate once more
}
req2 = unReorientRequirement(requirement, orientation);
req2 = requirement.unReorient(orientation);
var out2 = _generate(req2);
var out1 = reorientVault(out2, orientation);
var out2 = _generate(random, req2);
var out1 = out2.reorient(orientation);
log("$orientation ${requirement.vx} ${requirement.vy} ${req2.vx} ${req2.vy} ${out2.vx} ${out2.vy} ${out1.vx} ${out1.vy}");
assert(out1.vx == requirement.vx);
@ -60,98 +61,248 @@ class Generator {
return out1;
}
Vault _generate(Requirement req) {
Vault _generate(math.Random random, Requirement req) {
var vx = req.vx;
var vy = req.vy;
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
if (vx < 5 || vx * vy < 10) {
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) {
v.clear(LevelTile.floor);
} else {
// pick a split point
var splitVx = _random.nextInt(vx - 4) + 2;
var splitVx = random.nextInt(vx - 2) + 1;
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(reqLeft);
var vaultRight = generate(reqRight);
var vaultLeft = generate(random, reqLeft);
var vaultRight = generate(random, 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);
}
}
return ds2;
}
sugg = reorientVault(sugg, randomOrientation(_random));
sugg = _tidy(sugg, req);
if (sugg != null) {
return sugg;
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 null;
return ds2;
}
Vault? _popSuggestion() {
if (_queue.isEmpty) {
_queue = _vaults.randomFlight(_random);
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);
}
if (_queue.isEmpty) {
return null;
}
return _queue.removeLast();
return ds2;
}
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;
DirectionSet clone() {
var ds2 = DirectionSet({});
ds2.directions.addAll(directions);
return ds2;
}
}
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;
}
}
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);
}

@ -1,33 +0,0 @@
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;
}

@ -1,34 +0,0 @@
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;
}
}

@ -1,124 +0,0 @@
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);
}
}

@ -1,18 +0,0 @@
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;
}
}

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