JPiolho

[Minecraft][CC] Craftnet API

Feb 29th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.74 KB | None | 0 0
  1. -- Craftnet API
  2. -- Version 1.0
  3. -- By JPiolho
  4. -- Feel free to copy and edit as long you credit the original author
  5. -- More Info @ http://bit.ly/zoxn6V
  6.  
  7. _connections = {}
  8.  
  9. MODE = {
  10.     Clocked = 0,
  11.     Pulsed = 1
  12. }
  13.  
  14.  
  15. local function Conn(id)
  16.  return _connections[id]
  17. end
  18.  
  19. local function BitsToNum(btbl)
  20.     local num = 0
  21.     local exp = 1
  22.  
  23.     local i,v
  24.     for i,v in pairs(btbl) do
  25.         if v == 1 then
  26.             num = num + exp
  27.         end
  28.  
  29.         exp = exp * 2
  30.     end
  31.  
  32.     return num
  33. end
  34.  
  35. function Open(datasides,clockside,mode)
  36.     local connInfo = {
  37.         Side = datasides,
  38.         ClockSide = clockside,
  39.         Clock = true,
  40.  
  41.         Mode = mode,
  42.  
  43.         Receiving = false,
  44.         Sending = false,
  45.         Speed = 0.1,
  46.  
  47.         Buffer = {}
  48.     }
  49.  
  50.     if mode == nil then connInfo.Mode = MODE.Clocked end
  51.  
  52.  
  53.     table.insert(_connections,connInfo);
  54.     return #_connections
  55. end
  56.  
  57.  
  58.  
  59. function Receive(conn)
  60.     local info = Conn(conn)
  61.  
  62.     while true do
  63.         local e,v = os.pullEvent()
  64.  
  65.         if e == "redstone" then
  66.             if info.Receiving == false and redstone.getInput(info.ClockSide) == true then
  67.                 local anyTrue = false
  68.                 local i
  69.                 for i = 1, #info.Side do
  70.                     if redstone.getInput(info.Side[i]) == true then
  71.                         anyTrue = true
  72.                     end
  73.                 end
  74.  
  75.                 if anyTrue then
  76.                     info.Receiving = true
  77.                     break;
  78.                 end
  79.             end
  80.         end
  81.     end
  82. end
  83.  
  84. function IsReceiving(conn)
  85.     return Conn(conn).Receiving
  86. end
  87.  
  88. function IsSending(conn)
  89.     return Conn(conn).Sending
  90. end
  91.  
  92. function IsBusy(conn)
  93.     return Conn(conn).Receiving or Conn(conn).Sending
  94. end
  95.  
  96. function GetSpeed(conn)
  97.     return Conn(conn).Speed
  98. end
  99.  
  100. function WaitForClock(conn)
  101.     local info = Conn(conn)
  102.     while true do
  103.         local e,v = os.pullEvent()
  104.  
  105.         if e == "redstone" then
  106.             local expectedClock
  107.             if info.Clock == true then expectedClock = false else expectedClock = true end
  108.             if redstone.getInput(info.ClockSide) == expectedClock then
  109.                 info.Clock = expectedClock
  110.                 break
  111.             end
  112.         end
  113.     end
  114. end
  115.  
  116. local function ReceiveNumber(conn,size)
  117.     local info = Conn(conn)
  118.     local x = 1
  119.     local bits = {}
  120.  
  121.     while x < size do
  122.         WaitForClock(conn)
  123.  
  124.         local i
  125.         for i = 1, #info.Side do
  126.             local val
  127.             if redstone.getInput(info.Side[i]) == true then val = 1 else val = 0 end
  128.             table.insert(bits,val)
  129.  
  130.             x = x + 1
  131.             if x >= 8 then break end -- Dont read more than 8 bits
  132.         end
  133.     end
  134.  
  135.     return BitsToNum(bits)
  136. end
  137.  
  138. function ReceiveWeirdo(conn,size)
  139.     return ReceiveNumber(conn,size)
  140. end
  141.  
  142. function ReceiveByte(conn)
  143.     return ReceiveNumber(conn,8)
  144. end
  145.  
  146. function ReceiveInt16(conn)
  147.     return ReceiveNumber(conn,16)
  148. end
  149.  
  150. function ReceiveInt32(conn)
  151.     return ReceiveNumber(conn,32)
  152. end
  153.  
  154.  
  155. function SetSpeed(conn,speed)
  156.     local info = Conn(conn)
  157.     info.Speed = speed
  158. end
  159.  
  160. function ReceiveString(conn)
  161.     local size = ReceiveByte(conn)
  162.     local txt = ""
  163.     local i
  164.     for i = 1,size do
  165.         txt = txt .. string.char(ReceiveByte(conn))
  166.     end
  167.  
  168.     return txt
  169. end
  170.  
  171. function BeginPacket(conn)
  172.     local info = Conn(conn)
  173.  
  174.     if info.Receiving then
  175.         return
  176.     end
  177.  
  178.     local i
  179.     for i = 1, #info.Side do
  180.         redstone.setOutput(info.Side[i],true)
  181.     end
  182.  
  183.     redstone.setOutput(info.ClockSide,true)
  184.  
  185.     info.Clock = true
  186.  
  187.     info.Sending = true
  188.  
  189.     sleep(info.Speed)
  190. end
  191.  
  192.  
  193. local function SendNumber(conn,byte,size)
  194.     local info = Conn(conn)
  195.  
  196.     if info.Receiving == true then
  197.         return
  198.     end
  199.  
  200.     local bits = bit.tobits(byte)
  201.  
  202.     if #bits < size then
  203.         local x
  204.         for x = #bits+1,size do
  205.             table.insert(bits,0)
  206.         end
  207.     end
  208.     local k = 1
  209.     while k < #bits do
  210.         local i
  211.         for i = 1, #info.Side do
  212.  
  213.             if k > size then
  214.                 redstone.setOutput(info.Side[i],false)
  215.             end
  216.  
  217.             local val = bits[k]
  218.             if val == 1 then
  219.                 redstone.setOutput(info.Side[i],true)
  220.             else
  221.                 redstone.setOutput(info.Side[i],false)
  222.             end
  223.  
  224.             k = k + 1
  225.         end
  226.  
  227.         if info.Clock == true then
  228.             info.Clock = false
  229.             redstone.setOutput(info.ClockSide,false)
  230.         else
  231.             info.Clock = true
  232.             redstone.setOutput(info.ClockSide,true)
  233.         end
  234.  
  235.  
  236.         sleep(info.Speed)
  237.  
  238.     end
  239. end
  240.  
  241.  
  242. function SendByte(conn,byte)
  243.     SendNumber(conn,byte,8)
  244. end
  245.  
  246. function SendInt16(conn,int16)
  247.     SendNumber(conn,int16,16)
  248. end
  249.  
  250. function SendInt32(conn,int32)
  251.     SendNumber(conn,int32,32)
  252. end
  253.  
  254. function SendWeirdo(conn,weirdo,size)
  255.     SendNumber(conn,weirdo,size)
  256. end
  257.  
  258. function SendString(conn,text)
  259.     local info = Conn(conn)
  260.     if info.Receiving == true then
  261.         return
  262.     end
  263.  
  264.     SendByte(conn,string.len(text))
  265.     local i
  266.     for i = 1, string.len(text) do
  267.         SendByte(conn,string.byte(text,i))
  268.     end
  269. end
  270.  
  271. function EndPacket(conn,sleep)
  272.     local info = Conn(conn)
  273.     info.Sending = false
  274.     info.Receiving = false
  275.  
  276.     local i
  277.     for i = 1, #info.Side do
  278.         redstone.setOutput(info.Side[i],false)
  279.     end
  280.     redstone.setOutput(info.ClockSide,false)
  281.  
  282.     if sleep ~= nil and sleep == true then
  283.         sleep(info.Speed)
  284.     end
  285. end
Advertisement
Add Comment
Please, Sign In to add comment