Well, it's sort of working

This commit is contained in:
2023-09-20 20:56:30 -07:00
parent b5466919e6
commit c4c2b26653
13 changed files with 217 additions and 115 deletions

View File

@ -1,7 +1,8 @@
import 'dart:developer';
import 'dart:math' as math;
import 'package:dartterm/world/level.dart';
import 'package:dartterm/algorithms/regionalize.dart';
import 'package:dartterm/bitmap.dart';
import 'package:dartterm/skreek.dart';
part 'direction.dart';
part 'direction_set.dart';
@ -23,7 +24,7 @@ class Generator {
var vx = req.vx;
var vy = req.vy;
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
var v = Vault.blank(vx, vy, req.smooth, VaultTile.wall);
if (req.vx < 2 || req.vy < 2) {
return v;
@ -54,7 +55,7 @@ class Generator {
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}");
// 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.vy == requirement.vy);
return out1;
@ -63,10 +64,10 @@ class Generator {
Vault _generate(Requirement req) {
var vx = req.vx;
var vy = req.vy;
var v = Vault.blank(vx, vy, req.smooth, LevelTile.wall);
var v = Vault.blank(vx, vy, req.smooth, VaultTile.wall);
if (vx < 5 || vx * vy < 10) {
v.clear(LevelTile.floor);
v.clear(VaultTile.bspfloor);
} else {
// pick a split point
var splitVx = _random.nextInt(vx - 4) + 2;
@ -118,6 +119,9 @@ class Generator {
if (vault.vx < req.vx / 2 || vault.vy < req.vy / 2) {
return null;
}
if (!vault.smooth.directions.containsAll(req.smooth.directions)) {
return null;
}
var rsd = req.smooth.directions;
bool mustFillX =
@ -125,13 +129,12 @@ class Generator {
if (vault.vx != req.vx && mustFillX) {
return null;
}
bool mustFillY =
rsd.contains(Direction.left) && rsd.contains(Direction.right);
bool mustFillY = rsd.contains(Direction.up) && rsd.contains(Direction.down);
if (vault.vy != req.vy && mustFillY) {
return null;
}
Vault full = Vault.blank(req.vx, req.vy, req.smooth, LevelTile.wall);
Vault full = Vault.blank(req.vx, req.vy, req.smooth, VaultTile.wall);
int vx = vault.vx;
int dx;
if (rsd.contains(Direction.left)) {
@ -149,7 +152,7 @@ class Generator {
} else if (rsd.contains(Direction.down)) {
dy = req.vy - vy;
} else {
dy = _random.nextInt(req.vx - vx);
dy = _random.nextInt(req.vy - vy);
}
full.blitFrom(vault, dx, dy);
return full;

View File

@ -17,18 +17,4 @@ class Requirement {
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;
}
}

View File

@ -3,7 +3,7 @@ part of 'generator.dart';
class Vault {
final int vx, vy;
final DirectionSet smooth;
final List<LevelTile> tiles;
final List<VaultTile> tiles;
Vault(this.vx, this.vy, this.smooth, this.tiles) {
assert(tiles.length == vx * vy);
@ -14,7 +14,7 @@ class Vault {
// presence or absence of walls
//
// In other words, this is wrong.
static Vault fromVaultData(int vx, int vy, List<LevelTile> tiles) {
static Vault fromVaultData(int vx, int vy, List<VaultTile> tiles) {
assert(tiles.length == vx * vy);
var smooth = {
Direction.up,
@ -23,25 +23,25 @@ class Vault {
Direction.right
};
for (var x = 0; x < vx; x++) {
if (tiles[x + 0 * vx] == LevelTile.wall) {
if (tiles[x + 0 * vx] == VaultTile.wall) {
smooth.remove(Direction.up);
break;
}
}
for (var x = 0; x < vx; x++) {
if (tiles[x + (vy - 1) * vx] == LevelTile.wall) {
if (tiles[x + (vy - 1) * vx] == VaultTile.wall) {
smooth.remove(Direction.down);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[0 + y * vx] == LevelTile.wall) {
if (tiles[0 + y * vx] == VaultTile.wall) {
smooth.remove(Direction.left);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[vx - 1 + y * vx] == LevelTile.wall) {
if (tiles[vx - 1 + y * vx] == VaultTile.wall) {
smooth.remove(Direction.right);
break;
}
@ -50,7 +50,7 @@ class Vault {
return Vault(vx, vy, DirectionSet(smooth), tiles);
}
void clear(LevelTile lt) {
void clear(VaultTile lt) {
for (var y = 0; y < vy; y++) {
for (var x = 0; x < vx; x++) {
tiles[y * vx + x] = lt;
@ -71,7 +71,7 @@ class Vault {
}
Vault flip() {
List<LevelTile> tiles2 = [
List<VaultTile> tiles2 = [
for (var y = 0; y < vy; y++)
for (var x = vx - 1; x >= 0; x--) tiles[y * vx + x]
];
@ -82,7 +82,7 @@ class Vault {
// TODO: Actually test this logic.
// This worked in Python, so it might even be right!
Vault rotateRight() {
List<LevelTile> tiles2 = [
List<VaultTile> tiles2 = [
for (var x = 0; x < vx; x++)
for (var y = 0; y < vy; y++) tiles[(vy - 1 - y) * vx + x]
];
@ -91,7 +91,7 @@ class Vault {
}
Vault rotateLeft() {
List<LevelTile> tiles2 = [
List<VaultTile> tiles2 = [
for (var x = vx - 1; x >= 0; x++)
for (var y = vy - 1; y >= 0; y++) tiles[y * vx + (vx - 1 - x)]
];
@ -99,21 +99,7 @@ class Vault {
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) {
static Vault blank(int vx, int vy, DirectionSet smooth, VaultTile lt) {
var tiles = [
for (var y = 0; y < vy; y++)
for (var x = 0; x < vx; x++) lt

View File

@ -3,11 +3,6 @@ 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 = [];
@ -15,4 +10,85 @@ class Vaults {
list2.shuffle(rng);
return list2;
}
static Future<Vaults> load(String name) async {
var basis = await Bitmap.load(name, colorToVaultTile);
var regions = regionalize(basis.rect, (x, y) => basis.get(x, y) != null);
var vs = Vaults();
for (var region in regions) {
Vault v = loadVault(region, basis);
vs._primitive.add(v);
}
return vs;
}
static Vault loadVault(Region r, Bitmap<VaultTile?> b) {
skreek("Loading vault: $r");
var tiles = [
for (var y = r.rect.top; y < r.rect.bottom; y++)
for (var x = r.rect.left; x < r.rect.right; x++)
b.get(x, y) ?? VaultTile.wall
];
DirectionSet smooth = DirectionSet(
{Direction.up, Direction.left, Direction.right, Direction.down});
for (var x = r.rect.left; x < r.rect.right; x++) {
if (b.get(x, r.rect.top) == null) {
smooth.directions.remove(Direction.up);
break;
}
}
for (var x = r.rect.left; x < r.rect.right; x++) {
if (b.get(x, r.rect.bottom - 1) == null) {
smooth.directions.remove(Direction.down);
break;
}
}
for (var y = r.rect.top; y < r.rect.bottom; y++) {
if (b.get(r.rect.left, y) == null) {
smooth.directions.remove(Direction.left);
break;
}
}
for (var y = r.rect.top; y < r.rect.bottom; y++) {
if (b.get(r.rect.right - 1, y) == null) {
smooth.directions.remove(Direction.right);
break;
}
}
return Vault(r.rect.width, r.rect.height, smooth, tiles);
}
}
enum VaultTile {
exit,
door,
bspfloor,
floor,
wall,
}
VaultTile? colorToVaultTile(int c) {
switch (c) {
// RGBA
case 0x000000FF:
case 0x707070FF:
return VaultTile.wall;
case 0xFFFFFFFF:
case 0xFFFF00FF:
case 0xFF00FFFF:
return VaultTile.floor;
case 0x0087FFFF:
return VaultTile.door;
case 0xFF0000FF:
return VaultTile.exit;
case 0x007F00FF:
return null;
default:
throw Exception("unrecognized pixel: $c");
}
}