· last year · Jul 03, 2024, 01:35 AM
1function tprint (tbl, indent)
2 if not indent then indent = 0 end
3 local toprint = string.rep(" ", indent) .. "{\r\n"
4 indent = indent + 2
5 for k, v in pairs(tbl) do
6 toprint = toprint .. string.rep(" ", indent)
7 if (type(k) == "number") then
8 toprint = toprint .. "[" .. k .. "] = "
9 elseif (type(k) == "string") then
10 toprint = toprint .. k .. "= "
11 end
12 if (type(v) == "number") then
13 toprint = toprint .. v .. ",\r\n"
14 elseif (type(v) == "string") then
15 toprint = toprint .. "\"" .. v .. "\",\r\n"
16 elseif (type(v) == "table") then
17 toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
18 else
19 toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
20 end
21 end
22 toprint = toprint .. string.rep(" ", indent-2) .. "}"
23 return toprint
24
25end
26function log(text)
27 local h = fs.open("log.txt", "a")
28 h.write(text)
29 h.close()
30end
31
32function countArray(arrayToCount)
33 local counter = 0
34 for _, part in ipairs(arrayToCount) do
35 counter = counter + 1
36 end
37 return counter
38end
39
40reactor = {}
41turbines = {}
42capacitor_bank = {}
43turbineFunctions = {
44 getStats = function (turbine)
45 local stats = {
46 maxEnergyProducedInTick = 0,
47 energyProducedLastTick = 0,
48 active = false,
49 api = turbine,
50 id = tonumber(countArray(turbines)) +1
51 }
52 if turbine.id ~= nil then
53 stats = turbine
54 else
55 stats.api = turbine
56 stats.active = stats.api.getActive()
57 end
58 stats.energyProducedLastTick = stats.api.getEnergyProducedLastTick()
59 if stats.maxEnergyProducedInTick < stats.energyProducedLastTick then
60 stats.maxEnergyProducedInTick = stats.energyProducedLastTick
61 end
62 turbines[stats.id] = stats
63 end,
64
65 spinUpTurbine = function(turbine)
66 turbine.api.setActive(true)
67 if turbine.api.getFluidFlowRate() < Settings.FluidFlowRateMax then
68 turbine.api.setFluidFlowRateMax(Settings.FluidFlowRateMax)
69 end
70 if turbine.api.getRotorSpeed() < Settings.rotorSpinUpValue then
71 if turbine.api.getInductorEngaged() then
72 turbineFunctions.setTurbineInductorEngaged(turbine, false)
73 end
74 end
75 end,
76 spinDownTurbine = function (turbine)
77 turbine.api.setActive(false)
78 turbine.api.setFluidFlowRateMax(0)
79 turbineFunctions.setTurbineInductorEngaged(turbine, false)
80 end,
81 spinUpAllTurbines = function ()
82 for key, value in ipairs(turbines) do
83 turbineFunctions.spinUpTurbine(value)
84 end
85 end,
86
87 getTurbineEnergyOutputLastTick = function (turbine)
88 return turbine.api.getEnergyProducedLastTick()
89 end,
90
91 setTurbineInductorEngaged = function(turbine, value)
92 turbine.api.setInductorEngaged(value)
93 end,
94
95 stopAllTurbinesIfNeeded = function(turbine)
96 for _, turbine in ipairs(turbines) do
97 turbineFunctions.spinDownTurbine(turbine)
98 end
99 end,
100
101 proofRotorSpeedIsEnough = function (turbine)
102 if turbine.api.getRotorSpeed() >= Settings.rotorSpinUpValue then
103 turbineFunctions.setTurbineInductorEngaged(turbine,true)
104 end
105 end
106}
107TankMaxFill = 15
108TankEmptyFill = 0
109TankSide = ""
110Settings = {
111 maxSteamToStorePercentage = 90,
112 minSteamToStorePercentage = 20,
113 FluidFlowRateMax = 2000,
114 rotorSpinUpValue = 1820,
115 maxEnergyToStorePercentage = 90,
116 minEnergyToStorePercentage = 20,
117}
118
119function loadSettings()
120 if not fs.exists("settings.conf") then
121 saveSettings()
122 end
123 local file = fs.open("settings.conf", "r")
124 local content = file.readAll()
125 settings = textutils.unserialize(content)
126 file.close()
127end
128
129function saveSettings()
130 local file = fs.open("settings.conf", "w")
131 file.write(textutils.serialize(settings))
132 file.close()
133end
134
135function initialise()
136 loadSettings()
137 getCapacitorBank()
138 getReactor()
139 getTurbines()
140 sleep(1)
141 turbineFunctions.spinUpAllTurbines()
142end
143
144function loop()
145 while true do
146 --control Reactor by Steam in Tank (quik)
147 getControlRodAdjustment()
148 for _,turbine in ipairs(turbines) do
149 if turbine.api.getActive() and not turbine.api.getInductorEngaged() then
150 turbineFunctions.proofRotorSpeedIsEnough(turbine)
151 end
152 if turbine.api.getActive() and turbine.api.getInductorEngaged() then
153 turbineFunctions.getStats(turbine)
154 end
155 end
156 --control Turbines by Energy Puffer Storage (we have 4 Turbines energy storage = 60%) so 2 Turbines are running
157 calculateIfMoreOreLessTurbinesNeeded()
158 sleep(1)
159 end
160end
161
162function getCapacitorBank()
163 local peripherals = peripheral.getNames()
164 for _, name in ipairs(peripherals) do
165 if peripheral.getType(name) == "capacitor_bank" then
166 capacitor_bank = peripheral.wrap(name)
167 break
168 end
169 end
170end
171
172function getTank()
173 local possibleSides = redstone.getSides()
174 for _, sides in ipairs(possibleSides) do
175 if (redstone.getInput(sides)) then
176 TankSide = sides
177 break
178 end
179 end
180end
181
182function getTankFillstate()
183 if TankSide == "" then
184 getTank()
185 sleep(1)
186 return getTankFillstate()
187 end
188 return redstone.getAnalogueInput(TankSide)
189end
190
191function getReactor()
192 local peripherals = peripheral.getNames()
193 for _, name in ipairs(peripherals) do
194 if peripheral.getType(name) == "BigReactors-Reactor" then
195 reactor = peripheral.wrap(name)
196 break
197 end
198 end
199end
200
201function getTurbines()
202 local peripherals = peripheral.getNames()
203 for test, name in ipairs(peripherals) do
204 if peripheral.getType(name) == "BigReactors-Turbine" then
205 turbineFunctions.getStats(peripheral.wrap(name))
206 end
207 end
208end
209
210function getControlRodAdjustment()
211 local currentStorredSteam = getTankFillstate()
212 local difMinMaxEnergyToStore = Settings.maxSteamToStorePercentage - Settings.minSteamToStorePercentage
213 local maxPower = (TankMaxFill / 100) * Settings.maxSteamToStorePercentage
214 local minPower = (TankMaxFill / 100) * Settings.minSteamToStorePercentage
215
216 if currentStorredSteam >= maxPower then
217 currentStorredSteam = maxPower
218 elseif currentStorredSteam <= minPower then
219 currentStorredSteam = minPower
220 end
221 --wie viel energy ist mehr als das minimum bereits gespeichert
222 currentStorredSteam = tonumber(currentStorredSteam - (TankMaxFill / 100) * Settings.minSteamToStorePercentage)
223 local rfInBetween = (TankMaxFill / 100) * difMinMaxEnergyToStore
224 local rodLevel = tonumber(math.ceil((currentStorredSteam / rfInBetween) * 100))
225
226 setControlRodsLevels(rodLevel)
227end
228
229function setControlRodsLevels (state)
230 --local self = setmetatable({},reactor)
231 local controlRods = reactor.getControlRodsLevels()
232 for i in pairs(controlRods) do
233 controlRods[i] = state
234 end
235 reactor.setControlRodsLevels(controlRods)
236end
237
238--pufferStoragePowerControll
239function calculateIfMoreOreLessTurbinesNeeded()
240 local turbinesCount = countArray(turbines)
241 local calculatedMaxEnergyStorage = (capacitor_bank.getMaxEnergyStored() / 100) * Settings.maxEnergyToStorePercentage
242 local calculatedMinEnergyStorage = (capacitor_bank.getMaxEnergyStored() / 100) * Settings.minEnergyToStorePercentage
243 local storedEnergyCorrected = capacitor_bank.getEnergyStored() - calculatedMinEnergyStorage
244 if storedEnergyCorrected <= 0 then
245 storedEnergyCorrected = 0
246 end
247 local diffBetweenMaxMinEnergyStorage = calculatedMaxEnergyStorage - calculatedMinEnergyStorage
248 local rfPerTurbine = diffBetweenMaxMinEnergyStorage / turbinesCount
249 local neededAmountOfTurbines = turbinesCount - (math.floor(storedEnergyCorrected / rfPerTurbine))
250 if calculatedMaxEnergyStorage <= capacitor_bank.getEnergyStored() then
251 neededAmountOfTurbines = 0
252 end
253 for _,turbine in ipairs(turbines) do
254 if neededAmountOfTurbines >= (tonumber(turbine.id)) then
255 if not turbine.api.getActive() then
256 turbineFunctions.spinUpTurbine(turbine)
257 end
258 else
259 turbineFunctions.spinDownTurbine(turbine)
260 end
261 end
262end
263
264initialise()
265--log(turbines)
266loop()
267--local periList = peripheral.getNames()
268--log(periList)
269
270--test = peripheral.find("BigReactors-Reactor")
271--log(tprint(test))