Skip to content

Commit

Permalink
Fix incorrect destId and destIndex when using SimpleBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 19, 2024
1 parent 1703ba6 commit 24b2331
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/map-arrange/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ export function printMapArrangeQueue(
lastPrint = Date.now()
const res = drawMapArrangeQueue(queue, scale, nonFinished, mapsAdd, keepInTheSamePlace, color)
const len = res.split('\n')[0].length
console.clear()
// console.clear()
console.log(add + ' ' + '='.repeat(Math.max(1, len - add.length - 1)) + ' ' + '\n' + res + '\n' + '='.repeat(len))
}
42 changes: 28 additions & 14 deletions src/map-arrange/map-picker/configurable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import { Id, BuildQueueAccesor, NextQueueEntryGenerator } from '../../build-queu
import { MapArrangeData, TprArrange, MapArrange } from '../../map-arrange/map-arrange'
import { Dir } from '../../util/geometry'
import { assert } from '../../util/util'
import { printMapArrangeQueue } from '../drawer'

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

export type MapPickerData = {
newId?: Id
newIndex?: number
nextBranch?: number
nextConfig?: MapPicker.ConfigNodeBuildtime
}
export type MapPicker = (
id: Id,
accesor: BuildQueueAccesor<MapArrangeData>,
newId?: Id,
nextBranch?: number
data?: MapPickerData
) => NextQueueEntryGenerator<MapArrangeData>

export namespace MapPicker {
Expand All @@ -36,6 +42,8 @@ export namespace MapPicker {
finishedWhole?: boolean
nodeId: number
nodeProgress?: number
destId: number
destIndex: number
}
) => NextQueueEntryGenerator<MapArrangeData>
export type NodeBuilderRecord = { [key in ConfigTypes]: NodeBuilder<key> }
Expand Down Expand Up @@ -94,17 +102,15 @@ export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {
const mapPicker = (
id: Id,
accesor: BuildQueueAccesor<MapArrangeData>,
newId = id + 1,
nextBranch?: number,
nextConfig?: MapPicker.ConfigNodeBuildtime
{ newId = id + 1, newIndex = 0, nextBranch, nextConfig }: MapPickerData = {}
): NextQueueEntryGenerator<MapArrangeData> => {
const last = id == -1 ? undefined : (accesor.get(id) as MapArrange)
// {
// const push = accesor.globalPushCount
// const pop = accesor.globalPopCount
// const str = `push: ${push}, pop: ${pop}, len: ${accesor.queue.length}, ratio: ${(accesor.queue.length / pop).toPrecision(4)}`
// printQueue(accesor, true, undefined, true, true, str)
// }
{
const push = accesor.globalPushCount
const pop = accesor.globalPopCount
const str = `push: ${push}, pop: ${pop}, len: ${accesor.queue.length}, ratio: ${(accesor.queue.length / pop).toPrecision(4)}`
printMapArrangeQueue(accesor, 16, true, undefined, true, true, str)
}

const lastTpr = last
? (last.restTprs.find(t => t.destId == newId)! as TprArrange)
Expand All @@ -131,7 +137,7 @@ export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {
assert(config.followedBy)
const nextConfig = config.followedBy as MapPicker.ConfigNodeBuildtime

return mapPicker(id, accesor, newId, undefined, nextConfig)
return mapPicker(id, accesor, { newId, nextConfig })
} else {
const branchDone = !config.followedBy && config.count - 1 <= nodeProgress
const finishedWhole = branchDone && nodeId == lastNodeId
Expand All @@ -144,6 +150,8 @@ export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {
nodeId,
nodeProgress: nodeProgress + 1,
finishedWhole,
destId: id,
destIndex: newIndex,
})
}
}
Expand All @@ -152,10 +160,16 @@ export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {
if (nextBranch !== undefined) {
assert(last?.createNextBranch)
const nextConfig = config.branches[nextBranch] as MapPicker.ConfigNodeBuildtime
return mapPicker(id, accesor, newId, undefined, nextConfig)
return mapPicker(id, accesor, { newId, nextConfig, newIndex })
}
const generator = nodeConfigs[config.type]
return generator(config as any, { exitTpr: lastTpr, mapPicker, nodeId: nodeId })
return generator(config as any, {
exitTpr: lastTpr,
mapPicker,
nodeId: nodeId,
destId: id,
destIndex: newIndex,
})
}

