This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
99 lines (81 loc) · 2.01 KB
/
api.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
package mmap
import (
"github.com/anon55555/mt"
"github.com/ev2-1/minetest-go"
"github.com/EliasFleckenstein03/mtmap"
)
// LoadBlk sends a minetest.Client a blk at pos
// TODO: if force is false, will only update every 10 seconds (atm sends only once (per client))
// triggers SBMs
func LoadBlk(c *minetest.Client, p [3]int16, force bool) {
loadedChunksMu.Lock()
if loadedChunks[c] == nil {
loadedChunks[c] = make(map[pos]bool)
}
loadedChunksMu.Unlock()
loadedChunksMu.RLock()
if !force && loadedChunks[c][p] {
loadedChunksMu.RUnlock()
return
}
loadedChunksMu.RUnlock()
ch := GetBlk(p)
go func() {
blkdata := <-ch
if blkdata == nil {
SetBlk(p, &exampleBlk)
blkdata = &exampleBlk
}
loadedChunksMu.Lock()
loadedChunks[c][p] = true
loadedChunksMu.Unlock()
doSBM(c, p, blkdata)
c.SendCmd(&mt.ToCltBlkData{
Blkpos: p,
Blk: blkdata.MapBlk,
})
}()
return
}
// func GetBlk(p [3]int16) *mtmap.MapBlk)
// func SetBlk(p [3]int16, blk *mtmap.MapBlk)
// are in db.go
// GetNode returns the given mt.Content at a specified spot
// returns nil if blk does not exist
func GetNode(pos [3]int16) *mt.Node {
p, i := mt.Pos2Blkpos(pos)
blk := <-GetBlk(p)
if blk == nil {
return nil
}
return &mt.Node{
Param0: blk.Param0[i],
Param1: blk.Param1[i],
Param2: blk.Param2[i],
}
}
// SetNode reads a blk and sets node to node
// then saves
func SetNode(pos [3]int16, node mt.Node) {
blk, i := mt.Pos2Blkpos(pos)
oldBlk := <-GetBlk(blk)
if oldBlk == nil {
oldBlk = EmptyBlk()
oldBlk.Flags |= mtmap.NotGenerated
}
oldBlk.Param0[i] = node.Param0
oldBlk.Param1[i] = node.Param1
oldBlk.Param2[i] = node.Param2
// TODO: node meta
SetBlk(blk, oldBlk)
}
// GetConfigFields returns a list of configuration fields incl a description
// TODO: write configuration lib
/*func GetConfigFileds() []struct{ Name, Desc string } {
return []struct{ Name, Desc string }{
struct{ Name, Desc string }{
Name: "Load",
Desc: "bool; if false disables spiral loading alogorithm; default true",
},
}
}*/