Add and pervasively use grid type (Bitmap)

This commit is contained in:
2023-09-21 15:37:12 -07:00
parent 5de409515e
commit ae0c62b010
7 changed files with 220 additions and 102 deletions

View File

@ -1,5 +1,6 @@
import 'dart:math' as math;
import 'package:dartterm/algorithms/geometry.dart' as geo;
import 'package:dartterm/algorithms/regionalize.dart';
import 'package:dartterm/bitmap.dart';
import 'package:dartterm/skreek.dart';
@ -50,8 +51,9 @@ class Generator {
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.vxMin && out1.vx <= requirement.vxMax);
assert(out1.vy >= requirement.vyMin && out1.vy <= requirement.vyMax);
var geo.Size(:dx, :dy) = out1.size;
assert(dx >= requirement.vxMin && dx <= requirement.vxMax);
assert(dy >= requirement.vyMin && dy <= requirement.vyMax);
assert(out1.smooth.directions.containsAll(requirement.smooth.directions));
return out1;
}
@ -70,11 +72,11 @@ class Generator {
var vyRand = _random.nextInt(vyMax + 1 - vyMin) + vyMin;
if (vxMax < 2 || vyMax < 2) {
return Vault.blank(vxMax, vyRand, req.smooth, VaultTile.wall);
return Vault.blank(vxMax, vyRand, VaultTile.wall, req.smooth);
} else if (vxMax < 9 || (vxMax - 2) * (vyMax - 2) < 12) {
var v2 =
Vault.blank(vxMax - 2, vyMax - 2, req.smooth, VaultTile.bspfloor);
var v = Vault.blank(vxMax, vyMax, req.smooth, VaultTile.wall);
var v2 = Vault.blank(
vxMax - 2, vyMax - 2, VaultTile.bspfloor, req.smooth.clone());
var v = Vault.blank(vxMax, vyMax, VaultTile.wall, req.smooth.clone());
v.blitFrom(v2, 1, 1);
return v;
} else {
@ -87,8 +89,7 @@ class Generator {
var vyMaxRight = vyMax;
if (smoothUpDown) {
vyMinRight = leftChild.vy;
vyMaxRight = leftChild.vy;
vyMaxRight = vyMinRight = leftChild.size.dy;
}
var rightReq = Requirement(
@ -106,14 +107,14 @@ class Generator {
if (smoothUp) {
var v =
Vault.blank(vxTotal, vyTotal, req.smooth.clone(), VaultTile.wall);
Vault.blank(vxTotal, vyTotal, VaultTile.wall, req.smooth.clone());
v.blitFrom(leftChild, 0, 0);
v.blitFrom(rightChild, leftChild.vx - 1, 0);
return v;
}
if (smoothDown) {
var v =
Vault.blank(vxTotal, vyTotal, req.smooth.clone(), VaultTile.wall);
Vault.blank(vxTotal, vyTotal, VaultTile.wall, req.smooth.clone());
v.blitFrom(leftChild, 0, vyTotal - leftChild.vy);
v.blitFrom(rightChild, leftChild.vx - 1, vyTotal - rightChild.vy);
return v;
@ -125,7 +126,7 @@ class Generator {
if (vyTMax > vyTotal) {
vyTotal += _random.nextInt(vyTMax - vyTotal);
}
var v = Vault.blank(vxTotal, vyTotal, req.smooth.clone(), VaultTile.wall);
var v = Vault.blank(vxTotal, vyTotal, VaultTile.wall, req.smooth.clone());
if (_random.nextBool()) {
v.blitFrom(leftChild, 0, 0);
v.blitFrom(rightChild, leftChild.vx - 1, vyTotal - rightChild.vy);
@ -173,32 +174,43 @@ class Generator {
if (!vault.smooth.directions.containsAll(req.smooth.directions)) {
return null;
}
// NOTE: If the vault has metaBSP regions, and they touch the outer edge, then it should be possible
// to extend those regions
// Extending a metaBSP region results in _two_ metaBSP regions:
// a big version of the original, and a second one covering all the space left over
// in the set of rows or columns the metabsp region did not touch
//
// Ex:
// XXXX##
// XXXX #
// XXXX #
// # #
// ######
//
// becomes
//
// XXXZYY
// XXXZYY
// XXXZYY
// XXXX##
// XXXX #
// XXXX #
// # #
// ######
//
// (where the Zs are spaces that Xs and Ys both touch)
//
// Extension can happen more than once, on each axis:
//
// XXXXXZYY
// XXXXXZYY
// XXXXXZYY
// XXXXXX##
// XXXXXX #
// BBXXXX #
// AA# #
// AA######
//
return vault;
/*
var rsd = req.smooth.directions;
Vault full = Vault.blank(req.vx, req.vy, req.smooth, VaultTile.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.vy - vy);
}
full.blitFrom(vault, dx, dy);
return full;
*/
}
}

View File

@ -1,69 +1,36 @@
part of 'generator.dart';
class Vault {
final int vx, vy;
final Bitmap<VaultTile> tiles;
final DirectionSet smooth;
final List<VaultTile> tiles;
Vault(this.vx, this.vy, this.smooth, this.tiles) {
assert(tiles.length == vx * vy);
geo.Size get size => tiles.size;
int get vx => size.dx;
int get vy => size.dy;
Vault(this.tiles, this.smooth);
static Vault blank(int vx, int vy, VaultTile lt, DirectionSet smooth) {
return Vault(Bitmap.blank(vx, vy, lt), smooth);
}
void clear(VaultTile lt) {
for (var y = 0; y < vy; y++) {
for (var x = 0; x < vx; x++) {
tiles[y * vx + x] = lt;
}
}
tiles.clear(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];
}
}
tiles.blitFrom(other.tiles, dx, dy);
}
Vault flip() {
List<VaultTile> 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);
return Vault(tiles.flip(), smooth.flip());
}
// TODO: Actually test this logic.
// This worked in Python, so it might even be right!
Vault rotateRight() {
List<VaultTile> 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);
return Vault(tiles.rotateRight(), smooth.rotateRight());
}
Vault rotateLeft() {
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)]
];
return Vault(vy, vx, smooth.rotateLeft(), tiles2);
}
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
];
return Vault(vx, vy, smooth, tiles);
return Vault(tiles.rotateLeft(), smooth.rotateLeft());
}
}

View File

@ -62,7 +62,7 @@ class Vaults {
}
}
return Vault(r.rect.width, r.rect.height, smooth, tiles);
return Vault(Bitmap(geo.Size(r.rect.width, r.rect.height), tiles), smooth);
}
}