Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dataLocation = "/data/"
- local dataFile = "data"
- local data = {}
- local flag = true
- local function doSort(min)
- local function swap(a,b,data)
- turtle.select(a)
- turtle.transferTo(16)
- turtle.select(b)
- turtle.transferTo(a)
- turtle.select(16)
- turtle.transferTo(b)
- local tmp = data[a]
- data[a] = data[b]
- data[b] = tmp
- end
- local dat = {}
- local flg = true
- for i = min,16 do --Get what items are there
- local meta = turtle.getItemDetail(i)
- if meta then
- dat[i] = meta.name
- end
- end
- if dat[16] then--If item in "swap" slot, move it to another slot.
- for i = min,15 do
- if not dat[i] then
- swap(16,i,dat)
- break
- end
- end
- end
- while flg do
- flg = false
- for i = min,15 do
- print(i,"=",textutils.serialize(dat))
- print()
- if not dat[i] and dat[i+1] ~= nil then--If item[i] doesnt exist, but item[i+1] exists, search for the first available slot, then send the item there.
- print("Item a does not exist, item b does")
- flg = true
- for i2 = i,min,-1 do
- print("StartLoop")
- if dat[i2] then
- print("Dati2")
- turtle.select(i+1)
- if dat[i2] == dat[i+1] then --If they are the same, balance.
- print("dati2 = dati+1")
- turtle.transferTo(i2)
- if turtle.getItemCount(i+1) == 0 then --If there is no item left, remove from cache, otherwise move it to i2+1
- print("dati+1 = nil")
- dat[i+1] = nil
- end
- end
- print("selecti+1")
- turtle.transferTo(i2+1)
- dat[i2+1] = dat[i+1]
- dat[i+1] = nil
- break
- elseif i2 == min and not dat[i2] then -- If nothing in slot min, then move current item to slot min.
- turtle.select(i+1)
- turtle.transferTo(1)
- dat[1] = dat[i+1]
- dat[i+1] = nil
- end
- end
- elseif ( dat[i] and dat[i+1] and dat[i] < dat[i+1] ) then
- --if both exist, and item[i] < item[i+1] then swap them
- flg = true
- swap(i,i+1,dat)
- elseif dat[i] == dat[i+1] and dat[i] ~= nil then
- --if they are equal, try to balance.
- turtle.select(i+1)
- turtle.transferTo(i)
- if turtle.getItemCount(i+1) == 0 then
- dat[i+1] = nil
- end
- end
- end
- end
- end
- --[[
- Sort: Sorts items according to seed requirements.
- ]]
- local function sort()
- print("Sorting...")
- if data.RequireSeeds then
- local dat = {}
- for i = 1,16 do --Get what items are there
- local meta = turtle.getItemDetail(i)
- if meta then
- dat[i] = meta.name
- end
- end
- if dat[1] ~= data.SeedName then --If the seedslot is not occupied by seeds, move them out of the way.
- for i = 1,16 do
- if not dat[i] then
- dat[i] = dat[1]
- dat[1] = nil
- turtle.select(1)
- turtle.transferTo(i)
- break
- end
- end
- end
- for i = 1,16 do
- if dat[i] and dat[i] == data.SeedName then --Move all seeds to slot 1
- turtle.select(i)
- turtle.transferTo(1)
- end
- end
- doSort(2) --Sort all slots except slot 1
- else
- doSort(1) --Sort all slots
- end
- end
- --[[
- Count: Counts how many slots are used, returns it.
- ]]
- local function count()
- print("Counting inventory...")
- local spUsed = 0
- for i = 1,16 do
- --turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- spUsed = spUsed + 1
- end
- end
- print(spUsed, "of 16 slots used")
- return spUsed
- end
- --[[
- Watcher: Reads inventory, and sets a flag to false if it's full, otherwise the flag is set to true.
- Possibly shut down instead?
- Also will sort the inventory according to data.
- ]]
- local function watcher()
- local ca = count()
- if ca < 14 then
- print("Good to continue")
- flag = true
- else
- print("Stopping.")
- flag = false
- end
- sort()
- end
- --[[
- Dig: Digs a block, and checks if it needs to plant seeds.
- ]]
- local function dig()
- print("Dig and replace")
- turtle.dig()
- if data.RequireSeeds then
- turtle.select(1)
- turtle.place()
- end
- end
- --[[
- Farmer: Checks the block in front of it, and sees if it needs to be farmed. Turns then sleeps
- if inventory is full, sleep 10 seconds, otherwise do nothing.
- ]]
- local function farmer()
- local i = 0
- while true do
- if flag then
- print("Inspecting...")
- local block,meta = turtle.inspect()
- if block then
- if data.MaxGrowth > 0 then
- if meta.state then
- if meta.state.age >= data.MaxGrowth then
- dig()
- end
- else
- if meta.metadata >= data.MaxGrowth then
- dig()
- end
- end
- else
- dig()
- end
- else
- if data.RequireSeeds then
- turtle.select(1)
- turtle.place()
- end
- end
- else
- print("NO OP")
- os.sleep(data.Delay * 5)
- watcher()
- end
- if i % 10 == 0 then
- watcher()
- i = 0
- end
- i = i + 1
- print("Turning")
- turtle.turnRight()
- print("Sleeping", data.Delay, "seconds.")
- os.sleep(data.Delay)
- end
- end
- --[[
- CreateData: Creates your data boi
- ]]
- local function createData(location)
- if not fs.isDir(dataLocation) then
- fs.makeDir(dataLocation)
- end
- local h = fs.open(dataLocation..dataFile,"w")
- h.writeLine("return {")
- h.writeLine(" MaxGrowth = 7,")
- h.writeLine(" Delay = 3,")
- h.writeLine(" RequireSeeds = true,")
- h.writeLine(" SeedName = \"minecraft:wheat_seeds\",")
- h.writeLine("}")
- h.close()
- end
- --[[
- ReadData: Reads the data into variable "data"
- ]]
- local function readData()
- data = dofile(dataLocation..dataFile)
- end
- -----------------------------------------------------------Program starts here
- print("Starting up.")
- if fs.exists(dataLocation..dataFile) then
- readData()
- else
- createData(dataLocation..dataFile)
- end
- print("Ready.")
- local ok,err = pcall(farmer)
- if not ok then
- printError(err)
- os.sleep(10)
- os.reboot()
- else print(ok,err)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement