Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --VARS
- farm_length = 5;
- farm_width = 5;
- turtle_slot_saplings_1 = 1
- turtle_slot_saplings_2 = 2
- turtle_slot_dirt_1 = 3
- turtle_slot_dirt_2 = 4
- --COMMON FUNCTIONS
- function printMsg(content)
- print('[TreeBot] ' .. content)
- end
- function exit(msg)
- error(msg)
- return
- end
- -- COMMON TURTLE FUNCTIONS
- function selectSlot(slot)
- turtle_slot_current = slot
- turtle.select(slot)
- end
- function findSaplingSlot()
- if ((turtle.getItemCount(turtle_slot_saplings_1)) > 0) then
- selectSlot(turtle_slot_saplings_1)
- elseif ((turtle.getItemCount(turtle_slot_saplings_2)) > 0) then
- selectSlot(turtle_slot_saplings_2)
- else
- exit('No saplings available')
- end
- end
- function findDirtSlot()
- if ((turtle.getItemCount(turtle_slot_dirt_1)) > 0) then
- selectSlot(turtle_slot_dirt_1)
- elseif ((turtle.getItemCount(turtle_slot_dirt_2)) > 0) then
- selectSlot(turtle_slot_dirt_2)
- else
- exit('No dirt available')
- end
- end
- function moveDig(direction, times)
- for i = 1, times do
- if (direction == 'forward') then
- while(turtle.forward() ~= true) do
- while (turtle.detect()) do
- turtle.dig()
- end
- end
- elseif (direction == 'up') then
- while(turtle.up() ~= true) do
- while (turtle.detectUp()) do
- turtle.digUp()
- end
- end
- elseif (direction == 'down') then
- while(turtle.down() ~= true) do
- while (turtle.detectDown()) do
- turtle.digDown()
- end
- end
- end
- end
- end
- function rotate(direction, times)
- for i = 1, times do
- if (direction == 'left') then
- turtle.turnLeft()
- elseif (direction == 'right') then
- turtle.turnRight()
- end
- end
- end
- --SINGLE FUNCTIONS
- function checkCurrentSpot()
- if (turtle.detect()) then
- printMsg('Found tree, trying to remove')
- newHeight = 0
- while (turtle.detect()) do
- moveDig('up', 1)
- newHeight = newHeight + 1
- end
- moveDig('forward', 1)
- for i = 1, (newHeight + 1) do
- moveDig('down', 1)
- end
- moveDig('up', 1)
- findSaplingSlot()
- turtle.placeDown()
- printMsg('Tree removed')
- return true
- else
- return false
- end
- end
- -- FUNCTIONALITY FUNCTIONS
- function checkFarm()
- --bot is at init position
- currWidth = 0
- while (currWidth < farm_width) do
- currWidth = currWidth + 1
- for currLength = 1, farm_length do
- printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
- removeResult = checkCurrentSpot()
- if (removeResult ~= true) then
- moveDig('forward', 1)
- tile_saplingIsPlanted = turtle.detectDown()
- if (tile_saplingIsPlanted ~= true) then
- --check wether dirt exists
- moveDig('down', 1)
- tile_dirtIsPlanted = turtle.detectDown()
- if (tile_dirtIsPlanted ~= true) then
- findDirtSlot()
- turtle.placeDown()
- end
- moveDig('up', 1)
- findSaplingSlot()
- turtle.placeDown()
- end
- moveDig('forward', 3)
- else
- moveDig('forward', 3)
- end
- --check if it should return
- if (currLength == farm_length) then
- moveDig('forward', 1)
- rotate('left', 1)
- moveDig('forward', 4)
- rotate('left', 1)
- moveDig('forward', 3)
- currWidth = currWidth + 1
- -- check if operation is complete
- if (currWidth < farm_width) then
- for backLength = 1, (farm_length) do
- --process on the way home
- printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
- removeResult = checkCurrentSpot()
- if (removeResult ~= true) then
- moveDig('forward', 1)
- tile_saplingIsPlanted = turtle.detectDown()
- if (tile_saplingIsPlanted ~= true) then
- --check wether dirt exists
- moveDig('down', 1)
- tile_dirtIsPlanted = turtle.detectDown()
- if (tile_dirtIsPlanted ~= true) then
- findDirtSlot()
- turtle.placeDown()
- end
- moveDig('up', 1)
- findSaplingSlot()
- turtle.placeDown()
- end
- moveDig('forward', 3)
- else
- moveDig('forward', 3)
- end
- end
- rotate('right', 1)
- moveDig('forward', 4)
- rotate('right', 1)
- moveDig('forward', 2)
- else
- --operation is complete
- moveDig('forward', (farm_length * 4))
- rotate('left', 1)
- moveDig('forward', (farm_width * 4)) -- line up with last col
- rotate('left', 1)
- moveDig('forward', 2) --now at original position
- end
- end
- end
- end
- printMsg('End of journey. Returning to original position')
- end
- checkFarm();
Advertisement
Add Comment
Please, Sign In to add comment