Skip to content

Commit

Permalink
Improve server-tower generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kneelawk committed Jul 26, 2023
1 parent 2b16bb8 commit 0e4ef7c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/github/hotm/mod/HotMMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.hotm.mod.item.HotMItems
import com.github.hotm.mod.misc.HotMCreativeTabs
import com.github.hotm.mod.world.biome.NecterePortalData
import com.github.hotm.mod.world.gen.carver.HotMCarvers
import com.github.hotm.mod.world.gen.decorator.HotMPlacementModifiers
import com.github.hotm.mod.world.gen.feature.HotMFeatures
import com.github.hotm.mod.world.gen.structure.HotMStructurePieces
import com.github.hotm.mod.world.gen.structure.HotMStructures
Expand All @@ -27,6 +28,7 @@ object HotMMod : ModInitializer {
HotMBlockEntities.init()
HotMPointOfInterestTypes.init()
HotMCarvers.init()
HotMPlacementModifiers.init()
HotMFeatures.init()
HotMSurfaceBuilders.init()
HotMStructures.init()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.hotm.mod.world.gen.decorator

import com.github.hotm.mod.Constants.id
import net.minecraft.registry.Registries
import net.minecraft.registry.Registry
import net.minecraft.world.gen.decorator.PlacementModifierType

object HotMPlacementModifiers {
val RANDOM_SURFACE_IN_RANGE by lazy { PlacementModifierType { RandomSurfaceInRangePlacementModifier.MODIFIER_CODEC } }

fun init() {
Registry.register(Registries.PLACEMENT_MODIFIER_TYPE, id("random_surface_in_range"), RANDOM_SURFACE_IN_RANGE)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.hotm.mod.world.gen.decorator

import java.util.stream.Stream
import it.unimi.dsi.fastutil.ints.IntArrayList
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.util.math.BlockPos
import net.minecraft.util.random.RandomGenerator
import net.minecraft.world.gen.YOffset
import net.minecraft.world.gen.decorator.DecoratorContext
import net.minecraft.world.gen.decorator.PlacementModifierType
import net.minecraft.world.gen.feature.PlacementModifier

class RandomSurfaceInRangePlacementModifier(val minInclusive: YOffset, val maxInclusive: YOffset) :
PlacementModifier() {
companion object {
val MODIFIER_CODEC: Codec<RandomSurfaceInRangePlacementModifier> = RecordCodecBuilder.create { instance ->
instance.group(
YOffset.OFFSET_CODEC.fieldOf("min_inclusive").forGetter { it.minInclusive },
YOffset.OFFSET_CODEC.fieldOf("max_inclusive").forGetter { it.maxInclusive }
).apply(instance, ::RandomSurfaceInRangePlacementModifier)
}

private val levels: ThreadLocal<IntArrayList> = ThreadLocal.withInitial { IntArrayList() }
}

override fun getPositions(context: DecoratorContext, random: RandomGenerator, pos: BlockPos): Stream<BlockPos> {
val minHeight = minInclusive.getY(context)
val maxHeight = maxInclusive.getY(context)

val mutable = pos.mutableCopy()
val ls = levels.get()
ls.clear()
for (y in minHeight..maxHeight) {
mutable.y = y
if (context.getBlockState(mutable).isAir && !context.getBlockState(mutable.down()).isAir) {
ls.add(y)
}
}

if (ls.isEmpty) {
return Stream.empty()
} else {
return Stream.of(BlockPos(pos.x, ls.getInt(random.nextInt(ls.size)), pos.z))
}
}

override fun getType(): PlacementModifierType<*> = HotMPlacementModifiers.RANDOM_SURFACE_IN_RANGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,6 @@ class ServerTowerFeature(codec: Codec<ServerTowerConfig>) : Feature<ServerTowerC
y--
}

return config.maxDrop
return pos.y - config.maxDrop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"max_height": 10,
"min_border": 0,
"max_border": 2,
"max_drop": -40,
"max_drop": 5,
"flat_lamp_chance": 0.5,
"structure": {
"Name": "hotm:smooth_thinking_stone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"max_height": 10,
"min_border": 0,
"max_border": 2,
"max_drop": -40,
"max_drop": 5,
"flat_lamp_chance": 0.5,
"structure": {
"Name": "hotm:smooth_thinking_stone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
"type": "minecraft:in_square"
},
{
"type": "minecraft:height_range",
"height": {
"type": "minecraft:uniform",
"min_inclusive": {
"absolute": 0
},
"max_inclusive": {
"absolute": 20
}
"type": "hotm:random_surface_in_range",
"min_inclusive": {
"absolute": 0
},
"max_inclusive": {
"absolute": 100
}
},
{
Expand Down

0 comments on commit 0e4ef7c

Please sign in to comment.