Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local isCalibrated = false
- local isFacingNormal = true
- local isRightward = false --Which way from the start do the crops go
- local fuelNeeded = 80 --Starts defined for calibration
- local timer = 45 --Seconds
- local height = 0
- local width = 1
- --turtle.inspect [bool, content[name, metadata], error] name=thaumicbases:lazullia
- function Calibrate(crop)
- if Refuel(fuelNeeded) == false then
- turtle.turnLeft()
- if RestockFuel(fuelNeeded) == true then
- Refuel(fuelNeeded)
- turtle.turnRight()
- else
- print(math.ceil(((fuelNeeded - turtle.getFuelLevel()) / 80)), ' coal required for calibration.')
- turtle.turnRight()
- return
- end
- end
- print('Calibrating...')
- --Find height
- turtle.up()
- turtle.forward()
- while true do
- local inspected = {turtle.inspectDown()}
- if inspected[1] ~= false and string.find(inspected[2].name, crop) then
- height = height + 1
- turtle.forward()
- else
- break
- end
- end
- --Position turtle for width
- turtle.back()
- turtle.turnLeft()
- turtle.forward()
- local inspected = {turtle.inspectDown()}
- if inspected[1] == true and string.find(inspected[2].name, crop) == true then
- isRightward = false
- else
- isRightward = true
- turtle.back()
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- end
- --Find width
- while true do
- local inspected = {turtle.inspectDown()}
- if inspected[1] ~= false and string.find(inspected[2].name, crop) then
- width = width + 1
- turtle.forward()
- else
- break
- end
- end
- --Return
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, width, 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- for i = 1, height, 1 do
- turtle.forward()
- end
- turtle.turnRight()
- turtle.turnRight()
- turtle.down()
- --Get required fuel
- if math.floor(math.fmod(width,2)) == (width/2) then
- fuelNeeded = height*width + width --if even
- else
- fuelNeeded = height*width + height+width --if odd
- end
- --Display info
- print('Calibration completed!\n')
- print('Height: ', height)
- print('Width: ', width)
- print('Expected ', crop, ': ', height*width)
- print('Charcoal per run: ', fuelNeeded/80)
- isCalibrated = true
- end
- function Refuel(RequiredFuel)
- for i = 1, 16, 1 do
- if turtle.getFuelLevel() >= RequiredFuel then --When refueled
- print('Fuel checked')
- return true
- elseif turtle.getItemDetail(i) and turtle.getItemDetail(i).name == "minecraft:coal" then --Continue refill
- turtle.select(i)
- turtle.refuel(math.ceil(((RequiredFuel - turtle.getFuelLevel()) / 80)))
- if turtle.getFuelLevel() >= RequiredFuel then --When refueled
- print('Fuel checked')
- return true
- end
- end
- end
- if turtle.getFuelLevel() < RequiredFuel then --When failed
- print('Not enough fuel')
- return false
- end
- end
- function Deposit(itemName)
- local count = 0
- for i = 1, 16, 1 do
- turtle.select(i)
- local selectedDetail = turtle.getItemDetail()
- if selectedDetail and string.find(selectedDetail.name, itemName) then
- turtle.drop()
- count = count + selectedDetail.count
- end
- end
- print(count, itemName, 'deposited.')
- end
- function Harvest(maxGrowth, seedName)
- local state, contents, err = turtle.inspectDown()
- if contents.metadata == nil or contents.metadata < maxGrowth then --Skip ungrown crop
- return
- elseif contents.metadata >= maxGrowth then --Harvest grown crop
- turtle.digDown()
- for i = 1, 16, 1 do
- local selectedDetail = turtle.getItemDetail(i)
- if selectedDetail and string.find(selectedDetail.name, seedName) then --Replant seed
- turtle.select(i)
- turtle.placeDown()
- turtle.select(1)
- return
- end
- end
- end
- end
- function Idle(Timer)
- os.startTimer(Timer) --45 sec
- local evt = {os.pullEvent()}
- if evt[1] == 'timer' and Refuel(fuelNeeded) then --On timer and fueled
- isFacingNormal = true
- --Display
- --term.clear()
- --term.setCursorPos(1,1)
- return false
- elseif evt[1] == 'timer' then --On timer, fuel required
- turtle.turnLeft()
- if RestockFuel(fuelNeeded) then --Restock fuel
- turtle.turnRight()
- if Refuel(fuelNeeded) then --Refuel
- return false
- end
- else --Restock failure
- turtle.turnRight()
- return true
- end
- else
- return true
- end
- end
- function RestockFuel(RequiredFuel) --TODO: simplify this shit
- print('Restocking fuel...')
- local coalSlot
- for i = 1, 16, 1 do
- local detail = turtle.getItemDetail(i)
- if (detail ~= nil and detail.name == "minecraft:coal") then
- coalSlot = i
- break
- elseif detail == nil then
- coalSlot = i
- break
- end
- end
- for i = 1, 16, 1 do
- turtle.select(i)
- if (turtle.getItemDetail() ~= nil) and (turtle.getItemDetail().name == "minecraft:coal") then
- turtle.transferTo(coalSlot)
- end
- end
- turtle.select(coalSlot)
- turtle.suck(turtle.getItemSpace())
- if turtle.getItemCount(coalSlot) >= math.ceil(((RequiredFuel - turtle.getFuelLevel()) / 80)) then
- print('Restocking successful')
- return true
- else
- print('Restocking failed!')
- return false
- end
- end
- function ReturnHome()
- if isFacingNormal and isRightward then --O:1
- print('Running O:1')
- turtle.turnLeft()
- for i = 1, width-1, 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- for i = 1, height, 1 do
- turtle.forward()
- end
- turtle.down()
- elseif isFacingNormal and isRightward == false then --O:2
- print('Running O:2')
- turtle.turnRight()
- for i = 1, width-1, 1 do
- turtle.forward()
- end
- turtle.turnRight()
- for i = 1, height, 1 do
- turtle.forward()
- end
- turtle.down()
- elseif isFacingNormal == false and isRightward then --O:3
- print('Running O:3')
- turtle.turnRight()
- for i = 1, width-1, 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- turtle.forward()
- turtle.down()
- elseif isFacingNormal == false and isRightward == false then --O:4
- print('Running O:4')
- turtle.turnLeft()
- for i = 1, width-1, 1 do
- turtle.forward()
- end
- turtle.turnRight()
- turtle.forward()
- turtle.down()
- end
- end
- function Rows(seedName)
- for i = 1, width, 1 do
- for i = 1, height-1, 1 do
- turtle.forward()
- Harvest(7, seedName)
- end
- if i < width then
- if isFacingNormal and isRightward then --Turn right
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- isFacingNormal = not isFacingNormal
- print('Normal: ', isFacingNormal)
- Harvest(7, seedName)
- elseif isFacingNormal and isRightward == false then --Turn left
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- isFacingNormal = not isFacingNormal
- print('Normal: ', isFacingNormal)
- Harvest(7, seedName)
- elseif isFacingNormal == false and isRightward then --Turn left
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- isFacingNormal = not isFacingNormal
- print('Normal: ', isFacingNormal)
- Harvest(7, seedName)
- elseif isFacingNormal == false and isRightward == false then --Turn right
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- isFacingNormal = not isFacingNormal
- print('Normal: ', isFacingNormal)
- Harvest(7, seedName)
- end
- end
- end
- end
- --Main
- while true do
- if not isCalibrated then --Calibrate
- Calibrate('lazullia')
- if not isCalibrated then
- sleep(timer)
- end
- elseif Idle(timer) == false and isCalibrated then --Harvest crops after timer
- --This is a fucking mess
- --This looping rapidly, Idle(timer) == false not yet tested
- print('Harvesting...')
- turtle.up()
- turtle.forward()
- Harvest(7, seedName)
- Rows("seed")
- ReturnHome()
- Deposit("dye")
- turtle.turnRight()
- turtle.turnRight()
- print('Harvest completed.')
- end
- end
Add Comment
Please, Sign In to add comment