Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
leish0chiW1

to convert BigReactor code from Direwolf20 computeurcraft to opencomputers

Question

Greeting,
First of all please forgive my English because I use a translator
And thank you for any help and code optimization

I'm currently playing on the Modpack Project Ozone 3 and unlike 2, the modpack uses Opencomputers instead of Computercraft.

The problem that I currently encounter after countless error display and almost three sleepless nights (I specify that I am not a programmer, any time I tinker a little).

On Project Ozone 2 I used a Direwolf20 program code to manage my Big Reactor with computercraft, naturally I wanted to do the same by posting the code as it was on Opencomputer.
Following the first error encountered, I did some research and I came across some topics of this forum:
- a topic explaining basically the difference between the two mods (in particular on "os.load ()" and "require ()") - link : opencomputers-for-computercraft-users
- a topic about Direwolf20 Computercraft API button code conversion to Opencomputers
After an incalculable error display number (again) I came to understand that instead of "API.button.XXXXXX" it was enough just to replace "button.XXXX" with "API.XXXX"

Exemple code topic :

API.setTable("Tor", tor, 10,20,3,5)

Exemple my code :

API.button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)

 

also little question on the topic of each function code begins with "API.nameFunction" while on my own I just replaced the variable "button.XXXX" with "API.XXXX" and it finally seems to work when removes some line of the code.

Exemple code topic :

function API.fillTable()
  API.setTable("Tor", tor, 10,20,3,5)  
  API.setTable("Bahn", bahn, 22,32,3,5)
  API.setTable("Raus", test3, 10,20,8,10)
  API.setTable("Rein", test4, 22,32,8,10)
  API.screen()
end

Exemple original code program :

function autoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  menuMode()
end



Currently I have the following error that appears
/home/br_Reactor.lua:220: bad argument # 1 (string exected, got number):
[C]: in function 'error'
machine: 69: in function 'checkArg'
/home/br_Reactor.lua:220: in function 'comma_value'
/home/br_Reactor.lua:241: in function 'displayEn'
/home/br_Reactor.lua:429: in function 'findOutFuelRods'
/home/br_reactor.lua:617: in main chunk
(... tail calls ...)
[C]: in function 'xpcall'
machine: 791: in function'xpcall '
/lib/process.lua:63: in function </lib/process.lua:59>


And when I delete lines "
trm.write(comma_value(XXXXXXXXXX).. "XXXXXXX")" the code seems to work but with some bug like:
- The display is done in two colors (gray and white)
- The program displays a turbine error in the absence of it, on computeurcraft I just use the reactor or combi reactor / turbine it works
- The buttons are not touch

- the control of the rods does not seem to control the rods

can you help me please and if so how to otpimize the code

buttonAPI.lua

local API    = {}
local button = {}

local component = require("component") -- On appelle l'API "component"
local term      = require("term")      -- On appelle l'API "term"
local colors    = require("colors")    -- On appelle l'API "colors"

local mon = component.gpu
local trm = term

function API.clear()
  mon.setBackground(colors.black, true)
  mon.fill(1, 1, w, h, " ")
end

function API.clearTable()
    button = {}
	term.clear()
end

function API.setButton(name, buttonOn)
	print(name)
    print(button[name]["active"])
    button[name]["active"] = buttonOn
    API.screen()
end

function API.setTable(name, func, param, xmin, xmax, ymin, ymax)
    button[name] = {}
    button[name]["func"] = func
    button[name]["active"] = false
    button[name]["param"] = param
    button[name]["xmin"] = xmin
    button[name]["ymin"] = ymin
    button[name]["xmax"] = xmax
    button[name]["ymax"] = ymax
end

-- function API.fill(text, color, bData)
    -- mon.setBackground(color, true)
    -- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
    -- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
    -- for j = bData["ymin"], bData["ymax"] do
        -- trm.setCursor(bData["xmin"], j)
        -- if j == yspot then
            -- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
                -- if k == xspot then
                    -- trm.write(text)
                -- else
                    -- trm.write(" ")
                -- end
            -- end
        -- else
            -- for i = bData["xmin"], bData["xmax"] do
                -- trm.write(" ")
            -- end
        -- end
    -- end
    -- mon.setBackground(colors.black)
-- end

function API.fill(text, color, bData)
	-- ## this part works just fine
	local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
	local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
	-- ## save any previous color used before, not just black
	local oldColor = mon.setBackground(color, true)
	-- ## gpu.fill(x,y,width,height,char) char here = " " for a blank space of color
		mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
	-- ## write your text here from the gpu.set function instead of io.
		mon.set(xspot, yspot, text)
	-- ## reset the old color
		mon.setBackground(oldColor)
end

function API.screen()
    local currColor
    for name,data in pairs(button) do
        local on = data["active"]
        if on == true then
            currColor = colors.lime
        else
            currColor = colors.red
        end
        API.fill(name, currColor, data)
    end
end

function API.toggleButton(name)
    button[name]["active"] = not button[name]["active"]
    API.screen()
end

function API.flash(name)
    API.toggleButton(name)
    API.screen()
    os.sleep(0.15)
    API.toggleButton(name)
    API.screen()
end

function API.checkxy(x, y)
    for name, data in pairs(button) do
        if y>=data["ymin"] and  y <= data["ymax"] then
            if x>=data["xmin"] and x<= data["xmax"] then
                if data["param"] == "" then
                    data["func"]()
                else
                    data["func"](data["param"])
                end
                return true
                --data["active"] = not data["active"]
                --print(name)
            end
        end
    end
    return false
end

function API.heading(text)
    w, h = mon.getResolution()
    trm.setCursor((w-string.len(text))/2+1, 1)
    trm.write(text)
end

function API.label(w, h, text)
    trm.setCursor(w, h)
    trm.write(text)
end

return API

br_Reactor.lua

local API = require("buttonAPI")
local button = {}

local component = require("component")
local term = require("term")
local event = require("event")
local colors = require("colors")
local sides = require("sides")

mon = component.gpu
trm = term
egy = component.energy_device
brR = component.br_reactor
brT = component.br_turbine

local steamReactor = brR.isActivelyCooled()
local menuType = "Reactor"

local numCapacitors = 36
local turnOnAt = 50
local turnOffAt = 90

local targetSpeed = 1840

local energy = 0
local energyStored = 0
local energyMax = 0
local energyStoredPercent = 0
local timerCode
local mode = "Automatic"
local RFProduction = 0
local fuelUse = 0
local coreTemp = 0
local reactorOnline = false
local rodLevel = 0

local turbineOnline = false
local turbineRotorSpeed = 0
local turbineRFGen = 0
local turbineFluidRate = 0
local turbineInductor = false

local OptFuelRodLevel = 0

function autoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  menuMode()
end

function manualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Online", online, "", 3, 13, 8, 8)
  API.setTable("Offline", offline, "", 15, 25, 8, 8)
  API.setTable("0", setRods, 0, 11,14, 10, 10)
  API.setTable("10", setRods, 10, 5,8, 12, 12)
  API.setTable("20", setRods, 20, 11,14, 12, 12)
  API.setTable("30", setRods, 30, 17,20, 12, 12)
  API.setTable("40", setRods, 40, 5,8, 14, 14)
  API.setTable("50", setRods, 50, 11,14, 14, 14)
  API.setTable("60", setRods, 60, 17,20, 14, 14)
  API.setTable("70", setRods, 70, 5,8, 16, 16)
  API.setTable("80", setRods, 80, 11,14, 16, 16)
  API.setTable("90", setRods, 90, 17,20, 16, 16)
  API.setTable("+", rodPlus, "", 23, 25, 12, 12)
  API.setTable("-", rodMinus, "", 23, 25, 16, 16)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  reactorOnOff()
  menuMode()
end

function turbineAutoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.screen()
  checkMode()
  menuMode()
end

function turbineManualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.setTable("Online", setTurbineOnline, "", 3, 13, 8, 8)
  API.setTable("Offline", setTurbineOffline, "", 15, 25, 8, 8)
  API.setTable("Coils On", coilsOn, "", 3, 13, 10, 10)
  API.setTable("Coils Off", coilsOff, "", 15, 25, 10, 10)
  API.screen()
  checkMode()
  turbineOnOff()
  coilsOnOff()
  menuMode()
end

function reactorMenu()
  menuType = "Reactor"
  displayScreen()
end

function turbineMenu()
  menuType = "Turbine"
  displayScreen()
end

function online()
  brR.setActive(true)
  --API.flash("Online")
end

function offline()
  brR.setActive(false)
  --API.flash("Offline")
end

function setTurbineOnline()
  brT.setActive(true)
  --API.flash("Online")
end

function setTurbineOffline()
  brT.setActive(false)
  --API.flash("Offline")
end

function reactorOnOff()
  API.setButton("Online", brR.getActive())
  API.setButton("Offline", not brR.getActive())
end

function turbineOnOff()
  API.setButton("Online", brT.getActive())
  API.setButton("Offline", not brT.getActive())
end

function coilsOnOff()
  API.setButton("Coils On", brT.getInductorEngaged())
  API.setButton("Coils Off", not brT.getInductorEngaged())
end

function coilsOn()
  brT.setInductorEngaged(true)
end

function coilsOff()
  brT.setInductorEngaged(false)
end

function menuMode()
  if steamReactor then
    if menuType == "Reactor" then
      API.setButton("Reactor", true)
      API.setButton("Turbine", false)
    else
      API.setButton("Reactor", false)
      API.setButton("Turbine", true)
    end
  end
end

function setRods(setLevel)
  print("Setting Rod Level: "..setLevel)
  API.flash(tostring(setLevel))
  brR.setAllControlRodLevels(setLevel)
  fuelRodLevel()
end

function rodPlus()
  API.flash("+")
  brR.setAllControlRodLevels(rodLevel+1)
  fuelRodLevel()
end

function rodMinus()
  API.flash("-")
  brR.setAllControlRodLevels(rodLevel-1)
  fuelRodLevel()
end

function checkMode()
  API.toggleButton(mode)
end

function manualMode()
  mode = "Manual"
  manualMenu()
end

function autoMode()
  mode = "Automatic"
  displayScreen()
end

function comma_value(amount)
  local formatted = amount
  local swap = false
  if formatted < 0 then
    formatted = formatted*-1
    swap = true
  end
  while true do
    formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
    if k == 0 then
      break
    end
  end
  if swap then
    formatted = "-"..formatted
  end
  return formatted
end

