Advertisement
Phishphan420

Satisfactory FIN Farm Control Program

Nov 19th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.63 KB | None | 0 0
  1. local function find(waldo)
  2.     w = component.findComponent(waldo)[1]
  3.     if w then
  4.         return component.proxy(w)
  5.     else
  6.         return nil
  7.     end
  8. end
  9.  
  10. local panel = find("big_control_panel")
  11. local panel2 = find("small_control_panel")
  12. local colors = {    
  13.     red =       { 255,  0,      0   },
  14.     green =     { 0,    255,    0   },
  15.     blue =      { 0,    0,      255 },
  16.     yellow =    { 255,  255,    0   },
  17.     purple =    { 255,  0,      255 },
  18.     teal =      { 0,    255,    255 },
  19.     white =     { 255,  255,    255 },
  20.     black =     { 0,    0,      0   }
  21. }
  22.  
  23. local function loc(x,y,pane)
  24.     return panel:getModule(x,y,pane)
  25. end
  26.  
  27. local function setColor(light, color, isLit)
  28.     if type(color) == "table" then
  29.         light:setColor(color[1], color[2], color[3], isLit)
  30.     elseif type(color) == "string" then
  31.         light:setColor(colors[color][1],colors[color][2],colors[color][3], isLit)
  32.     end
  33. end
  34.  
  35. local function isPowerOn(switch)
  36.     return switch.isSwitchOn
  37. end
  38.  
  39. local indicators = {
  40.     extractor = { dirt = loc(0,8,0), bog = loc(1,8,0), peat = loc(2,8,0) },
  41.     groups = { extractor = loc(0,3,0), dirt = loc(1,3,0), bog = loc(2,3,0), peat = loc(3,3,0) },
  42.     water = { water = loc(3,8,0) },
  43.     dirt = { wheat = loc(7, 8, 0) , carrot = loc(8, 8, 0) , potato = loc(9, 8, 0) , corn = loc(10, 8, 0) },
  44.     bog = { pumpkin = loc(7, 5, 0), cotton = loc(8, 5, 0), yosse = loc(9, 5, 0), mycelium = loc(10, 5, 0) },
  45.     peat = { apple = loc(8, 2, 0), banana = loc(9, 2, 0) }
  46. }
  47.  
  48. local panel_swiches = {
  49.     extractor = { dirt = loc(0,7,0), bog = loc(1,7,0), peat = loc(2,7,0) },
  50.     groups = { extractor = loc(0,1,0), dirt = loc(1,1,0), bog = loc(2,1,0), peat = loc(3,1,0) },
  51.     water = { water = loc(3,7,0) },
  52.     dirt = { wheat = loc(7, 7, 0) , carrot = loc(8, 7, 0) , potato = loc(9, 7, 0) , corn = loc(10, 7, 0) },
  53.     bog = { pumpkin = loc(7, 4, 0), cotton = loc(8, 4, 0), yosse = loc(9, 4, 0), mycelium = loc(10, 4, 0) },
  54.     peat = { apple = loc(8, 1, 0), banana = loc(9, 1, 0) }
  55. }
  56.  
  57. local power_switches = {
  58.     extractor = { dirt = { find("dirt_cutoff_1"), find("dirt_cutoff_2") }, bog = { find("bog_cutoff_1"), find("bog_cutoff_2") }, peat = find("peat_cutoff") },
  59.     water = { water = find("water_cutoff") },
  60.     dirt = { wheat = find("wheat_cutoff"), carrot = find("carrot_cutoff"), potato = find("potato_cutoff"), corn = find("corn_cutoff") },
  61.     bog = { pumpkin = find("pumpkin_cutoff"), cotton = find("cotton_cutoff"), yosse = find("yosse_cutoff"), mycelium = find("mycelium_cutoff") },
  62.     peat = { apple = find("apple_cutoff"), banana = find("banana_cutoff") }
  63. }
  64.  
  65. local valves = {
  66.     water_extracters = {    find("water_valve_1"), find("water_valve_2"), find("water_valve_3"), find("water_valve_4"),
  67.                             find("water_valve_5"), find("water_valve_6"), find("water_valve_7"), find("water_valve_8") },
  68.     farms = {   wheat = find("wheat_valve"), carrot = find("carrot_valve"), potato = find("potato_valve"), corn = find("corn_valve"),
  69.                 pumpkin = find("pumpkin_valve"), cotton = find("cotton_valve"), yosse = find("yosse_valve"), mycelium = find("mycelium_valve"),
  70.                 apple = find("apple_valve"), banana = find("banana_valve")
  71.             }}
  72.  
  73. local gauges = {
  74.     water_extracters = { loc(0,8,1), loc(1,8,1), loc(2,8,1), loc(3,8,1), loc(4,8,1), loc(5,8,1), loc(6,8,1), loc(7,8,1) },
  75.     farms = {   wheat = loc(0,2,1), carrot = loc(1,2,1), potato = loc(2,2,1), corn = loc(3,2,1),
  76.                 pumpkin = loc(4,2,1), cotton = loc(5,2,1), yosse = loc(6,2,1), mycelium = loc(7,2,1),
  77.                 apple = loc(8,2,1), banana = loc(9,2,1)
  78.             },
  79.     groups = { total_water_in = loc(0, 1, 2), total_water_out = loc(2, 1, 2), dirt = loc(4, 1, 2), bog = loc(6, 1, 2), peat = loc(8, 1, 2) }
  80. }
  81.  
  82. local mini_displays = {
  83.     water_extracters = { loc(0,7,1), loc(1,7,1), loc(2,7,1), loc(3,7,1), loc(4,7,1), loc(5,7,1), loc(6,7,1), loc(7,7,1) },
  84.     farms = {   wheat = loc(0,1,1), carrot = loc(1,1,1), potato = loc(2,1,1), corn = loc(3,1,1),
  85.                 pumpkin = loc(4,1,1), cotton = loc(5,1,1), yosse = loc(6,1,1), mycelium = loc(7,1,1),
  86.                 apple = loc(8,1,1), banana = loc(9,1,1)
  87.             }
  88. }
  89.  
  90. local ptn = {
  91.     water_extracters = { loc(0,6,1), loc(1,6,1), loc(2,6,1), loc(3,6,1), loc(4,6,1), loc(5,6,1), loc(6,6,1), loc(7,6,1) },
  92.     farms = {   wheat = loc(0,0,1), carrot = loc(1,0,1), potato = loc(2,0,1), corn = loc(3,0,1),
  93.                 pumpkin = loc(4,0,1), cotton = loc(5,0,1), yosse = loc(6,0,1), mycelium = loc(7,0,1),
  94.                 apple = loc(8,0,1), banana = loc(9,0,1)
  95.             }
  96. }
  97.  
  98. local displays = {
  99.     groups = { total_water_in = loc(0, 0, 2), total_water_out = loc(2, 0, 2), dirt = loc(4, 0, 2), bog = loc(6, 0, 2), peat = loc(8, 0, 2) },
  100.     power = { total = loc(0, 4, 2), extractors = loc(2, 4, 2), dirt = loc(4, 4, 2), bog = loc(6, 4, 2), peat = loc(8, 4, 2) }
  101.     --big_power = { main = loc(4, 7, 2) }
  102. }
  103.  
  104. local e_stop = loc(4, 3, 0):getSubModule()
  105. local resume = loc(4, 0, 0):getSubModule()
  106.  
  107. local mappings = {}
  108. local flows = {}
  109. local groupFlows = {}
  110.  
  111. function handle3Switch(event, sender, p1, p2)  
  112.     h = mappings[sender.Hash]
  113.     state = false
  114.        
  115.     if p1 == 0 then
  116.         setColor(h.light, "red", 0)
  117.         state = false
  118.     elseif p1 == 1 then
  119.         setColor(h.light, "green", 0)
  120.         state = true
  121.     elseif p1 == 2 then
  122.         setColor(h.light, "blue", 0)
  123.         state = h.parent.state
  124.     end
  125.        
  126.     if type(h.power) == "table" then
  127.         for _, v in pairs(h.power) do
  128.             v:setIsSwitchOn(state)
  129.         end
  130.     else
  131.         h.power:setIsSwitchOn(state)
  132.     end
  133. end
  134.  
  135. function handle2Switch(event, sender, p1, p2)
  136.     h = mappings[sender.Hash]
  137.    
  138.     if p1 == false then
  139.         setColor(mappings[sender.Hash].light, "red", 0)
  140.     elseif p1 == true then
  141.         setColor(mappings[sender.Hash].light, "green", 0)
  142.     end
  143.    
  144.     if type(h.power) == "table" then
  145.         for _, v in pairs(h.power) do
  146.             v:setIsSwitchOn(p1)
  147.         end
  148.     else
  149.         h.power:setIsSwitchOn(p1)
  150.     end
  151. end
  152.  
  153. function handle2SwitchParent(event, sender, p1, p2)
  154.     h = mappings[sender.Hash]
  155.    
  156.     if p1 == false then
  157.         setColor(h.light, "red", 0)
  158.     elseif p1 == true then
  159.         setColor(h.light, "green", 0)
  160.     end
  161.     for k,v in pairs(h.children) do
  162.         if v.state == 2 and mappings[v.Hash].power then
  163.             if type(mappings[v.Hash].power) == "table" then
  164.                 for _, w in pairs(mappings[v.Hash].power) do
  165.                     w:setIsSwitchOn(p1)
  166.                 end
  167.             else
  168.                 mappings[v.Hash].power:setIsSwitchOn(p1)
  169.             end
  170.         end
  171.     end
  172. end
  173.  
  174. function handleFlowGroup(event, sender, p1, p2)
  175.     h = mappings[sender.Hash]
  176.     h.valve.userFlowLimit = p1 / 60.0
  177.     h.gauge.limit = p1 / 600.0
  178. end
  179.  
  180. function handleEStop(event, sender, p1, p2)
  181.     for _, v in pairs(mappings) do
  182.         if v.power then
  183.             if type(v.power) == "table" then
  184.                 for _, w in pairs(v.power) do
  185.                     w:setIsSwitchOn(false)
  186.                 end
  187.             else
  188.                 v.power:setIsSwitchOn(false)
  189.             end
  190.            
  191.             print(v.btn)
  192.             setColor(v.btn, "red")
  193.             setColor(v.light, "red")
  194.         end
  195.     end
  196.    
  197.     for _, v in pairs(mini_displays) do
  198.         for _, w in pairs(v) do
  199.             setColor(w, "red")
  200.         end
  201.     end
  202.    
  203.     for _, v in pairs(displays) do
  204.         for _, w in pairs(v) do
  205.             setColor(w, "red")
  206.         end
  207.     end
  208.    
  209.     for _, v in pairs(indicators.groups) do
  210.             setColor(v, "red")
  211.     end
  212. end
  213.  
  214. function handleResume(event, sender, p1, p2)
  215.     for _, v in pairs(mappings) do
  216.         if v.btn then
  217.             v.action("resume", v.btn, v.btn.state, nil)
  218.             if v.btn.setColor ~= nil then
  219.                 setColor(v.btn, "black")
  220.             end
  221.         end
  222.     end
  223.    
  224.     for _, v in pairs(mini_displays) do
  225.         for _, w in pairs(v) do
  226.             setColor(w, "green")
  227.         end
  228.     end
  229.    
  230.     for _, v in pairs(displays) do
  231.         for _, w in pairs(v) do
  232.             setColor(w, "green")
  233.         end
  234.     end
  235.    
  236.     for _, v in pairs(indicators.groups) do
  237.             setColor(v, "green")
  238.     end
  239. end
  240.  
  241. local function registerSwitch(btn, action, light)
  242.     event.listen(btn)
  243.     mappings[btn.Hash] = { btn = btn, action = action, light = light }
  244. end
  245.  
  246. local function registerSwitchPower(btn, action, light, power_switch)
  247.     event.listen(btn)
  248.     mappings[btn.Hash] = { btn = btn, action = action, light = light, power = power_switch }
  249. end
  250.  
  251. local function registerParentSwitch(btn, action, light, children)
  252.     event.listen(btn)
  253.     mappings[btn.Hash] = { btn = btn, action = action, light = light, children = children }
  254. end
  255.  
  256. local function registerSubSwitch(btn, action, light, power_switch, parent)
  257.     event.listen(btn)
  258.     mappings[btn.Hash] = { btn = btn, action = action, light = light, power = power_switch, parent = parent }
  259. end
  260.  
  261. local function registerFlow(gauge, display, ptn, valve, action)
  262.     event.listen(ptn)
  263.     ptn.max = 600
  264.     gauge.limit = ptn.value / 600
  265.     mappings[ptn.Hash] = { action = action, gauge = gauge, display = display, valve = valve }
  266.     flows[valve] = { gauge = gauge, display = display }
  267. end
  268.  
  269. local function registerFlowGroup(gauge, display, valves)
  270.     table.insert(groupFlows, { gauge = gauge, display = display, valves = valves } )
  271.     gauge.limit = 1.0
  272. end
  273.  
  274. local function registerMushButton(btn, action, color)
  275.     event.listen(btn)
  276.     mappings[btn.Hash] = { action = action }
  277.     setColor(btn, color)
  278. end
  279.  
  280. registerSwitchPower(panel_swiches.water.water, handle2Switch, indicators.water.water, power_switches.water.water)
  281.  
  282. registerParentSwitch(panel_swiches.groups.extractor, handle2SwitchParent, indicators.groups.extractor, panel_swiches.extractor)
  283. registerSubSwitch(panel_swiches.extractor.dirt, handle3Switch, indicators.extractor.dirt, power_switches.extractor.dirt, panel_swiches.groups.extractor)
  284. registerSubSwitch(panel_swiches.extractor.bog, handle3Switch, indicators.extractor.bog, power_switches.extractor.bog, panel_swiches.groups.extractor)
  285. registerSubSwitch(panel_swiches.extractor.peat, handle3Switch, indicators.extractor.peat, power_switches.extractor.peat, panel_swiches.groups.extractor)
  286.  
  287. registerParentSwitch(panel_swiches.groups.dirt, handle2SwitchParent, indicators.groups.dirt, panel_swiches.dirt)
  288. registerSubSwitch(panel_swiches.dirt.wheat, handle3Switch, indicators.dirt.wheat, power_switches.dirt.wheat, panel_swiches.groups.dirt)
  289. registerSubSwitch(panel_swiches.dirt.carrot, handle3Switch, indicators.dirt.carrot, power_switches.dirt.carrot, panel_swiches.groups.dirt)
  290. registerSubSwitch(panel_swiches.dirt.potato, handle3Switch, indicators.dirt.potato, power_switches.dirt.potato, panel_swiches.groups.dirt)
  291. registerSubSwitch(panel_swiches.dirt.corn, handle3Switch, indicators.dirt.corn, power_switches.dirt.corn, panel_swiches.groups.dirt)
  292.  
  293. registerParentSwitch(panel_swiches.groups.bog, handle2SwitchParent, indicators.groups.bog, panel_swiches.bog)
  294. registerSubSwitch(panel_swiches.bog.pumpkin, handle3Switch, indicators.bog.pumpkin, power_switches.bog.pumpkin, panel_swiches.groups.bog)
  295. registerSubSwitch(panel_swiches.bog.cotton, handle3Switch, indicators.bog.cotton, power_switches.bog.cotton, panel_swiches.groups.bog)
  296. registerSubSwitch(panel_swiches.bog.yosse, handle3Switch, indicators.bog.yosse, power_switches.bog.yosse, panel_swiches.groups.bog)
  297. registerSubSwitch(panel_swiches.bog.mycelium, handle3Switch, indicators.bog.mycelium, power_switches.bog.mycelium, panel_swiches.groups.bog)
  298.  
  299. registerParentSwitch(panel_swiches.groups.peat, handle2SwitchParent, indicators.groups.peat, panel_swiches.peat)
  300. registerSubSwitch(panel_swiches.peat.apple, handle3Switch, indicators.peat.apple, power_switches.peat.apple, panel_swiches.groups.peat)
  301. registerSubSwitch(panel_swiches.peat.banana, handle3Switch, indicators.peat.banana, power_switches.peat.banana, panel_swiches.groups.peat)
  302.  
  303. registerFlow(gauges.water_extracters[1], mini_displays.water_extracters[1], ptn.water_extracters[1], valves.water_extracters[1], handleFlowGroup)
  304. registerFlow(gauges.water_extracters[2], mini_displays.water_extracters[2], ptn.water_extracters[2], valves.water_extracters[2], handleFlowGroup)
  305. registerFlow(gauges.water_extracters[3], mini_displays.water_extracters[3], ptn.water_extracters[3], valves.water_extracters[3], handleFlowGroup)
  306. registerFlow(gauges.water_extracters[4], mini_displays.water_extracters[4], ptn.water_extracters[4], valves.water_extracters[4], handleFlowGroup)
  307. registerFlow(gauges.water_extracters[5], mini_displays.water_extracters[5], ptn.water_extracters[5], valves.water_extracters[5], handleFlowGroup)
  308. registerFlow(gauges.water_extracters[6], mini_displays.water_extracters[6], ptn.water_extracters[6], valves.water_extracters[6], handleFlowGroup)
  309. registerFlow(gauges.water_extracters[7], mini_displays.water_extracters[7], ptn.water_extracters[7], valves.water_extracters[7], handleFlowGroup)
  310. registerFlow(gauges.water_extracters[8], mini_displays.water_extracters[8], ptn.water_extracters[8], valves.water_extracters[8], handleFlowGroup)
  311.  
  312. registerFlow(gauges.farms.wheat, mini_displays.farms.wheat, ptn.farms.wheat, valves.farms.wheat, handleFlowGroup)
  313. registerFlow(gauges.farms.carrot, mini_displays.farms.carrot, ptn.farms.carrot, valves.farms.carrot, handleFlowGroup)
  314. registerFlow(gauges.farms.potato, mini_displays.farms.potato, ptn.farms.potato, valves.farms.potato, handleFlowGroup)
  315. registerFlow(gauges.farms.corn, mini_displays.farms.corn, ptn.farms.corn, valves.farms.corn, handleFlowGroup)
  316.  
  317. registerFlow(gauges.farms.pumpkin, mini_displays.farms.pumpkin, ptn.farms.pumpkin, valves.farms.pumpkin, handleFlowGroup)
  318. registerFlow(gauges.farms.cotton, mini_displays.farms.cotton, ptn.farms.cotton, valves.farms.cotton, handleFlowGroup)
  319. registerFlow(gauges.farms.yosse, mini_displays.farms.yosse, ptn.farms.yosse, valves.farms.yosse, handleFlowGroup)
  320. registerFlow(gauges.farms.mycelium, mini_displays.farms.mycelium, ptn.farms.mycelium, valves.farms.mycelium, handleFlowGroup)
  321.  
  322. registerFlow(gauges.farms.apple, mini_displays.farms.apple, ptn.farms.apple, valves.farms.apple, handleFlowGroup)
  323. registerFlow(gauges.farms.banana, mini_displays.farms.banana, ptn.farms.banana, valves.farms.banana, handleFlowGroup)
  324.  
  325. registerFlowGroup(gauges.groups.total_water_in, displays.groups.total_water_in, valves.water_extracters)
  326. registerFlowGroup(gauges.groups.total_water_out, displays.groups.total_water_out, valves.farms)
  327. registerFlowGroup(gauges.groups.dirt, displays.groups.dirt, { valves.farms.wheat, valves.farms.carrot, valves.farms.potato, valves.farms.corn })
  328. registerFlowGroup(gauges.groups.bog, displays.groups.bog, { valves.farms.pumpkin, valves.farms.cotton, valves.farms.yosse, valves.farms.mycelium })
  329. registerFlowGroup(gauges.groups.peat, displays.groups.peat, { valves.farms.apple, valves.farms.banana })
  330.  
  331. registerMushButton(e_stop, handleEStop, "red")
  332. registerMushButton(resume, handleResume, "green")
  333.  
  334. while true do --main loop
  335.  
  336.     local e, s, p1, p2, type, data = event.pull(0.1)
  337.     if e then  
  338.         mappings[s.Hash].action(e, s, p1, p2)
  339.     end
  340.    
  341.     for k, v in pairs(flows) do
  342.         v.gauge.percent = k.flowPct
  343.         v.display:setText(math.floor(k.flow * 60))
  344.     end
  345.    
  346.     for _, v in pairs(groupFlows) do
  347.         flowPctTotal = 0
  348.         flowTotal = 0
  349.         count = 0
  350.        
  351.         for _, w in pairs(v.valves) do
  352.             flowPctTotal = flowPctTotal + w.flowPct
  353.             flowTotal = flowTotal + w.flow
  354.             count = count + 1
  355.         end
  356.        
  357.         if count == 0 then count = 1 end
  358.            
  359.         v.gauge.percent = flowPctTotal / count
  360.         v.display:setText(math.floor(flowTotal * 60))
  361.     end
  362. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement