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

Questions about colors.combine

Started by Mnddbbl, 01 March 2012 - 11:25 PM
Mnddbbl #1
Posted 02 March 2012 - 12:25 AM
Hi guys !
Found out computercraft yesterday and as i didn't knew anything about programming, i tried to learn some basic by making a test program :
Basically, it's meant to do the same as the redworks "Bundleworks" one, but with some kind of visuals levers i can toggle on /off by typing enter, and move between levers with arrow keys.
For now the interface works good (cursor moves, cables on are displayed, and enter key seems to works) but when it comes to toggling cables, it start making some shit…
when toggling on, it does toggle the good one, but toggles off all cables that where on. I think i don't understand how colors.combine works, and why i doesn't work like colors.subtract.
Maybe you can help me managing to make it work ?

I've got some strange behavior with the substract and the combine thingy : sometimes (i don't really know why), cursor get blocked after typing enter on a lever, and few seconds later the terminal writes "178" with no more informations. What does it mean, and why it happens ?!

By the way, maybe you can explain me a way to make the first function (redupdate()) less bulky ? I'm pretty sure that isn't the fastest way to make this.

Forgive my bad english, and i hope you can help me fixing this.
Have a nice day :unsure:/>/>







term.clear()
term.setCursorPos(11,1)
print("LeverReplica 1.0")
term.setCursorPos(1,4)
print("[_][_][_][_][_][_][_][_][_][_][_] [_]")

function redupdate()

   brownstate = rs.testBundledInput("back",colors.brown)
   brown = 0
	if brownstate == true then
	 term.setCursorPos(2,4)
	 print("1")
	 brown = 1
	else
	 term.setCursorPos(2,4)
	 print("0")
	 brown = 0
	end
   orangestate = rs.testBundledInput("back",colors.orange)
   orange = 0
	if orangestate == true then
	 term.setCursorPos(5,4)
	 print("1")
	 orange = 1
	else
	 term.setCursorPos(5,4)
	 print("0")
	 orange = 0
	end
   yellowstate = rs.testBundledInput("back",colors.yellow)
   yellow = 0
	if yellowstate == true then
	 term.setCursorPos(8,4)
	 print("1")
	 yellow = 1
	else
	 term.setCursorPos(8,4)
	 print("0")
	 yellow = 0
	end

   limestate = rs.testBundledInput("back",colors.lime)
   lime = 0
	if limestate == true then
	 term.setCursorPos(11,4)
	 print("1")
	 lime = 1
	else
	 term.setCursorPos(11,4)
	 print("0")
	 lime = 0
	end
  greenstate = rs.testBundledInput("back",colors.green)
  green = 0
	if greenstate == true then
	 term.setCursorPos(14,4)
	 print("1")
	 green = 1
	else
	 term.setCursorPos(14,4)
	 print("0")
	 green = 0
	end
  cyanstate = rs.testBundledInput("back",colors.cyan)
  cyan = 0
	if cyanstate == true then
	 term.setCursorPos(17,4)
	 print("1")
	 cyan = 1
	else
	 term.setCursorPos(17,4)
	 print("0")
	 cyan = 0
	end
  pinkstate = rs.testBundledInput("back",colors.pink)
  pink = 0
	if pinkstate == true then
	 term.setCursorPos(20,4)
	 print("1")
	 pink = 1
	else
	 term.setCursorPos(20,4)
	 print("0")
	 pink = 0
	end
  redstate = rs.testBundledInput("back",colors.red)
  red = 0
	if redstate == true then
	 term.setCursorPos(23,4)
	 print("1")
	 red = 1
	else
	 term.setCursorPos(23,4)
	 print("0")
	 red = 0
	end
  magentastate = rs.testBundledInput("back",colors.magenta)
  magenta = 0
	if magentastate == true then
	 term.setCursorPos(26,4)
	 print("1")
	 magenta = 1
	else
	 term.setCursorPos(26,4)
	 print("0")
	 magenta = 0
	end
  purplestate = rs.testBundledInput("back",colors.purple)
  purple = 0
	if purplestate == true then
	 term.setCursorPos(29,4)
	 print("1")
	 purple = 1
	else
	 term.setCursorPos(29,4)
	 print("0")
	 purple = 0
	end
  bluestate = rs.testBundledInput("back",colors.blue)
  blue = 0
	if bluestate == true then
	 term.setCursorPos(32,4)
	 print("1")
	 blue = 1
	else
	 term.setCursorPos(32,4)
	 print("0")
	 blue = 0
	end
  lightBluestate = rs.testBundledInput("back",colors.lightBlue)
  lightBlue = 0
	if lightBluestate == true then
	 term.setCursorPos(36,4)
	 print("1")
	 lightBlue = 1
	else
	 term.setCursorPos(36,4)
	 print("0")
	 lightBlue = 0
	end
end

