Skip to content

Commit

Permalink
Add map construction for Simple
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 18, 2024
1 parent 4840417 commit 8adc662
Show file tree
Hide file tree
Showing 18 changed files with 755 additions and 163 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.

199 changes: 106 additions & 93 deletions src/area/area-json-creator.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,106 @@
// import { BuildQueueAccesor } from '../build-queue/build-queue'
// import { copyMapArrange, MapArrange, MapArrangeData, offsetMapArrange } from '../map-arrange/map-arrange'
// import { Rect } from '../util/geometry'
// import { ObjectEntriesT } from '../util/modify-prototypes'
// import { assert } from '../util/util'
//
// export {}
// declare global {
// namespace sc {
// namespace AreaLoadable {
// interface Data {
// shaduungeonCustom?: boolean
// }
// /* SD - ShaDuungeon */
// namespace SDCustom {
// // @ts-expect-error
// interface Data extends sc.AreaLoadable.Data {
// shaduungeonCustom: true
// floors: Floor[]
// }
// interface Floor {
// level: number
// name: ig.LangLabel.Data
//
// maps: Map[]
// connections: Connection[]
// icons: Icon[]
// landmarks: Landmark[]
// rooms?: sc.AreaRoomBounds[]
// }
// interface Map extends sc.AreaLoadable.Map {}
// }
// }
// }
// }
//
// export function createAreaJsonFromBuildQueue(
// accesor: BuildQueueAccesor<MapArrangeData>,
// areaName: string,
// defaultFloor: number,
// chests: number,
// floorNames: Record<number, ig.LangLabel.Data>,
// ): sc.AreaLoadable.SDCustom.Data {
// const maps: MapArrange[] = []
// for (let id = 0; ; id++) {
// const res = accesor.tryGet(id)
// if (!res) break
// maps.push(copyMapArrange(res))
// }
//
// const bounds: Rect = Rect.boundsOfArr(maps.flatMap(a => a.rects))
// const offset = Vec2.mulC(bounds, -1)
// for (const map of maps) offsetMapArrange(map, offset)
//
// const mapsByFloor: Record<number, MapArrange[]> = {}
// for (const map of maps) {
// ;(mapsByFloor[map.floor ?? 0] ??= []).push(map)
// }
//
// const floors: sc.AreaLoadable.SDCustom.Floor[] = []
// let actualDefaultFloor!: number
// for (const [floor, maps] of ObjectEntriesT(mapsByFloor)) {
// if (floor == defaultFloor) actualDefaultFloor = floors.length
//
// const areaMaps: sc.AreaLoadable.SDCustom.Map[] = maps.map(map => {
// return map as any
// })
//
// const name = floorNames[floor]
// assert(name)
// floors.push({
// level: floor,
// name,
//
// maps: areaMaps,
// connections: [],
// icons: [],
// landmarks: [],
// })
// }
//
// assert(actualDefaultFloor)
// return {
// DOCTYPE: 'AREAS_MAP',
// shaduungeonCustom: true,
// name: areaName,
// width: bounds.width,
// height: bounds.height,
// defaultFloor: actualDefaultFloor,
// chests,
// floors,
// }
// }
import { unique } from 'jquery'
import { AreaInfo, MapConstruct } from '../map-construct/map-construct'
import { Rect } from '../util/geometry'
import { ObjectEntriesT } from '../util/modify-prototypes'
import { allLangs, Array2d, assert } from '../util/util'

export {}
declare global {
namespace sc {
namespace AreaLoadable {
interface Data {
shaduungeonCustom?: boolean
}
/* SD - ShaDuungeon */
namespace SDCustom {
// @ts-expect-error
interface Data extends sc.AreaLoadable.Data {
shaduungeonCustom: true
floors: Floor[]
}
interface Floor {
level: number
name: ig.LangLabel.Data

maps: Map[]
connections: Connection[]
icons: Icon[]
landmarks: Landmark[]
rooms?: sc.AreaRoomBounds[]
}
interface Map extends sc.AreaLoadable.Map {}
}
}
}
}

