is there a formula that's giving me the original color numbers instead off a total sum?
colours.white = 1
colours.orange = 2
if both of theme is turned on i'm getting number 3 as an input, but i want number 1 and 2
if rs.testBundledInput( "left", colors.white) then
print("White is on")
end
if rs.testBundledInput("left", colors.red) then
print("Red is on")
--# etc for the other colours
end
input = rs.getBundledInput("left")
isRedOn = colors.test(input, colors.red)
isBlackOn = colors.test(input, colors.black)
isGreenOn = colors.test(input, colors.green)
-- etc
isRedGreenOn = colors.test(input,(colors.red + colors.green))
and what if there is 3 colors active at the same time?isRedGreenOn = colors.test(input,(colors.red + colors.green + colors.black))
how many combination is there?
local function getInput(side)
local input = {} --#Store our input here
for i=1,16 do --#For every color
input[2^(i-1)] = rs.testBundledInput(side, 2^(i-1))
end
return input
end
local bundledInput = getInput()
if bundledInput[colors.red] and bundledInput[colors.green] then
print("Red & green on.")
end
if rs.testBundledInput("left", colors.red) and rs.testBundledInput("left",colors.blue) then
print("Red and green on.")
end
for i = 1,#input do
for i2 = 1,#input do
if bundledInput[input[i]] and bundledInput[input[i2]] then
print(input[i].." & "..input[i2].." is on")
end
end
end
it may only print numbers right now, but doing something with that shouldn't be a problemWell, you do it individually for each colour, and then you doimmibis. what if the input is 'red' and 'green'?and what if there is 3 colors active at the same time?isRedGreenOn = colors.test(input,(colors.red + colors.green))
how many combination is there?isRedGreenOn = colors.test(input,(colors.red + colors.green + colors.black))
just have to be an easier way to solve this…
isRedGreenOn = isRedOn and isGreenOn
there is 148 of different color combination when there is 1 or 2 active. do the math for all 16Well, you do it individually for each colour, and then you doisRedGreenOn = isRedOn and isGreenOn
What do you actually want to find out from the bundled cable?
What do you actually want to find out from the bundled cable?
witch color is turned on or off, but it have to accept multiple colors
if rs.testBundledInput("left", colors.green) and rs.testBundledInput("left", colors.blue) then
--#stuff
elseif rs.testBundledInput("left", colors.white) then
--#stuff
end
if rs.testBundledInput("left", colors.blue) then
--#stuff
end
if rs.testBundledInput("left", colors.green) then
--#stuff
end
local response = {
[colors.green] = function(state)
--#respond to the state variable, which will be a bool
end;
[colors.blue] = function(state)
end;
}
for i=1,16 do
if response[2^(i-1)] then --#If the response table has a function corresponding to the color
response[2^(i-1)](rs.testBundledInput("left", 2^(i-1))) --#Run the function with the cable state as an argument
end
end
if rs.testBundledInput("back",colors.blue) then
print("blue")
elseif rs.testBundledInput("back",colors.brown) then
print("brown")
elseif rs.testBundledInput("back",colors.brown) and rs.testBundledInput("back",colors.blue) then
print("blue,brown")
else
print("noop")
end
the problem with this is, as long blue is active it will only print 'blue' because it is the first ting it checks
if rs.testBundledInput("left", colors.blue) then
--#stuff
end
if rs.testBundledInput("left", colors.green) then
--#stuff
end
If I'm understanding you right, you want a function that:
If white is on and everything else is off, returns white
If orange is on and everything else is off, returns orange
<same for all the other colours>
If white is on, orange is on, and everything else is off, returns ??????
<same for all the other combinations>
What's the ??????
and how can i make this under 100 lines and still cover all possible color combinations?if rs.testBundledInput("left", colors.blue) then --#stuff end if rs.testBundledInput("left", colors.green) then --#stuff end
Or if want to get rid of all those ugly if statements (my favorite):local response = { [colors.green] = function(state) --#respond to the state variable, which will be a bool end; [colors.blue] = function(state) end; } for i=1,16 do if response[2^(i-1)] then --#If the response table has a function corresponding to the color response[2^(i-1)](rs.testBundledInput("left", 2^(i-1))) --#Run the function with the cable state as an argument end end
binary = {0,0,0,0,0,0,0,0.....}
if rs.testBundledInput("left", colors.white) then
binary[16] = 1
else
binary[16] = 0
end
if rs.testBundledInput("left", colors.orange) then
binary[15] = 1
else
binary[15] = 0
end
for i = 1,#binary do
if binary[i] = 1 then
--do stuff
end
end
If I'm understanding you right, you want a function that:
If white is on and everything else is off, returns white
If orange is on and everything else is off, returns orange
<same for all the other colours>
If white is on, orange is on, and everything else is off, returns ??????
<same for all the other combinations>
What's the ??????
close. if white is on and everything else is off. i want it to print whats on and whats off on a screen
input = rs.getBundledInput("left")
isRedOn = colors.test(input, colors.red)
isBlackOn = colors.test(input, colors.black)
-- repeat for other colours
if isRedOn then print("red: on") else print("red: off") end
if isBlackOn then print("black: on") else print("black: off") end
-- repeat for other colours
binary = {0,0,0,0,0,0,0,0.....} if rs.testBundledInput("left", colors.white) then binary[16] = 1 else binary[16] = 0 end if rs.testBundledInput("left", colors.orange) then binary[15] = 1 else binary[15] = 0 end
something like this?
local combos = {
[1] = {
colors.red, colors.blue, colors.green, --#So these would be stored as numerical indexes, meaning that we could unpack them without issue
func = function() --#This is not a numerical index, so it would not be unpacked
print("red blue and green on")
end
};
[2] = {
colors.white, colors.orange, colors.yellow,
func = function()
print("white orange and yellow on")
end
};
}
local function testMultipleInputs(...) --#Takes any number of arguments and tests whether the
--#inputs are true or not. If any one of them is false, the function returns false.
local args = {...}
for i=1,#args do
if not rs.testBundledInput(args[i]) then
return false
end
end
return true
end
for i=1,#combos do --#For every combo
if testMultipleInputs(unpack(combos[i])) then --Check if all the inputs are true
combos[i]["func"]() --#If they are then execute the function associated with the combo.
end
end
local currentlyActive = {}
local colorCodes = {
[1] = "white";
[2] = "orange";
[4] = "magenta";
[8] = "lightBlue";
--#You get the gist. You can see the rest of these under rom/apis/colors
}
for i=1,16 do
if rs.testBundledInput("left", 2^(i-1)) then
table.insert(currentlyActive, colorCodes[2^(i-1)])
end
end
local function printInput()
for i=1,#currentlyActive do
write(currentlyActive[i].." is active, ")
end
end
printInput()
-- there should be an API for this
function parts(bin)
ret = {}
for i=1,16 do
if bin%2^i>=2^i then
table.insert(ret,i)
end
end
return ret
end
-- you would want to call this
parts(rs.getBundledInput('left'))
-- and get this
{1,2}
Why not do it in an understandable way, and use colors.test?Why not do it on a mathematical way, since every color can be on just once, you could substract the next lower value of the sum. If there is a rest above zero the color is on. Lets take maybe 33537. You sub the next lower that is 32768. Now rest is 769. Now you know - black is on. Next lower to 769 is 512. Rest is 257. Ok, we know 512 is on, whatever the hell this color is. Now we come to 256 is on and one is on. Color 32768 is true and so on…
Edit: when finally zero is reached, you know you got them all..