assert(false)
Expand Down
16 changes: 14 additions & 2 deletions src/map-arrange/map-picker/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { printMapArrangeQueue } from '../drawer'
import { simpleMapArrange } from '../../maps/simple'
import { simpleMapBranchTunnelArrange } from '../../maps/simple-branch'
import { simpleMapTunnelArrange } from '../../maps/simple-tunnel'
import { MapPicker } from './configurable'
import { MapPicker, MapPickerData } from './configurable'

export function mapPickerSimpleSeedRandomSize(count: number) {
const mapPicker: MapPicker = (id, accesor) => {
Expand All @@ -22,6 +22,8 @@ export function mapPickerSimpleSeedRandomSize(count: number) {
size,
randomizeDirTryOrder: true,
finishedWhole: id + 2 >= count,
destId: id,
destIndex: 0,
})
return roomGen
}
Expand All @@ -43,6 +45,8 @@ export function mapPickerSimpleTunnelSeedRandomSize(count: number, tunnelSize =
tunnelSize,
randomizeDirTryOrder: true,
finishedWhole: id + 2 >= count,
destId: id,
destIndex: 0,
})
return roomGen
}
Expand All @@ -58,7 +62,11 @@ export function mapPickerSimpleTunnelBranch(branchCount: 1 | 2 | 3) {
}

