Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local S_WOOD = 1
- local S_SAPLING = 2
- local S_MEAL = 3
- local S_DIRT = 15
- local S_CCOAL = 16
- local OS_VERSION = "CraftOS 1.6"
- local sleep_time = 120
- local qty_wood = 0
- local count_checked = 0
- local t = turtle
- function suckAllSapling()
- t.select(S_SAPLING)
- t.suck()
- t.suckUp()
- t.suckDown()
- end
- function waitForEnoughItems(itemName, n, slot)
- t.select(slot)
- while t.getItemCount(slot) < n do
- if itemName then print(string.format("Insert %s into slot %d",itemName,slot)) end
- os.sleep(0.5)
- os.pullEvent("turtle_inventory")
- end
- end
- function waitForEnoughFuel(minLevel, slot)
- local qty = t.getFuelLevel()
- local refuel = function()
- if qty > minLevel then return qty end
- t.select(slot)
- t.refuel()
- qty = t.getFuelLevel()
- print("fuel: ",qty)
- return qty
- end
- while refuel() < minLevel do
- print(string.format("Insert fuel-items into slot %d: %d/%d", slot, qty, minLevel))
- os.sleep(1)
- os.pullEvent("turtle_inventory")
- end
- end
- function sel(slot, func)
- t.select(slot)
- return func()
- end
- function useMeals()
- while (not sel(S_WOOD, t.compare)) and (sel(S_MEAL, t.getItemCount) > 2) do
- t.select(S_MEAL)
- t.place()
- end
- end
- function cutWoods()
- t.select(S_WOOD)
- t.dig()
- t.forward()
- while t.detectUp() do
- t.digUp()
- t.up()
- end
- end
- function cutLeaves()
- t.select(S_SAPLING)
- while not t.detectDown() do
- for i=1,4 do
- t.dig()
- suckAllSapling()
- t.turnRight()
- end
- t.down()
- end
- end
- function getSapling()
- t.select(S_DIRT)
- t.digDown()
- t.down()
- for i=1,4 do
- suckAllSapling()
- t.turnRight()
- end
- t.up()
- sel(S_DIRT, t.placeDown)
- t.back()
- sel(S_SAPLING, t.place)
- end
- function dropItem(slot, nLeaves)
- t.select(slot)
- local n = t.getItemCount()
- if n > nLeaves then
- if slot == S_WOOD then
- qty_wood = qty_wood + (n-nLeaves)
- print(qty_wood)
- end
- return t.drop(n-nLeaves)
- else
- return false, "No items to drop"
- end
- end
- function fillItemSlot(slot, direction)
- t.select(slot)
- local n = t.getItemSpace()
- if n < 1 then
- return false,"No space for items"
- else
- print("Item Space is ",n)
- end
- if direction == "down" then
- return t.suckDown(n)
- else
- return t.suck(n)
- end
- end
- function repeatFunc(n, func)
- for i=1,n do
- func()
- end
- end
- function makeCoal()
- repeatFunc(3,t.back)
- repeatFunc(2,t.turnRight)
- fillItemSlot(S_CCOAL, "down")
- dropItem(S_CCOAL, 2)
- if t.getFuelLevel() < 80 then
- t.select(S_CCOAL)
- assert(t.refuel(1), "FuelLevel is low.")
- end
- repeatFunc(2,t.up)
- local b_status, err_msg = dropItem(S_WOOD, 2)
- if (not b_status) and (err_msg == "Inventory full") then
- assert(b_status, err_msg)
- end
- repeatFunc(2,t.down)
- t.turnLeft()
- dropItem(S_WOOD, 2)
- dropItem(S_SAPLING, 32)
- repeatFunc(2,t.turnLeft)
- fillItemSlot(S_MEAL)
- t.turnRight()
- repeatFunc(3,t.forward)
- end
- -- main
- assert(os.version() == OS_VERSION, "required "..OS_VERSION)
- waitForEnoughItems("BirchWood", 1, S_WOOD)
- waitForEnoughItems("BirchSapling", 2, S_SAPLING)
- waitForEnoughItems("Dirt", 2, S_DIRT)
- waitForEnoughFuel(80, S_CCOAL)
- while true do
- if sel(S_MEAL, t.getItemCount) > 2 then
- sleep_time = 20
- else
- sleep_time = 120
- end
- local limit = math.floor(300/sleep_time)
- if sel(S_WOOD, t.compare) or sel(S_MEAL, t.getItemCount) or count_checked>=limit then
- print("Start cutting.")
- useMeals()
- count_checked = 0
- cutWoods()
- cutLeaves()
- getSapling()
- if sel(S_WOOD, t.getItemCount)>32 then makeCoal() end
- else
- count_checked = count_checked+1
- print(string.format("It isn't wood: %d/%d",count_checked, limit))
- end
- suckAllSapling()
- os.sleep(sleep_time)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement