-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_mesh.go
163 lines (151 loc) · 4.45 KB
/
db_mesh.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/tidwall/buntdb"
)
/* ---- */
/* MESH */
/* ---- */
func (db *DB) newMesh(p *tMesh) {
if p == nil {
panic("newMesh: TMesh is NIL")
}
err := db.db.Update(func(tx *buntdb.Tx) error {
oldMeshes := []string{}
X := strconv.FormatInt(p.Position.X, 10)
Z := strconv.FormatInt(p.Position.Z, 10)
tx.Intersects("meshPos", "["+X+" "+Z+"],["+X+" "+Z+"]", func(key, val string) bool {
kID := strings.Split(key, ":")[1]
vl, err := tx.Get("mesh:" + kID + ":verticalLevel")
if err == nil {
verticalLevel, err := strconv.ParseInt(vl, 10, 64)
utilsCheck(err)
if verticalLevel != p.VerticalLevel { // do not delete if it is not in the same vertical level
return true
}
}
oldMeshes = append(oldMeshes, key)
return true
})
for _, m := range oldMeshes {
_, err := tx.Delete(m)
if err != nil {
fmt.Println("error deleting", m, err)
}
}
index := dbNextIndex(tx)
tx.Set("mesh:"+index+":type", strconv.FormatInt(p.Type, 10), nil)
tx.Set("mesh:"+index+":pos", fmt.Sprintf("[%v %v]", p.Position.X, p.Position.Z), nil)
tx.Set("mesh:"+index+":verticalLevel", strconv.FormatInt(p.VerticalLevel, 10), nil)
tx.Set("mesh:"+index+":walkable", utilsBoolToString(p.Walkable), nil)
tx.Set("mesh:"+index+":walkingCost", strconv.Itoa(p.WalkingCost), nil)
tx.Set("mesh:"+index+":rotation", p.Rotation, nil)
p.ID = index
return nil
})
utilsCheck(err)
}
func (db *DB) getMesh(p *tMesh, ID string) {
db.db.View(func(tx *buntdb.Tx) error {
rot, err := tx.Get("mesh:" + ID + ":rotation")
T, err := tx.Get("mesh:" + ID + ":type")
utilsCheck(err)
pos, err := tx.Get("mesh:" + ID + ":pos")
utilsCheck(err)
verticalLevel, err := tx.Get("mesh:" + ID + ":verticalLevel")
utilsCheck(err)
walkable, err := tx.Get("mesh:" + ID + ":walkable")
utilsCheck(err)
walkingCost, err := tx.Get("mesh:" + ID + ":walkingCost")
utilsCheck(err)
if rot == "" {
rot = "down" // down default value
}
var X, Z int64
fmt.Sscanf(pos, "[%d %d]", &X, &Z)
p.ID = ID
p.Type, err = strconv.ParseInt(T, 10, 64)
p.Position.X = X
p.Position.Z = Z
p.Rotation = rot
p.VerticalLevel, err = strconv.ParseInt(verticalLevel, 10, 64)
p.Walkable = utilsStringToBool(walkable)
p.WalkingCost, err = strconv.Atoi(walkingCost)
utilsCheck(err)
return nil
})
}
func (db *DB) deleteMesh(ID string) {
fmt.Println("DELETING", ID)
err := db.db.Update(func(tx *buntdb.Tx) error {
tx.Delete("mesh:" + ID + ":rotation")
tx.Delete("mesh:" + ID + ":type")
tx.Delete("mesh:" + ID + ":pos")
tx.Delete("mesh:" + ID + ":verticalLevel")
tx.Delete("mesh:" + ID + ":walkable")
tx.Delete("mesh:" + ID + ":walkingCost")
return nil
})
utilsCheck(err)
fmt.Println("DELETED")
}
func (db *DB) deleteMeshByPos(x, z, y int64) {
meshAux := &tMesh{}
meshesKeys := []string{}
_x := strconv.FormatInt(x, 10)
_z := strconv.FormatInt(z, 10)
db.db.View(func(tx *buntdb.Tx) error {
return tx.Intersects("meshPos", "["+_x+" "+_z+"],["+_x+" "+_z+"]", func(key, val string) bool {
kID := strings.Split(key, ":")[1]
db.getMesh(meshAux, kID)
if meshAux.VerticalLevel == y {
fmt.Printf("DELETE mesh found! %v %+v", kID, meshAux)
meshesKeys = append(meshesKeys, kID)
}
return true
})
})
for _, m := range meshesKeys {
db.deleteMesh(m)
}
}
func (db *DB) getNearbyMeshes(x, z, dist int64, meshesKeys *[]string, meshesValues *[]string) {
if meshesValues == nil {
meshesValues = &[]string{}
}
topLeftX := strconv.FormatInt(x-dist, 10)
topLeftZ := strconv.FormatInt(z-dist, 10)
bottomRightX := strconv.FormatInt(x+dist, 10)
bottomRightZ := strconv.FormatInt(z+dist, 10)
db.db.View(func(tx *buntdb.Tx) error {
tx.Intersects("meshPos", "["+topLeftX+" "+topLeftZ+"],["+bottomRightX+" "+bottomRightZ+"]", func(key, val string) bool {
kID := strings.Split(key, ":")[1]
*meshesKeys = append(*meshesKeys, kID)
*meshesValues = append(*meshesValues, val)
return true
})
return nil
})
}
func (db *DB) getMeshByPos(x, z, y int64) *tMesh {
res := &tMesh{}
meshesKeys := []string{}
_x := strconv.FormatInt(x, 10)
_z := strconv.FormatInt(z, 10)
db.db.View(func(tx *buntdb.Tx) error {
return tx.Intersects("meshPos", "["+_x+" "+_z+"],["+_x+" "+_z+"]", func(key, val string) bool {
kID := strings.Split(key, ":")[1]
meshesKeys = append(meshesKeys, kID)
return true
})
})
for _, m := range meshesKeys {
db.getMesh(res, m)
if res.VerticalLevel == y {
return res
}
}
return nil
}