SHOW:
|
|
- or go back to the newest paste.
| 1 | --Logic Gates by Croswat & VikeStep | |
| 2 | ||
| 3 | local gatevars = nil --variable for gates | |
| 4 | local inputs = nil | |
| 5 | local outputs = nil | |
| 6 | ||
| 7 | function change(event) | |
| 8 | print("Press x to change gates")
| |
| 9 | parallel.waitForAll(waitforkey,event) | |
| 10 | end | |
| 11 | ||
| 12 | function run() | |
| 13 | term.clear() | |
| 14 | term.setCursorPos(1,1) | |
| 15 | print("running "..gatevars[1])
| |
| 16 | if gatevars[1] == "timer" then | |
| 17 | change(timer) | |
| 18 | elseif gatevars[1] == "and" then | |
| 19 | change(andgate) | |
| 20 | elseif gatevars[1] == "or" then | |
| 21 | change(orgate) | |
| 22 | elseif gatevars[1] == "nor" then | |
| 23 | change(norgate) | |
| 24 | elseif gatevars[1] == "nand" then | |
| 25 | change(nandgate) | |
| 26 | elseif gatevars[1] == "repeater" then | |
| 27 | change(repeater) | |
| 28 | elseif gatevars[1] == "xor" then | |
| 29 | change(xorgate) | |
| 30 | elseif gatevars[1] == "xnor" then | |
| 31 | change(xnorgate) | |
| 32 | end | |
| 33 | end | |
| 34 | ||
| 35 | function waitforkey() | |
| 36 | while true do | |
| 37 | local sEvent, param = os.pullEvent("key")
| |
| 38 | if sEvent == "key" then | |
| 39 | - | if param == 45 then |
| 39 | + | if param == keys.x then |
| 40 | print("Are you sure? (y/n)")
| |
| 41 | local sEvent, param2 = os.pullEvent("key")
| |
| 42 | if sEvent == "key" then | |
| 43 | - | if param2 == 21 then |
| 43 | + | if param2 == keys.y then |
| 44 | fs.delete("lgvariables")
| |
| 45 | os.reboot() | |
| 46 | else | |
| 47 | os.reboot() | |
| 48 | end | |
| 49 | end | |
| 50 | end | |
| 51 | end | |
| 52 | end | |
| 53 | end | |
| 54 | ||
| 55 | function loadVariables() -- get fom file | |
| 56 | local file = fs.open("lgvariables","r")
| |
| 57 | gatevars = split(file.readLine(), "@") | |
| 58 | file.close() | |
| 59 | end | |
| 60 | ||
| 61 | function saveVariables(variables) | |
| 62 | local file = fs.open("lgvariables","w")
| |
| 63 | file.writeLine(variables) | |
| 64 | file.close() | |
| 65 | end | |
| 66 | ||
| 67 | ||
| 68 | function split(pString, pPattern) | |
| 69 | numchanged = 0 | |
| 70 | local Table = {} -- NOTE: use {n = 0} in Lua-5.0
| |
| 71 | local fpat = "(.-)" .. pPattern | |
| 72 | local last_end = 1 | |
| 73 | local s, e, cap = pString:find(fpat, 1) | |
| 74 | while s do | |
| 75 | if s ~= 1 or cap ~= "" then | |
| 76 | table.insert(Table,cap) | |
| 77 | numchanged = numchanged +1 | |
| 78 | end | |
| 79 | last_end = e+1 | |
| 80 | s, e, cap = pString:find(fpat, last_end) | |
| 81 | end | |
| 82 | if last_end <= #pString then | |
| 83 | cap = pString:sub(last_end) | |
| 84 | table.insert(Table, cap) | |
| 85 | numchanged = numchanged +1 | |
| 86 | end | |
| 87 | return Table | |
| 88 | end | |
| 89 | ||
| 90 | function readgate() | |
| 91 | local loopflag = true | |
| 92 | local gatetype = nil | |
| 93 | while loopflag do | |
| 94 | gatetype = read() | |
| 95 | gatetype = string.lower(gatetype) | |
| 96 | if gatetype == "list" then | |
| 97 | print("AND, OR, NOT, NOR, NAND, XOR, XNOR, Timer, Repeater")
| |
| 98 | elseif gatetype == "timer" or gatetype == "and" or gatetype == "or" or gatetype == "not" or gatetype == "nor" or gatetype == "inverter" or gatetype == "nand" or gatetype == "xor" or gatetype == "xnor" or gatetype == "buffer" or gatetype == "repeater" then -- check for gate types here | |
| 99 | loopflag = false | |
| 100 | else | |
| 101 | print("gate not recognised, try again")
| |
| 102 | end | |
| 103 | end | |
| 104 | return gatetype | |
| 105 | end | |
| 106 | ||
| 107 | function printCube() -- The info for whether to type in inputs or not will be displayed after this | |
| 108 | term.clear() | |
| 109 | term.setCursorPos(1,1) | |
| 110 | print [[ | |
| 111 | Back Please type in the | |
| 112 | | the side(s) in | |
| 113 | +---+---+ the following format | |
| 114 | /| V /| | |
| 115 | / | / | "front, left, right" | |
| 116 | / |Top / | | |
| 117 | +-------+ |<-Right | |
| 118 | | | | | | |
| 119 | | +---|---+ | |
| 120 | Left->| Front | / | |
| 121 | | / | / | |
| 122 | |/ ^ |/ | |
| 123 | +-------+ | |
| 124 | | | |
| 125 | Bottom | |
| 126 | ]] | |
| 127 | end | |
| 128 | ||
| 129 | --menu and setting up gates | |
| 130 | function setup() | |
| 131 | term.clear() | |
| 132 | term.setCursorPos(1,1) | |
| 133 | print("To get started, write down what gate you wish to use")
| |
| 134 | print("type \"list\" to see what gates are available")
| |
| 135 | local gatetype = readgate() | |
| 136 | --work out what gate, then get the other inputs, and save to file. then run() | |
| 137 | if gatetype == "timer" then | |
| 138 | setuptimer() | |
| 139 | elseif gatetype == "and" then | |
| 140 | setupand() | |
| 141 | elseif gatetype == "or" then | |
| 142 | setupor() | |
| 143 | elseif gatetype == "nor" or gatetype == "not" or gatetype== "inverter" then | |
| 144 | setupnor() | |
| 145 | elseif gatetype == "nand" then | |
| 146 | setupnand() | |
| 147 | elseif gatetype == "xor" then | |
| 148 | setupxor() | |
| 149 | elseif gatetype == "xnor" then | |
| 150 | setupxnor() | |
| 151 | elseif gatetype == "buffer" or gatetype == "repeater" then | |
| 152 | setuprepeater() | |
| 153 | end | |
| 154 | ||
| 155 | end | |
| 156 | ||
| 157 | function ioToArray() | |
| 158 | inputs = split(gatevars[2],", ") | |
| 159 | outputs = split(gatevars[3],", ") | |
| 160 | end | |
| 161 | ||
| 162 | function getNumberInputs() | |
| 163 | local number = table.getn(inputs) | |
| 164 | return number | |
| 165 | end | |
| 166 | ||
| 167 | function getNumberOutputs() | |
| 168 | local number = table.getn(outputs) | |
| 169 | return number | |
| 170 | end | |
| 171 | ||
| 172 | function round(x) | |
| 173 | floor = tonumber(x)-math.floor(tonumber(x)) | |
| 174 | ceil = math.ceil(tonumber(x))-tonumber(x) | |
| 175 | if floor==ceil then | |
| 176 | return math.ceil(tonumber(x)) | |
| 177 | elseif floor<ceil then | |
| 178 | return math.floor(tonumber(x)) | |
| 179 | elseif floor>ceil then | |
| 180 | return math.ceil(tonumber(x)) | |
| 181 | end | |
| 182 | end | |
| 183 | ||
| 184 | function getside(printstring) | |
| 185 | valid=false | |
| 186 | tempsidesvalid=0 | |
| 187 | printCube() | |
| 188 | print(printstring) | |
| 189 | while not valid do | |
| 190 | text = read() | |
| 191 | sides = split(text,", ") | |
| 192 | amountsides = table.getn(sides) | |
| 193 | for i=1, amountsides do | |
| 194 | if sides[i] == "front" or sides[i] == "left" or sides[i] == "right" or sides[i] == "back" or sides[i] == "bottom" or sides[i] == "top" then | |
| 195 | tempsidesvalid = tempsidesvalid + 1 | |
| 196 | else | |
| 197 | printCube() | |
| 198 | print(sides[1].." is not a valid side") | |
| 199 | print(printstring) | |
| 200 | break | |
| 201 | end | |
| 202 | end | |
| 203 | if tempsidesvalid==amountsides then | |
| 204 | valid=true | |
| 205 | else | |
| 206 | tempsidesvalid=0 | |
| 207 | end | |
| 208 | end | |
| 209 | return text | |
| 210 | end | |
| 211 | ||
| 212 | --Timer | |
| 213 | ||
| 214 | function setuptimer() | |
| 215 | local timerinput = getside("Type in the inputs (disables the timer)")
| |
| 216 | local timeroutput = getside("Type in the outputs (side pulsed to)")
| |
| 217 | term.clear() | |
| 218 | term.setCursorPos(1,1) | |
| 219 | print("enter how many seconds you would like the timer to cycle")
| |
| 220 | local loopflag = true | |
| 221 | while loopflag do | |
| 222 | timerseconds = read() | |
| 223 | integer=tonumber(timerseconds) | |
| 224 | if integer == nil or integer <= 0 then | |
| 225 | term.clear() | |
| 226 | term.setCursorPos(1,1) | |
| 227 | print(timerseconds.." is not a valid positive number") | |
| 228 | print("enter how many seconds you would like the timer to cycle (can enter decimal)")
| |
| 229 | else | |
| 230 | loopflag = false | |
| 231 | end | |
| 232 | end | |
| 233 | saveVariables("timer".."@"..tostring(timerinput).."@"..tostring(timeroutput).."@"..tostring(timerseconds))
| |
| 234 | loadVariables() | |
| 235 | term.clear() | |
| 236 | term.setCursorPos(1,1) | |
| 237 | change(timer) | |
| 238 | end | |
| 239 | ||
| 240 | function timer() | |
| 241 | input = false | |
| 242 | pulseLength = 1 -- In ticks | |
| 243 | ioToArray() | |
| 244 | numberInputs=tonumber(getNumberInputs()) | |
| 245 | numberOutputs=tonumber(getNumberOutputs()) | |
| 246 | clockSpeed = round(20*tonumber(gatevars[4])) -- In ticks, remember 20 ticks a second | |
| 247 | while true do | |
| 248 | for i=1, numberInputs do | |
| 249 | if rs.getInput(inputs[i])==true then | |
| 250 | input=true | |
| 251 | else | |
| 252 | end | |
| 253 | end | |
| 254 | while not input do | |
| 255 | for i=1, numberOutputs do | |
| 256 | rs.setOutput(outputs[i], true) | |
| 257 | end | |
| 258 | sleep(pulseLength / 20) | |
| 259 | for i=1, numberOutputs do | |
| 260 | rs.setOutput(outputs[i], false) | |
| 261 | end | |
| 262 | sleep((clockSpeed - pulseLength) / 20) | |
| 263 | for i=1, numberInputs do | |
| 264 | if rs.getInput(inputs[i])==true then | |
| 265 | input=true | |
| 266 | else | |
| 267 | end | |
| 268 | end | |
| 269 | end | |
| 270 | event = os.pullEvent("redstone")
| |
| 271 | input=false | |
| 272 | end | |
| 273 | end | |
| 274 | ||
| 275 | --AND Gate | |
| 276 | ||
| 277 | function setupand() | |
| 278 | local andinput = getside("Type in the inputs (when all are inputs are on, output is on)")
| |
| 279 | local andoutput = getside("Type in the outputs (when all inputs are on, output is on)")
| |
| 280 | saveVariables("and".."@"..tostring(andinput).."@"..tostring(andoutput))
| |
| 281 | loadVariables() | |
| 282 | term.clear() | |
| 283 | term.setCursorPos(1,1) | |
| 284 | change(andgate) | |
| 285 | end | |
| 286 | ||
| 287 | function andgate() | |
| 288 | input = false | |
| 289 | ioToArray() | |
| 290 | numberInputs=tonumber(getNumberInputs()) | |
| 291 | numberOutputs=tonumber(getNumberOutputs()) | |
| 292 | amount = 0 | |
| 293 | ||
| 294 | while true do | |
| 295 | amount=0 | |
| 296 | for i=1, numberInputs do | |
| 297 | if rs.getInput(inputs[i])==true then | |
| 298 | amount = amount + 1 | |
| 299 | else | |
| 300 | end | |
| 301 | end | |
| 302 | if amount == numberInputs then | |
| 303 | input=true | |
| 304 | else | |
| 305 | amount = 0 | |
| 306 | input=false | |
| 307 | end | |
| 308 | if input == true then | |
| 309 | for i=1,numberOutputs do | |
| 310 | rs.setOutput(outputs[i],true) | |
| 311 | end | |
| 312 | else | |
| 313 | for i=1,numberOutputs do | |
| 314 | rs.setOutput(outputs[i],false) | |
| 315 | end | |
| 316 | end | |
| 317 | event = os.pullEvent("redstone")
| |
| 318 | end | |
| 319 | end | |
| 320 | ||
| 321 | -- OR Gate | |
| 322 | ||
| 323 | function setupor() | |
| 324 | local orinput = getside("Type in the inputs (when one of the inputs are on, output is on)")
| |
| 325 | local oroutput = getside("Type in the outputs (when one of the inputs are on, output is on)")
| |
| 326 | saveVariables("or".."@"..tostring(orinput).."@"..tostring(oroutput))
| |
| 327 | loadVariables() | |
| 328 | term.clear() | |
| 329 | term.setCursorPos(1,1) | |
| 330 | change(orgate) | |
| 331 | end | |
| 332 | ||
| 333 | function orgate() | |
| 334 | input = false | |
| 335 | ioToArray() | |
| 336 | numberInputs=tonumber(getNumberInputs()) | |
| 337 | numberOutputs=tonumber(getNumberOutputs()) | |
| 338 | ||
| 339 | while true do | |
| 340 | input=false | |
| 341 | for i=1, numberInputs do | |
| 342 | if rs.getInput(inputs[i])==true then | |
| 343 | input=true | |
| 344 | else | |
| 345 | end | |
| 346 | end | |
| 347 | if input == true then | |
| 348 | for i=1,numberOutputs do | |
| 349 | rs.setOutput(outputs[i],true) | |
| 350 | end | |
| 351 | else | |
| 352 | for i=1,numberOutputs do | |
| 353 | rs.setOutput(outputs[i],false) | |
| 354 | end | |
| 355 | end | |
| 356 | event = os.pullEvent("redstone")
| |
| 357 | end | |
| 358 | end | |
| 359 | ||
| 360 | -- Repeater | |
| 361 | ||
| 362 | function setuprepeater() | |
| 363 | local delay | |
| 364 | local repeaterinput = getside("Type in the inputs (when one of the inputs are on, output is on)")
| |
| 365 | local repeateroutput = getside("Type in the outputs (when one of the inputs are on, output is on)")
| |
| 366 | term.clear() | |
| 367 | term.setCursorPos(1,1) | |
| 368 | print("Enter the delay (seconds) for the signal, type 0 for no delay (can be decimal)")
| |
| 369 | local loopflag = true | |
| 370 | while loopflag do | |
| 371 | delay = read() | |
| 372 | integer=tonumber(delay) | |
| 373 | if integer == nil or integer < 0 then | |
| 374 | term.clear() | |
| 375 | term.setCursorPos(1,1) | |
| 376 | print(delay.." is not a valid positive number") | |
| 377 | print("Enter the delay for the signal, type 0 for no delay")
| |
| 378 | else | |
| 379 | loopflag = false | |
| 380 | end | |
| 381 | end | |
| 382 | saveVariables("repeater".."@"..tostring(repeaterinput).."@"..tostring(repeateroutput).."@"..tostring(delay))
| |
| 383 | loadVariables() | |
| 384 | term.clear() | |
| 385 | term.setCursorPos(1,1) | |
| 386 | change(orgate) | |
| 387 | end | |
| 388 | ||
| 389 | function repeater() | |
| 390 | local delay = round(20*tonumber(gatevars[4])) | |
| 391 | input = false | |
| 392 | ioToArray() | |
| 393 | numberInputs=tonumber(getNumberInputs()) | |
| 394 | numberOutputs=tonumber(getNumberOutputs()) | |
| 395 | ||
| 396 | while true do | |
| 397 | input=false | |
| 398 | for i=1, numberInputs do | |
| 399 | if rs.getInput(inputs[i])==true then | |
| 400 | input=true | |
| 401 | else | |
| 402 | end | |
| 403 | end | |
| 404 | if input == true then | |
| 405 | sleep(delay) | |
| 406 | for i=1,numberOutputs do | |
| 407 | rs.setOutput(outputs[i],true) | |
| 408 | end | |
| 409 | else | |
| 410 | for i=1,numberOutputs do | |
| 411 | rs.setOutput(outputs[i],false) | |
| 412 | end | |
| 413 | end | |
| 414 | event = os.pullEvent("redstone")
| |
| 415 | end | |
| 416 | end | |
| 417 | ||
| 418 | -- NOT/NOR Gate | |
| 419 | ||
| 420 | function setupnor() | |
| 421 | local norinput = getside("Type in the inputs (if 1 input is on, output is off)")
| |
| 422 | local noroutput = getside("Type in the outputs (if 1 input is on, output is off)")
| |
| 423 | saveVariables("nor".."@"..tostring(norinput).."@"..tostring(noroutput))
| |
| 424 | loadVariables() | |
| 425 | term.clear() | |
| 426 | term.setCursorPos(1,1) | |
| 427 | change(norgate) | |
| 428 | end | |
| 429 | ||
| 430 | function norgate() | |
| 431 | input = false | |
| 432 | ioToArray() | |
| 433 | numberInputs=tonumber(getNumberInputs()) | |
| 434 | numberOutputs=tonumber(getNumberOutputs()) | |
| 435 | ||
| 436 | while true do | |
| 437 | input=false | |
| 438 | for i=1, numberInputs do | |
| 439 | if rs.getInput(inputs[i])==true then | |
| 440 | input=true | |
| 441 | else | |
| 442 | end | |
| 443 | end | |
| 444 | if input == false then | |
| 445 | for i=1,numberOutputs do | |
| 446 | rs.setOutput(outputs[i],true) | |
| 447 | end | |
| 448 | else | |
| 449 | for i=1,numberOutputs do | |
| 450 | rs.setOutput(outputs[i],false) | |
| 451 | end | |
| 452 | end | |
| 453 | event = os.pullEvent("redstone")
| |
| 454 | end | |
| 455 | end | |
| 456 | ||
| 457 | --NAND Gate | |
| 458 | ||
| 459 | function setupnand() | |
| 460 | local nandinput = getside("Type in the inputs (when at least 1 input is off, output is on)")
| |
| 461 | local nandoutput = getside("Type in the outputs (when at least 1 input is off, output is on)")
| |
| 462 | saveVariables("nand".."@"..tostring(nandinput).."@"..tostring(nandoutput))
| |
| 463 | loadVariables() | |
| 464 | term.clear() | |
| 465 | term.setCursorPos(1,1) | |
| 466 | change(nandgate) | |
| 467 | end | |
| 468 | ||
| 469 | function nandgate() | |
| 470 | input = false | |
| 471 | ioToArray() | |
| 472 | numberInputs=tonumber(getNumberInputs()) | |
| 473 | numberOutputs=tonumber(getNumberOutputs()) | |
| 474 | amount = 0 | |
| 475 | ||
| 476 | while true do | |
| 477 | amount=0 | |
| 478 | for i=1, numberInputs do | |
| 479 | if rs.getInput(inputs[i])==true then | |
| 480 | amount = amount + 1 | |
| 481 | else | |
| 482 | end | |
| 483 | end | |
| 484 | if amount == numberInputs then | |
| 485 | input=true | |
| 486 | else | |
| 487 | amount = 0 | |
| 488 | input=false | |
| 489 | end | |
| 490 | if input == false then | |
| 491 | for i=1,numberOutputs do | |
| 492 | rs.setOutput(outputs[i],true) | |
| 493 | end | |
| 494 | else | |
| 495 | for i=1,numberOutputs do | |
| 496 | rs.setOutput(outputs[i],false) | |
| 497 | end | |
| 498 | end | |
| 499 | event = os.pullEvent("redstone")
| |
| 500 | end | |
| 501 | end | |
| 502 | ||
| 503 | -- XOR Gate | |
| 504 | ||
| 505 | function setupxor() | |
| 506 | local xorinput = getside("Type in the inputs (when an odd number of inputs are on, output is on)")
| |
| 507 | local xoroutput = getside("Type in the outputs (when an odd number of inputs are on, output is on)")
| |
| 508 | saveVariables("xor".."@"..tostring(xorinput).."@"..tostring(xoroutput))
| |
| 509 | loadVariables() | |
| 510 | term.clear() | |
| 511 | term.setCursorPos(1,1) | |
| 512 | change(xorgate) | |
| 513 | end | |
| 514 | ||
| 515 | function xorgate() | |
| 516 | input = false | |
| 517 | ioToArray() | |
| 518 | numberInputs=tonumber(getNumberInputs()) | |
| 519 | numberOutputs=tonumber(getNumberOutputs()) | |
| 520 | amount = 0 | |
| 521 | ||
| 522 | while true do | |
| 523 | amount=0 | |
| 524 | for i=1, numberInputs do | |
| 525 | if rs.getInput(inputs[i])==true then | |
| 526 | amount = amount + 1 | |
| 527 | else | |
| 528 | end | |
| 529 | end | |
| 530 | if amount % 2 == 1 then | |
| 531 | input=true | |
| 532 | else | |
| 533 | amount = 0 | |
| 534 | input=false | |
| 535 | end | |
| 536 | if input == true then | |
| 537 | for i=1,numberOutputs do | |
| 538 | rs.setOutput(outputs[i],true) | |
| 539 | end | |
| 540 | else | |
| 541 | for i=1,numberOutputs do | |
| 542 | rs.setOutput(outputs[i],false) | |
| 543 | end | |
| 544 | end | |
| 545 | event = os.pullEvent("redstone")
| |
| 546 | end | |
| 547 | end | |
| 548 | ||
| 549 | -- XNOR Gate | |
| 550 | ||
| 551 | function setupxnor() | |
| 552 | local xnorinput = getside("Type in the inputs (when an even number of inputs are on, output is on)")
| |
| 553 | local xnoroutput = getside("Type in the outputs (when an even number of inputs are on, output is on)")
| |
| 554 | saveVariables("xnor".."@"..tostring(xnorinput).."@"..tostring(xnoroutput))
| |
| 555 | loadVariables() | |
| 556 | term.clear() | |
| 557 | term.setCursorPos(1,1) | |
| 558 | change(xnorgate) | |
| 559 | end | |
| 560 | ||
| 561 | function xnorgate() | |
| 562 | input = false | |
| 563 | ioToArray() | |
| 564 | numberInputs=tonumber(getNumberInputs()) | |
| 565 | numberOutputs=tonumber(getNumberOutputs()) | |
| 566 | amount = 0 | |
| 567 | ||
| 568 | while true do | |
| 569 | amount=0 | |
| 570 | for i=1, numberInputs do | |
| 571 | if rs.getInput(inputs[i])==true then | |
| 572 | amount = amount + 1 | |
| 573 | end | |
| 574 | end | |
| 575 | if amount % 2 == 1 then | |
| 576 | input=true | |
| 577 | else | |
| 578 | amount = 0 | |
| 579 | input=false | |
| 580 | end | |
| 581 | if input == false then | |
| 582 | for i=1,numberOutputs do | |
| 583 | rs.setOutput(outputs[i],true) | |
| 584 | end | |
| 585 | else | |
| 586 | for i=1,numberOutputs do | |
| 587 | rs.setOutput(outputs[i],false) | |
| 588 | end | |
| 589 | end | |
| 590 | event = os.pullEvent("redstone")
| |
| 591 | end | |
| 592 | end | |
| 593 | ||
| 594 | -- functions go above here | |
| 595 | term.clear() | |
| 596 | term.setCursorPos(1,1) | |
| 597 | --start | |
| 598 | if fs.exists("lgvariables") then -- variables saved
| |
| 599 | loadVariables() | |
| 600 | run() | |
| 601 | else | |
| 602 | setup() | |
| 603 | end |