Skip to content

Commit 34b532c

Browse files
committed
fix dungeon
1 parent eecda4a commit 34b532c

2 files changed

Lines changed: 51 additions & 26 deletions

File tree

src/modules/places/dungeons/dungeon.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EntityTypes, Player, StructureRotation, StructureSaveMode, system, world } from '@minecraft/server'
2-
import { MinecraftBlockTypes } from '@minecraft/vanilla-data'
2+
import { MinecraftBlockTypes, MinecraftEntityTypes } from '@minecraft/vanilla-data'
33
import {
44
ActionForm,
55
adventureModeRegions,
@@ -21,9 +21,11 @@ import { createLogger } from 'lib/utils/logger'
2121
import { structureLikeRotate, structureLikeRotateRelative, toAbsolute, toRelative } from 'lib/utils/structure'
2222
import { Dungeon } from './loot'
2323

24+
const logger = createLogger('dungeon')
25+
2426
export interface DungeonRegionDatabase extends JsonObject {
2527
chests: Record<string, number | null>
26-
spawnerCooldowns: Record<string, number>
28+
spawnerCooldowns?: Record<string, number>
2729
structureId: string
2830
rotation: StructureRotation
2931
terrainStructureId: string
@@ -68,9 +70,10 @@ export class DungeonRegion extends Region {
6870
}
6971

7072
for (const spawner of dungeon.spawners) {
73+
dungeon.ldb.spawnerCooldowns ??= {}
7174
const cooldown = dungeon.ldb.spawnerCooldowns[spawner.id]
7275
if (!cooldown || Cooldown.isExpired(cooldown, spawner.restoreTime)) {
73-
dungeon.spawn(spawner)
76+
if (dungeon.spawn(spawner)) dungeon.ldb.spawnerCooldowns[spawner.id] = Date.now()
7477
}
7578
}
7679

@@ -136,9 +139,11 @@ export class DungeonRegion extends Region {
136139
for (const f of toRotated(enderChestPositions)) this.createChest(f, powerfullLoot)
137140

138141
for (const { loc, inv } of shulkers) {
142+
this.ldb.spawnerCooldowns ??= {}
143+
139144
const [rotated] = this.rotate([this.fromRelativeToAbsolute(loc)])
140145
if (!rotated) {
141-
console.warn('Not rotated')
146+
logger.warn('Not rotated')
142147
continue
143148
}
144149

@@ -169,15 +174,15 @@ export class DungeonRegion extends Region {
169174
}
170175

171176
configureSize() {
172-
// console.log('Configuring size of', this.structureId, this.linkedDatabase)
177+
// logger.info('Configuring size of', this.structureId, this.linkedDatabase)
173178
if (isKeyof(this.structureId, structureFiles)) {
174179
this.structureFile = structureFiles[this.structureId]
175-
// console.log('Created dungeon with size', Vector.string(this.structureSize))
180+
// logger.info('Created dungeon with size', Vector.string(this.structureSize))
176181
} else return false
177182

178183
if (this.area instanceof SphereArea) {
179184
this.area.radius = Vec.distance(this.getStructurePosition(), this.area.center)
180-
// console.log('Changed radius of dungeon to', this.area.radius)
185+
// logger.info('Changed radius of dungeon to', this.area.radius)
181186
}
182187

183188
return true
@@ -236,11 +241,19 @@ export class DungeonRegion extends Region {
236241
})
237242
this.ldb.terrainStructureId = id
238243
this.ldb.terrainStructurePosition = from
239-
console.log('Saved backup for', this.structureId, 'with id', id)
244+
logger.info('Saved backup for', this.structureId, 'with id', id)
240245
}
241246

242-
console.log('Placing structure', position, this.structureId)
247+
logger.info('Placing structure', position, this.structureId)
243248
world.structureManager.place(this.structureId, this.dimension, position, { rotation: this.ldb.rotation })
249+
250+
for (const spawner of this.spawners) {
251+
try {
252+
this.dimension.setBlockType(spawner.location, MinecraftBlockTypes.Air)
253+
} catch (e) {
254+
logger.error('clear spawner', e)
255+
}
256+
}
244257
}
245258

246259
fromAbsoluteToRelative(vector: Vector3) {
@@ -274,7 +287,7 @@ export class DungeonRegion extends Region {
274287
for (const value of inv) {
275288
const entityTypeId = /^(?:minecraft:)(.+)_spawn_egg$/.exec(value.typeId)?.[1]
276289
if (!entityTypeId) {
277-
console.warn('No entity type id for', value.typeId)
290+
logger.warn('No entity type id for', value.typeId)
278291
continue
279292
}
280293

@@ -283,7 +296,7 @@ export class DungeonRegion extends Region {
283296
this.createSpawner(rotated, entities, restoreTime)
284297
}
285298

286-
protected createSpawner(location: Vector3, entities: DungeonSpawner['entities'], restoreTime = ms.from('min', 20)) {
299+
protected createSpawner(location: Vector3, entities: DungeonSpawner['entities'], restoreTime = ms.from('sec', 30)) {
287300
this.spawners.push({
288301
id: Vec.string(location),
289302
location,
@@ -294,14 +307,18 @@ export class DungeonRegion extends Region {
294307

295308
private static invalidSpawnerTypeIds = new Set<string>()
296309

310+
private brokenTypeIdsMap = new Map([['evoker', MinecraftEntityTypes.EvocationIllager]])
311+
297312
private spawn(spawner: DungeonSpawner) {
298-
if (!anyPlayerNear(spawner.location, this.dimensionType, 20)) return
313+
if (!anyPlayerNear(spawner.location, this.dimensionType, 50)) return
299314

300-
for (const { typeId, amount } of spawner.entities) {
315+
// eslint-disable-next-line prefer-const
316+
for (let { typeId, amount } of spawner.entities) {
317+
typeId = this.brokenTypeIdsMap.get(typeId) ?? typeId
301318
const type = EntityTypes.get<string>(typeId)
302319
if (!type) {
303320
if (!DungeonRegion.invalidSpawnerTypeIds.has(typeId)) {
304-
console.warn('Dungeon spawner invalid typeId', typeId, spawner)
321+
logger.warn('Dungeon spawner invalid typeId', typeId, spawner)
305322
DungeonRegion.invalidSpawnerTypeIds.add(typeId)
306323
}
307324
continue
@@ -310,8 +327,16 @@ export class DungeonRegion extends Region {
310327
const entities = this.dimension.getEntities({ location: spawner.location, maxDistance: 20 })
311328
if (entities.length < amount) {
312329
const toSpawn = amount - entities.length
330+
logger.info(
331+
noI18n`Spawning for ${this.displayName} at ${Vec.string(spawner.location, true)}: ${toSpawn} ${typeId} `,
332+
)
313333
for (let i = 0; i < toSpawn; i++) {
314-
this.dimension.spawnEntity(type, spawner.location)
334+
try {
335+
this.dimension.spawnEntity(type, spawner.location)
336+
return true
337+
} catch (e) {
338+
logger.error('spawn', e)
339+
}
315340
}
316341
}
317342
}
@@ -324,7 +349,7 @@ export class DungeonRegion extends Region {
324349
}
325350

326351
protected createChest(location: Vector3, loot: LootTable, restoreTime = ms.from('min', 20)) {
327-
// console.log(
352+
// logger.info(
328353
// 'Created a chest at',
329354
// Vector.string(location, true),
330355
// Vector.string(this.fromRelativeToAbsolute(location)),
@@ -361,12 +386,12 @@ export class DungeonRegion extends Region {
361386
system.delay(() => {
362387
try {
363388
if (id) {
364-
console.log('Placing structure', id, 'at', position)
389+
logger.info('Placing structure', id, 'at', position)
365390
world.structureManager.place(id, dimension, position)
366391
world.structureManager.delete(id)
367392
}
368393
} catch (e) {
369-
console.warn('Unable to place structure with id', id, e)
394+
logger.warn('Unable to place structure with id', id, e)
370395
}
371396
})
372397
}

src/modules/places/dungeons/loot.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,25 @@ const powerfullLoot: Record<string, LootTable | undefined> = {
7171
.item('GoldenApple')
7272
.weight('10%')
7373
.amount({
74-
'5...20': '1%',
74+
'1...3': '1%',
7575
})
7676

7777
.item('IronIngot')
7878
.weight('10%')
7979
.amount({
80-
'5...10': '1%',
80+
'2...5': '1%',
8181
})
8282

8383
.item('GoldenCarrot')
8484
.weight('10%')
8585
.amount({
86-
'5...20': '1%',
86+
'1...4': '1%',
8787
})
8888

8989
.item('GoldIngot')
9090
.weight('5%')
9191
.amount({
92-
'5...10': '1%',
92+
'1...5': '1%',
9393
})
9494

9595
.item('TotemOfUndying')
@@ -112,13 +112,13 @@ const loot: Record<string, LootTable | undefined> = {
112112
.item('Apple')
113113
.weight('10%')
114114
.amount({
115-
'5...20': '1%',
115+
'1...5': '1%',
116116
})
117117

118118
.item('IronIngot')
119119
.weight('5%')
120120
.amount({
121-
'5...10': '1%',
121+
'1...3': '1%',
122122
})
123123

124124
.item('Carrot')
@@ -130,8 +130,8 @@ const loot: Record<string, LootTable | undefined> = {
130130
.item(Items.Money)
131131
.weight('100%')
132132
.amount({
133-
'10...20': '80%',
134-
'21...64': '20%',
133+
'1...5': '80%',
134+
'6...10': '20%',
135135
}).build,
136136
[d.GasStationGarage]: defaultLoot,
137137
} satisfies Record<StructureDungeonsId, LootTable>

0 commit comments

Comments
 (0)