Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = "Furnace Sation Beta 0.1.0"
- local computerID = os.getComputerID()
- local turtleID = 7
- local amountFurnace = 10
- local timer = {}
- local finished = {}
- w,h = term.getSize()
- rednet.open("back")
- -- timer[furnacenumber]
- for i=1,amountFurnace do
- timer[i] = 0
- finished[i] = false
- print(timer[i])
- 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)
- sendingmessage = tostring(sendingmessage)
- while true do
- repeat
- rednet.send(turtleID,sendingmessage)
- os.startTimer(2)
- event, id, msg = os.pullEvent()
- until event == 'rednet_message'
- if id == turtleID then
- return msg
- end
- end
- end
- function receiveMessage()
- while true do
- local senderChannel, msg = rednet.receive()
- if senderChannel == turtleID then
- sleep(0.2)
- rednet.send(turtleID,"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,amountFurnace do
- timer[i] = tonumber(file.readLine())
- local line = file.readLine()
- if line == "true" then
- finished[i] = true else finished[i] = false end
- end
- file.close()
- return true
- end
- function saveVariables()
- local file = fs.open("fsVariables", "w")
- file.writeLine(computerID)
- file.writeLine(amountFurnace)
- for a=1,amountFurnace do
- file.writeLine(timer[i])
- file.writeLine(finished[i])
- end
- file.close()
- end
- function countDown()
- for i=1,amountFurnace do
- if timer[i] > 1 then
- timer[i] = timer[i] - 1
- elseif timer[i] == 1 then
- timer[i] = 0
- finished[i] = true
- end
- end
- end
- function check()
- local furnacenumber = 0
- for i=1,amountFurnace do
- if finished[i] then
- furnacenumber = i
- break
- end
- end
- if furnacenumber > 0 then
- local message = {"finished",furnacenumber,0}
- sendMessage(textutils.serialize(message))
- return
- end
- local timeLeft = 90000
- for i=1,amountFurnace do
- print(timer[i])
- if timer[i] > 0 and timeLeft > timer[i] then
- timeLeft = timer[i]
- furnacenumber = i
- end
- end
- if furnacenumber > 0 then
- local message = {"allmost done",furnacenumber, timeLeft}
- sendMessage(textutils.serialize(message))
- return
- end
- sendMessage(textutils.serialize{"furnaces are empty",0,0})
- end
- function run()
- while true do
- if countArray(timer) > 0 then
- os.startTimer(1)
- end
- local event, id, m = os.pullEvent()
- if event == "timer" then
- countDown()
- elseif event == "rednet_message" then
- print(m)
- if m[1] == "new" then
- print("new: " ..m[2] .. " " .. m[3])
- timer[m[2]] = m[3]
- elseif m[1] == "done" then
- print("done: " .. m[2])
- finished[m[2]] = false
- end
- print("check")
- check()
- end
- saveVariables()
- end
- end
- loadVariables()
- run()
Advertisement
Add Comment
Please, Sign In to add comment