Savage_Me55iah

Bigreactor

Aug 1st, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.10 KB | None | 0 0
  1. local log_file = "LOG"
  2. local button_log = "BUTTON_LOG"
  3. local save_direct = "saves"
  4.  
  5. do
  6.   if fs.exists(tostring(save_direct.."/"..log_file)) then fs.delete(tostring(save_direct.."/"..log_file)) end
  7.   if fs.exists(tostring(save_direct.."/"..button_log)) then fs.delete(tostring(save_direct.."/"..button_log)) end
  8.   log_add = function(...)
  9.     local params = {...}
  10.     local text = tostring(params[1])
  11.     local filehandler = fs.open(tostring(save_direct.."/"..log_file), 'a')
  12.     if params[2] then text = "--"..params[1] end
  13.     filehandler.write(text)
  14.     filehandler.write("\n")
  15.     filehandler.close()
  16.     return true
  17.   end
  18.   log_button = function(...)
  19.     local params = {...}
  20.     local text = tostring(params[1])
  21.     local filehandler = fs.open(tostring(save_direct.."/"..button_log), 'a')
  22.     if params[2] then text = "--"..params[1] end
  23.     filehandler.write(text)
  24.     filehandler.write("\n")
  25.     filehandler.close()
  26.     return true
  27.   end
  28. end
  29.  
  30. -- LABEL --
  31.  
  32. if not os.getComputerLabel() then
  33.   term.clear() term.setCursorPos(1,1)
  34.   log_add("no computer label")
  35.   print("Enter computer label: ")
  36.   os.setComputerLabel(read())
  37.   log_add("computer label added: "..os.getComputerLabel())
  38.   term.clear()
  39. end
  40.  
  41. -- SETTINGS --
  42.  
  43. local settings_file = "config"
  44.  
  45. local settings_template = [[-- adjust these configuration options as necessary.
  46. -- IMPORTANT!, API won't load if there are spaces in the values
  47. COMPUTER_LABEL = "${label}"
  48. COMPUTER_ID = ${id}
  49.  
  50. HEADING = "${heading}"
  51. HOPPER_DIRECTION = "${hopper_direction}"
  52. CHEST_DIRECTION = "${chest_direction}"
  53. SCREEN_REFRESH_RATE = ${refresh_rate}
  54. TEXT_SCALE = ${text_scale}
  55. CONTROL_ROD = ${control_rod}
  56. REACTOR_ON = "${reactor_on}"
  57. ]]
  58.  
  59. local function interpolate(s, params)
  60.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  61. end
  62.  
  63. do  
  64.   save_settings = function()
  65.   local filehandler = fs.open(tostring(save_direct.."/"..settings_file), "w")
  66.    
  67.   settings = {
  68.         label = COMPUTER_LABEL or os.getComputerLabel(),
  69.         id = os.getComputerID(),
  70.         heading = HEADING or "Savage_Reactor",
  71.         hopper_direction = HOPPER_DIRECTION or "south",
  72.         chest_direction = CHEST_DIRECTION or "down",
  73.         refresh_rate = SCREEN_REFRESH_RATE or 300,
  74.         text_scale = TEXT_SCALE or 1,
  75.         control_rod = CR or CONTROL_ROD or 50,
  76.         reactor_on = RO or REACTOR_ON or "false"
  77. }
  78.  
  79.   filehandler.write(interpolate(settings_template, settings))
  80.   filehandler.close()
  81.   log_add("settings saved")
  82.   end
  83.  
  84.   load_settings = function()
  85.     if fs.exists(tostring(save_direct.."/"..settings_file)) == false then
  86.       log_add("Settings file not found", true)
  87.       log_add("Generating Settings")
  88.       save_settings()
  89.       return
  90.     else
  91.       os.unloadAPI(tostring(save_direct.."/"..settings_file))
  92.  
  93.       if os.loadAPI(tostring(save_direct.."/"..settings_file)) == false then log_add("Could not load the settings file!", true) end
  94.  
  95.       CONFIG = nil
  96.  
  97.       for k, v in pairs(_G) do
  98.         if k == settings_file then CONFIG = v break end
  99.       end
  100.  
  101.       if CONFIG == nil then log_add("CONFIG came up nil", true) end
  102.     end
  103.   end
  104.   load_settings()
  105. end
  106.  
  107. -- SAVE/LOAD --
  108.  
  109. do
  110.   load_one = function(command)
  111.       if fs.exists(command) then
  112.         local fileHandler = fs.open(command, 'r')
  113.         return textutils.unserialize(fileHandler.readAll())
  114.       end
  115.   end
  116.  
  117.   save_one = function(command1, command2)
  118.       local fileHandler = fs.open(command1, 'w')
  119.       fileHandler.write(textutils.serialize(command2))
  120.       fileHandler.close()
  121.       log_add("Saving: "..command1)
  122.   end
  123. end
  124.  
  125. local sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  126. local screens, capacitors, appl_energ, echest, stats, reactor = {[1] = term}, {}, {}, {}, {}, {}
  127. if fs.exists(save_direct.."/stats") then stats = load_one(tostring(save_direct.."/stats")) else save_one(tostring(save_direct.."/stats"), stats) end
  128.  
  129.  
  130. local value = {["item.ingotyellorium"] = 1,
  131.   ["item.eyeofender"] = 0.5}
  132.  
  133. value["item.ingotcyanite"] = value["item.ingotyellorium"] / 2
  134. value["item.ingotblutonium"] = value["item.ingotyellorium"]
  135. value["item.dustcyanite"] = value["item.ingotyellorium"] / 2
  136. value["item.dustyellorium"] = value["item.ingotyellorium"]
  137. value["item.dustblutonium"] = value["item.ingotyellorium"]
  138. value["tile.brmetal.0"] = value["item.ingotyellorium"] * 9
  139. value["tile.brmetal.1"] = value["item.ingotcyanite"] * 9
  140. value["tile.brmetal.3"] = value["item.ingotblutonium"] * 9
  141. value["item.enderpearl"] = value["item.eyeofender"] / 2
  142. value["item.blazepowder"] = value["item.eyeofender"] / 2
  143. value["item.blazerod"] = value["item.eyeofender"] * 2
  144.  
  145. local turbines = 18
  146.  
  147. -- PERIPHERALS --
  148.  
  149. do
  150.   peripherals_find = function()
  151.     log_add("Finding Peripherals Direct")
  152.     peripheral_list = {[1] = term}
  153.     local num = 2
  154.     for k,v in ipairs(sides) do
  155.       p_type = peripheral.getType(v) or nil
  156.       if p_type ~= nil then peripheral_list[num] = v num = num + 1 log_add("Found Peripheral: "..v.." "..p_type) end
  157.     end
  158.     log_add("Find Peripherals Direct Complete") log_add("Checking for Remote")
  159.     local remote = nil
  160.     for k,v in ipairs(peripheral_list) do
  161.       if peripheral.getType(tostring(v)) == "modem" then
  162.         if not peripheral.call(v, "isWireless") then remote = peripheral.wrap(v) log_add("Remote found: "..tostring(v)) end
  163.       end
  164.     end
  165.  
  166.     if remote ~= nil then
  167.       log_add("Finding Peripherals Remote")
  168.       peripheral_list = {}
  169.       peripheral_list = remote.getNamesRemote()
  170.       peripheral_list[(table.maxn(peripheral_list) + 1)] = peripheral_list[1]
  171.       peripheral_list[1] = term
  172.       local num = (table.maxn(peripheral_list) + 1)
  173.       for k,v in ipairs(sides) do
  174.         p_type = peripheral.getType(v) or nil
  175.         if p_type ~= nil then peripheral_list[num] = v num = num + 1 end
  176.       end
  177.     else
  178.       log_add("Remote not found", true)
  179.     end
  180.   end
  181. end
  182.  
  183. -- PERIPHERAL SORTER --
  184.  
  185. function decimal(number)
  186.   log_add("Converting #: "..tostring(number))
  187.   if type(number) == "number" then
  188.     local text, num = ".", number
  189.       if num >= 1000 then text = "K" end
  190.       if num >= 1000000 then text = "M" end
  191.       if num >= 1000000000 then text = "B" end
  192.       if num >= 1000000000000 then text = "T" end
  193.     while num >= 1000 do
  194.       num = num / 1000
  195.     end
  196.     if text ~= "." then
  197.       text = tostring(math.floor(num)..text) or 0
  198.     else
  199.       text = tostring(math.floor(num)) or 0
  200.     end
  201.     log_add("# Conversion complete: "..text)
  202.     return text
  203.   else
  204.     return number
  205.   end
  206. end
  207.  
  208. do
  209.   peripherals_echest_sort = function(...)
  210.     local params = {...}
  211.     log_add("Sorting Ender Chests")
  212.     local answer, num, itemlist = {}, 1, nil
  213.     for a,b in pairs(echest) do
  214.       log_add("Sorting Ender Chest: "..b)
  215.       peripheral.call(b, "condenseItems")
  216.       itemlist = peripheral.call(b, "getAllStacks")
  217.       for c,d in pairs(itemlist) do
  218.         log_add("Running through Inv: ".."c: "..itemlist[c]["rawName"]..": "..itemlist[c]["name"])
  219.         if itemlist[c]["rawName"] == "item.paper" then
  220.           local side = b
  221.           echest[a] = {["side"] = b, ["name"] = itemlist[c]["name"]}
  222.           log_add("Paper Found: "..tostring(echest[a]["name"])..": "..tostring(echest[a]["side"]))
  223.         end
  224.       end
  225.     end
  226.     for a,b in pairs(echest) do
  227.       local matched = false
  228.       for c,d in pairs(stats) do
  229.         if echest[a]["name"] == stats[c]["name"] then
  230.           stats[a]["name"] = echest[a]["name"]
  231.           stats[a]["side"] = echest[a]["side"]
  232.           log_add("Matched Ender Chest User: "..echest[a]["name"])
  233.           matched = true
  234.         end
  235.       end
  236.       if not matched then
  237.         local num = table.maxn(stats) + 1
  238.         stats[num] = {}
  239.         stats[num]["name"] = echest[a]["name"]
  240.         stats[num]["side"] = echest[a]["side"]
  241.         stats[num]["time"] = 0
  242.         stats[num]["on"] = true
  243.         log_add("Adding Ender Chest User: "..echest[a]["name"])
  244.       end
  245.     end
  246.     log_add("Ender Chest Sort End.")
  247.   end
  248.  
  249.   peripherals_capacitor = function(...)
  250.     local params = {...}
  251.     log_add("Getting Capacitor details: "..tostring(params[1]))
  252.     local answer = {[1] = false}
  253.     local command = peripheral.wrap(params[1])
  254.     local p_methods = peripheral.getMethods(tostring(params[1]))
  255.     for k,v in ipairs(p_methods) do
  256.       if v == "getMaxEnergyStored" then answer[1] = true end
  257.     end
  258.     if answer[1] then
  259.       log_add("Method <getMaxEnergyStored> applicable: "..tostring(params[1]))
  260.       answer["capacity"] = command.getMaxEnergyStored("north") or 0
  261.       answer["stored"] = command.getEnergyStored("north") or 0
  262.       answer["percentage"] = math.floor((answer["stored"] / answer["capacity"])).."%" or "0%"
  263.       log_add(tostring(params[1]).." Details: "..answer["capacity"].." "..answer["stored"].." "..answer["percentage"])
  264.     else
  265.       log_add("Method <getMaxEnergyStored> not applicable: "..tostring(params[1]), true)
  266.     end
  267.     return answer
  268.   end
  269.  
  270.   peripherals_compile = function()
  271.     local answer = {[1] = 0, [2] = 0, [3] = 0}
  272.     local details = {}
  273.     log_add("Compiling Capacitors data")
  274.     for k,v in ipairs(capacitors) do
  275.       details = peripherals_capacitor(v)
  276.       answer[2] = answer[2] + details["capacity"] or 0
  277.       answer[1] = answer[1] + details["stored"] or 0
  278.     end
  279.     answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
  280.     log_add("Compile Results: "..answer[2].." "..answer[1].." "..answer[3])
  281.     return answer
  282.   end
  283.  
  284.   peripherals_applied_energ = function(command)
  285.     local params = {[1] = command or nil}
  286.     log_add("Getting AE details: "..tostring(params[1]))
  287.     local answer, applicable = {}, false
  288.     if tostring(params[1]) == "nil" then params[1] = appl_energ[1] end
  289.     if tostring(params[1]) ~= "nil" then
  290.     local p_methods = peripheral.getMethods(params[1])
  291.     for k,v in pairs(p_methods) do
  292.       if v == "getTotalBytes" then applicable = true end
  293.     end
  294.     if applicable then
  295.       log_add("Method <getTotalBytes> applicable: "..tostring(params[1]))
  296.       answer[2] = peripheral.call(params[1], "getTotalBytes") or 0
  297.       answer[1] = (answer[2] - peripheral.call(params[1], "getFreeBytes")) or 0
  298.       answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
  299.       answer[5] = peripheral.call(params[1], "getTotalItemTypes") or 0
  300.       answer[4] = (answer[5] - peripheral.call(params[1], "getRemainingItemTypes")) or 0
  301.       answer[6] = tostring(math.floor((answer[4] / answer[5]) * 100)) .. "%"
  302.       log_add(tostring(params[1]).." Details: "..answer[1].." "..answer[2].." "..answer[3])
  303.       log_add(tostring(params[1]).." Details: "..answer[4].." "..answer[5].." "..answer[6])
  304.     else
  305.       log_add("Method <getTotalBytes> not applicable: "..tostring(params[1]), true)
  306.     end
  307.     end
  308.     return answer
  309.   end
  310.  
  311.   peripherals_sort = function()
  312.     log_add("Sorting peripherals")
  313.     local mon, cap, ae, ec, react, turb = 2, 1, 1, 1, 1, 1
  314.     screens, capacitors, appl_energ, echest, reactor, turbine = {[1] = term}, {}, {}, {}, {}, {}
  315.  
  316.     for num=2,table.maxn(peripheral_list) do
  317.       log_add("Sorting Peripheral: "..tostring(peripheral_list[num]))
  318.       local test1, test2 = {}, {}
  319.       test1 = peripherals_capacitor(peripheral_list[num])
  320.       test2 = peripherals_applied_energ(peripheral_list[num])
  321.    
  322.       if peripheral.getType(peripheral_list[num]) == "monitor" then
  323.         screens[mon] = peripheral_list[num] log_add("Adding Monitor: "..tostring(peripheral_list[num])) mon = mon + 1
  324.       elseif test1["capacity"] ~= nil then
  325.         capacitors[cap] = peripheral_list[num] log_add("Adding Capacitor: "..tostring(peripheral_list[num])) cap = cap + 1
  326.       elseif test2[2] ~= nil then
  327.         appl_energ[ae] = peripheral_list[num] log_add("Adding AE: "..tostring(peripheral_list[num])) ae = ae + 1
  328.       elseif peripheral.getType(peripheral_list[num]) == "modem" then
  329.         if peripheral.call(peripheral_list[num], "isWireless") then
  330.           wireless = peripheral_list[num] log_add("Found Wireless Modem: "..tostring(peripheral_list[num]))
  331.         end
  332.       elseif peripheral.getType(peripheral_list[num]) == "ender_chest" then
  333.         echest[ec] = peripheral_list[num] log_add("Adding Ender Chest: "..tostring(peripheral_list[num])) ec = ec + 1
  334.       elseif peripheral.getType(peripheral_list[num]) == "BigReactors-Reactor" then
  335.         reactor[react] = peripheral_list[num] log_add("Adding Reactor: "..tostring(peripheral_list[num])) react = react + 1
  336.       elseif peripheral.getType(peripheral_list[num]) == "BigReactors-Turbine" then
  337.         turbine[turb] = peripheral_list[num] log_add("Adding Turbine: "..tostring(peripheral_list[num])) turb = turb + 1
  338.       end
  339.     end
  340.     if wireless ~= nil then
  341.       rednet.open(wireless) log_add("Wireless added")
  342.     else
  343.       log_add("Failure to add Wireless Modem: Program Terminated", true) print("Failure to find Wireless Modem: Program Terminated") shell.exit()
  344.     end
  345.   end
  346. end
  347.  
  348. -- SCREENS --
  349.  
  350. do
  351.   screen_change_colors = function(...)
  352.     local params = {...}
  353.     local color1, color2 = params[2] or colors.white, params[3] or colors.black
  354.     params[1].setTextColor(color1)
  355.     params[1].setBackgroundColor(color2)
  356.     log_add("Changing colors to: "..color1.." "..color2)
  357.   end
  358.  
  359.   screen_text = function(...)
  360.     local params = {...}
  361.     params[1].setCursorPos(params[3], params[4])
  362.     screen_change_colors(params[1], (params[5] or nil), (params[6] or nil))
  363.     log_add("Writting on: "..tostring(params[1]).." "..tostring(params[2]).." "..tostring(params[3]).." "..tostring(params[4]))
  364.     params[1].write(params[2]:upper())
  365.     screen_change_colors(params[1])
  366.   end
  367.  
  368.   screen_box = function(...)
  369.     local params = {...}
  370.     term.redirect(params[1])
  371.     paintutils.drawLine(params[5], params[4], params[3], params[4], params[2])
  372.     paintutils.drawLine(params[3], params[4], params[3], params[6], params[2])
  373.     paintutils.drawLine(params[5], params[6], params[3], params[6], params[2])
  374.     paintutils.drawLine(params[5], params[4], params[5], params[6], params[2])
  375.     term.restore()
  376.   end
  377.    
  378.   time_calc = function(number)
  379.     log_add("Converting #: "..tostring(number))
  380.     if type(number) == "number" then
  381.       local text, num = ".", number
  382.         if num >= 20 then num = num / 20 text = "secs" end
  383.         if num >= 60 and text == "secs" then num = num / 60 text = "mins" end
  384.         if num >= 60 and text == "mins" then num = num / 60 text = "hrs" end
  385.         if num >= 24 and text == "hrs" then num = num / 24 text = "days" end
  386.       if text ~= "." then
  387.         text = tostring(math.floor(num)..text) or 0
  388.       else
  389.         text = tostring(math.floor(num)) or 0
  390.       end
  391.       log_add("# Conversion complete: "..text)
  392.       return text
  393.     else
  394.       return number
  395.     end
  396.   end
  397.  
  398.   screen_drawall = function(...)
  399.     local params = {...}
  400.     mon_button = {[1] = {}}
  401.     term_button = {[1] = {}}
  402.     if current_screen == nil then current_screen = "main" end
  403.     num = 2
  404.       log_add("Draw All: Drawing Screen: "..tostring(screens[num]))
  405.       if screens[num] == term then mon = screens[num] else mon = peripheral.wrap(screens[num]) end
  406.       mon.clear()
  407.       if num == 1 then
  408.         term_x, term_y = mon.getSize()
  409.         term_x, term_y = term_x - 2, term_y - 4
  410.         term_x1, term_y1, term_x2, term_y2 = 2, 4, term_x + 1, term_y + 2
  411.         if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true term_x, term_x1 = term_x - 9, term_x1 + 9 end
  412.         term_rows, term_cols = math.floor(term_y / 2), math.floor(term_x / button_length())
  413.         term_total = term_rows * term_cols
  414.         log_add("Term Details log: "..term_x.." "..term_y) log_add("Term Details log: "..term_x1.." "..term_y1.." "..term_x2.." "..term_y2) log_add("Term Details log: "..term_rows.." "..term_cols.." "..term_total)
  415.       elseif num == 2 then
  416.         mon.setTextScale(tonumber(CONFIG.TEXT_SCALE))
  417.         mon_x, mon_y = mon.getSize()
  418.         mon_x, mon_y = mon_x - 2, mon_y - 4
  419.         mon_x1, mon_y1, mon_x2, mon_y2 = 2, 4, mon_x + 1, mon_y + 2
  420.         if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true mon_x, mon_x1 = mon_x - 9, mon_x1 + 9 end
  421. --        mon_rows, mon_cols = math.floor(mon_y / 2), math.floor(mon_x / button_length())
  422. --        mon_total = mon_rows * mon_cols
  423. --        log_add("Mon Details log: "..mon_x.." "..mon_y) log_add("Mon Details log: "..mon_x1.." "..mon_y1.." "..mon_x2.." "..mon_y2) ---log_add("Mon Details log: "..mon_rows.." "..mon_cols.." "..mon_total)
  424.       end
  425.       if peripheral.getType(tostring(screens[num])) == "monitor" then mon.setTextScale(tonumber(CONFIG.TEXT_SCALE)) end
  426.       local x, y, x1, y1, x2, y2 = 2, 2, 1, 1, 1, 1
  427.       if tostring(CONFIG.REACTOR_ON) == "true" then color1 = colors.green else color1 = colors.red end
  428.       screen_text(mon, tostring("reactor on: "..tostring(CONFIG.REACTOR_ON)), x, y, color1)
  429.       x, y = mon.getCursorPos() x = x + 1
  430.       screen_text(mon, (" "..string.gsub(tostring(CONFIG.HEADING), "_", " ").." "), x, y, colors.white, colors.blue)
  431.       x, y = 2, 4
  432.       local temp1, temp2, details = peripheral.wrap(tostring(reactor[1])), peripheral.wrap(tostring(turbine[1])), {}
  433.       if tostring(temp1.getActive()) == "true" then
  434.         details = {[1] = "CR: + - "..tostring(temp1.getControlRodLevel(1)).."%",
  435.                        [2] = "Steam: "..tostring(decimal(temp1.getEnergyProducedLastTick())).."b/t",
  436.                        [3] = "Fuel: "..tostring(decimal(temp1.getFuelAmount())).."/"..tostring(decimal(temp1.getFuelAmountMax())).." mb",
  437.                        [4] = "Usage: "..tostring(math.floor(temp1.getFuelConsumedLastTick() * 1000) / 1000).." mb/t",
  438.                        [5] = "Speed: "..tostring(math.floor(temp2.getRotorSpeed())).."rpm",
  439.                        [6] = "Power: "..tostring(decimal(tonumber(temp2.getEnergyProducedLastTick() * turbines))).."rf/t",
  440.                        [7] = "rf-ingot = "..tostring(decimal(((1000 / temp1.getFuelConsumedLastTick()) * temp2.getEnergyProducedLastTick() * turbines))).." rf",
  441.                        [8] = "t-ingot = "..tostring(math.floor(1000 / tonumber(temp1.getFuelConsumedLastTick()))) }
  442.       else
  443.         details = {[1] = "CR: + - "..tostring(CONFIG.CONTROL_RODS).."%",
  444.                        [2] = "Steam: NIL b/t",
  445.                        [3] = "Fuel: NIL mb",
  446.                        [4] = "Usage: NIL mb/t",
  447.                        [5] = "Speed: NIL rpm",
  448.                        [6] = "Power: NIL rf/t",
  449.                        [7] = "rf-ingot = NIL rf",
  450.                        [8] = "t-ingot = NIL" }
  451.       end
  452.       for k,v in pairs(details) do
  453.         screen_text(mon, tostring(v), x, y, colors.yellow)
  454.         x2, y2 = mon.getCursorPos()
  455.         if x2 > x1 then x1 = x2 end
  456.         y = y + 2
  457.       end
  458.       x, y = x1 + 2, 4
  459.       for k,v in pairs(stats) do
  460.         if stats[k]["time"] ~= 0 and stats[k]["on"] then color1 = colors.green else color1 = colors.red end
  461.         screen_text(mon, tostring(stats[k]["name"]..": "..tostring(time_calc(tonumber(stats[k]["time"])))), x, y, color1)
  462.         y = y + 1
  463.       end
  464.       log_add("Draw All: Drawing Screen: "..tostring(screens[num])..": Complete")
  465.   end
  466.  
  467. -- PROGRAM --
  468.  
  469.   program_check_ec = function(...)
  470.     local answer, temp = {}, temp
  471.     local list, ticks = {}, nil
  472.     for k,v in pairs(stats) do
  473.       temp = peripheral.wrap(tostring(stats[k]["side"]))
  474.       list = {}
  475.       list = temp.getAllStacks()
  476.       stats[k]["on"] = true
  477.       for c,d in pairs(list) do
  478.         if tostring(value[list[c]["rawName"]]) ~= "nil" then
  479.           if tostring(peripheral.call(tostring(reactor[1]), "getActive")) == "true" then
  480.             ticks = peripheral.call(tostring(reactor[1]), "getFuelConsumedLastTick")
  481.           else
  482.             ticks = 0.06
  483.           end
  484.           stats[k]["time"] = stats[k]["time"] + (math.floor(tonumber(1000 / ticks) * tonumber(list[c]["qty"]) * tonumber(value[list[c]["rawName"]])))
  485.           temp.pushItem(tostring(CONFIG.HOPPER_DIRECTION), tonumber(c), tonumber(list[c]["qty"]))
  486.         elseif list[c]["rawName"] == "item.stick" then
  487.           stats[k]["on"] = false
  488.         elseif list[c]["rawName"] ~= "tile.stone" and list[c]["rawName"] ~= "item.paper" then
  489.           temp.pushItem(tostring(CONFIG.HOPPER_DIRECTION), tonumber(c), tonumber(list[c]["qty"]))
  490.         end
  491.       end
  492.     end
  493.   end
  494.  
  495.   program_reducetime = function(...)
  496.     for k,v in pairs(stats) do
  497.       if stats[k]["on"] then
  498.         stats[k]["time"] = stats[k]["time"] - (tonumber(CONFIG.SCREEN_REFRESH_RATE) * 20)
  499.         if stats[k]["time"] <= 0 then stats[k]["time"] = 0 end
  500.       end
  501.     end
  502.   end
  503.  
  504.   program_checktimes = function(...)
  505.     local params, list, temp = {...}, {}, nil
  506.     for a,b in pairs(stats) do
  507.       list = {}
  508.       if stats[a]["time"] >= 1 and stats[a]["on"] then
  509.         list = peripheral.call(tostring(stats[a]["side"]), "getAllStacks")
  510.         for c,d in pairs(list) do
  511.           if list[c]["rawName"] == "tile.stone" then
  512.             temp = peripheral.wrap(tostring(stats[a]["side"]))
  513.             temp.pushItem(tostring(CONFIG.CHEST_DIRECTION), tonumber(c), tonumber(list[c]["qty"]), 1)
  514.           end
  515.         end
  516.       else
  517.         temp = peripheral.wrap(tostring(stats[a]["side"]))
  518.         temp.pullItem(tostring(CONFIG.CHEST_DIRECTION), 1)
  519.       end
  520.     end
  521.   end
  522.  
  523.   program_adjustreactor = function(...)
  524.     local answer, temp = {}, nil
  525.     local temp = peripheral.wrap(tostring(reactor[1]))
  526.     temp.setAllControlRodLevels(control_rod)
  527.     while tonumber(peripheral.call(tostring(turbine[1]), "getFluidFlowRate") * turbines) <= tonumber(peripheral.call(tostring(reactor[1]), "getEnergyProducedLastTick")) and control_rod ~= 1 do
  528.       control_rod = tonumber(temp.getControlRodLevel(1)) - 1
  529.       if control_rod <= 1 then control_rod = 1 end
  530.       temp.setAllControlRodLevels(control_rod)
  531.       sleep(20)
  532.     end
  533.   end
  534.  
  535.   program_adjustreactor2 = function(...)
  536.     local answer, temp = {}, nil
  537.     local temp = peripheral.wrap(tostring(reactor[1]))
  538.     temp.setAllControlRodLevels(control_rod)
  539.     if tonumber(peripheral.call(tostring(turbine[1]), "getFluidFlowRate") * turbines) <= tonumber(peripheral.call(tostring(reactor[1]), "getEnergyProducedLastTick")) and control_rod ~= 1 then
  540.       control_rod = tonumber(temp.getControlRodLevel(1)) - 1
  541.       if control_rod <= 1 then control_rod = 1 end
  542.       temp.setAllControlRodLevels(control_rod)
  543.     end
  544.   end
  545. end
  546.  
  547.  
  548. peripherals_find()
  549. peripherals_sort()
  550.  
  551. peripherals_echest_sort()
  552. --if tostring(peripheral.call(tostring(reactor[1]), "getActive")) == "true" then
  553. --  local temp = peripheral.wrap(tostring(reactor[1]))
  554. --  if tonumber(temp.getControlRodLevel(1)) == 1 then control_rod = 90 else control_rod = tonumber(temp.getControlRodLevel(1)) end
  555. --  program_adjustreactor()
  556. --end
  557. temp = peripheral.wrap(tostring(reactor[1]))
  558. temp.setAllControlRodLevels(tonumber(CONFIG.CONTROL_ROD))
  559. os.startTimer(tonumber(CONFIG.SCREEN_REFRESH_RATE))
  560. RO = tostring(CONFIG.REACTOR_ON)
  561.  
  562. while true do
  563.   if event == "timer" then
  564.     program_reducetime()
  565.     os.startTimer(tonumber(CONFIG.SCREEN_REFRESH_RATE))
  566.   end
  567.  
  568.   if event == "monitor_touch" then
  569.     temp = peripheral.wrap(tostring(reactor[1]))
  570.     RO = tostring(CONFIG.REACTOR_ON)
  571.     if x >= 2 and x <= 18 and y == 2 then
  572.       if RO == "true" then
  573.         RO = "false"
  574.       else
  575.         RO = "true"
  576.       end
  577.     end
  578.     save_settings()
  579.     load_settings()
  580.   end
  581.  
  582.   if event == "monitor_touch" and tostring(CONFIG.REACTOR_ON) == "true" then
  583.     temp = peripheral.wrap(tostring(reactor[1]))
  584.     CR = tonumber(temp.getControlRodLevel(1))
  585.     if x == 6 and y == 4 then
  586.       CR = tonumber(CR) + 1
  587.     elseif x == 8 and y == 4 then
  588.       CR = tonumber(CR) - 1
  589.     end
  590.     save_settings()
  591.     load_settings()
  592.     temp.setAllControlRodLevels(tonumber(CR))
  593.   end
  594.  
  595.   if RO == "true" then
  596.       temp.setActive(true)
  597.   elseif RO == "false" then
  598.       temp.setActive(false)
  599.   end
  600.  
  601.   program_check_ec()
  602.   program_checktimes()
  603.   screen_drawall()
  604. --  program_adjustreactor2()
  605.  
  606.   save_one("saves/stats", stats)
  607.   event, id, x, y = os.pullEvent()
  608. end
Advertisement
Add Comment
Please, Sign In to add comment