Guest User

Redstone Gates API by Islanderon

a guest
Apr 11th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. -- Redstone Gates API by Islanderon
  2. -- Note: Using any wireless redstone functions requires a rednet modem and will open any rednet modem on your computer automaticlly.
  3. -- v1.0.0
  4.  
  5. local function openRednet()
  6. local listOfSides = rs.getSides()
  7. local listofPossibles = {}
  8. local counter1 = 0
  9. while true do
  10.   counter1 = counter1 +1
  11.  
  12.   if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
  13.    table.insert(listofPossibles,tostring(listOfSides[counter1]))
  14.   end
  15.  
  16.   if counter1 == 6 and table.maxn(listofPossibles) == 0 then
  17.    print("no wifi present")
  18.    return nil
  19.   end
  20.  
  21.   if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
  22.    rednet.open(listofPossibles[1])
  23.    return listofPossibles[1]
  24.   end
  25. end
  26. end
  27.  
  28. local function find(str, match, startIndex)  --Find function by tommas (StrUtils API)
  29. if not match then return nil end
  30. str = tostring(str)
  31. local _ = startIndex or 1
  32. local _s = nil
  33. local _e = nil
  34. local _len = match:len()
  35. while true do
  36. local _t = str:sub( _ , _len + _ - 1)
  37. if _t == match then
  38. _s = _
  39. _e = _ + _len - 1
  40. break
  41. end
  42. _ = _ + 1
  43. if _ > str:len() then break end
  44. end
  45. if _s == nil then return nil else return _s, _e end
  46. end
  47.  
  48. local function seperate(str, divider) -- Seperate function by tommas (StrUtils API)
  49. if not divider then return nil end
  50. str = tostring(str)
  51. local start = {}
  52. local endS = {}
  53. local n=1
  54. repeat
  55. if n==1 then
  56. start[n], endS[n] = find(str, divider)
  57. else
  58. start[n], endS[n] = find(str, divider, endS[n-1]+1)
  59.                 end
  60. n=n+1
  61. until start[n-1]==nil
  62. local subs = {}
  63. for n=1, #start+1 do
  64. if n==1 then
  65. subs[n] = str:sub(1, start[n]-1)
  66. elseif n==#start+1 then
  67. subs[n] = str:sub(endS[n-1]+1)
  68. else
  69. subs[n] = str:sub(endS[n-1]+1, start[n]-1)
  70.                 end
  71. end
  72. return subs
  73. end
  74.  
  75. local function toboolean(input)
  76. if input == "true" then
  77. return true
  78. else
  79. return false
  80. end
  81. end
  82.  
  83. local function fromboolean(input)
  84. if input == true then
  85. return "true"
  86. else
  87. return "false"
  88. end
  89. end
  90.  
  91. function recState(freq, set, timeout)
  92. openRednet()
  93. if timeout == nil then
  94. id, msg = rednet.receive()
  95. else
  96. id, msg = rednet.receive(tonumber(timeout))
  97. end
  98. parseddata = seperate(msg, " : ")
  99. if parseddata[1] == "RSW" and parseddata[2] == freq then
  100. finald = { }
  101. finald[1] = parseddata[3]
  102. finald[2] = toboolean(parseddata[4])
  103. if set == true then
  104. redstone.setOutput(finald[1], finald[2])
  105. return finald
  106. else
  107. return finald
  108. end
  109. end
  110. end
  111.  
  112. function pulser(side, interval, amount)
  113.  if side == "left" or side == "right" or side == "front" or side == "back" or side == "top" or side == "bottom" then
  114.   if amount == nil then
  115.    while true do
  116.     redstone.setOutput(side, true)
  117.     sleep(interval)
  118.     redstone.setOutput(side, false)
  119.     sleep(interval)
  120.    end
  121.   else
  122.    while amount > 0 do
  123.     redstone.setOutput(side, true)
  124.     sleep(interval)
  125.     redstone.setOutput(side, false)
  126.     amount = amount - 1
  127.     sleep(interval)
  128.    end
  129.   end
  130.  else
  131.   return "Invalid side"
  132.  end
  133. end
  134.  
  135. --[[
  136. Note: function currently bugged. To be fixed in a later version.
  137.  
  138. function notGate(side1, side2)
  139. while true do
  140. os.pullEvent("redstone")
  141. if redstone.getInput(side1) == true then
  142. redstone.setOutput(side2, false)
  143. else
  144. redstone.setOutput(side2, true)
  145. end
  146. end
  147. end
  148. ]]--
  149.  
  150. function sendState(freq, side, state)
  151. openRednet()
  152. state = fromboolean(state)
  153. rednet.broadcast("RSW : "..freq.." : "..side.." : "..state)
  154. end
  155.  
  156. function orGate(side1, side2, output)
  157.  while true do
  158.   os.pullEvent("redstone")
  159.   if redstone.getInput(side1) == true or redstone.getInput(side2) == true then
  160.    redstone.setOutput(output, true)
  161.   else
  162.    redstone.setOutput(output, false)
  163.   end
  164.  end
  165. end
  166.  
  167. function spinner(interval, sleeptime)
  168.  local currentside = "front"
  169.  while true do
  170.   redstone.setOutput(currentside, true)
  171.   sleep(sleeptime)
  172.   redstone.setOutput(currentside, false)
  173.   if currentside == "front" then
  174.    currentside = "right"
  175.   elseif currentside == "left" then
  176.    currentside = "back"
  177.   elseif currentside == "back" then
  178.    currentside = "left"
  179.   elseif currentside == "left" then
  180.    currentside = "front"
  181.   end
  182.   sleep(interval)
  183.  end
  184. end
Advertisement
Add Comment
Please, Sign In to add comment