function displayEn()
  trm.clear()
  trm.setCursor(1,1)
  --print("Energy Use: "..energy)
  trm.write("Energy Use: ")
  if energy < 0 then
    mon.setForeground(colors.red)
  else
    mon.setForeground(colors.green)
  end
  trm.write(comma_value(math.floor(energy)).. "RF/t")
  mon.setForeground(colors.white)
  trm.setCursor(1,2)
  trm.write("Energy Stored: "..energyStoredPercent.."%")
  if menuType == "Reactor" then
    trm.setCursor(1,3)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    if steamReactor then
      trm.write("Steam: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."MB/t")
    else
      trm.write("RF Gen: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."RF/t")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Core Temp: "..math.floor(coreTemp).."c")
    trm.setCursor(22,3)
    trm.write("Fuel Use: "..fuelUse.."MB/t")  
  else
    trm.setCursor(1,3)
    trm.write("Turbine is: ")
    if turbineOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    trm.setCursor(1,4)
    mon.setForeground(colors.white)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    trm.write("RFGen: ")
    mon.setForeground(colors.green)
    trm.write(comma_value(math.floor(turbineRFGen)).."RF/t")
     
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Rotor: "..comma_value(math.floor(turbineRotorSpeed)).." RPM")
    trm.setCursor(22,3)
    trm.write("Steam: "..comma_value(turbineFluidRate).."MB/t")  
  end 
end
 
function checkEn()
  local tempEnergy = 0
  energyStored = egy.getEnergyStored()
  energyMax = egy.getMaxEnergyStored()
  energyStoredPercent = math.floor((energyStored/energyMax)*100)
  RFProduction = brR.getEnergyProducedLastTick()
  fuelUse = brR.getFuelConsumedLastTick()
  fuelUse = math.floor(fuelUse*100)
  fuelUse = fuelUse/100
  coreTemp = brR.getFuelTemperature()
  reactorOnline = brR.getActive()
  tempEnergy = egy.getEnergyStored()
  os.sleep(0.1)
  energy = (egy.getEnergyStored()-tempEnergy)/2
  energy = energy*numCapacitors
  if steamReactor then
    turbineOnline = brT.getActive()
    turbineRotorSpeed = brT.getRotorSpeed()
    turbineRFGen = brT.getEnergyProducedLastTick()
    turbineFluidRate = brT.getFluidFlowRate()
  end
end
 
function fuelRodLevel()
  rodLevel = brR.getControlRodLevel(0)
  --print(rodLevel)
  trm.setCursor(30,5)
  trm.write(tostring(rodLevel).."%")
  mon.setBackground(colors.white)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.white)
    trm.write(" ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    if rodLevel/10 >= i then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.yellow)
    end
    trm.write("   ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    mon.setBackground(colors.white)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end
 
function turbineInductorDisplay()
  turbineInductor = brT.getInductorEngaged()
  trm.setCursor(30,5)
  if turbineInductor then
    trm.write("On")
  else
    trm.write("Off")
  end
  mon.setBackground(colors.gray)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,7 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if i % 2 == 0 then
      mon.setBackground(colors.gray)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if i % 2 ~= 0 then
      mon.setBackground(colors.lightGray)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  for i = 8,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end

function getClick()
  local event, side, x, y = os.pullEvent("monitor_touch")
  API.checkxy(x,y)
end

function findOptFuelRods()
  trm.clear()
  brR.setActive(false)
  checkEn()
  displayEn()
  fuelRodLevel()
  while brR.getFuelTemperature() > 99 do
    for i= 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level")
      trm.setCursor(3,7)
      trm.write("Core Temp: "..brR.getFuelTemperature())
        trm.setCursor(3,8)
        trm.write("Waiting for 99c")
      os.sleep(1)
    end
  end
  while brR.getHotFluidAmount() > 10000 do
    for i = 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level, please wait ....")
      trm.setCursor(3,7)
      trm.write("Fluid Amount: "..comma_value(brR.getHotFluidAmount()).."mb")
        trm.setCursor(3,8)
        trm.write("Waiting for 10,000mb")
      os.sleep(1)
    end
  end
  brR.setAllControlRodLevels(99)
  brR.setActive(true)
  while brR.getFuelTemperature() < 100 do
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Set all rod levels to 99")
        trm.setCursor(3,7)
        trm.write("Waiting 5 seconds...")
      os.sleep(1)
    end
  end
  for i = 1,5 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Set all rod levels to 99")
      trm.setCursor(3,7)
      trm.write("Waiting 5 seconds...")
    os.sleep(1)
  end
  local tempMB = brR.getEnergyProducedLastTick()
  print(tempMB.."MB/t of steam")
  local tempRodLevels = math.floor(2000/tempMB)
  print("2000/"..tempMB.." = "..tempRodLevels)
  tempRodLevels = 100-tempRodLevels+5
  print("Adding 5 to Rod Levels: "..math.floor(tempRodLevels))
  brR.setAllControlRodLevels(math.floor(tempRodLevels))
  print("Waiting 10 seconds to confirm...")
  for i = 1,10 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Estimated Level: "..tempRodLevels)
      trm.setCursor(3,7)
      trm.write("Waiting 10 seconds...")
    os.sleep(1)
  end
  tempMB = brR.getEnergyProducedLastTick()
  while tempMB > 2000 do
    tempRodLevels = tempRodLevels+1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting below 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  while tempMB < 2000 do
    tempRodLevels = tempRodLevels -1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting Above 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  OptFuelRodLevel = tempRodLevels
end

function autoReactor()
  if not steamReactor then
    brR.setAllControlRodLevels(0)
    if energyStoredPercent < turnOnAt then
      if not reactorOnline then
        online()
      end
    end
    if energyStoredPercent > turnOffAt then
      if reactorOnline then
        offline()
      end
    end
  else
    brR.setAllControlRodLevels(OptFuelRodLevel)
    if energyStoredPercent < turnOnAt then
      --online()
      setTurbineOnline()
      coilsOn()
    end
    if energyStoredPercent > turnOffAt then
      --if turbineRotorSpeed > 1800 then
      --   offline()
      --else
      --   online()
      --end
      setTurbineOnline()
      coilsOff()
    end
    if turbineRotorSpeed > targetSpeed then
      offline()
    else
      online()
    end
  end
end

function displayScreen()
 --  repeat
  checkEn()
  displayEn()
  if menuType == "Reactor" then
    fuelRodLevel()
    if mode == "Automatic" then
      autoMenu()
      autoReactor()
    else
      manualMenu()
    end
  else
    turbineInductorDisplay()
    if mode == "Automatic" then
      turbineAutoMenu()
      autoReactor()
    else
      turbineManualMenu()
    end
  end
  timerCode = os.startTimer(1)
  local event, side, x, y
  repeat
    event, side, x, y = os.pullEvent()
    print(event)
    if event == "timer" then
      print(timerCode..":"..side)
      if timerCode ~= side then
        print("Wrong Code")
      else
        print("Right Code")
      end
    end
  until event~= "timer" or timerCode == side
  if event == "monitor_touch" then
    print(x..":"..y)
    API.checkxy(x,y)
  end
 --  until event ~= "timer"
end

if steamReactor then
  findOptFuelRods()
end

while true do
  displayScreen()
  
  os.sleep(1)
end

 

Share this post


Link to post
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Hey,

I didn't check the entire code, it's a lot of stuff to go through, but I found the reason for your error.

You are calling string.gsub on a number, but it's a string lib, so you have to convert the number to a string first, by calling tostring(number). Your while loop in lines 219-224 would then look like this:

while true do
  formatted, k = string.gsub(tostring(formatted), "^(%d+)(%d%d%d)", '%1,%2')
  if k == 0 then
  break
  end
end

May I also suggest to use local whenever possible? Like all variables, functions etc. It helps to reduce errors in form of variable overrides etc, plus access to locals is faster.

Try that and see, if it works, if not, just post the error message and I'll have a look again.

Share this post


Link to post
Share on other sites
  • 0
20 hours ago, CptMercury said:

Hey,

I didn't check the entire code, it's a lot of stuff to go through, but I found the reason for your error.

You are calling string.gsub on a number, but it's a string lib, so you have to convert the number to a string first, by calling tostring(number). Your while loop in lines 219-224 would then look like this:


while true do
  formatted, k = string.gsub(tostring(formatted), "^(%d+)(%d%d%d)", '%1,%2')
  if k == 0 then
  break
  end
end

May I also suggest to use local whenever possible? Like all variables, functions etc. It helps to reduce errors in form of variable overrides etc, plus access to locals is faster.

Try that and see, if it works, if not, just post the error message and I'll have a look again.

Hey,

Thank you for your help CptMercury


I did as said use "local", however I had to miss somewhere because I have as error "line 72: attempt to call global 'checkMod'

local function autoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode() <-- on this line
  menuMode()
end

 

Share this post


Link to post
Share on other sites
  • 0

Can you insert your entire code? Then I could help you find the issue.

If you kept the order of your function definitions, you need to change the order a bit, I will talk you through once posted the new code.

Share this post


Link to post
Share on other sites
  • 0
8 hours ago, CptMercury said:

Can you insert your entire code? Then I could help you find the issue.

If you kept the order of your function definitions, you need to change the order a bit, I will talk you through once posted the new code.

here is the new code
on the other hand I spent the evening to test and I ended up doing a ball ... under the effect of fatigue I save over the main code and remove the test code with all changes who goes with it ... so I do not really know what I did on this one compared to the other.

I also tried to change the order this morning after reading your answer, but I'm coming from a mistake

local API       = require("buttonAPI") -- On appelle l'API "buttonAPI"
local component = require("component") -- On appelle l'API "component"
local term      = require("term")      -- On appelle l'API "term"
local event     = require("event")     -- On appelle l'API "event"
local colors    = require("colors")    -- On appelle l'API "colors"

local mon = component.gpu
local trm = term
local egy = component.energy_device
local brR = component.br_reactor
local brT = component.br_turbine

local steamReactor = brR.isActivelyCooled()
local menuType = "Reactor"

local numCapacitors = 36
local turnOnAt = 50
local turnOffAt = 90

local targetSpeed = 1840

local energy = 0
local energyStored = 0
local energyMax = 0
local energyStoredPercent = 0
local timerCode
local mode = "Automatic"
local RFProduction = 0
local fuelUse = 0
local coreTemp = 0
local reactorOnline = false
local rodLevel = 0

local turbineOnline = false
local turbineRotorSpeed = 0
local turbineRFGen = 0
local turbineFluidRate = 0
local turbineInductor = false

local OptFuelRodLevel = 0

-- COLORS
local colors = {}
colors.white     = 0xFFFFFF
colors.orange    = 0xFFA500
colors.magenta   = 0xFF00FF
colors.lightblue = 0x00AEEF
colors.yellow    = 0xFFFF00
colors.lime      = 0x00FF00
colors.pink      = 0xFFC0CB
colors.gray      = 0x555555
colors.lightGray = 0xD3D3D3
colors.grey      = 0x555555
colors.silver    = 0xAAAAAA
colors.cyan      = 0x00FFFF
colors.purple    = 0x800080
colors.blue      = 0x0000FF
colors.brown     = 0x603913
colors.green     = 0x008000
colors.red       = 0xFF0000
colors.black     = 0x000000

local function checkMode()
  API.toggleButton(mode)
end

local function menuMode()
  if steamReactor then
    if menuType == "Reactor" then
      API.setButton("Reactor", true)
      API.setButton("Turbine", false)
    else
      API.setButton("Reactor", false)
      API.setButton("Turbine", true)
    end
  end
end

local function autoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  menuMode()
end

local function manualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Online", online, "", 3, 13, 8, 8)
  API.setTable("Offline", offline, "", 15, 25, 8, 8)
  API.setTable("0", setRods, 0, 11,14, 10, 10)
  API.setTable("10", setRods, 10, 5,8, 12, 12)
  API.setTable("20", setRods, 20, 11,14, 12, 12)
  API.setTable("30", setRods, 30, 17,20, 12, 12)
  API.setTable("40", setRods, 40, 5,8, 14, 14)
  API.setTable("50", setRods, 50, 11,14, 14, 14)
  API.setTable("60", setRods, 60, 17,20, 14, 14)
  API.setTable("70", setRods, 70, 5,8, 16, 16)
  API.setTable("80", setRods, 80, 11,14, 16, 16)
  API.setTable("90", setRods, 90, 17,20, 16, 16)
  API.setTable("+", rodPlus, "", 23, 25, 12, 12)
  API.setTable("-", rodMinus, "", 23, 25, 16, 16)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  reactorOnOff()
  menuMode()
end

local function turbineAutoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.screen()
  checkMode()
  menuMode()
end

local function turbineManualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.setTable("Online", setTurbineOnline, "", 3, 13, 8, 8)
  API.setTable("Offline", setTurbineOffline, "", 15, 25, 8, 8)
  API.setTable("Coils On", coilsOn, "", 3, 13, 10, 10)
  API.setTable("Coils Off", coilsOff, "", 15, 25, 10, 10)
  API.screen()
  checkMode()
  turbineOnOff()
  coilsOnOff()
  menuMode()
end

local function reactorMenu()
  local menuType = "Reactor"
  displayScreen()
end

local function turbineMenu()
  local menuType = "Turbine"
  displayScreen()
end

local function online()
  brR.setActive(true)
  --API.flash("Online")
end

local function offline()
  brR.setActive(false)
  --API.flash("Offline")
end

local function setTurbineOnline()
  brT.setActive(true)
  --API.flash("Online")
end

local function setTurbineOffline()
  brT.setActive(false)
  --API.flash("Offline")
end

local function reactorOnOff()
  API.setButton("Online", brR.getActive())
  API.setButton("Offline", not brR.getActive())
end

local function turbineOnOff()
  API.setButton("Online", brT.getActive())
  API.setButton("Offline", not brT.getActive())
end

local function coilsOnOff()
  API.setButton("Coils On", brT.getInductorEngaged())
  API.setButton("Coils Off", not brT.getInductorEngaged())
end

local function coilsOn()
  brT.setInductorEngaged(true)
end

local function coilsOff()
  brT.setInductorEngaged(false)
end

local function setRods(setLevel)
  print("Setting Rod Level: "..setLevel)
  API.flash(tostring(setLevel))
  brR.setAllControlRodLevels(setLevel)
  fuelRodLevel()
end

local function rodPlus()
  API.flash("+")
  brR.setAllControlRodLevels(rodLevel+1)
  fuelRodLevel()
end

local function rodMinus()
  API.flash("-")
  brR.setAllControlRodLevels(rodLevel-1)
  fuelRodLevel()
end

local function manualMode()
  local mode = "Manual"
  manualMenu()
end

local function autoMode()
  local mode = "Automatic"
  displayScreen()
end

local function comma_value(amount)
  local formatted = amount
  local swap = false
  if formatted < 0 then
    formatted = formatted*-1
    swap = true
  end
  while true do
    formatted, k = string.gsub(tostring(formatted), "^(%d+)(%d%d%d)", '%1,%2')
    if k == 0 then
      break
    end
  end
  if swap then
    formatted = "-"..formatted
  end
  return formatted
end

local function displayEn()
  trm.clear()
  trm.setCursor(1,1)
  --print("Energy Use: "..energy)
  trm.write("Energy Use: ")
  if energy < 0 then
    mon.setForeground(colors.red)
  else
    mon.setForeground(colors.green)
  end
  trm.write(comma_value(math.floor(energy)).. "RF/t")
  mon.setForeground(colors.white)
  trm.setCursor(1,2)
  trm.write("Energy Stored: "..energyStoredPercent.."%")
  if menuType == "Reactor" then
    trm.setCursor(1,3)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    if steamReactor then
      trm.write("Steam: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."MB/t")
    else
      trm.write("RF Gen: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."RF/t")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Core Temp: "..math.floor(coreTemp).."c")
    trm.setCursor(22,3)
    trm.write("Fuel Use: "..fuelUse.."MB/t")  
  else
    trm.setCursor(1,3)
    trm.write("Turbine is: ")
    if turbineOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    trm.setCursor(1,4)
    mon.setForeground(colors.white)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    trm.write("RFGen: ")
    mon.setForeground(colors.green)
    trm.write(comma_value(math.floor(turbineRFGen)).."RF/t")
     
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Rotor: "..comma_value(math.floor(turbineRotorSpeed)).." RPM")
    trm.setCursor(22,3)
    trm.write("Steam: "..comma_value(turbineFluidRate).."MB/t")  
  end 
end
 
local function checkEn()
  local tempEnergy = 0
  energyStored = egy.getEnergyStored()
  energyMax = egy.getMaxEnergyStored()
  energyStoredPercent = math.floor((energyStored/energyMax)*100)
  RFProduction = brR.getEnergyProducedLastTick()
  fuelUse = brR.getFuelConsumedLastTick()
  fuelUse = math.floor(fuelUse*100)
  fuelUse = fuelUse/100
  coreTemp = brR.getFuelTemperature()
  reactorOnline = brR.getActive()
  tempEnergy = egy.getEnergyStored()
  os.sleep(0.1)
  energy = (egy.getEnergyStored()-tempEnergy)/2
  energy = energy*numCapacitors
  if steamReactor then
    turbineOnline = brT.getActive()
    turbineRotorSpeed = brT.getRotorSpeed()
    turbineRFGen = brT.getEnergyProducedLastTick()
    turbineFluidRate = brT.getFluidFlowRate()
  end
end
 
local function fuelRodLevel()
  rodLevel = brR.getControlRodLevel(0)
  --print(rodLevel)
  trm.setCursor(30,5)
  trm.write(tostring(rodLevel).."%")
  mon.setBackground(colors.white)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.white)
    trm.write(" ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    if rodLevel/10 >= i then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.yellow)
    end
    trm.write("   ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    mon.setBackground(colors.white)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end
 
local function turbineInductorDisplay()
  turbineInductor = brT.getInductorEngaged()
  trm.setCursor(30,5)
  if turbineInductor then
    trm.write("On")
  else
    trm.write("Off")
  end
  mon.setBackground(colors.gray)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,7 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if i % 2 == 0 then
      mon.setBackground(colors.gray)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if i % 2 ~= 0 then
      mon.setBackground(colors.lightGray)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  for i = 8,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end

local function getClick()
  local event, side, x, y = event.pull(1,touch)
  if x == nil or y == nil then
    local h, w = mon.getResolution()
    mon.set(h, w, ".")
    mon.set(h, w, " ")
  else 
    API.checkxy(x,y)
  end
end

local function findOptFuelRods()
  trm.clear()
  brR.setActive(false)
  checkEn()
  displayEn()
  fuelRodLevel()
  while brR.getFuelTemperature() > 99 do
    for i= 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level")
      trm.setCursor(3,7)
      trm.write("Core Temp: "..brR.getFuelTemperature())
        trm.setCursor(3,8)
        trm.write("Waiting for 99c")
      os.sleep(1)
    end
  end
  while brR.getHotFluidAmount() > 10000 do
    for i = 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level, please wait ....")
      trm.setCursor(3,7)
      trm.write("Fluid Amount: "..comma_value(brR.getHotFluidAmount()).."mb")
        trm.setCursor(3,8)
        trm.write("Waiting for 10,000mb")
      os.sleep(1)
    end
  end
  brR.setAllControlRodLevels(99)
  brR.setActive(true)
  while brR.getFuelTemperature() < 100 do
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Set all rod levels to 99")
        trm.setCursor(3,7)
        trm.write("Waiting 5 seconds...")
      os.sleep(1)
    end
  end
  for i = 1,5 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Set all rod levels to 99")
      trm.setCursor(3,7)
      trm.write("Waiting 5 seconds...")
    os.sleep(1)
  end
  local tempMB = brR.getEnergyProducedLastTick()
  print(tempMB.."MB/t of steam")
  local tempRodLevels = math.floor(2000/tempMB)
  print("2000/"..tempMB.." = "..tempRodLevels)
  tempRodLevels = 100-tempRodLevels+5
  print("Adding 5 to Rod Levels: "..math.floor(tempRodLevels))
  brR.setAllControlRodLevels(math.floor(tempRodLevels))
  print("Waiting 10 seconds to confirm...")
  for i = 1,10 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Estimated Level: "..tempRodLevels)
      trm.setCursor(3,7)
      trm.write("Waiting 10 seconds...")
    os.sleep(1)
  end
  tempMB = brR.getEnergyProducedLastTick()
  while tempMB > 2000 do
    tempRodLevels = tempRodLevels+1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting below 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  while tempMB < 2000 do
    tempRodLevels = tempRodLevels -1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting Above 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  OptFuelRodLevel = tempRodLevels
end

local function autoReactor()
  if not steamReactor then
    brR.setAllControlRodLevels(0)
    if energyStoredPercent < turnOnAt then
      if not reactorOnline then
        online()
      end
    end
    if energyStoredPercent > turnOffAt then
      if reactorOnline then
        offline()
      end
    end
  else
    brR.setAllControlRodLevels(OptFuelRodLevel)
    if energyStoredPercent < turnOnAt then
      --online()
      setTurbineOnline()
      coilsOn()
    end
    if energyStoredPercent > turnOffAt then
      --if turbineRotorSpeed > 1800 then
      --   offline()
      --else
      --   online()
      --end
      setTurbineOnline()
      coilsOff()
    end
    if turbineRotorSpeed > targetSpeed then
      offline()
    else
      online()
    end
  end
end

local function displayScreen()
 --  repeat
  checkEn()
  displayEn()
  if menuType == "Reactor" then
    fuelRodLevel()
    if mode == "Automatic" then
      autoMenu()
      autoReactor()
    else
      manualMenu()
    end
  else
    turbineInductorDisplay()
    if mode == "Automatic" then
      turbineAutoMenu()
      autoReactor()
    else
      turbineManualMenu()
    end
  end
  getClick()
end

if steamReactor then
  findOptFuelRods()
end

while true do
  displayScreen()
  
  os.sleep(1)
end

 

Share this post


Link to post
Share on other sites
  • 0

Ok, so the issue with locals you had is the following.

When using local, it's important to have the right order of variable/function definitions, let me give an example:

local function func_x()
  print("X")
 end
-- that is some random function

local function func_xy()
  func_x()
  func_y()
end
-- a function that will call func_x amd func_y
-- this will throw an error, because func_y is defined after it is called by func_xy

local function func_y()
  print("Y")
end

-- you have to ensure the right order!

  

Ok, so I did some rearrangement of your functions, I hope it works.

local API       = require("buttonAPI") -- On appelle l'API "buttonAPI"
local component = require("component") -- On appelle l'API "component"
local term      = require("term")      -- On appelle l'API "term"
local event     = require("event")     -- On appelle l'API "event"
local colors    = require("colors")    -- On appelle l'API "colors"
-- you are overwriting the colors api later, do you actually need that api?

local mon = component.gpu
local trm = term
local egy = component.energy_device
local brR = component.br_reactor
local brT = component.br_turbine

local steamReactor = brR.isActivelyCooled()
local menuType = "Reactor"

local numCapacitors = 36
local turnOnAt = 50
local turnOffAt = 90

local targetSpeed = 1840

local energy = 0
local energyStored = 0
local energyMax = 0
local energyStoredPercent = 0
local timerCode
local mode = "Automatic"
local RFProduction = 0
local fuelUse = 0
local coreTemp = 0
local reactorOnline = false
local rodLevel = 0

local turbineOnline = false
local turbineRotorSpeed = 0
local turbineRFGen = 0
local turbineFluidRate = 0
local turbineInductor = false

local OptFuelRodLevel = 0

-- COLORS
-- here are you overwriting colors. If you only need this table, you can remove 'require("colors")' above.
local colors = {}
colors.white     = 0xFFFFFF
colors.orange    = 0xFFA500
colors.magenta   = 0xFF00FF
colors.lightblue = 0x00AEEF
colors.yellow    = 0xFFFF00
colors.lime      = 0x00FF00
colors.pink      = 0xFFC0CB
colors.gray      = 0x555555
colors.lightGray = 0xD3D3D3
colors.grey      = 0x555555
colors.silver    = 0xAAAAAA
colors.cyan      = 0x00FFFF
colors.purple    = 0x800080
colors.blue      = 0x0000FF
colors.brown     = 0x603913
colors.green     = 0x008000
colors.red       = 0xFF0000
colors.black     = 0x000000

local function checkMode()
  API.toggleButton(mode)
end

local function menuMode()
  if steamReactor then
    if menuType == "Reactor" then
      API.setButton("Reactor", true)
      API.setButton("Turbine", false)
    else
      API.setButton("Reactor", false)
      API.setButton("Turbine", true)
    end
  end
end

local function online()
  brR.setActive(true)
  --API.flash("Online")
end

local function offline()
  brR.setActive(false)
  --API.flash("Offline")
end

local function setTurbineOnline()
  brT.setActive(true)
  --API.flash("Online")
end

local function setTurbineOffline()
  brT.setActive(false)
  --API.flash("Offline")
end

local function reactorOnOff()
  API.setButton("Online", brR.getActive())
  API.setButton("Offline", not brR.getActive())
end

local function turbineOnOff()
  API.setButton("Online", brT.getActive())
  API.setButton("Offline", not brT.getActive())
end

local function coilsOnOff()
  API.setButton("Coils On", brT.getInductorEngaged())
  API.setButton("Coils Off", not brT.getInductorEngaged())
end

local function coilsOn()
  brT.setInductorEngaged(true)
end

local function coilsOff()
  brT.setInductorEngaged(false)
end

local function fuelRodLevel()
  rodLevel = brR.getControlRodLevel(0)
  --print(rodLevel)
  trm.setCursor(30,5)
  trm.write(tostring(rodLevel).."%")
  mon.setBackground(colors.white)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.white)
    trm.write(" ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    if rodLevel/10 >= i then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.yellow)
    end
    trm.write("   ")
    mon.setBackground(colors.yellow)
    trm.write(" ")
    mon.setBackground(colors.white)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end

local function turbineInductorDisplay()
  turbineInductor = brT.getInductorEngaged()
  trm.setCursor(30,5)
  if turbineInductor then
    trm.write("On")
  else
    trm.write("Off")
  end
  mon.setBackground(colors.gray)
  trm.setCursor(28,6)
  trm.write("       ")
  for i = 1,7 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if i % 2 == 0 then
      mon.setBackground(colors.gray)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if i % 2 ~= 0 then
      mon.setBackground(colors.lightGray)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  for i = 8,10 do
    trm.setCursor(28,i+6)
    mon.setBackground(colors.gray)
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
    if turbineInductor then
      mon.setBackground(colors.red)
    else
      mon.setBackground(colors.blue)
    end
    trm.write(" ")
    mon.setBackground(colors.lightGray)
    trm.write(" ")
    mon.setBackground(colors.gray)
    trm.write(" ")
  end
  trm.setCursor(28,17)
  trm.write("       ")
  mon.setBackground(colors.black)
end

local function getClick()
  local event, side, x, y = event.pull(1,"touch") -- This should be the string "touch" as you want to look for a touch event, right?
  if x == nil or y == nil then
    local h, w = mon.getResolution()
    mon.set(h, w, ".")
    mon.set(h, w, " ")
  else 
    API.checkxy(x,y)
  end
end

local function comma_value(amount)
  local formatted = amount
  local swap = false
  if formatted < 0 then
    formatted = formatted*-1
    swap = true
  end
  while true do
    formatted, k = string.gsub(tostring(formatted), "^(%d+)(%d%d%d)", '%1,%2')
    if k == 0 then
      break
    end
  end
  if swap then
    formatted = "-"..formatted
  end
  return formatted
end

local function displayEn()
  trm.clear()
  trm.setCursor(1,1)
  --print("Energy Use: "..energy)
  trm.write("Energy Use: ")
  if energy < 0 then
    mon.setForeground(colors.red)
  else
    mon.setForeground(colors.green)
  end
  trm.write(comma_value(math.floor(energy)).. "RF/t")
  mon.setForeground(colors.white)
  trm.setCursor(1,2)
  trm.write("Energy Stored: "..energyStoredPercent.."%")
  if menuType == "Reactor" then
    trm.setCursor(1,3)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    if steamReactor then
      trm.write("Steam: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."MB/t")
    else
      trm.write("RF Gen: ")
      mon.setForeground(colors.green)
      trm.write(comma_value(math.floor(RFProduction)).."RF/t")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Core Temp: "..math.floor(coreTemp).."c")
    trm.setCursor(22,3)
    trm.write("Fuel Use: "..fuelUse.."MB/t")  
  else
    trm.setCursor(1,3)
    trm.write("Turbine is: ")
    if turbineOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    trm.setCursor(1,4)
    mon.setForeground(colors.white)
    trm.write("Reactor is: ")
    if reactorOnline then
      mon.setForeground(colors.green)
      trm.write("Online")
    else
      mon.setForeground(colors.red)
      trm.write("Offline")
    end
    mon.setForeground(colors.white)
    trm.setCursor(22,1)
    trm.write("RFGen: ")
    mon.setForeground(colors.green)
    trm.write(comma_value(math.floor(turbineRFGen)).."RF/t")
     
    mon.setForeground(colors.white)
    trm.setCursor(22,2)
    trm.write("Rotor: "..comma_value(math.floor(turbineRotorSpeed)).." RPM")
    trm.setCursor(22,3)
    trm.write("Steam: "..comma_value(turbineFluidRate).."MB/t")  
  end 
end
 
local function checkEn()
  local tempEnergy = 0
  energyStored = egy.getEnergyStored()
  energyMax = egy.getMaxEnergyStored()
  energyStoredPercent = math.floor((energyStored/energyMax)*100)
  RFProduction = brR.getEnergyProducedLastTick()
  fuelUse = brR.getFuelConsumedLastTick()
  fuelUse = math.floor(fuelUse*100)
  fuelUse = fuelUse/100
  coreTemp = brR.getFuelTemperature()
  reactorOnline = brR.getActive()
  tempEnergy = egy.getEnergyStored()
  os.sleep(0.1)
  energy = (egy.getEnergyStored()-tempEnergy)/2
  energy = energy*numCapacitors
  if steamReactor then
    turbineOnline = brT.getActive()
    turbineRotorSpeed = brT.getRotorSpeed()
    turbineRFGen = brT.getEnergyProducedLastTick()
    turbineFluidRate = brT.getFluidFlowRate()
  end
end

local function findOptFuelRods()
  trm.clear()
  brR.setActive(false)
  checkEn()
  displayEn()
  fuelRodLevel()
  while brR.getFuelTemperature() > 99 do
    for i= 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level")
      trm.setCursor(3,7)
      trm.write("Core Temp: "..brR.getFuelTemperature())
        trm.setCursor(3,8)
        trm.write("Waiting for 99c")
      os.sleep(1)
    end
  end
  while brR.getHotFluidAmount() > 10000 do
    for i = 1,3 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
      trm.write("Finding Optimal Rod Level, please wait ....")
      trm.setCursor(3,7)
      trm.write("Fluid Amount: "..comma_value(brR.getHotFluidAmount()).."mb")
        trm.setCursor(3,8)
        trm.write("Waiting for 10,000mb")
      os.sleep(1)
    end
  end
  brR.setAllControlRodLevels(99)
  brR.setActive(true)
  while brR.getFuelTemperature() < 100 do
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Set all rod levels to 99")
        trm.setCursor(3,7)
        trm.write("Waiting 5 seconds...")
      os.sleep(1)
    end
  end
  for i = 1,5 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Set all rod levels to 99")
      trm.setCursor(3,7)
      trm.write("Waiting 5 seconds...")
    os.sleep(1)
  end
  local tempMB = brR.getEnergyProducedLastTick()
  print(tempMB.."MB/t of steam")
  local tempRodLevels = math.floor(2000/tempMB)
  print("2000/"..tempMB.." = "..tempRodLevels)
  tempRodLevels = 100-tempRodLevels+5
  print("Adding 5 to Rod Levels: "..math.floor(tempRodLevels))
  brR.setAllControlRodLevels(math.floor(tempRodLevels))
  print("Waiting 10 seconds to confirm...")
  for i = 1,10 do
    checkEn()
    displayEn()
    fuelRodLevel()
      trm.setCursor(3,6)
      trm.write("Estimated Level: "..tempRodLevels)
      trm.setCursor(3,7)
      trm.write("Waiting 10 seconds...")
    os.sleep(1)
  end
  tempMB = brR.getEnergyProducedLastTick()
  while tempMB > 2000 do
    tempRodLevels = tempRodLevels+1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting below 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  while tempMB < 2000 do
    tempRodLevels = tempRodLevels -1
    brR.setAllControlRodLevels(math.floor(tempRodLevels))
    print("Setting Rod Levels to: "..tempRodLevels)
    for i = 1,5 do
      checkEn()
      displayEn()
      fuelRodLevel()
        trm.setCursor(3,6)
        trm.write("Getting Above 2000mb/t")
        trm.setCursor(3,7)
        trm.write("Currently at: "..tempMB)
      os.sleep(1)
    end
    tempMB = brR.getEnergyProducedLastTick()
  end
  OptFuelRodLevel = tempRodLevels
end

local function setRods(setLevel)
  print("Setting Rod Level: "..setLevel)
  API.flash(tostring(setLevel))
  brR.setAllControlRodLevels(setLevel)
  fuelRodLevel()
end

local function rodPlus()
  API.flash("+")
  brR.setAllControlRodLevels(rodLevel+1)
  fuelRodLevel()
end

local function rodMinus()
  API.flash("-")
  brR.setAllControlRodLevels(rodLevel-1)
  fuelRodLevel()
end

local function autoReactor()
  if not steamReactor then
    brR.setAllControlRodLevels(0)
    if energyStoredPercent < turnOnAt then
      if not reactorOnline then
        online()
      end
    end
    if energyStoredPercent > turnOffAt then
      if reactorOnline then
        offline()
      end
    end
  else
    brR.setAllControlRodLevels(OptFuelRodLevel)
    if energyStoredPercent < turnOnAt then
      --online()
      setTurbineOnline()
      coilsOn()
    end
    if energyStoredPercent > turnOffAt then
      --if turbineRotorSpeed > 1800 then
      --   offline()
      --else
      --   online()
      --end
      setTurbineOnline()
      coilsOff()
    end
    if turbineRotorSpeed > targetSpeed then
      offline()
    else
      online()
    end
  end
end

local displayScreen   -- some of your functions calle ach other, that makes it hard to find a correct order
-- so I just define displayScreen, so the function can be defined later
local manualMode

local function autoMode()
  local mode = "Automatic"
  displayScreen()
end

local function reactorMenu()
  local menuType = "Reactor"
  displayScreen()
end

local function turbineMenu()
  local menuType = "Turbine"
  displayScreen()
end

local function manualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Online", online, "", 3, 13, 8, 8)
  API.setTable("Offline", offline, "", 15, 25, 8, 8)
  API.setTable("0", setRods, 0, 11,14, 10, 10)
  API.setTable("10", setRods, 10, 5,8, 12, 12)
  API.setTable("20", setRods, 20, 11,14, 12, 12)
  API.setTable("30", setRods, 30, 17,20, 12, 12)
  API.setTable("40", setRods, 40, 5,8, 14, 14)
  API.setTable("50", setRods, 50, 11,14, 14, 14)
  API.setTable("60", setRods, 60, 17,20, 14, 14)
  API.setTable("70", setRods, 70, 5,8, 16, 16)
  API.setTable("80", setRods, 80, 11,14, 16, 16)
  API.setTable("90", setRods, 90, 17,20, 16, 16)
  API.setTable("+", rodPlus, "", 23, 25, 12, 12)
  API.setTable("-", rodMinus, "", 23, 25, 16, 16)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  reactorOnOff()
  menuMode()
end

function manualMode()
  local mode = "Manual"
  manualMenu()
end

local function autoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  if steamReactor then
    API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
    API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  end
  API.screen()
  checkMode()
  menuMode()
end

local function turbineAutoMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.screen()
  checkMode()
  menuMode()
end

local function turbineManualMenu()
  API.clearTable()
  API.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  API.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  API.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  API.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  API.setTable("Online", setTurbineOnline, "", 3, 13, 8, 8)
  API.setTable("Offline", setTurbineOffline, "", 15, 25, 8, 8)
  API.setTable("Coils On", coilsOn, "", 3, 13, 10, 10)
  API.setTable("Coils Off", coilsOff, "", 15, 25, 10, 10)
  API.screen()
  checkMode()
  turbineOnOff()
  coilsOnOff()
  menuMode()
end

function displayScreen()
 --  repeat
  checkEn()
  displayEn()
  if menuType == "Reactor" then
    fuelRodLevel()
    if mode == "Automatic" then
      autoMenu()
      autoReactor()
    else
      manualMenu()
    end
  else
    turbineInductorDisplay()
    if mode == "Automatic" then
      turbineAutoMenu()
      autoReactor()
    else
      turbineManualMenu()
    end
  end
  getClick()
end

if steamReactor then
  findOptFuelRods()
end

while true do
  displayScreen()
  
  os.sleep(1)
end

 

Share this post


Link to post
Share on other sites
  • 0

Once again I thank you

for the colors API, set if I do not use "true [example mon.setBackground (colors.black)]" nothing is displayed, black screen.
however, if I use "true [example mon.setBackground (colors.black, true)]" all colors are either gray, black or white
(for the test I use full tier 3)
otherwise as I said above, before going to sleep I made many tests and I save over the main code and that's part of the test.

for the party "touch" actually it is for the touch
it's a code that I found on the forum and that I applied to the code

the program launches but after the bug display

Share this post


Link to post
Share on other sites
  • 0

I do not have an error message this time
but a display bug after a few minutes
attached a screen

moreover, when I use the color chart, all the colors are correctly displayed
when I do not use the array and I add true "my.setBackground (colors.white, true)" all colors are replaced with gray, white and black

 

297.png

Share this post


Link to post
Share on other sites
  • 0

Ok, I see. 

With the colors, I would use your custom table, it should be fine, so you don't need to require colors.

Sadly I am really busy right now, so I can't have a look at the code for like 2 days.

It would be great if you could try to do as many tests and improvements as possible, and then I will have a look at your new results.

Share this post


Link to post
Share on other sites
  • 0

a big thank you for your help (we never say it enough :D)

this code may not have been the best code to begin coding especially on opencomputer.
but the fact of going from CC to OC and seeing the code does not work, it made me want to continue and to interest me a little more.
without your help I would not have advanced to the point where I am, I still have progress to make but as we say Rome was not done in 1 day

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.