SHOW:
|
|
- or go back to the newest paste.
1 | --[[ | |
2 | 3D Print | |
3 | A printing program for use with NPaintPro | |
4 | ||
5 | By NitrogenFingers | |
6 | ]]-- | |
7 | ||
8 | local activeCommander = -1 | |
9 | local operatingPrint = false | |
10 | ||
11 | --Whether or not the print can be ended | |
12 | local function endPrint() | |
13 | operatingPrint = false | |
14 | end | |
15 | ||
16 | --The list of all commands the printer can be ginve | |
17 | local commandList = { | |
18 | ["FW"] = { turtle.dig, turtle.forward }; | |
19 | ["BK"] = turtle.back; | |
20 | ["UP"] = { turtle.digUp, turtle.up }; | |
21 | ["DW"] = { turtle.digDown, turtle.down }; | |
22 | ["TL"] = turtle.turnLeft; | |
23 | ["TR"] = turtle.turnRight; | |
24 | ["TU"] = { turtle.turnLeft, turtle.turnLeft }; | |
25 | ["PF"] = { turtle.dig, turtle.place }; | |
26 | ["PU"] = { turtle.digUp, turtle.placeUp }; | |
27 | ["PD"] = { turtle.digDown, turtle.placeDown }; | |
28 | ["SS"] = turtle.select; | |
29 | ["RF"] = turtle.refuel; | |
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 | print("Command : "..msg) | |
79 | ||
80 | if id == activeCommander and string.find(msg, "$PC") == 1 then | |
81 | local cmds = split(msg, " ") | |
82 | ||
83 | --It's a bit of a hack, but those are the 2 methods required for a refuel | |
84 | if turtle.getFuelLevel() == 0 and cmds[2] ~= "SS" and cmds[2] ~= "RF" then | |
85 | rednet.send(id, "$3DPRINT OOF") | |
86 | elseif (tonumber(cmds[3])) and turtle.getItemCount(tonumber(cmds[3])) == 0 and | |
87 | turtle.getFuelLevel() ~= 0 then | |
88 | rednet.send(id, "$3DPRINT DEP") | |
89 | else | |
90 | if cmds[2] == "RF" then cmds[3] = "64" end | |
91 | if type(commandList[cmds[2]]) == "function" then | |
92 | commandList[cmds[2]](tonumber(cmds[3])) | |
93 | elseif type(commandList[cmds[2]]) == "table" then | |
94 | for i=1,#commandList[cmds[2]] do | |
95 | commandList[cmds[2]][i](tonumber(cmds[3])) | |
96 | end | |
97 | end | |
98 | ||
99 | rednet.send(activeCommander, "$3DPRINT ACK") | |
100 | end | |
101 | end | |
102 | end | |
103 | end | |
104 | ||
105 | rednet.open("right") | |
106 | term.clear() | |
107 | term.setCursorPos(1,1) | |
108 | if not os.getComputerLabel() then | |
109 | term.write("Name this computer:") | |
110 | os.setComputerLabel(io.read()) | |
111 | end | |
112 | print("3D printer online") | |
113 | ||
114 | while true do | |
115 | --Wait for activation | |
116 | respondToQuery() | |
117 | --Perform the print | |
118 | performPrint() | |
119 | end |