-
Notifications
You must be signed in to change notification settings - Fork 140
/
water.go
175 lines (158 loc) · 4.69 KB
/
water.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package block
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/item/potion"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"time"
)
// Water is a natural fluid that generates abundantly in the world.
type Water struct {
empty
replaceable
// Still makes the water appear as if it is not flowing.
Still bool
// Depth is the depth of the water. This is a number from 1-8, where 8 is a source block and 1 is the
// smallest possible water block.
Depth int
// Falling specifies if the water is falling. Falling water will always appear as a source block, but its
// behaviour differs when it starts spreading.
Falling bool
}
// EntityCollide ...
func (w Water) EntityCollide(e world.Entity) {
if fallEntity, ok := e.(FallDistanceEntity); ok {
fallEntity.ResetFallDistance()
}
if flammable, ok := e.(entity.Flammable); ok {
flammable.Extinguish()
}
}
// FillBottle ...
func (w Water) FillBottle() (world.Block, item.Stack, bool) {
if w.Depth == 8 {
return w, item.NewStack(item.Potion{Type: potion.Water()}, 1), true
}
return nil, item.Stack{}, false
}
// LiquidDepth returns the depth of the water.
func (w Water) LiquidDepth() int {
return w.Depth
}
// SpreadDecay returns 1 - The amount of levels decreased upon spreading.
func (Water) SpreadDecay() int {
return 1
}
// WithDepth returns the water with the depth passed.
func (w Water) WithDepth(depth int, falling bool) world.Liquid {
w.Depth = depth
w.Falling = falling
w.Still = false
return w
}
// LiquidFalling returns Water.Falling.
func (w Water) LiquidFalling() bool {
return w.Falling
}
// HasLiquidDrops ...
func (Water) HasLiquidDrops() bool {
return false
}
// LightDiffusionLevel ...
func (Water) LightDiffusionLevel() uint8 {
return 2
}
// ScheduledTick ...
func (w Water) ScheduledTick(pos cube.Pos, wo *world.World) {
if w.Depth == 7 {
// Attempt to form new water source blocks.
count := 0
pos.Neighbours(func(neighbour cube.Pos) {
if neighbour[1] == pos[1] {
if liquid, ok := wo.Liquid(neighbour); ok {
if water, ok := liquid.(Water); ok && water.Depth == 8 && !water.Falling {
count++
}
}
}
})
if count >= 2 {
func() {
if canFlowInto(w, wo, pos.Side(cube.FaceDown), true) {
return
}
// Only form a new source block if there either is no water below this block, or if the water
// below this is not falling (full source block).
wo.SetLiquid(pos, Water{Depth: 8, Still: true})
}()
}
}
tickLiquid(w, pos, wo)
}
// NeighbourUpdateTick ...
func (Water) NeighbourUpdateTick(pos, _ cube.Pos, wo *world.World) {
wo.ScheduleBlockUpdate(pos, time.Second/4)
}
// LiquidType ...
func (Water) LiquidType() string {
return "water"
}
// Harden hardens the water if lava flows into it.
func (w Water) Harden(pos cube.Pos, wo *world.World, flownIntoBy *cube.Pos) bool {
if flownIntoBy == nil {
return false
}
if lava, ok := wo.Block(pos.Side(cube.FaceUp)).(Lava); ok {
ctx := event.C()
wo.Handler().HandleLiquidHarden(ctx, pos, w, lava, Stone{})
ctx.Continue(func() {
wo.PlaceBlock(pos, Stone{})
wo.PlaySound(pos.Vec3Centre(), sound.Fizz{})
})
return true
} else if lava, ok := wo.Block(*flownIntoBy).(Lava); ok {
ctx := event.C()
wo.Handler().HandleLiquidHarden(ctx, pos, w, lava, Cobblestone{})
ctx.Continue(func() {
wo.PlaceBlock(*flownIntoBy, Cobblestone{})
wo.PlaySound(pos.Vec3Centre(), sound.Fizz{})
})
return true
}
return false
}
// EncodeBlock ...
func (w Water) EncodeBlock() (name string, properties map[string]interface{}) {
if w.Depth < 1 || w.Depth > 8 {
panic("invalid water depth, must be between 1 and 8")
}
v := 8 - w.Depth
if w.Falling {
v += 8
}
if w.Still {
return "minecraft:water", map[string]interface{}{"liquid_depth": int32(v)}
}
return "minecraft:flowing_water", map[string]interface{}{"liquid_depth": int32(v)}
}
// allWater returns a list of all water states.
func allWater() (b []world.Block) {
f := func(still, falling bool) {
b = append(b, Water{Still: still, Falling: falling, Depth: 8})
b = append(b, Water{Still: still, Falling: falling, Depth: 7})
b = append(b, Water{Still: still, Falling: falling, Depth: 6})
b = append(b, Water{Still: still, Falling: falling, Depth: 5})
b = append(b, Water{Still: still, Falling: falling, Depth: 4})
b = append(b, Water{Still: still, Falling: falling, Depth: 3})
b = append(b, Water{Still: still, Falling: falling, Depth: 2})
b = append(b, Water{Still: still, Falling: falling, Depth: 1})
}
f(true, true)
f(true, false)
f(false, false)
f(false, true)
return
}