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

Refinery Control problem

Started by mttasch1, 28 September 2012 - 08:47 PM
mttasch1 #1
Posted 28 September 2012 - 10:47 PM
Working on a script that will make a terminal act as the central control unit to an oil refining facility. Little harder than I expected…




local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local Command = stop

while Command == stop do
 term.clear()
 term.setCursorPos(1, 1)
 print("What refinery command would you like to execute?\nIf you don't know what to type, type 'help')")
 Command = read()
while Command ~= stop do 
  if Command==test then
  term.clear()
  term.setCursorPos(1, 1)
  print("This is a test")
  sleep(3)
  Command = stop
  sleep(1)
 end
 if Command == help then
  term.clear()
  term.setCursorPos(1, 1)
  print("You may type the following:")
  print("'help' to display this text.")
  print("'alpha primary on' to turn the Alpha Primary Engines on.")
  print("Type 'stop' on any screen to go back to command line")
  Command = read()
end

What I am trying to achieve is an infinite loop that when a user enters a listed command, it will execute that command and reset itself. But I have been unable to achieve that, Help? Yes this code is very much unfinished…
Cranium #2
Posted 28 September 2012 - 10:52 PM
You need to have your commands in quotations. Right now, you are comparing if they are equal to varibles like stop, help, and test. If you put them in quotes, you are comparing to strings that read like that.
mttasch1 #3
Posted 28 September 2012 - 10:59 PM
Well that is much better… But I am still at the problem where when a command is executed, the script ends. So how would I make this run indefinitely?
Cranium #4
Posted 28 September 2012 - 11:07 PM
Instead of doing while command == stop do, you can do while true do, and then use break to exit out when you want to. For example:

while true do --start infinite loop
local input = read()
if input == "stop" then
break --exits the loop.
end
end
mttasch1 #5
Posted 28 September 2012 - 11:15 PM
I don't quite understand how that would work. [while Command == "stop" do] accomplishes what it needs to, but only once. So when Command ~="stop", it will execute the command, but the problem I have is that it will not return to the [while Command == "stop" do] loop. Will break keep the loop running so that when Command is returned to the "stop" string, it will re-enter the [while Command == "stop" do] loop?
dimitriye98 #6
Posted 28 September 2012 - 11:18 PM
Don't use break
Break is bad programming
dimitriye98 #7
Posted 28 September 2012 - 11:20 PM
Here is a template of how you would do what you want:


local bRunning = true
while bRunning do --Set bRunning to false when you want to stop the whole thing.
  local sCommand = ""
  while sCommand ~= "stop" do
    --Do stuff
  end
end
Cranium #8
Posted 28 September 2012 - 11:21 PM
Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use…
I use break quite often in my programming, so I have no idea how you think it's bad.
dimitriye98 #9
Posted 28 September 2012 - 11:25 PM
Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use…
I use break quite often in my programming, so I have no idea how you think it's bad.

Break is just a glorified GOTO.

You want your program to use control flows, not jumps. In the real world break is only used for exiting on abnormal conditions.
dimitriye98 #10
Posted 28 September 2012 - 11:26 PM
Don't get me wrong, there are situations where using break is justifiable, but for the sake of readability, use of it is generally frowned upon.
mttasch1 #11
Posted 28 September 2012 - 11:28 PM
Really? How is break bad programming? If you want a program to run until you give a command, then go to the previous loop or shell, then break is the perfect thing to use…
I use break quite often in my programming, so I have no idea how you think it's bad.

I did not create this thread to offend people, just to get help. That sounds more like what I am trying to accomplish than dimitri's latter post, So once I put the break in

while true do
 term.clear()
 term.setCursorPos(1, 1)
 print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
 Command = read()
 break
end
it should be kept continuously running? So when a command is executed and the Command variable returns to it's "stop" variable, it will continue that loop?
Cranium #12
Posted 28 September 2012 - 11:31 PM
I would disagree. I will only say that break is a tool, as is GOTO. If what I want to do is accomplished by the code and script I give it, who the hell cares? But this is not the place to argue proper syntax. I think either would be able to help mttasch1, if implemented properly.
Cranium #13
Posted 28 September 2012 - 11:33 PM

while true do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
Command = read()
if Command == "stop" then break --if given the stop command, stop the loop
elseif Command =="somethingElse" then
   --do something else
 end
end
It's used more like this.
dimitriye98 #14
Posted 28 September 2012 - 11:37 PM
while true do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
Command = read()
break
end
That break is useless…
It simply terminates the loop at the end of first execution. Here:

