More fun!
Grid-based Level Generator to compliment my Maze Generator, not sure if I should give it's own thread but it's very basic right now.
How it works.. (In relation to my example)
Every level in the grid starts off as mazeconcept2a.nw, and then depending on it's walls borrows the cell data from mazeconcept2b.nw and overwrites it's original data resulting in paths/exits.
I plan on adding support for further randomizing grid levels so with enough level work you could make it generate a fairly unique maze every time.
As well as support for level NPCs, and a generate GMAP file function.
PHP Code:
function onCreated() {
// Generate Maze
temp.maze = generateMaze(4, 4);
// Make levels
// Adjust paths for your own server
createGridMaze("cartridge/maze/mazeconcept2", "cartridge/maze/generation", temp.maze.link());
}
/*
!! INCLUDE MY PREVIOUS MAZE CODE FOR WORKING EXAMPLE !!
*/
/*
Level Generation
*/
public function createGridMaze(mazepath, newpath, maze) {
// Load Template Levels
temp.grid_a.loadlines(mazepath @ "a.nw");
temp.grid_b.loadlines(mazepath @ "b.nw");
// Get Grid Size
temp.gridhead = temp.grid_a.index("//MAZEGRID");
temp.gridwidth = temp.grid_a[temp.gridhead+1].substring("//GRIDWIDTH".length()).trim().tokenize(",");
temp.gridheight = temp.grid_a[temp.gridhead+1].substring("//GRIDHEIGHT".length()).trim().tokenize(",");
// Determine Grids Width and Height
temp.mazeheight = maze.size();
temp.mazewidth = maze[0].size();
// Generate Levels
for (temp.y = 0; temp.y < temp.mazewidth; temp.y++) {
for (temp.x = 0; temp.x < temp.mazeheight; temp.x++) {
// Generate Level
temp.lvlname = newpath @ "_" @ num(temp.x, mazewidth) @ "_" @ num(temp.y, mazeheight) @ ".nw";
temp.lvl = "";
temp.lvl = temp.grid_a.subarray(0, 65);
// Begin Removing Walls
temp.cell = maze[temp.y][temp.x];
for (temp.i = 0; temp.i < 4; temp.i++) {
// Remove Wall
if (temp.cell[temp.i] == false) {
// Locate Grid Positions
temp.tx = temp.ty = 0;
temp.gx = 1 + vecx(temp.i);
temp.gy = 1 + vecy(temp.i);
for (temp.j = 0; temp.j < temp.gx; temp.j++) temp.tx += temp.gridwidth[j];
for (temp.j = 0; temp.j < temp.gy; temp.j++) temp.ty += temp.gridheight[j];
// Cut-n-paste Level Data
for (temp.j = 0; temp.j < temp.gridheight[temp.gy]; temp.j++) {
temp.rowstart = format("BOARD 0 %i 64 0 ", temp.ty).length();
temp.line = "";
temp.line = temp.lvl[temp.ty+1+temp.j];
temp.newl = temp.grid_b[temp.ty+temp.j+1].substring(temp.rowstart + (temp.tx * 2), temp.gridwidth[temp.gx] * 2);
temp.line = temp.line.substring(0, temp.rowstart + (temp.tx * 2)) @ temp.newl @ temp.line.substring(temp.rowstart + (temp.tx * 2) + (temp.gridwidth[temp.gx] * 2));
temp.lvl[temp.ty+temp.j+1] = temp.line;
}
// Add Level Link
if (temp.i == 0) {
temp.lx = temp.tx;
temp.ly = 0;
temp.lw = temp.gridwidth[temp.gx];
temp.lh = 1;
temp.p = "playerx 61";
}
else if (temp.i == 1) {
temp.lx = 0;
temp.ly = temp.ty;
temp.lw = 1;
temp.lh = temp.gridheight[temp.gy];
temp.p = "61 playery";
}
else if (temp.i == 2) {
temp.lx = temp.tx;
temp.ly = 63;
temp.lw = temp.gridwidth[temp.gx];
temp.lh = 1;
temp.p = "playerx 0";
}
else if (temp.i == 3) {
temp.lx = 63;
temp.ly = temp.ty;
temp.lw = 1;
temp.lh = temp.gridheight[temp.gy];
temp.p = "0 playery";
}
temp.nlvlname = extractfilebase(newpath) @ "_" @ num(temp.x + vecx(temp.i), mazewidth) @ "_" @ num(temp.y + vecy(temp.i), mazeheight) @ ".nw";
temp.link = format("LINK %s %i %i %i %i %s", nlvlname, lx, ly, lw, lh, p);
temp.lvl.add(temp.link);
}
// Save Level
temp.lvl.savelines(temp.lvlname, 0);
}
}
}
}
function num(a, m) {
if (m > 10) {
return (a < 10 ? "0" @ a : a);
}
return a;
}