Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local item = {}
- local sorter
- local unknownAmount = 0
- local sorterSide = "down"
- local chestSide = "north"
- local computerDirection = "north"
- local unknownDirection = "north"
- local direction = {["down"]=0,["up"]=1,["north"]=2,["south"]=3,["west"]=4,["east"]=5}
- local antiSide = {["down"]="up",["up"]="down",["north"]="south",["south"]="north",["west"]="east",["east"]="west"}
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function saveVar()
- local file = fs.open("SorterVar","w")
- file.writeLine(unknownAmount)
- file.writeLine(sorterSide)
- file.writeLine(chestSide)
- file.writeLine(computerDirection)
- file.writeLine(unknownDirection)
- file.writeLine(countArray(item))
- for k,v in pairs(item) do
- file.writeLine(k)
- for i=1,3 do
- file.writeLine(item[k][i])
- end
- end
- file.close()
- end
- function loadVar()
- if not fs.exists("SorterVar") then return false end
- local file = fs.open("SorterVar","r")
- unknownAmount = tonumber(file.readLine())
- sorterSide = file.readLine()
- chestSide = file.readLine()
- computerDirection = file.readLine()
- unknownDirection = file.readLine()
- local amount = file.readLine()
- for i=1,amount do
- local k = file.readLine()
- local a = file.readLine()
- local b = file.readLine()
- local c = tonumber(file.readLine())
- item[k] = {a,b,c}
- end
- file.close()
- return true
- end
- function addItem()
- clear()
- centered("NEW ITEM",1)
- centered("Tipe in the id plus metadata of the item", 4)
- centered("Example: " .. math.ceil(math.random()*3000) .. ":" .. math.ceil(math.random()*100) .. " or " .. math.ceil(math.random()*3000), 6)
- left("",20,8)
- local newId = read()
- clear()
- centered("NEW ITEM",1)
- centered("If you want to you can name it here.",4)
- centered("This name will be shown in your list.",5)
- centered("By just hitting Enter",6)
- centered("you just use the id as name",7)
- left("",15,9)
- local newName = read()
- if newName == "" then
- newName = newId
- end
- clear()
- centered("NEW ITEM",1)
- centered("In which direction should this item be send?",4)
- local newDirection = getDir()
- clear()
- centered("FINAL",1)
- centered("Do you want to add this item?",4)
- centered("Y/N",5)
- centered(("ID: " .. newId) ,8)
- centered(("Name: " .. newName) ,9)
- centered(("Direction: " .. newDirection) ,10)
- while true do
- local event = {os.pullEvent("char")}
- if event[2] == "y" then
- item[newId]={newName, newDirection, 0}
- saveVar()
- return true
- elseif event[2] == "n" then
- return false
- end
- end
- end
- function getRelDir()
- left("1 - Behind",20,10)
- left("2 - Right",20,11)
- left("3 - Front",20,12)
- left("4 - Left",20,13)
- while true do
- local event = {os.pullEvent("char")}
- local input = tonumber(event[2])
- if type(input) == "number" and input >= 1 and input <= 6 then
- if input == 1 then
- return "back"
- elseif input == 2 then
- return "right"
- elseif input == 3 then
- return "front"
- elseif input == 4 then
- return "left"
- end
- end
- end
- end
- function getDir()
- left("1 - Below",20,8)
- left("2 - Above",20,9)
- left("3 - Behind",20,10)
- left("4 - Right",20,11)
- left("5 - Front",20,12)
- left("6 - Left",20,13)
- while true do
- local event = {os.pullEvent("char")}
- local input = tonumber(event[2])
- if type(input) == "number" and input >= 1 and input <= 6 then
- if input == 1 then
- return "down"
- elseif input == 2 then
- return "up"
- elseif input == 3 then
- return antiSide[computerDirection]
- elseif input == 4 then
- if computerDirection == "north" then
- return "west"
- elseif computerDirection == "east" then
- return "north"
- elseif computerDirection == "south" then
- return "east"
- elseif computerDirection == "west" then
- return "south"
- end
- elseif input == 5 then
- return computerDirection
- elseif input == 6 then
- if computerDirection == "north" then
- return "east"
- elseif computerDirection == "east" then
- return "south"
- elseif computerDirection == "south" then
- return "west"
- elseif computerDirection == "west" then
- return "north"
- end
- end
- end
- end
- end
- function setSides()
- centered("COMPUTER FACING",1)
- centered("In which direction is the computer facing?",4)
- centered("Look in the direction the screen",5)
- centered("of the computer is facing,",6)
- centered("hit F3 and search for the direction name.",7)
- left("1 - North",20,9)
- left("2 - East",20,10)
- left("3 - South",20,11)
- left("4 - West",20,12)
- while true do
- local event = {os.pullEvent("char")}
- local input = tonumber(event[2])
- if type(input == "number") and input >= 1 and input <= 4 then
- if input == 1 then
- computerDirection = "north"
- elseif input == 2 then
- computerDirection = "east"
- elseif input == 3 then
- computerDirection = "south"
- elseif input == 4 then
- computerDirection = "west"
- end
- break
- end
- end
- clear()
- sleep(0.1)
- centered("SORTER SIDE",1)
- centered("On which side of the computer does the",4)
- centered("interactive sorter take place?",5)
- sorterSide = getRelDir()
- clear()
- sleep(0.1)
- centered("CHEST SIDE",1)
- centered("Imagine the interactive sorter is facing",4)
- centered("the same direction as the computer does.",5)
- centered("From which side should the interactive sorter",6)
- centered("take items out of the chest?",7)
- chestSide = getDir()
- clear()
- sleep(0.1)
- centered("UNKNOWN ITEM SIDE",1)
- centered("Where should all items go,",3)
- centered("which aren't listed by you?",4)
- centered("Once again, imagine the interactive sorter is",5)
- centered("facing in the same direction",6)
- centered("as the computer does!",7)
- unknownDirection = getDir()
- saveVar()
- clear()
- end
- function getID(uuid)
- if uuid > 32768 then
- meta = math.floor(uuid/32768)
- id = uuid%32768
- return ( id .. ":" .. meta )
- else
- return tostring(uuid)
- end
- end
- function countArray(a)
- local amount = 0
- for k,v in pairs(a) do
- amount = amount + 1
- end
- return amount
- end
- function centered(text,y,x)
- x = x or 25
- term.setCursorPos(x - #text/2, y)
- term.write(text)
- end
- function left(text,x,y)
- term.setCursorPos(x,y)
- term.write(text)
- end
- -- CHANGE ITEM
- function changeItem(choosenId)
- clear()
- centered("CHANGE ITEM",1)
- centered(("ID: " .. choosenId) ,4)
- centered(("Name: " .. item[choosenId][1]) ,5)
- centered(("Direction: " .. item[choosenId][2]) ,6)
- centered(("Amount: " .. item[choosenId][3] ),7)
- centered("How should this item be named?",9)
- centered("",10)
- local newName = read()
- clear()
- centered("CHANGE ITEM",1)
- centered(("ID: " .. choosenId) ,3)
- centered(("Name: " .. newName) ,4)
- centered(("Direction: " .. item[choosenId][2]) ,5)
- centered(("Amount: " .. item[choosenId][3] ),6)
- centered("Which side should the item be send?",7)
- local newDir = getDir()
- clear()
- centered("CHANGE ITEM",1)
- centered("Do you want to make this change?",4)
- centered("Y/N",5)
- while true do
- local event = {os.pullEvent("char")}
- if event[2] == "y" then
- item[choosenId]={newName, newDir, item[choosenId][3]}
- saveVar()
- return
- elseif event[2] == "n" then
- return
- end
- end
- end
- -- OVERVIEW
- function overview()
- clear()
- local mode = "CHANGE"
- local more = 0
- while true do
- clear()
- local amountItems = countArray(item)
- centered("OVERVIEW ITEMS " .. more+1 .. " TO " .. more+7,1)
- local counter = 1-more
- if more == 0 then
- centered("Not listed items: " .. unknownAmount, 3)
- end
- for k,v in pairs(item) do
- if counter >= 1 and counter <= 7 then
- left( counter .. " - " .. item[k][1] ,5,counter+3)
- left(item[k][2] ,36,counter+3)
- left(tostring(item[k][3]),43,counter+3)
- end
- counter = counter + 1
- end
- left("8 - Mode: " .. mode ,15,15)
- if more > 0 and more+7 >= amountItems then
- left("9 - Back to the first items",15,16)
- elseif more+7 < amountItems then
- left("9 - More items",15,16)
- end
- left("0 - Back to the menu",15,17)
- local event = {os.pullEvent("char")}
- local input = tonumber(event[2])
- if type(input) == "number" then
- if input == 8 then
- if mode == "CHANGE" then
- mode = "DELETE"
- elseif mode == "DELETE" then
- mode = "CHANGE"
- end
- elseif input == 9 then
- if more > 0 and more+7 >= amountItems then
- more = 0
- elseif more+7 < amountItems then
- more = more + 7
- end
- elseif input == 0 then
- return
- elseif input >= 1 and input <= 7 then
- if more+input <= amountItems then
- local choosenId = ""
- local anotherCounter = 1
- for k,v in pairs(item) do
- if anotherCounter == more+input then
- choosenId = k
- break
- end
- anotherCounter = anotherCounter + 1
- end
- if mode == "CHANGE" then
- changeItem(choosenId)
- elseif mode == "DELETE" then
- item[choosenId] = nil
- saveVar()
- end
- end
- end
- end
- end
- end
- -- MENU
- function menu()
- while true do
- clear()
- centered("NEXT GENERATION DIAMOND PIPE",1)
- left("1 - Start",10,4)
- left("2 - Overview",10,5)
- left("3 - Add specific item",10,6)
- left("4 - Change settings",10,7)
- left("by UNOBTANIUM",37,19)
- local event = {os.pullEvent("char")}
- if event[2] == "1" then
- return
- elseif event[2] == "2" then
- overview()
- elseif event[2] == "3" then
- addItem()
- elseif event[2] == "4" then
- setVar()
- end
- end
- end
- -- RUN
- function run()
- local inventory = sorter.list(direction[chestSide])
- if countArray(inventory) == 0 then
- return false
- end
- local chest = {}
- for k,v in pairs(inventory) do
- if type(chest[k]) == "nil" then
- chest[k] = v
- else
- chest[k] = chest[k] + v
- end
- end
- for k,v in pairs(chest) do
- local itemID = getID(k)
- if type(item[itemID]) == "table" then
- sorter.extract(direction[chestSide],k,direction[item[getID(k)][2]],v)
- item[itemID] = {item[itemID][1],item[itemID][2],item[itemID][3]+v}
- else
- sorter.extract(direction[chestSide],k,direction[unknownDirection],v)
- unknownAmount = unknownAmount + v
- end
- end
- saveVar()
- return true
- end
- clear()
- if not loadVar() then
- setSides()
- end
- sorter = peripheral.wrap(sorterSide)
- clear()
- centered("NEXT GENERATION DIAMOND PIPE",1)
- left("1 - Start",10,4)
- left("2 - Overview",10,5)
- left("3 - Add specific item",10,6)
- left("4 - Change settings",10,7)
- centered("RUNNING",9)
- left("by UNOBTANIUM",37,19)
- while true do
- if run() then
- os.startTimer(1.5)
- else
- os.startTimer(5)
- end
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" then
- break
- elseif event[1] == "key" then
- menu()
- centered("RUNNING",9)
- break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement