Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Portal Turtle v 2.2 (inspired by Armitige Shanks You Tube video of same)
- -- by Mike Simpkins December 2013 (Creative Commons)
- -- Paired with Portal Computer (http://pastebin.com/amCuLiLa)
- local page = 1
- local id = os.getComputerID()
- -- set the id values to reflect this and other turtles
- if (id==132) then page=1 elseif (id==129) then page = 2 else page = 3 end
- local slots={}
- control=0
- local message=""
- --Announce Turtle to Controller
- function announceTurtle()
- -- Turn on Modem
- if(not rednet.isOpen("right")) then rednet.open("right") end
- displayStatus("Announcing Turtle...")
- repeat
- timer=os.startTimer(3)
- rednet.broadcast("TURTLE"..page)
- repeat
- event,control,message,_ = os.pullEvent()
- --print(message or "null")
- until ((event=="rednet_message" and message=="CONTROL") or (event=="timer" and control==timer))
- until (message =="CONTROL")
- initTurtle()
- end
- --Initialize turtle inventory and communicate with controller
- function initTurtle()
- displayStatus(" Initializing...",10,0)
- --print ("Begin Init Turtle")
- local zeroCount = 0
- local slot
- local status
- --Reset Router
- redstone.setOutput("bottom", false)
- sleep (.2)
- redstone.setOutput("bottom", true)
- -- Check Inventory
- for slot=1,16 do
- slots[slot]=turtle.getItemCount(slot)
- if (slots[slot]==0) then zeroCount=zeroCount+1 end
- end
- -- Turn on Modem
- if(not rednet.isOpen("right")) then rednet.open("right") end
- --print("modem is on")
- if (zeroCount>0) then
- status = textutils.serialize(slots)
- else
- status = "FULL"
- end
- repeat
- --print("Sending message to "..control)
- rednet.send(control,"InitTurtle"..page,true)
- --print ("Message Sent")
- repeat
- timer=os.startTimer(3)
- event,sender, message,_ = os.pullEvent("rednet_message")
- --print ("Sender = "..sender.." message= "..message)
- until ((event=="rednet_message" and sender==control) or (event=="timer" and sender==timer))
- until (message==("PROCEED"..page))
- --print ("Given PROCEED")
- repeat
- rednet.send(control,status,true)
- repeat
- sender, message,_ = rednet.receive()
- until (sender==control)
- until (message==status)
- rednet.send(control,"OK"..page,true)
- --print("Turtle"..page.." initialized")
- displayStatus("Connected",12,0)
- end
- function displayStatus(state, line, clearScreen)
- line = line or 7
- clearScreen = clearScreen or 1
- if (clearScreen==1) then term.clear() end
- local position = math.ceil((39-#state)/2)
- term.setCursorPos(position,line)
- term.write(state)
- end
- function placeBook(book)
- if (book>0 and book<17 and turtle.getItemCount(book)>0) then
- displayStatus("Placing Book"..tostring(book),7)
- turtle.select(book)
- turtle.drop(1)
- return "OK"
- else
- displayStatus("No Book in Slot",11,0)
- return "NoBook"
- end
- end
- function pullBook(book)
- displayStatus("Pulling Book"..tostring(book),9,0)
- -- force router to eject
- redstone.setOutput("bottom", false)
- sleep (.2)
- -- check book placement, if placed in wrong slot move it
- if turtle.getItemCount(book)==0 then
- for slot=1,16 do
- if (turtle.getItemCount(slot)~=slots[slot]) then
- --print ("Bad Slot")
- turtle.select(slot)
- xferResult =true
- if (not turtle.transferTo(book,1)) then
- --print("could not moved book to "..tostring(slot))
- slots[slot]=turtle.getItemCount(slot)
- else
- --print("moved book to "..tostring(slot))
- break
- end
- end
- end
- end
- redstone.setOutput("bottom", true) -- turns router back on
- end
- announceTurtle()
- while true do
- displayStatus("Awaiting Command")
- event,arg1,arg2,arg3,arg4,a4g5 =os.pullEvent()
- --print("Event ="..tostring(event).." Message "..tostring(arg2))
- if event=="rednet_message" and arg1~=os.getComputerID() then
- sender = arg1
- message = arg2
- if (sender==control or message=="ANNOUNCE") then --only messages from send or ANNOUNCE Broadcasts
- --print("Control Message = "..message)
- if (message=="ANNOUNCE") then
- control = sender
- announceTurtle()
- elseif (message=="InitTurtle"..page) then --Request from Controller to init this turtle
- initTurtle()
- elseif (string.sub(message,1,7)=="GetBook") then
- book = tonumber(string.sub(message,8))
- result = placeBook(book)
- rednet.send(control,result,true)
- if (result=="OK") then
- sleep(4)
- rednet.send(control,"READY",true)
- sleep(2)
- pullBook(book)
- end
- else
- rednet.send(control,"FAIL " ..message,true)
- end
- end
- elseif (event=="char" and arg1=="a") then announceTurtle()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment