Skip to content

Instantly share code, notes, and snippets.

@lukacat10
Last active September 15, 2021 13:53
Show Gist options
  • Save lukacat10/935bdb44d7ff1034a7debc2ca1e0f866 to your computer and use it in GitHub Desktop.
Save lukacat10/935bdb44d7ff1034a7debc2ca1e0f866 to your computer and use it in GitHub Desktop.
fuel managment + support for canola, rice, beetroot
--os.loadAPI(shell.resolve(".").."/gameutils")
local fuels = {
["minecraft:coal"] = true
}
local mat_to_color = {
["minecraft:wheat"] = "1",
["minecraft:carrots"] = "2",
["simplefarming:carrot_crop"] = "2",
["immersiveengineering:hemp"] = "3",
["immersiveengineering:seed"] = "3",
["actuallyadditions:block_canola"] = "4",
["actuallyadditions:item_canola_seed"] = "4",
["actuallyadditions:block_rice"] = "5",
["actuallyadditions:item_rice_seed"] = "5",
["minecraft:beetroots"] = "6",
["minecraft:beetroot_seeds"] = "6"
}
local color_to_mat = {
["1"] = "minecraft:wheat_seeds",
["2"] = "minecraft:carrot",
["3"] = "immersiveengineering:seed",
["4"] = "actuallyadditions:item_canola_seed",
["5"] = "actuallyadditions:item_rice_seed",
["6"] = "minecraft:beetroot_seeds"
}
local required_fuel = 0
local farm_layout = {}
local loaded = false
local layoutfile = "layout.nfp"
function load_layout_from_file()
if fs.exists(layoutfile) == true then
local h = fs.open(layoutfile, "r")
for i=9,1,-1 do
farm_layout[i] = {}
line = h.readLine()
if line ~= nil then
for j=1,#line do
farm_layout[i][j] = line:sub(j,j)
end
end
end
loaded = true
h.close()
end
end
load_layout_from_file()
local_x = 0
local_y = 0
local_z = 1
local_facing = "x+"
x = 0
y = 0
z = 0
facing = "east"
function get_color_to_mat(color)
if color == nil then
return nil
end
return color_to_mat[color]
end
function get_from_layout(myy,myx)
if farm_layout[myy] ~= nil and farm_layout[myy][myx] ~= nil then
return farm_layout[myy][myx]
end
return nil
end
function select_by_name(name)
if name == nil then
return -1
end
for i=1,16 do
data = turtle.getItemDetail(i)
if data and data.name == name then
return i
end
end
return -1
end
function up()
required_fuel = required_fuel + 1
turtle.up()
y = y + 1
local_y = local_y + 1
end
function down()
required_fuel = required_fuel + 1
turtle.down()
y = y - 1
local_y = local_y - 1
end
function right()
if facing == "east" then
facing = "south"
elseif facing == "south" then
facing = "west"
elseif facing == "west" then
facing = "north"
elseif facing == "north" then
facing = "east"
end
if local_facing == "x+" then
local_facing = "z+"
elseif local_facing == "z+" then
local_facing = "x-"
elseif local_facing == "x-" then
local_facing = "z-"
elseif local_facing == "z-" then
local_facing = "x+"
end
turtle.turnRight()
end
function left()
if facing == "east" then
facing = "north"
elseif facing == "north" then
facing = "west"
elseif facing == "west" then
facing = "south"
elseif facing == "south" then
facing = "east"
end
if local_facing == "x+" then
local_facing = "z-"
elseif local_facing == "z-" then
local_facing = "x-"
elseif local_facing == "x-" then
local_facing = "z+"
elseif local_facing == "z+" then
local_facing = "x+"
end
turtle.turnLeft()
end
function get_forward()
my_x = local_x
my_z = local_z
if local_facing == "x+" then
my_x = my_x + 1
elseif local_facing == "x-" then
my_x = my_x - 1
elseif local_facing == "z-" then
my_z = my_z - 1
elseif local_facing == "z+" then
my_z = my_z + 1
end
return my_x, my_z
end
function forward()
if facing == "east" then
x = x + 1
elseif facing == "west" then
x = x - 1
elseif facing == "north" then
z = z - 1
elseif facing == "south" then
z = z + 1
end
if local_facing == "x+" then
local_x = local_x + 1
elseif local_facing == "x-" then
local_x = local_x - 1
elseif local_facing == "z-" then
local_z = local_z - 1
elseif local_facing == "z+" then
local_z = local_z + 1
end
required_fuel = required_fuel + 1
turtle.forward()
end
function forward_break()
a, b = turtle.inspect()
if a == true then
turtle.dig()
end
forward()
end
function scan_cane()
a, b = turtle.inspect()
while a == true and b.name == "immersiveengineering:hemp" do
up()
a, b = turtle.inspect()
end
forward()
while y > 1 do
turtle.digDown()
down()
end
end
function scan_cane_column(amount)
for i=1,amount do
scan_cane()
end
end
function scan_wheat_no_forward()
a,b = turtle.inspectDown()
if a == true and mat_to_color[b.name] ~= nil then
if farm_layout[local_x] == nil then
farm_layout[local_x] = {}
end
farm_layout[local_x][local_z] = mat_to_color[b.name]
--print("x: "..local_x.." z: "..local_z)
end
--if a == false then
-- turtle.placeDown()
--end
if a == true and (((b.name == "simplefarming:carrot_crop" or b.name == "minecraft:carrots" or b.name == "minecraft:wheat" or b.name == "minecraft:wheat" or b.name == "actuallyadditions:block_canola" or b.name == "actuallyadditions:block_rice") and b.state.age == 7) or (b.name == "minecraft:beetroots" and b.state.age == 3)) then
--previousSelection = turtle.getSelectedSlot()
turtle.select(1)
turtle.digDown()
--turtle.select(previousSelection)
end
slot_for_placing = select_by_name(get_color_to_mat(get_from_layout(local_x,local_z)))
print("<>")
print(get_from_layout(local_x,local_z))
print(type(get_from_layout(local_x,local_z)))
print(get_color_to_mat(get_from_layout(local_x,local_z)))
print(slot_for_placing)
if slot_for_placing ~= -1 then
turtle.select(slot_for_placing)
turtle.placeDown()
end
end
function scan_hemp_no_forward()
a,b = turtle.inspect()
if a == true and b.name == "immersiveengineering:hemp" then
myx,myz = get_forward()
if farm_layout[myx] == nil then
farm_layout[myx] = {}
end
farm_layout[myx][myz] = mat_to_color["immersiveengineering:hemp"]
previousSelection = turtle.getSelectedSlot()
turtle.select(1)
turtle.dig()
turtle.select(previousSelection)
end
end
function scan_wheat()
scan_hemp_no_forward()
forward()
scan_hemp_no_forward()
scan_wheat_no_forward()
end
function scan_wheat_column(amount)
for i=1,amount do
scan_wheat()
end
end
function setup()
print("This turtle will farm a 9x9 farm. Place it so it faces the bottom left corp of the farm.")
print("-----------Setup Info-----------")
print("1. Ensure a chest is below the turtle")
print("2. Place fuel in the first slot.")
print("3. Place crops to plant in the farm's columns on each of the next slots (2-10) - Optional")
print("4. Place chest in bottom right (slot 16)")
print("--------------------------------")
turtle.select(16)
detected, info = turtle.inspectDown()
chestCount = turtle.getItemCount(16)
slot16Info = turtle.getItemDetail(16)
if (detected and info.name ~= "minecraft:chest") and (chestCount == 0 or slot16Info.name ~= "minecraft:chest") then
print("No chest in slot 16 or below. Bye!")
return false
end
if detected == false then
turtle.select(16)
turtle.placeDown()
end
--Refueling:
if turtle.getFuelLevel() < 100 then
print("Please refuel the turtle!")
end
turtle.select(1)
while true do
if turtle.getFuelLevel() >= 100 then
break
end
local valid = turtle.refuel(0)
if not valid then
print("No fuel in 1 slot, please insert fuel and press enter!")
read()
else
turtle.refuel(1)
end
end
return true
end
function wait_till_next_harvest()
secs = math.random(1200,3600)
print("Sleeping for about 1200 to 3600 seconds (FUCK YOU CALCULATE IT YOURSELF!)")
os.sleep(secs)
end
function scan()
turtle.select(2)
up()
scan_wheat_column(9)
right()
turtle.select(3)
scan_wheat()
right()
scan_wheat_column(8)
left()
turtle.select(4)
scan_wheat()
left()
scan_wheat_column(8)
right()
turtle.select(5)
scan_wheat()
right()
scan_wheat_column(8)
--up()
--forward()
--forward()
--down()
--scan_wheat_no_forward()
--scan_wheat_column(3)
left()
turtle.select(6)
scan_wheat()
left()
scan_wheat_column(8)
--up()
--up()
--forward()
--forward()
--down()
--down()
--scan_wheat_no_forward()
--scan_wheat_column(3)
right()
turtle.select(7)
scan_wheat()
right()
scan_wheat_column(8)
left()
turtle.select(8)
scan_wheat()
left()
scan_wheat_column(8)
right()
turtle.select(9)
scan_wheat()
right()
scan_wheat_column(8)
left()
turtle.select(10)
scan_wheat()
left()
scan_wheat_column(8)
right()
right()
for i=1,8 do
forward_break()
end
forward()
down()
right()
for i=1,8 do
forward()
end
right()
end
function unload_to_chest()
a,b = turtle.inspectDown()
if a == true and b.name == "minecraft:chest" then
for i=1,16 do
turtle.select(i)
turtle.dropDown()
end
end
end
function calculate_required_items()
required = {}
for i=1,9 do
for j=1,9 do
mat = get_color_to_mat(get_from_layout(i, j))
if mat ~= nil then
if required[mat] == nil then
required[mat] = 1
else
required[mat] = required[mat] + 1
end
end
end
end
return required
end
function load_from_chest()
required = calculate_required_items()
chest = peripheral.wrap("bottom")
chestItems = chest.list()
for i=1,#chestItems do
if fuels[chestItems[i].name] == true then
chest.pushItems("up", i)
end
for k, v in pairs(required) do
if v > 0 and chestItems[i].name == k then
chest.pushItems("up", i)
required[k] = required[k] - chestItems[i].count
end
end
end
end
function save_to_file()
local h = fs.open(layoutfile, "w")
for i=1,9 do
line = ""
for j=1,9 do
color = get_from_layout(10 - i, j)
if color == nil then
line = line.." "
else
line = line..color
end
end
h.writeLine(line)
end
h.close()
loaded = true
end
if setup() == true then
while true do
scan()
unload_to_chest()
load_from_chest()
shell.run("refuel 64")
print('fuel used during last round: '..required_fuel)
while turtle.getFuelLevel() < required_fuel do
print("Not enough fuel, insert fuel in chest!")
unload_to_chest()
load_from_chest()
shell.run("refuel 64")
end
required_fuel = 0
save_to_file()
wait_till_next_harvest()
load_layout_from_file()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment