EDIT: Also, if you know how to make my code shorter, tell me please :D/>
the turtle:
Spoiler
--Vars
local a = 0
local sIndex = 1
local material = 1
local util = 1
local item = 1
local state = 1
--FUNCTIONS
local function forward(num)
for i = 1,num do
turtle.forward()
end
end
local function shiftLeft(num)
turtle.turnLeft()
for i = 1,num do
turtle.forward()
end
turtle.turnRight()
end
local function shiftRight(num)
turtle.turnRight()
for i = 1,num do
turtle.forward()
end
turtle.turnLeft()
end
local function VertMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == sIndex then x = x-1 end;
term.setCursorPos(x,y);
if i == sIndex then term.write("[") end;
term.write(tableofItems[i]);
if i == sIndex then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then sIndex = sIndex +1 end;
if s == 200 then sIndex = sIndex -1 end;
if sIndex > #tableofItems then sIndex = 1 end;
if sIndex < 1 then sIndex = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[sIndex]
end
end
end
local function materialMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == material then x = x-1 end;
term.setCursorPos(x,y);
if i == material then term.write("[") end;
term.write(tableofItems[i]);
if i == material then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then material = material +1 end;
if s == 200 then material = material -1 end;
if material > #tableofItems then material = 1 end;
if material < 1 then material = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[material]
end
end
end
local function ItemMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == item then x = x-1 end;
term.setCursorPos(x,y);
if i == item then term.write("[") end;
term.write(tableofItems[i]);
if i == item then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then item = item +1 end;
if s == 200 then item = item -1 end;
if item > #tableofItems then item = 1 end;
if item < 1 then item = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[item]
end
end
end
local function UtilityMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == util then x = x-1 end;
term.setCursorPos(x,y);
if i == util then term.write("[") end;
term.write(tableofItems[i]);
if i == util then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then util = util +1 end;
if s == 200 then util = util -1 end;
if util > #tableofItems then util = 1 end;
if util < 1 then util = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[item]
end
end
end
local function category()
ItemMenu({"Tools", "Utility","Rednet"})
if item == 1 then
VertMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
materialMenu({"Diamond", "Gold", "Iron", "Stone", "Wood"})
elseif item == 2 then
UtilityMenu({"Furnace","Crafting Table", "Chest"})
end
end
local function fuel()
fuel = turtle.getFuelLevel()
print("Current fuel level: "..fuel)
if fuel == 0 then
print("No fuel! Put some coal in slot 16!")
end
end
local function prompt()
if (item == 1) and (item == 2) then
write("Quantity: ")
number = read()
number = tonumber(number)
end
turtle.select(16)
turtle.refuel()
turtle.select(1)
if item == 1 then
tools()
elseif item == 2 then
utility()
end
end
local function tools()
if sIndex == 1 then
forward(1)
turtle.turnLeft()
forward(1)
turtle.suck()
turtle.drop(turtle.getItemCount(1) - number)
turtle.transferTo(10, number)
materialTools()
turtle.suck()
turtle.drop(turtle.getItemCount(1) - number * 2)
turtle.transferTo(6, number)
turtle.transferTo(2, number)
elseif sIndex == 2 then
handle()
materialTools()
turtle.suck()
turtle.drop(turtle.getItemCount(1) - number)
turtle.transferTo(2, number)
elseif sIndex == 3 then
handle()
materialTools()
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 3 * number)
turtle.transferTo(2, number)
turtle.transferTo(3, number)
elseif sIndex == 4 then
handle()
materialTools()
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 3 * number)
turtle.transferTo(2, number)
turtle.transferTo(3, number)
turtle.transferTo(7, number)
elseif sIndex == 5 then
handle()
materialTools()
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 2 * number)
turtle.transferTo(2, number)
turtle.transferTo(3, number)
end
end
local function materialTools()
if material == 1 then -- Diamond
turtle.up()
forward(1)
elseif material == 2 then -- Gold
shiftRight(3)
elseif material == 3 then -- Iron
shiftRight(3)
turtle.up()
forward(1)
elseif material == 4 then -- Stone
shiftRight(6)
elseif material == 5 then -- Wood
shiftRight(6)
turtle.up()
forward(1)
end
end
local function utility()
if util == 1 then
forward(1)
turtle.turnLeft()
forward(1)
shiftRight(6)
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 8 * number)
for i = 1,11 do
if (i ~= 4) and (i ~= 6) and (i ~= 8) then
turtle.transferTo(i, number)
end
end
elseif util == 2 then
forward(1)
turtle.turnLeft()
forward(1)
shiftRight(6)
turtle.up()
forward(1)
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 4 * number)
for i = 1,6 do
if (i ~=3) and (i ~=4) then
turtle.transferTo(i, number)
end
end
elseif util == 3 then
forward(1)
turtle.turnLeft()
forward(1)
shiftRight(6)
turtle.up()
forward(1)
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 8 * number)
for i = 1,11 do
if (i ~=4) and (i ~= 6) and (i ~= 8) then
turtle.transferTo(i,number)
end
end
end
end
local function handle()
forward(1)
turtle.turnLeft()
forward(1)
turtle.suck()
turtle.drop(turtle.getItemCount(1) - 2 * number)
turtle.transferTo(10, number)
turtle.transferTo(6, number)
end
local function craft()
for i = 1,number do
turtle.craft()
turtle.turnRight()
turtle.drop()
turtle.turnLeft()
end
end
local function reset()
if item == 1 then -- Tools
if material == 1 then
turtle.back()
turtle.down()
turtle.back()
turtle.turnRight()
turtle.back()
elseif material == 2 then
shiftLeft(3)
turtle.back()
turtle.turnRight()
turtle.back()
elseif material == 3 then
turtle.back()
turtle.down()
shiftLeft(3)
turtle.back()
turtle.turnRight()
turtle.back()
elseif material == 4 then
shiftLeft(6)
turtle.back()
turtle.turnRight()
turtle.back()
elseif material == 5 then
turtle.back()
turtle.down()
shiftLeft(6)
turtle.back()
turtle.turnRight()
turtle.back()
end --if
elseif item == 2 then
if util == 1 then
shiftLeft(6)
turtle.back()
turtle.turnRight()
turtle.back()
elseif (util == 2) or (util == 3) then
turtle.back()
turtle.down()
shiftLeft(6)
turtle.back()
turtle.turnRight()
turtle.back()
end
end
end
local function redn()
number = 1
if item == 3 then
rednet.open("right")
id,message = rednet.receive()
print("Waiting for command...")
if message == "sword" then
sIndex = 1
elseif message == "shovel" then
sIndex = 2
elseif message == "pickaxe" then
sIndex = 3
elseif message == "axe" then
sIndex = 4
elseif message == "hoe" then
sIndex = 5
end
sleep(2)
if message == "diamond" then
material = 1
elseif message == "gold" then
material = 2
elseif message == "iron" then
material = 3
elseif message == "stone" then
material = 4
elseif message == "wood" then
material = 5
end
if message == "furnace" then
util = 1
elseif message == "craftingtable" then
util = 2
elseif message == "chest" then
util = 3
end
end
end
local function main()
category()
redn()
fuel()
prompt()
reset()
craft()
term.clear()
term.setCursorPos(1,1)
end
main()
the computer:
Spoiler
local mode = 1
local craftmode = 1
local util = 1
local tool = 1
local material = 1
local function mainMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == mode then x = x-1 end;
term.setCursorPos(x,y);
if i == mode then term.write("[") end;
term.write(tableofItems[i]);
if i == mode then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then mode = mode +1 end;
if s == 200 then mode = mode -1 end;
if mode > #tableofItems then mode = 1 end;
if mode < 1 then mode = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[mode]
end
end
end
local function craftMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == craftmode then x = x-1 end;
term.setCursorPos(x,y);
if i == craftmode then term.write("[") end;
term.write(tableofItems[i]);
if i == craftmode then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then craftmode = craftmode +1 end;
if s == 200 then craftmode = craftmode -1 end;
if craftmode > #tableofItems then craftmode = 1 end;
if craftmode < 1 then craftmode = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[craftmode]
end
end
end
local function utilMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == util then x = x-1 end;
term.setCursorPos(x,y);
if i == util then term.write("[") end;
term.write(tableofItems[i]);
if i == util then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then util = util +1 end;
if s == 200 then util = util -1 end;
if util > #tableofItems then util = 1 end;
if util < 1 then util = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[util]
end
end
end
local function toolMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == tool then x = x-1 end;
term.setCursorPos(x,y);
if i == tool then term.write("[") end;
term.write(tableofItems[i]);
if i == tool then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then tool = tool +1 end;
if s == 200 then tool = tool -1 end;
if tool > #tableofItems then tool = 1 end;
if tool < 1 then tool = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[tool]
end
end
end
local function materialMenu(tableofItems)
local selection = "";
while (selection == "") do
term.clear();
local xMax, yMax = term.getSize();
local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
y = y - #tableofItems;
if y<1 then y = 1 end;
if #tableofItems > yMax then
print "ERROR: List does not fit on screen, reduce number of items in table";
return "";
end
for i = 1, #tableofItems do
if string.len(tableofItems[i]) >= xMax then
term.clear();
term.setCursorPos(1,1);
print (("ERROR: List item # "..i).." is too long please reduce");
return "";
end
local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
if i == material then x = x-1 end;
term.setCursorPos(x,y);
if i == material then term.write("[") end;
term.write(tableofItems[i]);
if i == material then term.write("]") end;
y = y+1;
end
local r,s = os.pullEvent("key");
if s == 208 then material = material +1 end;
if s == 200 then material = material -1 end;
if material > #tableofItems then material = 1 end;
if material < 1 then material = #tableofItems end;
if s == 28 then
term.clear();
term.setCursorPos(1,1);
return tableofItems[material]
end
end
end
function menus()
rednet.open("top")
mainMenu({"Craft items"})
craftMenu({"Tools","Utility"})
if craftmode == 1 then
toolMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
materialMenu({"Diamond","Gold","Iron","Stone","Wood"})
elseif craftmode == 2 then
utilMenu({"Furnace","Crafting table","Chest"})
end
end
function redn()
if craftmode == 1 then
if tool == 1 then
rednet.send(21, "sword")
elseif tool == 2 then
rednet.send(21, "shovel")
elseif tool == 3 then
rednet.send(21, "pickaxe")
elseif tool == 4 then
rednet.send(21, "axe")
elseif tool == 5 then
rednet.send(21, "hoe")
end
sleep(2)
if material == 1 then
rednet.send(21,"diamond")
elseif material == 2 then
rednet.send(21, "gold")
elseif material == 3 then
rednet.send(21,"iron")
elseif material == 4 then
rednet.send(21, "stone")
elseif material == 5 then
rednet.send(21, "wood")
end
elseif craftmode == 2 then
if util == 1 then
rednet.send(21,"furnace")
elseif util == 2 then
rednet.send(21, "craftingtable")
elseif util == 3 then
rednet.send(21, "chest")
end
end
end
function main()
menus()
redn()
end
main()