forked from quag/mcobj
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocktypes.go
127 lines (104 loc) · 2.15 KB
/
blocktypes.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
package main
type BoundaryLocator struct {
describer BlockDescriber
}
func (b *BoundaryLocator) Init() {
var describer = new(Describer)
describer.Init()
b.describer = describer
}
func (b *BoundaryLocator) IsBoundary(blockId, otherBlockId uint16) bool {
var (
block = b.describer.BlockInfo(byte(blockId & 0xff))
other = b.describer.BlockInfo(byte(otherBlockId & 0xff))
)
if !block.IsEmpty() {
if other.IsEmpty() {
return true
}
if block.IsItem() {
return true
}
if other.IsTransparent() && (other.IsItem() || blockId&0xff != otherBlockId&0xff) {
return true
}
}
return false
}
type Describer struct {
unknown BlockInfo
cache map[byte]BlockInfoByte
}
func (d *Describer) Init() {
d.cache = make(map[byte]BlockInfoByte)
for i := 0; i < 256; i++ {
var blockId = byte(i)
var blockType, hasType = blockTypeMap[blockId]
var value byte
if hasType {
if blockType.mass == Mass {
value += 1
}
if blockType.transparency == Transparent {
value += 2
}
if blockType.empty {
value += 4
}
} else {
value = 1
}
var infoByte = BlockInfoByte(value)
d.cache[blockId] = infoByte
}
}
func (d *Describer) BlockInfo(blockId byte) BlockInfo {
var info, _ = d.cache[blockId]
return info
}
type BlockDescriber interface {
BlockInfo(blockId byte) BlockInfo
}
type BlockInfo interface {
IsItem() bool
IsMass() bool
IsOpaque() bool
IsTransparent() bool
IsEmpty() bool
}
type BlockType struct {
blockId byte
mass SingularOrAggregate
transparency Transparency
empty bool
}
type Transparency bool
type SingularOrAggregate bool
const (
Transparent Transparency = true
Opaque Transparency = false
Item SingularOrAggregate = true
Mass SingularOrAggregate = false
)
type BlockInfoByte byte
func (b BlockInfoByte) IsItem() bool {
return b&1 == 0
}
func (b BlockInfoByte) IsMass() bool {
return b&1 != 0
}
func (b BlockInfoByte) IsOpaque() bool {
return b&2 == 0
}
func (b BlockInfoByte) IsTransparent() bool {
return b&2 != 0
}
func (b BlockInfoByte) IsEmpty() bool {
return b&4 != 0
}
func init() {
blockTypeMap = make(map[byte]*BlockType)
}
var (
blockTypeMap map[byte]*BlockType
)