Advertisement
CodeCrafter

With lock

Apr 4th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. local bios = true
  2. --change this to true if using this as an OS lock.
  3.  
  4. os.pullEvent = os.pullEventRaw
  5. local tX, tY = term.getSize()
  6.  
  7. --check if PIN exists
  8. if not fs.exists(".PIN") then
  9. local PIN = {"0","0","0","0"}
  10. local writeFile = fs.open(".PIN", "w")
  11. writeFile.write(textutils.serialize(PIN))
  12. writeFile.close()
  13. end
  14.  
  15. --gui functions
  16. local scannerFrame = {
  17. '+-+-+-+-+-+-+-+-+-+-+',
  18. '| |',
  19. '+ +',
  20. '| |',
  21. '+ +',
  22. '| |',
  23. '+ +',
  24. '| |',
  25. '+ +',
  26. '| |',
  27. '+ +',
  28. '| |',
  29. '+-+-+-+-+-+-+-+-+-+-+'
  30. }
  31. local scanner = {
  32. ' | | | | | | | | | ',
  33. '-+-+-+-+-+-+-+-+-+-',
  34. ' | | | | | | | | | ',
  35. '-+-+-+-+-+-+-+-+-+-',
  36. ' | | | | | | | | | ',
  37. '-+-+-+-+-+-+-+-+-+-',
  38. ' | | | | | | | | | ',
  39. '-+-+-+-+-+-+-+-+-+-',
  40. ' | | | | | | | | | ',
  41. '-+-+-+-+-+-+-+-+-+-',
  42. ' | | | | | | | | | '
  43. }
  44.  
  45.  
  46. -- lay out the button labels
  47. local labels = {
  48. ' 1 ', ' 2 ', ' 3 ',
  49. ' 4 ', ' 5 ', ' 6 ',
  50. ' 7 ', ' 8 ', ' 9 ',
  51. 'CLR', ' 0 ', 'ENT'
  52. }
  53.  
  54. -- generate the objects
  55. local objects = {}
  56. for i=1, #labels do
  57. table.insert(objects, {
  58. x = (((i - 1)%3 + 1)*5) - 2;
  59. y = (math.ceil(i/3) * 3) + 3;
  60. label = labels[i];
  61. -- make CLR red and ENT green
  62. color = i==10 and colors.red or i==12 and colors.green or colors.lightGray;
  63. })
  64. end
  65.  
  66. local function draw()
  67. term.setBackgroundColor(colors.black)
  68. term.clear()
  69. for i=1, #objects do
  70. local obj = objects[i]
  71. term.setTextColor(colors.gray)
  72.  
  73. for num, line in pairs{'+---+','| |','+---+'} do
  74. term.setCursorPos(obj.x, obj.y + num - 1)
  75. write(line)
  76. end
  77.  
  78. term.setCursorPos(obj.x+1, obj.y+1)
  79. term.setTextColor(obj.color)
  80. write(obj.label)
  81. end
  82. end
  83.  
  84. local function gui()
  85. term.setTextColor(colors.red)
  86. term.setCursorPos(1,1)
  87. write("."..string.rep("-", tX-2)..".")
  88. for i = 2, tY - 1 do
  89. term.setCursorPos(1,i)
  90. write("|")
  91. term.setCursorPos(tX,i)
  92. write("|")
  93. end
  94. term.setCursorPos(1,tY)
  95. write("'"..string.rep("-", tX-2).."'")
  96. term.setTextColor(colors.yellow)
  97. end
  98.  
  99. local function display(scan)
  100. term.setTextColor(colors.black)
  101. term.setBackgroundColor(colors.lightGray)
  102. for i = 3, 5 do
  103. term.setCursorPos(3,i)
  104. write(string.rep(" ", 15))
  105. end
  106. term.setTextColor(colors.gray)
  107. term.setBackgroundColor(colors.black)
  108. for i = 1, #scannerFrame do
  109. term.setCursorPos(24,i + 4)
  110. write(scannerFrame[i])
  111. end
  112. term.setTextColor(scan and colors.gray or colors.lightGray)
  113. term.setBackgroundColor(scan and colors.lime or colors.green)
  114. for i = 1, #scanner do
  115. term.setCursorPos(25,i + 5)
  116. write(scanner[i])
  117. end
  118. end
  119.  
  120. --PIN functions
  121. --retrieve PIN from file
  122. local function retrieve()
  123. local readFile = fs.open(".PIN", "r")
  124. local pinText = readFile.readAll()
  125. readFile.close()
  126. local pinTab = textutils.unserialize(pinText)
  127. return pinTab
  128. end
  129.  
  130. --create new PIN
  131. local function saveNew(pin)
  132. if fs.exists(".PIN") then
  133. fs.delete(".PIN")
  134. end
  135. local newFile = fs.open(".PIN", "w")
  136. newFile.write(textutils.serialize(pin))
  137. newFile.close()
  138. end
  139.  
  140. local function takeNumbers()
  141. local verPin = {}
  142. while true do
  143. local events = {os.pullEvent()}
  144. if events[1] == "mouse_click" and events[2] == 1 then
  145. if events[3] >= 3 and events[3] <= 7 and events[4] >= 6 and events[4] <= 8 then
  146. table.insert(verPin, 1)
  147. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 6 and events[4] <= 8 then
  148. table.insert(verPin, 2)
  149. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 6 and events[4] <= 8 then
  150. table.insert(verPin, 3)
  151. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 9 and events[4] <= 11 then
  152. table.insert(verPin, 4)
  153. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 9 and events[4] <= 11 then
  154. table.insert(verPin, 5)
  155. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 9 and events[4] <= 11 then
  156. table.insert(verPin, 6)
  157. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 12 and events[4] <= 14 then
  158. table.insert(verPin, 7)
  159. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 12 and events[4] <= 14 then
  160. table.insert(verPin, 8)
  161. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 12 and events[4] <= 14 then
  162. table.insert(verPin, 9)
  163. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 15 and events[4] <= 17 then
  164. verPin = {}
  165. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 15 and events[4] <= 17 then
  166. table.insert(verPin, 0)
  167. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 15 and events[4] <= 17 then
  168. if #verPin == 4 then break end
  169. end
  170. if # verPin >= 5 then table.remove(verPin, #verPin) end
  171. display()
  172. for i = 1, #verPin do
  173. term.setTextColor(colors.red)
  174. term.setBackgroundColor(colors.lightGray)
  175. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,3)
  176. write("\\|/")
  177. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,4)
  178. write("-+-")
  179. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,5)
  180. write("/|\\")
  181. end
  182. end
  183. end
  184. return verPin
  185. end
  186.  
  187. --verification
  188. local function verify()
  189. local curPin = retrieve()
  190. if tonumber(curPin[1]) == 0 and tonumber(curPin[2]) == 0 and tonumber(curPin[3]) == 0 and tonumber(curPin[4]) == 0 then
  191. term.setCursorPos(5,4)
  192. term.setTextColor(colors.yellow)
  193. term.setBackgroundColor(colors.lightGray)
  194. write("Create PIN")
  195. local newPin = takeNumbers()
  196. saveNew(newPin)
  197. return "NEW"
  198. else
  199. term.setCursorPos(5,4)
  200. term.setTextColor(colors.yellow)
  201. term.setBackgroundColor(colors.lightGray)
  202. write("Enter PIN")
  203. local checkPin = takeNumbers()
  204. if tonumber(checkPin[1]) == tonumber(curPin[1]) and tonumber(checkPin[2]) == tonumber(curPin[2]) and tonumber(checkPin[3]) == tonumber(curPin[3]) and tonumber(checkPin[4]) == tonumber(curPin[4]) then
  205. return true
  206. else
  207. return false
  208. end
  209. end
  210. end
  211.  
  212. --operation loop
  213. if door and bios then
  214. print("Error occured in configuration.")
  215. print(shell.getRunningProgram().." cannot be used as a door lock as well as an OS protection")
  216. print("Please check config options and try again")
  217. return
  218. end
  219.  
  220. while true do
  221. draw()
  222. gui()
  223. display(false)
  224. local state = verify()
  225. if door and state == true then
  226. display(true)
  227. term.setCursorPos(5,3)
  228. term.setTextColor(colors.yellow)
  229. term.setBackgroundColor(colors.lightGray)
  230. write("Place hand")
  231. term.setCursorPos(5,5)
  232. write("on scanner")
  233. while true do
  234. local events = {os.pullEvent()}
  235. if events[1] == "mouse_click" and events[2] == 1 then
  236. if events[3] >= 25 and events[3] <= 43 then
  237. if events[4] >= 6 and events[4] <= 16 then
  238. for i = 1, #scanner do
  239. term.setTextColor(colors.gray)
  240. term.setCursorPos(25,i + 5)
  241. term.setBackgroundColor(colors.yellow)
  242. write(scanner[i])
  243. sleep(.1)
  244. term.setCursorPos(25, i + 5)
  245. term.setBackgroundColor(colors.lime)
  246. write(scanner[i])
  247. end
  248. for i = #scanner, 1, -1 do
  249. term.setTextColor(colors.gray)
  250. term.setCursorPos(25,i + 5)
  251. term.setBackgroundColor(colors.yellow)
  252. write(scanner[i])
  253. sleep(.1)
  254. term.setCursorPos(25, i + 5)
  255. term.setBackgroundColor(colors.lime)
  256. write(scanner[i])
  257. end
  258. break
  259. end
  260. end
  261. end
  262. end
  263. rs.setOutput(doorSide, true)
  264. sleep(3)
  265. rs.setOutput(doorSide, false)
  266. elseif bios and state == true then
  267. display(true)
  268. term.setCursorPos(5,3)
  269. term.setTextColor(colors.yellow)
  270. term.setBackgroundColor(colors.lightGray)
  271. write("Place hand")
  272. term.setCursorPos(5,5)
  273. write("on scanner")
  274. while true do
  275. local events = {os.pullEvent()}
  276. if events[1] == "mouse_click" and events[2] == 1 then
  277. if events[3] >= 25 and events[3] <= 43 then
  278. if events[4] >= 6 and events[4] <= 16 then
  279. for i = 1, #scanner do
  280. term.setTextColor(colors.gray)
  281. term.setCursorPos(25,i + 5)
  282. term.setBackgroundColor(colors.yellow)
  283. write(scanner[i])
  284. sleep(.1)
  285. term.setCursorPos(25, i + 5)
  286. term.setBackgroundColor(colors.lime)
  287. write(scanner[i])
  288. end
  289. for i = #scanner, 1, -1 do
  290. term.setTextColor(colors.gray)
  291. term.setCursorPos(25,i + 5)
  292. term.setBackgroundColor(colors.yellow)
  293. write(scanner[i])
  294. sleep(.1)
  295. term.setCursorPos(25, i + 5)
  296. term.setBackgroundColor(colors.lime)
  297. write(scanner[i])
  298. end
  299. break
  300. end
  301. end
  302. end
  303. end
  304. term.setTextColor(colors.white)
  305. term.setBackgroundColor(colors.black)
  306. term.clear()
  307. term.setCursorPos(1,1)
  308. break
  309. elseif not state == true then
  310. display(false)
  311. term.setCursorPos(6,4)
  312. term.setTextColor(colors.yellow)
  313. term.setBackgroundColor(colors.lightGray)
  314. write("Incorrect")
  315. sleep(2)
  316. end
  317. end
  318.  
  319. shell.run(".MicroData/Data/boot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement