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

Bundled cable troubles.

Started by plazter, 23 August 2016 - 01:14 PM
plazter #1
Posted 23 August 2016 - 03:14 PM
Hello again pros!

- I am trying to make a simple code for a few spawners, and want to use bundled cables, but i've hit a brick wall… im not sure what im doing wrong.. the code works more or less,
but its not saving the colors thats allready active in the table i want to use for it..

(Sorry for the bad describtion)

http://pastebin.com/0ynx1UvH

Spoiler

col = {
[1] = 0,	 -- Inactive
[2] = 1,	 -- White
[3] = 2,	 -- Orange
[4] = 4,	 -- Magenta
[5] = 8,	 -- Light Blue
[6] = 16,	-- Yellow
[7] = 32,	-- Lime
[8] = 64,	-- Pink
[9] = 128,   -- Gray
[10] = 256,   -- Light Gray
[11] = 512,   -- Cyan
[12] = 1024,  -- Purple
[13] = 2048,  -- Blue
[14] = 4096,  -- Brown
[15] = 8192,  -- Green
[16] = 16384, -- Red
[17] = 32768, -- Black
}
on = {}
function getOn()
  for i = 1,#col do
	if rs.getBundledOutput("right") == col[i] then
	  table.insert(on, col[i])
	end
  end
end

getOn()
for i = 1,#on do
print(on[i])
end
input = read()
if input then  
   if input == "white" then
	 if not on == col[1] then
   for i = 1,#on do
			rs.setbundledOutput("right", col[2]+on[i])
	  end
	 else
		 rs.setBundledOutput("right", col[2])
  end
	end
	
	if input == "green" then
   if not on == col[1] then
   for i = 1,#on do
			rs.setbundledOutput("right", col[2]+on[i])
	  end
  else
		 rs.setBundledOutput("right", col[15])  
  end  
end
	
	if input == "red" then
	 if not on == col[1] then
   for i = 1,#on do
		  rs.setbundledOutput("right", col[2]+on[i])
	 end
  else
   rs.setBundledOutput("right", col[16])
  end
end
end

Hope you can help me!!

Regards Plazter
Dragon53535 #2
Posted 23 August 2016 - 03:25 PM
getBundledOutput doesn't work like you think it does. It gives back a 16bit number in which each of the individual bits corresponds to a different wire being on or off, 1 or 0.

There is a method called colors.test() in which it tests if a number, usually 16 bit color code, contains another number, 16 bit color code.


if(colors.test(rs.getBundledOutput("right"),colors.green)) then
--#If the input from the right has the color green on.
end
You can use this to setup an if-else statement in which it tests for colors that are on/off.
Or you could of course use this to test for if the color is in the output and then save it.

Another way to do so, would be to grab the Output number and work in order backwards and modulus each number, starting at bit 16, and down to bit 1, if the modulus is 0, then the output contains that color. If it contains that number, subtract it from the total.
Edited on 23 August 2016 - 01:36 PM
plazter #3
Posted 23 August 2016 - 03:36 PM
Spoiler
getBundledOutput doesn't work like you think it does. It gives back a 16bit number in which each of the individual bits corresponds to a different wire being on or off, 1 or 0.

There is a method called colors.test() in which it tests if a number, usually 16 bit color code, contains another number, 16 bit color code.


if(colors.test(rs.getBundledOutput("right"),colors.green)) then
--#If the input from the right has the color green on.
end
You can use this to setup an if-else statement in which it tests for colors that are on/off.
Or you could of course use this to test for if the color is in the output and then save it.

Another way to do so, would be to grab the Output number and work in order backwards and modulus each number, starting at bit 16, and down to bit 1, if the modulus is 0, then the output contains that color.

uhm.. so 1 to 16 .. it returns with the correct number (heximal) from the cc wiki :s..
but how would i get it to add the colors aswell tho? thats kinda the problem currently :P/>
Dragon53535 #4
Posted 23 August 2016 - 03:38 PM
Adding the colors to the output is easy as it's just a number and each power of 2 is literally a single binary digit. Add or subtract the number.
plazter #5
Posted 23 August 2016 - 03:41 PM
well its easy to hard code yes, but theres more colors to come since i got a lot of spawners on the server im playing on :)/>, kinda why i want it to automatic remember the colors thats on and unless i click the right button it goes off ofc.. (not sure that made sense :B)/>
Dragon53535 #6
Posted 23 August 2016 - 03:44 PM
So you want persistent storage of the number?

File Tutorial

If I misinterpreted that and you mean you don't know how to set new colors in with the old. Just set the output with the old or new color subtracted or added in.


local num = rs.getBundledOutput("right")
num = num - 256
rs.setBundledOutput("right",num)
plazter #7
Posted 23 August 2016 - 03:47 PM
So you want persistent storage of the number?

File Tutorial

If I misinterpreted that and you mean you don't know how to set new colors in with the old. Just set the output with the old or new color subtracted or added in.


local num = rs.getBundledOutput("right")
num = num - 256
rs.setBundledOutput("right",num)

not persistant (i think) but just keep the ones thats on at the moment (currently got 8 spawners connected to a bundled cable with levers.. really ugly :P/>)
but as u can i see i tried to make the current "signal" to be saved in the table on.

-snip-
num = num - 256

Whats the 256 for? O_o
Dragon53535 #8
Posted 23 August 2016 - 04:48 PM
The second part should be what you want then. The 256 is just one of the bits, in this case LightGray is the color.

You could of course use colors.add and colors.subtract, but I like dealing with the numbers myself
Edited on 23 August 2016 - 02:49 PM
plazter #9
Posted 23 August 2016 - 05:11 PM
Ah ok, ill look at it once im a little more fresh :P/> drove tired in it xD
Lyqyd #10
Posted 23 August 2016 - 07:52 PM
It's better to use colors.add and colors.subtract, as these will ensure that the value remains unchanged if the color was already present or already absent.