Advertisement
MoonlightOwl

Hologram Viewer 0.60 (English version)

Nov 11th, 2014
1,990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. --         Hologram Viewer
  2. -- by NEO, Totoro (aka MoonlightOwl)
  3. -- 11/12/2014, all right reserved =)
  4.  
  5. local fs = require('filesystem')
  6. local com = require('component')
  7. local args = { ... }
  8.  
  9. -- ================================ H O L O G R A M S   S T U F F ================================ --
  10. -- loading add. components
  11. function trytofind(name)
  12.   if com.isAvailable(name) then
  13.     return com.getPrimary(name)
  14.   else
  15.     return nil
  16.   end
  17. end
  18.  
  19. -- constants
  20. HOLOH = 32
  21. HOLOW = 48
  22.  
  23. -- hologram vars
  24. holo = {}
  25. colortable = {{},{},{}}
  26. hexcolortable = {}
  27. proj_scale = 1.0
  28.  
  29. function set(x, y, z, value)
  30.   if holo[x] == nil then holo[x] = {} end
  31.   if holo[x][y] == nil then holo[x][y] = {} end
  32.   holo[x][y][z] = value
  33. end
  34. function get(x, y, z)
  35.   if holo[x] ~= nil and holo[x][y] ~= nil and holo[x][y][z] ~= nil then
  36.     return holo[x][y][z]
  37.   else
  38.     return 0
  39.   end
  40. end
  41. function rgb2hex(r,g,b)
  42.   return r*65536+g*256+b
  43. end
  44.  
  45. function loadHologram(filename)
  46.   if filename == nil then
  47.     error("[ERROR] Wrong file name.")
  48.     return false
  49.   end
  50.   if string.sub(filename, -3) ~= '.3d' then
  51.     filename = filename..'.3d'
  52.   end
  53.   if fs.exists(filename) then
  54.     file = io.open(filename, 'rb')
  55.     -- load palette
  56.     for i=1, 3 do
  57.       for c=1, 3 do
  58.         colortable[i][c] = string.byte(file:read(1))
  59.       end
  60.       hexcolortable[i] = rgb2hex(colortable[i][1], colortable[i][2], colortable[i][3])
  61.     end
  62.     -- load voxel array
  63.     holo = {}
  64.     for x=1, HOLOW do
  65.       for y=1, HOLOH do
  66.         for z=1, HOLOW, 4 do
  67.           byte = string.byte(file:read(1))
  68.           for i=0, 3 do
  69.             a = byte % 4
  70.             byte = math.floor(byte / 4)
  71.             if a ~= 0 then set(x,y,z+i, a) end
  72.           end
  73.         end
  74.       end
  75.     end
  76.     file:close()
  77.     print("File is successfully loaded.")
  78.     return true
  79.   else
  80.     error("[ERROR] File "..filename.." not found.")
  81.     return false
  82.   end
  83. end
  84.  
  85. function scaleHologram(scale)
  86.   if scale == nil or scale<0.33 or scale>4 then
  87.     error("[ERROR] Hologram scale value should be in [0.33, 4.00] range.")
  88.   end
  89.   proj_scale = scale
  90. end
  91.  
  92. function drawHologram()
  93.   -- check hologram projector availability
  94.   h = trytofind('hologram')
  95.   if h ~= nil then
  96.     local depth = h.maxDepth()
  97.     -- clear projector
  98.     h.clear()
  99.     -- set projector scale
  100.     h.setScale(proj_scale)
  101.     -- send palette
  102.     if depth == 2 then
  103.       for i=1, 3 do
  104.         h.setPaletteColor(i, hexcolortable[i])
  105.       end
  106.     else
  107.       h.setPaletteColor(1, hexcolortable[1])
  108.     end
  109.     -- send voxel array
  110.     for x=1, HOLOW do
  111.       for y=1, HOLOH do
  112.         for z=1, HOLOW do
  113.           n = get(x,y,z)
  114.           if n ~= 0 then
  115.             if depth == 2 then
  116.               h.set(x,y,z,n)
  117.             else
  118.               h.set(x,y,z,1)
  119.             end
  120.           end
  121.         end
  122.       end      
  123.     end
  124.     print("Done.")
  125.   else
  126.     error("[ERROR] Hologram projector not found.")
  127.   end
  128. end
  129. -- =============================================================================================== --
  130.  
  131. -- Main part
  132. loadHologram(args[1])
  133.  
  134. if args[2] ~= nil then
  135.   scaleHologram(tonumber(args[2]))
  136. end
  137.  
  138. drawHologram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement