Posted 12 May 2013 - 12:51 PM
I came back to Minecraft just the other day because I found out about ComputerCraft and was playing around a little bit with it and instantly a lot of ideas were flying through my head what one could do with this great addon.
I know there are a ton of door lock programs here and I have also seen a couple of programs using the advanced monitors with a touchpad. Still I determined a touchpad door lock should be my first program using ComputerCraft and LUA. I have tried to keep everything as generic as possible.
The beginning of the code defines a couple of variables which should make it easy to customize the code to different setups without having to go through the whole thing. Here's what I tried to keep in mind when writing this:
- password length should be dynamic (have a longer password if you like without having to change the code)
- the output of the redstone signal and the location of the monitor relative to the computer should be easily customizable without having to touch the code itself
The output to the monitor is coded for a single monitor setup. One thing I'd like to add later is to dynamically change the output (e.g. center it on screen) depending on the monitor size attached to the computer.
So maybe some newbies like me might have a use for these lines of code and maybe the Pros have some suggestions on how to make this thing more efficient/elegant.
Here we go:
What do you think?
I know there are a ton of door lock programs here and I have also seen a couple of programs using the advanced monitors with a touchpad. Still I determined a touchpad door lock should be my first program using ComputerCraft and LUA. I have tried to keep everything as generic as possible.
The beginning of the code defines a couple of variables which should make it easy to customize the code to different setups without having to go through the whole thing. Here's what I tried to keep in mind when writing this:
- password length should be dynamic (have a longer password if you like without having to change the code)
- the output of the redstone signal and the location of the monitor relative to the computer should be easily customizable without having to touch the code itself
The output to the monitor is coded for a single monitor setup. One thing I'd like to add later is to dynamically change the output (e.g. center it on screen) depending on the monitor size attached to the computer.
So maybe some newbies like me might have a use for these lines of code and maybe the Pros have some suggestions on how to make this thing more efficient/elegant.
Here we go:
-- Variables -----------------------------------------------
local password = "123"
local keypadtable = {{3,2,"1"}, {4,2,"2"}, {5,2,"3"}, {3,3,"4"}, {4,3,"5"}, {5,3,"6"}, {3,4,"7"}, {4,4,"8"}, {5,4,"9"}}
local dooropendelay = 4
local errordelay = 3
local monitorlocation = 'back'
local redstonesignalside = 'left'
------------------------------------------------------------
local monitor = peripheral.wrap(monitorlocation)
local maxx, maxy = monitor.getSize()
local function idleScreen ()
monitor.clear()
monitor.setCursorPos(2,2)
monitor.write("Enter")
monitor.setCursorPos(3,3)
monitor.write("PIN")
end
local function loginOK ()
monitor.clear()
monitor.setCursorPos(1,3)
monitor.write("Welcome")
end
local function loginFail ()
monitor.clear()
monitor.setCursorPos(2,3)
monitor.write("Error")
end
local function printKeyPad ()
monitor.clear()
for i=1,#keypadtable do
monitor.setCursorPos(keypadtable[i][1],keypadtable[i][2])
monitor.write(keypadtable[i][3])
end
end
local function getKeyFromPad (xclick, yclick)
for i=1,#keypadtable do
if ((keypadtable[i][1] == xclick) and (keypadtable[i][2] == yclick)) then
return (keypadtable[i][3])
end
end
return ('')
end
local function openDoor ()
redstone.setOutput (redstonesignalside, true)
end
local function closeDoor ()
redstone.setOutput (redstonesignalside, false)
end
-- MAIN ------------------------------------------------------------
while true do
idleScreen()
local pwentered = ''
-- Get first keypress and show pinpad
local event, monside, xpos, ypos = os.pullEvent()
if event == 'monitor_touch' then
print ('* Starting keypad entry')
printKeyPad()
end
while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) do
local pressed = ''
local event, monside, xpos, ypos = os.pullEvent()
if event == 'monitor_touch' then
local pressed = tostring(getKeyFromPad (xpos, ypos))
if pressed ~= "" then
pwentered = pwentered .. pressed
print ("* Pressed: " .. pressed)
end
end
end
if (pwentered == password) then
print ('* Login SUCCESS')
loginOK ()
openDoor ()
sleep (dooropendelay)
closeDoor ()
else
print ('* Login FAIL')
loginFail ()
sleep (errordelay)
end
end
What do you think?