export function createArea(
maps: MapConstruct[],
areaInfo: AreaInfo,
defaultFloor: number,
chests: number,
floorNames: Record<number, ig.LangLabel.Data>
): sc.AreaLoadable.SDCustom.Data {
const { width, height }: Rect = Rect.boundsOfArr(maps.flatMap(a => a.rects))

const mapsByFloor: Record<number, MapConstruct[]> = {}
for (const map of maps) {
;(mapsByFloor[map.floor ?? 0] ??= []).push(map)
}

const floors: sc.AreaLoadable.SDCustom.Floor[] = []
let actualDefaultFloor!: number
for (const [floor, maps] of ObjectEntriesT(mapsByFloor)) {
if (floor == defaultFloor) actualDefaultFloor = floors.length

const areaMaps: sc.AreaLoadable.SDCustom.Map[] = maps.map(map => {
return {
path: map.constructed.name,
name: allLangs(map.title),
offset: { x: 0, y: 0 },
dungeon: 'DUNGEON',
}
})

const name = floorNames[floor]
assert(name)
floors.push({
level: floor,
name,
tiles: Array2d.empty({ x: width, y: height }, 0),

maps: areaMaps,
connections: [],
icons: [],
landmarks: [],
})
}

assert(actualDefaultFloor !== undefined)
return {
DOCTYPE: 'AREAS_MAP',
shaduungeonCustom: true,
name: areaInfo.id,
width,
height,
defaultFloor: actualDefaultFloor,
chests,
floors,
}
}

export function createAreaDbEntry(area: sc.AreaLoadable.SDCustom.Data, areaInfo: AreaInfo): sc.MapModel.Area {
return {
boosterItem: areaInfo.boosterItem.toString(),
landmarks: {},
name: allLangs(areaInfo.title),
description: allLangs(areaInfo.description),
keyItem: areaInfo.keyItem.toString(),
masterKeyItem: areaInfo.masterKeyItem.toString(),
areaType: areaInfo.type,
order: 1001,
track: true,
chests: area.chests,
position: areaInfo.pos,
}
}
9 changes: 7 additions & 2 deletions src/bun-run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import './setup-test'

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

const b = new DungeonBuilder()
b.build('das')
})()
// const b = new Test_DungeonBuilder()
// b.samePlaceFail()
41 changes: 33 additions & 8 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { createArea, createAreaDbEntry } from '../area/area-json-creator'
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 { AreaInfo, constructMapsFromMapsArrange } from '../map-construct/map-construct'
import { Item } from '../util/items'
import { setRandomSeed } from '../util/util'
import { DungeonPaths } from './paths'

export class DungeonBuilder {
build(seed: string) {
async build(seed: string) {
const dungeonId = 'mydng'

const queue = new BuildQueue<MapArrangeData>(true)
const randomizeDirTryOrder = true
const roomSizeReg = { x: 9*16, y: 9*16 }
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 }
Expand Down Expand Up @@ -54,7 +59,7 @@ export class DungeonBuilder {
root: {
type: 'Simple',
size: roomSizeReg,
count: 1,
count: 6,
randomizeDirTryOrder,

// followedBy: branch(
Expand All @@ -72,11 +77,31 @@ export class DungeonBuilder {
console.log(drawMapArrangeQueue(queue, 16, false, undefined, false, true))
if (!mapsArrange) throw new Error('res null')

const mapsConstruct = constructMapsFromMapsArrange(mapsArrange, {
id: 'myarea',
const areaInfo: AreaInfo = {
id: `dnggen-${dungeonId}-myarea`,
title: 'My area',
description: '8th ring of hell',
})
console.log(mapsConstruct)
pos: { x: 60, y: 60 },
type: 'DUNGEON',
masterKeyItem: Item.FajroKeyMaster,
keyItem: Item.FajroKey,
boosterItem: 999,
}
const mapsConstruct = constructMapsFromMapsArrange(mapsArrange, areaInfo)

const paths = new DungeonPaths(dungeonId)
await Promise.all(mapsConstruct.map(map => paths.saveMap(map.constructed)))

const area = createArea(mapsConstruct, areaInfo, 0, 0, { 0: 'myfloor0' })
const areaDb = createAreaDbEntry(area, areaInfo)
await paths.saveArea(areaInfo.id, area, areaDb)

await paths.saveConfig()

ig.game.teleport(
mapsConstruct[0].constructed.name,
ig.TeleportPosition.createFromJson({ marker: 'entrance_0', level: 0, baseZPos: 0, size: { x: 0, y: 0 } })
)
// console.dir(mapsConstruct, { depth: null })
}
}
Loading

0 comments on commit 8adc662

Please sign in to comment.