Advertisement
Guest User

Untitled

a guest
Jan 11th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. technic = technic or {}
  2. technic.nodes = {}
  3.  
  4. technic.nodes_file = {
  5.     load = function()
  6.         local nodesfile = io.open(minetest.get_worldpath()..'/technic_nodes.txt', "r")  
  7.         if nodesfile then
  8.             local contents = nodesfile:read('*all')
  9.             io.close(nodesfile)
  10.             if contents ~= nil then
  11.                 local lines = string.split(contents, "\n")
  12.                 for _,entry in ipairs(lines) do
  13.                     local name, px, py, pz, meta = unpack(string.split(entry, "°"))
  14.                     technic.set_node({x=px, y=py, z=pz}, name, meta)
  15.                 end
  16.             end
  17.         end
  18.     end,
  19.     save = function() --WRITE CHANGES TO FILE
  20.         local output = ''
  21.         for x,ys in pairs(technic.nodes) do
  22.             for y,zs in pairs(ys) do
  23.                 for z,names in pairs(zs) do
  24.                     for name,meta in pairs(names) do
  25.                         output = name.."°"..x.."°"..y.."°"..z.."°"..dump(meta).."\n"
  26.                     end
  27.                 end
  28.             end
  29.         end
  30.         local f = io.open(minetest.get_worldpath()..'/technic_nodes.txt', "w")
  31.         f:write(output)
  32.         io.close(f)
  33.     end
  34. }
  35.  
  36. local function table_empty(tab) --looks if it's an empty table
  37.     if dump(tab) == "{}" then --tab == {} didn't work
  38.         return true
  39.     end
  40.     return false
  41. end
  42.  
  43. function technic.nodes_info() --returns an info string of the node table
  44.     local tmp = "[technic] "..dump(technic.nodes).."\n[technic]:\n"
  45.     for x,a in pairs(technic.nodes) do
  46.         for y,b in pairs(a) do
  47.             for z,c in pairs(b) do
  48.                 for name,meta in pairs(c) do
  49.                     tmp = tmp..">\t"..name.." "..x.." "..y.." "..z.." "..dump(meta).."\n"
  50.                 end
  51.             end
  52.         end
  53.     end
  54.     return tmp
  55. end
  56.  
  57. function technic.clean_node_table() --replaces {} with nil
  58.     local again = true
  59.     while again do
  60.         again = false
  61.         for x,ys in pairs(technic.nodes) do
  62.             if table_empty(ys) then
  63.                 technic.nodes[x] = nil
  64.                 again = true
  65.             else
  66.                 for y,zs in pairs(ys) do
  67.                     if table_empty(zs) then
  68.                         technic.nodes[x][y] = nil
  69.                         again = true
  70.                     else
  71.                         for z,names in pairs(zs) do
  72.                             if table_empty(names) then
  73.                                 technic.nodes[x][y][z] = nil
  74.                                 again = true
  75.                             else
  76.                                 for name,meta in pairs(names) do
  77.                                     if table_empty(meta)
  78.                                     or meta == "" then
  79.                                         technic.nodes[x][y][z][name] = nil
  80.                                         again = true
  81.                                     end
  82.                                 end
  83.                             end
  84.                         end
  85.                     end
  86.                 end
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. function technic.complete_node_table(pos, name) --neccesary because tab[1] wouldn't work if tab is not a table
  93.     local tmp = technic.nodes[pos.x]
  94.     if not tmp then
  95.         technic.nodes[pos.x] = {}
  96.     end
  97.     local tmp = technic.nodes[pos.x][pos.y]
  98.     if not tmp then
  99.         technic.nodes[pos.x][pos.y] = {}
  100.     end
  101.     local tmp = technic.nodes[pos.x][pos.y][pos.z]
  102.     if not tmp then
  103.         technic.nodes[pos.x][pos.y][pos.z] = {}
  104.     end
  105.     local tmp = technic.nodes[pos.x][pos.y][pos.z][name]
  106.     if not tmp then
  107.         technic.nodes[pos.x][pos.y][pos.z][name] = {}
  108.     end
  109. end
  110.  
  111. function technic.get_node(pos, name)
  112.     if not pos then
  113.         return false
  114.     end
  115.     local tmp = technic.nodes[pos.x]
  116.     if not tmp
  117.     or table_empty(tmp) then
  118.         return false
  119.     end
  120.     local tmp = technic.nodes[pos.x][pos.y]
  121.     if not tmp
  122.     or table_empty(tmp) then
  123.         return false
  124.     end
  125.     local tmp = technic.nodes[pos.x][pos.y][pos.z]
  126.     if not tmp
  127.     or table_empty(tmp) then
  128.         return false
  129.     end
  130.  
  131.     -- if name isn't mentioned, just look if there's a node
  132.     if not name then
  133.         return true
  134.     end
  135.  
  136.     local tmp = technic.nodes[pos.x][pos.y][pos.z][name]
  137.     if not tmp
  138.     or table_empty(tmp) then
  139.         return false
  140.     end
  141.     return tmp
  142. end
  143.  
  144. function technic.remove_node(pos)
  145.     if not pos then
  146.         return false
  147.     end
  148.     if technic.get_node(pos) then
  149.         technic.nodes[pos.x][pos.y][pos.z] = nil
  150.         local xarr = technic.nodes[pos.x]
  151.         if table_empty(xarr[pos.y]) then
  152.             technic.nodes[pos.x][pos.y] = nil
  153.         end
  154.         if table_empty(xarr) then
  155.             technic.nodes[pos.x] = nil
  156.         end
  157.     else
  158.         print("[technic] Warning: The node at ("..pos.x.."|"..pos.y.."|"..pos.z..") wasn't stored in technic.nodes.")
  159.     end
  160. end
  161.  
  162. function technic.set_node(pos, name, meta)
  163.     if not (name or pos) then
  164.         return false
  165.     end
  166.     technic.complete_node_table(pos, name)
  167.     meta = meta or true
  168.     technic.nodes[pos.x][pos.y][pos.z][name] = meta
  169. end
  170.  
  171. minetest.register_node("ac:block9", {
  172.     description = "vartestblock",
  173.     tiles = {"ac_block.png"},
  174.     groups = {snappy=1,bendy=2,cracky=1},
  175.     sounds = default_stone_sounds,
  176.     on_construct = function(pos)
  177.         technic.set_node(pos, "LV", 3)
  178.         --print(technic.nodes_info())
  179.     end,
  180.     after_dig_node = function(pos)
  181.         technic.remove_node(pos)
  182.         --print(technic.nodes_info())
  183.     end
  184. })
  185.  
  186. minetest.register_chatcommand('cleantechnictable',{
  187.     description = 'Tidy up technic.nodes.',
  188.     params = "",
  189.     privs = {},
  190.     func = function(name)
  191.         technic.clean_node_table()
  192.         local tmp = technic.nodes_info()
  193.         minetest.chat_send_player(name, tmp)
  194.         print("[technic] "..tmp)
  195.     end
  196. })
  197.  
  198. technic.nodes_file["load"]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement