Break up the vaults code into multiple files

This commit is contained in:
Pyrex 2023-09-20 18:09:08 -07:00
parent b91f3097f2
commit 0a600fd930
8 changed files with 305 additions and 259 deletions

View File

@ -29,11 +29,10 @@ void main() async {
int seed = 0; int seed = 0;
while (true) { while (true) {
Vault output = vaults.generateBoxed( Vault output = Generator(math.Random(seed), vaults).generateBoxed(
math.Random(seed),
Requirement( Requirement(
16, 32,
16, 24,
DirectionSet({ DirectionSet({
Direction.up, Direction.up,
Direction.left, Direction.left,
@ -42,8 +41,8 @@ void main() async {
}))); })));
var w = output.vx; var w = output.vx;
var h = output.vy; var h = output.vy;
for (var y = 0; y < w; y++) { for (var y = 0; y < h; y++) {
for (var x = 0; x < h; x++) { for (var x = 0; x < w; x++) {
var cursor = at(x * 2, y * 2).big(); var cursor = at(x * 2, y * 2).big();
switch (output.tiles[x + y * w]) { switch (output.tiles[x + y * w]) {
case LevelTile.floor: 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,50 @@ import 'dart:math' as math;
import 'package:dartterm/world/level.dart'; import 'package:dartterm/world/level.dart';
enum Direction { part 'direction.dart';
up, part 'direction_set.dart';
left, part 'orientation.dart';
down, part 'requirement.dart';
right, part 'vault.dart';
} part 'vaults.dart';
class Vaults { class Generator {
List<Vault> _primitive = []; final math.Random _random;
final Vaults _vaults;
static Future<Vaults> load(String filename) async { Generator(this._random, this._vaults);
// TODO
return Vaults();
}
Vault generateBoxed(math.Random random, Requirement req) { Vault generateBoxed(Requirement req) {
var vx = req.vx; var vx = req.vx;
var vy = req.vy; var vy = req.vy;
var tiles = [ var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
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) { if (req.vx < 2 || req.vy < 2) {
return v; return v;
} }
var req2 = Requirement(vx - 2, vy - 2, req.smooth); var req2 = Requirement(vx - 2, vy - 2, req.smooth);
var inner = generate(random, req2); var inner = generate(req2);
v.blitFrom(inner, 1, 1); v.blitFrom(inner, 1, 1);
return v; return v;
} }
Vault generate(math.Random random, Requirement requirement) { Vault generate(Requirement requirement) {
// TODO: Pick a relevant vault from the vaults file if possible // 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 // 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 // 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) { if (req2.vy > (req2.vx - 2) * 3 / 2) {
orientation = (orientation + 2) % 8; // rotate once more orientation = (orientation + 2) % 8; // rotate once more
} }
req2 = requirement.unReorient(orientation); req2 = unReorientRequirement(requirement, orientation);
var out2 = _generate(random, req2); var out2 = _generate(req2);
var out1 = out2.reorient(orientation); var out1 = reorientVault(out2, orientation);
log("$orientation ${requirement.vx} ${requirement.vy} ${req2.vx} ${req2.vy} ${out2.vx} ${out2.vy} ${out1.vx} ${out1.vy}"); log("$orientation ${requirement.vx} ${requirement.vy} ${req2.vx} ${req2.vy} ${out2.vx} ${out2.vy} ${out1.vx} ${out1.vy}");
assert(out1.vx == requirement.vx); assert(out1.vx == requirement.vx);
@ -61,29 +54,24 @@ class Vaults {
return out1; return out1;
} }
Vault _generate(math.Random random, Requirement req) { Vault _generate(Requirement req) {
var vx = req.vx; var vx = req.vx;
var vy = req.vy; var vy = req.vy;
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
var tiles = [ if (vx < 5 || vx * vy < 10) {
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); v.clear(LevelTile.floor);
} else { } else {
// pick a split point // 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()); var reqLeft = Requirement(splitVx, vy, req.smooth.clone());
reqLeft.smooth.directions.add(Direction.right); reqLeft.smooth.directions.add(Direction.right);
var reqRight = Requirement((vx - splitVx - 1), vy, req.smooth.clone()); var reqRight = Requirement((vx - splitVx - 1), vy, req.smooth.clone());
reqRight.smooth.directions.add(Direction.left); reqRight.smooth.directions.add(Direction.left);
var vaultLeft = generate(random, reqLeft); var vaultLeft = generate(reqLeft);
var vaultRight = generate(random, reqRight); var vaultRight = generate(reqRight);
v.blitFrom(vaultLeft, 0, 0); v.blitFrom(vaultLeft, 0, 0);
v.blitFrom(vaultRight, splitVx + 1, 0); v.blitFrom(vaultRight, splitVx + 1, 0);
} }
@ -91,218 +79,3 @@ class Vaults {
return v; return v;
} }
} }
// 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;
}
}
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);
}

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);
}
}

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

@ -0,0 +1,10 @@
part of 'generator.dart';
class Vaults {
final List<Vault> _primitive = [];
static Future<Vaults> load(String filename) async {
// TODO
return Vaults();
}
}