dartterm/lib/gen/vaults.dart

118 lines
2.9 KiB
Dart
Raw Permalink Normal View History

part of 'generator.dart';
class Vaults {
final List<Vault> _primitive = [];
2023-09-21 02:30:03 +00:00
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;
}
2023-09-21 03:56:30 +00:00
static Future<Vaults> load(String name) async {
var basis = await Bitmap.load(name, colorToVaultTile);
2023-09-22 02:29:34 +00:00
var (regions, _) =
regionalize(basis.rect, (x, y) => basis.get(x, y) != null);
2023-09-21 03:56:30 +00:00
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 = [
2023-09-21 23:11:34 +00:00
for (var y = r.rect.y0; y < r.rect.y1; y++)
for (var x = r.rect.x0; x < r.rect.x1; x++)
2023-09-21 05:00:45 +00:00
r.points.contains((x, y))
? (b.get(x, y) ?? VaultTile.wall)
: VaultTile.wall
2023-09-21 03:56:30 +00:00
];
DirectionSet smooth = DirectionSet(
{Direction.up, Direction.left, Direction.right, Direction.down});
2023-09-21 23:11:34 +00:00
for (var x = r.rect.x0; x < r.rect.x1; x++) {
if (b.get(x, r.rect.y0) == null) {
2023-09-21 03:56:30 +00:00
smooth.directions.remove(Direction.up);
break;
}
}
2023-09-21 23:11:34 +00:00
for (var x = r.rect.x0; x < r.rect.x1; x++) {
if (b.get(x, r.rect.y1 - 1) == null) {
2023-09-21 03:56:30 +00:00
smooth.directions.remove(Direction.down);
break;
}
}
2023-09-21 23:11:34 +00:00
for (var y = r.rect.y0; y < r.rect.y1; y++) {
if (b.get(r.rect.x0, y) == null) {
2023-09-21 03:56:30 +00:00
smooth.directions.remove(Direction.left);
break;
}
}
2023-09-21 23:11:34 +00:00
for (var y = r.rect.y0; y < r.rect.y1; y++) {
if (b.get(r.rect.x1 - 1, y) == null) {
2023-09-21 03:56:30 +00:00
smooth.directions.remove(Direction.right);
break;
}
}
2023-09-21 23:11:34 +00:00
return Vault(Bitmap(r.rect.size, tiles), smooth);
2023-09-21 03:56:30 +00:00
}
}
enum VaultTile {
2023-09-21 23:11:34 +00:00
meta0,
2023-09-21 03:56:30 +00:00
exit,
door,
bspfloor,
floor,
2023-09-22 02:29:34 +00:00
doorpronefloor,
defaultwall, // defaultwall is in generated rooms and is overwritten by anything
archpronewall, // archpronewall cannot overwrite wall or archwall
archwall, // archwall cannot overwrite wall.
2023-09-21 03:56:30 +00:00
wall,
}
VaultTile? colorToVaultTile(int c) {
2023-09-21 23:11:34 +00:00
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:
2023-09-21 03:56:30 +00:00
return VaultTile.wall;
case 0xFF8000:
return VaultTile.archwall;
case 0x7F4000:
return VaultTile.archpronewall;
2023-09-22 02:29:34 +00:00
case 0xBCCFFF:
return VaultTile.doorpronefloor;
2023-09-21 23:11:34 +00:00
case 0xFFFFFF:
case 0xFFFF00:
case 0xFF00FF:
2023-09-21 03:56:30 +00:00
return VaultTile.floor;
2023-09-21 23:11:34 +00:00
case 0x0087FF:
2023-09-21 03:56:30 +00:00
return VaultTile.door;
2023-09-21 23:11:34 +00:00
case 0xFF0000:
2023-09-21 03:56:30 +00:00
return VaultTile.exit;
default:
throw Exception("unrecognized pixel: $c");
}
}