Advertisement
fatboychummy

stoneOreGenerator.lua

Aug 2nd, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. local num = ...
  2. assert(type(tonumber(num)) == "number", "Input first arg as number")
  3. num = tonumber(num)
  4. local cLog = {}
  5. local lg = "/.log.csv"
  6.  
  7.  
  8. if not fs.exists(lg) then
  9.   local h = io.open(lg, 'w')
  10.   if h then
  11.     h:write("ITEM,DAMAGE,AMOUNT")
  12.     h:close()
  13.   else
  14.     error("Failed to open file for writing.", 0)
  15.   end
  16. end
  17. do
  18.   local h = io.open(lg, 'r')
  19.   dat = h:read("*a")
  20.   h:close()
  21.  
  22.   local tmp = {}
  23.   local tmp2 = {}
  24.  
  25.   --[[
  26.     {
  27.       "line 1",
  28.       "Line 2",
  29.       ...
  30.     }
  31.  
  32.   ]]
  33.   for line in dat:gmatch("[^\n]+") do
  34.     table.insert(tmp, line)
  35.   end
  36.  
  37.  
  38.   --[[
  39.     {
  40.       {
  41.         "word1",
  42.         "word2",
  43.         "word3"
  44.       },
  45.       {
  46.         "minecraft:something",
  47.         "0",
  48.         "24"
  49.       },
  50.       {
  51.         "derp",
  52.         2,
  53.         3
  54.         4
  55.       },
  56.     }
  57.   ]]
  58.   for i = 2, #tmp do
  59.     tmp2[i - 1] = {}
  60.     local x = 1
  61.     for word in tmp[i]:gmatch("[^,]+") do
  62.       tmp2[i - 1][x] = word
  63.       x = x + 1
  64.     end
  65.   end
  66.  
  67.   -- actually serialized lua table.
  68.   for i = 1, #tmp2 do
  69.     if not cLog[tmp2[i][1]] then
  70.       cLog[tmp2[i][1]] = {}
  71.     end
  72.  
  73.     cLog[tmp2[i][1]][tonumber(tmp2[i][2])] = tonumber(tmp2[i][3])
  74.   end
  75.  
  76. end
  77.  
  78. local function log(block, damage)
  79.   local function csv(x)
  80.     local tmp = {"ITEM,DAMAGE,AMOUNT"}
  81.     local i = 2
  82.     for item, v in pairs(x) do
  83.       for damage, amount in pairs(v) do
  84.         tmp[i] = item .. ',' .. tostring(damage) .. ',' .. tostring(amount)
  85.         i = i + 1
  86.       end
  87.     end
  88.     return table.concat(tmp, '\n')
  89.   end
  90.  
  91.   if not cLog[block] then
  92.     cLog[block] = {}
  93.   end
  94.   if not cLog[block][damage] then
  95.     cLog[block][damage] = 0
  96.   end
  97.  
  98.   cLog[block][damage] = cLog[block][damage] + 1
  99.   fs.delete(lg)
  100.   local h = io.open(lg, 'w')
  101.   if h then
  102.     h:write(csv(cLog))
  103.     h:close()
  104.   else
  105.     error("Failed to open file for writing.", 0)
  106.   end
  107. end
  108.  
  109. local function dump()
  110.   for i = 1, 16 do
  111.     local a = turtle.getItemDetail(i)
  112.     if a and a.name ~= "minecraft:stone" then
  113.       turtle.select(i)
  114.       turtle.dropUp()
  115.     end
  116.   end
  117. end
  118.  
  119. local function checkInv()
  120.   local o = 0
  121.   for i = 1, 16 do
  122.     if turtle.getItemCount(i) > 0 then
  123.       o = o + 1
  124.     end
  125.   end
  126.   if o >= 14 then return true end
  127.   return false
  128. end
  129.  
  130. local function collapseInventory()
  131.   -- TODO: this
  132. end
  133.  
  134. local function findStone()
  135.   for i = 1, 16 do
  136.     local a = turtle.getItemDetail(i)
  137.     if a and a.name == "minecraft:stone" and a.damage == 0 then
  138.       return i
  139.     end
  140.   end
  141.   error("No stone.")
  142. end
  143.  
  144. local function checkStone()
  145.   local a, b = turtle.inspect()
  146.   if a and b.name == "minecraft:stone" and b.metadata == 0 then
  147.     return true
  148.   end
  149.   return false, a and b.name, a and b.metadata
  150. end
  151.  
  152. local function placeStone()
  153.   turtle.select(findStone())
  154.   turtle.place()
  155.   rs.setOutput("right", true)
  156.   os.sleep(0.1)
  157.   rs.setOutput("right", false)
  158.   os.sleep(1.9)
  159. end
  160.  
  161. local function digStone()
  162.   local a, b, m = checkStone()
  163.   if not a then
  164.     if not b or not m then
  165.       b = "ENTITY_BLOCKING"
  166.       m = 0
  167.     end
  168.     log(b, m)
  169.     turtle.dig()
  170.   else
  171.     os.sleep(5)
  172.     placeStone()
  173.     digStone()
  174.   end
  175. end
  176.  
  177.  
  178. local runMain = true
  179. local runSide = true
  180.  
  181. local function main()
  182.   local i = 1
  183.   while i < num + 1 do
  184.     if runMain then
  185.       runSide = false
  186.       placeStone()
  187.       digStone()
  188.       i = i + 1
  189.       runSide = true
  190.       os.sleep(0.5)
  191.     else
  192.       os.sleep(1)
  193.     end
  194.   end
  195.   dump()
  196. end
  197.  
  198. local function sideLoad()
  199.   while true do
  200.     os.sleep(0.7)
  201.     if runSide and checkInv() then
  202.       runMain = false
  203.       dump()
  204.       runMain = true
  205.     end
  206.   end
  207. end
  208.  
  209. parallel.waitForAny(main, sideLoad)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement