surferpup

PortalTurtle

Dec 10th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. -- Portal Turtle v 2.2 (inspired by Armitige Shanks You Tube video of same)
  2. -- by Mike Simpkins December 2013 (Creative Commons)
  3. -- Paired with Portal Computer (http://pastebin.com/amCuLiLa)
  4.  
  5. local page = 1
  6. local id = os.getComputerID()
  7. -- set the id values to reflect this and other turtles
  8. if (id==132) then page=1 elseif (id==129) then page = 2 else page = 3 end
  9. local slots={}
  10. control=0
  11. local message=""
  12.  
  13. --Announce Turtle to Controller
  14. function announceTurtle()
  15.     -- Turn on Modem
  16.     if(not rednet.isOpen("right")) then rednet.open("right") end
  17.     displayStatus("Announcing Turtle...")
  18.     repeat
  19.         timer=os.startTimer(3)
  20.         rednet.broadcast("TURTLE"..page)
  21.        
  22.         repeat
  23.             event,control,message,_ = os.pullEvent()
  24.             --print(message or "null")
  25.         until ((event=="rednet_message" and message=="CONTROL") or (event=="timer" and control==timer))
  26.     until (message =="CONTROL")
  27.     initTurtle()
  28. end
  29.  
  30. --Initialize turtle inventory and communicate with controller
  31. function initTurtle()
  32.     displayStatus(" Initializing...",10,0)
  33.     --print ("Begin Init Turtle")
  34.     local zeroCount = 0
  35.     local slot
  36.     local status
  37.    
  38.    
  39.     --Reset Router
  40.     redstone.setOutput("bottom", false)
  41.     sleep (.2)
  42.     redstone.setOutput("bottom", true)
  43.  
  44.     -- Check Inventory
  45.     for slot=1,16 do
  46.         slots[slot]=turtle.getItemCount(slot)
  47.         if (slots[slot]==0) then zeroCount=zeroCount+1 end
  48.     end
  49.  
  50.     -- Turn on Modem
  51.     if(not rednet.isOpen("right")) then rednet.open("right") end
  52.     --print("modem is on")
  53.     if  (zeroCount>0) then
  54.         status = textutils.serialize(slots)
  55.         else
  56.         status = "FULL"
  57.     end
  58.     repeat
  59.         --print("Sending message to "..control)
  60.         rednet.send(control,"InitTurtle"..page,true)
  61.         --print ("Message Sent")
  62.         repeat
  63.             timer=os.startTimer(3)
  64.             event,sender, message,_ = os.pullEvent("rednet_message")
  65.             --print ("Sender = "..sender.." message= "..message)
  66.         until ((event=="rednet_message" and sender==control) or (event=="timer" and sender==timer))
  67.     until (message==("PROCEED"..page))
  68.     --print ("Given PROCEED")
  69.     repeat
  70.         rednet.send(control,status,true)
  71.         repeat
  72.             sender, message,_ = rednet.receive()
  73.         until (sender==control)
  74.     until (message==status)
  75.     rednet.send(control,"OK"..page,true)
  76.     --print("Turtle"..page.." initialized")
  77.     displayStatus("Connected",12,0)
  78. end
  79.  
  80. function displayStatus(state, line, clearScreen)
  81.     line = line or 7
  82.     clearScreen = clearScreen or 1
  83.     if (clearScreen==1) then term.clear() end
  84.     local position = math.ceil((39-#state)/2)
  85.     term.setCursorPos(position,line)
  86.     term.write(state)
  87. end
  88. function placeBook(book)
  89.    
  90.     if (book>0 and book<17 and turtle.getItemCount(book)>0) then
  91.         displayStatus("Placing Book"..tostring(book),7)
  92.         turtle.select(book)
  93.         turtle.drop(1)
  94.         return "OK"
  95.     else
  96.         displayStatus("No Book in Slot",11,0)
  97.         return "NoBook"
  98.     end
  99. end
  100.  
  101. function pullBook(book)
  102.     displayStatus("Pulling Book"..tostring(book),9,0)
  103.     -- force router to eject
  104.     redstone.setOutput("bottom", false)
  105.     sleep (.2)
  106.     -- check book placement, if placed in wrong slot move it
  107.     if turtle.getItemCount(book)==0 then
  108.         for slot=1,16 do
  109.             if (turtle.getItemCount(slot)~=slots[slot]) then
  110.                 --print ("Bad Slot")
  111.                 turtle.select(slot)
  112.                 xferResult =true
  113.                
  114.                 if (not turtle.transferTo(book,1)) then
  115.                     --print("could not moved book to "..tostring(slot))
  116.                     slots[slot]=turtle.getItemCount(slot)
  117.                 else
  118.                     --print("moved book to "..tostring(slot))
  119.                     break
  120.                 end
  121.             end
  122.         end
  123.     end
  124.     redstone.setOutput("bottom", true) -- turns router back on
  125. end
  126.  
  127.  
  128. announceTurtle()
  129. while true do
  130.     displayStatus("Awaiting Command")
  131.     event,arg1,arg2,arg3,arg4,a4g5 =os.pullEvent()
  132.     --print("Event ="..tostring(event).." Message "..tostring(arg2))
  133.     if event=="rednet_message" and arg1~=os.getComputerID() then
  134.         sender = arg1
  135.         message = arg2
  136.         if (sender==control or message=="ANNOUNCE") then --only messages from send or ANNOUNCE Broadcasts
  137.             --print("Control Message = "..message)
  138.             if (message=="ANNOUNCE") then
  139.                 control = sender
  140.                 announceTurtle()
  141.             elseif (message=="InitTurtle"..page) then --Request from Controller to init this turtle
  142.                 initTurtle()
  143.             elseif (string.sub(message,1,7)=="GetBook") then
  144.                 book = tonumber(string.sub(message,8))
  145.                 result = placeBook(book)
  146.                 rednet.send(control,result,true)
  147.                 if (result=="OK") then
  148.                     sleep(4)
  149.                     rednet.send(control,"READY",true)
  150.                     sleep(2)
  151.                     pullBook(book)
  152.                 end
  153.             else
  154.                 rednet.send(control,"FAIL " ..message,true)
  155.             end
  156.         end
  157.     elseif (event=="char" and arg1=="a") then announceTurtle()
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment