Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TODO : make check for slot 1 being empty
- -- create exit for when chest is full
- --Load the saved Turtle state from disk if available, if not set to 0
- function loadState()
- if fs.exists("xpTurtleState") then
- local file = fs.open("xpTurtleState","r")
- data = file.readAll()
- file.close()
- data=tonumber(data)
- moveState=data
- else
- print("debug -- no xpTurtleState found, setting movement state to 0\n")
- moveState = 0
- end
- end
- -- Save the current state to disk
- function saveState(a)
- local file = fs.open("xpTurtleState","w")
- file.write(a)
- file.close()
- -- io.write(string.format("debug -- wrote %s to file\n",a))
- end
- -- Check where the turtle is right now, and it it isn't at block 0, move there
- function moveToStart()
- loadState()
- -- io.write(string.format("debug -- Moving %s blocks to starting point\n", moveState))
- if moveState~=nil then
- printStatus("moving to start", getLvl())
- moveForward(moveState)
- end
- end
- -- Move forward with checking for blockage and save the move state
- function moveForward(nDistance)
- printStatus("moving to start", getLvl())
- -- print("moveForward nDistance is ", nDistance)
- while nDistance > 0 do
- if turtle.forward() then
- nDistance=nDistance-1
- moveState = moveState -1
- saveState(moveState)
- else
- sleep(0.5)
- end
- end
- end
- -- Move back with checking for blockage and save the move state
- function moveBack(nDistance)
- while nDistance > 0 do
- printStatus("moving to enchant area", curLev)
- -- print("moveBack () nDistance is ", nDistance)
- if turtle.back() then
- nDistance=nDistance-1
- moveState = moveState +1
- saveState(moveState)
- else
- sleep(0.5)
- end
- end
- end
- -- check if there's enough fuel, if not refuel from slot 16. If no fuel available, exit.
- function checkFuel()
- if turtle.getFuelLevel()<20 then
- if turtle.getItemCount(16)>=1 then
- turtle.select(16)
- turtle.refuel(1)
- else
- exitMsg(1)
- end
- end
- end
- -- get the current level of the turtle
- function getLvl()
- curLev = m.getLevels()
- return curLev
- end
- -- Check if nLvl(default=30) levels have been reached, if not wait, if yes, return true.
- function checkLevel(nLvl)
- getLvl()
- if curLev<nLvl then
- printStatus("gathering XP", curLev)
- return false
- else
- printStatus("moving to enchant area", getLvl())
- return true
- end
- end
- -- Look for items to enchant in slots 2 through 14, if there are none, default to findBooks()
- function getItems()
- for i=2,14 do
- if turtle.getItemCount(i)>0 then
- turtle.select(i)
- turtle.transferTo(1,1)
- turtle.select(1)
- return true
- end
- end
- -- print("no items found, trying to find books")
- if findBook() then
- -- print("Books found")
- return true
- else
- return false
- end
- end
- -- Get books from inventory slot 15 and put 1 in slot 1, returning true, if not return false
- function findBook()
- if turtle.getItemCount(15)>=1 then
- turtle.select(15)
- turtle.transferTo(1,1)
- turtle.select(1)
- return true
- else
- return false
- end
- end
- -- enchant the book in slot one and drop it into the chest underneath the turtle
- function enchantBook(nLvl)
- if m.enchant(nLvl) then
- turtle.dropDown()
- else
- error("Unexpected error while trying to enchant")
- end
- end
- -- End program with a message
- function exitMsg(exitNr)
- if exitNr == 1 then exitStr="Out of fuel. Place more fuel in slot 16 and restart the program"
- elseif exitNr == 2 then exitStr="No more items or books"
- -- elseif exitNr == 3 then exitStr=""
- -- elseif exitNr == 4 then exitStr=""
- -- elseif exitNr == then exitStr=""
- end
- term.clear()
- term.setCursorPos(2,2)
- sleep(0.5)
- print(exitStr)
- run.state="end"
- end
- function printIntro()
- term.clear()
- print("---------------------------------------")
- print("------- XPturtle 0.2 by Gili710 -------")
- print("---------------------------------------")
- print()
- print("This turtle will automatically enchant items in its inventory")
- print("Place any items in slots 2 through 14")
- print("Place some books in slot 15.")
- print("Place some fuel in slot 16")
- print("Press any key to start")
- print()
- end
- function printStatus(curTask, curLvl)
- term.clear()
- term.setCursorPos(1,1)
- print("------- XPturtle 0.2 by Gili710 -------")
- term.setCursorPos(2,4)
- io.write(string.format("Current Task: %s", curTask))
- term.setCursorPos(2,6)
- io.write(string.format("Current Level: %s", curLvl))
- end
- --INIT
- m=peripheral.wrap("right")
- m.setAutoCollect(true)
- nLvl=30
- run = {}
- run.state="running"
- screenw, screenh = term.getSize()
- -- Main Program
- printIntro()
- os.pullEvent()
- checkFuel()
- if run.state=="running" then
- moveToStart()
- end
- -- start main loop
- while run.state=="running" do
- while not checkLevel(nLvl) do
- sleep(1)
- end
- checkFuel()
- loadState()
- -- Extra safety here
- while moveState ~= 5 do
- moveToStart()
- moveBack(5)
- end
- printStatus("enchanting", getLvl())
- sleep(2)
- if getItems() then
- enchantBook(nLvl)
- sleep(2)
- moveForward(5)
- else
- exitMsg(2)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement