Advertisement
osmarks

holomerge

Dec 28th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. local f = fs.open("holo.conf", "r")
  2. local conf = textutils.unserialise(f.readAll())
  3. f.close()
  4.  
  5. local function find_by_partial_ID(id)
  6.     return peripheral.find("hologram", function(name)
  7.         return name:find(id) ~= nil
  8.     end)
  9. end
  10.  
  11. local real_holos = {}
  12. for id, pos in pairs(conf.projectors) do
  13.     local p = find_by_partial_ID(id)
  14.     if not p then error(id .. " not found!") end
  15.     table.insert(real_holos, { peripheral = p, position = pos})
  16. end
  17.  
  18. local function on_all(fn, ...)
  19.     for _, h in pairs(real_holos) do
  20.         h.peripheral[fn](...)
  21.     end
  22. end
  23.  
  24. local h = {}
  25. h.translation = {0, 0, 0}
  26. h.scale = 1
  27.  
  28. function h.getTranslation()
  29.     return unpack(h.translation)
  30. end
  31.  
  32. function h.update_translation()
  33.     local x, y, z = unpack(h.translation)
  34.     for _, r in pairs(real_holos) do
  35.         local hx, hy, hz = unpack(r.position)
  36.         local tx, ty, tz = hx * h.scale * 3, hy * h.scale * 2, hz * h.scale * 3
  37.         r.peripheral.setTranslation(x - tx, y - ty, z - tz)
  38.     end
  39. end
  40.  
  41. function h.setTranslation(x, y, z)
  42.     h.translation = {x, y, z}
  43.     h.update_translation()
  44. end
  45.  
  46. function h.setScale(s)
  47.     on_all("setScale", s)
  48.     h.update_translation()
  49.     h.scale = s
  50. end
  51.  
  52. function h.getScale()
  53.     return h.scale
  54. end
  55.  
  56. function h.maxDepth()
  57.     return 3 -- no idea what else to put - dynamically calculating it is probably fiddly
  58. end
  59.  
  60. local function proxy(method)
  61.     return function(...)
  62.         on_all(method, ...)
  63.     end
  64. end
  65.  
  66. h.clear = proxy "clear"
  67. h.copy = proxy "copy"
  68.  
  69. local function color_index(col)
  70.     if col == 0 then return 1, 0 end
  71.     local projector = math.ceil(col / 3)
  72.     local index = (col % 3) + 1
  73.     return projector, index
  74. end
  75.  
  76. function h.set(x, y, z, c)
  77.     on_all("set", x, y, z, 0) -- clear it on other projectors
  78.     local p, i = color_index(c)
  79.     real_holos[p].peripheral.set(x, y, z, i)
  80. end
  81.  
  82. function h.get(x, y, z)
  83.     error "Unavailable"
  84. end
  85.  
  86. function h.fill(x, z, minY, maxY, c)
  87.     on_all("fill", x, z, minY, maxY, 0) -- clear it on other projectors
  88.     local p, i = color_index(c)
  89.     real_holos[p].peripheral.fill(x, z, minY, maxY, i)
  90. end
  91.  
  92. function h.getPaletteColor(c)
  93.     local p, i = color_index(c)
  94.     return real_holos[p].peripheral.getPaletteColor(i)
  95. end
  96.  
  97. function h.setPaletteColor(c, rgb)
  98.     local p, i = color_index(c)
  99.     return real_holos[p].peripheral.setPaletteColor(i, rgb)
  100. end
  101.  
  102. h.setScale(1)
  103. h.clear()
  104. h.setTranslation(0, 0, 0)
  105.  
  106. return h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement