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?
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.
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.