import {ConceptualCell, maps} from "./maps.ts"; import {Grid, Point, Size} from "./engine/datatypes.ts"; import {ALL_STATS} from "./datatypes.ts"; import {LoadedMap, MapCell, MapCellContent} from "./huntmode.ts"; export function generate(): LoadedMap { let mapNames: Array = Object.keys(maps); let mapName = mapNames[Math.floor(Math.random() * mapNames.length)]; let map = maps[mapName]; let baseCells = map.map((ccell, _xy) => { return generateCell(ccell); }) let cells = new Grid( new Size(baseCells.size.w + 2, baseCells.size.h + 2), (xy) => { let offset = xy.offset(new Point(-1, -1)); if (offset.x == -1 || offset.y == -1 || offset.x == baseCells.size.w || offset.y == baseCells.size.h) { return generateBoundaryCell(); } return baseCells.get(offset) } ) let validSpawns = []; for (let x = 0; x < cells.size.w; x++) { for (let y = 0; y < cells.size.h; y++) { let position = new Point(x, y); if (cells.get(position).isValidSpawn) { validSpawns.push(position); } } } let player = choose(validSpawns); cells.get(player).content = {type: "empty"}; let nStairs = choose([1, 1, 1, 0]); for (let i = 0; i < nStairs; i++) { while (true) { let x = Math.floor(Math.random() * cells.size.w); let y = Math.floor(Math.random() * cells.size.h); let xy = new Point(x, y); let item = cells.get(new Point(x, y)); if (player.equals(xy)) { continue; } if (item.content.type == "block" || item.content.type == "stairs") { continue; } item.content = {type: "stairs"} break; } } return { cells, player, } } function generateCell(conceptual: ConceptualCell): MapCell { switch (conceptual) { case "X": return { content: {type: "block"}, revealed: false, isValidSpawn: false, nextMoveAccessible: false}; case " ": return { content: generateContent(), revealed: false, isValidSpawn: false, nextMoveAccessible: false }; case ".": return { content: generateContent(), revealed: false, isValidSpawn: true, nextMoveAccessible: false }; } } function generateBoundaryCell() { return generateCell("X"); } function generateContent(): MapCellContent { // stat pickup let gsp = (): MapCellContent => { return {type: "statPickup", stat: choose(ALL_STATS)} }; let exp = (): MapCellContent => { return {type: "resourcePickup", resource: "EXP"} } // TODO: Other objects? return choose([ gsp, gsp, gsp, gsp, exp, ])(); } function choose(array: Array): T { if (array.length == 0) { throw `array cannot have length 0 for choose` } return array[Math.floor(Math.random() * array.length)] }