· 4 years ago · Apr 30, 2021, 06:40 AM
1function mine (length, width, height)
2 for i = 0, height do
3 for j = 0, width do
4 for k = 0, length do
5 turtle.dig()
6 turtle.forward()
7 end
8 turtle.turnLeft()
9 turtle.dig()
10 turtle.forward()
11 turtle.turnLeft()
12 end
13 turtle.digUp()
14 turtle.up()
15 end
16end
17
18function tunnel(length, width, height)
19 for i = 1, length do
20 for j = 1, width do
21 turtle.digUp()
22 turtle.dig()
23 turtle.digDown()
24 turtle.forward()
25 end
26 if i % 2 == 0 then
27 turtle.turnLeft()
28 turtle.dig()
29 turtle.forward()
30 turtle.turnLeft()
31 else
32 turtle.turnRight()
33 turtle.dig()
34 turtle.forward()
35 turtle.turnRight()
36 end
37
38 end
39end
40
41tunnel(200, 10, 3)
42
43------ Mining Turtle Code ------
44
45------ Api Call ------
46-- Builds the body for the post request --
47-- Sends the information about the turtle and its surroundings via post request --
48------ Api Call ------
49function callApi()
50 id = os.getComputerLabel()
51 body = id
52
53 x, y, z = gps.locate()
54
55 if not x then
56 print("No GPS response received. Shutting Down")
57 return
58 end
59
60 table.insert(body,x)
61 table.insert(body, y)
62 table.insert(body, z)
63
64 sAbove, sForward, SBelow = getSurroundings()
65 table.insert(body, sAbove)
66 table.insert(body, sForward)
67 table.insert(body, SBelow)
68
69 inventory = getInventory()
70 for i = 1, 32, 1 do
71 table.insert(body, inventory[i])
72 end
73
74 fuel = turtle.getFuelLevel()
75 table.insert(body, fuel)
76
77 print(serializeTable(body))
78
79 request = http.post("http://175.35.119.124:5000/turtles/turtle", serializeTable(body))
80end
81
82-- Get the ivnentory items and return them as an array --
83function getInventory()
84
85 inventory = {}
86
87 for i = 1, 16 do
88 item = turtle.getItemDetail(i)
89 if not item then
90 table.insert(inventory, "none")
91 table.insert(inventory, "65")
92 else
93 table.insert(inventory, item.name)
94 table.insert(inventory, item.count)
95 end
96 end
97 return inventory
98
99end
100
101-- Get the blocks above, below and infront of the turtle --
102function getSurroundings()
103
104 above = turtle.inspectUp()
105 forward = turtle.inspect()
106 below = turtle.inspectDown()
107
108 return above, forward, below
109
110end
111
112-- Stolen Code --
113-- Convert Table to a string --
114function serializeTable(val, name, skipnewlines, depth)
115 local result = "{"
116 for k, v in pairs(tbl) do
117 -- Check the key type (ignore any numerical keys - assume its an array)
118 if type(k) == "string" then
119 result = result.."[\""..k.."\"]".."="
120 end
121
122 -- Check the value type
123 if type(v) == "table" then
124 result = result..table_to_string(v)
125 elseif type(v) == "boolean" then
126 result = result..tostring(v)
127 else
128 result = result.."\""..v.."\""
129 end
130 result = result..","
131 end
132 -- Remove leading commas from the result
133 if result ~= "" then
134 result = result:sub(1, result:len()-1)
135 end
136 return result.."}"
137end
138
139callApi()