MarcosKoco

3DnPrint

Jun 6th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. --[[
  2.         3D Print
  3.         A printing program for use with NPaintPro
  4.        
  5.         By NitrogenFingers
  6.         Changed for in own scripts
  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.     ["RF"] = turtle.refuel;
  31.     ["DE"] = endPrint;
  32.     ["EE"] = exit()
  33. }
  34.  
  35. --Splits a string according to a pattern into a table              
  36. local function split(str, pattern)
  37.   local t = { }
  38.   local fpat = "(.-)" .. pattern
  39.   local last_end = 1
  40.   local s, e, cap = str:find(fpat, 1)
  41.   while s do
  42.     if s ~= 1 or cap ~= "" then
  43.       table.insert(t,cap)
  44.     end
  45.     last_end = e+1
  46.     s, e, cap = str:find(fpat, last_end)
  47.   end
  48.   if last_end <= #str then
  49.     cap = str:sub(last_end)
  50.     table.insert(t, cap)
  51.   end
  52.   return t
  53. end
  54.  
  55. --Listens for any instructions given referring to identification and activation. Once activated, the mode exits.
  56. local function respondToQuery()
  57.     while true do
  58.         print("Listening for ACT/ID query")
  59.         local id,key = rednet.receive()
  60.         print("Received : "..key)
  61.        
  62.         if key == "$3DPRINT IDENTIFY" then
  63.             print("Requested Identification")
  64.             rednet.send(id, "$3DPRINT IDACK "..os.getComputerLabel())
  65.        
  66.         elseif key == "$3DPRINT ACTIVATE" then
  67.             print("Requested Activation")
  68.             activeCommander = id
  69.             rednet.send(id, "$3DPRINT ACTACK")
  70.             break
  71.         end
  72.     end
  73. end
  74.  
  75. --Performs the print. Follows instrutions as given, and responds as necessary
  76. local function performPrint()
  77.     operatingPrint = true
  78.     while operatingPrint do
  79.         local id,msg = rednet.receive()
  80.         print("Command : "..msg)
  81.        
  82.         if id == activeCommander and string.find(msg, "$PC") == 1 then
  83.             local cmds = split(msg, " ")
  84.            
  85.             --It's a bit of a hack, but those are the 2 methods required for a refuel
  86.             if turtle.getFuelLevel() == 0 and cmds[2] ~= "SS" and cmds[2] ~= "RF" then
  87.                 rednet.send(id, "$3DPRINT OOF")
  88.             elseif (tonumber(cmds[3])) and turtle.getItemCount(tonumber(cmds[3])) == 0 and
  89.                     turtle.getFuelLevel() ~= 0 then
  90.                 rednet.send(id, "$3DPRINT DEP")
  91.             else
  92.                 if cmds[2] == "RF" then cmds[3] = "64" end
  93.                 if type(commandList[cmds[2]]) == "function" then
  94.                     commandList[cmds[2]](tonumber(cmds[3]))
  95.                 elseif type(commandList[cmds[2]]) == "table" then
  96.                     for i=1,#commandList[cmds[2]] do
  97.                         commandList[cmds[2]][i](tonumber(cmds[3]))
  98.                     end
  99.                 end
  100.            
  101.                 rednet.send(activeCommander, "$3DPRINT ACK")
  102.             end
  103.         end
  104.     end
  105. end
  106.  
  107. rednet.open("right")
  108. term.clear()
  109. term.setCursorPos(1,1)
  110. if not os.getComputerLabel() then
  111.     term.write("Name this computer:")
  112.     os.setComputerLabel(io.read())
  113. end
  114. print("3D printer online")
  115.  
  116. while true do
  117.     --Wait for activation
  118.     respondToQuery()
  119.     --Perform the print
  120.     performPrint()
  121. end
Add Comment
Please, Sign In to add comment