Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Config:
- WIDTH, HEIGHT = CHANGE_ME, CHANGE_ME
- HOE, PICKAXE = 'left', 'right'
- HARVEST_TOOL = PICKAXE -- change to PICKAXE if available
- MIN_FUEL = WIDTH * HEIGHT * 3
- MIN_SLOTS = 0 -- 0 = ignore inv check
- FIELD_CYCLE_DELAY = 3*60 -- Delay after moving over complete field
- SOIL_BLOCKS = { 'minecraft:dirt', 'minecraft:grass', 'minecraft:end_stone' }
- -- Rice:
- --CROP_ITEM = 'harvestcraft:riceitem'
- --CROP_BLOCK = 'harvestcraft:pamricecrop'
- --CROP_BLOCK_SEASONED_METADATA = 3
- -- Carrots:
- --CROP_ITEM = 'minecraft:carrot'
- --CROP_BLOCK = 'minecraft:carrots'
- --CROP_BLOCK_SEASONED_METADATA = 7
- -- Beetroot:
- --CROP_ITEM = 'minecraft:beetroot_seeds'
- --CROP_BLOCK = 'minecraft:beetroots'
- --CROP_OTHER_ITEMS = { 'minecraft:beetroot' }
- --CROP_BLOCK_SEASONED_METADATA = 3
- -- Ender Lilly:
- CROP_ITEM = 'extrautils2:enderlilly'
- CROP_BLOCK = 'extrautils2:enderlilly'
- CROP_OTHER_ITEMS = { 'minecraft:ender_pearl' }
- CROP_BLOCK_SEASONED_METADATA = 7
- ------------
- t = turtle
- missingFields, seasonedFields = 0, 0
- function contains(list, element)
- i = 0
- while i < #list do
- i = i + 1
- if list[i] == element then
- return true
- end
- end
- return false
- end
- function countFreeSlots()
- freeSlots = 0
- slot = 1
- while slot <= 16 do
- if t.getItemCount(slot) == 0 then
- freeSlots = freeSlots + 1
- end
- slot = slot + 1
- end
- return freeSlots
- end
- function countCropItem()
- cropItems = 0
- slot = 1
- while slot <= 16 do
- if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
- cropItems = cropItems + t.getItemCount(slot)
- end
- slot = slot + 1
- end
- return cropItems
- end
- function fuelCheck()
- if not refuel() then
- print('Waiting for fuel...')
- while not refuel() do
- sleep(3)
- end
- end
- end
- function invCheck()
- if MIN_SLOTS > 0 and countFreeSlots() < MIN_SLOTS then
- print('Waiting for inv to be cleared.')
- while countFreeSlots() < MIN_SLOTS do
- sleep(3)
- end
- end
- end
- function attemptDeposit()
- keepAttempting, depositSucceeded = true, false
- while keepAttempting do
- i = 0
- while i < 4 do
- if not depositSucceeded then
- depositSucceeded = depositCropItems()
- end
- i = i + 1
- t.turnRight()
- end
- if depositSucceeded then
- keepAttempting = false
- elseif not detectDown() then
- t.down()
- elseif not depositSucceeded and detectDown() then
- keepAttempting = false
- end
- end
- end
- function depositCropItems()
- found, data = t.inspect()
- if not found then
- return false
- end
- if string.find(data['name'], 'chest') == nil then
- return false
- end
- cropItems = countCropItem()
- slot = 1
- while slot <= 16 do
- if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
- t.select(slot)
- if cropItems >= missingFields then
- count = math.min(cropItems - missingFields, t.getItemCount())
- if t.drop(count) then
- cropItems = cropItems - count
- else
- return false
- end
- end
- elseif not(CROP_OTHER_ITEMS == nil) and t.getItemCount(slot) > 0 and contains(CROP_OTHER_ITEMS, t.getItemDetail(slot)['name']) then
- t.select(slot)
- if not t.drop(t.getItemCount(slot)) then
- return false
- end
- end
- slot = slot + 1
- end
- return true
- end
- function refuel()
- if t.getFuelLevel() > MIN_FUEL then
- return true
- end
- slot = 1
- while slot <= 16 do
- t.select(slot)
- t.refuel()
- slot = slot + 1
- end
- return t.getFuelLevel() > MIN_FUEL
- end
- function detectDown()
- -- Also detects fluids
- found, _ = t.inspectDown()
- return found
- end
- function selectCropItem()
- slot = 16
- while slot >= 1 do
- if t.getItemCount(slot) > 0 and t.getItemDetail(slot)['name'] == CROP_ITEM then
- t.select(slot)
- return true
- end
- slot = slot - 1
- end
- --print('NO CROP ITEMS FOUND!')
- return false
- end
- function countAvailableCropItemSpace()
- availableSpace = 0
- slot = 1
- while slot <= 16 do
- if t.getItemCount(slot) == 0 then
- availableSpace = availableSpace + 64
- elseif t.getItemDetail(slot)['name'] == CROP_ITEM then
- availableSpace = availableSpace + t.getItemSpace(slot)
- end
- slot = slot + 1
- end
- return availableSpace
- end
- function harvestAndPlant(tool)
- t.select(1) -- Add correctly to inv
- plantCropItem = false
- if tool == PICKAXE then
- t.digDown(PICKAXE)
- -- Automatically picks up items
- plantCropItem = true
- elseif tool == HOE then
- if selectCropItem() then
- t.placeDown()
- else
- t.digDown(HOE)
- plantCropItem = true
- end
- -- Doesn't pick up items
- t.suckDown()
- sleep(0.25)
- t.suckDown()
- end
- if plantCropItem and selectCropItem() then
- t.placeDown()
- elseif plantCropItem then
- missingFields = missingFields + 1
- end
- end
- function careRow(length)
- pos = 0
- while pos < length do
- -- Get to ground:
- while not detectDown() do
- t.down()
- end
- -- Field:
- _, data = t.inspectDown()
- name = data['name']
- -- Place crop item on empty field:
- if contains(SOIL_BLOCKS, name) then
- if selectCropItem() then
- t.up()
- t.digDown(HOE)
- t.placeDown()
- else
- missingFields = missingFields + 1
- end
- end
- -- Check crop item field state:
- if name == CROP_BLOCK and data['metadata'] == CROP_BLOCK_SEASONED_METADATA then
- if (CROP_OTHER_ITEMS == nil and countAvailableCropItemSpace() >= 3) or countFreeSlots() > 0 then
- harvestAndPlant(HARVEST_TOOL)
- else
- seasonedFields = seasonedFields + 1
- end
- end
- -- Climb:
- while t.detect() and not t.detectUp() do
- t.up()
- end
- if not (pos == (length-1)) then
- while not t.forward() do
- sleep(1)
- end
- end
- pos = pos + 1
- end
- end
- f, belowData = t.inspectDown()
- if f and not (string.find(belowData['name'], 'chest') == nil) then
- t.forward()
- end
- xDir, yDir = 1, 1 -- 1 = forwards ; -1 = back
- col = 0
- x, y = 0, 0
- fuelCheck()
- invCheck()
- while true do
- careRow(HEIGHT)
- x = x + xDir
- y = y + yDir*(HEIGHT-1)
- yDir = yDir * -1
- --print('X: ' .. x .. ' ; Y: ' .. y)
- if x == WIDTH or x == 0 then
- attemptDeposit()
- t.turnRight()
- t.turnRight()
- xDir = xDir * -1
- if missingFields > 0 then
- print(missingFields .. ' crop items are needed for next cycle.')
- end
- if seasonedFields > 0 or (missingFields > 0 and selectCropItem()) then
- print('Work is not done, yet.')
- else
- print('Sleeping ' .. FIELD_CYCLE_DELAY .. ' seconds...')
- sleep(FIELD_CYCLE_DELAY)
- end
- seasonedFields = 0
- missingFields = 0
- fuelCheck()
- invCheck()
- elseif (xDir == 1 and x % 2 == 1 and not (y == 0)) or (xDir == -1 and x % 2 == 0 and y == 0) then
- -- Next row is the the right:
- t.turnRight()
- -- Climb:
- while t.detect() do
- t.up()
- end
- while not t.forward() do
- sleep(1)
- end
- t.turnRight()
- elseif (xDir == 1 and x % 2 == 0 and y == 0) or (yDir == -1 and x % 2 == 1 and not (y == 0)) then
- -- Next row is to the left:
- t.turnLeft()
- -- Climb:
- while t.detect() do
- t.up()
- end
- while not t.forward() do
- sleep(1)
- end
- t.turnLeft()
- else
- print('PANIC: UNEXPECTED CASE!!!')
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement