seiterarch

CC sUtil API

Jan 10th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. --Sterilization functions. Most assume defaults are correctly formatted to save ovehead.
  2. --If you want <function> to sterilize its default, add default=<function>(default)
  3. function nForm(n, default) --integers
  4.     if type(default) ~= 'number' then default = 0 end
  5.    
  6.     n = tonumber(n) or default
  7.     n = math.floor(n)
  8.    
  9.     return n
  10. end
  11. function slotForm(s, keepNil, default) --turtle slots, keepNil for no change in slot
  12.     default = nForm(default, 1)
  13.    
  14.     if s ~= nil or not keepNil then
  15.         s = tonumber(s) or default
  16.         s = s % 16
  17.         if s == 0 then s = 16 end
  18.     end
  19.    
  20.     return s
  21. end
  22. function faceForm(face, default) --orthogonal directions
  23.     local facelist = {'front', 'right', 'back', 'left', 'up', 'down'}
  24.    
  25.     if type(default) == 'number' then
  26.         default = facelist[default + 1]
  27.     end
  28.     if type(default) ~= 'string' then
  29.         default = {'front'}
  30.     end
  31.    
  32.     if type(face) == 'number' then 
  33.         face = math.floor(face)
  34.         if face < 0 or face > 5 then
  35.             face = default
  36.         else
  37.             face = facelist[face + 1]
  38.         end
  39.        
  40.     elseif type(face) == 'string' then 
  41.         local b = false
  42.         for i, v in ipairs(facelist) do
  43.             if face == v then
  44.                 b = true
  45.             end
  46.         end
  47.         if b == false then
  48.             print('Invalid face arguament, using default.')
  49.             face = default
  50.         end
  51.        
  52.     else
  53.         face = default
  54.     end
  55.  
  56.     return face
  57. end
  58. function vForm(v, default) --integer vectors
  59.     if type(default) ~= 'table' then
  60.         default = {0, 0, 0}
  61.     end
  62.    
  63.     if type(v) ~= 'table' then v = {} end
  64.     for i = 1, 3 do
  65.         v[i] = nForm(v[i], default[i])
  66.     end
  67.     return v
  68. end
  69. function posForm(pos, default) --integer vectors w/ direction
  70.     if type(default) ~= 'table' then
  71.         default = {0, 0, 0, 3}
  72.     end
  73.    
  74.     if type(pos) ~= 'table' then pos = {} end
  75.     for i=1,4 do
  76.         pos[i] = nForm(pos[i], default[i])
  77.     end
  78.     pos[4] = pos[4] % 4
  79.    
  80.     return pos
  81. end
  82.  
  83. --Abstract functions
  84. function corners(v1, v2) --Stores volume info
  85.     v1 = vForm(v1)
  86.     v2 = vForm(v2)
  87.     local c = {}
  88.     for i=1,3 do
  89.         c[i] = {}
  90.         c[i][1] = math.min(v1[i], v2[i])
  91.         c[i][2] = math.max(v1[i], v2[i])
  92.     end
  93.     return c
  94. end
  95. function faceInv(face) --Reflects face
  96.     face = faceForm(face)
  97.     if face == 'right' then face = 'left'
  98.     elseif face == 'left' then face = 'right'
  99.     end
  100.     return face
  101. end
  102.  
  103. function transpose(n, face)
  104.     n = tonumber(n)
  105.     if face ~= 'top' and face ~= 'bottom' then
  106.         face = sUtil.faceForm(face)
  107.     end
  108.    
  109.     if redstone.getOutput(face) then
  110.         error('Redstone signal in facing is currently on')
  111.     end
  112.    
  113.     for i=1,n do
  114.         redstone.setOutput(face, true)
  115.         sleep(0.1)
  116.         redstone.setOutput(face, false)
  117.         sleep(0.2)
  118.     end
  119.    
  120.     return true
  121. end
  122.  
  123. function redPulse(faces, pLength, sterile)
  124.     faces = faces or {'back'}
  125.     pLength = pLength or 0.1
  126.     pLength = tonumber(pLength)
  127.     for i, v in ipairs(faces) do
  128.         if sterile ~= true then v = strl.faceForm(v) end
  129.         redstone.setOutput(v, true)
  130.     end
  131.     sleep(pLength)
  132.     for i, v in ipairs(faces) do
  133.         redstone.setOutput(v, false)
  134.     end
  135. end
  136.  
  137. --Same functionality as a redpower2 timer by default,
  138. --with options to change clock and pulse length, and
  139. --define input (disable) and output faces.
  140. function redClock(length, outFaces, pLength, inFaces)
  141.     --input sterilization
  142.     length = length or 10
  143.     length = tonumber(length)
  144.     if outFaces ~= nil then
  145.         for i, v in ipairs(outFaces) do
  146.             v = strl.faceForm(v)
  147.         end
  148.     else
  149.         outFaces = {'left', 'right', 'back'}
  150.     end
  151.     pLength = pLength or 0.1
  152.     pLength = tonumber(pLength)
  153.     if inFaces ~= nil then
  154.         for i, v in ipairs(outFaces) do
  155.             v = strl.faceForm(v)
  156.         end
  157.     else
  158.         inFaces = {'front'}
  159.     end
  160.    
  161.     --clock functionality
  162.     while true do
  163.         local disable = false
  164.         for i, v in ipairs(inFaces) do
  165.             if redstone.getInput(v) then disable = true end
  166.         end
  167.         if not(disable) then
  168.             sleep(length - pLength)
  169.             redPulse(pLength, faces, true)
  170.         end
  171.     end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment