forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stair.go
119 lines (112 loc) · 4.63 KB
/
stair.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
package model
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity/physics"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)
// Stair is a model for stair-like blocks. These have different solid sides depending on the direction the
// stairs are facing, the surrounding blocks and whether it is upside down or not.
type Stair struct {
// Facing specifies the direction that the full side of the Stair faces.
Facing cube.Direction
// UpsideDown turns the Stair upside-down, meaning the full side of the Stair is turned to the top side of the
// block.
UpsideDown bool
}
// AABB returns a slice of physics.AABB depending on if the Stair is upside down and which direction it is facing.
// Additionally, these AABBs depend on the Stair blocks surrounding this one, which can influence the model.
func (s Stair) AABB(pos cube.Pos, w *world.World) []physics.AABB {
b := []physics.AABB{physics.NewAABB(mgl64.Vec3{}, mgl64.Vec3{1, 0.5, 1})}
if s.UpsideDown {
b[0] = physics.NewAABB(mgl64.Vec3{0, 0.5, 0}, mgl64.Vec3{1, 1, 1})
}
t := s.cornerType(pos, w)
face, oppositeFace := s.Facing.Face(), s.Facing.Opposite().Face()
if t == noCorner || t == cornerRightInner || t == cornerLeftInner {
b = append(b, physics.NewAABB(mgl64.Vec3{0.5, 0.5, 0.5}, mgl64.Vec3{0.5, 1, 0.5}).
ExtendTowards(face, 0.5).
Stretch(s.Facing.RotateRight().Face().Axis(), 0.5))
}
if t == cornerRightOuter {
b = append(b, physics.NewAABB(mgl64.Vec3{0.5, 0.5, 0.5}, mgl64.Vec3{0.5, 1, 0.5}).
ExtendTowards(face, 0.5).
ExtendTowards(s.Facing.RotateLeft().Face(), 0.5))
} else if t == cornerLeftOuter {
b = append(b, physics.NewAABB(mgl64.Vec3{0.5, 0.5, 0.5}, mgl64.Vec3{0.5, 1, 0.5}).
ExtendTowards(face, 0.5).
ExtendTowards(s.Facing.RotateRight().Face(), 0.5))
} else if t == cornerRightInner {
b = append(b, physics.NewAABB(mgl64.Vec3{0.5, 0.5, 0.5}, mgl64.Vec3{0.5, 1, 0.5}).
ExtendTowards(oppositeFace, 0.5).
ExtendTowards(s.Facing.RotateRight().Face(), 0.5))
} else if t == cornerLeftInner {
b = append(b, physics.NewAABB(mgl64.Vec3{0.5, 0.5, 0.5}, mgl64.Vec3{0.5, 1, 0.5}).
ExtendTowards(oppositeFace, 0.5).
ExtendTowards(s.Facing.RotateLeft().Face(), 0.5))
}
if s.UpsideDown {
for i := range b[1:] {
b[i+1] = b[i+1].Translate(mgl64.Vec3{0, -0.5})
}
}
return b
}
// FaceSolid returns true for all faces of the Stair that are completely filled.
func (s Stair) FaceSolid(pos cube.Pos, face cube.Face, w *world.World) bool {
if !s.UpsideDown && face == cube.FaceDown {
// Non-upside-down stairs have a closed side at the bottom.
return true
} else if s.UpsideDown && face == cube.FaceUp {
// Upside-down stairs always have a closed side at the top.
return true
}
t := s.cornerType(pos, w)
if t == cornerRightOuter || t == cornerLeftOuter {
// Small corner blocks, they do not block water flowing out horizontally.
return false
} else if t == noCorner {
// Not a corner, so only block directly behind the stairs.
return s.Facing.Face() == face
}
if t == cornerRightInner {
return face == s.Facing.RotateRight().Face() || face == s.Facing.Face()
}
return face == s.Facing.RotateLeft().Face() || face == s.Facing.Face()
}
const (
noCorner = iota
cornerRightInner
cornerLeftInner
cornerRightOuter
cornerLeftOuter
)
// cornerType returns the type of the corner that the stairs form, or 0 if it does not form a corner with any
// other stairs.
func (s Stair) cornerType(pos cube.Pos, w *world.World) uint8 {
rotatedFacing := s.Facing.RotateRight()
if closedSide, ok := w.Block(pos.Side(s.Facing.Face())).Model().(Stair); ok && closedSide.UpsideDown == s.UpsideDown {
if closedSide.Facing == rotatedFacing {
return cornerLeftOuter
} else if closedSide.Facing == rotatedFacing.Opposite() {
// This will only form a corner if there is not a stair on the right of this one with the same
// direction.
if side, ok := w.Block(pos.Side(s.Facing.RotateRight().Face())).Model().(Stair); !ok || side.Facing != s.Facing || side.UpsideDown != s.UpsideDown {
return cornerRightOuter
}
return noCorner
}
}
if openSide, ok := w.Block(pos.Side(s.Facing.Opposite().Face())).Model().(Stair); ok && openSide.UpsideDown == s.UpsideDown {
if openSide.Facing == rotatedFacing {
// This will only form a corner if there is not a stair on the right of this one with the same
// direction.
if side, ok := w.Block(pos.Side(s.Facing.RotateRight().Face())).Model().(Stair); !ok || side.Facing != s.Facing || side.UpsideDown != s.UpsideDown {
return cornerRightInner
}
} else if openSide.Facing == rotatedFacing.Opposite() {
return cornerLeftInner
}
}
return noCorner
}