Skip to content

Commit

Permalink
Add map construction for SimpleTunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 19, 2024
1 parent 05ab9bc commit 6c6fdc8
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/area/area-json-creator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Id } from '../build-queue/build-queue'
import { RoomArrange } from '../map-arrange/map-arrange'
import { AreaInfo, MapConstruct } from '../map-construct/map-construct'
import { AreaInfo, MapConstruct, RoomConsturct } from '../map-construct/map-construct'
import { Rect } from '../util/geometry'
import { ObjectEntriesT } from '../util/modify-prototypes'
import { allLangs, assert } from '../util/util'
Expand Down Expand Up @@ -37,7 +37,7 @@ declare global {
id: Id
min: Vec2
max: Vec2
rects: (RoomArrange & {
rects: (RoomConsturct & {
drawEmptyRect?: Rect
drawRect?: Rect & { x2: number; y2: number }
areaRect?: Rect
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 @@ -255,7 +255,7 @@ sc.MapRoom.inject({
const rect = o.drawRect!
const eRect = o.drawEmptyRect!

const borderIncGlobal = 0 // o.roomType == RoomType.Tunnel ? 1 : 0
const borderIncGlobal = o.wallsFull ? 0 : 1
const biPX = o.walls[Dir.EAST] ? 0 : borderIncGlobal
const biNX = o.walls[Dir.WEST] ? 0 : borderIncGlobal
const biPY = o.walls[Dir.SOUTH] ? 0 : borderIncGlobal
Expand Down
11 changes: 6 additions & 5 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class DungeonBuilder {

const queue = new BuildQueue<MapArrangeData>(true)
const randomizeDirTryOrder = true
const roomSizeReg = { x: 9 * 16, y: 9 * 16 }
// const tunnelSizeReg = { x: 1, y: 1 }
const roomSizeReg = { x: 13 * 16, y: 13 * 16 }
const tunnelSizeReg = { x: 5 * 16, y: 3 * 16 }
// const roomSizeBranch = { x: 5, y: 5 }
// const tunnelSizeBranch = { x: 1, y: 1 }
//
Expand Down Expand Up @@ -57,9 +57,10 @@ export class DungeonBuilder {

const mapPicker: MapPicker = mapPickerConfigurable({
root: {
type: 'Simple',
size: roomSizeReg,
count: 6,
type: 'SimpleTunnel',
roomSize: roomSizeReg,
tunnelSize: tunnelSizeReg,
count: 5,
randomizeDirTryOrder,

// followedBy: branch(
Expand Down
6 changes: 3 additions & 3 deletions src/map-construct/layer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MapThemeConfig } from "../map-construct/theme"
import { Coll } from "../util/map"
import { Array2d } from "../util/util"
import { MapThemeConfig } from '../map-construct/theme'
import { Coll } from '../util/map'
import { Array2d } from '../util/util'

export interface MapConstructionLayers {
background: number[][][]
Expand Down
26 changes: 22 additions & 4 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyMapArrange, MapArrange, offsetMapArrange } from '../map-arrange/map-arrange'
import { copyMapArrange, MapArrange, offsetMapArrange, RoomArrange } from '../map-arrange/map-arrange'
import { MapPicker } from '../map-arrange/map-picker/configurable'
import { Id } from '../build-queue/build-queue'
import { assert } from '../util/util'
Expand All @@ -12,6 +12,11 @@ export interface MapConstruct extends MapArrange {
constructed: sc.MapModel.Map
rectsAbsolute: Rect[]
title: string
rects: RoomConsturct[]
}

export interface RoomConsturct extends RoomArrange {
wallsFull: boolean
}

export interface AreaInfo {
Expand Down Expand Up @@ -60,14 +65,18 @@ export function baseMapConstruct(
map: MapArrange,
mapName: string,
areaId: string,
theme: MapTheme
theme: MapTheme,
extend: Record<Dir, number>
): { mic: MapInConstruction; rectsAbsolute: Rect[] } {
const rectsAbsolute = map.rects.map(Rect.copy)

const boundsEntity = Rect.boundsOfArr(map.rects)

Rect.extend(boundsEntity, 7 * 16, { [Dir.NORTH]: true })
Rect.extend(boundsEntity, 1 * 16, [false, true, true, true])
for (let dir = 0 as Dir; dir < 4; dir++) {
if (extend[dir]) {
Rect.extend(boundsEntity, extend[dir] * 16, { [dir]: true })
}
}
const offset = Vec2.mulC(boundsEntity, -1)

offsetMapArrange(map, offset)
Expand All @@ -93,3 +102,12 @@ export function baseMapConstruct(
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
}
66 changes: 65 additions & 1 deletion src/maps/simple-tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import {
TprArrange3d,
} from '../map-arrange/map-arrange'
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import {
baseMapConstruct,
convertRoomsArrangeToRoomsConstruct,
getTprName,
registerMapConstructor,
} from '../map-construct/map-construct'
import { placeRoom } from '../map-construct/room'
import { MapTheme } from '../map-construct/theme'
import { Dir, DirU, Rect } from '../util/geometry'
import { shuffleArray } from '../util/util'

Expand Down Expand Up @@ -114,7 +122,7 @@ export function simpleMapTunnelArrange({
dir: DirU.flip(dir),
})
const walls: Record<Dir, boolean> = [true, true, true, true]
walls[dir] = false
walls[DirU.flip(dir)] = false
tunnelExit = { ...rect, walls }
map.rects.push(tunnelExit)
}
Expand Down Expand Up @@ -151,3 +159,59 @@ export function simpleMapTunnelArrange({
return ret
}
}

registerMapConstructor('SimpleTunnel', (map, areaInfo, pathResolver, _mapsArranged, _mapsConstructed) => {
const theme = MapTheme.default
const { mic, rectsAbsolute } = baseMapConstruct(map, pathResolver(map.id), areaInfo.id, theme, [9, 2, 2, 2])

function pushTprEntity(tpr: TprArrange3d, isEntrance: boolean, index: number) {
const name = getTprName(isEntrance, index)
const dir = DirU.flip(tpr.dir as Dir)
if (tpr.destId == -1) {
const middle = Vec2.subC(Rect.middle(map.rects[0]), 16, 16)
return mic.entities.push({
type: 'Marker',
...middle,
level: 0,
settings: { name, dir: DirU.toString(dir) },
})
}

let x = tpr.x
let y = tpr.y

if (tpr.dir != Dir.SOUTH) y -= 16
if (tpr.dir != Dir.EAST) x -= 16

mic.entities.push({
type: 'Door',
x,
y,
level: 0,
settings: {
name,
map: pathResolver(tpr.destId),
marker: getTprName(!isEntrance, tpr.destIndex ?? 0),
dir: DirU.toString(dir),
},
})
}

map.entranceTprs.forEach((tpr, i) => pushTprEntity(tpr, true, i))
map.restTprs.forEach((tpr, i) => pushTprEntity(tpr, false, i))

const rects = convertRoomsArrangeToRoomsConstruct(map.rects)
for (const room of rects) {
placeRoom(room, mic, theme.config, true)
}

const constructed: sc.MapModel.Map = Object.assign(mic, { layers: undefined })

return {
...map,
rects,
constructed,
rectsAbsolute,
title: `map ${constructed.name}`,
}
})
15 changes: 11 additions & 4 deletions src/maps/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import {
import { MapPicker, registerMapPickerNodeConfig } from '../map-arrange/map-picker/configurable'
import { Dir, DirU, Rect } from '../util/geometry'
import { shuffleArray } from '../util/util'
import { baseMapConstruct, getTprName, registerMapConstructor } from '../map-construct/map-construct'
import { placeRoom } from '../map-construct/room'
import {
baseMapConstruct,
convertRoomsArrangeToRoomsConstruct,
getTprName,
registerMapConstructor,
} from '../map-construct/map-construct'
import { MapTheme } from '../map-construct/theme'
import { placeRoom } from '../map-construct/room'

declare global {
export namespace MapPickerNodeConfigs {
Expand Down Expand Up @@ -126,7 +131,7 @@ export function simpleMapArrange({

registerMapConstructor('Simple', (map, areaInfo, pathResolver, _mapsArranged, _mapsConstructed) => {
const theme = MapTheme.default
const { mic, rectsAbsolute } = baseMapConstruct(map, pathResolver(map.id), areaInfo.id, theme)
const { mic, rectsAbsolute } = baseMapConstruct(map, pathResolver(map.id), areaInfo.id, theme, [7, 1, 1, 1])

function pushTprEntity(tpr: TprArrange3d, isEntrance: boolean, index: number) {
const name = getTprName(isEntrance, index)
Expand Down Expand Up @@ -164,14 +169,16 @@ registerMapConstructor('Simple', (map, areaInfo, pathResolver, _mapsArranged, _m
map.entranceTprs.forEach((tpr, i) => pushTprEntity(tpr, true, i))
map.restTprs.forEach((tpr, i) => pushTprEntity(tpr, false, i))

for (const room of map.rects) {
const rects = convertRoomsArrangeToRoomsConstruct(map.rects)
for (const room of rects) {
placeRoom(room, mic, theme.config, true)
}

const constructed: sc.MapModel.Map = Object.assign(mic, { layers: undefined })

return {
...map,
rects,
constructed,
rectsAbsolute,
title: `map ${constructed.name}`,
Expand Down

0 comments on commit 6c6fdc8

Please sign in to comment.