Skip to content

Commit

Permalink
scale exit meta to final ascii
Browse files Browse the repository at this point in the history
  • Loading branch information
khrome committed Feb 6, 2024
1 parent 76833ea commit 998f124
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
73 changes: 69 additions & 4 deletions lib/mazes/Metroidvania.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ function initialize(width, height) {
return map;
}

export default function ALGORITHM_METROIDVANIA(tileMap, options) {
export default function ALGORITHM_METROIDVANIA(tileMap, options={}) {
try{
tileMap.fill(0); // Fill with walls
const maxDimension = Math.max(tileMap.width, tileMap.height);
if(maxDimension <= 5){
Expand Down Expand Up @@ -622,13 +623,77 @@ export default function ALGORITHM_METROIDVANIA(tileMap, options) {
roomDiff: doorDiff, // When adding a new door, room ID distance
roomDiffOdds: 1/2 // Odds of inserting a new door on opportunity
});
const built = generator.build(()=>{
return tileMap.random();
});
let built = null;
if(options.retries){
let generated = false;
let currentTry = 0;
while(currentTry < options.retries && !generated){
try{
currentTry++
built = generator.build(()=>{
return tileMap.random();
});
generated = true;
}catch(ex){}
}
}else{
built = generator.build(()=>{
return tileMap.random();
});
}
const flattened = built.world.reduce(((agg, line)=> agg.concat(line)), []);
built.world = null;
built.scaledExits = {};
let chr = null;
['north', 'south', 'east', 'west'].forEach((direction)=>{
chr = direction[0];
const scaled = {
x: built.exits[chr].x * roomSizeWidth,
y: (built.exits[chr].y * roomSizeHeight)
};
built.scaledExits[direction] = [];
switch(direction){
case 'north':
scaled.x += Math.floor(roomSizeWidth/2);
built.scaledExits[direction].push(scaled);
built.scaledExits[direction].push({
x: scaled.x-1,
y: scaled.y
});
break;
case 'south':
scaled.x += Math.floor(roomSizeWidth/2);
scaled.y += roomSizeHeight-1;
built.scaledExits[direction].push(scaled);
built.scaledExits[direction].push({
x: scaled.x-1,
y: scaled.y
});
break;
case 'east':
scaled.y += Math.floor(roomSizeHeight/2);
scaled.x += roomSizeWidth-1;
built.scaledExits[direction].push(scaled);
built.scaledExits[direction].push({
x: scaled.x,
y: scaled.y-1
});
break;
case 'west':
scaled.y += Math.floor(roomSizeHeight/2);
built.scaledExits[direction].push(scaled);
built.scaledExits[direction].push({
x: scaled.x,
y: scaled.y-1
});
break;
}
});
tileMap.world = built;
for(let lcv=0; lcv < tileMap.data.length; lcv++){
tileMap.data[lcv] = flattened[lcv];
}
}catch(ex){

This comment has been minimized.

Copy link
@Marak

Marak Mar 4, 2024

Contributor

Adding a try / catch here is not good API design.

We have issue now upstream where error condition is required and cannot be caught, this line eats the error.

Moving forward I would recommend never to silently consume mission-critical errors in API which is designed to be consumed from upstream API.

Resolves with e86fb2b

Thanks again for your contributions.

console.log(ex);
}
}
51 changes: 51 additions & 0 deletions test/metroidvania-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import tap from 'tape';
import TileMap from '../lib/TileMap.js';
import metroidvania from '../lib/mazes/Metroidvania.js';


const twoDTileData = (tileMap)=>{
const data = tileMap.data;
const twoD = [];
while(data.length) twoD.push(data.splice(0, tileMap.width));
return twoD;
};

// Test querying by coordinates for 2D map
tap.test('meta matches output', (t) => {

const tileMap = new TileMap({ width: 128, height: 128 });
tileMap.fill2D(1); // Fill the entire map with '1's for simplicity
metroidvania(tileMap, {retries: 5});


const twoD = twoDTileData(tileMap);

//overwrite all exits
twoD[tileMap.world.scaledExits.north[0].y][tileMap.world.scaledExits.north[0].x] = 'X';
twoD[tileMap.world.scaledExits.north[1].y][tileMap.world.scaledExits.north[1].x] = 'X';

twoD[tileMap.world.scaledExits.south[0].y][tileMap.world.scaledExits.south[0].x] = 'X';
twoD[tileMap.world.scaledExits.south[1].y][tileMap.world.scaledExits.south[1].x] = 'X';

twoD[tileMap.world.scaledExits.east[0].y][tileMap.world.scaledExits.east[0].x] = 'X';
twoD[tileMap.world.scaledExits.east[1].y][tileMap.world.scaledExits.east[1].x] = 'X';

twoD[tileMap.world.scaledExits.west[0].y][tileMap.world.scaledExits.west[0].x] = 'X';
twoD[tileMap.world.scaledExits.west[1].y][tileMap.world.scaledExits.west[1].x] = 'X';


const oneD = twoD.reduce(((agg, line)=> agg.concat(line)), []);
const index = {};

console.log(twoD.map(line => line.join('')).join('\n'));
oneD.forEach((type)=>{
if(!index[type]){
index[type] = 1;
}else{
index[type]++;
}
});
t.same(index['X'], 8, 'Wrote in 8 options');
t.same(index[6], undefined, 'Has no exits after being overwritten');
t.end();
});

0 comments on commit 998f124

Please sign in to comment.