Skip to content

Commit

Permalink
Big refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 16, 2024
1 parent aad03e5 commit e37c856
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 186 deletions.
17 changes: 0 additions & 17 deletions src/area/builder.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, Test, TestCase, TestSuite } from 'testyts'

import { BuildQueue, NextQueueEntryGenerator, QueueEntry } from '../dungeon/build-queue'
import { assert } from '../util/util'
import { NextQueueEntryGenerator, QueueEntry, BuildQueue } from './build-queue'

@TestSuite()
export class Test_DungeonQueue {
@TestSuite('Build queue')
export class Test_BuildQueue {
@Test()
branchless() {
type Data = { countLeft: number }
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BuildQueue, Id } from './build-queue'
import { setRandomSeed } from '../util/util'
import { RoomChooser, roomChooserConfigurable } from './room-choosers/configurable'
import { drawQueue } from './queue-drawer'
import { Id, BuildQueue } from '../build-queue/build-queue'
import { drawMapArrangeQueue } from '../map-arrange/drawer'
import { MapArrangeData } from '../map-arrange/map-arrange'
import { MapPicker, mapPickerConfigurable } from '../map-arrange/map-picker/configurable'
import { setRandomSeed } from '../util/util'

export type RoomBlueprint = {}

Expand All @@ -17,7 +17,7 @@ export class DungeonBuilder {
const tunnelSizeBranch = { x: 1, y: 1 }
const randomizeDirTryOrder = true

function tunnel(count: number, followedBy?: RoomChooser.ConfigNode): RoomChooser.ConfigNode {
function tunnel(count: number, followedBy?: MapPicker.ConfigNode): MapPicker.ConfigNode {
return {
type: 'SimpleTunnel',
roomSize: roomSizeReg,
Expand All @@ -32,7 +32,7 @@ export class DungeonBuilder {
branchTunnelCount: () => number,
finalTunnelCount: () => number,
branchCount: () => 1 | 2 | 3
): RoomChooser.ConfigNode {
): MapPicker.ConfigNode {
if (deep == 0) {
return tunnel(finalTunnelCount())
}
Expand All @@ -53,7 +53,7 @@ export class DungeonBuilder {
}
}

const roomChooser: RoomChooser = roomChooserConfigurable({
const mapPicker: MapPicker = mapPickerConfigurable({
root: {
type: 'Simple',
size: roomSizeReg,
Expand All @@ -70,9 +70,9 @@ export class DungeonBuilder {
})

setRandomSeed(seed)
const res = queue.begin(roomChooser(-1, queue))
const res = queue.begin(mapPicker(-1, queue))
console.log(!!res)
// console.dir(queue.queue, { depth: null })
console.log(drawQueue(queue, false, undefined, false, true))
console.log(drawMapArrangeQueue(queue, false, undefined, false, true))
}
}
8 changes: 4 additions & 4 deletions src/dungeon/queue-drawer.ts → src/map-arrange/drawer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Rect } from '../util/geometry'
import { Array2d } from '../util/util'
import { BuildQueueAccesor } from './build-queue'
import { Vec2 } from '../util/vec2'
import { MapArrangeData, MapArrange, offsetMapArrange } from '../map-arrange/map-arrange'
import { BuildQueueAccesor } from '../build-queue/build-queue'
import 'colorts/lib/string'

const colorMap = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan'] as const

export function drawQueue(
export function drawMapArrangeQueue(
queue: BuildQueueAccesor<MapArrangeData>,
nonFinished: boolean = false,
mapsAdd: MapArrangeData[] = [],
Expand Down Expand Up @@ -71,7 +71,7 @@ export function drawQueue(

let lastPrint: number = 0

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

lastPrint = Date.now()
const res = drawQueue(queue, nonFinished, mapsAdd, keepInTheSamePlace, color)
const res = drawMapArrangeQueue(queue, 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
18 changes: 16 additions & 2 deletions src/map-arrange/map-arrange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Id, NextQueueEntryGenerator } from '../dungeon/build-queue'
import { Dir, Dir3d, Rect } from '../util/geometry'
import { BuildQueueAccesor, Id, NextQueueEntryGenerator } from '../build-queue/build-queue'
import { Dir, Dir3d, DirU, Rect, Vec2Dir } from '../util/geometry'
import { Vec2 } from '../util/vec2'

export interface TprArrange3d extends Vec2 {
Expand Down Expand Up @@ -32,3 +32,17 @@ export function offsetMapArrange(map: MapArrange, vec: Vec2) {
}

export type MapArrangeData = Partial<MapArrange>

export interface RoomArrange extends Rect {}

export function doesMapArrangeFit(
accesor: BuildQueueAccesor<MapArrangeData>,
mapToFit: Pick<MapArrange, 'rects'>,
id: Id
): boolean {
for (let i = id - 1; i >= 0; i--) {
const map = accesor.get(i)
if (Rect.doesArrOverlapArr(map.rects!, mapToFit.rects)) return false
}
return true
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { expect, Test, TestCase, TestSuite } from 'testyts/build/testyCore'
import { setRandomSeed, sha256 } from '../../util/util'
import { BuildQueue } from '../build-queue'
import { drawQueue } from '../queue-drawer'
import { RoomChooser, roomChooserConfigurable } from './configurable'
import { MapArrangeData } from '../../map-arrange/map-arrange'
import { MapPicker, mapPickerConfigurable } from './configurable'
import { BuildQueue } from '../../build-queue/build-queue'
import { drawMapArrangeQueue } from '../drawer'
import { MapArrangeData } from '../map-arrange'

@TestSuite()
export class Test_RoomChooserConfigurable {
@TestSuite('Configurable Map Picker')
export class Test_ConfigurableMapPicker {
@Test()
@TestCase('seed: hello', 'hello', 'ad89a7a5132d192e3b9d2b2408879f6af95b212208639e76162ad4983bf98b1d')
build(seed: string, expected: string) {
Expand All @@ -17,7 +17,7 @@ export class Test_RoomChooserConfigurable {
const tunnelSizeBranch = { x: 1, y: 1 }
const randomizeDirTryOrder = true

function tunnel(count: number, followedBy?: RoomChooser.ConfigNode): RoomChooser.ConfigNode {
function tunnel(count: number, followedBy?: MapPicker.ConfigNode): MapPicker.ConfigNode {
return {
type: 'SimpleTunnel',
roomSize: roomSizeReg,
Expand All @@ -32,7 +32,7 @@ export class Test_RoomChooserConfigurable {
branchTunnelCount: () => number,
finalTunnelCount: () => number,
branchCount: () => 1 | 2 | 3
): RoomChooser.ConfigNode {
): MapPicker.ConfigNode {
if (deep == 0) {
return tunnel(finalTunnelCount())
}
Expand All @@ -53,7 +53,7 @@ export class Test_RoomChooserConfigurable {
}
}

const roomChooser: RoomChooser = roomChooserConfigurable({
const mapPicker: MapPicker = mapPickerConfigurable({
root: {
type: 'Simple',
size: roomSizeReg,
Expand All @@ -70,8 +70,8 @@ export class Test_RoomChooserConfigurable {
})

setRandomSeed(seed)
queue.begin(roomChooser(-1, queue))
const res = drawQueue(queue)
queue.begin(mapPicker(-1, queue))
const res = drawMapArrangeQueue(queue)
const sha = sha256(res)
expect.toBeEqual(sha, expected)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Id, BuildQueueAccesor, NextQueueEntryGenerator } from '../../build-queue/build-queue'
import { MapArrangeData, TprArrange, MapArrange } from '../../map-arrange/map-arrange'
import { simpleMapRoomArrange, simpleMapRoomTunnelArrange, simpleMapRoomBranchTunnelArrange } from '../../map-arrange/simple'
import { Dir } from '../../util/geometry'
import { simpleMapArrange, simpleMapTunnelArrange, simpleMapBranchTunnelArrange } from '../maps/simple'
import { assert } from '../../util/util'
import { Id, BuildQueueAccesor, NextQueueEntryGenerator } from '../build-queue'

declare global {
export namespace RoomChooserNodeConfigs {
export namespace MapPickerNodeConfigs {
export interface All {}

export interface All {
Expand All @@ -15,7 +15,7 @@ declare global {
count: number
size: Vec2
randomizeDirTryOrder?: boolean
followedBy?: RoomChooser.ConfigNode
followedBy?: MapPicker.ConfigNode
}

export interface All {
Expand All @@ -26,7 +26,7 @@ declare global {
roomSize: Vec2
tunnelSize: Vec2
randomizeDirTryOrder?: boolean
followedBy?: RoomChooser.ConfigNode
followedBy?: MapPicker.ConfigNode
}

export interface All {
Expand All @@ -36,36 +36,36 @@ declare global {
roomSize: Vec2
tunnelSize: Vec2
branches:
| [RoomChooser.ConfigNode]
| [RoomChooser.ConfigNode, RoomChooser.ConfigNode]
| [RoomChooser.ConfigNode, RoomChooser.ConfigNode, RoomChooser.ConfigNode]
| [MapPicker.ConfigNode]
| [MapPicker.ConfigNode, MapPicker.ConfigNode]
| [MapPicker.ConfigNode, MapPicker.ConfigNode, MapPicker.ConfigNode]
randomizeDirTryOrder?: boolean
}
}
}

export type RoomChooser = (
export type MapPicker = (
id: Id,
accesor: BuildQueueAccesor<MapArrangeData>,
newId?: Id,
nextBranch?: number
) => NextQueueEntryGenerator<MapArrangeData>

export namespace RoomChooser {
export namespace MapPicker {
export interface NodeCommon<T extends ConfigTypes> {
type: T
}

type ExtractValues<T> = T[keyof T]
export type ConfigNode = ExtractValues<{
[key in keyof RoomChooserNodeConfigs.All]: RoomChooserNodeConfigs.All[key] & NodeCommon<key>
[key in keyof MapPickerNodeConfigs.All]: MapPickerNodeConfigs.All[key] & NodeCommon<key>
}>

export type ConfigTypes = ConfigNode['type']
export type NodeBuilder<T extends ConfigTypes> = (
data: RoomChooserNodeConfigs.All[T],
data: MapPickerNodeConfigs.All[T],
buildtimeData: {
roomChooser: RoomChooser
mapPicker: MapPicker
exitTpr: TprArrange
branchDone?: boolean
finishedWhole?: boolean
Expand All @@ -87,10 +87,10 @@ export namespace RoomChooser {
}
}

const nodeConfigs: RoomChooser.NodeBuilderRecord = {} as any
export function registerRoomChooserNodeConfig<T extends RoomChooser.ConfigTypes>(
const nodeConfigs: MapPicker.NodeBuilderRecord = {} as any
export function registerRoomChooserNodeConfig<T extends MapPicker.ConfigTypes>(
type: T,
builder: RoomChooser.NodeBuilderRecord[T]
builder: MapPicker.NodeBuilderRecord[T]
) {
assert(!nodeConfigs[type])
nodeConfigs[type] = builder
Expand All @@ -101,34 +101,34 @@ function registerStuff() {
if (registered) return
registered = true
registerRoomChooserNodeConfig('Simple', (data, buildtimeData) => {
return simpleMapRoomArrange({ ...data, ...buildtimeData })
return simpleMapArrange({ ...data, ...buildtimeData })
})
registerRoomChooserNodeConfig('SimpleTunnel', (data, buildtimeData) => {
return simpleMapRoomTunnelArrange({ ...data, ...buildtimeData })
return simpleMapTunnelArrange({ ...data, ...buildtimeData })
})
registerRoomChooserNodeConfig('SimpleBranch', (data, buildtimeData) => {
return simpleMapRoomBranchTunnelArrange({ ...data, ...buildtimeData, branchCount: data.branches.length })
return simpleMapBranchTunnelArrange({ ...data, ...buildtimeData, branchCount: data.branches.length })
})
}

export function roomChooserConfigurable(_config: RoomChooser.Config): RoomChooser {
export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {
registerStuff()

const config = _config as RoomChooser.ConfigBuildtime
const idToNodeMap: Record<number, RoomChooser.ConfigNodeBuildtime> = {}
const config = _config as MapPicker.ConfigBuildtime
const idToNodeMap: Record<number, MapPicker.ConfigNodeBuildtime> = {}
let lastNodeId: number
{
let nodeId = 0
function setNodeIdsRecursive(node: RoomChooser.ConfigNodeBuildtime) {
function setNodeIdsRecursive(node: MapPicker.ConfigNodeBuildtime) {
node.nodeId = nodeId
idToNodeMap[nodeId] = node
nodeId++
if ('followedBy' in node && node.followedBy) {
setNodeIdsRecursive(node.followedBy as RoomChooser.ConfigNodeBuildtime)
setNodeIdsRecursive(node.followedBy as MapPicker.ConfigNodeBuildtime)
}
if ('branches' in node && node.branches) {
for (const branch of node.branches) {
setNodeIdsRecursive(branch as RoomChooser.ConfigNodeBuildtime)
setNodeIdsRecursive(branch as MapPicker.ConfigNodeBuildtime)
}
}
}
Expand All @@ -143,12 +143,12 @@ export function roomChooserConfigurable(_config: RoomChooser.Config): RoomChoose
}
}

const roomChooser = (
const mapPicker = (
id: Id,
accesor: BuildQueueAccesor<MapArrangeData>,
newId = id + 1,
nextBranch?: number,
nextConfig?: RoomChooser.ConfigNodeBuildtime
nextConfig?: MapPicker.ConfigNodeBuildtime
): NextQueueEntryGenerator<MapArrangeData> => {
const last = id == -1 ? undefined : (accesor.get(id) as MapArrange)
// {
Expand Down Expand Up @@ -181,9 +181,9 @@ export function roomChooserConfigurable(_config: RoomChooser.Config): RoomChoose
assert(config.count > 0, 'config.count cannot be 0')
if (config.count == nodeProgress) {
assert(config.followedBy)
const nextConfig = config.followedBy as RoomChooser.ConfigNodeBuildtime
const nextConfig = config.followedBy as MapPicker.ConfigNodeBuildtime

return roomChooser(id, accesor, newId, undefined, nextConfig)
return mapPicker(id, accesor, newId, undefined, nextConfig)
} else {
const branchDone = !config.followedBy && config.count - 1 <= nodeProgress
const finishedWhole = branchDone && nodeId == lastNodeId
Expand All @@ -192,7 +192,7 @@ export function roomChooserConfigurable(_config: RoomChooser.Config): RoomChoose
const generator = nodeConfigs[config.type]
return generator(config as any, {
exitTpr: lastTpr,
roomChooser,
mapPicker,
branchDone,
nodeId,
nodeProgress: nodeProgress + 1,
Expand All @@ -204,15 +204,14 @@ export function roomChooserConfigurable(_config: RoomChooser.Config): RoomChoose
if ('branches' in config) {
if (nextBranch !== undefined) {
assert(last?.createNextBranch)
const nextConfig = config.branches[nextBranch] as RoomChooser.ConfigNodeBuildtime
return roomChooser(id, accesor, newId, undefined, nextConfig)
const nextConfig = config.branches[nextBranch] as MapPicker.ConfigNodeBuildtime
return mapPicker(id, accesor, newId, undefined, nextConfig)
}
const generator = nodeConfigs[config.type]
return generator(config as any, { exitTpr: lastTpr, roomChooser, nodeId: nodeId })
return generator(config as any, { exitTpr: lastTpr, mapPicker, nodeId: nodeId })
}

assert(false)
}
return roomChooser
return mapPicker
}

Loading

0 comments on commit e37c856

Please sign in to comment.