Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/lua
- function ShellCommand( cmd )
- local tmpname= ".tmp"
- local str= nil
- local rc= os.execute( cmd.." >"..tmpname )
- if rc==0 then
- local f= io.open( tmpname, OPEN_RT )
- --ASSUME(f)
- str= f:read("*a") -- read all at once
- f:close()
- end
- os.remove(tmpname)
- if str then
- if string.sub( str, -1 ) == '\n' then
- str= string.sub( str, 1, -2 )
- end
- end
- return str
- end
- term = ShellCommand("echo $TERM")
- pos256 = string.find(term, "256", nil, true)
- if pos256 ~= nil then
- print(string.sub(term, 1, pos256-1)
- .."\027[32m"..string.sub(term, pos256, pos256+2).."\027[0m"
- ..string.sub(term, pos256+3))
- else print(term)
- end
- CharRatio = 1.7
- p3d = {}
- p3d.__index = p3d
- function p3d.create (x, y, z)
- local point = {}
- setmetatable(point, p3d)
- point.x = x
- point.y = y
- point.z = z
- return point
- end
- function p3d:rotateX (angle)
- local rad = angle * math.pi / 180
- local cosa = math.cos(rad)
- local sina = math.sin(rad)
- local y = self.y * cosa - self.z * sina
- local z = self.y * sina + self.z * cosa
- return p3d.create(self.x, y, z)
- end
- function p3d:rotateY (angle)
- local rad = angle * math.pi / 180
- local cosa = math.cos(rad)
- local sina = math.sin(rad)
- local z = self.z * cosa - self.x * sina
- local x = self.z * sina + self.x * cosa
- return p3d.create(x, self.y, z)
- end
- function p3d:rotateZ (angle)
- local rad = angle * math.pi / 180
- local cosa = math.cos(rad)
- local sina = math.sin(rad)
- local x = self.x * cosa - self.y * sina
- local y = self.x * sina + self.y * cosa
- return p3d.create(x, y, self.z)
- end
- function p3d:project (win_width, win_height, fov, viewer_distance)
- local factor = fov / (viewer_distance + self.z)
- local x = self.x * factor + win_width / 2
- local y = -self.y * factor + win_height / 2
- return pxl.create(x, y)
- end
- pxl = {}
- pxl.__index = pxl
- function pxl.create(x, y)
- pixel = {}
- setmetatable(pixel, pxl)
- pixel.x = x
- pixel.y = y
- return pixel
- end
- function pxl:out(text)
- if self.x > 0 and self.x < 90 and
- self.y > 0 and self.y < 60 then
- local x = math.floor(self.x)
- local y = math.floor(self.y)
- if text ~= nil then
- print("\027[" .. y .. ";" .. x .. "H"..text)
- else
- print("\027[" .. y .. ";" .. x .. "H ")
- end
- end
- end
- function pxl.outline(inp1, inp2, text, ext, degr)
- if text == nil then text = "." end
- if ext == nil then ext = false end
- local p1 = pxl.create(math.floor(inp1.x * CharRatio), math.floor(inp1.y))
- local p2 = pxl.create(math.floor(inp2.x * CharRatio), math.floor(inp2.y))
- inp1, inp2 = p1, p2
- if p2.x < p1.x then p1, p2 = p2, p1 end
- local incx
- if (p2.y > p1.y and p2.y - p1.y > p2.x - p1.x)
- or (p2.y < p1.y and -p2.y + p1.y > p2.x - p1.x) then incx = false
- else incx = true end
- if incx then
- local slope = (p2.y - p1.y) / (p2.x - p1.x)
- local degrslope
- if degr ~= nil then
- degrslope = (degr[2] - degr[1]) / (inp2.x - inp1.x)
- end
- for x = p1.x, p2.x do
- local y = p1.y + slope * (x - p1.x)
- local int_y, fra_y = math.modf(y)
- local p = pxl.create(x, int_y)
- if (not ext and fra_y >= .5)
- or (ext and fra_y < .5) then
- p.y = p.y + 1
- end
- if degr ~= nil then
- p:out("\027[48;5;"..degr[1]+math.floor((x-inp1.x)*degrslope).."m"..text)
- else
- p:out(text)
- end
- end
- else
- --sort p1 and p2 again so p1 has lowest y (highest point on-screen)
- if p2.y < p1.y then p1, p2 = p2, p1 end
- local slope = (p2.x - p1.x) / (p2.y - p1.y)
- local degrslope
- if degr ~= nil then
- degrslope = (degr[2] - degr[1]) / (inp2.y - inp1.y)
- end
- for y = p1.y, p2.y do
- local x = p1.x + slope * (y - p1.y)
- local int_x, fra_x = math.modf(x)
- local p = pxl.create(int_x, y)
- if (not ext and fra_x >= .5)
- or (ext and fra_x < .5) then
- p.x = p.x + 1
- end
- if degr ~= nil then
- p:out("\027[48;5;"..degr[1]+math.floor((y-inp1.y)*degrslope).."m"..text)
- else
- p:out(text)
- end
- end
- end
- end
- local socket = require("socket")
- socket.sleep(1)
- vs = {
- { 1, 1, 1}, { 1, 1, -1},
- {-1, 1, 1}, {-1, 1, -1},
- { 1, -1, 1}, { 1, -1, -1},
- {-1, -1, 1}, {-1, -1, -1}
- }
- for vc = 1, #vs do
- v = vs[vc]
- vs[vc] = p3d.create(v[1], v[2], v[3])
- end
- bs = {
- {1, 2}, {3, 4}, {5, 6}, {7, 8},
- {1, 3}, {1, 5}, {3, 7}, {5, 7},
- {2, 4}, {2, 6}, {4, 8}, {6, 8}
- }
- for bc = 1, #bs do
- b = bs[bc]
- bs[bc] = {
- vs[b[1]],
- vs[b[2]]
- }
- end
- angles = {}
- angles.x = 1
- angles.y = 1
- angles.z = 1
- i = 1
- function frame()
- os.execute("clear")
- local ps = {}
- local rs = {}
- for vc = 1, #vs do
- v = vs[vc]
- angles.x = angles.x + .2
- angles.y = angles.y + .2
- angles.z = angles.z + .2
- r = v:rotateX(angles.x):rotateY(angles.y):rotateZ(angles.z)
- rs[v] = r --associate each vector reference with the currently rotated p3d
- p = r:project(50, 45, 30, 3)
- ps[v] = p --associate each vector reference with a 1:1 pxl
- end
- print("\027[48;5;247m")
- for bc = 1, #bs do
- b = bs[bc]
- pxl.outline(ps[b[1]], ps[b[2]], " ", false, {
- math.floor((-rs[b[1]].z*6)+244-i),
- math.floor((-rs[b[2]].z*6)+244-i)
- })
- end
- print("\027[48;5;240m")
- for bc = 1, #bs do
- b = bs[bc]
- pxl.outline(ps[b[1]], ps[b[2]], " ", true, {
- math.floor((-rs[b[1]].z*3)+238-i),
- math.floor((-rs[b[2]].z*3)+238-i)
- })
- end
- print("\027[0m")
- socket.sleep(.06)
- i = i + 1
- if i == 232 then
- i = 1
- end
- end
- while true do
- local status, err = pcall(frame)
- if not status then
- print(err)
- error()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment