Use vaults if we have them

This commit is contained in:
2023-09-20 19:30:03 -07:00
parent 0a600fd930
commit b5466919e6
5 changed files with 152 additions and 1 deletions

View File

@ -10,9 +10,12 @@ part 'requirement.dart';
part 'vault.dart';
part 'vaults.dart';
const vaultTries = 10;
class Generator {
final math.Random _random;
final Vaults _vaults;
List<Vault> _queue = [];
Generator(this._random, this._vaults);
@ -32,7 +35,10 @@ class Generator {
}
Vault generate(Requirement requirement) {
// TODO: Pick a relevant vault from the vaults file if possible
Vault? suggested = _suggest(vaultTries, requirement);
if (suggested != null) {
return suggested;
}
// First of all: randomize orientation
// This way we only have to consider one kind of spilt
@ -78,4 +84,74 @@ class Generator {
return v;
}
Vault? _suggest(int tries, Requirement req) {
for (var i = 0; i < tries; i++) {
var sugg = _popSuggestion();
if (sugg == null) {
return null;
}
sugg = reorientVault(sugg, randomOrientation(_random));
sugg = _tidy(sugg, req);
if (sugg != null) {
return sugg;
}
}
return null;
}
Vault? _popSuggestion() {
if (_queue.isEmpty) {
_queue = _vaults.randomFlight(_random);
}
if (_queue.isEmpty) {
return null;
}
return _queue.removeLast();
}
Vault? _tidy(Vault vault, Requirement req) {
if (vault.vx > req.vx || vault.vy > req.vy) {
return null;
}
if (vault.vx < req.vx / 2 || vault.vy < req.vy / 2) {
return null;
}
var rsd = req.smooth.directions;
bool mustFillX =
rsd.contains(Direction.left) && rsd.contains(Direction.right);
if (vault.vx != req.vx && mustFillX) {
return null;
}
bool mustFillY =
rsd.contains(Direction.left) && rsd.contains(Direction.right);
if (vault.vy != req.vy && mustFillY) {
return null;
}
Vault full = Vault.blank(req.vx, req.vy, req.smooth, LevelTile.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.vx - vx);
}
full.blitFrom(vault, dx, dy);
return full;
}
}

View File

@ -7,4 +7,12 @@ class Vaults {
// TODO
return Vaults();
}
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;
}
}