Advertisement
serafim7

Hologram Viewer 0.70 Beta [OpenComputers]

Jul 26th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. --http://computercraft.ru/topic/259-hologram-editor-opencomputers/
  2.  
  3. --      Hologram Viewer v0.70
  4. -- by NEO, Totoro (aka MoonlightOwl)
  5. -- 11/12/2014, all right reserved =)
  6. --        computercraft.ru
  7.  
  8. local fs = require('filesystem')
  9. local com = require('component')
  10. local args = { ... }
  11.  
  12. -- ================================ H O L O G R A M S   S T U F F ================================ --
  13. -- loading add. components
  14. function trytofind(name)
  15.   if com.isAvailable(name) then
  16.     return com.getPrimary(name)
  17.   else
  18.     return nil
  19.   end
  20. end
  21.  
  22. -- constants
  23. HOLOH = 32
  24. HOLOW = 48
  25.  
  26. -- hologram vars
  27. holo = {}
  28. colortable = {{},{},{}}
  29. hexcolortable = {}
  30. proj_scale = 1.0
  31.  
  32. function set(x, y, z, value)
  33.   if holo[x] == nil then holo[x] = {} end
  34.   if holo[x][y] == nil then holo[x][y] = {} end
  35.   holo[x][y][z] = value
  36. end
  37. function get(x, y, z)
  38.   if holo[x] ~= nil and holo[x][y] ~= nil and holo[x][y][z] ~= nil then
  39.     return holo[x][y][z]
  40.   else
  41.     return 0
  42.   end
  43. end
  44. function rgb2hex(r,g,b)
  45.   return r*65536+g*256+b
  46. end
  47.  
  48.  
  49. local reader = {}
  50. function reader:init(file)
  51.   self.buffer = {}
  52.   self.file = file
  53. end
  54. function reader:read()
  55.   if #self.buffer == 0 then
  56.     if not self:fetch() then return nil end
  57.   end
  58.   local sym = self.buffer[#self.buffer]
  59.   self.buffer[#self.buffer] = nil
  60.   return sym
  61. end
  62. function reader:fetch()
  63.   self.buffer = {}
  64.   local char = file:read(1)
  65.   if char == nil then return false
  66.   else
  67.     local byte = string.byte(char)
  68.     for i=0, 3 do
  69.       local a = byte % 4
  70.       byte = math.floor(byte / 4)
  71.       self.buffer[4-i] = a
  72.     end
  73.     return true
  74.   end
  75. end
  76.  
  77. local function loadHologram(filename)
  78.   if filename == nil then
  79.     error("[ОШИБКА] Введено неверное имя файла.")
  80.   end
  81.  
  82.   local compressed
  83.   if string.sub(filename, -4) == '.3dx' then
  84.     compressed = true
  85.   elseif string.sub(filename, -3) == '.3d' then
  86.     compressed = false
  87.   else
  88.     error("[ОШИБКА] Укажите расширение файла.")
  89.   end
  90.  
  91.   if fs.exists(filename) then
  92.     file = io.open(filename, 'rb')
  93.     if file ~= nil then
  94.       for i=1, 3 do
  95.         for c=1, 3 do
  96.           colortable[i][c] = string.byte(file:read(1))
  97.         end
  98.         hexcolortable[i] = rgb2hex(colortable[i][1], colortable[i][2], colortable[i][3])
  99.       end
  100.       holo = {}
  101.       reader:init(file)
  102.       if compressed then
  103.         local x, y, z = 1, 1, 1
  104.         while true do
  105.           local a = reader:read()
  106.           if a == nil then file:close(); return true end
  107.           local len = 1
  108.           while true do
  109.             local b = reader:read()
  110.             if b == nil then
  111.               file:close()
  112.               if a == 0 then return true
  113.               else error("[ОШИБКА] Некорректный формат.") end
  114.             end
  115.             local fin = (b > 1)
  116.             if fin then b = b-2 end
  117.             len = bit32.lshift(len, 1)
  118.             len = len + b
  119.             if fin then break end
  120.           end
  121.           len = len - 1
  122.           for i=1, len do
  123.             if a ~= 0 then set(x,y,z, a) end
  124.             z = z+1
  125.             if z > HOLOW then
  126.               y = y+1
  127.               if y > HOLOH then
  128.                 x = x+1
  129.                 if x > HOLOW then file:close(); return true end
  130.                 y = 1
  131.               end
  132.               z = 1
  133.             end  
  134.           end
  135.         end
  136.       else
  137.         for x=1, HOLOW do
  138.           for y=1, HOLOH do
  139.             for z=1, HOLOW do
  140.               local a = reader:read()
  141.               if a ~= 0 and a ~= nil then
  142.                 set(x,y,z, a)
  143.               end
  144.             end
  145.           end
  146.         end
  147.       end
  148.       file:close()
  149.       return true
  150.     else
  151.       error("[ОШИБКА] Невозможно открыть файл "..filename.."!")
  152.     end
  153.   else
  154.       error("[ОШИБКА] Файл "..filename.." не найден!")
  155.   end
  156. end
  157.  
  158. function scaleHologram(scale)
  159.   if scale == nil or scale<0.33 or scale>4 then
  160.     error("[ОШИБКА] Масштаб голограммы должен быть числом от 0.33 до 4.00")
  161.   end
  162.   proj_scale = scale
  163. end
  164.  
  165. function drawHologram()
  166.   -- check hologram projector availability
  167.   h = trytofind('hologram')
  168.   if h ~= nil then
  169.     local depth = h.maxDepth()
  170.     -- clear projector
  171.     h.clear()
  172.     -- set projector scale
  173.     h.setScale(proj_scale)
  174.     -- send palette
  175.     if depth == 2 then
  176.       for i=1, 3 do
  177.         h.setPaletteColor(i, hexcolortable[i])
  178.       end
  179.     else
  180.       h.setPaletteColor(1, hexcolortable[1])
  181.     end
  182.     -- send voxel array
  183.     for x=1, HOLOW do
  184.       for y=1, HOLOH do
  185.         for z=1, HOLOW do
  186.           n = get(x,y,z)
  187.           if n ~= 0 then
  188.             if depth == 2 then
  189.               h.set(x,y,z,n)
  190.             else
  191.               h.set(x,y,z,1)
  192.             end
  193.           end
  194.         end
  195.       end      
  196.     end
  197.     print("Готово. Голограмма выведена на проектор.")
  198.   else
  199.     error("[ОШИБКА] Не найден проектор.")
  200.   end
  201. end
  202. -- =============================================================================================== --
  203.  
  204. -- Main part
  205. loadHologram(args[1])
  206.  
  207. if args[2] ~= nil then
  208.   scaleHologram(tonumber(args[2]))
  209. end
  210.  
  211. drawHologram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement