-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
133 lines (110 loc) · 2.48 KB
/
map_test.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
package maprenderer_test
import (
"bufio"
"encoding/hex"
"fmt"
"os"
"strconv"
"strings"
"github.com/minetest-go/mapparser"
"github.com/minetest-go/types"
)
func NewMap() *Map {
return &Map{
hex_data: make(map[int64]string),
map_data: make(map[int64]*types.MapBlock),
}
}
type Map struct {
hex_data map[int64]string
map_data map[int64]*types.MapBlock
}
func (m Map) Load(csvfile string) error {
file, err := os.Open(csvfile)
if err != nil {
return err
}
sc := bufio.NewScanner(file)
line_num := 0
for sc.Scan() {
line := sc.Text()
parts := strings.Split(line, ",")
if len(parts) != 2 {
return fmt.Errorf("invalid format @ %d", line_num)
}
if len(parts[1])%2 != 0 {
return fmt.Errorf("invalid hex count @ %d, len: %d", line_num, len(parts[1]))
}
pos, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return err
}
m.hex_data[pos] = parts[1]
line_num++
}
return nil
}
func (m *Map) GetNode(pos *types.Pos) (*types.Node, error) {
mb_pos := pos.Divide(16)
pos_plain := CoordToPlain(mb_pos[0], mb_pos[1], mb_pos[2])
if m.hex_data[pos_plain] == "" {
// no mapblock there
return nil, nil
}
md := m.map_data[pos_plain]
if md == nil {
b, err := hex.DecodeString(m.hex_data[pos_plain])
if err != nil {
return nil, err
}
md, err = mapparser.Parse(b)
if err != nil {
return nil, err
}
m.map_data[pos_plain] = md
}
rel_pos := pos.Subtract(mb_pos.Multiply(16))
n := &types.Node{
Name: md.GetNodeName(rel_pos),
Param1: 0,
Param2: md.GetParam2(rel_pos),
Pos: pos,
}
return n, nil
}
// utils
const (
numBitsPerComponent = 12
modulo = 1 << numBitsPerComponent
maxPositive = modulo / 2
minValue = -1 << (numBitsPerComponent - 1)
maxValue = 1<<(numBitsPerComponent-1) - 1
MinPlainCoord = -34351347711
)
func CoordToPlain(x, y, z int) int64 {
return int64(z)<<(2*numBitsPerComponent) +
int64(y)<<numBitsPerComponent +
int64(x)
}
func unsignedToSigned(i int16) int {
if i < maxPositive {
return int(i)
}
return int(i - maxPositive*2)
}
// To match C++ code.
func pythonModulo(i int16) int16 {
const mask = modulo - 1
if i >= 0 {
return i & mask
}
return modulo - -i&mask
}
func PlainToCoord(i int64) (int, int, int) {
x := unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(x)) >> numBitsPerComponent
y := unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(y)) >> numBitsPerComponent
z := unsignedToSigned(pythonModulo(int16(i)))
return x, y, z
}