Advertisement
guitarplayer616

Instructions2tsp

Jan 14th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. height,width,length = 16,15,8
  2. startx,starty,startz = 0,0,0
  3. finalx,finaly,finalz = 15,14,7
  4. shell.run("3opt")
  5.  
  6. blocks = textutils.unserialize( fs.open("blocks","r").readAll() )
  7. data = textutils.unserialize( fs.open("data","r").readAll() )
  8.  
  9. getBlockId = function(x,y,z)
  10.   return blocks[y + z*width + x*length*width + 1]
  11. end
  12.  
  13. getData = function(x,y,z)
  14.   return data[y + z*width + x*length*width + 1]
  15. end
  16.  
  17. --deconstruct
  18.  
  19.  
  20. instructions = textutils.unserialize( fs.open("instructions","r").readAll() )
  21.  
  22. local function instructions2layers(instructions)
  23.     local layers = {}
  24.     for x = startx,finalx do
  25.         layers[x] = {}
  26.         for n=1,#instructions do
  27.             if instructions[n][1] == x then
  28.                 table.insert(layers[x],{unpack(instructions[n],1,3)})
  29.             end
  30.         end
  31.     end
  32.     return layers
  33. end
  34.  
  35. function printNodes(nodes)
  36.     for i,v in pairs(nodes) do
  37.         textutils.pagedPrint("["..i.."] = {"..v[1]..","..v[2]..","..v[3].."}")
  38.     end
  39. end
  40.  
  41. function slowNodes(nodes)
  42.     for i,v in pairs(nodes) do
  43.         term.setCursorPos(v[2],v[3])
  44.         term.setBackgroundColor(2^getData(unpack(v)))
  45.         term.write(" ")
  46.         sleep(0)
  47.     end
  48. end
  49.  
  50. function slowSimulate(layers)
  51.     shell.run("clr")
  52.     for i = startx,finalx do
  53.         slowNodes(layers[i])
  54.         term.setBackgroundColor(colors.black)
  55.         shell.run("clr")
  56.     end
  57. end
  58.  
  59. function simulateIns(instructions)
  60.     local lastx = 0
  61.     shell.run("clr")
  62.     for n = 1,#instructions do
  63.         if lastx~=instructions[n][1] then
  64.             term.setBackgroundColor(colors.black)
  65.             shell.run("clr")
  66.         end
  67.         term.setCursorPos(instructions[n][2]+1,instructions[n][3]+1)
  68.         term.setBackgroundColor(2^instructions[n][5])
  69.         term.write(" ")
  70.         lastx = instructions[n][1]
  71.         sleep(0)
  72.     end
  73. end
  74.  
  75. --textutils.pagedPrint(textutils.serialize(layers))
  76. --printNodes(layers[0])
  77. local function organize(layers)
  78.     local startingPosition = {startx,starty,startz}
  79.     for x = startx,finalx do
  80.         table.insert(layers[x],1,startingPosition)
  81.         layers[x] = tsp_algorithm(layers[x])
  82.         startingPosition = layers[x][#layers[x]]
  83.         --startingPosition[1] = startingPosition[1] + 1
  84.     end
  85.     return layers
  86. end
  87.  
  88. local function layers2instructions(layers)
  89.     local instructions = {}
  90.     --organizedlayers only
  91.     for x = startx,finalx do
  92.         for i = 2,#layers[x] do
  93.             table.insert(instructions,layers[x][i])
  94.         end
  95.     end
  96.     return instructions
  97. end
  98.  
  99. local function add_id_and_data(instructions)
  100.     for n = 1,#instructions do
  101.         instructions[n][4] = getBlockId(unpack(instructions[n],1,3))
  102.         instructions[n][5] = getData(unpack(instructions[n],1,3))
  103.     end
  104.     return instructions
  105. end
  106.  
  107.  
  108. layers = instructions2layers(instructions)
  109. layers = organize(layers)
  110. new_instructions = layers2instructions(layers)
  111. new_instructions = add_id_and_data(new_instructions)
  112. --slowSimulate(layers)
  113. --printNodes(layers[1])
  114.  
  115. --textutils.pagedPrint(textutils.serialize(instructions))
  116. --for i,v in pairs(new_instructions) do
  117.     --textutils.pagedPrint(i..": "..v[1]..","..v[2]..","..v[3]..","..v[4]..","..v[5])  
  118. --end
  119. simulateIns(new_instructions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement