Advertisement
Sv443

ComputerCraft Redstone State Toggler

Apr 3rd, 2025
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | Gaming | 0 0
  1. -- If this side receives a pulse, the computer switches to the next state
  2. TOGGLE_SIDE = "right"
  3.  
  4. -- Cooldown between accepting pulses
  5. -- Set to 0 to disable
  6. -- Has to be a multiple of 0.1 (1 redstone tick)
  7. TOGGLE_COOLDOWN = 1
  8.  
  9. -- All redstone states that can be toggled between
  10. -- First layer is what gets switched between when TOGGLE_SIDE receives a pulse
  11. -- Second layer is a list of {"side", value, beforeDelay, afterDelay} pairs (delays are optional, multiple of 0.1)
  12. -- The example below is perfect for using two regular pistons pointing at each other to swap two blocks:
  13. STATES = {
  14.     {
  15.         {"right", 0, nil, 0.1},
  16.         {"left", 15}
  17.     },
  18.     {
  19.         {"left", 0, nil, 0.1},
  20.         {"right", 15}
  21.     },
  22. }
  23.  
  24. -- Whether the computer goes down the list of states (1) or up (-1)
  25. -- Other values are also valid, though when wrapping around, the state will always be set to the first or last
  26. STATE_INCREMENT = 1
  27.  
  28. -- Whether to save the state between reboots, MP server restarts, SP world rejoins, etc.
  29. PERSIST_STATE = true
  30.  
  31. -- Whether to add a delay between receiving the pulse and toggling the state
  32. -- Set to 0 to disable
  33. STATE_TOGGLE_DELAY = 0
  34.  
  35. -- Which number state in the STATES list the computer starts with
  36. INITIAL_STATE_INDEX = 1
  37.  
  38. -- The initial signal to set on all sides when the computer starts (0-15)
  39. INITIAL_SIGNAL = 0
  40.  
  41.  
  42.  
  43.  
  44. -- Do not edit below or cobuder gets angry >:(
  45.  
  46.  
  47.  
  48.  
  49. -- #region loop
  50.  
  51. -- current state index, used when calling applyState()
  52. local stateIdx = INITIAL_STATE_INDEX
  53. local lastToggleTime = 0
  54.  
  55. function loop()
  56.     local toggleState = redstone.getInput(TOGGLE_SIDE)
  57.     local currentTime = os.clock()
  58.  
  59.     if toggleState and currentTime - lastToggleTime > TOGGLE_COOLDOWN then
  60.         lastToggleTime = currentTime
  61.  
  62.         if STATE_TOGGLE_DELAY > 0 then
  63.             sleep(STATE_TOGGLE_DELAY)
  64.         end
  65.  
  66.         stateIdx = stateIdx + STATE_INCREMENT
  67.         if stateIdx > #STATES then
  68.             stateIdx = 1
  69.         elseif stateIdx < 1 then
  70.             stateIdx = #STATES
  71.         end
  72.  
  73.         applyState()
  74.         print("* Applied state " .. stateIdx)
  75.     end
  76. end
  77.  
  78. function applyState()
  79.     for _, sideVal in ipairs(STATES[stateIdx]) do
  80.         if sideVal[3] ~= nil and sideVal[3] > 0 then
  81.             sleep(sideVal[3])
  82.         end
  83.         redstone.setAnalogOutput(sideVal[1], sideVal[2])
  84.         if sideVal[4] ~= nil and sideVal[4] > 0 then
  85.             sleep(sideVal[4])
  86.         end
  87.     end
  88.     if PERSIST_STATE then
  89.         settings.set("stateIdx", stateIdx)
  90.         settings.save()
  91.     end
  92. end
  93.  
  94. function b64dec(data)
  95.     local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  96.     data = string.gsub(data, "[^"..b.."=]", "")
  97.  
  98.     return (data:gsub(".", function(x)
  99.         if x == "=" then return "" end
  100.         local r, f = "", (b:find(x) - 1)
  101.         for i = 6, 1, -1 do
  102.             r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0")
  103.         end
  104.         return r
  105.     end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x)
  106.         if #x ~= 8 then return "" end
  107.         local c = 0
  108.         for i = 1, 8 do
  109.             c = c + (x:sub(i, i) == "1" and 2 ^ (8 - i) or 0)
  110.         end
  111.         return string.char(c)
  112.     end))
  113. end
  114.  
  115. --#region run
  116.  
  117. function run()
  118.     -- cause pastebin hates links
  119.     print(b64dec("CnwgU3RhdGVUb2dnbGUgYnkgU3Y0NDMKfCBzdjQ0My5uZXQvci9jYy1wcm9qZWN0cwo="))
  120.  
  121.     for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  122.         redstone.setAnalogOutput(side, INITIAL_SIGNAL or 0)
  123.     end
  124.  
  125.     stateIdx = settings.get("stateIdx") or INITIAL_STATE_INDEX
  126.     applyState()
  127.     print("* Applied initial state " .. stateIdx)
  128.  
  129.     while true do
  130.         loop()
  131.         sleep(0.1)
  132.     end
  133. end
  134.  
  135. run()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement