Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- technic = technic or {}
- technic.nodes = {}
- technic.nodes_file = {
- load = function()
- local nodesfile = io.open(minetest.get_worldpath()..'/technic_nodes.txt', "r")
- if nodesfile then
- local contents = nodesfile:read('*all')
- io.close(nodesfile)
- if contents ~= nil then
- local lines = string.split(contents, "\n")
- for _,entry in ipairs(lines) do
- local name, px, py, pz, meta = unpack(string.split(entry, "°"))
- technic.set_node({x=px, y=py, z=pz}, name, meta)
- end
- end
- end
- end,
- save = function() --WRITE CHANGES TO FILE
- local output = ''
- for x,ys in pairs(technic.nodes) do
- for y,zs in pairs(ys) do
- for z,names in pairs(zs) do
- for name,meta in pairs(names) do
- output = name.."°"..x.."°"..y.."°"..z.."°"..dump(meta).."\n"
- end
- end
- end
- end
- local f = io.open(minetest.get_worldpath()..'/technic_nodes.txt', "w")
- f:write(output)
- io.close(f)
- end
- }
- local function table_empty(tab) --looks if it's an empty table
- if dump(tab) == "{}" then --tab == {} didn't work
- return true
- end
- return false
- end
- function technic.nodes_info() --returns an info string of the node table
- local tmp = "[technic] "..dump(technic.nodes).."\n[technic]:\n"
- for x,a in pairs(technic.nodes) do
- for y,b in pairs(a) do
- for z,c in pairs(b) do
- for name,meta in pairs(c) do
- tmp = tmp..">\t"..name.." "..x.." "..y.." "..z.." "..dump(meta).."\n"
- end
- end
- end
- end
- return tmp
- end
- function technic.clean_node_table() --replaces {} with nil
- local again = true
- while again do
- again = false
- for x,ys in pairs(technic.nodes) do
- if table_empty(ys) then
- technic.nodes[x] = nil
- again = true
- else
- for y,zs in pairs(ys) do
- if table_empty(zs) then
- technic.nodes[x][y] = nil
- again = true
- else
- for z,names in pairs(zs) do
- if table_empty(names) then
- technic.nodes[x][y][z] = nil
- again = true
- else
- for name,meta in pairs(names) do
- if table_empty(meta)
- or meta == "" then
- technic.nodes[x][y][z][name] = nil
- again = true
- end
- end
- end
- end
- end
- end
- end
- end
- end
- end
- function technic.complete_node_table(pos, name) --neccesary because tab[1] wouldn't work if tab is not a table
- local tmp = technic.nodes[pos.x]
- if not tmp then
- technic.nodes[pos.x] = {}
- end
- local tmp = technic.nodes[pos.x][pos.y]
- if not tmp then
- technic.nodes[pos.x][pos.y] = {}
- end
- local tmp = technic.nodes[pos.x][pos.y][pos.z]
- if not tmp then
- technic.nodes[pos.x][pos.y][pos.z] = {}
- end
- local tmp = technic.nodes[pos.x][pos.y][pos.z][name]
- if not tmp then
- technic.nodes[pos.x][pos.y][pos.z][name] = {}
- end
- end
- function technic.get_node(pos, name)
- if not pos then
- return false
- end
- local tmp = technic.nodes[pos.x]
- if not tmp
- or table_empty(tmp) then
- return false
- end
- local tmp = technic.nodes[pos.x][pos.y]
- if not tmp
- or table_empty(tmp) then
- return false
- end
- local tmp = technic.nodes[pos.x][pos.y][pos.z]
- if not tmp
- or table_empty(tmp) then
- return false
- end
- -- if name isn't mentioned, just look if there's a node
- if not name then
- return true
- end
- local tmp = technic.nodes[pos.x][pos.y][pos.z][name]
- if not tmp
- or table_empty(tmp) then
- return false
- end
- return tmp
- end
- function technic.remove_node(pos)
- if not pos then
- return false
- end
- if technic.get_node(pos) then
- technic.nodes[pos.x][pos.y][pos.z] = nil
- local xarr = technic.nodes[pos.x]
- if table_empty(xarr[pos.y]) then
- technic.nodes[pos.x][pos.y] = nil
- end
- if table_empty(xarr) then
- technic.nodes[pos.x] = nil
- end
- else
- print("[technic] Warning: The node at ("..pos.x.."|"..pos.y.."|"..pos.z..") wasn't stored in technic.nodes.")
- end
- end
- function technic.set_node(pos, name, meta)
- if not (name or pos) then
- return false
- end
- technic.complete_node_table(pos, name)
- meta = meta or true
- technic.nodes[pos.x][pos.y][pos.z][name] = meta
- end
- minetest.register_node("ac:block9", {
- description = "vartestblock",
- tiles = {"ac_block.png"},
- groups = {snappy=1,bendy=2,cracky=1},
- sounds = default_stone_sounds,
- on_construct = function(pos)
- technic.set_node(pos, "LV", 3)
- --print(technic.nodes_info())
- end,
- after_dig_node = function(pos)
- technic.remove_node(pos)
- --print(technic.nodes_info())
- end
- })
- minetest.register_chatcommand('cleantechnictable',{
- description = 'Tidy up technic.nodes.',
- params = "",
- privs = {},
- func = function(name)
- technic.clean_node_table()
- local tmp = technic.nodes_info()
- minetest.chat_send_player(name, tmp)
- print("[technic] "..tmp)
- end
- })
- technic.nodes_file["load"]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement