UNOBTANIUM

FurnaceStation Computer

Jun 11th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.54 KB | None | 0 0
  1. --[[
  2.     FURNACE: NEXUS
  3.  
  4.  
  5. TASKS:
  6. - checking fuel chest (light)
  7. -- if empty: check furnace fuel left (maybe disable single furnace)
  8.  
  9. - check output chest (light)
  10.  
  11. - check for input chest
  12. -- sort/count stacks (multiple options)
  13. -- if a low amount of stacks then split them up between all furnace (faster)
  14. -- check for same item types (same furncae)
  15. -- check for emtpy furnace or furnace who have low items in them (ALLMOST FINISHED FURNACE MARKED)
  16. -- create task and send it to slave (keep track till finished)
  17.  
  18. - track furnace with timer if finished
  19. -- different furnace types
  20. -- amount of items
  21.  
  22. - keep track of fuel in furnace
  23. -- refuel (all in one run)
  24.  
  25. -- update displays
  26.  
  27.  
  28.  
  29.  
  30. ]]--
  31.  
  32. local system = "my furnace";
  33. rednet.open("back");
  34. local timerID = os.startTimer(1);
  35.  
  36. local furnaceCount = 8; -- 8, 16, 24, 32 ect on each side
  37. local furnaceSpeed = 10; -- seconds required to smelt a single item;
  38. local fuelValue = 80; -- can smelt for x seconds (coal 80, blaze rod 120)
  39. local fuelMultiplier = 1.0;
  40.  
  41. local useFuel = true;
  42. local useOutput = true;
  43.  
  44. local errorMessage = "";
  45.  
  46. local menu = {
  47.         {{"Which type of furnace is in use?"}, {"Standard Furnace","Iron Furnace","Custom"}, {{10,1},{8,1.25}}},
  48.         {{"How should smelted items be handled?"},{"let turtle gather finished items"}, {"take care about items yourself"}, {true, false}},
  49.         {{"How should fuel be handled?"}, {"let the turtle refuel the furnaces"}, {"take care about refueling yourself"}, {true, false}},
  50.         {{"Which fuel type is in use?"}, {"Coal", "Blaze Rod", "Custom"}, {80, 120}},
  51.         {{"Enter the name of this furnace station:"}}
  52. }
  53.  
  54.  
  55. function setSettings()
  56.     local selected = 1;
  57.     for i = 1, 5, 1 do
  58.        
  59.     end
  60. end
  61.  
  62. function loadSettings()
  63.  
  64. end
  65.  
  66.  
  67. local idleTimer = 0;
  68. local furnace = {};
  69. for x = 1, furnaceCount, 1 do
  70.     table.insert(furnace, {["x"]=x, ["y"]=1, ["state"]="empty", ["fuel"]=16, ["timeFuel"]=0, ["finishedItems"]=0, ["queuedItems"]=0, ["timeItem"]=furnaceSpeed });
  71.     table.insert(furnace, {["x"]=x, ["y"]=-1, ["state"]="empty", ["fuel"]=16, ["timeFuel"]=0, ["finishedItems"]=0, ["queuedItems"]=0, ["timeItem"]=furnaceSpeed });
  72. end
  73.  
  74.  
  75.  
  76. local task = {};
  77.  
  78.  
  79. -- Save and Load
  80.  
  81. function save()
  82.     local file = fs.open("furnacestation","w");
  83.         file.writeLine(string.gsub(textutils.serialize(furnace),"\n%S-",""));
  84.         file.writeLine(system);
  85.         file.writeLine(furnaceCount);
  86.         file.writeLine(furnaceSpeed);
  87.         file.writeLine(fuelValue);
  88.         file.writeLine(fuelMultiplier);
  89.         file.writeLine(tostring(useFuel));
  90.         file.writeLine(tostring(useOutput));
  91.     file.close();
  92. end
  93.  
  94. function load()
  95.     if not fs.exists("furnacestation") then return end
  96.     local file = fs.open("furnacestation","r")
  97.         furnace = textutils.unserialize(file.readLine())
  98.         system = file.readLine()
  99.         furnaceCount = tonumber(file.readLine())
  100.         furnaceSpeed = tonumber(file.readLine())
  101.         fuelValue = tonumber(file.readLine())
  102.         fuelMultiplier = tonumber(file.readLine())
  103.         if (tostring(file.readLine()) == "true") then
  104.             useFuel = true
  105.         else
  106.             useFuel = false
  107.         end
  108.         if (tostring(file.readLine()) == "true") then
  109.             useOutput = true
  110.         else
  111.             useOutput = false
  112.         end
  113.     file.close();
  114. end
  115.  
  116.  
  117.  
  118. -- Task
  119.  
  120. function isRefuelRunWorthIt()
  121.     local amount = 0;
  122.     local lowestAmount = 0;
  123.     for i = 1, furnaceCount*2, 1 do
  124.         local fuel = furnace[i].fuel;
  125.         if (lowestAmount > fuel) then
  126.             lowestAmount = fuel;
  127.         end
  128.         amount = amount + fuel;
  129.     end
  130.     if (lowestAmount < 40 or amount/(furnaceCount*2) < 55) then
  131.         return true;
  132.     end
  133.     return false;
  134. end
  135.  
  136.  
  137. function sendTask()
  138.     local cP = 9001;
  139.     local cI = -1;
  140.     for i, t in pairs(task) do
  141.         if (t.priority <= cP) then -- find oldest task with highest priority
  142.             cP = t.priority;
  143.             cI = i;
  144.         end
  145.     end
  146.     if (cI == -1) then
  147.         if (useFuel and idleTimer == 5 and isRefuelRunWorthIt()) then
  148.             sendMessage({["taskname"]="refuelall", ["furnacecount"]=furnaceCount}); -- nothing to do for a long time: refuel furnaces now if it is worth it
  149.         else
  150.             sendMessage({["taskname"]="checkitems"}); -- nothing to do. check for new items
  151.         end
  152.         idleTimer = idleTimer + 1;
  153.     else -- refuel or retrieveitem
  154.         sendMessage(table.remove(task, cI));
  155.         idleTimer = 0;
  156.     end
  157. end
  158.  
  159. function updateFurnace(message)
  160.     local i = message.furnacenumber;
  161.     if (message.action == "addedFuel") then
  162.         furnace[i].fuel = furnace[i].fuel + message.itemvalue;
  163.         if (furnace[i].fuel > 64) then
  164.             furnace[i].fuel = 64;
  165.         end
  166.     elseif (message.action == "addedItems") then
  167.         furnace[i].queuedItems = furnace[i].queuedItems + message.itemvalue;
  168.         if (furnace[i].queuedItems > 64) then
  169.             furnace[i].queuedItems = 64;
  170.         end
  171.     elseif (message.action == "retrievedItems") then
  172.         furnace[i].finishedItems = furnace[i].finishedItems - message.itemvalue;
  173.         if (furnace[i].finishedItems < 0) then
  174.             furnace[i].finishedItems = 0;
  175.         end
  176.     end
  177. end
  178.  
  179. function addTask(taskname, furnacenumber, ...)
  180.     if (useFuel and taskname == "refuel") then
  181.         table.insert(task, {["taskname"]="refuel", ["priority"]=2, ["furnacenumber"]=furnacenumber, ["x"]=furnace[furnacenumber].x, ["y"]=furnace[furnacenumber].y, ["itemcount"]=0});
  182.     elseif (useOutput and taskname == "retrieveitems") then
  183.         table.insert(task, {["taskname"]="retrieveitems", ["priority"]=3, ["furnacenumber"]=furnacenumber, ["x"]=furnace[furnacenumber].x, ["y"]=furnace[furnacenumber].y, ["itemcount"]=arg[1]});
  184.     end
  185. end
  186.  
  187.  
  188. function updateFurnaceTimer()
  189.     term.clear();
  190.     for i, t in pairs(task) do
  191.         term.setCursorPos(23, i);
  192.         term.write(t.taskname .. " " .. t.furnacenumber);
  193.     end
  194.     print(errorMessage);
  195.     term.setCursorPos(1, 1);
  196.     for i = 1, furnaceCount*2, 1 do
  197.         print(" " .. i .. " " .. furnace[i].fuel .. " " .. furnace[i].queuedItems .. " " .. furnace[i].finishedItems .. " " .. furnace[i].state);
  198.         if (useFuel and furnace[i].timeFuel > 0) then
  199.             furnace[i].timeFuel = furnace[i].timeFuel - 1;
  200.         end
  201.  
  202.         if (furnace[i].state == "empty") then
  203.             if (furnace[i].queuedItems > 0) then
  204.                 furnace[i].state = "smelting";
  205.             end
  206.         elseif (furnace[i].state == "smelting") then
  207.             if (furnace[i].finishedItems < 64) then
  208.                 furnace[i].timeItem = furnace[i].timeItem - 1;
  209.             end
  210.             if (furnace[i].timeItem == 0) then
  211.                 furnace[i].finishedItems = furnace[i].finishedItems + 1;
  212.                 furnace[i].queuedItems = furnace[i].queuedItems - 1;
  213.                 furnace[i].timeItem = furnaceSpeed;
  214.                 if (furnace[i].queuedItems == 0 and furnace[i].finishedItems > 0) then
  215.                     furnace[i].state = "finished";
  216.                     if (useOutput) then
  217.                         addTask("retrieveitems", i, furnace[i].finishedItems);
  218.                     else
  219.                         furnace[i].state = "empty";
  220.                         furnace[i].finishedItems = 0;
  221.                     end
  222.                 end
  223.             end
  224.         elseif (furnace[i].state == "finished") then
  225.             if (furnace[i].finishedItems == 0) then
  226.                 furnace[i].state = "empty";
  227.             end
  228.         end
  229.  
  230.         if (useFuel and furnace[i].timeFuel == 0 and furnace[i].queuedItems > 0 and furnace[i].finishedItems < 64 and furnace[i].fuel > 0) then
  231.             furnace[i].timeFuel = fuelValue*fuelMultiplier;
  232.             furnace[i].fuel = furnace[i].fuel - 1;
  233.             if (furnace[i].fuel <= 10) then
  234.                 local b = true;
  235.                 for j, t in pairs(task) do
  236.                     if (t.taskname == "refuel" and t.furnacenumber == i) then
  237.                         b = false;
  238.                         break;
  239.                     end
  240.                 end
  241.                 if (b) then
  242.                     addTask("refuel", i);
  243.                 end
  244.             end
  245.         end
  246.     end
  247. end
  248.  
  249.  
  250. function checkTimer()
  251.     timerID = os.startTimer(1);
  252.     updateFurnaceTimer();
  253. end
  254.  
  255. function findEmptyFurnace()
  256.     for i = 1, furnaceCount*2, 1 do
  257.         if (furnace[i].state == "empty") then
  258.             sendMessage({["taskname"]="additems", ["furnacenumber"]=i, ["x"]=furnace[i].x, ["y"]=furnace[i].y});
  259.             return;
  260.         end
  261.     end
  262.     if (table.getn(task) > 0) then
  263.         sendTask();
  264.     else
  265.         sendMessage({["taskname"]="noemptyfurnace"});
  266.     end
  267. end
  268.  
  269. function sendMessage(message)
  270.     save();
  271.     rednet.broadcast(textutils.serialize(message), system);
  272. end
  273.  
  274. function checkMessage(message)
  275.     if (message.state == "updatefurnace") then
  276.         updateFurnace(message);
  277.     elseif (message.state == "getemptyfurnace") then
  278.         findEmptyFurnace();
  279.     elseif (message.state == "idle") then
  280.         sendTask();
  281.     elseif (message.state == "error") then
  282.         -- ouput chest full, fuel chest empty
  283.         -- turn on redstone signal, toggle every second?
  284.         errorMessage = message["errormessage"];
  285.     end
  286. end
  287.  
  288.  
  289.  
  290. function loop()
  291.     while (true) do
  292.         local e = { os.pullEvent() }
  293.         if ( e[1] == "timer" and timerID == e[2] ) then
  294.             checkTimer();
  295.             save();
  296.         elseif (e[1] == "rednet_message" and e[4] == system) then
  297.             checkMessage(textutils.unserialize(e[3]));
  298.             save();
  299.         end
  300.     end
  301. end
  302.  
  303. load();
  304. loop();
Advertisement
Add Comment
Please, Sign In to add comment