Skip to content

Commit

Permalink
Add puzzle room arranging
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 22, 2024
1 parent 260faef commit 7aa1787
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/area/area-json-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export function createArea(
const pos: Vec2 = Vec2.copy(tpr)
Vec2.divC(pos, divider)
Vec2.sub(pos, offsetDas)

Vec2.divC(Vec2.floor(Vec2.mulC(pos, 8)), 8)

connections.push({
tx: pos.x,
ty: pos.y,
Expand Down
2 changes: 1 addition & 1 deletion src/area/custom-area-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function drawConnection(v: Vec2, connection: sc.AreaLoadable.SDCustom.Connection
x += 2
let h = 4
if (connection.dir == Dir.NORTH) {
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.empty.draw(x, y, connection.size, h + 2)
inactiveColors.border.draw(x - 1, y + 1, 1, h - 2)
inactiveColors.border.draw(x + connection.size, y + 1, 1, h - 2)
} else if (connection.dir == Dir.EAST) {
Expand Down
28 changes: 17 additions & 11 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { drawMapArrangeQueue } from '../map-arrange/drawer'
import { MapArrange, MapArrangeData } from '../map-arrange/map-arrange'
import { MapPicker, mapPickerConfigurable } from '../map-arrange/map-picker/configurable'
import { AreaInfo, constructMapsFromMapsArrange } from '../map-construct/map-construct'
import { initAllPuzzles } from '../maps/puzzle-data'
import { Dir } from '../util/geometry'
import { Item } from '../util/items'
import { setRandomSeed } from '../util/util'
import { DungeonPaths } from './paths'
Expand All @@ -15,7 +17,7 @@ export class DungeonBuilder {
const queue = new BuildQueue<MapArrangeData>(true)
const randomizeDirTryOrder = true
const roomSizeReg = { x: 13 * 16, y: 13 * 16 }
const tunnelSizeReg = { x: 5 * 16, y: 3 * 16 }
const tunnelSizeReg = { x: 5 * 16, y: 5 * 16 }
const roomSizeBranch = { x: 17 * 16, y: 17 * 16 }
const tunnelSizeBranch = { x: 5 * 16, y: 5 * 16 }

Expand Down Expand Up @@ -57,21 +59,25 @@ export class DungeonBuilder {

const mapPicker: MapPicker = mapPickerConfigurable({
root: {
type: 'Simple',
size: roomSizeReg,
count: 2,
type: 'DngPuzzleTunnel',
tunnelSize: tunnelSizeReg,
// size: roomSizeReg,
count: 20,
randomizeDirTryOrder,

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

setRandomSeed(seed)

await initAllPuzzles()

const mapsArrange = queue.begin(mapPicker(-1, queue)) as MapArrange[]
// console.dir(queue.queue, { depth: null })
console.log(drawMapArrangeQueue(queue, 16, false, undefined, false, true))
Expand Down Expand Up @@ -103,6 +109,6 @@ export class DungeonBuilder {
mapsConstruct[0].constructed.name,
ig.TeleportPosition.createFromJson({ marker: 'entrance_0', level: 0, baseZPos: 0, size: { x: 0, y: 0 } })
)
// console.dir(mapsConstruct, { depth: null })
console.dir(mapsConstruct, { depth: null })
}
}
15 changes: 14 additions & 1 deletion src/map-arrange/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MapArrangeData, MapArrange, offsetMapArrange, copyMapArrange } from '..
import { BuildQueueAccesor } from '../build-queue/build-queue'
import 'colorts/lib/string'

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

export function drawMapArrangeQueue(
queue: BuildQueueAccesor<MapArrangeData>,
Expand Down Expand Up @@ -68,6 +68,19 @@ export function drawMapArrangeQueue(
console.error(`error while trying to parse rect ${Rect.toString(rect)} of map ${map.id}`, e)
}
}
// for (const tpr of map.entranceTprs) {
// const { x, y } = Vec2.round(Vec2.divC(Vec2.copy(tpr), scale))
// let char = (map.id % 10).toString()
// if (color) char = char.green.italic
//
// strmap[y][x] = char
// }
// for (const tpr of map.rects) {
// const { x, y } = Vec2.round(Vec2.divC(Vec2.copy(tpr), scale))
// let char = (map.id % 10).toString()
// if (color) char = char.red.italic
// strmap[y][x] = char
// }
}

return strmap.map(arr => arr.join('')).join('\n')
Expand Down
3 changes: 2 additions & 1 deletion src/map-arrange/map-picker/configurable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export namespace MapPicker {

export interface Config {
root: ConfigNode
startDir?: Dir
}

export type ConfigNodeBuildtime = ConfigNode & {
Expand Down Expand Up @@ -113,7 +114,7 @@ export function mapPickerConfigurable(_config: MapPicker.Config): MapPicker {

const lastTpr = last
? (last.restTprs.find(t => t.destId == newId)! as TprArrange)
: { x: 0, y: 0, dir: Dir.NORTH, destId: 0 }
: { x: 0, y: 0, dir: _config.startDir ?? Dir.NORTH, destId: 0 }

const nodeId = nextConfig?.nodeId ?? last?.nodeId ?? 0
const nodeProgress = nextConfig ? 0 : (last?.nodeProgress ?? 0)
Expand Down
7 changes: 5 additions & 2 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { MapConstructionLayers } from './layer'
import { getEmptyLayers } from './layer'
import { MapTheme } from './theme'

export type TprDoorLikeType = 'Door' | 'TeleportGround'
export type TprType = TprDoorLikeType | 'TeleportField'

export interface MapConstruct extends MapArrange {
arrangeCopy: MapArrange
constructed: sc.MapModel.Map
Expand Down Expand Up @@ -67,7 +70,7 @@ export function baseMapConstruct(
areaId: string,
theme: MapTheme,
extend: Record<Dir, number>
): MapInConstruction {
): MapInConstruction {
const boundsEntity = Rect.boundsOfArr(map.rects)

for (let dir = 0 as Dir; dir < 4; dir++) {
Expand All @@ -81,7 +84,7 @@ export function baseMapConstruct(

const bounds = Rect.div(Rect.copy(boundsEntity), 16)

const mapSize: Vec2 = Rect.toTwoVecSize(bounds)[1]
const mapSize: Vec2 = { x: bounds.width, y: bounds.height }

const mic: MapInConstruction = {
name: mapName,
Expand Down
4 changes: 4 additions & 0 deletions src/map-construct/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function placeRoom(room: RoomArrange, map: MapInConstruction, tc: MapThem
const rect = Rect.div(Rect.copy(room), 16)
const { x: rx, y: ry } = rect
const { x: rx2, y: ry2 } = Rect.x2y2(rect)
assert(rx % 1 == 0)
assert(ry % 1 == 0)
assert(rx2 % 1 == 0)
assert(ry2 % 1 == 0)
const background = map.layers.background[0]
const shadow = map.layers.shadow
const light = map.layers.light
Expand Down
146 changes: 146 additions & 0 deletions src/maps/dng-puzzle-tunnel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { Id, NextQueueEntryGenerator } from '../build-queue/build-queue'
import {
TprArrange,
MapArrangeData,
MapArrange,
RoomArrange,
doesMapArrangeFit,
TprArrange3d,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { registerMapConstructor } from '../map-construct/map-construct'
import { Dir, DirU, Rect } from '../util/geometry'
import { shuffleArray } from '../util/util'
import { Vec2 } from '../util/vec2'
import { getPuzzleList } from './puzzle-data'
import { simpleMapConstructor } from './simple'

declare global {
export namespace MapPickerNodeConfigs {
export interface All {
DngPuzzleTunnel: DngPuzzleTunnel
}
export interface DngPuzzleTunnel {
count: number
tunnelSize: Vec2
randomizeDirTryOrder?: boolean
followedBy?: MapPicker.ConfigNode
}
}
}
registerMapPickerNodeConfig('DngPuzzleTunnel', (data, buildtimeData) => {
return das({ ...data, ...buildtimeData })
})
export function das({
mapPicker,
exitTpr,
tunnelSize,
destId,
destIndex,
finishedWhole,
branchDone,
nodeId,
nodeProgress,
}: {
mapPicker: MapPicker
exitTpr: TprArrange
tunnelSize: Vec2
destId: Id
destIndex: number
finishedWhole?: boolean
branchDone?: boolean
nodeId?: number
nodeProgress?: number
}): NextQueueEntryGenerator<MapArrangeData> {
return (id, _, accesor) => {
const tpr: TprArrange = {
dir: DirU.flip(exitTpr.dir),
x: exitTpr.x,
y: exitTpr.y,
destId,
destIndex,
}
const map: MapArrange = {
type: 'DngPuzzleTunnel',
rects: [],
restTprs: [],
id,
entranceTprs: [tpr],
branchDone,
nodeId,
nodeProgress,
}

let tunnelEntrance: RoomArrange
{
const rect = Rect.centered(tunnelSize, tpr)
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[exitTpr.dir] = false
tunnelEntrance = { ...rect, walls }
map.rects.push(tunnelEntrance)
}
if (!doesMapArrangeFit(accesor, map, id)) return null

const puzzles = shuffleArray(getPuzzleList(tpr.dir))

return {
data: map,
id,
branch: 0,
branchCount: puzzles.length,

nextQueueEntryGenerator: (_, branch, accesor) => {
const map = { rects: [] as RoomArrange[], restTprs: [] as TprArrange3d[] }
const puzzle = puzzles[branch]

const bounds = Rect.boundsOfArr(puzzle.rects)
if (puzzle.sel.data.type == blitzkrieg.PuzzleRoomType.AddWalls) Rect.extend(bounds, 3 * 2 * 16)
const size = { x: bounds.width, y: bounds.height }
const offset = { x: 0, y: 0 }
if ((tunnelSize.x / 16) % 2 != (size.x / 16) % 2) offset.x += 16
if ((tunnelSize.x / 16) % 2 != (size.y / 16) % 2) offset.y += 16

Vec2.add(size, offset)

const rect = Rect.centered(size, {
...Rect.middle(Rect.side(tunnelEntrance, exitTpr.dir)),
dir: tpr.dir,
})
// const rect = Rect.centered(size, tpr)

const room: RoomArrange = {
...rect,
walls: [true, true, true, true],
}

map.rects.push(room)

if (!doesMapArrangeFit(accesor, map, id)) return null

{
const pos: Vec2 = Rect.sideVec(room, Vec2.add(Vec2.copy(puzzle.exit.vec), room), puzzle.exit.dir)
Vec2.round(pos)

map.restTprs.push({
...pos,
dir: puzzle.exit.dir,
destId: id + 1,
})
}

return {
data: map,
id,
finishedEntry: true,
finishedWhole,

branch: 0,
branchCount: 1,
getNextQueueEntryGenerator: () => mapPicker(id, accesor),
}
},
}
}
}

registerMapConstructor('DngPuzzleTunnel', simpleMapConstructor)
Loading

0 comments on commit 7aa1787

Please sign in to comment.