jgtrosh

prttty.lua

Dec 29th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. #!/usr/bin/lua
  2.  
  3. function ShellCommand( cmd )
  4.     local tmpname= ".tmp"
  5.     local str= nil
  6.     local rc= os.execute( cmd.." >"..tmpname )
  7.     if rc==0 then
  8.         local f= io.open( tmpname, OPEN_RT )
  9.         --ASSUME(f)
  10.         str= f:read("*a")   -- read all at once
  11.         f:close()
  12.     end
  13.     os.remove(tmpname)
  14.     if str then
  15.         if string.sub( str, -1 ) == '\n' then
  16.             str= string.sub( str, 1, -2 )
  17.         end
  18.     end
  19.     return str
  20. end
  21.  
  22. term = ShellCommand("echo $TERM")
  23. pos256 = string.find(term, "256", nil, true)
  24. if pos256 ~= nil then
  25.     print(string.sub(term, 1, pos256-1)
  26.     .."\027[32m"..string.sub(term, pos256, pos256+2).."\027[0m"
  27.     ..string.sub(term, pos256+3))
  28. else print(term)
  29. end
  30.  
  31. CharRatio = 1.7
  32.  
  33. p3d = {}
  34. p3d.__index = p3d
  35.  
  36. function p3d.create (x, y, z)
  37.     local point = {}
  38.     setmetatable(point, p3d)
  39.     point.x = x
  40.     point.y = y
  41.     point.z = z
  42.     return point
  43. end
  44.  
  45. function p3d:rotateX (angle)
  46.     local rad = angle * math.pi / 180
  47.     local cosa = math.cos(rad)
  48.     local sina = math.sin(rad)
  49.     local y = self.y * cosa - self.z * sina
  50.     local z = self.y * sina + self.z * cosa
  51.     return p3d.create(self.x, y, z)
  52. end
  53.  
  54. function p3d:rotateY (angle)
  55.     local rad = angle * math.pi / 180
  56.     local cosa = math.cos(rad)
  57.     local sina = math.sin(rad)
  58.     local z = self.z * cosa - self.x * sina
  59.     local x = self.z * sina + self.x * cosa
  60.     return p3d.create(x, self.y, z)
  61. end
  62.  
  63. function p3d:rotateZ (angle)
  64.     local rad = angle * math.pi / 180
  65.     local cosa = math.cos(rad)
  66.     local sina = math.sin(rad)
  67.     local x = self.x * cosa - self.y * sina
  68.     local y = self.x * sina + self.y * cosa
  69.     return p3d.create(x, y, self.z)
  70. end
  71.  
  72. function p3d:project (win_width, win_height, fov, viewer_distance)
  73.     local factor = fov / (viewer_distance + self.z)
  74.     local x = self.x * factor + win_width / 2
  75.     local y = -self.y * factor + win_height / 2
  76.     return pxl.create(x, y)
  77. end
  78.  
  79. pxl = {}
  80. pxl.__index = pxl
  81.  
  82. function pxl.create(x, y)
  83.     pixel = {}
  84.     setmetatable(pixel, pxl)
  85.     pixel.x = x
  86.     pixel.y = y
  87.     return pixel
  88. end
  89.  
  90. function pxl:out(text)
  91.     if  self.x > 0 and self.x < 90 and
  92.         self.y > 0 and self.y < 60 then
  93.         local x = math.floor(self.x)
  94.         local y = math.floor(self.y)
  95.         if text ~= nil then
  96.             print("\027[" .. y .. ";" .. x .. "H"..text)
  97.         else
  98.             print("\027[" .. y .. ";" .. x .. "H ")
  99.         end
  100.     end
  101. end
  102.  
  103. function pxl.outline(inp1, inp2, text, ext, degr)
  104.     if text == nil then text = "." end
  105.     if ext == nil then ext = false end
  106.     local p1 = pxl.create(math.floor(inp1.x * CharRatio), math.floor(inp1.y))
  107.     local p2 = pxl.create(math.floor(inp2.x * CharRatio), math.floor(inp2.y))
  108.     inp1, inp2 = p1, p2
  109.     if p2.x < p1.x then p1, p2 = p2, p1 end
  110.     local incx
  111.     if (p2.y > p1.y and  p2.y - p1.y > p2.x - p1.x)
  112.     or (p2.y < p1.y and -p2.y + p1.y > p2.x - p1.x) then incx = false
  113.     else incx = true end
  114.     if incx then
  115.         local slope = (p2.y - p1.y) / (p2.x - p1.x)
  116.         local degrslope
  117.         if degr ~= nil then
  118.             degrslope = (degr[2] - degr[1]) / (inp2.x - inp1.x)
  119.         end
  120.         for x = p1.x, p2.x do
  121.             local y = p1.y + slope * (x - p1.x)
  122.             local int_y, fra_y = math.modf(y)
  123.             local p = pxl.create(x, int_y)
  124.             if (not ext and fra_y >= .5)
  125.                 or (ext and fra_y < .5) then
  126.                 p.y = p.y + 1
  127.             end
  128.             if degr ~= nil then
  129.                 p:out("\027[48;5;"..degr[1]+math.floor((x-inp1.x)*degrslope).."m"..text)
  130.             else
  131.                 p:out(text)
  132.             end
  133.         end
  134.     else
  135.         --sort p1 and p2 again so p1 has lowest y (highest point on-screen)
  136.         if p2.y < p1.y then p1, p2 = p2, p1 end
  137.         local slope = (p2.x - p1.x) / (p2.y - p1.y)
  138.         local degrslope
  139.         if degr ~= nil then
  140.             degrslope = (degr[2] - degr[1]) / (inp2.y - inp1.y)
  141.         end
  142.         for y = p1.y, p2.y do
  143.             local x = p1.x + slope * (y - p1.y)
  144.             local int_x, fra_x = math.modf(x)
  145.             local p = pxl.create(int_x, y)
  146.             if (not ext and fra_x >= .5)
  147.                 or (ext and fra_x < .5) then
  148.                 p.x = p.x + 1
  149.             end
  150.             if degr ~= nil then
  151.                 p:out("\027[48;5;"..degr[1]+math.floor((y-inp1.y)*degrslope).."m"..text)
  152.             else
  153.                 p:out(text)
  154.             end
  155.         end
  156.     end
  157. end
  158.  
  159. local socket = require("socket")
  160. socket.sleep(1)
  161.  
  162. vs = {
  163.     { 1,  1, 1}, { 1,  1, -1},
  164.     {-1,  1, 1}, {-1,  1, -1},
  165.     { 1, -1, 1}, { 1, -1, -1},
  166.     {-1, -1, 1}, {-1, -1, -1}
  167. }
  168.  
  169. for vc = 1, #vs do
  170.     v = vs[vc]
  171.     vs[vc] = p3d.create(v[1], v[2], v[3])
  172. end
  173.  
  174. bs = {
  175.     {1, 2}, {3, 4}, {5, 6}, {7, 8},
  176.     {1, 3}, {1, 5}, {3, 7}, {5, 7},
  177.     {2, 4}, {2, 6}, {4, 8}, {6, 8}
  178. }
  179.  
  180. for bc = 1, #bs do
  181.     b = bs[bc]
  182.     bs[bc] = {
  183.         vs[b[1]],
  184.         vs[b[2]]
  185.     }
  186. end
  187.  
  188. angles = {}
  189. angles.x = 1
  190. angles.y = 1
  191. angles.z = 1
  192.  
  193. i = 1
  194. function frame()
  195.     os.execute("clear")
  196.     local ps = {}
  197.     local rs = {}
  198.     for vc = 1, #vs do
  199.         v = vs[vc]
  200.         angles.x = angles.x + .2
  201.         angles.y = angles.y + .2
  202.         angles.z = angles.z + .2
  203.         r = v:rotateX(angles.x):rotateY(angles.y):rotateZ(angles.z)
  204.         rs[v] = r --associate each vector reference with the currently rotated p3d
  205.         p = r:project(50, 45, 30, 3)
  206.         ps[v] = p --associate each vector reference with a 1:1 pxl
  207.     end
  208.     print("\027[48;5;247m")
  209.     for bc = 1, #bs do
  210.         b = bs[bc]
  211.         pxl.outline(ps[b[1]], ps[b[2]], " ", false, {
  212.             math.floor((-rs[b[1]].z*6)+244-i),
  213.             math.floor((-rs[b[2]].z*6)+244-i)
  214.         })
  215.     end
  216.     print("\027[48;5;240m")
  217.     for bc = 1, #bs do
  218.         b = bs[bc]
  219.         pxl.outline(ps[b[1]], ps[b[2]], " ", true, {
  220.             math.floor((-rs[b[1]].z*3)+238-i),
  221.             math.floor((-rs[b[2]].z*3)+238-i)
  222.         })
  223.     end
  224.     print("\027[0m")
  225.     socket.sleep(.06)
  226.     i = i + 1
  227.     if i == 232 then
  228.         i = 1
  229.     end
  230. end
  231.  
  232. while true do
  233.     local status, err = pcall(frame)
  234.     if not status then
  235.         print(err)
  236.         error()
  237.     end
  238. end
Advertisement
Add Comment
Please, Sign In to add comment