2020-11-09 19:45:05 +00:00
|
|
|
--[[Function Module program by Al Sweigart
|
|
|
|
Provides useful utility functions.]]
|
|
|
|
|
|
|
|
-- selectItem() selects the inventory
|
|
|
|
-- slot with the named item, returns
|
|
|
|
-- true if found and false if not
|
2020-11-12 16:45:08 +00:00
|
|
|
function select_item(name)
|
2020-11-09 19:45:05 +00:00
|
|
|
-- check all inventory slots
|
|
|
|
local item
|
2020-11-12 16:45:08 +00:00
|
|
|
for slot = 1, 16 do
|
|
|
|
item = turtle.getItemDetail(slot)
|
|
|
|
if item ~= nil and item['name'] == name then
|
|
|
|
turtle.select(slot)
|
|
|
|
return true
|
|
|
|
end
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return false -- couldn't find item
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- selectEmptySlot() selects inventory
|
|
|
|
-- slot that is empty, returns true if
|
|
|
|
-- found, false if no empty spaces
|
2020-11-12 16:45:08 +00:00
|
|
|
function select_emptySlot()
|
2020-11-09 19:45:05 +00:00
|
|
|
|
|
|
|
-- loop through all slots
|
|
|
|
for slot = 1, 16 do
|
|
|
|
if turtle.getItemCount(slot) == 0 then
|
|
|
|
turtle.select(slot)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false -- couldn't find empty space
|
|
|
|
end
|
|
|
|
|
|
|
|
-- countInventory() returns the total
|
2020-11-20 21:35:04 +00:00
|
|
|
-- number of a given minecraft id item in the inventory
|
|
|
|
function count_inventory(minecraft_id, times)
|
2020-11-12 16:45:08 +00:00
|
|
|
local total = 0
|
2020-11-09 19:45:05 +00:00
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
for slot = 1, 16 do
|
2020-11-12 20:25:57 +00:00
|
|
|
item = turtle.getItemDetail(slot)
|
|
|
|
if item ~= nil and item['name'] == 'minecraft:' .. minecraft_id then
|
2020-11-20 21:35:04 +00:00
|
|
|
total = total + turtle.getItemCount(slot)
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
2020-11-12 16:45:08 +00:00
|
|
|
end
|
|
|
|
return total
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
-- selectAndPlaceDown() selects a slot with given
|
|
|
|
-- minecraft id and places a block from it under the turtle
|
|
|
|
function select_and_place_down(minecraft_id)
|
|
|
|
for slot = 1, 16 do
|
2020-11-20 21:35:04 +00:00
|
|
|
item = turtle.getItemDetail(slot)
|
|
|
|
if item ~= nil and item['name'] == 'minecraft:' .. minecraft_id then
|
2020-11-12 16:45:08 +00:00
|
|
|
turtle.select(slot)
|
|
|
|
turtle.placeDown()
|
|
|
|
return
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
2020-11-12 16:45:08 +00:00
|
|
|
end
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
-- build_wall() creates a wall stretching
|
2020-11-09 19:45:05 +00:00
|
|
|
-- in front of the turtle
|
2020-11-12 16:45:08 +00:00
|
|
|
function build_wall(length, height)
|
|
|
|
if modules.count_inventory() > length * height then
|
|
|
|
return false --not enough block
|
|
|
|
end
|
2020-11-09 19:45:05 +00:00
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
turtle.up()
|
|
|
|
|
|
|
|
local movingForward = true
|
2020-11-09 19:45:05 +00:00
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
for currentHeight = 1, height do
|
|
|
|
for currentLength = 1, length do
|
|
|
|
select_and_place_down() -- Place the block
|
|
|
|
if movingForward and currentLength ~= length then
|
2020-11-09 19:45:05 +00:00
|
|
|
turtle.forward()
|
2020-11-12 16:45:08 +00:00
|
|
|
elseif not movingForward and currentLength ~= length then
|
|
|
|
turtle.back()
|
|
|
|
end
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
2020-11-12 16:45:08 +00:00
|
|
|
if currentHeight ~= height then
|
|
|
|
turtle.up()
|
|
|
|
end
|
|
|
|
movingForward = not movingForward
|
|
|
|
end
|
2020-11-09 19:45:05 +00:00
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
-- done building wall; move to end position
|
|
|
|
if movingForward then
|
|
|
|
-- turtle is near the start position
|
|
|
|
for currentLength = 1, length do
|
|
|
|
turtle.forward()
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
2020-11-12 16:45:08 +00:00
|
|
|
else
|
|
|
|
-- turtle is near the end position
|
|
|
|
turtle.forward()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- move down to the ground
|
|
|
|
for currentHeight = 1, height do
|
|
|
|
turtle.down()
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- build_room() constructs four walls
|
|
|
|
-- and a ceiling
|
|
|
|
function build_room(length, width, height)
|
|
|
|
if modules.count_inventory() < (((length -1) * height * 2) +
|
|
|
|
((width -1) * height * 2)) then
|
|
|
|
return false -- not enough blocks
|
|
|
|
end
|
|
|
|
|
|
|
|
-- build the four walls
|
|
|
|
build_wall(length -1, height)
|
|
|
|
turtle.turnRight()
|
|
|
|
|
|
|
|
build_wall(width - 1, height)
|
|
|
|
turtle.turnRight()
|
|
|
|
|
|
|
|
build_wall(length - 1, height)
|
|
|
|
turtle.turnRight()
|
|
|
|
|
|
|
|
build_wall(width - 1, height)
|
|
|
|
turtle.turnRight()
|
2020-11-09 19:45:05 +00:00
|
|
|
|
2020-11-12 16:45:08 +00:00
|
|
|
return true
|
2020-11-09 19:45:05 +00:00
|
|
|
end
|
2020-11-16 14:19:29 +00:00
|
|
|
|
|
|
|
-- sweepField() moves acress the rows
|
|
|
|
-- and columns of an area in front and
|
|
|
|
-- to the right of the turtle, calling
|
|
|
|
-- the provided sweepFunc at each space
|
|
|
|
function sweepField(length, width, sweepFunc)
|
|
|
|
local turnRightNext = true
|
|
|
|
|
|
|
|
for x = 1, width do
|
|
|
|
for y = 1, length do
|
|
|
|
sweepFunc()
|
|
|
|
|
|
|
|
-- don't move forward on the last row
|
|
|
|
if y ~= width then
|
|
|
|
turtle.forward()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- don't turn on the last column
|
|
|
|
if x ~= width then
|
|
|
|
-- turn to the next column
|
|
|
|
if turnRightNext then
|
|
|
|
turtle.turnRight()
|
|
|
|
turtle.forward()
|
|
|
|
turtle.turnRight()
|
|
|
|
else
|
|
|
|
turtle.turnLeft()
|
|
|
|
turtle.forward()
|
|
|
|
turtle.turnLeft()
|
|
|
|
end
|
|
|
|
|
|
|
|
turnRightNext = not turnRightNext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- move back to the start position
|
|
|
|
if width % 2 == 0 then
|
|
|
|
turtle.turnRight()
|
|
|
|
else
|
|
|
|
for y = 1, length -1 do
|
|
|
|
turtle.back()
|
|
|
|
end
|
|
|
|
turtle.turnLeft()
|
|
|
|
end
|
|
|
|
turtle.turnRight()
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--findBlock() spins around searching
|
|
|
|
--for the named block next to the turtle
|
|
|
|
function findBlock(name)
|
|
|
|
local result, block
|
|
|
|
|
|
|
|
for i = 1, 4 do
|
|
|
|
result, block = turtle.inspect()
|
|
|
|
if block ~= nil and block['name'] == name then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
turtle.turnRight()
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2020-11-21 19:44:27 +00:00
|
|
|
|
|
|
|
-- puke() puke everything that is in the turtles
|
|
|
|
-- inventory out in front of it (hopefully there is
|
|
|
|
-- a chest
|
|
|
|
function puke()
|
|
|
|
for i = 1, 16 do
|
|
|
|
turtle.select(i)
|
|
|
|
turtle.drop()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check Fuel checks if the turtle has enough fuel.
|
|
|
|
-- if not, it asks for fuel in slot 1 and refuels.
|
|
|
|
function check_fuel()
|
|
|
|
local refueled = false
|
|
|
|
while not refueled do
|
|
|
|
if turtle.getFuelLevel() == 0 then
|
|
|
|
print("Fuel is empty. Please put fuel in slot 1 and press a key.")
|
|
|
|
read()
|
|
|
|
turtle.select(1)
|
|
|
|
turtle.refuel()
|
|
|
|
else
|
|
|
|
refueled = true
|
|
|
|
end
|
|
|
|
end
|
2020-11-21 21:25:41 +00:00
|
|
|
end
|