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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end
--pseudoload.lua
--responsible for keeping up a database of all rail nodes existant in the world, regardless of whether the mapchunk is loaded.
advtrains.trackdb={}
--trackdb[tt][y][x][z]={conn1, conn2, rely1, rely2, railheight}
--serialization format:
--(2byte x)(2byte y)(2byte z)(4bits conn1, 4bits conn2)[(plain rely1)|(plain rely2)|(plain railheight)]\n
--[] may be missing if 0,0,0
--load initially
--[[ TODO temporary outcomment
--delayed since all traintypes need to be registered
minetest.after(0, function()
for tt, _ in pairs(advtrains.all_traintypes) do
local pl_fpath=minetest.get_worldpath().."/advtrains_trackdb_"..tt
advtrains.trackdb[tt]={}
local file, err = io.open(pl_fpath, "r")
if not file then
local er=err or "Unknown Error"
print("[advtrains]Failed loading advtrains trackdb save file "..er)
else
--custom format to save memory
while true do
local xbytes=file:read(2)
if not xbytes or #xbytes<2 then
break --eof reached
end
print(xbytes)
local ybytes=file:read(2)
local zbytes=file:read(2)
local x=(string.byte(xbytes[1])-128)*256+(string.byte(xbytes[2]))
local y=(string.byte(ybytes[1])-128)*256+(string.byte(ybytes[2]))
local z=(string.byte(zbytes[1])-128)*256+(string.byte(zbytes[2]))
local conn1=string.byte(file:read(1))
local conn1=string.byte(file:read(1))
if not advtrains.trackdb[tt][y] then advtrains.trackdb[tt][y]={} end
if not advtrains.trackdb[tt][y][x] then advtrains.trackdb[tt][y][x]={} end
local rest=file.read("*l")
if rest~="" then
local rely1, rely2, railheight=string.match(rest, "([^|]+)|([^|]+)|([^|]+)")
if rely1 and rely2 and railheight then
advtrains.trackdb[tt][y][x][z]={
conn1=conn1, conn2=conn2,
rely1=rely1, rely2=rely2,
railheight=railheight
}
else
advtrains.trackdb[tt][y][x][z]={
conn1=conn1, conn2=conn2
}
end
else
advtrains.trackdb[tt][y][x][z]={
conn1=conn1, conn2=conn2
}
end
end
file:close()
end
end
--end minetest.after
end)
function advtrains.save_trackdb()
for tt, _ in pairs(advtrains.all_traintypes) do
|