Metaregions 1

This commit is contained in:
Pyrex 2023-09-21 16:11:34 -07:00
parent ae0c62b010
commit 9ad13f7a5a
6 changed files with 84 additions and 28 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 735 B

After

Width:  |  Height:  |  Size: 737 B

View File

@ -2,6 +2,8 @@
// and I strongly prefer half-open bounds
//
// So: Here's a reimplementation of the geometry I need
import 'package:dartterm/skreek.dart';
class Size {
final int dx;
final int dy;
@ -10,6 +12,11 @@ class Size {
assert(dx >= 0);
assert(dy >= 0);
}
@override
String toString() {
return "$dx x $dy";
}
}
class Offset {
@ -17,6 +24,11 @@ class Offset {
final int y;
const Offset(this.x, this.y);
@override
String toString() {
return "@($x, $y)";
}
}
class Rect {
@ -33,6 +45,8 @@ class Rect {
assert(dy >= 0);
}
Size get size => Size(dx, dy);
bool contains(int x, int y) {
return x0 <= x && x < x1 && y0 <= y && y < y1;
}
@ -44,4 +58,9 @@ class Rect {
bool containsRect(Rect rect) {
return x0 <= rect.x0 && y0 <= rect.y0 && rect.x1 <= x1 && rect.y1 <= y1;
}
@override
String toString() {
return "@($x0, $y0) $size";
}
}

View File

@ -3,10 +3,10 @@ import 'dart:math' as math;
import 'package:dartterm/algorithms/geometry.dart' as geo;
class Region {
final math.Rectangle<int> rect;
final geo.Rect rect;
final Set<(int, int)> points;
bool get isRectangle => points.length == rect.width * rect.height;
bool get isRectangle => points.length == rect.dx * rect.dy;
Region(this.rect, this.points);
@ -16,8 +16,7 @@ class Region {
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 + 1, yMax + 1));
var rect = geo.Rect(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
return Region(rect, s);
}

View File

@ -38,7 +38,6 @@ void main() async {
var cursor = at(x * 2, y * 2).big();
switch (output.tiles.get(x, y)) {
case VaultTile.bspfloor:
case null:
cursor.puts(" ");
case VaultTile.floor:
cursor.puts(".");
@ -48,6 +47,9 @@ void main() async {
cursor.puts("#");
case VaultTile.exit:
cursor.puts("X");
case VaultTile.meta0:
case null:
cursor.puts("?");
}
}
}

View File

@ -25,7 +25,7 @@ class Generator {
if (canBeVault) {
Vault? suggested = _suggest(vaultTries, requirement);
if (suggested != null) {
return suggested;
return _fillMetaRegions(requirement, suggested);
}
}
@ -213,4 +213,31 @@ class Generator {
//
return vault;
}
Vault _fillMetaRegions(Requirement requirement, Vault vault) {
var geo.Size(:dx, :dy) = vault.size;
var metaregions = regionalize(geo.Rect(0, 0, dx, dy),
(x, y) => vault.tiles.get(x, y) == VaultTile.meta0);
for (var i in metaregions) {
assert(i.isRectangle);
var sz = i.rect.size;
// TODO: Relax these based on our environs -- for instance, if one of our sides doesn't need to be smooth, that metaregion doesn't either
var metaRequirement = Requirement(
sz.dx,
sz.dx,
sz.dy,
sz.dy,
DirectionSet(
{Direction.up, Direction.left, Direction.down, Direction.right}));
var inner = generate(metaRequirement, true);
var dest = Vault(Bitmap.blank(vault.vx, vault.vy, VaultTile.wall),
vault.smooth.clone());
dest.blitFrom(vault, 0, 0);
dest.blitFrom(inner, i.rect.x0, i.rect.y0);
vault = dest;
}
return vault;
}
}

View File

@ -29,44 +29,46 @@ class Vaults {
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++)
for (var y = r.rect.y0; y < r.rect.y1; y++)
for (var x = r.rect.x0; x < r.rect.x1; x++)
r.points.contains((x, y))
? (b.get(x, y) ?? VaultTile.wall)
: 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) {
for (var x = r.rect.x0; x < r.rect.x1; x++) {
if (b.get(x, r.rect.y0) == 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) {
for (var x = r.rect.x0; x < r.rect.x1; x++) {
if (b.get(x, r.rect.y1 - 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) {
for (var y = r.rect.y0; y < r.rect.y1; y++) {
if (b.get(r.rect.x0, 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) {
for (var y = r.rect.y0; y < r.rect.y1; y++) {
if (b.get(r.rect.x1 - 1, y) == null) {
smooth.directions.remove(Direction.right);
break;
}
}
return Vault(Bitmap(geo.Size(r.rect.width, r.rect.height), tiles), smooth);
return Vault(Bitmap(r.rect.size, tiles), smooth);
}
}
enum VaultTile {
meta0,
exit,
door,
bspfloor,
@ -75,21 +77,28 @@ enum VaultTile {
}
VaultTile? colorToVaultTile(int c) {
switch (c) {
// RGBA
case 0x000000FF:
case 0x707070FF:
switch (c ~/ 256) {
// RGB (originally rgba)
// == spacers ==
case 0x007F00: // deep green
return null; // separates vaults
// == metasyntax ==
case 0x00FF00: // green
return VaultTile.meta0; // call back into BSP
// == level elements ==
case 0x000000:
case 0x707070:
return VaultTile.wall;
case 0xFFFFFFFF:
case 0xFFFF00FF:
case 0xFF00FFFF:
case 0xFFFFFF:
case 0xFFFF00:
case 0xFF00FF:
return VaultTile.floor;
case 0x0087FFFF:
case 0x0087FF:
return VaultTile.door;
case 0xFF0000FF:
case 0xFF0000:
return VaultTile.exit;
case 0x007F00FF:
return null;
default:
throw Exception("unrecognized pixel: $c");
}