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

YASDL - Yet Another Simple Door Lock

Started by Xenthera, 28 May 2013 - 07:27 PM
Xenthera #1
Posted 28 May 2013 - 09:27 PM
Earlier someone asked for a re-creation of a door lock they saw on their server. I thought, hey what the heck, it'll take no more than 10 mins.
Anyway this is my first program posted in the programs section, so go easy on me! :P/>

http://pastebin.com/Zd6yQmqG


pastebin get Zd6yQmqG lock

A screenshot, (It's much better using the actual code though. ;)/>)

Mess around with the code, adjust it, use it for other things etc.
lieudusty #2
Posted 28 May 2013 - 09:31 PM
Looks very nice! :D/>
Spongy141 #3
Posted 29 May 2013 - 12:37 AM
Nice for a first program (posted), not really 'simple', more 'average' since it does use buttons and simple is just typing in the password, anyways looks nice :)/>
Xenthera #4
Posted 29 May 2013 - 01:17 AM
Nice for a first program (posted), not really 'simple', more 'average' since it does use buttons and simple is just typing in the password, anyways looks nice :)/>

I would call typing into a terminal 'super simple'. This is code I can write in 5 mins without thinking about it. ;)/>
shauder #5
Posted 31 May 2013 - 08:12 PM
I like this a lot thanks.

Here is a vertical version I made modifying yours. I also fixed it so you can use os.pullEvent = os.pullEventRaw to prevent termination and added 0 to the numbers. When setting the unlock code u must use 10 to represent 0. So 34510 would be 3450 to unlock.



See this for the fix:

for i = 1, #buttons do
-- if there is no value do nothing (for when termination is being attempted)
	   if y ~= nill then
		  if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
			if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
			
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  sleep(0.2)
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  clicked = i
			  if buttons[i]["func"] ~= nil then
				buttons[i]["func"]()
			  
			  
			  end
			end
		  end
		end
	end

Get program:

pastebin get nSXX0mbx lock


Full code for vertical style:
os.pullEvent = os.pullEventRaw
--monitor side
monitorside = "top"

--redstone side
outputside = "left"

--pasword
pass = "1234"

--Countdown timer
waittime = 10

m = peripheral.wrap(monitorside)
x,y = m.getSize()

--button on/off color
bactive = colors.cyan
binactive=colors.gray
--text on/off color
tactive=colors.white
tinactive=colors.white
--Background color
bgcolor = colors.black

function printCentered(text,y)
m.setCursorPos(x/2 - #tostring(text)/2,y)
m.write(text)
end

buttons = {}

	  function newButton(id,xmin,xmax,ymin,ymax,text,func)
		buttons[id] = {}
		buttons[id]["xmin"] = xmin
		buttons[id]["xmax"] = xmax
		buttons[id]["ymin"] = ymin
		buttons[id]["ymax"] = ymax
		buttons[id]["active"] = false
		buttons[id]["text"] = text
		buttons[id]["func"] = func

	  end
	  ---------------------------------------------------------------------------------
	  function printButton(id)
		ymin = buttons[id]["ymin"]
		ymax = buttons[id]["ymax"]
		xmin = buttons[id]["xmin"]
		xmax = buttons[id]["xmax"]
		text = buttons[id]["text"]
		ia = buttons[id]["active"]



			width = xmax - xmin
			height = ymax - ymin

			if ia then m.setBackgroundColor(bactive) m.setTextColor(tactive)
			else m.setBackgroundColor(binactive) m.setTextColor(tinactive) end

			for j = ymin,ymax do
			 m.setCursorPos(xmin,j)
			  for i = xmin,xmax do
				m.write(" ")
			 end
		   end

		m.setCursorPos(xmin + width / 2 - #text / 2 + 1,ymin + height / 2)

	   m.write(text)
	   m.setBackgroundColor(bgcolor)

	  end
	  ----------------------------------------------------------------------------------
	  function refreshButtons()
		for i = 1,#buttons do
		  printButton(i)
		end
	  end
	function checkxy( x,y )
	  for i = 1, #buttons do
		if y ~= nill then
		  if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
			if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  sleep(0.2)
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  clicked = i
			  if buttons[i]["func"] ~= nil then
				buttons[i]["func"]()
			  end
			end
		  end
		end
	  end
	end

permbactive = bactive

function countDown(time)
m.clear()
for i = time, 0, -1 do


m.setCursorPos(9,3)
if i >= 10 then
i = string.sub(i,1,2)
else
i = string.sub(i,1,1)
end
m.clearLine(3)
if tonumber(i) > time / 2 then
m.setTextColor(colors.lime)
elseif tonumber(i) > time / 4 then
m.setTextColor(colors.orange)
elseif tonumber(i) > time / 8 then
m.setTextColor(colors.red)
end


m.write(i)
sleep(1)
end
m.clear()
end

function allButtons(active)
for i = 1, #buttons do
buttons[i]["active"] = active
refreshButtons()
end
end

function blink(color)

bactive = color
allButtons(true)
sleep(.2)
allButtons(false)
sleep(.2)
allButtons(true)
sleep(.2)
allButtons(false)
sleep(.2)
bactive = permbactive
end

checkpass = ""

function append()
if clicked ~= 11 then
checkpass = checkpass..clicked

else
checkpass = ""

end

if #checkpass == #pass then
if checkpass == pass then
blink(colors.lime)
rs.setOutput(outputside, true)
countDown(waittime)
rs.setOutput(outputside, false)
checkpass = ""
else
blink(colors.red)
checkpass = ""
end
end

clicked = nil

end

newButton(1, 1,3,1,1,"1",append)
newButton(2, 5,7,1,1,"2",append)
newButton(3, 1,3,3,3,"3",append)
newButton(4, 5,7,3,3,"4",append)
newButton(5, 1,3,5,5,"5",append)
newButton(6, 5,7,5,5,"6",append)
newButton(7, 1,3,7,7,"7",append)
newButton(8, 5,7,7,7,"8",append)
newButton(9, 1,3,9,9,"9",append)
newButton(10, 5,7,9,9,"0",append)
newButton(11, 1,7,11,11,"C",append)
------------------------------------------------

m.clear()
refreshButtons()

--main loop
while true do
e,side,x,y = os.pullEvent("monitor_touch")
checkxy(x,y)

refreshButtons()
end
Xenthera #6
Posted 10 June 2013 - 03:00 PM
Spoiler
I like this a lot thanks.

Here is a vertical version I made modifying yours. I also fixed it so you can use os.pullEvent = os.pullEventRaw to prevent termination and added 0 to the numbers. When setting the unlock code u must use 10 to represent 0. So 34510 would be 3450 to unlock.



See this for the fix:

for i = 1, #buttons do
-- if there is no value do nothing (for when termination is being attempted)
	   if y ~= nill then
		  if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
			if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
			
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  sleep(0.2)
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  clicked = i
			  if buttons[i]["func"] ~= nil then
				buttons[i]["func"]()
			  
			  
			  end
			end
		  end
		end
	end

Get program:

pastebin get nSXX0mbx lock


Full code for vertical style:
os.pullEvent = os.pullEventRaw
--monitor side
monitorside = "top"

--redstone side
outputside = "left"

--pasword
pass = "1234"

--Countdown timer
waittime = 10

m = peripheral.wrap(monitorside)
x,y = m.getSize()

--button on/off color
bactive = colors.cyan
binactive=colors.gray
--text on/off color
tactive=colors.white
tinactive=colors.white
--Background color
bgcolor = colors.black

function printCentered(text,y)
m.setCursorPos(x/2 - #tostring(text)/2,y)
m.write(text)
end

buttons = {}

	  function newButton(id,xmin,xmax,ymin,ymax,text,func)
		buttons[id] = {}
		buttons[id]["xmin"] = xmin
		buttons[id]["xmax"] = xmax
		buttons[id]["ymin"] = ymin
		buttons[id]["ymax"] = ymax
		buttons[id]["active"] = false
		buttons[id]["text"] = text
		buttons[id]["func"] = func

	  end
	  ---------------------------------------------------------------------------------
	  function printButton(id)
		ymin = buttons[id]["ymin"]
		ymax = buttons[id]["ymax"]
		xmin = buttons[id]["xmin"]
		xmax = buttons[id]["xmax"]
		text = buttons[id]["text"]
		ia = buttons[id]["active"]



			width = xmax - xmin
			height = ymax - ymin

			if ia then m.setBackgroundColor(bactive) m.setTextColor(tactive)
			else m.setBackgroundColor(binactive) m.setTextColor(tinactive) end

			for j = ymin,ymax do
			 m.setCursorPos(xmin,j)
			  for i = xmin,xmax do
				m.write(" ")
			 end
		   end

		m.setCursorPos(xmin + width / 2 - #text / 2 + 1,ymin + height / 2)

	   m.write(text)
	   m.setBackgroundColor(bgcolor)

	  end
	  ----------------------------------------------------------------------------------
	  function refreshButtons()
		for i = 1,#buttons do
		  printButton(i)
		end
	  end
	function checkxy( x,y )
	  for i = 1, #buttons do
		if y ~= nill then
		  if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
			if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  sleep(0.2)
			  buttons[i]["active"] = not buttons[i]["active"]
			  refreshButtons()
			  clicked = i
			  if buttons[i]["func"] ~= nil then
				buttons[i]["func"]()
			  end
			end
		  end
		end
	  end
	end

permbactive = bactive

function countDown(time)
m.clear()
for i = time, 0, -1 do


m.setCursorPos(9,3)
if i >= 10 then
i = string.sub(i,1,2)
else
i = string.sub(i,1,1)
end
m.clearLine(3)
if tonumber(i) > time / 2 then
m.setTextColor(colors.lime)
elseif tonumber(i) > time / 4 then
m.setTextColor(colors.orange)
elseif tonumber(i) > time / 8 then
m.setTextColor(colors.red)
end


m.write(i)
sleep(1)
end
m.clear()
end

function allButtons(active)
for i = 1, #buttons do
buttons[i]["active"] = active
refreshButtons()
end
end

function blink(color)

bactive = color
allButtons(true)
sleep(.2)
allButtons(false)
sleep(.2)
allButtons(true)
sleep(.2)
allButtons(false)
sleep(.2)
bactive = permbactive
end

checkpass = ""

function append()
if clicked ~= 11 then
checkpass = checkpass..clicked

else
checkpass = ""

end

if #checkpass == #pass then
if checkpass == pass then
blink(colors.lime)
rs.setOutput(outputside, true)
countDown(waittime)
rs.setOutput(outputside, false)
checkpass = ""
else
blink(colors.red)
checkpass = ""
end
end

clicked = nil

end

newButton(1, 1,3,1,1,"1",append)
newButton(2, 5,7,1,1,"2",append)
newButton(3, 1,3,3,3,"3",append)
newButton(4, 5,7,3,3,"4",append)
newButton(5, 1,3,5,5,"5",append)
newButton(6, 5,7,5,5,"6",append)
newButton(7, 1,3,7,7,"7",append)
newButton(8, 5,7,7,7,"8",append)
newButton(9, 1,3,9,9,"9",append)
newButton(10, 5,7,9,9,"0",append)
newButton(11, 1,7,11,11,"C",append)
------------------------------------------------

m.clear()
refreshButtons()

--main loop
while true do
e,side,x,y = os.pullEvent("monitor_touch")
checkxy(x,y)

refreshButtons()
end

You can just add an if statement to append()


function append()
if clicked ~= 11 then
   if clicked == 10 then clicked = 0 end
   checkpass = checkpass..clicked
else
   checkpass = ""
end

No need for "34101" to = "3401"

Also, thanks for modifying it, it's really cool when someone does that kind of stuff. :)/>
MCuniverse #7
Posted 12 June 2013 - 05:13 AM
I don't think YASDL is another simple door lock. More like an intermediate door lock.
natenat3p #8
Posted 12 June 2013 - 09:15 AM
can this be used to emit a redstone signal? like for an iron door?

Just wondering because I might have to use this for my server. and then maybe use mffs to make sure that you have to use the keypad thing. XD
Xenthera #9
Posted 12 June 2013 - 07:29 PM
can this be used to emit a redstone signal? like for an iron door?

Just wondering because I might have to use this for my server. and then maybe use mffs to make sure that you have to use the keypad thing. XD

That's actually exactly what is does. You can change the side of output in the code.