Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FURNACE: NEXUS
- TASKS:
- - checking fuel chest (light)
- -- if empty: check furnace fuel left (maybe disable single furnace)
- - check output chest (light)
- - check for input chest
- -- sort/count stacks (multiple options)
- -- if a low amount of stacks then split them up between all furnace (faster)
- -- check for same item types (same furncae)
- -- check for emtpy furnace or furnace who have low items in them (ALLMOST FINISHED FURNACE MARKED)
- -- create task and send it to slave (keep track till finished)
- - track furnace with timer if finished
- -- different furnace types
- -- amount of items
- - keep track of fuel in furnace
- -- refuel (all in one run)
- -- update displays
- ]]--
- local system = "my furnace";
- rednet.open("back");
- local timerID = os.startTimer(1);
- local furnaceCount = 8; -- 8, 16, 24, 32 ect on each side
- local furnaceSpeed = 10; -- seconds required to smelt a single item;
- local fuelValue = 80; -- can smelt for x seconds (coal 80, blaze rod 120)
- local fuelMultiplier = 1.0;
- local useFuel = true;
- local useOutput = true;
- local errorMessage = "";
- local menu = {
- {{"Which type of furnace is in use?"}, {"Standard Furnace","Iron Furnace","Custom"}, {{10,1},{8,1.25}}},
- {{"How should smelted items be handled?"},{"let turtle gather finished items"}, {"take care about items yourself"}, {true, false}},
- {{"How should fuel be handled?"}, {"let the turtle refuel the furnaces"}, {"take care about refueling yourself"}, {true, false}},
- {{"Which fuel type is in use?"}, {"Coal", "Blaze Rod", "Custom"}, {80, 120}},
- {{"Enter the name of this furnace station:"}}
- }
- function setSettings()
- local selected = 1;
- for i = 1, 5, 1 do
- end
- end
- function loadSettings()
- end
- local idleTimer = 0;
- local furnace = {};
- for x = 1, furnaceCount, 1 do
- table.insert(furnace, {["x"]=x, ["y"]=1, ["state"]="empty", ["fuel"]=16, ["timeFuel"]=0, ["finishedItems"]=0, ["queuedItems"]=0, ["timeItem"]=furnaceSpeed });
- table.insert(furnace, {["x"]=x, ["y"]=-1, ["state"]="empty", ["fuel"]=16, ["timeFuel"]=0, ["finishedItems"]=0, ["queuedItems"]=0, ["timeItem"]=furnaceSpeed });
- end
- local task = {};
- -- Save and Load
- function save()
- local file = fs.open("furnacestation","w");
- file.writeLine(string.gsub(textutils.serialize(furnace),"\n%S-",""));
- file.writeLine(system);
- file.writeLine(furnaceCount);
- file.writeLine(furnaceSpeed);
- file.writeLine(fuelValue);
- file.writeLine(fuelMultiplier);
- file.writeLine(tostring(useFuel));
- file.writeLine(tostring(useOutput));
- file.close();
- end
- function load()
- if not fs.exists("furnacestation") then return end
- local file = fs.open("furnacestation","r")
- furnace = textutils.unserialize(file.readLine())
- system = file.readLine()
- furnaceCount = tonumber(file.readLine())
- furnaceSpeed = tonumber(file.readLine())
- fuelValue = tonumber(file.readLine())
- fuelMultiplier = tonumber(file.readLine())
- if (tostring(file.readLine()) == "true") then
- useFuel = true
- else
- useFuel = false
- end
- if (tostring(file.readLine()) == "true") then
- useOutput = true
- else
- useOutput = false
- end
- file.close();
- end
- -- Task
- function isRefuelRunWorthIt()
- local amount = 0;
- local lowestAmount = 0;
- for i = 1, furnaceCount*2, 1 do
- local fuel = furnace[i].fuel;
- if (lowestAmount > fuel) then
- lowestAmount = fuel;
- end
- amount = amount + fuel;
- end
- if (lowestAmount < 40 or amount/(furnaceCount*2) < 55) then
- return true;
- end
- return false;
- end
- function sendTask()
- local cP = 9001;
- local cI = -1;
- for i, t in pairs(task) do
- if (t.priority <= cP) then -- find oldest task with highest priority
- cP = t.priority;
- cI = i;
- end
- end
- if (cI == -1) then
- if (useFuel and idleTimer == 5 and isRefuelRunWorthIt()) then
- sendMessage({["taskname"]="refuelall", ["furnacecount"]=furnaceCount}); -- nothing to do for a long time: refuel furnaces now if it is worth it
- else
- sendMessage({["taskname"]="checkitems"}); -- nothing to do. check for new items
- end
- idleTimer = idleTimer + 1;
- else -- refuel or retrieveitem
- sendMessage(table.remove(task, cI));
- idleTimer = 0;
- end
- end
- function updateFurnace(message)
- local i = message.furnacenumber;
- if (message.action == "addedFuel") then
- furnace[i].fuel = furnace[i].fuel + message.itemvalue;
- if (furnace[i].fuel > 64) then
- furnace[i].fuel = 64;
- end
- elseif (message.action == "addedItems") then
- furnace[i].queuedItems = furnace[i].queuedItems + message.itemvalue;
- if (furnace[i].queuedItems > 64) then
- furnace[i].queuedItems = 64;
- end
- elseif (message.action == "retrievedItems") then
- furnace[i].finishedItems = furnace[i].finishedItems - message.itemvalue;
- if (furnace[i].finishedItems < 0) then
- furnace[i].finishedItems = 0;
- end
- end
- end
- function addTask(taskname, furnacenumber, ...)
- if (useFuel and taskname == "refuel") then
- table.insert(task, {["taskname"]="refuel", ["priority"]=2, ["furnacenumber"]=furnacenumber, ["x"]=furnace[furnacenumber].x, ["y"]=furnace[furnacenumber].y, ["itemcount"]=0});
- elseif (useOutput and taskname == "retrieveitems") then
- table.insert(task, {["taskname"]="retrieveitems", ["priority"]=3, ["furnacenumber"]=furnacenumber, ["x"]=furnace[furnacenumber].x, ["y"]=furnace[furnacenumber].y, ["itemcount"]=arg[1]});
- end
- end
- function updateFurnaceTimer()
- term.clear();
- for i, t in pairs(task) do
- term.setCursorPos(23, i);
- term.write(t.taskname .. " " .. t.furnacenumber);
- end
- print(errorMessage);
- term.setCursorPos(1, 1);
- for i = 1, furnaceCount*2, 1 do
- print(" " .. i .. " " .. furnace[i].fuel .. " " .. furnace[i].queuedItems .. " " .. furnace[i].finishedItems .. " " .. furnace[i].state);
- if (useFuel and furnace[i].timeFuel > 0) then
- furnace[i].timeFuel = furnace[i].timeFuel - 1;
- end
- if (furnace[i].state == "empty") then
- if (furnace[i].queuedItems > 0) then
- furnace[i].state = "smelting";
- end
- elseif (furnace[i].state == "smelting") then
- if (furnace[i].finishedItems < 64) then
- furnace[i].timeItem = furnace[i].timeItem - 1;
- end
- if (furnace[i].timeItem == 0) then
- furnace[i].finishedItems = furnace[i].finishedItems + 1;
- furnace[i].queuedItems = furnace[i].queuedItems - 1;
- furnace[i].timeItem = furnaceSpeed;
- if (furnace[i].queuedItems == 0 and furnace[i].finishedItems > 0) then
- furnace[i].state = "finished";
- if (useOutput) then
- addTask("retrieveitems", i, furnace[i].finishedItems);
- else
- furnace[i].state = "empty";
- furnace[i].finishedItems = 0;
- end
- end
- end
- elseif (furnace[i].state == "finished") then
- if (furnace[i].finishedItems == 0) then
- furnace[i].state = "empty";
- end
- end
- if (useFuel and furnace[i].timeFuel == 0 and furnace[i].queuedItems > 0 and furnace[i].finishedItems < 64 and furnace[i].fuel > 0) then
- furnace[i].timeFuel = fuelValue*fuelMultiplier;
- furnace[i].fuel = furnace[i].fuel - 1;
- if (furnace[i].fuel <= 10) then
- local b = true;
- for j, t in pairs(task) do
- if (t.taskname == "refuel" and t.furnacenumber == i) then
- b = false;
- break;
- end
- end
- if (b) then
- addTask("refuel", i);
- end
- end
- end
- end
- end
- function checkTimer()
- timerID = os.startTimer(1);
- updateFurnaceTimer();
- end
- function findEmptyFurnace()
- for i = 1, furnaceCount*2, 1 do
- if (furnace[i].state == "empty") then
- sendMessage({["taskname"]="additems", ["furnacenumber"]=i, ["x"]=furnace[i].x, ["y"]=furnace[i].y});
- return;
- end
- end
- if (table.getn(task) > 0) then
- sendTask();
- else
- sendMessage({["taskname"]="noemptyfurnace"});
- end
- end
- function sendMessage(message)
- save();
- rednet.broadcast(textutils.serialize(message), system);
- end
- function checkMessage(message)
- if (message.state == "updatefurnace") then
- updateFurnace(message);
- elseif (message.state == "getemptyfurnace") then
- findEmptyFurnace();
- elseif (message.state == "idle") then
- sendTask();
- elseif (message.state == "error") then
- -- ouput chest full, fuel chest empty
- -- turn on redstone signal, toggle every second?
- errorMessage = message["errormessage"];
- end
- end
- function loop()
- while (true) do
- local e = { os.pullEvent() }
- if ( e[1] == "timer" and timerID == e[2] ) then
- checkTimer();
- save();
- elseif (e[1] == "rednet_message" and e[4] == system) then
- checkMessage(textutils.unserialize(e[3]));
- save();
- end
- end
- end
- load();
- loop();
Advertisement
Add Comment
Please, Sign In to add comment