Skiret

nn

Nov 29th, 2020
656
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local CONF = "/etc/nn.conf"
  2.  
  3. local m = require("component").modem
  4. local event = require("event")
  5. local ser = require("serialization")
  6. local fs = require("filesystem")
  7. local unicode = require("unicode")
  8. _G.port = _G.port or 27091
  9. _G.max = _G.max or 15
  10. _G.effects = _G.effects or {}
  11. _G.effectscomb = _G.effectscomb or {}
  12. _G.groups = _G.groups or {}
  13.  
  14. local function s(...)
  15.   m.broadcast(_G.port, "nanomachines", ...)
  16. end
  17.  
  18. local function g(...)
  19.   s(...)
  20.   return {event.pull(6, "modem_message")}
  21. end
  22.  
  23. local function init(rqpt, prpt)
  24.   _G.port = rqpt or _G.port
  25.   prpt = prpt or _G.port
  26.   m.broadcast(prpt, "nanomachines", "setResponsePort", _G.port)
  27.   event.pull(6, "modem_message")
  28.   m.close(prpt)
  29.   m.open(_G.port)
  30.   _G.max = g("getTotalInputCount")[8]
  31.   if fs.exists(CONF) then
  32.     dofile(CONF)
  33.   else
  34.     group = {}
  35.   end
  36.   _G.groups = group
  37.   print("Configured: PORT " .. _G.port .. ", MAX " .. _G.max)
  38. end
  39.  
  40. local function isIn(tbl, value)
  41.   for i = 1, #tbl, 1 do
  42.     if tbl[i] == value then
  43.       return true, i
  44.     end
  45.   end
  46.   return false
  47. end
  48.  
  49. local function test(...)
  50.   local exclude = {...}
  51.   print("Starting basic testing")
  52.   print("Total runs: " .. _G.max)
  53.   print("Testing starts in 3s...")
  54.   os.sleep(3)
  55.   print("Beginning test")
  56.   _G.effects = {}
  57.   for i = 1, _G.max, 1 do
  58.     if not isIn(exclude, i) then
  59.       print("Run #" .. i)
  60.       g("setInput", i, true)
  61.       _G.effects[i] = g("getActiveEffects")[8]
  62.       g("setInput", i, false)
  63.       print("Effects found:")
  64.       print(_G.effects[i])
  65.     else
  66.       print("Run #" .. i .. " skipped on user's request")
  67.     end
  68.   end
  69. end
  70.  
  71. local function recurSum(num)
  72.   if num > 0 then
  73.     return num + recurSum(num - 1)
  74.   end
  75.   return 0
  76. end
  77.  
  78. local function splitComma(str)
  79.   str = str:sub(2, -2)
  80.   local l = {}
  81.   for i in str:gmatch("(.-),.-") do
  82.     table.insert(l, i)
  83.   end
  84.   table.insert(l, str:match(".+,(.+)"))
  85.   if #l == 0 then
  86.     if str ~= "" then
  87.       table.insert(l, str)
  88.     end
  89.   end
  90.   return l
  91. end
  92.  
  93. local function combotest(...)
  94.   print("Combinatoric test")
  95.   print("Total runs: " .. recurSum(_G.max))
  96.   print("It may take very long time!")
  97.   print("Testing begins is 3s...")
  98.   os.sleep(3)
  99.   if #_G.effects == 0 then
  100.     print("No input info, starting basic testing")
  101.     test(...)
  102.   end
  103.   print("Started combinatoric test")
  104.   _G.effectscomb = {}
  105.   local exclude = {...}
  106.   for i = 1, _G.max, 1 do
  107.     if not isIn(exclude, i) then
  108.       _G.effectscomb[i] = {}
  109.       print("Run #" .. i)
  110.       g("setInput", i, true)
  111.       for j = i, _G.max, 1 do
  112.         if i ~= j then
  113.           if not isIn(exclude, j) then
  114.             print("Run #" .. i .. "." .. j .. "...")
  115.             g("setInput", j, true)
  116.             local effComb = g("getActiveEffects")[8]
  117.             local effI, effJ = splitComma(_G.effects[i]), splitComma(_G.effects[j])
  118.             local effCombUS = splitComma(effComb)
  119.             local toRemove = {}
  120.             for num, i in ipairs(effI) do
  121. --            print("i", num, i, "isin", isIn(effCombUS, i))
  122.               if isIn(effCombUS, i) then
  123. --              print("Here [i]")
  124.                 table.insert(toRemove, i)
  125.               end
  126.             end
  127.             for num, i in ipairs(toRemove) do
  128.               local _, pos = isIn(effCombUS, i)
  129.               table.remove(effCombUS, pos)
  130.             end
  131.             toRemove = {}
  132.             for num, j in ipairs(effJ) do
  133. --            print("j", num, j, "isin", isIn(effCombUS, j))
  134.               if isIn(effCombUS, j) then
  135. --              print("Here [j]", isIn(effCombUS, j))
  136.                 table.insert(toRemove, j)
  137.               end
  138.             end
  139.             for num, i in ipairs(toRemove) do
  140.               local _, pos = isIn(effCombUS, i)
  141.               table.remove(effCombUS, pos)
  142.             end
  143.             effComb = ser.serialize(effCombUS)
  144.             _G.effectscomb[i][j] = effComb
  145.             print("Found effects:")
  146.             print(_G.effectscomb[i][j])
  147.             g("setInput", j, false)
  148.           else
  149.             print("Run #" .. i .. "." .. j .. " skipped on user's request")
  150.           end
  151.         end
  152.       end
  153.       g("setInput", i, false)
  154.     else
  155.       print("Run #" .. i .. " skipped on user's request")
  156.     end
  157.   end
  158. end
  159.  
  160. local function clear()
  161.   for i = 1, _G.max, 1 do
  162.     print("Turning off #" .. i)
  163.     g("setInput", i, false)
  164.   end
  165. end
  166.  
  167. local function ge()
  168.   for i = 1, _G.max, 1 do
  169.     if _G.effects[i] then
  170.       print("Input #" .. i .. ":\t" .. _G.effects[i])
  171.     end
  172.   end
  173. end
  174.  
  175. local function getCombo()
  176.   for numi, i in pairs(_G.effectscomb) do
  177.     for numj, j in pairs(_G.effectscomb[numi]) do
  178.       if j ~= "{}" then
  179.         print("Input #" .. numi .. "+" .. numj .. ":\t" .. j)
  180.       end
  181.     end
  182.   end
  183. end
  184.  
  185. local function reset()
  186.   _G.max, _G.port, _G.effects = 15, 27091, {}
  187. end
  188.  
  189. local function info()
  190.   print("PORT: " .. _G.port)
  191.   print("MAX: " .. _G.max)
  192.   print("EFFECTS: ")
  193.   ge()
  194. end
  195.  
  196. local function gc(...)
  197.   local data = g(...)
  198.   io.write("FROM " .. data[4] .. " in " .. data[5] .. " msg: ")
  199.   for i = 7, #data, 1 do
  200.     io.write(data[i] .. " ")
  201.   end
  202.   print()
  203. end
  204.  
  205. local function on(i)
  206.   g("setInput", i, true)
  207. end
  208.  
  209. local function off(i)
  210.   g("setInput", i, false)
  211. end
  212.  
  213. local function getHP()
  214.   local data = g("getHealth")
  215.   io.write("HP: " .. string.rep("♥", data[8]) .. string.rep("♡", data[9] - data[8]) .. " (" .. data[8] .. "/" .. data[9] .. ")\n")
  216. end
  217.  
  218. local function getHung()
  219.   local data = g("getHunger")
  220.   io.write("Hunger: " .. data[8] .. " | Saturation: " .. data[9])
  221. end
  222.  
  223. local function getEnergy()
  224.   local data = g("getPowerState")
  225.   io.write("↯: " .. data[8] .. "/" .. data[9] .. " (" .. math.floor(data[8] / data[9] * 100) .. "%)")
  226. end
  227.  
  228. local function formatNum(num)
  229.   return num > 0 and "+" .. tostring(num) or tostring(num)
  230. end
  231.  
  232. local function usage()
  233.   print("Requesting data...")
  234.   local data = {}
  235.   for run = 1, 2, 1 do
  236.     data[run] = g("getPowerState")
  237.     os.sleep(1)
  238.   end
  239.   print("Usage: " .. formatNum(data[2][8] - data[1][8]) .. " per second")
  240. end
  241.  
  242. local function getAge()
  243.   local data = g("getAge")
  244.   io.write("Age: " .. data[8] .. "s")
  245. end
  246.  
  247. local function getName()
  248.   local data = g("getName")
  249.   io.write("Player's name is " .. data[8])
  250. end
  251.  
  252. local function getInputsInfo()
  253.   local safe = g("getSafeActiveInputs")
  254.   local max = g("getMaxActiveInputs")
  255.   print("Safe: " .. safe[8] .. ", max: " .. max[8])
  256. end
  257.  
  258. local function getActiveEffects()
  259.   local data = g("getActiveEffects")
  260.   print(data[8])
  261. end
  262.  
  263. local function group(...)
  264.   local args = {...}
  265.   local command = args[1]
  266.   table.remove(args, 1)
  267.   if command == "set" then
  268.     local name = args[1]
  269.     table.remove(args, 1)
  270.     local inputs = args
  271.     for num, i in pairs(inputs) do
  272.       if not tonumber(i) then
  273.         table.remove(inputs, num)
  274.       end
  275.     end
  276.     _G.groups[name] = inputs
  277.     print("Added group \"" .. name .. "\" with inputs:\t" .. unicode.sub(ser.serialize(inputs), 2, -2))
  278.   elseif command == "del" then
  279.     local name = args[1]
  280.     _G.groups[name] = nil
  281.     print("Removed group \"" .. name .. "\"")
  282.   elseif command == "save" then
  283.     local f = io.open(CONF, "w")
  284.     f:write("group={")
  285.     local grstr = ""
  286.     for name, value in pairs(groups) do
  287.       grstr = grstr .. "[\"" .. name .. "\"]={"
  288.       for _, i in ipairs(value) do
  289.         grstr = grstr .. i .. ","
  290.       end
  291.       grstr = unicode.sub(grstr, 1, -2) .. "},"
  292.     end
  293.     grstr = unicode.sub(grstr, 1, -2)
  294.     f:write(grstr.."}")
  295.     f:close()
  296.     print("Saved to file")
  297.   elseif command == "on" or command == "off" then
  298.     local name = args[1]
  299.     table.remove(args, 1)
  300.     if _G.groups[name] then
  301.       for _, i in pairs(_G.groups[name]) do
  302.         if command == "on" then
  303.           on(i)
  304.         else
  305.           off(i)
  306.         end
  307.       end
  308.       print("Group \"" .. name .. "\" " .. (command == "on" and "activated" or "disabled"))
  309.     end
  310.   elseif command == "list" then
  311.     for name, value in pairs(_G.groups) do
  312.       print("Group \"" .. name .. "\":\t" .. unicode.sub(ser.serialize(value), 2, -2))
  313.     end
  314.   end
  315. end
  316.  
  317. local actions = {
  318.   get = ge,
  319.   clear = clear,
  320.   test = test,
  321.   init = init,
  322.   g = gc,
  323.   s = s,
  324.   reset = reset,
  325.   info = info,
  326.   on = on,
  327.   off = off,
  328.   hp = getHP,
  329.   hunger = getHung,
  330.   energy = getEnergy,
  331.   usage = usage,
  332.   age = getAge,
  333.   name = getName,
  334.   input = getInputsInfo,
  335.   efon = getActiveEffects,
  336.   combo = combotest,
  337.   getcombo = getCombo,
  338.   group = group
  339. }
  340.  
  341. local args = {...}
  342. local command = args[1]
  343. table.remove(args, 1)
  344.  
  345. for num, i in ipairs(args) do
  346.   if tonumber(i) then
  347.     args[num] = tonumber(i)
  348.   end
  349. end
  350.  
  351. if not command then
  352.   actions["init"]()
  353. end
  354. if actions[command] then
  355.   actions[command](table.unpack(args))
  356. end
RAW Paste Data