Advertisement
guerre10

barGraphs.lua

Apr 13th, 2023
1,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. slot= 1
  2. turtle.select(slot)
  3. --This function is placed before every place block to check if the turtle is out of inventory, if the first slot is empty it will swap to the next slot
  4. function autoswap()
  5.     while turtle.getItemCount(slot) == 0 do
  6.         slot = slot+ 1
  7.         if slot == 16 then print("Too much") break end
  8.         turtle.select(slot)
  9.     end
  10. end
  11.  
  12. --Scan takes an array and attempts to append the name of the material in front of it, and keep count of it. If the material already exists then it adds one to the count. The next two following functions do the same thing but scan up or down.
  13.  
  14. function scan(n)
  15.     print("hey I'm starting")
  16.     x, y= turtle.inspect()
  17.     material= y.name
  18.     for i=1, #(n[1]) do
  19.         if n[1][i] == material then
  20.             n[2][i]= n[2][i]+1
  21.             print("old material")
  22.             return n
  23.         end
  24.     end
  25.     tempEnd= #(n[1])+1
  26.     n[1][tempEnd]= material
  27.     n[2][tempEnd]= 1
  28.     print("new material")
  29.     return n
  30. end
  31.  
  32. function scanUp(n)
  33.     print("hey I'm starting")
  34.     x, y= turtle.inspectUp()
  35.     material= y.name
  36.     for i=1, #(n[1]) do
  37.         if n[1][i] == material then
  38.             n[2][i]= n[2][i]+1
  39.             print("old material")
  40.             return n
  41.         end
  42.     end
  43.     tempEnd= #(n[1])+1
  44.     n[1][tempEnd]= material
  45.     n[2][tempEnd]= 1
  46.     print("new material")
  47.     return n
  48. end
  49.  
  50. function scanDown(n)
  51.     print("hey I'm starting")
  52.     x, y= turtle.inspectDown()
  53.     material= y.name
  54.     for i=1, #(n[1]) do
  55.         if n[1][i] == material then
  56.             n[2][i]= n[2][i]+1
  57.             print("old material")
  58.             return n
  59.         end
  60.     end
  61.     tempEnd= #(n[1])+1
  62.     n[1][tempEnd]= material
  63.     n[2][tempEnd]= 1
  64.     print("new material")
  65.     return n
  66. end
  67.  
  68. --loops through a given array and prints out the name of the material, and the associated count
  69.  
  70. function display(n)
  71.     print(" ")
  72.     for i=1, #(n[1]) do
  73.    
  74.         print(n[1][i] .. "  ".. n[2][i])
  75.     end
  76. end    
  77.  
  78. --Must be used in order to save your array for later
  79. function saveCount(n, name)
  80.     file= io.open(name, "w")
  81.     for i=1, #(n[1]) do
  82.         file:write(n[1][i].. "\n".. n[2][i].. "\n")
  83.     end
  84.     file:close()
  85. end
  86.  
  87. --Loads another file as an array
  88. function loadCount(dataName)
  89.     file= io.open(dataName, "r")
  90.     local lineNumber= 1
  91.     local mats= {}
  92.     local matCounts= {}
  93.     local count= 1
  94.     for line in file:lines() do
  95.         if lineNumber%2 == 1 then
  96.             mats[count]= line
  97.         else
  98.             matCounts[count]= tonumber(line)
  99.             count= count +1
  100.            
  101.         end
  102.         lineNumber= lineNumber+1
  103.     end
  104.    return {mats, matCounts}    
  105.    
  106. end    
  107.  
  108. function barGraph(input)
  109.     -- Find the max height and make that the height of the bar graph then reset to original spot
  110.     max= 1
  111.     for i=1, #(input[2]) do
  112.         if max< input[2][i] then max= input[2][i] end
  113.     end
  114.     autoswap()
  115.     turtle.place()
  116.     for i=1, max do
  117.         turtle.up()
  118.         autoswap()
  119.         turtle.place()
  120.     end
  121.     while not turtle.detectDown() do turtle.down() end
  122.     --Position self to place directly down and do movement make horizontal axis
  123.     turtle.turnRight()
  124.     turtle.forward()
  125.     turtle.turnLeft()
  126.     turtle.up()
  127.     turtle.forward()
  128.     turtle.turnRight()
  129.     for i=1, #(input[2])*2 do
  130.         autoswap()
  131.         turtle.placeDown()
  132.         turtle.forward()
  133.     end
  134.     --Reset position and be ready to place bars
  135.     for i=1, #(input[2])*2 do turtle.back() end
  136.     turtle.turnRight()
  137.     turtle.forward()
  138.     turtle.down()
  139.     turtle.turnLeft()
  140.     turtle.forward()
  141.     turtle.turnLeft()
  142.    
  143.     --bar graphs
  144.     for i=1, #(input[2]) do
  145.         for height= 1, input[2][i] do
  146.             turtle.up()
  147.             autoswap()
  148.             turtle.place()
  149.         end
  150.         while not turtle.detectDown() do turtle.down() end
  151.         turtle.back()
  152.         turtle.select(16)
  153.   material= input[1][i]
  154.   material= string.sub(material,11,-1)
  155.      turtle.place(material)
  156.         turtle.select(slot)
  157.         turtle.turnRight()
  158.         turtle.forward()
  159.         turtle.forward()
  160.         turtle.turnLeft()
  161.         turtle.forward()
  162.     end
  163. end
  164.  
  165. list = loadCount(myData)
  166. barGraph(list)
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement