Mine turtle: first ugly rev.

This commit is contained in:
Théophile Bastian 2020-08-18 18:19:45 +02:00
parent 8917140e77
commit 410c18d917
1 changed files with 215 additions and 0 deletions

215
turtles/mine.lua Normal file
View File

@ -0,0 +1,215 @@
facing = 0 -- Direction towards mining shafts. Incr. clockwise.
height = 0 -- At ground level, lowest mining level
MAX_HEIGHT = 7 -- Will not mine higher than this.
depth = -2 -- Out of the mining shafts
side = 6 -- Increases towards the right when facing shafts
DOCK_POINT_H = 0
DOCK_POINT_D = -2
DOCK_POINT_S = 6
DROP_POINT_H = 0
DROP_POINT_D = -2
DROP_POINT_S = 0
FUEL_POINT_H = 0
FUEL_POINT_D = -2
FUEL_POINT_S = 3
FUEL_EXPECTATION = 2000
FUEL_LOW = 1000
SHAFT_DEPTH = 100
function distance_to_drop()
return math.abs(depth - DROP_POINT_D)
+ math.abs(height - DROP_POINT_H)
+ math.abs(side - DROP_POINT_S);
end
function distance_to_refuel()
local drop_to_fuel = math.abs(FUEL_POINT_D - DROP_POINT_D)
+ math.abs(FUEL_POINT_H - DROP_POINT_H)
+ math.abs(FUEL_POINT_S - DROP_POINT_S);
return distance_to_drop() + drop_to_fuel;
end
function turn_abs(dir)
local diff = (dir - facing) % 4
if diff == 3 then
turtle.turnLeft()
else
for i=1,diff do turtle.turnRight() end
end
facing = dir
end
function turn_rel(dir)
turn_abs((facing + dir) % 4)
end
function go_to_depth(dd)
turn_abs(0)
while depth > dd do
turtle.back()
depth = depth - 1
end
while depth < dd do
turtle.forward()
depth = depth + 1
end
end
-- Go to a target position, assuming no excavation is needed
function go_to(dh, dd, ds)
go_to_depth(0)
while height > dh do
turtle.down()
height = height - 1
end
while height < dh do
turtle.up()
height = height + 1
end
turn_abs(1)
while side > ds do
turtle.back()
side = side - 1
end
while side < ds do
turtle.forward()
side = side + 1
end
go_to_depth(dd)
end
-- enter failed/done state
function abort(msg)
print("ABORTING: ")
print(msg)
go_to(DOCK_POINT_H, DOCK_POINT_D, DOCK_POINT_S)
making_a_fatal_error_awdadwdaw()
end
-- Discharge the inventory in the drop chest
function discharge_inventory()
go_to(DROP_POINT_H, DROP_POINT_D, DROP_POINT_S)
for slot=1,16 do
turtle.select(slot)
turtle.dropUp()
end
end
-- Refuel from fuel point
function refuel_turtle()
turtle.select(1)
if turtle.getItemCount() > 0 then
return false
end
go_to(DROP_POINT_H, DROP_POINT_D, DROP_POINT_S)
while turtle.getFuelLevel() < FUEL_EXPECTATION do
local rc, desc = turtle.suckUp()
if not rc then
abort("No more fuel available")
end
local rc, desc = turtle.refuel()
if not rc then
abort("Found some non-fuel in the fuel")
end
end
end
-- Find the next unmined shaft and end up in front of it
function find_next_shaft()
go_to(0, 0, 0)
while true do
print("Climb")
for hei=0,MAX_HEIGHT-1 do
if (hei % 3) == 0 then
if turtle.detect() then
return
end
end
if turtle.detectUp() then
turtle.digUp()
end
if not turtle.up() then abort("Misshaped lobby") end
height = height + 1
end
turn_abs(1)
for i=0,1 do
if turtle.detect() then
turtle.dig()
end
if not turtle.forward() then abort("Misshaped lobby") end
side = side + 1
end
turn_abs(0)
print("Down")
for hei=MAX_HEIGHT-1,0,-1 do
if (hei % 3) ~= 0 then
if turtle.detect() then
return
end
end
if turtle.detectDown() then
turtle.digDown()
end
if not turtle.down() then abort("Misshaped lobby") end
height = height - 1
end
turn_abs(1)
for i=0,1 do
if turtle.detect() then
turtle.dig()
end
if not turtle.forward() then abort("Misshaped lobby") end
side = side + 1
end
turn_abs(0)
end
end
-- Mine a new shaft in front of the turtle
function mine_shaft()
turtle.dig()
end
-- Main function
function main()
if turtle.getFuelLevel() < FUEL_LOW then
refuel_turtle()
else
print("NO FUEL AT ALL.")
return
end
while true do
print("Finding shaft")
find_next_shaft()
print("Mining shaft")
mine_shaft()
print("Discharging")
discharge_inventory()
if turtle.getFuelLevel() < FUEL_LOW then
refuel_turtle()
end
end
end
main()