local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local bRunning = true
while bRunning do
  term.clear()
  term.setCursorPos(1, 1)
  print("What refinery command would you like to execute?nIf you don't know what to type, type 'help')")
  local Command
  while Command ~= "stop" do
	Command = read()
	if Command=="test" then
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("This is a test")
	  sleep(3)
	  Command = "stop"
	  sleep(1)
	elseif Command == "help" then
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("You may type the following:")
	  print("'help' to display this text.")
	  print("'alpha primary on' to turn the Alpha Primary Engines on.")
	  print("Type 'stop' on any screen to go back to command line")
	  Command = read()
	end
  end
end
That should work.

Edit: Damn forum software makes my perfectly well indented code look weird…
mttasch1 #15
Posted 28 September 2012 - 11:40 PM
Can I have more than one elseif in the code? or it does not work that way? Bear in mind there will be more than "test" and "help".
dimitriye98 #16
Posted 28 September 2012 - 11:43 PM
It works fine but there's an infinite loop, infinite loops = bad.
Instead of "while true do" use:

local bRunning = true
while bRunning do
That makes it possible to exit without a break which is as I said before, bad.

Edit: also replace this line:

if Command == "stop" then break
with this:

if command == "stop" then bRunning = false

2nd edit: Also, being labeled a "Script Kiddie" is demeaning :P/>/>
mttasch1 #17
Posted 29 September 2012 - 12:05 AM
YES! It works, and actually I accomplished it with both of your code. So thank you gentlemen of this thread for your help!
Cranium #18
Posted 29 September 2012 - 12:08 AM
Dimitriye98 is right. bRunning will work. I just never liked doing it that way. So now we have one scripter who likes it his way, but accepts others as being valid, and uses either, and one scripter vehemently denouncing a tool that works. Do what you want. Either works.
dimitriye98 #19
Posted 29 September 2012 - 12:16 AM
I'm not making up the no break thing. That's what they teach you in programming class in school. GOTO, break, and continue make code difficult to read and become huge problems if you ever work in a collaborative project.
mttasch1 #20
Posted 29 September 2012 - 12:17 AM

local Alphaprimary = false
local Alphasecondary = false
local Betaprimary = false
local Betaprimary = false
local bCommand = true
while bCommand do
term.clear()
term.setCursorPos(1, 1)
print("(If you don't know what to type, type 'help')nExecute Refinery Command:")
input = read()
if input == "help" then
  term.clear()
  term.setCursorPos(1, 1)
  print("You may type the following:")
  print("*")
  print("'help' to display this text.")
  print("*")
  print("'alpha primary on' to turn the Alpha Primary Engines on.")
  print("*")
  print("PRESS ANY KEY TO CONTINUE")
  Command = read()
end
if input == "alpha primary on" then
  term.clear()
  term.setCursorPos(1, 1)
  redstone.setOutput("back", true)
  print("Alpha Primary Engines Activated!")
  sleep(3)
end
if input == "alpha primary off" then
  term.clear()
  term.setCursorPos(1, 1)
  redstone.setOutput("back", false)
  print("Alpha Primary Engines Deactivated!")
  sleep(3)
end
end

This is what I have, and it works. I would like to know of alternatives to accomplishing what has been achieved here, I do not feel I am leaving this thread with a good understanding of what break actually does. Oh and another thing, would there be a way to make a terminal power more than top, bottom, left, right, front and back? I mean I have to dictate what these sides power, but I have only 3 open spaces with 4 things that need powered.
Cranium #21
Posted 29 September 2012 - 12:44 AM
Instead of having several if statements, you can use one if, as many elseif's as you want, and only one end. This is much easier for coding.
As for the power, you can only output a maximum of six sides. If you have RedPower installed(another mod), you can use bundled cable to output up to 16 different signals on each side.
NIN3 #22
Posted 29 September 2012 - 12:46 AM
I'm not making up the no break thing. That's what they teach you in programming class in school. GOTO, break, and continue make code difficult to read and become huge problems if you ever work in a collaborative project.
Ok, im sorry here, but I have to get my 2 bits in.

Why are you so anti-break? I've tryed it, ive never really liked it, but I wouldent run around the forums nay saying it. Your teacher may have told you that its non proper coding, but then when your learning some times the easyest way is the best. Just like protecting from from Control+T. Everyone puts pull.event = pullevent raw (not quite how its written) But no one uses pcall(), which is in my opinion, a better and more controlled alternitive.

(Im not saying pcall() is the be all end all eather)


On topic, it seams im late to the show and have nothing really to add.
mttasch1 #23
Posted 29 September 2012 - 12:50 AM
Well, I definitely have a better understanding of Computercraft now :P/>/>. This was my first big project, so thank you guys for your help!
Cranium #24
Posted 29 September 2012 - 12:52 AM
Always glad to help. Feel free to ask any questions here. There are no stupid questions, unless you didn't search for the answer first.