Skip to content

Commit

Permalink
Add prototype puzzle room pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 23, 2024
1 parent 7aa1787 commit 43b80b6
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 61 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.

3 changes: 2 additions & 1 deletion src/area/area-json-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare global {
drawEmptyRect?: Rect
drawRect?: Rect & { x2: number; y2: number }
areaRect?: Rect
wallsFull?: boolean
})[]
}
interface Connection {
Expand Down Expand Up @@ -114,7 +115,7 @@ export function createArea(
min: Vec2.sub(Vec2.divC(Vec2.copy(boundsAbsolute), divider), offset),
max: Vec2.sub(Vec2.divC(Rect.x2y2(boundsAbsolute), divider), offset),
rects: map.rects.map(a => {
const copy = { ...a }
const copy: sc.AreaLoadable.SDCustom.Map['rects'][number] = { ...a }
Rect.div(copy, divider)
Vec2.sub(copy, offsetRelative)
return copy
Expand Down
1 change: 1 addition & 0 deletions src/area/custom-area-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ sc.MapRoom.inject({
o.drawEmptyRect = Rect.copy(rect)
rect.x2 = rect.x + rect.width - 1
rect.y2 = rect.y + rect.height - 1
o.wallsFull ??= o.walls[0] && o.walls[1] && o.walls[2] && o.walls[3]

if (o.walls[Dir.SOUTH]) {
black.draw(rect.x, rect.y2, rect.width, 1)
Expand Down
16 changes: 8 additions & 8 deletions src/bun-run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import './setup-test'

import { DungeonBuilder } from './dungeon/builder'
import { initLibraries } from './library-providers'
;(async () => {
await initLibraries()

const b = new DungeonBuilder()
b.build('das')
})()
// import { DungeonBuilder } from './dungeon/builder'
// import { initLibraries } from './library-providers'
// ;(async () => {
// await initLibraries()
//
// const b = new DungeonBuilder()
// b.build('das')
// })()
// const b = new Test_DungeonBuilder()
// b.samePlaceFail()
3 changes: 2 additions & 1 deletion src/dungeon/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ export class DungeonBuilder {
}

const mapPicker: MapPicker = mapPickerConfigurable({
startDir: Dir.WEST,
root: {
type: 'DngPuzzleTunnel',
tunnelSize: tunnelSizeReg,
// size: roomSizeReg,
count: 20,
count: 1,
randomizeDirTryOrder,

// followedBy: branch(
Expand Down
15 changes: 15 additions & 0 deletions src/map-arrange/map-arrange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BuildQueueAccesor, Id, NextQueueEntryGenerator } from '../build-queue/build-queue'
import { PuzzleData } from '../maps/puzzle-data'
import { Dir, Dir3d, Rect } from '../util/geometry'
import { Vec2 } from '../util/vec2'
import { MapPicker } from './map-picker/configurable'
Expand Down Expand Up @@ -27,6 +28,10 @@ export interface MapArrange {

nodeId?: number
nodeProgress?: number

placeData?: {
puzzle?: PuzzleData
}
}

export function copyMapArrange(map: MapArrangeData): MapArrange {
Expand All @@ -44,6 +49,8 @@ export function copyMapArrange(map: MapArrangeData): MapArrange {

nodeId: map.nodeId,
nodeProgress: map.nodeProgress,

placeData: map.placeData,
}
}

Expand All @@ -55,8 +62,16 @@ export function offsetMapArrange(map: MapArrange, vec: Vec2) {

export type MapArrangeData = Partial<MapArrange>

export const RoomPlaceOrder = {
Room: 0,
Tunnel: 1,
} as const
export type RoomPlaceOrder = (typeof RoomPlaceOrder)[keyof typeof RoomPlaceOrder]

export interface RoomArrange extends Rect {
walls: Record<Dir, boolean>
placeOrder?: RoomPlaceOrder
dontPlace?: boolean
}

export function doesMapArrangeFit(
Expand Down
12 changes: 3 additions & 9 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export interface MapConstruct extends MapArrange {
rects: RoomConsturct[]
}

export interface RoomConsturct extends RoomArrange {
wallsFull: boolean
}
export interface RoomConsturct extends RoomArrange {}

export interface AreaInfo {
id: string
Expand Down Expand Up @@ -104,11 +102,7 @@ export function getTprName(isEntrance: boolean, index: number): string {
return `${isEntrance ? 'entrance' : 'rest'}_${index}`
}

function areWallsFull(walls: Record<Dir, boolean>): boolean {
return walls[0] && walls[1] && walls[2] && walls[3]
}
export function convertRoomsArrangeToRoomsConstruct(rooms: RoomArrange[]): RoomConsturct[] {
const newRooms = rooms.map(room => Object.assign(room, { wallsFull: areWallsFull(room.walls) }) as RoomConsturct)
newRooms.sort((a, b) => (b.wallsFull ? 1 : -1) - (a.wallsFull ? 1 : -1))
return newRooms
rooms.sort((a, b) => (a.placeOrder ?? 0) - (b.placeOrder ?? 0))
return rooms
}
1 change: 0 additions & 1 deletion src/map-construct/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function placeRoom(room: RoomArrange, map: MapInConstruction, tc: MapThem
}
}
}

if (room.walls[Dir.NORTH]) {
for (let x = rx; x < rx2; x++) {
placeWall(map, tc, { x, y: ry }, Dir.NORTH)
Expand Down
11 changes: 7 additions & 4 deletions src/map-construct/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export type MapThemeConfig = {
wallDown: number[]
wallRight: number[]
wallLeft: number[]
addLight?: boolean
lightTile?: number
lightStep?: number
} & (
| {
addShadows?: false
Expand All @@ -35,7 +32,13 @@ export type MapThemeConfig = {
edgeShadowBottomRight: number[][]
edgeShadowBottomLeft: number[][]
}
)
) & ({
addLight?: false
} | {
addLight: true
lightTile: number
lightStep: number
})

export class MapTheme {
constructor(public config: MapThemeConfig) {}
Expand Down
88 changes: 73 additions & 15 deletions src/maps/dng-puzzle-tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
RoomArrange,
doesMapArrangeFit,
TprArrange3d,
RoomPlaceOrder,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { registerMapConstructor } from '../map-construct/map-construct'
import { AreaInfo, MapConstruct, registerMapConstructor } from '../map-construct/map-construct'
import { Dir, DirU, Rect } from '../util/geometry'
import { shuffleArray } from '../util/util'
import { assert, shuffleArray } from '../util/util'
import { Vec2 } from '../util/vec2'
import { getPuzzleList } from './puzzle-data'
import { simpleMapConstructor } from './simple'
Expand Down Expand Up @@ -76,7 +77,7 @@ export function das({
const rect = Rect.centered(tunnelSize, tpr)
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[exitTpr.dir] = false
tunnelEntrance = { ...rect, walls }
tunnelEntrance = { ...rect, walls, placeOrder: RoomPlaceOrder.Tunnel }
map.rects.push(tunnelEntrance)
}
if (!doesMapArrangeFit(accesor, map, id)) return null
Expand All @@ -90,27 +91,47 @@ export function das({
branchCount: puzzles.length,

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

const bounds = Rect.boundsOfArr(puzzle.rects)
if (puzzle.sel.data.type == blitzkrieg.PuzzleRoomType.AddWalls) Rect.extend(bounds, 3 * 2 * 16)
Rect.extend(bounds, puzzle.pasteOffset * 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 = {
...Vec2.snapToGrid(
tunnelEntrance,
16,
Vec2.sub(Rect.middle(Rect.side(tunnelEntrance, exitTpr.dir)), puzzle.entrance.vec)
),
width: size.x,
height: size.y,
}

function assertRect(rect: Rect, rects1: Rect[]) {
const rects = rects1.map(Rect.copy)
const bounds = Rect.boundsOfArr(rects)

const rect = Rect.centered(size, {
...Rect.middle(Rect.side(tunnelEntrance, exitTpr.dir)),
dir: tpr.dir,
})
// const rect = Rect.centered(size, tpr)
const rect1 = Rect.copy(rect)
Vec2.sub(rect1, bounds)
assert(rect1.x % 16 == 0)
assert(rect1.y % 16 == 0)
assert(rect1.width % 16 == 0)
assert(rect1.height % 16 == 0)
}
assertRect(tunnelEntrance, [tunnelEntrance, rect])
assertRect(rect, [tunnelEntrance, rect])

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

map.rects.push(room)
Expand Down Expand Up @@ -143,4 +164,41 @@ export function das({
}
}

registerMapConstructor('DngPuzzleTunnel', simpleMapConstructor)
registerMapConstructor(
'DngPuzzleTunnel',
(
map: MapArrange,
areaInfo: AreaInfo,
pathResolver: (id: Id) => string,
mapsArranged: MapArrange[],
mapsConstructed: MapConstruct[]
) => {
const res = simpleMapConstructor(map, areaInfo, pathResolver, mapsArranged, mapsConstructed, [8, 1, 1, 1])
const puzzle = res.placeData!.puzzle!

const puzzleMapRaw: string = blitzkrieg.mapUtil.cachedMaps[puzzle.map]
assert(puzzleMapRaw)
const puzzleMap: sc.MapModel.Map = JSON.parse(blitzkrieg.mapUtil.cachedMaps[puzzle.map])
const pastePos = Vec2.add(Vec2.divC(Vec2.copy(res.rects[0]), 16), {
x: puzzle.pasteOffset,
y: puzzle.pasteOffset,
})
const out: sc.MapModel.Map = blitzkrieg.mapUtil.copySelMapAreaTo(
res.constructed,
puzzleMap,
puzzle.sel,
pastePos,
[],
{
disableEntities: false,
mergeLayers: false,
removeCutscenes: true,
// makePuzzlesUnique: true,
// uniqueId: puzzle.usel.id,
// uniqueSel: puzzle.usel.sel,
}
)
res.constructed = out
return res
}
)
5 changes: 5 additions & 0 deletions src/maps/puzzle-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface PuzzleData {
exit: ReturnType<typeof Rect.closestSideArr>
exitTpr?: PuzzleData.Tpr
completionCondition?: { path: string; value: any }
pasteOffset: number
}

let allPuzzles!: PuzzleData[]
Expand Down Expand Up @@ -84,6 +85,9 @@ function createPuzzleData(map: string, sel: PuzzleSelection, mapData: sc.MapMode

const entrance = Rect.closestSideArr(rects, Vec2.sub(Vec2.copy(sel.data.startPos), selPos))
const exit = Rect.closestSideArr(rects, Vec2.sub(Vec2.copy(sel.data.endPos), selPos))

const pasteOffset = sel.data.type == blitzkrieg.PuzzleRoomType.AddWalls ? 3 : 0

const res: PuzzleData = {
sel,
rects,
Expand All @@ -92,6 +96,7 @@ function createPuzzleData(map: string, sel: PuzzleSelection, mapData: sc.MapMode
exit,
exitTpr,
completionCondition,
pasteOffset,
}

if (res.entrance.dir == res.exit.dir) return
Expand Down
5 changes: 3 additions & 2 deletions src/maps/simple-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RoomArrange,
doesMapArrangeFit,
TprArrange3d,
RoomPlaceOrder,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { registerMapConstructor } from '../map-construct/map-construct'
Expand Down Expand Up @@ -68,7 +69,7 @@ export function simpleMapBranchTunnelArrange({
const rect = Rect.centered(tunnelSize, tpr)
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[exitTpr.dir] = false
tunnelEntrance = { ...rect, walls }
tunnelEntrance = { ...rect, walls, placeOrder: RoomPlaceOrder.Tunnel }
map.rects.push(tunnelEntrance)
}
let room: RoomArrange
Expand Down Expand Up @@ -127,7 +128,7 @@ export function simpleMapBranchTunnelArrange({
})
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[DirU.flip(dir)] = false
tunnelExit = { ...rect, walls }
tunnelExit = { ...rect, walls, placeOrder: RoomPlaceOrder.Tunnel }
map.rects.push(tunnelExit)
}
if (!doesMapArrangeFit(accesor, map, id)) return null
Expand Down
5 changes: 3 additions & 2 deletions src/maps/simple-tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RoomArrange,
doesMapArrangeFit,
TprArrange3d,
RoomPlaceOrder,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { registerMapConstructor } from '../map-construct/map-construct'
Expand Down Expand Up @@ -81,7 +82,7 @@ export function simpleMapTunnelArrange({
const rect = Rect.centered(tunnelSize, tpr)
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[exitTpr.dir] = false
tunnelEntrance = { ...rect, walls }
tunnelEntrance = { ...rect, walls, placeOrder: RoomPlaceOrder.Tunnel }
map.rects.push(tunnelEntrance)
}
let room: RoomArrange
Expand Down Expand Up @@ -122,7 +123,7 @@ export function simpleMapTunnelArrange({
})
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[DirU.flip(dir)] = false
tunnelExit = { ...rect, walls }
tunnelExit = { ...rect, walls, placeOrder: RoomPlaceOrder.Tunnel }
map.rects.push(tunnelExit)
}
if (!doesMapArrangeFit(accesor, map, id)) return null
Expand Down
Loading

0 comments on commit 43b80b6

Please sign in to comment.