Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 18, 2024
1 parent ce91bd4 commit 4d6ca20
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 79 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 58 additions & 51 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,53 @@ import { BuildQueue } from '../build-queue/build-queue'
import { drawMapArrangeQueue } from '../map-arrange/drawer'
import { MapArrange, MapArrangeData } from '../map-arrange/map-arrange'
import { MapPicker, mapPickerConfigurable } from '../map-arrange/map-picker/configurable'
import { constructMapsFromMapsArrange } from '../map-construct/map-construct'
import { setRandomSeed } from '../util/util'

export class DungeonBuilder {
build(seed: string): MapArrange[] {
build(seed: string) {
const queue = new BuildQueue<MapArrangeData>(true)
const roomSizeReg = { x: 3, y: 3 }
const tunnelSizeReg = { x: 1, y: 1 }
const roomSizeBranch = { x: 5, y: 5 }
const tunnelSizeBranch = { x: 1, y: 1 }
const randomizeDirTryOrder = true

function tunnel(count: number, followedBy?: MapPicker.ConfigNode): MapPicker.ConfigNode {
return {
type: 'SimpleTunnel',
roomSize: roomSizeReg,
tunnelSize: tunnelSizeReg,
count,
randomizeDirTryOrder,
followedBy,
}
}
function branch(
deep: number,
branchTunnelCount: () => number,
finalTunnelCount: () => number,
branchCount: () => 1 | 2 | 3
): MapPicker.ConfigNode {
if (deep == 0) {
return tunnel(finalTunnelCount())
}
return {
type: 'SimpleBranch',
roomSize: roomSizeBranch,
tunnelSize: tunnelSizeBranch,
branches: [
...new Array(branchCount())
.fill(null)
.map(_ =>
tunnel(
branchTunnelCount(),
branch(deep - 1, branchTunnelCount, finalTunnelCount, branchCount)
)
),
] as any,
}
}
const roomSizeReg = { x: 9*16, y: 9*16 }
// const tunnelSizeReg = { x: 1, y: 1 }
// const roomSizeBranch = { x: 5, y: 5 }
// const tunnelSizeBranch = { x: 1, y: 1 }
//
// function tunnel(count: number, followedBy?: MapPicker.ConfigNode): MapPicker.ConfigNode {
// return {
// type: 'SimpleTunnel',
// roomSize: roomSizeReg,
// tunnelSize: tunnelSizeReg,
// count,
// randomizeDirTryOrder,
// followedBy,
// }
// }
// function branch(
// deep: number,
// branchTunnelCount: () => number,
// finalTunnelCount: () => number,
// branchCount: () => 1 | 2 | 3
// ): MapPicker.ConfigNode {
// if (deep == 0) {
// return tunnel(finalTunnelCount())
// }
// return {
// type: 'SimpleBranch',
// roomSize: roomSizeBranch,
// tunnelSize: tunnelSizeBranch,
// branches: [
// ...new Array(branchCount())
// .fill(null)
// .map(_ =>
// tunnel(
// branchTunnelCount(),
// branch(deep - 1, branchTunnelCount, finalTunnelCount, branchCount)
// )
// ),
// ] as any,
// }
// }

const mapPicker: MapPicker = mapPickerConfigurable({
root: {
Expand All @@ -56,20 +57,26 @@ export class DungeonBuilder {
count: 1,
randomizeDirTryOrder,

followedBy: branch(
2,
() => 2,
() => 2,
() => 2
),
// followedBy: branch(
// 2,
// () => 2,
// () => 2,
// () => 2
// ),
},
})

setRandomSeed(seed)
const res = queue.begin(mapPicker(-1, queue)) as MapArrange[]
const mapsArrange = queue.begin(mapPicker(-1, queue)) as MapArrange[]
// console.dir(queue.queue, { depth: null })
console.log(drawMapArrangeQueue(queue, false, undefined, false, true))
if (!res) throw new Error('res null')
return res
console.log(drawMapArrangeQueue(queue, 16, false, undefined, false, true))
if (!mapsArrange) throw new Error('res null')

const mapsConstruct = constructMapsFromMapsArrange(mapsArrange, {
id: 'myarea',
title: 'My area',
description: '8th ring of hell',
})
console.log(mapsConstruct)
}
}
18 changes: 14 additions & 4 deletions src/map-arrange/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const colorMap = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan'] as const

export function drawMapArrangeQueue(
queue: BuildQueueAccesor<MapArrangeData>,
scale: number,
nonFinished: boolean = false,
mapsAdd: MapArrangeData[] = [],
keepInTheSamePlace?: boolean,
Expand All @@ -20,7 +21,7 @@ export function drawMapArrangeQueue(
.concat(mapsAdd)
.filter(a => a.rects && a.rects.length > 0 && a.id !== undefined)
.map(copyMapArrange)

if (maps.length == 0) return 'drawQueue: no maps'
const rects = maps.flatMap(a => a.rects!)
const bounds: Rect = Rect.boundsOfArr(rects)
Expand All @@ -44,7 +45,10 @@ export function drawMapArrangeQueue(
for (const map of maps) offsetMapArrange(map, offset)
}

const strmap: string[][] = Array2d.empty({ x: bounds.width, y: bounds.height }, ' ')
const strmap: string[][] = Array2d.empty(
{ x: (bounds.width / scale).floor(), y: (bounds.height / scale).floor() },
' '
)
for (const map of maps) {
for (const rect of map.rects) {
let char = (map.id % 10).toString()
Expand All @@ -54,7 +58,12 @@ export function drawMapArrangeQueue(
}

try {
Array2d.pasteInto(strmap, Array2d.empty({ x: rect.width, y: rect.height }, char), rect.x, rect.y)
Array2d.pasteInto(
strmap,
Array2d.empty({ x: (rect.width / scale).floor(), y: (rect.height / scale).floor() }, char),
(rect.x / scale).floor(),
(rect.y / scale).floor()
)
} catch (e) {
console.error(`error while trying to parse rect ${Rect.toString(rect)} of map ${map.id}`, e)
}
Expand All @@ -68,6 +77,7 @@ let lastPrint: number = 0

export function printMapArrangeQueue(
queue: BuildQueueAccesor<MapArrangeData>,
scale: number,
nonFinished: boolean = false,
mapsAdd: MapArrange[] = [],
keepInTheSamePlace?: boolean,
Expand All @@ -77,7 +87,7 @@ export function printMapArrangeQueue(
if (!(lastPrint < Date.now() - 1000 / 10)) return

lastPrint = Date.now()
const res = drawMapArrangeQueue(queue, nonFinished, mapsAdd, keepInTheSamePlace, color)
const res = drawMapArrangeQueue(queue, scale, nonFinished, mapsAdd, keepInTheSamePlace, color)
const len = res.split('\n')[0].length
console.clear()
console.log(add + ' ' + '='.repeat(Math.max(1, len - add.length - 1)) + ' ' + '\n' + res + '\n' + '='.repeat(len))
Expand Down
2 changes: 1 addition & 1 deletion src/map-arrange/map-picker/configurable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Test_ConfigurableMapPicker {

setRandomSeed(seed)
queue.begin(mapPicker(-1, queue))
const res = drawMapArrangeQueue(queue)
const res = drawMapArrangeQueue(queue, 1)
const sha = sha256(res)
expect.toBeEqual(sha, expected)
}
Expand Down
2 changes: 1 addition & 1 deletion src/map-arrange/map-picker/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function mapPickerSimpleTunnelBranch(branchCount: 1 | 2 | 3) {

return lastBranch.createNextBranch!
}
printMapArrangeQueue(accesor, true)
printMapArrangeQueue(accesor, 1, true)

const lastTpr = last
? (last.restTprs.find(t => t.destId == newId)! as TprArrange)
Expand Down
29 changes: 19 additions & 10 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assert } from 'console'
import { MapArrange } from '../map-arrange/map-arrange'
import { copyMapArrange, MapArrange } from '../map-arrange/map-arrange'
import { MapPicker } from '../map-arrange/map-picker/configurable'
import { Id } from '../build-queue/build-queue'
import { assert } from '../util/util'

interface MapConstruct extends MapArrange {
//
export interface MapConstruct extends MapArrange {
constructed: sc.MapModel.Map
}

export interface AreaInfo {
Expand All @@ -12,21 +13,29 @@ export interface AreaInfo {
description: string
}

type MapConstructor = (
export type MapConstructFunc = (
map: MapArrange,
areaInfo: AreaInfo,
mapsArrange: MapArrange[],
mapsConstruct: MapConstruct[]
) => MapArrange
pathResolver: (id: Id) => string,
mapsArranged: MapArrange[],
mapsConstructed: MapConstruct[]
) => MapConstruct

const constructors: Record<MapPicker.ConfigTypes, MapConstructor> = {} as any
export function registerMapConstructor<T extends MapPicker.ConfigTypes>(type: T, constructor: MapConstructor) {
const constructors: Record<MapPicker.ConfigTypes, MapConstructFunc> = {} as any
export function registerMapConstructor<T extends MapPicker.ConfigTypes>(type: T, constructor: MapConstructFunc) {
assert(!constructors[type])
constructors[type] = constructor
}

export function constructMapsFromMapsArrange(maps: MapArrange[], areaInfo: AreaInfo): MapConstruct[] {
const mapsConstruct: MapConstruct[] = []
const pathResolver: (id: Id) => string = id => `${areaInfo.id}/${id}`

for (const map of maps) {
const constructor = constructors[map.type]
const constructedMap = constructor(copyMapArrange(map), areaInfo, pathResolver, maps, mapsConstruct)
mapsConstruct.push(constructedMap)
}

return mapsConstruct
}
6 changes: 3 additions & 3 deletions src/maps/simple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Test_SimpleMapPicker {
return roomGen
}
queue.begin(mapPicker(-1, queue))
const res = drawMapArrangeQueue(queue)
const res = drawMapArrangeQueue(queue, 1)
const expected = `55\n55\n44\n44\n33\n33\n22\n22\n11\n11\n00\n00`
expect.toBeEqual(res, expected)
}
Expand All @@ -53,7 +53,7 @@ export class Test_SimpleMapPicker {
}
setRandomSeed(seed)
queue.begin(mapPicker(-1, queue))
const res = drawMapArrangeQueue(queue)
const res = drawMapArrangeQueue(queue, 1)
expect.toBeEqual(res, expected)
}

Expand Down Expand Up @@ -106,7 +106,7 @@ export class Test_SimpleMapPicker {
}
setRandomSeed(seed)
queue.begin(mapPicker(-1, queue))
const res = drawMapArrangeQueue(queue)
const res = drawMapArrangeQueue(queue, 1)
const hash = sha256(res)
expect.toBeEqual(hash, expected)
}
Expand Down
49 changes: 47 additions & 2 deletions src/maps/simple.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Vec2 } from '../util/vec2'
import { NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import { TprArrange, MapArrangeData, MapArrange, RoomArrange, doesMapArrangeFit } from '../map-arrange/map-arrange'
import {
TprArrange,
MapArrangeData,
MapArrange,
RoomArrange,
doesMapArrangeFit,
offsetMapArrange,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { Dir, DirU, Rect } from '../util/geometry'
import { shuffleArray } from '../util/util'
import { registerMapConstructor } from '../map-construct/map-construct'
import { MapTheme } from '../map-construct/theme'

declare global {
export namespace MapPickerNodeConfigs {
Expand Down Expand Up @@ -51,7 +60,7 @@ export function simpleMapArrange({
destId: id - 1,
}
const map: MapArrange = {
type: 'SimpleBranch',
type: 'Simple',
rects: [],
restTprs: [],
id,
Expand Down Expand Up @@ -113,3 +122,39 @@ export function simpleMapArrange({
return ret
}
}

registerMapConstructor('Simple', (map, areaInfo, pathResolver, _mapsArranged, _mapsConstructed) => {
const bounds = Rect.boundsOfArr(map.rects)
const offset = Vec2.mulC(bounds, -1)
offsetMapArrange(map, offset)

const theme = MapTheme.default

const c: sc.MapModel.Map = {
name: pathResolver(map.id),
mapWidth: bounds.width,
mapHeight: bounds.height,
levels: [{ height: 0 }],
masterLevel: 0,
attributes: theme.getMapAttributes(areaInfo.id),
screen: { x: 0, y: 0 },
entities: [],
layer: [],
}

for (const tpr of [...map.entranceTprs, ...map.restTprs]) {
const entity = {
type: 'DOOR',
x: tpr.x,
y: tpr.y,
level: 0,
settings: {},
}
c.entities.push(entity)
}

return {
...map,
constructed: c,
}
})
5 changes: 5 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import './util/modify-prototypes'

import './map-arrange/map-picker/configurable'
import './map-construct/map-construct'

import './maps/simple'
import './maps/simple-branch'
import './maps/simple-tunnel'
Loading

0 comments on commit 4d6ca20

Please sign in to comment.