Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- function to check fuel and refuel if empty
- function checkAndRefuel()
- if turtle.getFuelLevel() == 0 then
- -- Select the slot with coal
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- if turtle.refuel(1) then
- local file = io.open("output.txt", "a")
- file:write("Refueled!\n")
- file:close()
- return true
- end
- end
- end
- local file = io.open("output.txt", "a")
- file:write("No coal found!\n")
- file:close()
- return false
- end
- return true
- end
- -- function to check if inventory is full
- function isInventoryFull()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- -- fixes inventory scattering
- function stackItems()
- -- remember seen items
- m = {}
- for i = 1, 16 do
- local this = turtle.getItemDetail(i)
- if this ~= nil then
- -- slot is not empty
- local saved = m[this.name .. (this.damage or "")]
- if saved ~= nil then
- -- we've seen this item before in the inventory
- local amount = this.count
- turtle.select(i)
- turtle.transferTo(saved.slot)
- if amount > saved.space then
- -- we have leftovers, and now the saved slot is full, so we replace it by the current one
- saved.slot = i
- saved.count = amount - saved.space
- -- update on table
- m[this.name .. (this.damage or "")] = saved
- elseif amount == saved.space then
- -- just delete the entry
- m[this.name .. (this.damage or "")] = nil
- end
- else
- -- there isn't another slot with this item so far, so sign this one up
- this.slot = i
- this.space = turtle.getItemSpace(i)
- m[this.name .. (this.damage or "")] = this
- end
- end
- end
- end
- -- selects an item if its in the inventory, otherwise returns false
- function selectItem(name)
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name == name then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- -- returns item count given item name
- function getItemCount(name)
- local count = 0
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name == name then
- count = count + data.count
- end
- end
- return count
- end
- -- gets direction
- -- 0 North, 1 East, 2 South, 3 West
- function getDirection()
- local old_x, old_y, old_z = gps.locate()
- -- determine direction we are facing
- if not checkAndRefuel() then
- return false
- end
- turtle.back()
- local new_x, new_y, new_z = gps.locate()
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- if new_x > old_x then
- -- facing west
- return true, 3
- elseif new_x < old_x then
- -- facing east
- return true, 1
- elseif new_z > old_z then
- -- facing north
- return true, 0
- end
- return true, 2
- end
- -- moves to position
- function moveTo(x, y, z)
- local cur_x, cur_y, cur_z = gps.locate()
- local dx = math.abs(x - cur_x)
- local dy = math.abs(y - cur_y)
- local dz = math.abs(z - cur_z)
- -- rotate towards south
- local direction_success, direction = getDirection()
- if not direction_success then
- return false
- end
- if direction == 3 then
- turtle.turnLeft()
- elseif direction == 1 then
- turtle.turnRight()
- elseif direction == 0 then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- now we are oriented towards south
- local file = io.open("output.txt", "a")
- file:write("Facing South Now\n")
- file:write("----------------\n")
- file:write("Current pos: (", cur_x, ", ", cur_y, ", ", cur_z, ")\n")
- file:write("Moving towards: (", x, ", ", y, ", ", z, ")\n")
- file:write("----------------\n")
- file:close()
- -- north -Z direction
- -- south +Z direction
- -- east +X direction
- -- west -X direction
- -- correct y first, then x and z
- local align_y_last = false
- if cur_y > y then
- align_y_last = true
- end
- if dy == 0 and not align_y_last then
- local file = io.open("output.txt", "a")
- file:write("Y aligned\n")
- file:close()
- elseif not align_y_last then
- local file = io.open("output.txt", "a")
- file:write("Correcting Y...\n")
- file:close()
- if cur_y < y then
- -- we need to head up
- for i = 1, dy do
- if not checkAndRefuel() then
- return false
- end
- turtle.up()
- end
- local file = io.open("output.txt", "a")
- file:write("Y aligned\n")
- file:close()
- else
- -- we need to head down
- for i = 1, dy do
- if not checkAndRefuel() then
- return false
- end
- turtle.down()
- end
- end
- end
- -- correct x
- if dx == 0 then
- local file = io.open("output.txt", "a")
- file:write("X aligned\n")
- file:close()
- else
- local file = io.open("output.txt", "a")
- file:write("Correcting X...\n")
- file:close()
- if cur_x < x then
- -- we need to head EAST
- local file = io.open("output.txt", "a")
- file:write("cur_x < x | rotating east\n")
- file:close()
- turtle.turnLeft()
- for i = 1, dx do
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- local file = io.open("output.txt", "a")
- file:write("rotating south\n")
- file:close()
- turtle.turnRight()
- else
- -- we need to head WEST
- local file = io.open("output.txt", "a")
- file:write("cur_x > x | rotating west\n")
- file:close()
- turtle.turnRight()
- for i = 1, dx do
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- local file = io.open("output.txt", "a")
- file:write("rotating south\n")
- file:close()
- turtle.turnLeft()
- end
- local file = io.open("output.txt", "a")
- file:write("X aligned\n")
- file:close()
- end
- -- correct z
- if dz == 0 then
- local file = io.open("output.txt", "a")
- file:write("Z aligned\n")
- file:close()
- else
- local file = io.open("output.txt", "a")
- file:write("Correcting Z...\n")
- file:close()
- if cur_z < z then
- -- we need to head south
- for i = 1, dz do
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- else
- local file = io.open("output.txt", "a")
- file:write("cur_z > z | rotating north\n")
- file:close()
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, dz do
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- local file = io.open("output.txt", "a")
- file:write("rotating south\n")
- file:close()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local file = io.open("output.txt", "a")
- file:write("Z aligned\n")
- file:close()
- end
- -- check if we align y last
- if dy == 0 and align_y_last then
- local file = io.open("output.txt", "a")
- file:write("Y aligned\n")
- file:close()
- elseif align_y_last then
- local file = io.open("output.txt", "a")
- file:write("Correcting Y...\n")
- file:close()
- if cur_y < y then
- -- we need to head up
- for i = 1, dy do
- if not checkAndRefuel() then
- return false
- end
- turtle.up()
- end
- local file = io.open("output.txt", "a")
- file:write("Y aligned\n")
- file:close()
- else
- -- we need to head down
- for i = 1, dy do
- if not checkAndRefuel() then
- return false
- end
- turtle.down()
- end
- end
- end
- cur_x, cur_y, cur_z = gps.locate()
- if cur_x ~= x or cur_y ~= y or cur_z ~= z then
- local file = io.open("output.txt", "a")
- file:write("Failed to move to (", x, ", ", y, ", ", z, ")\n")
- file:close()
- return false
- end
- return true
- end
- -- goes to storage chest, dumps inventory, returns back
- -- x y and z are the starting location of the quarry
- function storeItems(x, y, z)
- -- store current position and direction
- local cur_x, cur_y, cur_z = gps.locate()
- local direction_success, direction = getDirection()
- if not direction_success then
- return false
- end
- local file = io.open("output.txt", "a")
- file:write("Moving to Origin location!!!\n")
- file:close()
- -- move to origin of quarry
- if not moveTo(x, y, z) then
- return false
- end
- local file = io.open("output.txt", "a")
- file:write("Finished moving to Origin location!!!\n")
- file:close()
- -- rotate towards north
- turtle.turnLeft()
- turtle.turnLeft()
- local file = io.open("output.txt", "a")
- file:write("Checking for chest\n")
- file:close()
- -- now we are facing north towards our storage chest
- local inspect_data_success, inspect_data = turtle.inspect()
- if not inspect_data_success then
- local file = io.open("output.txt", "a")
- file:write("No block detected at starting point\n")
- file:close()
- return false
- end
- local file = io.open("output.txt", "a")
- file:write("CHEST DATA: {name=", inspect_data.name, "\n")
- file:close()
- if inspect_data.tags and inspect_data.tags["forge:chests"] then
- local file = io.open("output.txt", "a")
- file:write("Forge Chest detected. Storing items\n")
- file:close()
- stackItems()
- -- assume coal is in first item slot. transfer all other items to chest
- for i = 2, 16 do
- local item_data = turtle.getItemDetail(i)
- if item_data ~= nil then
- -- item slot is not empty, so we will select it and drop
- turtle.select(i)
- turtle.drop()
- end
- end
- else
- local file = io.open("output.txt", "a")
- file:write("Not a forge chest in front of the turtle.\n")
- file:close()
- return false
- end
- local file = io.open("output.txt", "a")
- file:write("Finished storing items\n")
- file:write("Moving to previous location\n")
- file:close()
- -- now we have emptied our inventory. now we need to return to (old_x, old_y, old_z) facing the direction we were previously facing
- if not moveTo(cur_x, cur_y, cur_z) then
- return false
- end
- local file = io.open("output.txt", "a")
- file:write("Re-orienting to old direction\n")
- file:close()
- -- facing south, rotate towards direction
- -- 0 North, 1 East, 2 South, 3 West
- if direction == 3 then
- turtle.turnRight()
- elseif direction == 1 then
- turtle.turnLeft()
- elseif direction == 0 then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local file = io.open("output.txt", "a")
- file:write("Finished with storeItems()\n")
- file:close()
- return true
- end
- -- mines a layer of the quarry
- -- x y and z are the starting location of the quarry
- function mine_layer(start_x, start_y, start_z)
- -- stackItems once per layer
- -- check if inventory is full after every block mined
- -- if inventory full then call storeItems
- stackItems()
- for x = 1, 16 do
- for y = 1, 15 do
- if isInventoryFull() then
- if not storeItems(start_x, start_y, start_z) then
- return false
- end
- end
- turtle.dig()
- if isInventoryFull() then
- if not storeItems(start_x, start_y, start_z) then
- return false
- end
- end
- turtle.digDown()
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- -- line finished so now we gotta dig down
- if isInventoryFull() then
- if not storeItems(start_x, start_y, start_z) then
- return false
- end
- end
- turtle.digDown()
- if x % 2 == 0 and x ~= 16 then
- turtle.turnLeft()
- if isInventoryFull() then
- if not storeItems(start_x, start_y, start_z) then
- return false
- end
- end
- turtle.dig()
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- turtle.turnLeft()
- else
- turtle.turnRight()
- if isInventoryFull() then
- if not storeItems(start_x, start_y, start_z) then
- return false
- end
- end
- turtle.dig()
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- turtle.turnRight()
- end
- -- ready to start new line
- end
- -- finished layer
- return true
- end
- -- main loop of quarry
- local start_x, start_y, start_z = gps.locate()
- local file = io.open("output.txt", "a")
- file:write("start_x = ", start_x, ", start_y = ", start_y, ", start_z = ", start_z, "\n")
- file:close()
- if start_x == nil or start_y == nil or start_z == nil then
- local file = io.open("output.txt", "a")
- file:write("GPS Offline\n")
- file:close()
- return
- end
- while 1 == 1 do
- if not mine_layer(start_x, start_y, start_z) then
- local file = io.open("output.txt", "a")
- file:write("Failed to mine layer. Either out of storage or fuel\n")
- file:close()
- return
- else
- local file = io.open("output.txt", "a")
- file:write("Mined layer\n")
- file:close()
- -- get ready for the next layer
- -- end layer facing south
- turtle.turnLeft()
- for i = 1, 15 do
- if not checkAndRefuel() then
- return false
- end
- turtle.forward()
- end
- turtle.turnRight()
- for i = 1, 2 do
- if not checkAndRefuel() then
- return false
- end
- turtle.down()
- turtle.digDown()
- end
- -- now we are ready to start the next layer
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement