Advertisement
nitrogenfingers

3dprint

Jan 13th, 2013
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. --[[
  2.         3D Print
  3.         A printing program for use with NPaintPro
  4.         But makes for a nice remote-control routine too
  5.        
  6.         By NitrogenFingers
  7. ]]--
  8.  
  9. local activeCommander = -1
  10. local operatingPrint = false
  11.  
  12. --Whether or not the print can be ended
  13. local function endPrint()
  14.     operatingPrint = false
  15. end
  16.  
  17. --The list of all commands the printer can be ginve
  18. local commandList = {
  19.     ["FW"] = { turtle.dig, turtle.forward };
  20.     ["BK"] = turtle.back;
  21.     ["UP"] = { turtle.digUp, turtle.up };
  22.     ["DW"] = { turtle.digDown, turtle.down };
  23.     ["TL"] = turtle.turnLeft;
  24.     ["TR"] = turtle.turnRight;
  25.     ["TU"] = { turtle.turnLeft, turtle.turnLeft };
  26.     ["PF"] = { turtle.dig, turtle.place };
  27.     ["PU"] = { turtle.digUp, turtle.placeUp };
  28.     ["PD"] = { turtle.digDown, turtle.placeDown };
  29.     ["SS"] = turtle.select;
  30.     ["DE"] = endPrint;
  31. }
  32.  
  33. --Splits a string according to a pattern into a table              
  34. local function split(str, pattern)
  35.   local t = { }
  36.   local fpat = "(.-)" .. pattern
  37.   local last_end = 1
  38.   local s, e, cap = str:find(fpat, 1)
  39.   while s do
  40.     if s ~= 1 or cap ~= "" then
  41.       table.insert(t,cap)
  42.     end
  43.     last_end = e+1
  44.     s, e, cap = str:find(fpat, last_end)
  45.   end
  46.   if last_end <= #str then
  47.     cap = str:sub(last_end)
  48.     table.insert(t, cap)
  49.   end
  50.   return t
  51. end
  52.  
  53. --Listens for any instructions given referring to identification and activation. Once activated, the mode exits.
  54. local function respondToQuery()
  55.     while true do
  56.         print("Listening for ACT/ID query")
  57.         local id,key = rednet.receive()
  58.         print("Received : "..key)
  59.        
  60.         if key == "$3DPRINT IDENTIFY" then
  61.             print("Requested Identification")
  62.             rednet.send(id, "$3DPRINT IDACK "..os.getComputerLabel())
  63.        
  64.         elseif key == "$3DPRINT ACTIVATE" then
  65.             print("Requested Activation")
  66.             activeCommander = id
  67.             rednet.send(id, "$3DPRINT ACTACK")
  68.             break
  69.         end
  70.     end
  71. end
  72.  
  73. --Performs the print. Follows instrutions as given, and responds as necessary
  74. local function performPrint()
  75.     operatingPrint = true
  76.     while operatingPrint do
  77.         local id,msg = rednet.receive()
  78.        
  79.         if id == activeCommander and string.find(msg, "$PC") == 1 then
  80.             local cmds = split(msg, " ")
  81.            
  82.             if(tonumber(cmds[3])) and turtle.getItemCount(tonumber(cmds[3])) == 0 then
  83.                 rednet.send(id, "$3DPRINT DEP")
  84.             else
  85.                 if type(commandList[cmds[2]]) == "function" then
  86.                     commandList[cmds[2]](tonumber(cmds[3]))
  87.                 elseif type(commandList[cmds[2]]) == "table" then
  88.                     for i=1,#commandList[cmds[2]] do
  89.                         commandList[cmds[2]][i](tonumber(cmds[3]))
  90.                     end
  91.                 end
  92.            
  93.                 rednet.send(activeCommander, "$3DPRINT ACK")
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. rednet.open("right")
  100. term.clear()
  101. term.setCursorPos(1,1)
  102. if not os.getComputerLabel() then
  103.     term.write("Name this computer:")
  104.     os.setComputerLabel(io.read())
  105. end
  106. print("3D printer online")
  107.  
  108. while true do
  109.     --Wait for activation
  110.     respondToQuery()
  111.     --Perform the print
  112.     performPrint()
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement