Advertisement
OscarSimplement

Reacteur

Jul 8th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.81 KB | None | 0 0
  1. local VERISON = "NEW"
  2.  
  3. function checkVersionTooOld()
  4. local monitorsCheck = {peripheral.find("monitor")}
  5. end
  6.  
  7. function checkVersionNEW()
  8. local reactorsArray = {peripheral.find("BigReactors-Reactor")}
  9. reactorsArray[0].getEnergyStats();
  10. end
  11.  
  12. function checkErrors()
  13. if pcall(checkVersionTooOld) then
  14.  
  15. else
  16. error("The version of ComputerCraft is too old to use this program, sorry", 0)
  17. end
  18. local monitorsCheck = {peripheral.find("monitor")}
  19. local reactorsCheck = {peripheral.find("BigReactors-Reactor")}
  20.  
  21. if monitorsCheck[1] == nil then
  22. error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  23. end
  24.  
  25. if reactorsCheck[1] == nil then
  26. error("The Reactor is not being detected, make sure the connections(modem) are activated. The problem could also be related to chunk protection on some public servers, ask an admin about it.", 0)
  27. end
  28.  
  29. if pcall(checkVersionNEW) then
  30. VERISON = "NEW"
  31. else
  32. VERISON = "OLD"
  33. end
  34. end
  35.  
  36. -- you need to give the index to be able to use the program
  37. -- ex : NameOfProgram Index (reactor Krakaen)
  38.  
  39. local args = { ... }
  40.  
  41. local button = {}
  42. local filleds = {}
  43. local boxes = {}
  44. local lines = {}
  45. local texts = {}
  46.  
  47. local refresh = true
  48.  
  49. local infosSize = {}
  50. local controlsSize = {}
  51. local numbersSize = {}
  52. local currentRfTotal = 1
  53. local currentRfTick = 1
  54. local currentFuelConsumedLastTick = 0.00001
  55.  
  56.  
  57. local rfPerTickMax = 1
  58. local minPowerRod = 0
  59. local maxPowerRod = 100
  60. local currentRodLevel = 1
  61.  
  62. local index = ""
  63.  
  64. checkErrors() -- verify that everything is okay to start the program
  65.  
  66. if (#args == 0) then
  67. error("No index Given, Make sure to start the 'start' program and not the 'reactor' program", 0)
  68. end
  69.  
  70. if (#args == 1) then
  71. index = args[1]
  72. end
  73.  
  74. local monitors = {peripheral.find("monitor")}
  75. local mon = monitors[1]
  76. local reactors = {peripheral.find("BigReactors-Reactor")}
  77.  
  78. -- use the black thingy sponge to clear the chalkboard
  79.  
  80. term.redirect(mon)
  81. mon.clear()
  82. mon.setTextColor(colors.white)
  83. mon.setBackgroundColor(colors.black)
  84.  
  85. function clearTable()
  86. button = {}
  87. end
  88.  
  89.  
  90.  
  91. -- All the things that make my buttons work
  92.  
  93. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  94. button[name] = {}
  95. button[name]["title"] = title
  96. button[name]["func"] = func
  97. button[name]["active"] = false
  98. button[name]["xmin"] = xmin
  99. button[name]["ymin"] = ymin
  100. button[name]["xmax"] = xmax
  101. button[name]["ymax"] = ymax
  102. button[name]["color"] = color
  103. button[name]["elem"] = elem
  104. button[name]["elem2"] = elem2
  105. end
  106.  
  107. -- stuff and things for buttons
  108.  
  109. function fill(text, color, bData)
  110. mon.setBackgroundColor(color)
  111. mon.setTextColor(colors.white)
  112. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  113. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  114. for j = bData["ymin"], bData["ymax"] do
  115. mon.setCursorPos(bData["xmin"], j)
  116. if j == yspot then
  117. for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  118. if k == xspot then
  119. mon.write(bData["title"])
  120. else
  121. mon.write(" ")
  122. end
  123. end
  124. else
  125. for i = bData["xmin"], bData["xmax"] do
  126. mon.write(" ")
  127. end
  128. end
  129. end
  130. mon.setBackgroundColor(colors.black)
  131. end
  132.  
  133. -- stuff and things for buttons
  134.  
  135. function screen()
  136. local currColor
  137. for name,data in pairs(button) do
  138. local on = data["active"]
  139. currColor = data["color"]
  140. fill(name, currColor, data)
  141. end
  142. end
  143.  
  144. -- stuff and things for buttons
  145.  
  146. function flash(name)
  147.  
  148. screen()
  149. end
  150.  
  151. -- magical handler for clicky clicks
  152.  
  153. function checkxy(x, y)
  154. for name, data in pairs(button) do
  155. if y>=data["ymin"] and y <= data["ymax"] then
  156. if x>=data["xmin"] and x<= data["xmax"] then
  157. data["func"](data["elem"], data["elem2"])
  158. flash(data['name'])
  159. return true
  160. --data["active"] = not data["active"]
  161. --print(name)
  162. end
  163. end
  164. end
  165. return false
  166. end
  167.  
  168. -- Don't question my code, it works on my machine...
  169.  
  170. function label(w, h, text)
  171. mon.setCursorPos(w, h)
  172. mon.write(text)
  173. end
  174.  
  175. -- Draw function : put's all the beautiful magic in the screen
  176.  
  177. function draw()
  178.  
  179. for key,value in pairs(filleds) do
  180. local counter = 1
  181. for i=value[2],value[4],1 do
  182. paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  183. counter = counter + 1
  184. end
  185. end
  186.  
  187. for key,value in pairs(boxes) do
  188. paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  189. paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  190. paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  191. paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  192. end
  193.  
  194. for key,value in pairs(lines) do
  195. paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  196. end
  197.  
  198. for key,value in pairs(texts) do
  199. mon.setCursorPos(value[1], value[2])
  200. mon.setTextColor(value[4])
  201. mon.setBackgroundColor(value[5])
  202. mon.write(value[3])
  203. end
  204. screen()
  205. resetDraw()
  206. end
  207.  
  208. -- Resets the elements to draw to only draw the neccessity
  209.  
  210. function resetDraw()
  211. filleds = {}
  212. boxes = {}
  213. lines = {}
  214. texts = {}
  215. end
  216.  
  217. -- Handles all the clicks for the buttons
  218.  
  219. function clickEvent()
  220. local myEvent={os.pullEvent("monitor_touch")}
  221. checkxy(myEvent[3], myEvent[4])
  222. end
  223.  
  224. -- Power up the reactor (M&N are a good source of food right?)
  225.  
  226. function powerUp(m,n)
  227. local reactor = reactors[1]
  228. reactor.setActive(true)
  229. end
  230.  
  231. -- Power down the reactor (M&N are a good source of food right?)
  232.  
  233. function powerDown(m,n)
  234. local reactor = reactors[1]
  235. reactor.setActive(false)
  236. end
  237.  
  238. -- Handles and calculate the Min and Max of the power limitation
  239.  
  240. function modifyRods(limit, number)
  241. local tempLevel = 0
  242.  
  243. if limit == "min" then
  244. tempLevel = minPowerRod + number
  245. if tempLevel <= 0 then
  246. minPowerRod = 0
  247. end
  248.  
  249. if tempLevel >= maxPowerRod then
  250. minPowerRod = maxPowerRod -10
  251. end
  252.  
  253. if tempLevel < maxPowerRod and tempLevel > 0 then
  254. minPowerRod = tempLevel
  255. end
  256. else
  257. tempLevel = maxPowerRod + number
  258. if tempLevel <= minPowerRod then
  259. maxPowerRod = minPowerRod +10
  260. end
  261.  
  262. if tempLevel >= 100 then
  263. maxPowerRod = 100
  264. end
  265.  
  266. if tempLevel > minPowerRod and tempLevel < 100 then
  267. maxPowerRod = tempLevel
  268. end
  269. end
  270.  
  271. table.insert(lines, {controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, controlsSize['inX'] + controlsSize['width'], controlsSize['inY']+(controlsSize['sectionHeight']*1)+4, colors.black})
  272.  
  273. table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  274. table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  275. table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  276.  
  277. setInfoToFile()
  278. adjustRodsLevel()
  279. end
  280.  
  281. -- Calculate and adjusts the level of the rods
  282.  
  283. function adjustRodsLevel()
  284. local reactor = reactors[1]
  285. local rfTotalMax = 10000000
  286.  
  287. local allStats = getAllStats()
  288. local currentRfTotal = allStats["rfTotal"]
  289. local reactorRodsLevel = allStats["reactorRodsLevel"]
  290.  
  291. differenceMinMax = maxPowerRod - minPowerRod
  292.  
  293. maxPower = (rfTotalMax/100) * maxPowerRod
  294. minPower = (rfTotalMax/100) * minPowerRod
  295.  
  296. if currentRfTotal >= maxPower then
  297. currentRfTotal = maxPower
  298. end
  299.  
  300. if currentRfTotal <= minPower then
  301. currentRfTotal = minPower
  302. end
  303.  
  304. currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  305.  
  306. local rfInBetween = (rfTotalMax/100) * differenceMinMax
  307. local rodLevel = math.ceil((currentRfTotal/rfInBetween)*100)
  308.  
  309. if VERSION == "NEW" then
  310. if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  311. for key,value in pairs(reactorRodsLevel) do
  312. reactorRodsLevel[key] = rodLevel
  313. end
  314. reactor.setControlRodsLevels(reactorRodsLevel)
  315. end
  316. else
  317. reactor.setAllControlRodLevels(rodLevel)
  318. end
  319. end
  320.  
  321. -- Creates the frame and the basic of the visual
  322. -- Also adds the variables informations for placement of stuff and things
  323.  
  324. function addDrawBoxesSingleReactor()
  325. local w, h = mon.getSize()
  326. local margin = math.floor((w/100)*2)
  327.  
  328. infosSize['startX'] = margin + 1
  329. infosSize['startY'] = margin + 1
  330. infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  331. infosSize['endY'] = h - margin
  332. infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  333. infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  334. infosSize['inX'] = infosSize['startX'] + margin +1
  335. infosSize['inY'] = infosSize['startY'] + margin +1
  336. infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  337.  
  338. table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.purple})
  339. local name = "Réacteur Yellorium par Oscar_Simplement"
  340. table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  341. table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  342.  
  343. local names = {}
  344. names[1] = 'DERNIER TICKS ENERGIE'
  345. names[2] = 'STOCK ENERGIE'
  346. names[3] = '% BARRE DE CONTROLE'
  347.  
  348. for i=0,2,1 do
  349. table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  350. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 2 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  351. end
  352.  
  353.  
  354. -- Controls
  355.  
  356. controlsSize['startX'] = infosSize['endX'] + margin + 1
  357. controlsSize['startY'] = margin + 1
  358. controlsSize['endX'] = w-margin
  359. controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  360. controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  361. controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  362. controlsSize['inX'] = controlsSize['startX'] + margin +1
  363. controlsSize['inY'] = controlsSize['startY'] + margin
  364.  
  365. table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.orange})
  366. name = "Contrôle"
  367. table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  368. table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  369.  
  370. controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  371.  
  372. reactor = reactors[1]
  373.  
  374. mon.setTextColor(colors.white)
  375. setButton("ON","INJECTION", powerUp, controlsSize['inX'], controlsSize['inY'], controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +2, 0, 0, colors.green)
  376. setButton("OFF","AZ-5", powerDown, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'], controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +2,0, 0, colors.red)
  377.  
  378. table.insert(texts, {controlsSize['inX']+8, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'Puissance', colors.white, colors.black})
  379.  
  380. table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MINIMUM', colors.lightBlue, colors.black})
  381. table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  382.  
  383. table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  384. table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAXIMUM', colors.purple, colors.black})
  385. table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  386. mon.setTextColor(colors.white)
  387.  
  388. setButton("low-10","-10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "min", -10, colors.lightBlue)
  389. setButton("high-10","-10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "max", -10, colors.purple)
  390.  
  391. setButton("low+10","+10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "min", 10, colors.lightBlue)
  392. setButton("high+10","+10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "max", 10, colors.purple)
  393.  
  394. -- Numbers
  395.  
  396. numbersSize['startX'] = infosSize['endX'] + margin + 1
  397. numbersSize['startY'] = controlsSize['endY'] + margin + 1
  398. numbersSize['endX'] = w-margin
  399. numbersSize['endY'] = h-margin
  400. numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  401. numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  402. numbersSize['inX'] = numbersSize['startX'] + margin +1
  403. numbersSize['inY'] = numbersSize['startY'] + margin
  404.  
  405. table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.green})
  406. name = "Status Réacteur"
  407. table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  408. table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  409.  
  410. refresh = true
  411. while refresh do
  412. parallel.waitForAny(refreshSingleReactor,clickEvent)
  413. end
  414. end
  415.  
  416. -- Makes and Handles the draw function for less lag in the visual
  417.  
  418. function getAllStats()
  419. local stats = {}
  420. local reactor = reactors[1]
  421.  
  422. if VERSION == "NEW" then
  423. if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  424. local reactorEnergyStats = reactor.getEnergyStats()
  425. local reactorFuelStats = reactor.getFuelStats()
  426. stats["reactorRodsLevel"] = reactor.getControlRodsLevels()
  427.  
  428. stats["rfTotal"] = reactorEnergyStats["energyStored"]
  429. stats["rfPerTick"] = math.ceil(reactorEnergyStats["energyProducedLastTick"])
  430. stats["rodLevel"] = stats["reactorRodsLevel"][0]
  431. stats["fuelPerTick"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  432. end
  433. else
  434. stats["rfTotal"] = reactor.getEnergyStored()
  435. stats["rfPerTick"] = math.floor(reactor.getEnergyProducedLastTick())
  436. stats["rodLevel"] = math.floor(reactor.getControlRodLevel(0))
  437. stats["fuelPerTick"] = reactor.getFuelConsumedLastTick()
  438. end
  439.  
  440. return stats
  441. end
  442.  
  443. function refreshSingleReactor()
  444. local rfPerTick = 0
  445. local rfTotal = 0
  446. local rfTotalMax = 10000000
  447. local reactor = reactors[1]
  448.  
  449. local allStats = getAllStats()
  450. rfTotal = allStats["rfTotal"]
  451. rfPerTick = allStats["rfPerTick"]
  452. rodLevel = allStats["rodLevel"]
  453. fuelPerTick = allStats["fuelPerTick"]
  454.  
  455. local i = 0
  456. local infotoAdd = 'RF par Ticks : '
  457.  
  458. if currentRfTick ~= rfPerTick then
  459. currentRfTick = rfPerTick
  460. if rfPerTick > rfPerTickMax then
  461. rfPerTickMax = rfPerTick
  462. end
  463.  
  464. table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  465. table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  466. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  467.  
  468. width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  469. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  470.  
  471. end
  472.  
  473. i = 1
  474. infotoAdd = 'Stock Energétique : '
  475. if currentRfTotal ~= rfTotal then
  476. currentRfTotal = rfTotal
  477.  
  478. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  479.  
  480. width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  481. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.cyan})
  482. table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  483. table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  484. end
  485.  
  486. i = 2
  487. infotoAdd = 'Barres de Contrôles : '
  488. if currentRodLevel ~= rodLevel then
  489. currentRodLevel = rodLevel
  490.  
  491. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  492.  
  493. width = math.floor((infosSize['width'] / 100)*rodLevel)
  494. table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.yellow})
  495. table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  496. table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  497. end
  498.  
  499. i = 3
  500. infotoAdd = 'Conso Yellorium : '
  501. if currentFuelConsumedLastTick ~= fuelPerTick then
  502. currentFuelConsumedLastTick = fuelPerTick
  503.  
  504. table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  505. table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  506. end
  507.  
  508. mon.setTextColor(colors.white)
  509. adjustRodsLevel()
  510.  
  511. draw()
  512.  
  513. sleep(2)
  514. end
  515.  
  516. --
  517. -- ** Get the informations from the index file
  518. -- line 1 = min ROD
  519. -- line 2 = max ROD
  520. --
  521.  
  522. function getInfoFromFile()
  523.  
  524. if (fs.exists(index..".txt") == false) then
  525. file = io.open(index..".txt","w")
  526. file:write("0")
  527. file:write("\n")
  528. file:write("100")
  529. file:close()
  530. else
  531. file = fs.open(index..".txt","r")
  532. minPowerRod = tonumber(file.readLine())
  533. maxPowerRod = tonumber(file.readLine())
  534. file.close()
  535. end
  536. end
  537.  
  538. -- Save informations to the index file
  539.  
  540. function setInfoToFile()
  541. file = io.open(index..".txt","w")
  542. file:write(minPowerRod .. "\n" .. maxPowerRod)
  543. file:flush()
  544. file:close()
  545. end
  546.  
  547. ---============================================================
  548. -- add comma to separate thousands
  549. -- From Lua-users.org/wiki/FormattingNumbers
  550. --
  551. --
  552. function comma_value(amount)
  553. local formatted = amount
  554. while true do
  555. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  556. if (k==0) then
  557. break
  558. end
  559. end
  560. return formatted
  561. end
  562.  
  563. ---============================================================
  564. -- rounds a number to the nearest decimal places
  565. -- From Lua-users.org/wiki/FormattingNumbers
  566. --
  567. --
  568. function round(val, decimal)
  569. if (decimal) then
  570. return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  571. else
  572. return math.floor(val+0.5)
  573. end
  574. end
  575.  
  576. --===================================================================
  577. -- given a numeric value formats output with comma to separate thousands
  578. -- and rounded to given decimal places
  579. -- From Lua-users.org/wiki/FormattingNumbers
  580. --
  581. function format_num(amount, decimal, prefix, neg_prefix)
  582. local str_amount, formatted, famount, remain
  583.  
  584. decimal = decimal or 2 -- default 2 decimal places
  585. neg_prefix = neg_prefix or "-" -- default negative sign
  586.  
  587. famount = math.abs(round(amount,decimal))
  588. famount = math.floor(famount)
  589.  
  590. remain = round(math.abs(amount) - famount, decimal)
  591.  
  592. -- comma to separate the thousands
  593. formatted = comma_value(famount)
  594.  
  595. -- attach the decimal portion
  596. if (decimal > 0) then
  597. remain = string.sub(tostring(remain),3)
  598. formatted = formatted .. "." .. remain ..
  599. string.rep("0", decimal - string.len(remain))
  600. end
  601.  
  602. -- attach prefix string e.g '$'
  603. formatted = (prefix or "") .. formatted
  604.  
  605. -- if value is negative then format accordingly
  606. if (amount<0) then
  607. if (neg_prefix=="()") then
  608. formatted = "("..formatted ..")"
  609. else
  610. formatted = neg_prefix .. formatted
  611. end
  612. end
  613.  
  614. return formatted
  615. end
  616.  
  617. -- Clear and make the pixel smaller because we are not blind
  618.  
  619. mon.setBackgroundColor(colors.black)
  620. mon.clear()
  621. mon.setTextScale(0.5)
  622.  
  623. -- Get the information from the index file
  624. getInfoFromFile()
  625.  
  626.  
  627. -- Add's the visual and starts the Loop
  628. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement