4 ITB (insidethebox) minetest game - Copyright (C) 2017-2018 sofar & nore
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1
9 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 local function bytes_to_string(bytes
)
26 s
[i
] = string.char(bytes
[i
])
28 return table.concat(s
)
31 function boxes
.import_box(id
, box_data
)
33 local function read_u8()
34 local c
= string.byte(box_data
, data_index
)
35 data_index
= data_index
+ 1
38 local function read_u16()
43 local function read_pos()
47 return {x
= x
, y
= y
, z
= z
}
49 local version
= read_u8()
51 local type = read_u8()
52 local size
= read_pos()
53 local entry
= read_pos()
54 local exit = read_pos()
55 db
.box_set_data(id
, string.sub(box_data
, data_index
))
66 function boxes
.export_box(id
)
69 local function write_u8(x
)
71 data_index
= data_index
+ 1
73 local function write_u16(x
)
74 write_u8(math
.floor(x
/ 256))
77 local function write_pos(p
)
82 local meta
= db
.box_get_meta(id
)
83 write_u8(1) -- version
85 write_pos(meta
.meta
.size
)
86 write_pos(meta
.meta
.entry
)
87 write_pos(meta
.meta
.exit)
88 return bytes_to_string(data
) .. db
.box_get_data(id
)
91 local function read_file(filename
)
92 local file
= io
.open(filename
, "r")
94 local file_content
= file
:read("*all")
101 --[[ -- kept for historical purposes.
102 local function write_file(filename, data)
103 local file, err = io.open(filename, "w")
113 local modpath
= minetest
.get_modpath(minetest
.get_current_modname())
115 if not db
.box_exists(0) then
116 local box_data
= read_file(modpath
.. "/entry.box")
117 boxes
.import_box(0, box_data
)
120 if not db
.box_exists(1) then
121 local box_data
= read_file(modpath
.. "/exit.box")
122 boxes
.import_box(1, box_data
)
125 if not db
.box_exists(2) then
126 local box_data
= read_file(modpath
.. "/box.box")
127 boxes
.import_box(2, box_data
)
130 -- local worldpath = minetest.get_worldpath()
131 -- write_file(worldpath .. "/entry.box", boxes.export_box(0))
132 -- write_file(worldpath .. "/exit.box", boxes.export_box(1))
133 -- write_file(worldpath .. "/box.box", boxes.export_box(2))