const tunnelSize = { x: 1, y: 1 }
const mapPicker: MapPicker = (id, accesor, newId = id + 1): NextQueueEntryGenerator<MapArrangeData> => {
const mapPicker: MapPicker = (
id,
accesor,
{ newId = id + 1 }: MapPickerData = {}
): NextQueueEntryGenerator<MapArrangeData> => {
const last = id == -1 ? undefined : (accesor.get(id) as MapArrange)

if (last?.branchDone) {
Expand Down Expand Up @@ -87,6 +95,8 @@ export function mapPickerSimpleTunnelBranch(branchCount: 1 | 2 | 3) {
tunnelSize,
branchCount,
randomizeDirTryOrder: false,
destId: id,
destIndex: 0,
})
} else {
const rand = 3 //randomInt(3, 5) * 2
Expand All @@ -101,6 +111,8 @@ export function mapPickerSimpleTunnelBranch(branchCount: 1 | 2 | 3) {
tunnelSize,
randomizeDirTryOrder: false,
branchDone,
destId: id,
destIndex: 0,
})
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/maps/simple-branch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import { Id, NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import {
TprArrange,
MapArrangeData,
Expand Down Expand Up @@ -38,6 +38,8 @@ export function simpleMapBranchTunnelArrange({
roomSize,
tunnelSize,
branchCount,
destId,
destIndex,
randomizeDirTryOrder,
nodeId,
}: {
Expand All @@ -46,6 +48,8 @@ export function simpleMapBranchTunnelArrange({
roomSize: Vec2
tunnelSize: Vec2
branchCount: 1 | 2 | 3
destId: Id
destIndex: number
randomizeDirTryOrder?: boolean
nodeId?: number
}): NextQueueEntryGenerator<MapArrangeData> {
Expand All @@ -54,9 +58,10 @@ export function simpleMapBranchTunnelArrange({
dir: DirU.flip(exitTpr.dir),
x: exitTpr.x,
y: exitTpr.y,
destId: id - 1,
destId,
destIndex,
}
const map: MapArrange = { type: 'Simple', rects: [], restTprs: [], id, entranceTprs: [tpr], nodeId }
const map: MapArrange = { type: 'SimpleBranch', rects: [], restTprs: [], id, entranceTprs: [tpr], nodeId }

let tunnelEntrance: RoomArrange
{
Expand Down Expand Up @@ -108,7 +113,8 @@ export function simpleMapBranchTunnelArrange({

branch: 0,
branchCount: 1,
getNextQueueEntryGenerator: () => mapPicker(id, accesor, prevId, currentBranch),
getNextQueueEntryGenerator: () =>
mapPicker(id, accesor, { newId: prevId, nextBranch: currentBranch }),
}
}
const dir = dirs[currentBranch]
Expand Down Expand Up @@ -143,7 +149,8 @@ export function simpleMapBranchTunnelArrange({

branch: 0,
branchCount: 1,
getNextQueueEntryGenerator: () => mapPicker(id, accesor, newId, currentBranch),
getNextQueueEntryGenerator: () =>
mapPicker(id, accesor, { newId, nextBranch: currentBranch, newIndex: currentBranch }),
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/maps/simple-tunnel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import { Id, NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import {
TprArrange,
MapArrangeData,
Expand Down Expand Up @@ -35,6 +35,8 @@ export function simpleMapTunnelArrange({
exitTpr,
roomSize,
tunnelSize,
destId,
destIndex,
randomizeDirTryOrder,
finishedWhole,
forceExit,
Expand All @@ -46,6 +48,8 @@ export function simpleMapTunnelArrange({
exitTpr: TprArrange
roomSize: Vec2
tunnelSize: Vec2
destId: Id
destIndex: number
randomizeDirTryOrder?: boolean
finishedWhole?: boolean
forceExit?: Dir
Expand All @@ -58,7 +62,8 @@ export function simpleMapTunnelArrange({
dir: DirU.flip(exitTpr.dir),
x: exitTpr.x,
y: exitTpr.y,
destId: id - 1,
destId,
destIndex,
}
const map: MapArrange = {
type: 'SimpleTunnel',
Expand Down
10 changes: 10 additions & 0 deletions src/maps/simple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class Test_SimpleMapPicker {
size: { x: 2, y: 2 },
randomizeDirTryOrder: false,
finishedWhole: id >= 5,
destId: id,
destIndex: 0,
})
return roomGen
}
Expand All @@ -48,6 +50,8 @@ export class Test_SimpleMapPicker {
size: { x: 2, y: 2 },
randomizeDirTryOrder: true,
finishedWhole: id >= 5,
destId: id,
destIndex: 0,
})
return roomGen
}
Expand All @@ -69,6 +73,8 @@ export class Test_SimpleMapPicker {
randomizeDirTryOrder: false,
finishedWhole: false,
forceExit: Dir.EAST,
destId: id,
destIndex: 0,
})
if (id == 0)
return simpleMapArrange({
Expand All @@ -78,6 +84,8 @@ export class Test_SimpleMapPicker {
randomizeDirTryOrder: false,
finishedWhole: true,
forceExit: Dir.EAST,
destId: id,
destIndex: 0,
})
assert(false)
}
Expand All @@ -101,6 +109,8 @@ export class Test_SimpleMapPicker {
tunnelSize: { x: 1, y: 2 },
randomizeDirTryOrder: true,
finishedWhole: id >= 5,
destId: id,
destIndex: 0,
})
return roomGen
}
Expand Down
9 changes: 7 additions & 2 deletions src/maps/simple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vec2 } from '../util/vec2'
import { NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import { Id, NextQueueEntryGenerator, QueueEntry } from '../build-queue/build-queue'
import {
TprArrange,
MapArrangeData,
Expand Down Expand Up @@ -42,6 +42,8 @@ export function simpleMapArrange({
mapPicker,
exitTpr,
size,
destId,
destIndex,
randomizeDirTryOrder,
finishedWhole,
forceExit,
Expand All @@ -52,6 +54,8 @@ export function simpleMapArrange({
mapPicker: MapPicker
exitTpr: TprArrange
size: Vec2
destId: Id
destIndex: number
randomizeDirTryOrder?: boolean
finishedWhole?: boolean
forceExit?: Dir
Expand All @@ -64,7 +68,8 @@ export function simpleMapArrange({
x: exitTpr.x,
y: exitTpr.y,
dir: DirU.flip(exitTpr.dir),
destId: id - 1,
destId,
destIndex,
}
const map: MapArrange = {
type: 'Simple',
Expand Down

0 comments on commit 24b2331

Please sign in to comment.