Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = "Furnace Station Beta 0.1.0"
- local turtleID = os.getComputerID()
- local computerID = 6
- local furnace = {}
- local coord = {}
- local needsFuel = true
- local durationFuel = 8
- local amountFurnace = 10
- local slotFinishedItems = {}
- local slotToBurnItems = {}
- w,h = term.getSize()
- rednet.open("right")
- --
- for i=1,14 do
- slotFinishedItems[i] = false
- end
- for i=1,12 do
- slotToBurnItems[i] = 0
- end
- -- coord[x|y|z|direction|extra]
- for i=1,5 do
- coord[i] = 0
- end
- -- furnace[number][aFuel|aItems]
- for i=1,amountFurnace do
- furnace[i]= {}
- for j=1,2 do
- furnace[i][j] = 0
- end
- end
- function printCentered(str, ypos)
- term.setCursorPos(w/2 - #str/2, ypos)
- term.write(str)
- end
- function printCopyright()
- local str = "by UNOBTANIUM"
- term.setCursorPos(w-#str, h)
- term.write(str)
- end
- function printHeader(title, line)
- printCentered(title, line)
- printCentered(string.rep("-", w), line+1)
- end
- function printReplace(str,line)
- printCentered(string.rep(" ", w), line)
- printCentered(str, line)
- end
- function clearScreen()
- term.clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- function countArray(array)
- local x = 0
- for k,v in pairs(array) do
- x = x + 1
- end
- return x
- end
- function sendMessage(sendingmessage)
- print("send: " .. sendingmessage)
- sendingmessage = tostring(sendingmessage)
- while true do
- repeat
- rednet.send(computerID,sendingmessage)
- os.startTimer(2)
- event, senderChannel, msg = os.pullEvent()
- until event == 'rednet_message'
- if senderChannel == computerID then
- return msg
- end
- end
- end
- function receiveMessage()
- while true do
- local senderChannel, msg = rednet.receive()
- if senderChannel == computerID then
- sleep(0.2)
- rednet.send(computerID,"successful")
- return msg
- end
- end
- end
- function loadVariables()
- if not fs.exists("fsVariables") then
- return false
- end
- local file = fs.open("fsVariables","r")
- computerID = tonumber(file.readLine())
- amountFurnace = tonumber(file.readLine())
- for i=1,14 do
- if file.readLine() == "true" then
- slotFinishedItems[i] = true else slotFinishedItems[i] = false end
- end
- for i=1,12 do
- slotToBurnItems[i] = tonumber(file.readLine())
- end
- for i=1,5 do
- coord[i] = tonumber(file.readLine())
- end
- for i=1,amountFurnace do
- for j=1,2 do
- furnace[i][j] = tonumber(file.readLine())
- end
- end
- file.close()
- return true
- end
- function saveVariables()
- local file = fs.open("fsVariables", "w")
- file.writeLine(computerID)
- file.writeLine(amountFurnace)
- for i=1,14 do
- file.writeLine(slotFinishedItems[i])
- end
- for i=1,12 do
- file.writeLine(slotToBurnItems[i])
- end
- for i=1,5 do
- file.writeLine(coord[i])
- end
- for i=1,amountFurnace do
- for j=1,2 do
- file.writeLine(furnace[i][j])
- end
- end
- file.close()
- end
- function changeCoord(a,b,c,d,e)
- coord[1] = a
- coord[2] = b
- coord[3] = c
- coord[4] = d
- coord[5] = e
- saveVariables()
- end
- -- changeCoord(coord[1],coord[2],coord[3],coord[4],coord[5])
- -- x horizontal
- -- y behind or directly above
- -- z height
- -- direction: 1 forward | 2 right | 3 back
- function moveTo(x,y,z,side)
- print("- move -")
- print("current: " .. coord[1] .. " " .. coord[2] .. " " .. coord[3] .. " " .. coord[4])
- print("go to: " .. x .. " " .. y " " .. z .. " " .. side)
- x = x-coord[1]
- z = z-coord[3]
- print("relative: " .. x .. " y " .. z)
- if z ~= 0 then
- print("prepare for changing height")
- if 0-coord[2] == -1 then
- turn(2)
- changeCoord(coord[1],0,coord[3],coord[4],coord[5])
- turtle.back()
- end
- end
- if z > 0 then
- print("change height")
- for i=1,z do
- changeCoord(coord[1],coord[2],coord[3]+1,coord[4],coord[5])
- turtle.up()
- end
- elseif z < 0 then
- z=-z
- for i=1,z do
- changeCoord(coord[1],coord[2],coord[3]-1,coord[4],coord[5])
- turtle.down()
- end
- end
- if not x == 0 then
- print("move x")
- if x > 0 then
- turn(1)
- for i=1,x do
- changeCoord(coord[1]+1,coord[2],coord[3],coord[4],coord[5])
- turtle.forward()
- end
- elseif x < 0 then
- turn(3)
- x=-x
- for i=1,x do
- changeCoord(coord[1]-1,coord[2],coord[3],coord[4],coord[5])
- turtle.forward()
- end
- end
- end
- y = y-coord[2]
- print("y: " .. y)
- if y == -1 then
- turn(2)
- changeCoord(coord[1],coord[2]+y,coord[3],coord[4],coord[5])
- turtle.back()
- elseif y == 1 then
- turn(2)
- changeCoord(coord[1],coord[2]+y,coord[3],coord[4],coord[5])
- turtle.forward()
- end
- turn(side)
- print("- move end- ")
- read()
- end
- function turn(side)
- print("- turn -")
- print("turn to side: " .. side)
- print("current side: " .. coord[4])
- while coord[4] ~= side do
- print("have to turn")
- if side == 1 then
- changeCoord(coord[1],coord[2],coord[3],coord[4]-1,coord[5])
- turtle.turnLeft()
- print("turn left")
- elseif side == 2 then
- if coord[4] == 1 then
- changeCoord(coord[1],coord[2],coord[3],2,coord[5])
- turtle.turnRight()
- print("turn right")
- elseif coord[4] == 3 then
- changeCoord(coord[1],coord[2],coord[3],2,coord[5])
- turtle.turnLeft()
- print("turn left")
- end
- elseif side == 3 then
- changeCoord(coord[1],coord[2],coord[3],coord[4]+1,coord[5])
- turtle.turnRight()
- print("turn right")
- end
- end
- print("- turn end -")
- read()
- end
- function dropFinishedItems()
- print("drop cooked items")
- moveTo(0,0,0,3)
- for i=1,14 do
- if slotFinishedItems[i] then
- turtle.select(i)
- while not turtle.drop() do sleep(5) end
- slotFinishedItems[i] = false
- saveVariables()
- end
- end
- end
- function suckToBurnItems()
- print("suck items")
- moveTo(0,1,1,3)
- turtle.select(1)
- for i=1,12 do
- while turtle.getItemCount(i) == 0 do
- if not turtle.suck() then break end
- end
- end
- for i=1,12 do
- if turtle.getItemCount(i) > 0 then
- slotToBurnItems[i] = turtle.getItemCount[i]
- end
- end
- saveVariables()
- end
- function getFuel()
- print("grabbing fuel")
- moveTo(0,-1,1,3)
- for i=15,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- for i=15,16 do
- turtle.select(15)
- while turtle.getItemCount(i) == 0 do
- turtle.suck()
- sleep(0.5)
- end
- end
- while turtle.getFuelLevel() <= 500 do
- turtle.select(16)
- turtle.refuel(1)
- end
- end
- function takeFinishedItemsOut(x)
- print("taking items out of the furnace")
- moveTo(x-1,0,0,2)
- local emptySlot = 0
- for i=1,14 do
- if turtle.getItemCount(i) == 0 then
- emptySlot = i
- break
- end
- end
- if emptySlot == 0 then
- print("no empty slot left")
- dropFinishedItems()
- takeFinishedItemsOut(x)
- return
- end
- print("taking items out")
- turtle.select(emptySlot)
- slotFinishedItems[emptySlot] = true
- local leftInFurnace = furnace[x][2]
- while furnace[x][2] > 0 do
- if turtle.suck() then
- furnace[x][2] = leftInFurnace - turtle.getItemCount(emptySlot)
- saveVariables()
- else
- sleep(1)
- end
- end
- end
- function checkInventory()
- print("checking inventory")
- local amountInUseSlots = 0
- local amountFinishedSlots = 0
- for i=1,14 do
- if slotFinishedItems[i] then
- amountFinishedSlots = amountFinishedSlots + 1
- end
- if i<= 12 and slotToBurnItems[i] > 0 then
- amountInUseSlots = amountInUseSlots + 1
- end
- end
- if amountFinishedSlots > amountInUseSlots then
- dropFinishedItems()
- suckToBurnItems()
- end
- if turtle.getFuelLevel() < 200 or turtle.getItemCount(15)+turtle.getItemCount(16) < 16 then
- getFuel()
- end
- end
- function dropCoalInFurnace(amount)
- print("droppping coal in the furnace")
- if turtle.getItemCount(15) >= amount then
- turtle.select(15)
- turtle.dropUp(amount)
- elseif turtle.getItemCount(16) >= amount then
- turtle.select(16)
- turtle.dropUp(amount)
- else
- turtle.select(15)
- turtle.transferTo(16, 64-turtle.getItemCount(16))
- turtle.select(16)
- turtle.dropUp(amount)
- end
- end
- function burn()
- print("burning items")
- local nextEmptyFurnace = 0
- for i=1,amountFurnace do
- if furnace[i][2] == 0 then
- nextEmptyFurnace = i
- break
- end
- end
- if nextEmptyFurnace == 0 then
- sleep(2)
- return
- end
- for i=1,12 do
- if slotToBurnItems[i] > 0 then
- if needsFuel and slotToBurnItems[i] < durationFuel then
- elseif needsFuel and slotToBurnItems[i] >= durationFuel then
- if math.floor(turtle.getItemCount(i)/durationFuel) > furnace[nextEmptyFurnace][1] then
- moveTo(nextEmptyFurnace-1,-1,-1,2)
- dropCoalInFurnace((math.floor(turtle.getItemCount(i)/durationFuel))-furnace[nextEmptyFurnace][1])
- furnace[nextEmptyFurnace][1] = (math.floor(turtle.getItemCount(i)/durationFuel))
- saveVariables()
- end
- moveTo(nextEmptyFurnace-1,1,1,2)
- turtle.select(i)
- turtle.dropDown(math.floor(slotToBurnItems[i]/durationFuel))
- furnace[nextEmptyFurnace][2] = math.floor(slotToBurnItems[i]/durationFuel)*durationFuel
- saveVariables()
- sendMessage(textutils.serialize({"new",nextEmptyFurnace, math.floor(slotToBurnItems[i]/durationFuel)*10}))
- return true
- elseif not needsFuel then
- moveTo(nextEmptyFurnace-1,1,1,2)
- turtle.select(i)
- turtle.dropDown(slotToBurnItems[i])
- furnace[nextEmptyFurnace][2] = slotToBurnItems[i]
- saveVariables()
- sendMessage(textutils.serialize({"new",nextEmptyFurnace, slotToBurnItems[i]*10}))
- return true
- end
- end
- end
- return false
- end
- function run()
- while true do
- checkInventory()
- sendMessage(textutils.serialize({"check",0,0}))
- local message = textutils.unserialize(receiveMessage())
- if message[1] == "furnaces are empty" then
- dropFinishedItems()
- suckToBurnItems()
- getFuel()
- burn()
- elseif message[1] == "finished" then
- takeFinishedItemsOut(message[2])
- elseif message[1] == "allmost done" then
- if message[3] > 90 then
- if not burn() then
- sleep(30)
- end
- elseif message[3] > 30 then
- for i=1,14 do
- if slotFinishedItems[i] then
- dropFinishedItems()
- break
- end
- end
- for i=1,12 do
- if turtle.getItemCount(i) == 0 then
- suckToBurnItems()
- break
- end
- end
- if needsFuel and turtle.getItemCount(15) + turtle.getItemCount(16) < 16 then
- getFuel()
- elseif turtle.getFuelLevel() < 200 then
- getFuel()
- end
- burn()
- elseif message[3] > 10 then
- burn()
- else
- takeFinishedItemsOut(message[2])
- end
- end
- end
- end
- coord[1] = 0
- coord[2] = 1
- coord[3] = -1
- coord[4] = 3
- loadVariables()
- run()
Advertisement
Add Comment
Please, Sign In to add comment