MoonlightOwl

Hologram Viewer 0.7.0 (English)

Feb 2nd, 2017
360
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --      Hologram Viewer v0.70
  2. -- by NEO, Totoro (aka MoonlightOwl)
  3. -- 11/12/2014, all right reserved =)
  4. --        computercraft.ru
  5.  
  6. local fs = require('filesystem')
  7. local com = require('component')
  8. local args = { ... }
  9.  
  10. -- ================================ H O L O G R A M S   S T U F F ================================ --
  11. -- loading add. components
  12. function trytofind(name)
  13.   if com.isAvailable(name) then
  14.     return com.getPrimary(name)
  15.   else
  16.     return nil
  17.   end
  18. end
  19.  
  20. -- constants
  21. HOLOH = 32
  22. HOLOW = 48
  23.  
  24. -- hologram vars
  25. holo = {}
  26. colortable = {{},{},{}}
  27. hexcolortable = {}
  28. proj_scale = 1.0
  29.  
  30. function set(x, y, z, value)
  31.   if holo[x] == nil then holo[x] = {} end
  32.   if holo[x][y] == nil then holo[x][y] = {} end
  33.   holo[x][y][z] = value
  34. end
  35. function get(x, y, z)
  36.   if holo[x] ~= nil and holo[x][y] ~= nil and holo[x][y][z] ~= nil then
  37.     return holo[x][y][z]
  38.   else
  39.     return 0
  40.   end
  41. end
  42. function rgb2hex(r,g,b)
  43.   return r*65536+g*256+b
  44. end
  45.  
  46.  
  47. local reader = {}
  48. function reader:init(file)
  49.   self.buffer = {}
  50.   self.file = file
  51. end
  52. function reader:read()
  53.   if #self.buffer == 0 then
  54.     if not self:fetch() then return nil end
  55.   end
  56.   local sym = self.buffer[#self.buffer]
  57.   self.buffer[#self.buffer] = nil
  58.   return sym
  59. end
  60. function reader:fetch()
  61.   self.buffer = {}
  62.   local char = file:read(1)
  63.   if char == nil then return false
  64.   else
  65.     local byte = string.byte(char)
  66.     for i=0, 3 do
  67.       local a = byte % 4
  68.       byte = math.floor(byte / 4)
  69.       self.buffer[4-i] = a
  70.     end
  71.     return true
  72.   end
  73. end
  74.  
  75. local function loadHologram(filename)
  76.   if filename == nil then
  77.     error("[ERROR] You must give some filename to show. Like: show myfile.3dx")
  78.   end
  79.  
  80.   local compressed
  81.   if string.sub(filename, -4) == '.3dx' then
  82.     compressed = true
  83.   elseif string.sub(filename, -3) == '.3d' then
  84.     compressed = false
  85.   else
  86.     error("[ERROR] Don't forget the filename extension! It's important to determine the compression type.")
  87.   end
  88.  
  89.   if fs.exists(filename) then
  90.     file = io.open(filename, 'rb')
  91.     if file ~= nil then
  92.       for i=1, 3 do
  93.         for c=1, 3 do
  94.           colortable[i][c] = string.byte(file:read(1))
  95.         end
  96.         hexcolortable[i] = rgb2hex(colortable[i][1], colortable[i][2], colortable[i][3])
  97.       end
  98.       holo = {}
  99.       reader:init(file)
  100.       if compressed then
  101.         local x, y, z = 1, 1, 1
  102.         while true do
  103.           local a = reader:read()
  104.           if a == nil then file:close(); return true end
  105.           local len = 1
  106.           while true do
  107.             local b = reader:read()
  108.             if b == nil then
  109.               file:close()
  110.               if a == 0 then return true
  111.               else error("[ERROR] Invalid file format. You can try the different format extension (3d/d3x).") end
  112.             end
  113.             local fin = (b > 1)
  114.             if fin then b = b-2 end
  115.             len = bit32.lshift(len, 1)
  116.             len = len + b
  117.             if fin then break end
  118.           end
  119.           len = len - 1
  120.           for i=1, len do
  121.             if a ~= 0 then set(x,y,z, a) end
  122.             z = z+1
  123.             if z > HOLOW then
  124.               y = y+1
  125.               if y > HOLOH then
  126.                 x = x+1
  127.                 if x > HOLOW then file:close(); return true end
  128.                 y = 1
  129.               end
  130.               z = 1
  131.             end  
  132.           end
  133.         end
  134.       else
  135.         for x=1, HOLOW do
  136.           for y=1, HOLOH do
  137.             for z=1, HOLOW do
  138.               local a = reader:read()
  139.               if a ~= 0 and a ~= nil then
  140.                 set(x,y,z, a)
  141.               end
  142.             end
  143.           end
  144.         end
  145.       end
  146.       file:close()
  147.       return true
  148.     else
  149.       error("[ERROR] Cannot open the file: "..filename.."!")
  150.     end
  151.   else
  152.       error("[ERROR] The file "..filename.." is not found!")
  153.   end
  154. end
  155.  
  156. function scaleHologram(scale)
  157.   if scale == nil or scale < 0.33 or scale > 4 then
  158.     error("[ERROR] Hologram scale parameter must be a number between 0.33 and 4.00")
  159.   end
  160.   proj_scale = scale
  161. end
  162.  
  163. function drawHologram()
  164.   -- check hologram projector availability
  165.   h = trytofind('hologram')
  166.   if h ~= nil then
  167.     local depth = h.maxDepth()
  168.     -- clear projector
  169.     h.clear()
  170.     -- set projector scale
  171.     h.setScale(proj_scale)
  172.     -- send palette
  173.     if depth == 2 then
  174.       for i=1, 3 do
  175.         h.setPaletteColor(i, hexcolortable[i])
  176.       end
  177.     else
  178.       h.setPaletteColor(1, hexcolortable[1])
  179.     end
  180.     -- send voxel array
  181.     for x=1, HOLOW do
  182.       for y=1, HOLOH do
  183.         for z=1, HOLOW do
  184.           n = get(x,y,z)
  185.           if n ~= 0 then
  186.             if depth == 2 then
  187.               h.set(x,y,z,n)
  188.             else
  189.               h.set(x,y,z,1)
  190.             end
  191.           end
  192.         end
  193.       end      
  194.     end
  195.     print("Done. The hologram was rendered to projector.")
  196.   else
  197.     error("[ERROR] No hologram projector found.")
  198.   end
  199. end
  200. -- =============================================================================================== --
  201.  
  202. -- Main part
  203. loadHologram(args[1])
  204.  
  205. if args[2] ~= nil then
  206.   scaleHologram(tonumber(args[2]))
  207. end
  208.  
  209. drawHologram()
RAW Paste Data