Advertisement
nintendowfi

Text output and hrly coal production 3.1

Aug 7th, 2022 (edited)
1,661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rednet.open("left")
  2. mon = peripheral.wrap("right")
  3.  
  4. local total_counted = 0
  5. local counted_hour = 0
  6. local old_total_counted = 0
  7. local lst_of_counted = {}
  8. local lst_for_graph = {}
  9. local cycle = 0
  10.  
  11.  
  12. function sum(t)
  13.     local sum = 0
  14.     for k,v in pairs(t) do
  15.         sum = sum + v
  16.     end
  17.     return sum
  18. end
  19.  
  20. function what_length(t)
  21.     local lengthNum = 0
  22.  
  23.     for k, v in pairs(t) do -- for every key in the table with a corresponding non-nil value
  24.         lengthNum = lengthNum + 1
  25.     end
  26.     return lengthNum
  27. end
  28.  
  29. function draw_square(i,j) --i,j are the cursor pos where the block is supposed to be placed.
  30.     i = tonumber(i)
  31.     j = tonumber(j)
  32.     mon.setBackgroundColor(colors.green)
  33.     mon.setCursorPos(i,j)
  34.     mon.write(" ")
  35.     mon.setBackgroundColor(colors.black) end --removed the return
  36.  
  37. function draw_graph(k) --k should be lst_of_counted
  38.     local counter = 0
  39.     local i = 1
  40.     counter = what_length(lst_for_graph) + 1
  41.     for x=1,50,1 do
  42.         counter = counter - 1
  43.         i = i + 1
  44.         if i > 49 then
  45.            i = 49 end
  46.         if k[counter] == nil then
  47.             return
  48.         elseif counter == 0 then
  49.             return
  50.         elseif k[counter] > 125 then
  51.             for a=18,10,-1 do --draw a 8 block high green bar
  52.                 draw_square(i,a)
  53.                 sleep(0.1) end
  54.         elseif k[counter] > 109 then
  55.             for a=18,11,-1 do --draw a 7 block high green bar
  56.                 draw_square(i,a)
  57.                 sleep(0.1) end
  58.         elseif k[counter] > 93 then
  59.             for a=18,12,-1 do --draw a 6 block high green bar
  60.                 draw_square(i,a)
  61.                 sleep(0.1) end
  62.         elseif k[counter] > 78 then
  63.             for a=18,13,-1 do --draw a 5 block high green bar
  64.                 draw_square(i,a)
  65.                 sleep(0.1) end
  66.         elseif k[counter] > 62 then
  67.             for a=18,14,-1 do --draw a 4 block high green bar
  68.                 draw_square(i,a)
  69.                 sleep(0.1) end
  70.         elseif k[counter] > 46 then
  71.             for a=18,15,-1 do --draw a 3 block high green bar
  72.                 draw_square(i,a)
  73.                 sleep(0.1) end
  74.         elseif k[counter] > 31 then
  75.             for a=18,16,-1 do --draw a 2 block high green bar
  76.                 draw_square(i,a)
  77.                 sleep(0.1) end
  78.         elseif k[counter] < 32 then
  79.             for a=18,17,-1 do --draw a 1 block high green bar
  80.                 draw_square(i,a)
  81.                 sleep(0.1) end
  82.              end
  83.     end
  84. end
  85.  
  86. while true do
  87.    
  88.     local currently_counted = 0
  89.    
  90.     x,y,z = rednet.receive()
  91.    
  92.     old_total_counted = total_counted
  93.     cycle = cycle+1
  94.     total_counted = y
  95.     currently_counted = y - old_total_counted
  96.     table.insert(lst_of_counted,currently_counted)
  97.     table.insert(lst_for_graph,currently_counted)
  98.    
  99.     if cycle > 20 then
  100.         table.remove(lst_of_counted, 1)
  101.         --delete last item of table and then sum together all
  102.         counted_hour = 0
  103.         counted_hour = sum(lst_of_counted)
  104.         end
  105.    
  106.     if cycle > 50 then
  107.         table.remove(lst_for_graph, 1)
  108.         end
  109.    
  110.     total_counted = tostring(tonumber(total_counted))
  111.     counted_hour = tostring(tonumber(counted_hour))
  112.    
  113.     mon.clear()
  114.     draw_graph(lst_for_graph)
  115.     mon.setTextColor(colors.white)
  116.     --Mark the graph
  117.     mon.setCursorPos(1,9)
  118.     mon.write("130")
  119.     mon.setCursorPos(2,19)
  120.     mon.write("3")
  121.     mon.setCursorPos(10.5,19)
  122.     mon.write("36")
  123.     mon.setCursorPos(24,19)
  124.     mon.write("72")
  125.     mon.setCursorPos(37.5,19)
  126.     mon.write("108")
  127.     mon.setCursorPos(48,19)
  128.     mon.write("144")
  129.     sleep(0.1)
  130.     mon.setCursorPos(1,1)
  131.     mon.write("+------------------------------------------------+")
  132.     mon.setCursorPos(1,3)
  133.     mon.write("We have produced a total of ")
  134.     mon.write(total_counted)
  135.     mon.write(" pieces of coal!")
  136.     mon.setCursorPos(1,5)
  137.     mon.write(total_counted)
  138.     mon.write(" makes ")
  139.     mon.write(tostring(tonumber(math.floor(total_counted/64))))
  140.     mon.write(" stacks of coal! ")
  141.     if cycle > 10 then
  142.         mon.setCursorPos(1,7)
  143.         mon.write("Last hour ")
  144.         mon.write(counted_hour)
  145.         mon.write(" pieces of coal were produced!")
  146.         mon.setCursorPos(1,20)
  147.         mon.write("+------------------------------------------------+")
  148.     else
  149.         mon.setCursorPos(1,7)
  150.         mon.write("Last hour [N/A] pieces of coal were produced!")
  151.         mon.setCursorPos(1,20)
  152.         mon.write("+------------------------------------------------+")
  153.     end
  154. end
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement