Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions server/block/dirt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package block

import (
"math/rand/v2"

"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
)

Expand Down Expand Up @@ -59,6 +62,37 @@ func (d Dirt) EncodeBlock() (string, map[string]any) {
return "minecraft:dirt", nil
}

// spreadDirt turns the block passed into dirt in low light and otherwise spreads it onto nearby dirt. It is
// the random tick behaviour shared by grass blocks and mycelium. minY is the lowest vertical offset a spread
// attempt may target.
func spreadDirt(b world.Block, pos cube.Pos, tx *world.Tx, r *rand.Rand, minY int) {
aboveLight := tx.Light(pos.Side(cube.FaceUp))
if aboveLight < 4 {
tx.SetBlock(pos, Dirt{}, nil)
return
}
if aboveLight < 9 {
return
}

// A single uint32 is enough as only 28 bits are needed, 7 per iteration.
n := r.Uint32()

for range 4 {
x, y, z := int(n)%3, int(n>>2)%5, int(n>>5)%3
n >>= 7

spreadPos := pos.Add(cube.Pos{x - 1, y + minY, z - 1})
if tx.Light(spreadPos.Side(cube.FaceUp)) < 4 {
continue
}
if dirt, ok := tx.Block(spreadPos).(Dirt); !ok || dirt.Coarse {
continue
}
tx.SetBlock(spreadPos, b, nil)
}
}

// supportsVegetation checks if the vegetation can exist on the block.
func supportsVegetation(vegetation, block world.Block) bool {
soil, ok := block.(Soil)
Expand Down
31 changes: 1 addition & 30 deletions server/block/grass.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,7 @@ func (g Grass) SoilFor(block world.Block) bool {

// RandomTick handles the ticking of grass, which may or may not result in the spreading of grass onto dirt.
func (g Grass) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
aboveLight := tx.Light(pos.Side(cube.FaceUp))
if aboveLight < 4 {
// The light above the block is too low: The grass turns to dirt.
tx.SetBlock(pos, Dirt{}, nil)
return
}
if aboveLight < 9 {
// Don't attempt to spread if the light level is lower than 9.
return
}

// Generate a single uint32 as we only need 28 bits (7 bits each iteration).
n := r.Uint32()

// Four attempts to spread to another block.
for i := 0; i < 4; i++ {
x, y, z := int(n)%3, int(n>>2)%5, int(n>>5)%3
n >>= 7

spreadPos := pos.Add(cube.Pos{x - 1, y - 3, z - 1})
// Don't spread grass to locations where dirt is exposed to hardly any light.
if tx.Light(spreadPos.Side(cube.FaceUp)) < 4 {
continue
}
b := tx.Block(spreadPos)
if dirt, ok := b.(Dirt); !ok || dirt.Coarse {
continue
}
tx.SetBlock(spreadPos, g, nil)
}
spreadDirt(g, pos, tx, r, -3)
}

// BoneMeal ...
Expand Down
5 changes: 5 additions & 0 deletions server/block/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions server/block/mycelium.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package block

import (
"math/rand/v2"

"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
)

// Mycelium is a variant of dirt that covers the surface of the mushroom fields biome.
type Mycelium struct {
solid
}

// SoilFor ...
func (m Mycelium) SoilFor(block world.Block) bool {
switch block.(type) {
case ShortGrass, Fern, DoubleTallGrass, Flower, DoubleFlower, NetherSprouts, PinkPetals, SugarCane, DeadBush, BambooSapling, Bamboo:
return true
}
return false
}

// RandomTick ...
func (m Mycelium) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
spreadDirt(m, pos, tx, r, -2)
}

// Shovel ...
func (Mycelium) Shovel() (world.Block, bool) {
return DirtPath{}, true
}

// BreakInfo ...
func (m Mycelium) BreakInfo() BreakInfo {
return newBreakInfo(0.6, alwaysHarvestable, shovelEffective, silkTouchOneOf(Dirt{}, m))
}

// EncodeItem ...
func (Mycelium) EncodeItem() (name string, meta int16) {
return "minecraft:mycelium", 0
}

// EncodeBlock ...
func (Mycelium) EncodeBlock() (string, map[string]any) {
return "minecraft:mycelium", nil
}
4 changes: 2 additions & 2 deletions server/block/nether_sprouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type NetherSprouts struct {
// NeighbourUpdateTick ...
func (n NetherSprouts) NeighbourUpdateTick(pos, _ cube.Pos, tx *world.Tx) {
if !supportsVegetation(n, tx.Block(pos.Side(cube.FaceDown))) {
breakBlock(n, pos, tx) // TODO: Nylium & mycelium
breakBlock(n, pos, tx) // TODO: Nylium
}
}

Expand All @@ -28,7 +28,7 @@ func (n NetherSprouts) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, tx
return false
}
if !supportsVegetation(n, tx.Block(pos.Side(cube.FaceDown))) {
return false // TODO: Nylium & mycelium
return false // TODO: Nylium
}

place(tx, pos, n, user, ctx)
Expand Down
2 changes: 2 additions & 0 deletions server/block/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func init() {
world.RegisterBlock(MossCarpet{})
world.RegisterBlock(MudBricks{})
world.RegisterBlock(Mud{})
world.RegisterBlock(Mycelium{})
world.RegisterBlock(NetherBrickFence{})
world.RegisterBlock(NetherGoldOre{})
world.RegisterBlock(NetherQuartzOre{})
Expand Down Expand Up @@ -366,6 +367,7 @@ func init() {
world.RegisterItem(MudBricks{})
world.RegisterItem(MuddyMangroveRoots{})
world.RegisterItem(Mud{})
world.RegisterItem(Mycelium{})
world.RegisterItem(NetherBrickFence{})
world.RegisterItem(NetherGoldOre{})
world.RegisterItem(NetherQuartzOre{})
Expand Down