Advertisement
guitarplayer616

Demo Fastest Chicken

Jan 14th, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 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. local tArgs = {...}
  5.  
  6. if #tArgs == 0 then
  7.     print("normal: run vanilla version")
  8.     print("distance: run with distance formula")
  9.     error("pick an arg")
  10. end
  11.  
  12. function get(name,address)
  13.     local response = nil
  14.     local tmp = fs.open(name,"w")
  15.     local function setColor(color)
  16.         if term.isColor() then
  17.             term.setTextColor(color)
  18.         end
  19.     end
  20.     setColor(colors.yellow)
  21.     if address:sub(1,4) == "http" then
  22.         write("Connecting to Github.com... ")
  23.         response = http.get(address)
  24.     else
  25.         write("Connecting to Pastebin.com... ")
  26.         response = http.get("http://pastebin.com/raw/"..tostring(address))
  27.     end
  28.     if response then
  29.         setColor(colors.green)
  30.         print("Success")
  31.         tmp.write( response.readAll() )
  32.         tmp.close()
  33.         setColor(colors.white)
  34.         return true
  35.     else
  36.         setColor(colors.red)
  37.         print("Failed")
  38.         setColor(colors.white)
  39.         return false
  40.     end
  41. end
  42.  
  43.  
  44.  
  45. get("3opt","Lh9TZzTw")
  46. get("textutilsFIX","3wguFBXn")
  47. get("instructions","txAUaKmw")
  48.  
  49. shell.run("3opt")
  50. os.loadAPI("textutilsFIX")
  51.  
  52. instructions = textutilsFIX.unserialize( fs.open("instructions","r").readAll() )
  53.  
  54. function calculateDistance(tNode1,tNode2)
  55.     local turnCost = 0
  56.     local deltaY,deltaZ
  57.     local y1 = tNode1[2]
  58.     local z1 = tNode1[3]
  59.     local y2 = tNode2[2]
  60.     local z2 = tNode2[3]
  61.    
  62.     deltaZ = z2-z1
  63.     deltaY = y2-y1
  64.    
  65.     if deltaZ == 0 or deltaY == 0 then
  66.         turnCost = 0
  67.     else
  68.         turnCost = 1
  69.     end
  70.    
  71.     if tArgs[1] == "distance" then
  72.         return math.sqrt( (z2-z1)^2 + (y2-y1)^2 )
  73.     else
  74.         return math.abs(deltaZ) + math.abs(deltaY) + turnCost
  75.     end
  76. end
  77.  
  78. getBlockId = function(x,y,z)
  79.   for n = 1,#instructions do
  80.     local xyz = instructions[n]
  81.     if xyz[1] == x and xyz[2] == y and xyz[3] == z then
  82.         return xyz[4]
  83.     end
  84.   end
  85. end
  86.  
  87. getData = function(x,y,z)
  88.   for n = 1,#instructions do
  89.     local xyz = instructions[n]
  90.     if xyz[1] == x and xyz[2] == y and xyz[3] == z then
  91.         return xyz[5]
  92.     end
  93.   end
  94. end
  95.  
  96. --deconstruct
  97.  
  98. local function instructions2layers(instructions)
  99.     local layers = {}
  100.     for x = startx,finalx do
  101.         layers[x] = {}
  102.         for n=1,#instructions do
  103.             if instructions[n][1] == x then
  104.                 table.insert(layers[x],{unpack(instructions[n],1,3)})
  105.             end
  106.         end
  107.     end
  108.     return layers
  109. end
  110.  
  111. function printNodes(nodes)
  112.     for i,v in pairs(nodes) do
  113.         textutils.pagedPrint("["..i.."] = {"..v[1]..","..v[2]..","..v[3].."}")
  114.     end
  115. end
  116.  
  117. function slowNodes(nodes)
  118.     for i,v in pairs(nodes) do
  119.         term.setCursorPos(v[2],v[3])
  120.         term.setBackgroundColor(2^getData(unpack(v)))
  121.         term.write(" ")
  122.         sleep(0)
  123.     end
  124. end
  125.  
  126. function slowSimulate(layers)
  127.     shell.run("clr")
  128.     for i = startx,finalx do
  129.         slowNodes(layers[i])
  130.         term.setBackgroundColor(colors.black)
  131.         shell.run("clr")
  132.     end
  133. end
  134.  
  135. function simulateIns(instructions)
  136.     local lastx = 0
  137.     shell.run("clr")
  138.     for n = 1,#instructions do
  139.         if lastx~=instructions[n][1] then
  140.             term.setBackgroundColor(colors.black)
  141.             shell.run("clr")
  142.         end
  143.         term.setCursorPos(instructions[n][2]+1,instructions[n][3]+1)
  144.         term.setBackgroundColor(2^instructions[n][5])
  145.         term.write(" ")
  146.         lastx = instructions[n][1]
  147.         sleep(0)
  148.     end
  149. end
  150.  
  151. --textutils.pagedPrint(textutils.serialize(layers))
  152. --printNodes(layers[0])
  153. local function organize(layers)
  154.     local startingPosition = {startx,starty,startz}
  155.     for x = startx,finalx do
  156.         table.insert(layers[x],1,startingPosition)
  157.         layers[x] = tsp_algorithm(layers[x])
  158.         startingPosition = layers[x][#layers[x]]
  159.         --startingPosition[1] = startingPosition[1] + 1
  160.     end
  161.     return layers
  162. end
  163.  
  164. local function layers2instructions(layers)
  165.     local instructions = {}
  166.     --organizedlayers only
  167.     for x = startx,finalx do
  168.         for i = 2,#layers[x] do
  169.             table.insert(instructions,layers[x][i])
  170.         end
  171.     end
  172.     return instructions
  173. end
  174.  
  175. local function add_id_and_data(instructions)
  176.     for n = 1,#instructions do
  177.         instructions[n][4] = getBlockId(unpack(instructions[n],1,3))
  178.         instructions[n][5] = getData(unpack(instructions[n],1,3))
  179.     end
  180.     return instructions
  181. end
  182.  
  183.  
  184. layers = instructions2layers(instructions)
  185. layers = organize(layers)
  186. new_instructions = layers2instructions(layers)
  187. new_instructions = add_id_and_data(new_instructions)
  188. --slowSimulate(layers)
  189. --printNodes(layers[1])
  190.  
  191. --textutils.pagedPrint(textutils.serialize(new_instructions))
  192. --for i,v in pairs(new_instructions) do
  193.     --textutils.pagedPrint(i..": "..v[1]..","..v[2]..","..v[3]..","..v[4]..","..v[5])  
  194. --end
  195. simulateIns(new_instructions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement