ComputerCraft/Modules/modules.lua
TheLux83 49bfd155e2 Changed functions in modules.lua and created circles.lua
hare.lua contains now everything from Al Sweigarts book and modules is my interpretation from it with a few tweaks.
Additionally I've created a circles.lua module. This module is used to build circles referenced from https://i.redd.it/swn3e9w8dyc31.jpg
2020-11-12 17:45:08 +01:00

131 lines
2.9 KiB
Lua

--[[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
function select_item(name)
-- check all inventory slots
local item
for slot = 1, 16 do
item = turtle.getItemDetail(slot)
if item ~= nil and item['name'] == name then
turtle.select(slot)
return true
end
end
return false -- couldn't find item
end
-- selectEmptySlot() selects inventory
-- slot that is empty, returns true if
-- found, false if no empty spaces
function select_emptySlot()
-- 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
-- numer of a given minecraft id item in the inventory
function count_inventory(minecraft_id)
local total = 0
for slot = 1, 16 do
if turtle.getItemDetail(slot) == 'minecraft:' .. minecraft_id then
total = total + turtle.getItem(slot)
end
end
return total
end
-- 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
if turtle.getItemDetail(slot) == 'minecraft:' .. minecraft_id then
turtle.select(slot)
turtle.placeDown()
return
end
end
end
-- build_wall() creates a wall stretching
-- in front of the turtle
function build_wall(length, height)
if modules.count_inventory() > length * height then
return false --not enough block
end
turtle.up()
local movingForward = true
for currentHeight = 1, height do
for currentLength = 1, length do
select_and_place_down() -- Place the block
if movingForward and currentLength ~= length then
turtle.forward()
elseif not movingForward and currentLength ~= length then
turtle.back()
end
end
if currentHeight ~= height then
turtle.up()
end
movingForward = not movingForward
end
-- done building wall; move to end position
if movingForward then
-- turtle is near the start position
for currentLength = 1, length do
turtle.forward()
end
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()
return true
end