This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darklight10's profile picture

Help me optimize this program/laugh at my mistakes?

Started by darklight10, 19 November 2012 - 12:31 AM
darklight10 #1
Posted 19 November 2012 - 01:31 AM
Hello! I've been working on a program to automatically calculate the materials required to craft Ultimate Hybrid Solar Panels from an IndustrialCraft2 addon, mainly as an exercise in working with variables. The framework of this program could be used for pretty much any other recipe in-game, but I'm at a loss for optimizing the code, which appears to be fairly sloppy in my mind.

As you can see below, I allow the user to specify a number of panels that they wish to craft, but if they don't specify a number (nil) then the number is 1. Is there a better way to do that? Also, why do I have to use one "=" for lines one and two, and two for line three? Is it because I'm specifying a string instead of a number?

local tArgs = {...}
local numpan = tArgs [1] -- numpan referrs to "number of panels", I guess it could be changed to "num"
if numpan == nil then
  numpan = 1
  end
local size = 1 -- this could be edited by the end user to change text size on montior
local side = "right" -- which side is the monitor on?
local m = peripheral.wrap(side)
m.setTextScale(size)
--Variables for multiplying the number of
--ingredients times the number of panels wanted.
--More can be added, but these should work for
--most recipies. MOST.
local norm1 = numpan -- I realize this isn't necessary, however when I was filling out the code,
--I had numbers in place of how many each panel needed, so it was easier to just write "norm"
--infront of every 1 instead of deleting the 1 and writing "numpan".
local norm2 = numpan*2
local norm3 = numpan*3
local norm4 = numpan*4
local norm8 = numpan*8
local norm14 = numpan*14
local norm16 = numpan*16
local norm24 = numpan*24
local norm128 = numpan*128
shell.run("monitor right clear") -- Clears the monitor AND terminal before displaying everything
shell.run("clear")
term.redirect(m) -- everything below is thrown onto the monitor
print("GOAL: " .. numpan .. " Ultimate Hybrid Solar Panels")
print("Resources required:")
print(norm3 .. " Coal Chunk (1 per chunk)") -- the parenthesis  clarify how many ingredients go
--into one; not needed, but I like to have that information handy.
print("  " .. norm3 .. " stacks of Coal (1 per chunk)")
print("  " .. norm24 .. " Flint (8 per chunk)")
print("  " .. norm1 .. " Obsidian (1 per chunk)")
print(norm2 .. " Sunnarium Alloy (2 per panel)")
print("  " .. norm16 .. " Iridium Plate (8 per sunnarium)")
print("	" .. norm128 .. " Advanced Alloy (4 per plate)")
print("	" .. norm128 .. " Iridium Ore (4 per plate)")
print("	  " .. norm14 .. " stacks UUM (28 pieces per plate)")
print(norm1 .. " Lapis Lazuli Block(s) (1 per panel)")
print(norm1 .. " Advanced Solar Panel (1 per panel)")
print("  " .. norm3 .. " Reinforced Glass (3 per panel)")
print("  " .. norm2 .. " Advanced Alloy (2 per panel)")
print("  " .. norm2 .. " Advanced Circuit (2 per panel)")
print("  " .. norm1 .. " Solar Panel (1 per panel)")
print("  " .. norm1 .. " Advanced Machine Block (1 per panel)")
term.restore() -- control handed back to the computer
The program itself tabs out nicely, though it may be hard to visualize from just the text. Is there a better way for me to do that in the future? That was a lot of staring to make sure I had everything right. Lol.
Before I had this program automate the number of materials necessary, I had everything in "print[[ ]]". I couldn't figure out how to do variables inside the brackets, though. Is that possible? Attached a picture of the program in-game, don't know if you can actually see it.
[attachment=676:2012-11-18_07.27.17.png]

Thank you for your time! Sorry for the many random questions.
billysback #2
Posted 19 November 2012 - 01:41 AM
Also, why do I have to use one "=" for lines one and two, and two for line three? Is it because I'm specifying a string instead of a number?
the "=" operator is used for setting values,
the "==" operator is used for comparing values to see if they are equal to each other.

Most languages which have these sorts of operators do it in a similar way I believe, the first "=" of the "==" operator can also be changed in order to change the comparison type of the two values, for example "~=" in lua is not equal to, or ">=" is greater than or equal to.

instead of:

local norm2 = numpan*2
local norm3 = numpan*3
local norm4 = numpan*4
local norm8 = numpan*8
local norm14 = numpan*14
local norm16 = numpan*16
local norm24 = numpan*24
local norm128 = numpan*128
etc.
do

local norm = {}
local vals = {{"Solar Panel", 1}, {"Adv. Machine Block", 1},  {"Advanced alloy", 2}, {"Advanced Circuit", 2} , {"Coal Stacks", 3},{"UUM Stacks", 14},{"Iridium Plate", 16},{"Flint", 24},{"Advanced Alloy", 128}}

for i=1,#vals do
local dat = vals[i]
norm[dat[1]] = numpan*dat[2]
end

shell.run("monitor right clear") -- Clears the monitor AND terminal before displaying everything
shell.run("clear")
term.redirect(m) -- everything below is thrown onto the monitor
print("GOAL: " .. numpan .. " Ultimate Hybrid Solar Panels")
print("Resources required:")

for k,v in pairs(norm) do
print(k.." :"..v)
end
Doyle3694 #3
Posted 19 November 2012 - 01:44 AM
to explain the = and ==,
= is for setting a value to a variable, telling him "you are this"
== is for asking a variable "are you this?", and he will return the boolean true if he is.


However, for optimizing code, Im not the guy to ask

EDIT: Damn you, ninja'd :)/>/>
remiX #4
Posted 19 November 2012 - 02:03 AM
Well you could make it only work if you provide an argument for the number of panels.


local tArgs = {...}
checkNum = tonumber(tArgs[1])
if #tArgs == 0 or not checkNum then
	print("Usage:n" .. fs.getName(shell.getRunningProgram()) .. " <number of panels>")
	return
end
local numpan = checkNum

EDIT: I fancy'd it up with colours and stuff :)/>/> I didn't change any numbers so should still be perfect.

Code: Pastebin or raw code in the spoiler below.

The monitor has to be atleast 4 high and 5 accross.
Spoiler

local tArgs = {...}
checkNum = tonumber(tArgs[1])
if #tArgs == 0 or not checkNum then
    print("Usage:n" .. fs.getName(shell.getRunningProgram()) .. " <number of panels>")
    return
end

-- Get monitor side
for i, v in pairs(rs.getSides()) do
    if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
        side = v
        break
    end
end
-- Defining Variables
tC = {
    white = 1,
    orange = 2,
    magenta = 4,
    lightBlue = 8,
    yellow = 16,
    lime = 32,
    pink = 64,
    gray = 128,
    lightGray = 256,
    cyan = 512,
    purple = 1024,
    blue = 2048,
    brown = 4096,
    green = 8192,
    red = 16384,
    black = 32768
}

local numpan = checkNum
local size = 1
local norm1 = numpan
local norm2 = numpan*2
local norm3 = numpan*3
local norm4 = numpan*4
local norm8 = numpan*8
local norm14 = numpan*14
local norm16 = numpan*16
local norm24 = numpan*24
local norm128 = numpan*128
local m = peripheral.wrap(side)
m.clear()
mX, mY = m.getSize()
sX, sY = term.getSize()
m.setTextScale(size)


function monW(x, y, col, str, numb, s)
    if x == 0 then
        m.setCursorPos((mX-#str)/2, y)
    else
        m.setCursorPos(x, y)
    end
    if numb and not s then
        m.setTextColour(tC.purple)
        m.write(numb .. " ")
    elseif numb and s then
        m.setTextColour(tC.lightBlue)
        m.write("- ")
        m.setTextColour(tC.purple)
        m.write(numb .. " ")
    end
        
        
    m.setTextColour(tC[col])
    m.write(str)
end

monW(0, 1, "red", "Ultimate Hybrid Solar Panels", numpan)
monW(10, 3, "yellow", "Resources required")
monW(2, 5, "green", "Coal Chunk (1 per chunk)", norm3)
monW(4, 6, "blue", "stacks of Coal (1 per chunk)", norm3, 1)
monW(4, 7, "blue", "Flint (8 per chunk)", norm24, 1)
monW(4, 8, "blue", "Obsidian (1 per chunk)", norm1, 1)
monW(2, 10, "green", "Sunnarium Alloy (2 per panel)", norm2)
monW(4, 11, "blue", "Iridium Plate (8 per sunnarium)", norm16, 1)
monW(4, 12, "blue", "Advanced Alloy (4 per plate)", norm128, 1)
monW(4, 13, "blue", "Iridium Ore (4 per plate)", norm128, 1)
monW(4, 14, "blue", "stacks UUM (28 pieces per plate)", norm14, 1)
monW(2, 16, "green", "Advanced Solar Panel (1 per panel)", norm1)
monW(4, 17, "blue", "Reinforced Glass (3 per panel)", norm3, 1)
monW(4, 18, "blue", "Advanced Alloy (2 per panel)", norm2, 1)
monW(4, 19, "blue", "Advanced Circuit (2 per panel)", norm2, 1)
monW(4, 20, "blue", "Solar Panel (1 per panel)", norm1, 1)
monW(4, 21, "blue", "Advanced Machine Block (1 per panel)", norm1, 1)
monW(2, 23, "green", "Lapis Lazuli Block(s) (1 per panel)", norm1)

term.clear()
term.setCursorPos(1,1)
term.setTextColour(tC.red) write("Printed the required recources to make ")
term.setTextColour(tC.blue) write(numpan)
term.setTextColour(tC.red) write(" Ultimate Hybrid Solar Panels on the monitor to the ")
term.setTextColour(tC.blue) write(side)
term.setTextColour(tC.red) print(".")
term.setTextColour(tC.yellow) print("Enjoy making them!nnCraftOS 1.4")

Pictures:
SpoilerMonitor
Spoiler
Computer terminal
Spoiler
darklight10 #5
Posted 19 November 2012 - 09:22 AM
Thank you, Billy and Doyle for clearing the whole equals sign thing up for me, that does make sense. I'd never thought to actually use a table as part of the program (that's a part of CC code I'm still a bit weary of, but seems like a good time to start experimenting!), I see where that would really make things more compact. I'll also have to investigate using colors, that breaks it up nicely and makes it look awesome, remiX! Thank you a bunch, guys! I love how you took the program and ran with it! I've definitely got some code stealing studying to do.