Advertisement
osmarks

3DGraph master

Dec 27th, 2020 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1.     local m = peripheral.find "modem"
  2. local CHAN = 7101
  3. m.open(CHAN)
  4.  
  5. local function send(ms) m.transmit(CHAN, CHAN, ms) end
  6.  
  7. local function receive()
  8.     while true do
  9.         local _, _, c, rc, ms = os.pullEvent "modem_message"
  10.         if type(ms) == "table" then
  11.             return ms
  12.         end
  13.     end
  14. end
  15.  
  16. local nodes = {}
  17.  
  18. parallel.waitForAny(function()
  19.     send { "ping" }
  20.     while true do
  21.         local ty, id = unpack(receive())
  22.         if ty == "pong" then
  23.             print(id, "detected")
  24.             table.insert(nodes, id)
  25.         end
  26.     end
  27. end, function() sleep(0.5) end)
  28.  
  29. local d_min = {202, 65, 231}
  30. local d_max = {202 + 63, 65 + 63, 231 + 63}
  31. local block = "botania:managlass"
  32.  
  33. local eqn = ...
  34. local fn, err = load(("local x, y, z = ...; return %s"):format(eqn), "=eqn", "t", math)
  35. if not fn then error("syntax error: " .. err, 0) end
  36.  
  37. print(#nodes, "nodes are available")
  38.  
  39. local x_range = d_max[1] - d_min[1] + 1
  40. local split = math.floor(x_range / #nodes)
  41.  
  42. local commands = {}
  43. local x_acc = d_min[1]
  44. for k, v in ipairs(nodes) do
  45.     local x_size = split
  46.     if k == #nodes then
  47.         x_size = x_range - ((#nodes - 1) * split)
  48.     end
  49.     local t = x_acc
  50.     x_acc = x_acc + x_size
  51.     send {"plot", {
  52.         x_min = d_min[1], x_max = d_max[1],
  53.         y_min = d_min[2], y_max = d_max[2],
  54.         z_min = d_min[3], z_max = d_max[3],
  55.         x_mod = #nodes, x_mod_eq = k - 1,
  56.         block = block,
  57.         equation = eqn,
  58.         id = v,
  59.         f_min = d_min,
  60.         f_max = d_max
  61.     }}
  62. end
  63.  
  64. local responses = {}
  65. while #responses ~= #nodes do
  66.     local m = receive()
  67.     if m[1] == "response" then
  68.         table.insert(responses, m[2])
  69.         print("response from", m[2].id, m[2].response)
  70.     end
  71. end
  72.  
  73. print "Plot plotted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement