@@ -15,7 +15,7 @@ import {
1515} from 'lib'
1616import { i18n , noI18n } from 'lib/i18n/text'
1717import { anyPlayerNearRegion } from 'lib/player-move'
18- import { ScheduleBlockPlace } from 'lib/scheduled-block-place '
18+ import { rollChance } from 'lib/rpg/random '
1919import { createLogger } from 'lib/utils/logger'
2020
2121// TODO Add chest generation
@@ -46,6 +46,8 @@ registerSaveableRegion('wardenDungeon', WardenDungeonRegion)
4646registerRegionType ( noI18n `Данж вардена` , WardenDungeonRegion )
4747
4848interface LinkedDatabase extends JsonObject {
49+ selected : boolean
50+ cleaned : boolean
4951 blocks : { x : number ; y : number ; z : number } [ ]
5052}
5153
@@ -54,7 +56,7 @@ class WardenDungeonLootRegion extends Region {
5456 get displayName ( ) : Text | undefined {
5557 return i18n . nocolor `§dНезеритовая жила`
5658 }
57- ldb : LinkedDatabase = { blocks : [ ] }
59+ ldb : LinkedDatabase = { selected : false , blocks : [ ] , cleaned : false }
5860
5961 protected defaultPermissions : RegionPermissions = {
6062 allowedAllItem : true ,
@@ -93,40 +95,70 @@ actionGuard((player, region, ctx) => {
9395
9496system . runInterval (
9597 ( ) => {
96- const regions = WardenDungeonLootRegion . getAll ( )
98+ let regions = WardenDungeonLootRegion . getAll ( )
9799 if ( ! regions . length ) return
98100
99101 const dungeonRegions = WardenDungeonRegion . getAll ( )
100102 if ( ! dungeonRegions [ 0 ] || ! anyPlayerNearRegion ( dungeonRegions [ 0 ] , 20 ) ) return
101103
102- const placedBefore = regions . find ( e => ! ! e . ldb . blocks . length )
104+ regions . forEach ( e => ( ( e . ldb . selected = false ) , ( e . ldb . cleaned = false ) , e . save ( ) ) )
103105
104- if ( placedBefore ) {
105- for ( const location of placedBefore . ldb . blocks ) {
106- if ( ! ScheduleBlockPlace . deleteAt ( location , placedBefore . dimensionType ) )
107- ScheduleBlockPlace . setAir ( location , placedBefore . dimensionType , 0 )
108- }
109- placedBefore . ldb . blocks = [ ]
110- placedBefore . save ( )
106+ const percent = 50
107+ const amount = Math . floor ( regions . length * ( percent / 100 ) )
108+
109+ const selectedRegions : WardenDungeonLootRegion [ ] = [ ]
110+ while ( selectedRegions . length <= amount ) {
111+ const newRegion = regions . randomElement ( )
112+ regions = regions . filter ( e => e !== newRegion )
113+ newRegion . ldb . selected = true
114+ selectedRegions . push ( newRegion )
111115 }
116+ } ,
117+ 'wardenDungeonUpdateLoot' ,
118+ fromMsToTicks ( ms . from ( 'min' , 1 ) ) ,
119+ )
112120
113- const newRegion = regions . randomElement ( )
114- newRegion . area . forEachVector ( ( location , isIn ) => {
115- if ( ! isIn ) return
116- if ( Vec . distance ( location , newRegion . area . center ) > newRegion . area . radius ) return
117- if ( Math . randomInt ( 1 , 6 ) === 1 ) return
118- const below = newRegion . dimension . getBlock ( Vec . add ( location , Vec . down ) )
119- if ( ! below || below . isAir ) return
120-
121- ScheduleBlockPlace . set ( {
122- restoreTime : 0 ,
123- dimension : newRegion . dimensionType ,
124- typeId : MinecraftBlockTypes . AncientDebris ,
125- location,
126- } )
127- newRegion . ldb . blocks . push ( location )
128- newRegion . save ( )
129- } , 1000 )
121+ system . runInterval (
122+ ( ) => {
123+ const regions = WardenDungeonLootRegion . getAll ( ) . slice ( )
124+ if ( ! regions . length ) return
125+
126+ const dungeonRegions = WardenDungeonRegion . getAll ( )
127+ if ( ! dungeonRegions [ 0 ] || ! anyPlayerNearRegion ( dungeonRegions [ 0 ] , 20 ) ) return
128+
129+ for ( const region of regions ) {
130+ if ( ! anyPlayerNearRegion ( region , 10 ) ) continue
131+
132+ if ( ! region . ldb . cleaned ) {
133+ region . ldb . cleaned = true
134+ for ( const block of region . ldb . blocks ) {
135+ region . dimension . setBlockType ( block , MinecraftBlockTypes . Air )
136+ }
137+ region . ldb . blocks = [ ]
138+ region . save ( )
139+ }
140+
141+ if ( ! region . ldb . selected || region . ldb . blocks . length ) continue
142+
143+ let chest = false
144+ region . area . forEachVector ( ( location , isIn ) => {
145+ if ( ! isIn ) return
146+ if ( Vec . distance ( location , region . area . center ) > region . area . radius ) return
147+ if ( ! rollChance ( 90 ) ) return
148+
149+ const below = region . dimension . getBlock ( Vec . add ( location , Vec . down ) )
150+ if ( ! below || below . isAir ) return
151+
152+ if ( rollChance ( 10 ) ) {
153+ chest = true
154+ region . dimension . setBlockType ( location , MinecraftBlockTypes . Chest )
155+ } else {
156+ region . dimension . setBlockType ( location , MinecraftBlockTypes . AncientDebris )
157+ }
158+ region . ldb . blocks . push ( location )
159+ region . save ( )
160+ } , 1000 )
161+ }
130162 } ,
131163 'wardenDungeonUpdateLoot' ,
132164 fromMsToTicks ( ms . from ( 'min' , 1 ) ) ,
0 commit comments