Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local num = ...
- assert(type(tonumber(num)) == "number", "Input first arg as number")
- num = tonumber(num)
- local cLog = {}
- local lg = "/.log.csv"
- if not fs.exists(lg) then
- local h = io.open(lg, 'w')
- if h then
- h:write("ITEM,DAMAGE,AMOUNT")
- h:close()
- else
- error("Failed to open file for writing.", 0)
- end
- end
- do
- local h = io.open(lg, 'r')
- dat = h:read("*a")
- h:close()
- local tmp = {}
- local tmp2 = {}
- --[[
- {
- "line 1",
- "Line 2",
- ...
- }
- ]]
- for line in dat:gmatch("[^\n]+") do
- table.insert(tmp, line)
- end
- --[[
- {
- {
- "word1",
- "word2",
- "word3"
- },
- {
- "minecraft:something",
- "0",
- "24"
- },
- {
- "derp",
- 2,
- 3
- 4
- },
- }
- ]]
- for i = 2, #tmp do
- tmp2[i - 1] = {}
- local x = 1
- for word in tmp[i]:gmatch("[^,]+") do
- tmp2[i - 1][x] = word
- x = x + 1
- end
- end
- -- actually serialized lua table.
- for i = 1, #tmp2 do
- if not cLog[tmp2[i][1]] then
- cLog[tmp2[i][1]] = {}
- end
- cLog[tmp2[i][1]][tonumber(tmp2[i][2])] = tonumber(tmp2[i][3])
- end
- end
- local function log(block, damage)
- local function csv(x)
- local tmp = {"ITEM,DAMAGE,AMOUNT"}
- local i = 2
- for item, v in pairs(x) do
- for damage, amount in pairs(v) do
- tmp[i] = item .. ',' .. tostring(damage) .. ',' .. tostring(amount)
- i = i + 1
- end
- end
- return table.concat(tmp, '\n')
- end
- if not cLog[block] then
- cLog[block] = {}
- end
- if not cLog[block][damage] then
- cLog[block][damage] = 0
- end
- cLog[block][damage] = cLog[block][damage] + 1
- fs.delete(lg)
- local h = io.open(lg, 'w')
- if h then
- h:write(csv(cLog))
- h:close()
- else
- error("Failed to open file for writing.", 0)
- end
- end
- local function dump()
- for i = 1, 16 do
- local a = turtle.getItemDetail(i)
- if a and a.name ~= "minecraft:stone" then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- end
- local function checkInv()
- local o = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- o = o + 1
- end
- end
- if o >= 14 then return true end
- return false
- end
- local function collapseInventory()
- -- TODO: this
- end
- local function findStone()
- for i = 1, 16 do
- local a = turtle.getItemDetail(i)
- if a and a.name == "minecraft:stone" and a.damage == 0 then
- return i
- end
- end
- error("No stone.")
- end
- local function checkStone()
- local a, b = turtle.inspect()
- if a and b.name == "minecraft:stone" and b.metadata == 0 then
- return true
- end
- return false, a and b.name, a and b.metadata
- end
- local function placeStone()
- turtle.select(findStone())
- turtle.place()
- rs.setOutput("right", true)
- os.sleep(0.1)
- rs.setOutput("right", false)
- os.sleep(1.9)
- end
- local function digStone()
- local a, b, m = checkStone()
- if not a then
- if not b or not m then
- b = "ENTITY_BLOCKING"
- m = 0
- end
- log(b, m)
- turtle.dig()
- else
- os.sleep(5)
- placeStone()
- digStone()
- end
- end
- local runMain = true
- local runSide = true
- local function main()
- local i = 1
- while i < num + 1 do
- if runMain then
- runSide = false
- placeStone()
- digStone()
- i = i + 1
- runSide = true
- os.sleep(0.5)
- else
- os.sleep(1)
- end
- end
- dump()
- end
- local function sideLoad()
- while true do
- os.sleep(0.7)
- if runSide and checkInv() then
- runMain = false
- dump()
- runMain = true
- end
- end
- end
- parallel.waitForAny(main, sideLoad)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement