View difference between Paste ID: 3beJHQZD and ds2DnWHQ
SHOW: | | - or go back to the newest paste.
1
API = require("buttonAPI")
2
local filesystem = require("filesystem")
3
local component = require("component")
4
local keyboard = require("keyboard")
5
local event = require("event")
6
local gpu = component.gpu
7
local reactor = component.br_reactor
8
9-
local versionType = "NEW"
9+
10
local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
11-
local DEBUG = false
11+
12-
local debugList = {}
12+
13-
local debugVars = {}
13+
14
gpu.setBackground(colors.black)
15
gpu.fill(1, 1, 132, 38, " ")
16
17
local sections = {}
18
local graphs = {}
19
local infos = {}
20
21
22
23
-- defninitions
24
reactor["stats"] = {}
25
local running = true
26
local maxRF = 0
27
local currentRodLevel = 0
28
local currentRf = 0
29
local currentRfTick = 0
30
local currenFuel = 0
31
32-
local reactorRodsLevel = {}
32+
33
local maxPowerRod = 100
34
35
36
-- functions
37
38
function setSections()
39
  sections["graph"] = { x = 5, y = 3, width = 78, height= 33, title = "  INFOS  "}
40
  sections["controls"] = { x = 88, y = 3, width = 40, height = 20, title = "  CONTROLS  "}
41
  sections["info"] = { x = 88, y = 26, width = 40, height= 10, title = "  NUMBERS  "}
42
end
43
44-
function toint(n)
44+
45-
    local s = tostring(n)
45+
46-
    local i, j = s:find('%.')
46+
47-
    if i then
47+
48-
        return tonumber(s:sub(1, i-1))
48+
49-
    else
49+
50-
        return n
50+
51
  infos["tick"] = { x = 92, y = 28, width = 73, height= 1, title = "RF PER TICK : ", unit = " RF"}
52
  infos["stored"] = { x = 92, y = 30, width = 73, height = 1, title = "ENERGY STORED : ", unit = " RF"}
53
  infos["rods"] = { x = 92, y = 32, width = 73, height= 1, title = "CONTROL ROD LEVEL : ", unit = "%"}
54
  infos["fuel"] = { x = 92, y = 34, width = 73, height= 1, title = "FUEL USAGE : ", unit = " Mb/t"}
55
end
56
57
function setButtons()
58
  API.setTable("ON", powerOn, 91, 5, 106, 7,"ON", {on = colors.green, off = colors.green})
59
  API.setTable("OFF", powerOff, 109, 5, 125, 7,"OFF", {on = colors.red, off = colors.red})
60
61
  API.setTable("lowerMinLimit", lowerMinLimit, 91, 15, 106, 17,"-5", {on = colors.blue, off = colors.blue})
62
  API.setTable("lowerMaxLimit", lowerMaxLimit, 109, 15, 125, 17,"-5", {on = colors.purple, off = colors.purple})
63
64
  API.setTable("augmentMinLimit", augmentMinLimit, 91, 19, 106, 21,"+5", {on = colors.blue, off = colors.blue})
65
  API.setTable("augmentMaxLimit", augmentMaxLimit, 109, 19, 125, 21,"+5", {on = colors.purple, off = colors.purple})
66
end
67
68
function printBorders(sectionName)
69
  local s = sections[sectionName]
70
71
  -- set border
72
  gpu.setBackground(colors.grey)
73-
function debugInfos()  
73+
74-
  debug["print"] = { x = 1, y = 38, width = 73, height= 1, title = "DBG : "}
74+
75
  gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
76
  gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
77
78
  -- set title
79
  gpu.setBackground(colors.black)
80
  gpu.set(s.x + 2, s.y, s.title)
81-
  API.setTable("lowerMinLimit", lowerMinLimit, 91, 15, 106, 17,"-10", {on = colors.blue, off = colors.blue})
81+
82-
  API.setTable("lowerMaxLimit", lowerMaxLimit, 109, 15, 125, 17,"-10", {on = colors.purple, off = colors.purple})
82+
83
function printGraphs(graphName)
84-
  API.setTable("augmentMinLimit", augmentMinLimit, 91, 19, 106, 21,"+10", {on = colors.blue, off = colors.blue})
84+
85-
  API.setTable("augmentMaxLimit", augmentMaxLimit, 109, 19, 125, 21,"+10", {on = colors.purple, off = colors.purple})
85+
86
  -- set graph
87
  gpu.setBackground(colors.lightGrey)
88
  gpu.fill(g.x, g.y, g.width, g.height, " ")
89
90
  -- set title
91
  gpu.setBackground(colors.black)
92
  gpu.set(g.x, g.y - 1, g.title)
93
end
94
95
function printActiveGraphs(activeGraph)
96
  local g = activeGraph
97
98
  -- set graph
99
  gpu.setBackground(colors.green)
100
  gpu.fill(g.x, g.y, g.width, g.height, " ")
101
  gpu.setBackground(colors.black)
102
end
103
104
function printStaticControlText()
105
  gpu.setForeground(colors.blue)
106
  gpu.set(97,12, "MIN")
107
  gpu.setForeground(colors.purple)
108
  gpu.set(116,12, "MAX")
109
  gpu.setForeground(colors.white)
110
  gpu.set(102,10, "AUTO-CONTROL")
111
  gpu.set(107,13, "--")
112
end
113
114
function printControlInfos()
115
  gpu.setForeground(colors.blue)
116
  gpu.set(97,13, minPowerRod .. "% ")
117
  gpu.setForeground(colors.purple)
118
  gpu.set(116,13, maxPowerRod .. "% ")
119
  gpu.setForeground(colors.white)
120
end
121
122
function printInfos(infoName)
123
  local maxLength = 15
124
  local i = infos[infoName]
125
  local spaces = string.rep(" ", maxLength - string.len(reactor.stats[infoName] .. i.unit))
126
  gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit .. spaces)
127
end
128
129
function getInfoFromReactor()
130
  reactor.stats["tick"] = math.ceil(reactor.getEnergyProducedLastTick())
131
  reactor.stats["stored"] = reactor.getEnergyStored()
132
  reactor.stats["rods"] = math.ceil(reactor.getControlRodLevel(0))
133
  reactor.stats["fuel"] = round(reactor.getFuelConsumedLastTick(), 2);
134
  currentRf = reactor.stats["stored"]
135
end
136
137
function augmentMinLimit()
138
  modifyRods("min", 5)
139
end
140
141
function lowerMinLimit()
142
  modifyRods("min", -5)
143
end
144
145
function augmentMaxLimit()
146
  modifyRods("max", 5)
147
end
148
149
function lowerMaxLimit()
150-
  local reactorEnergyStats = reactor.getEnergyStats()
150+
  modifyRods("max", -5)
151-
  local reactorFuelStats = reactor.getFuelStats()
151+
152-
  reactorRodsLevel = reactor.getControlRodsLevels()
152+
153
function powerOn()
154-
  reactor.stats["tick"] = toint(math.ceil(reactorEnergyStats["energyProducedLastTick"]))
154+
155-
  reactor.stats["stored"] = toint(reactorEnergyStats["energyStored"])
155+
156-
  reactor.stats["rods"] = toint(reactorRodsLevel[0])
156+
157-
  reactor.stats["fuel"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
157+
158
  reactor.setActive(false)
159
end
160
161-
function getInfoFromReactorOLD()
161+
162-
  reactor.stats["tick"] = toint(math.ceil(reactor.getEnergyProducedLastTick()))
162+
163-
  reactor.stats["stored"] = toint(reactor.getEnergyStored())
163+
164-
  reactor.stats["rods"] = toint(math.ceil(reactor.getControlRodLevel(0)))
164+
165-
  reactor.stats["fuel"] = round(reactor.getFuelConsumedLastTick(), 2)
165+
166
		if tempLevel <= 0 then
167
			minPowerRod = 0
168
		end
169
170-
  modifyRods("min", 10)
170+
171
			minPowerRod = maxPowerRod -10
172
		end
173
174-
  modifyRods("min", -10)
174+
175
			minPowerRod = tempLevel
176
		end
177
	else
178-
  modifyRods("max", 10)
178+
179
		if tempLevel <= minPowerRod then
180
			maxPowerRod = minPowerRod +10
181
		end
182-
  modifyRods("max", -10)
182+
183
		if tempLevel >= 100 then
184
			maxPowerRod = 100
185
		end
186
187
		if tempLevel > minPowerRod and tempLevel < 100 then
188
			maxPowerRod = tempLevel
189
		end
190
	end
191
192
  setInfoToFile()
193
  adjustRodsLevel()
194
end
195
196
-- Calculate and adjusts the level of the rods
197
function adjustRodsLevel()
198
	local rfTotalMax = 400000
199
  currentRf = reactor.stats["stored"]
200
201
	differenceMinMax = maxPowerRod - minPowerRod
202
203
	local maxPower = (rfTotalMax/100) * maxPowerRod
204
	local minPower = (rfTotalMax/100) * minPowerRod
205
206
	if currentRf >= maxPower then
207
		currentRf = maxPower
208
	end
209
210
	if currentRf <= minPower then
211
		currentRf = minPower
212
	end
213
214
	currentRf = currentRf - (rfTotalMax/100) * minPowerRod
215
	local rfInBetween = (rfTotalMax/100) * differenceMinMax
216
	local rodLevel = math.ceil((currentRf/rfInBetween)*100)
217
218
	reactor.setAllControlRodLevels(rodLevel)
219
end
220
221
function draw()
222
  if maxRF < reactor.stats["tick"] then
223
    maxRF = reactor.stats["tick"]
224
  end
225-
  calculateAdjustRodsLevel()
225+
226
  if currentRfTick ~= reactor.stats["tick"] then
227
    currentRfTick = reactor.stats["tick"]
228
    local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
229-
function calculateAdjustRodsLevel()
229+
230-
	local rfTotalMax = 10000000
230+
231
    printGraphs("tick")
232
    printActiveGraphs(currentRFTickObj)
233
  end
234
235
  if currentRF ~= reactor.stats["stored"] then
236
    currentRF = reactor.stats["stored"]
237
    local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
238
    local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height}
239
    printInfos("stored")
240
    printGraphs("stored")
241
    printActiveGraphs(currentRFObj)
242
  end
243
244
  if currentRodLevel ~= reactor.stats["rods"] then
245
    currentRodLevel = reactor.stats["rods"]
246-
	currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
246+
247
    local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
248-
  local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
248+
249-
  
249+
250-
  if versionType == "NEW" then
250+
251-
    AdjustRodsLevel(rodLevel)
251+
252
253-
    AdjustRodsLevelOLD(rodLevel)
253+
254
    currenFuel = reactor.stats["fuel"]
255
    printInfos("fuel")
256
  end
257-
function AdjustRodsLevel(rodLevel)
257+
258-
  for key,value in pairs(reactorRodsLevel) do 
258+
259-
    --reactorRodsLevel[key] = rodLevel
259+
260-
    reactor.setControlRodLevel(key, rodLevel)
260+
261
  getInfoFromFile()
262-
  --reactor.setControlRodsLevels(reactorRodsLevel)
262+
  getInfoFromReactor()
263
  setSections()
264
  setGraphs()
265-
function AdjustRodsLevelOLD(rodLevel)
265+
266-
  reactor.setAllControlRodLevels(rodLevel)
266+
267
268
  for name, data in pairs(sections) do
269-
function printDebug()  
269+
270-
  local maxLength = 132
270+
271-
  local i = debug["print"]
271+
272-
  local rodsvalues = ""
272+
273-
  
273+
274-
  rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
274+
275
    printInfos(name)
276-
  local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
276+
277-
  local spaces = string.rep(" ", maxLength - string.len(debugInformations))
277+
278-
  gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
278+
279
280
end
281
282
283
-- helpers
284
function round(val, decimal)
285
  if (decimal) then
286
    return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
287
  else
288
    return math.floor(val+0.5)
289
  end
290
end
291
292
function file_exists(name)
293
   local f=io.open(name,"r")
294
   if f~=nil then io.close(f) return false else return true end
295
end
296
297
function getInfoFromFile()
298
	 if file_exists("reactor.txt") then
299
	 	file = io.open("reactor.txt","w")
300
	    file:write("0")
301
	    file:write("\n")
302
	    file:write("100")
303
	    file:close()
304
	else
305
		file = io.open("reactor.txt","r")
306
		minPowerRod = tonumber(file:read("*l"))
307
		maxPowerRod = tonumber(file:read("*l"))
308
	    file:close()
309
	end
310
end
311
312
function setInfoToFile()
313
	file = io.open("reactor.txt","w")
314
	file:write(minPowerRod .. "\n" .. maxPowerRod)
315
    file:flush()
316
    file:close()
317
end
318-
  if DEBUG == true then
318+
319-
    printDebug()
319+
320
startup()
321
API.screen()
322
323
event.listen("touch", API.checkxy)
324
325-
  if versionType == "NEW" then
325+
while event.pull(0.05, "interrupted") == nil do
326-
    getInfoFromReactor()
326+
  getInfoFromReactor()
327
  adjustRodsLevel()
328-
    getInfoFromReactorOLD()
328+
329
  local event, address, arg1, arg2, arg3 = event.pull(1)
330
  if type(address) == "string" and component.isPrimary(address) then
331
    if event == "key_down" and arg2 == keyboard.keys.q then
332
      os.exit()
333
    end
334-
  if DEBUG == true then
334+
335-
    debugInfos()
335+