Advertisement
Guest User

SimpleAdore v1.0

a guest
Aug 20th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --[[
  2.  
  3.     SimpleAdore - wireless made simple
  4.     v1.0
  5.  
  6.     Copyright (C) 2017 HaddockDev
  7.  
  8.     This program is free software: you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation, either version 3 of the License, or
  11.     (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17. ]]
  18.  
  19. --Vars
  20. local Channels = {}
  21. local Modem = nil
  22. local isDebug = false
  23.  
  24. --Helper functions
  25. local function l (msg)
  26.     if isDebug then
  27.         print(msg)
  28.     end
  29. end
  30.  
  31. local function OneOf (needle,haystack)
  32.     for _k,v in ipairs(haystack) do
  33.         if v == needle then
  34.             return true
  35.         end
  36.     end
  37.     return false
  38. end
  39.  
  40. local function FindPeripheral (type)
  41.     local sides = {"top","bottom","left","right","back","front"}
  42.     for _k,v in ipairs(sides) do
  43.         if peripheral.getType(v) == type then
  44.             return type
  45.         end
  46.     end
  47.     return nil
  48. end
  49.  
  50. local function ModemCheck(err)
  51.     if not Modem then
  52.         if err then
  53.             error(err)
  54.         else
  55.             return false
  56.         end
  57.     end
  58.     return true
  59. end
  60.  
  61. local function Try(fn)
  62.     pcall(function()fn()end)
  63. end
  64.  
  65. --API
  66. function WrapModem(side)
  67.     local side = side or FindPeripheral("wireless_modem")
  68.     if side ~= nil then
  69.         Modem = peripheral.wrap(side)
  70.         l("Wrapped modem on side " .. side)
  71.     end
  72. end
  73.  
  74. function AddChannel(name, channel)
  75.     Channels[name] = channel
  76.     Modem.open(channel)
  77.     l("Added channel " .. name .. "/" .. channel)
  78. end
  79.  
  80. function GetChannels()
  81.     return Channels
  82. end
  83.  
  84. --Wireless
  85. function Transmit(channel, data)
  86.     if type(data) == "table" then
  87.         data = textutils.serialize(data)
  88.     end
  89.     Modem.transmit(channel, channel + 1, data)
  90. end
  91.  
  92. function Recieve (channel)
  93.     local ok = false
  94.     while not ok do
  95.         local e, _1, sChannel, _2, msg, dist = os.pullEvent("modem_message")
  96.         if sChannel == channel then
  97.             return msg, dist
  98.         end
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement