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

Set Bundled Cable To Default On For All Colors.

Started by Alcheya, 28 September 2013 - 02:54 PM
Alcheya #1
Posted 28 September 2013 - 04:54 PM
I am trying to modify DW20's touchscreen code for my soulshard spawners, but it seems that everything I do is failing. I was able to get it working the way I -thought- the spawners worked at first, active on redstone signal, but I realized it was actually the other way around. I have done several things to try and start the process off with everything on, but when any button is pressed, the rest of them go off, activating all the spawners at once and disabling the one pressed. This was just a proof of concept so I don't have all the mobs and colors listed, but I was hoping to get some help figuring out what I'm doing wrong.

"–Addition" to "–Addition END" and setTables are what I added. I tried using colors.combine from the start, but again, when any button was pressed, they all went off and the button pressed goes on.


Spoiler

local mon = peripheral.wrap("left")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
	
function setTable(name, func, xmin, xmax, ymin, ymax)
   button[name] = {}
   button[name]["func"] = func
   button[name]["active"] = false
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
end


--Addition


rsGet = rs.getBundledOutput("right")

do
rs.setBundledOutput("right", rsGet + colors.red + colors.blue + colors.white + colors.purple + colors.orange + colors.black + colors.green)
end


function zombieSpawn()
  if rs.testBundledInput("right", colors.red) == false then
   rsGet = colors.combine(rsGet, colors.red)
   rs.setBundledOutput("right", rsGet)
  else
   rsGet = colors.subtract(rsGet, colors.red)
   rs.setBundledOutput("right", rsGet)
  end
end

function skeletonSpawn()
  if rs.testBundledInput("right", colors.yellow) == false then
	rsGet = colors.combine(rsGet, colors.yellow)
	rs.setBundledOutput("right", rsGet)
  else
	rsGet = colors.subtract(rsGet, colors.yellow)
	rs.setBundledOutput("right", rsGet)
  end
end

function witherSpawn()
  if rs.testBundledInput("right", colors.green) == false then
	rsGet = colors.combine(rsGet, colors.green)
	rs.setBundledOutput("right", rsGet)
  else
	rsGet = colors.subtract(rsGet, colors.green)
	rs.setBundledOutput("right", rsGet)
end
end

function witchSpawn()
  if rs.testBundledInput("right", colors.purple) == false then
	rsGet = colors.combine(rsGet, colors.purple)
	rs.setBundledOutput("right", rsGet)
  else
	rsGet = colors.subtract(rsGet, colors.purple)
	rs.setBundledOutput("right", rsGet)
  end
end


--Addition END
	  
function fillTable()
   setTable("Skeletons", skeletonSpawn, 5, 25, 4, 8)
   setTable("Zombies", zombieSpawn, 5, 25, 10, 14)
   setTable("Wither Skeletons", witherSpawn, 5, 25, 16, 20)
   setTable("Witches", witchSpawn, 27, 47, 4, 8)
end  

function fill(text, color, bData)
   mon.setBackgroundColor(color)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
   for j = bData["ymin"], bData["ymax"] do
	  mon.setCursorPos(bData["xmin"], j)
	  if j == yspot then
		 for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
			if k == xspot then
			   mon.write(text)
			else
			   mon.write(" ")
			end
		 end
	  else
		 for i = bData["xmin"], bData["xmax"] do
			mon.write(" ")
		 end
	  end
   end
   mon.setBackgroundColor(colors.black)
end
	
function screen()
   local currColor
   for name,data in pairs(button) do
	  local on = data["active"]
	  if on == true then currColor = colors.lime else currColor = colors.red end
	  fill(name, currColor, data)
   end
end
	
function checkxy(x, y)
   for name, data in pairs(button) do
	  if y>=data["ymin"] and  y <= data["ymax"] then
		 if x>=data["xmin"] and x<= data["xmax"] then
			data["func"]()
			data["active"] = not data["active"]
			print(name)
		 end
	  end
   end
end
	
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
	
fillTable()
while true do
   mon.clear()
   heading("Spawner Control")
   screen()
   local e,side,x,y = os.pullEvent("monitor_touch")
   print(x..":"..y)
   checkxy(x,y)
   sleep(.1)
end
H4X0RZ #2
Posted 28 September 2013 - 06:08 PM
Little improvement, don't just add/subtract the colors in rs.setBundledOutput. use colors.combine(color1,color2)
Alcheya #3
Posted 28 September 2013 - 06:56 PM
Little improvement, don't just add/subtract the colors in rs.setBundledOutput. use colors.combine(color1,color2)

Yea, I noted that I did try using colors.combine at first, but that didn't work either. It does the same thing where when a button is pressed, it turns off all the redstone signals and only activates the button pressed.
Lyqyd #4
Posted 28 September 2013 - 09:35 PM
There are a couple functions in my useful snippets topic that may interest you.
Alcheya #5
Posted 29 September 2013 - 08:51 AM
There are a couple functions in my useful snippets topic that may interest you.

Beautiful, the toggle function is exactly what I was looking for. Condensed the code a bunch too, much easier to read and easier to program more buttons in. Thanks a million!