 # #OC simple miner

Oct 22nd, 2015
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. --[[
2. SIMPLE MINER V3
3. This program will mine a hole of your specified dimensions if you provide him a pickaxe.
4. It takes in arguments, first being the depth of the hole, second being the length in
5. the direction the robot is pointing and the third being the length that is facing the
6. robot to the right.
7.
8. Bugs:
9. - none atm
10. --]]
11.
12. local r = require("robot")
13. local arg = {...}
14.
15. -- getting variables for size / setting variables
16. if arg == nil or arg == nil or arg == nil then
17.   print("Depth number: ")
19.   print("Blocks X: ")
21.   print("Blocks Y: ")
23. else
24.   depth = tonumber(arg)
25.   sideX = tonumber(arg)
26.   sideY = tonumber(arg)
27. end
28.
29. side = "r"
30.
31. --[[
32. FUNCTIONS part
33. --]]
34.
35. -- The function to move in any direction
36. function move(direction)
37. -- u = up; d = down; f = forward
38.
39.   if direction == nil then
40.     -- forward
41.     if not r.forward() then
42.       local _, why = r.detect()
43.       if why == "entity" then
44.         while not r.forward() do
45.           io.write("Go away! (sleep for 3s)\n")
46.           r.swing()
47.           r.swing()
48.           os.sleep(3)
49.         end
50.       elseif why == "air" then -- why is this needed!!!
51.         if arg == debug then
52.           print("just moving through air -.-")
53.         end
54.         r.forward()
55.       else
56.         io.write("Obstacle! Can't move, stopping operation. (", why, ")\n")
57.         print("may I proceed?")
59.         r.forward()
60.       end
61.     end
62.
63.   elseif direction == "down" then
64.     -- down
65.     if not r.down() then
66.       local _, why = r.detectDown()
67.       if why == "entity" then
68.         while not r.down() do
69.           io.write("Go away! (sleep for 3s)\n")
70.           r.swing()
71.           r.swing()
72.           os.sleep(3)
73.         end
74.       elseif why == "air" then -- why is this needed!!!
75.         if arg == debug then
76.           print("just moving through air -.-")
77.         end
78.         r.down()
79.       else
80.         io.write("Obstacle! Can't move, stopping operation. (", why, ")\n")
81.         print("may I proceed?")
83.         r.down()
84.       end
85.     end
86.   end
87. end
88.
89. -- The function to swing / mine blocks
90. function swing(direction)
91. -- u = up; d = down; f = forward
92.   if direction == nil then
93.     -- forward
94.     if not r.swing() then
95.       local _, why = r.detect()
96.       if why == "entity" then
97.         while not r.swing() do
98.           io.write("Go away! (sleep for 3s)\n")
99.           r.swing()
100.           r.swing()
101.           os.sleep(3)
102.         end
103.       elseif why == "air" then
104.         io.write("just swinging at air -.-\n")
105.       else
106.         io.write("Obstacle! Can't swing, stopping operation. (", why, ")\n")
107.         io.write("Please remove the obstacle. May I proceed?\n")
109.       end
110.     end
111.
112.   elseif direction == "down" then
113.     -- down
114.     if not r.swingDown() then
115.       local _, why = r.detectDown()
116.       if why == "entity" then
117.         while not r.swingDown() do
118.           io.write("Go away! (sleep for 3s)\n")
119.           r.swing()
120.           r.swing()
121.           os.sleep(3)
122.         end
123.       elseif why == "air" then
124.         io.write("just swinging at air -.-\n")
125.       else
126.         io.write("Obstacle! Can't swing, stopping operation. (", why, ")\n")
127.         io.write("Please remove the obstacle. May I proceed?\n")
129.       end
130.     end
131.   end
132. end
133.
134. --[[
135. THE MAIN PROGRAM
136. --]]
137. -- ToDo: checks for tools (later maybe fuel or sth... if it has generator module, ...)
138.
139. -- the moving and swinging part
140. for i=1,depth do
141.   swing("down")
142.   move("down")
143.   io.write("Floor ", i, "\n")
144.   for j=1,sideX do
145.     io.write(" - row ", j, "\n")
146.     for k=1,sideY-1 do
147.       swing()
148.       move()
149.     end
150.     if j ~= sideX then
151.       if side == "r" then
152.         r.turnRight()
153.         swing()
154.         move()
155.         r.turnRight()
156.         side = "l"
157.       elseif side == "l" then
158.         r.turnLeft()
159.         swing()
160.         move()
161.         r.turnLeft()
162.         side = "r"
163.       end
164.     end
165.   end
166.   r.turnAround()
167. end