function cursor()
  term.setCursorPos(x,3)
  term.clearLine()
  print("V")
  term.setCursorPos(x,2)
  term.clearLine()
  print("_")
  term.setCursorPos(25,10)
end
x = 2
y = 3
l = 0
z = 0
  function f1()
while l == 0 do
	cursor()
	event, keynumber = os.pullEvent()
		if keynumber == 203 then
		   if x >= 3 then
			  x = x - y
			  cursor()
		   else
			  l = 0
		  end
		elseif keynumber == 205 then
		   if x <= 35 then
			  x = x + y
			  cursor()
		   else
			  l = 0
		  end
		elseif keynumber == 28 then
			  l = 1
			  togglever()
		elseif keynumber == 57 then
			  z = 1
			
		else
			  l = 0
	  
	   end
	 if x == 35 then
		x = x + 1
	 elseif x == 33 then
		x = x - 1
	end
end
end		
  
function togglever()
if x == 2 then
  if brown == 0 then
   rs.setBundledOutput("back", colors.combine(colors.brown))
  elseif brown == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.brown))
  end
elseif x == 5 then
  if orange == 0 then
   rs.setBundledOutput("back", colors.combine(colors.orange))
  elseif orange == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.orange))
  end
elseif x == 8 then
  if yellow == 0 then
   rs.setBundledOutput("back", colors.combine(colors.yellow))
  elseif yellow == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.yellow))
  end
elseif x == 11 then
  if lime == 0 then
   rs.setBundledOutput("back", colors.combine(colors.lime))
  elseif lime == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.lime))
  end
elseif x == 14 then
  if green == 0 then
   rs.setBundledOutput("back", colors.combine(colors.green))
  elseif green == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.green))
  end
elseif x == 17 then
  if cyan == 0 then
   rs.setBundledOutput("back", colors.combine(colors.cyan))
  elseif cyan == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.cyan))
  end
elseif x == 20 then
  if pink == 0 then
   rs.setBundledOutput("back", colors.combine(colors.pink))
  elseif pink == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.pink))
  end
elseif x == 23 then
  if red == 0 then
   rs.setBundledOutput("back", colors.combine(colors.red))
  elseif red == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.red))
  end
elseif x == 26 then
  if magenta == 0 then
   rs.setBundledOutput("back", colors.combine(colors.magenta))
  elseif magenta == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.magenta))
  end
elseif x == 29 then
  if purple == 0 then
   rs.setBundledOutput("back", colors.combine(colors.purple))
  elseif purple == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.purple))
  end
elseif x == 32 then
  if blue == 0 then
   rs.setBundledOutput("back", colors.combine(colors.blue))
  elseif blue == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.blue))
  end
elseif x == 36 then
  if lightBlue == 0 then
   rs.setBundledOutput("back", colors.combine(colors.lightBlue))
  elseif lightBlue == 1 then
   rs.setBundledOutput("back", colors.subtract(colors.lightBlue))
  end
end
end
while z == 0 do
redupdate()
cursor()
f1()
end

Casper7526 #2
Posted 02 March 2012 - 12:29 AM
Brief example of how to use a variable to keep track of an output.

backoutput = 0

backoutput = colors.combine(backoutput, colors.red)
backoutput = colors.combine(backoutput, colors.blue + colors.yellow)

rs.setBundledOutput("back", backoutput)  -- this would turn on red + blue + yellow

backoutput = colors.subtract(backoutput, colors.blue)

rs.setBundledOutput("back", backoutput) -- this would turn on red + yellow
Work on that part if your code first and get back to us :unsure:/>/>
Mnddbbl #3
Posted 02 March 2012 - 01:31 PM
Thanks for the fast answer !

It works now, so i'll make a fancier UI.
I put the changes here, if someone get the same question :

function redupdate()

   brownstate = rs.testBundledInput("back",colors.brown)
   brown = 0
	if brownstate == true then
	 term.setCursorPos(2,4)
	 print("1")
	 brown = 1
	 backoutput = colors.combine(backoutput, colors.brown)
	else
	 term.setCursorPos(2,4)
	 print("0")
	 brown = 0
	 backoutput = colors.subtract(backoutput, colors.brown)
	end


function togglever()
if x == 2 then
  if brown == 0 then
   backoutput = colors.combine(backoutput, colors.brown)
   rs.setBundledOutput("back", backoutput)
						l = 0
  elseif brown == 1 then
   backoutput = colors.subtract(backoutput, colors.brown)
						rs.setBundledOutput("back", backoutput)
						l = 0
  end


I've made some changes on the loop too, avoiding the too-much-time-without-yelding-crash
That's looks great, really nice mod, funny to use, and now i've got 12 lever on only one block. Yeah !
Now i'll try to play with turtles, so we'll meet again when i'll get stuck :unsure:/>/>

Edit : Just another little question : how can i resize the term screen for that program ONLY ?
Casper7526 #4
Posted 02 March 2012 - 04:40 PM
You can only resize the term for all computers from within the config file.