-
Notifications
You must be signed in to change notification settings - Fork 140
/
beacon.go
221 lines (200 loc) · 6.03 KB
/
beacon.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package block
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity/effect"
"github.com/df-mc/dragonfly/server/entity/physics"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math"
"time"
_ "unsafe" // For compiler directives.
)
// Beacon is a block that projects a light beam skyward, and can provide status effects such as Speed, Jump
// Boost, Haste, Regeneration, Resistance, or Strength to nearby players.
type Beacon struct {
solid
transparent
clicksAndSticks
// Primary and Secondary are the primary and secondary effects broadcast to nearby entities by the
// beacon.
Primary, Secondary effect.Effect
// level is the amount of the pyramid's levels, it is defined by the mineral blocks which build up the
// pyramid, and can be 0-4.
level int
}
// BreakInfo ...
func (b Beacon) BreakInfo() BreakInfo {
return newBreakInfo(3, alwaysHarvestable, nothingEffective, oneOf(b))
}
// UseOnBlock ...
func (b Beacon) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(w, pos, face, b)
if !used {
return
}
place(w, pos, b, user, ctx)
return placed(ctx)
}
// Activate manages the opening of a beacon by activating it.
func (b Beacon) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User) {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
}
}
// DecodeNBT ...
func (b Beacon) DecodeNBT(data map[string]interface{}) interface{} {
b.level = int(readInt32(data, "Levels"))
if primary, ok := effect.ByID(int(readInt32(data, "Primary"))); ok {
b.Primary = primary
}
if secondary, ok := effect.ByID(int(readInt32(data, "Secondary"))); ok {
b.Secondary = secondary
}
return b
}
// EncodeNBT ...
func (b Beacon) EncodeNBT() map[string]interface{} {
m := map[string]interface{}{
"Levels": int32(b.level),
}
if primary, ok := effect.ID(b.Primary); ok {
m["Primary"] = int32(primary)
}
if secondary, ok := effect.ID(b.Secondary); ok {
m["Secondary"] = int32(secondary)
}
return m
}
// CanDisplace ...
func (b Beacon) CanDisplace(l world.Liquid) bool {
_, water := l.(Water)
return water
}
// SideClosed ...
func (b Beacon) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
return false
}
// LightEmissionLevel ...
func (Beacon) LightEmissionLevel() uint8 {
return 15
}
// Level returns an integer 0-4 which defines the current pyramid level of the beacon.
func (b Beacon) Level() int {
return b.level
}
// Tick recalculates level, recalculates the active state of the beacon, and powers players,
// once every 80 ticks (4 seconds).
func (b Beacon) Tick(currentTick int64, pos cube.Pos, w *world.World) {
if currentTick%80 == 0 {
before := b.level
// Recalculating pyramid level and powering up players in range once every 4 seconds.
b.level = b.recalculateLevel(pos, w)
if before != b.level {
w.SetBlock(pos, b)
}
if b.level == 0 {
return
}
if !b.obstructed(pos, w) {
b.broadcastBeaconEffects(pos, w)
}
}
}
// recalculateLevel recalculates the level of the beacon's pyramid and returns it. The level can be 0-4.
func (b Beacon) recalculateLevel(pos cube.Pos, w *world.World) int {
var lvl int
iter := 1
// This loop goes over all 4 possible pyramid levels.
for y := pos.Y() - 1; y >= pos.Y()-4; y-- {
for x := pos.X() - iter; x <= pos.X()+iter; x++ {
for z := pos.Z() - iter; z <= pos.Z()+iter; z++ {
if s, ok := w.Block(pos).(BeaconSource); !ok || !s.PowersBeacon() {
return lvl
}
}
}
iter++
lvl++
}
return lvl
}
// obstructed determines whether the beacon is currently obstructed.
func (b Beacon) obstructed(pos cube.Pos, w *world.World) bool {
// Fast obstructed light calculation.
if w.SkyLight(pos.Add(cube.Pos{0, 1})) == 15 {
return false
// Slow obstructed light calculation, if the fast way out failed.
} else if w.HighestLightBlocker(pos.X(), pos.Z()) <= uint8(pos.Y()) {
return false
}
return true
}
// broadcastBeaconEffects determines the entities in range which could receive the beacon's powers, and
// determines the powers (effects) that these entities could get. Afterwards, the entities in range that are
// beaconAffected get their according effect(s).
func (b Beacon) broadcastBeaconEffects(pos cube.Pos, w *world.World) {
seconds := 9 + b.level*2
if b.level == 4 {
seconds--
}
dur := time.Duration(seconds) * time.Second
// Establishing what effects are active with the current amount of beacon levels.
primary, secondary := b.Primary, effect.Effect(nil)
switch b.level {
case 0:
primary = nil
case 1:
switch primary.(type) {
case effect.Resistance, effect.JumpBoost, effect.Strength:
primary = nil
}
case 2:
if _, ok := primary.(effect.Strength); ok {
primary = nil
}
case 3:
default:
secondary = b.Secondary
}
// Determining whether the primary power is set.
if primary != nil {
primary = primary.WithSettings(dur, 1, true)
// Secondary power can only be set if the primary power is set.
if secondary != nil {
// It is possible to select 2 primary powers if the beacon's level is 4.
pId, pOk := effect.ID(primary)
sId, sOk := effect.ID(secondary)
if pOk && sOk && pId == sId {
primary = primary.WithSettings(dur, 2, true)
secondary = nil
} else {
secondary = secondary.WithSettings(dur, 1, true)
}
}
}
// Finding entities in range.
r := 10 + (b.level * 10)
entitiesInRange := w.EntitiesWithin(physics.NewAABB(
mgl64.Vec3{float64(pos.X() - r), -math.MaxFloat64, float64(pos.Z() - r)},
mgl64.Vec3{float64(pos.X() + r), math.MaxFloat64, float64(pos.Z() + r)},
))
for _, e := range entitiesInRange {
if p, ok := e.(beaconAffected); ok {
if primary != nil {
p.AddEffect(primary)
}
if secondary != nil {
p.AddEffect(secondary)
}
}
}
}
// EncodeItem ...
func (Beacon) EncodeItem() (name string, meta int16) {
return "minecraft:beacon", 0
}
// EncodeBlock ...
func (Beacon) EncodeBlock() (string, map[string]interface{}) {
return "minecraft:beacon", nil
}