masoninsicily

Untitled

Nov 17th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  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 - 1))
  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) and not isIn(exclude, i .. "-" .. j) then
  114. print("Run #" .. i .. "." .. j .. "...")
  115. g("setInput", j, true)
  116. local effComb = g("getActiveEffects")[8] or "{}"
  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. if isIn(effCombUS, i) then
  122. table.insert(toRemove, i)
  123. end
  124. end
  125. for num, i in ipairs(toRemove) do
  126. local _, pos = isIn(effCombUS, i)
  127. table.remove(effCombUS, pos)
  128. end
  129. toRemove = {}
  130. for num, j in ipairs(effJ) do
  131. if isIn(effCombUS, j) then
  132. table.insert(toRemove, j)
  133. end
  134. end
  135. for num, i in ipairs(toRemove) do
  136. local _, pos = isIn(effCombUS, i)
  137. table.remove(effCombUS, pos)
  138. end
  139. effComb = ser.serialize(effCombUS)
  140. _G.effectscomb[i][j] = effComb
  141. print("Found effects:")
  142. print(_G.effectscomb[i][j])
  143. g("setInput", j, false)
  144. else
  145. print("Run #" .. i .. "." .. j .. " skipped on user's request")
  146. end
  147. end
  148. end
  149. g("setInput", i, false)
  150. else
  151. print("Run #" .. i .. " skipped on user's request")
  152. end
  153. end
  154. end
  155.  
  156. local function clear()
  157. for i = 1, _G.max, 1 do
  158. print("Turning off #" .. i)
  159. g("setInput", i, false)
  160. end
  161. end
  162.  
  163. local function ge()
  164. for i = 1, _G.max, 1 do
  165. if _G.effects[i] then
  166. print("Input #" .. i .. ":\t" .. _G.effects[i])
  167. end
  168. end
  169. end
  170.  
  171. local function getCombo()
  172. for numi, i in pairs(_G.effectscomb) do
  173. for numj, j in pairs(_G.effectscomb[numi]) do
  174. if j ~= "{}" then
  175. print("Input #" .. numi .. "+" .. numj .. ":\t" .. j)
  176. end
  177. end
  178. end
  179. end
  180.  
  181. local function reset()
  182. _G.max, _G.port, _G.effects = 15, 27091, {}
  183. end
  184.  
  185. local function info()
  186. print("PORT: " .. _G.port)
  187. print("MAX: " .. _G.max)
  188. print("EFFECTS: ")
  189. ge()
  190. end
  191.  
  192. local function gc(...)
  193. local data = g(...)
  194. io.write("FROM " .. data[4] .. " in " .. data[5] .. " msg: ")
  195. for i = 7, #data, 1 do
  196. io.write(data[i] .. " ")
  197. end
  198. print()
  199. end
  200.  
  201. local function on(i)
  202. g("setInput", i, true)
  203. end
  204.  
  205. local function off(i)
  206. g("setInput", i, false)
  207. end
  208.  
  209. local function getHP()
  210. local data = g("getHealth")
  211. io.write("HP: " .. string.rep("♥", data[8]) .. string.rep("♡", data[9] - data[8]) .. " (" .. data[8] .. "/" .. data[9] .. ")\n")
  212. end
  213.  
  214. local function getHung()
  215. local data = g("getHunger")
  216. io.write("Hunger: " .. data[8] .. " | Saturation: " .. data[9])
  217. end
  218.  
  219. local function getEnergy()
  220. local data = g("getPowerState")
  221. io.write("↯: " .. data[8] .. "/" .. data[9] .. " (" .. math.floor(data[8] / data[9] * 100) .. "%)")
  222. end
  223.  
  224. local function formatNum(num)
  225. return num > 0 and "+" .. tostring(num) or tostring(num)
  226. end
  227.  
  228. local function usage()
  229. print("Requesting data...")
  230. local data = {}
  231. for run = 1, 2, 1 do
  232. data[run] = g("getPowerState")
  233. os.sleep(1)
  234. end
  235. print("Usage: " .. formatNum(data[2][8] - data[1][8]) .. " per second")
  236. end
  237.  
  238. local function getAge()
  239. local data = g("getAge")
  240. io.write("Age: " .. data[8] .. "s")
  241. end
  242.  
  243. local function getName()
  244. local data = g("getName")
  245. io.write("Player's name is " .. data[8])
  246. end
  247.  
  248. local function getInputsInfo()
  249. local safe = g("getSafeActiveInputs")
  250. local max = g("getMaxActiveInputs")
  251. print("Safe: " .. safe[8] .. ", max: " .. max[8])
  252. end
  253.  
  254. local function getActiveEffects()
  255. local data = g("getActiveEffects")
  256. print(data[8])
  257. end
  258.  
  259. local function group(...)
  260. local args = {...}
  261. local command = args[1]
  262. table.remove(args, 1)
  263. if command == "set" then
  264. local name = args[1]
  265. table.remove(args, 1)
  266. local inputs = args
  267. for num, i in pairs(inputs) do
  268. if not tonumber(i) then
  269. table.remove(inputs, num)
  270. end
  271. end
  272. _G.groups[name] = inputs
  273. print("Added group \"" .. name .. "\" with inputs:\t" .. unicode.sub(ser.serialize(inputs), 2, -2))
  274. elseif command == "del" then
  275. local name = args[1]
  276. _G.groups[name] = nil
  277. print("Removed group \"" .. name .. "\"")
  278. elseif command == "save" then
  279. local f = io.open(CONF, "w")
  280. f:write("group={")
  281. local grstr = ""
  282. for name, value in pairs(groups) do
  283. grstr = grstr .. "[\"" .. name .. "\"]={"
  284. for _, i in ipairs(value) do
  285. grstr = grstr .. i .. ","
  286. end
  287. grstr = unicode.sub(grstr, 1, -2) .. "},"
  288. end
  289. grstr = unicode.sub(grstr, 1, -2)
  290. f:write(grstr.."}")
  291. f:close()
  292. print("Saved to file")
  293. elseif command == "on" or command == "off" then
  294. local name = args[1]
  295. table.remove(args, 1)
  296. if _G.groups[name] then
  297. for _, i in pairs(_G.groups[name]) do
  298. if command == "on" then
  299. on(i)
  300. else
  301. off(i)
  302. end
  303. end
  304. print("Group \"" .. name .. "\" " .. (command == "on" and "activated" or "disabled"))
  305. end
  306. elseif command == "list" then
  307. for name, value in pairs(_G.groups) do
  308. print("Group \"" .. name .. "\":\t" .. unicode.sub(ser.serialize(value), 2, -2))
  309. end
  310. end
  311. end
  312.  
  313. local actions = {
  314. get = ge,
  315. clear = clear,
  316. test = test,
  317. init = init,
  318. g = gc,
  319. s = s,
  320. reset = reset,
  321. info = info,
  322. on = on,
  323. off = off,
  324. hp = getHP,
  325. hunger = getHung,
  326. energy = getEnergy,
  327. usage = usage,
  328. age = getAge,
  329. name = getName,
  330. input = getInputsInfo,
  331. efon = getActiveEffects,
  332. combo = combotest,
  333. getcombo = getCombo,
  334. group = group
  335. }
  336.  
  337. local args = {...}
  338. local command = args[1]
  339. table.remove(args, 1)
  340.  
  341. for num, i in ipairs(args) do
  342. if tonumber(i) then
  343. args[num] = tonumber(i)
  344. end
  345. end
  346.  
  347. if not command then
  348. actions["init"]()
  349. end
  350. if actions[command] then
  351. actions[command](table.unpack(args))
  352. end
Add Comment
Please, Sign In to add comment