Qwe98qwE

MineFront for ComputerCraft

Dec 15th, 2015
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.39 KB | None | 0 0
  1. local wrapp = peripheral.wrap("front")
  2.  
  3. function getbuf(s)
  4.   local buf = {}
  5.   for i=1,s do
  6.     buf[i] = 0
  7.   end
  8.   return buf
  9. end
  10.  
  11. function toDegree(rad)
  12.  
  13.   return (180 / math.pi) * rad
  14. end
  15.  
  16. function toRadian(deg)
  17.  
  18.   return (math.pi / 180) * deg
  19. end
  20.  
  21. function int(doub)
  22.   return math.floor(doub)
  23. end
  24.  
  25. local w,h = wrapp.getSize()
  26. local wl = w-1
  27. local hl = h-1  
  28.   local pixels = getbuf(w*h)
  29.  
  30.   local zBuffer = getbuf(w*h)
  31.   local zBufferWall = getbuf(w)
  32.  
  33.   local c0 = colors.black
  34.   local c1 = colors.gray
  35.   local c2 = colors.lightGray
  36.   local c3 = colors.white
  37.  
  38.   local texture = {
  39.   c0, c0, c0, c1, c1, c1, c1, c3,
  40.   c0, c0, c0, c1, c1, c1, c1, c3,
  41.   c0, c0, c0, c1, c1, c1, c1, c3,
  42.   c1, c1, c1, c2, c2, c2, c2, c3,
  43.   c1, c1, c1, c2, c2, c2, c2, c3,
  44.   c1, c1, c1, c2, c2, c2, c2, c3,
  45.   c1, c1, c1, c2, c2, c2, c2, c3,
  46.   c3, c3, c3, c3, c3, c3, c3, c3}
  47.  
  48.   local c10 = colors.lightBlue
  49.   local c11 = colors.cyan
  50.  
  51.   local wallTexture = {
  52.     c10, c10, c11, c11, c10, c10, c11, c11,
  53.     c10, c10, c11, c11, c10, c10, c11, c11,
  54.     c11, c11, c10, c10, c11, c11, c10, c10,
  55.     c11, c11, c10, c10, c11, c11, c10, c10,
  56.     c10, c10, c11, c11, c10, c10, c11, c11,
  57.     c10, c10, c11, c11, c10, c10, c11, c11,
  58.     c11, c11, c10, c10, c11, c11, c10, c10,
  59.     c11, c11, c10, c10, c11, c11, c10, c10  
  60.   }
  61.   local floorPos = 8
  62.   local ceilPos = 8
  63.  
  64.   local rotation = 0
  65.  
  66.   local xPos = 0
  67.   local zPos = 0
  68.  
  69.   local squareSize = 6
  70.   local resetDot = 12
  71.  
  72.   function resetPosition()
  73.     xPos = resetDot
  74.     zPos = resetDot
  75.   end
  76.  
  77.   local level = {
  78.     0, 0, 0, 1, 1, 1, 0, 0,
  79.     0, 0, 0, 0, 0, 1, 0, 0,
  80.     0, 0, 0, 0, 0, 1, 0, 0,
  81.     0, 0, 0, 0, 0, 0, 0, 0,
  82.     0, 0, 0, 0, 0, 0, 0, 0,
  83.     0, 0, 1, 0, 0, 0, 0, 0,
  84.     0, 0, 1, 0, 0, 1, 0, 0,
  85.     0, 0, 1, 1, 1, 1, 0, 0  
  86.   }
  87.  
  88.   local levelW = 8
  89.   local levelH = 8
  90.  
  91.   function canMove(x,y)
  92.     return level[math.floor(x/8)+math.floor(y/8)*levelW+1] == 0
  93.   end
  94.  
  95.   function canMoveSquare(x,y)
  96.     local minX = canMove(x-squareSize,y)
  97.     local minZ = canMove(x,y-squareSize)
  98.     local maxX = canMove(x+squareSize,y)
  99.     local maxZ = canMove(x,y+squareSize)
  100.     return minX and minZ and maxX and maxZ
  101.   end
  102.  
  103. function floor()
  104.   wrapp.setTextScale(0.5)
  105.  
  106.   for x=1,w do
  107.     zBufferWall[x] = 0
  108.   end
  109.  
  110.   local cosine = math.cos(toRadian(rotation))
  111.   local sine = math.sin(toRadian(rotation))
  112.  
  113.   for y=0,hl do
  114.     local ceiling = (y - h / 2.0) / h
  115.    
  116.     local z = (floorPos) / ceiling
  117.    
  118.     if (ceiling < 0) then
  119.       z = (ceilPos) / -ceiling
  120.     end
  121.    
  122.     for x=0,wl do
  123.       local depth = (x - w / 2.0) / h
  124.       depth = depth * z
  125.       local xx = depth * cosine + z * sine
  126.       local yy = z * cosine - depth * sine
  127.      
  128.       local xPix = math.floor(xx + xPos)
  129.       local yPix = math.floor(yy + zPos)
  130.      
  131. --      wrapp.setCursorPos(x+1,y+1)
  132.       local bitX = bit.band(xPix, 7)
  133.       local bitY = bit.band(yPix, 7)
  134.       zBuffer[x + y * w+1] = z
  135.       local tind = (bitX) + (bitY) * 8
  136.       local col = texture[tind+1]
  137.       plot(x+1,y+1,col)
  138. --      print("BitX: "..bitX.." BitY: "..bitY.." X: "..x.." Y: "..y.." xPix: "..xPix.." yPix: "..yPix.." Tind: "..tind)
  139. --      print(xPix.." "..yPix.." "..x.." "..y.." "..tind.." ")
  140. --      wrapp.setBackgroundColor(col)
  141. --      wrapp.write(" ")
  142.      
  143.     end
  144.   end
  145.  
  146.   local size=w
  147.  
  148.   for xBlock=-size,size do
  149.     for zBlock=-size,size do
  150.       local block = lvlCreate(xBlock, zBlock)
  151.       local east = lvlCreate(xBlock + 1, zBlock)
  152.       local south = lvlCreate(xBlock, zBlock + 1)
  153.      
  154.       if block==1 then
  155.         if east==0 then
  156.           drawWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1, 0)
  157.           drawWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1, 0.5)
  158.         end
  159.         if south==0 then
  160.           drawWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1, 0)
  161.           drawWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1, 0.5)
  162.         end
  163.       else
  164.         if east==1 then
  165.           drawWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock, 0)
  166.           drawWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock, 0.5)
  167.         end
  168.         if south==1 then
  169.           drawWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1, 0)
  170.           drawWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1, 0.5)
  171.         end
  172.       end
  173.     end
  174.   end
  175. end
  176.  
  177. function lvlCreate(x,z)
  178.   if x < 0 or z < 0 or x >= levelW or z >= levelH then
  179.     return 1
  180.   end
  181.   return level[x+z*levelW+1]
  182. end
  183.  
  184. function drawWall(xLeft, xRight, zDistanceLeft, zDistanceRight, yHeight)
  185.   local forward = zPos
  186.   local right = xPos
  187.   local cosine = math.cos(toRadian(rotation))
  188.   local sine = math.sin(toRadian(rotation))
  189.  
  190.   local walking = 0
  191.  
  192.   local up = 0
  193.   local walkCorrect = -0.062
  194.   local upCorrect = 0.062
  195.   local rightCorrect = 0.062
  196.   local forwardCorrect = 0.062
  197.  
  198.   local xcLeft = ((xLeft / 2) - (right * rightCorrect)) * 2
  199.   local zcLeft = ((zDistanceLeft / 2) - (forward * forwardCorrect)) * 2
  200.  
  201.   local rotLeftSideX = xcLeft * cosine - zcLeft * sine
  202.   local yCornerTL = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
  203.   local yCornerBL = ((0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
  204.   local rotLeftSideZ = zcLeft * cosine + xcLeft * sine
  205.  
  206.   local xcRight = ((xRight / 2) - (right * rightCorrect)) * 2
  207.   local zcRight = ((zDistanceRight / 2) - (forward * forwardCorrect)) * 2
  208.  
  209.   local rotRightSideX = xcRight * cosine - zcRight * sine
  210.   local yCornerTR = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
  211.   local yCornerBR = ((0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
  212.   local rotRightSideZ = zcRight * cosine + xcRight * sine
  213.  
  214.  
  215.   local tex30 = 0
  216.   local tex40 = 8
  217.   local clip = 0.5
  218.  
  219.   if rotLeftSideZ < clip and rotRightSideZ < clip then
  220.     return
  221.   end
  222.  
  223.   if rotLeftSideZ < clip then
  224.     local clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ)
  225.     rotLeftSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0
  226.     rotLeftSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0
  227.     tex30 = tex30 + (tex40 - tex30) * clip0
  228.   end
  229.   if rotRightSideZ < clip then
  230.     local clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ)
  231.     rotRigthSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0
  232.     rotRightSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0
  233.     tex40 = tex30 + (tex40 - tex30) * clip0
  234.   end
  235.  
  236.  
  237.   local xPixelLeft = (rotLeftSideX / rotLeftSideZ * h + w / 2)
  238.   local xPixelRight = (rotRightSideX / rotRightSideZ * h + w / 2)
  239.  
  240.   if xPixelLeft >= xPixelRight then
  241.     return
  242.   end
  243.  
  244.   local xPixelLeftInt = int(xPixelLeft)
  245.   local xPixelRightInt = int(xPixelRight)
  246.  
  247.   if xPixelLeftInt < 0 then
  248.     xPixelLeftInt = 0
  249.   end
  250.  
  251.   if xPixelRightInt > w then
  252.     xPixelRightInt = w
  253.   end
  254.  
  255.   local yPixelLeftTop = int(yCornerTL / rotLeftSideZ * h + h / 2)
  256.   local yPixelLeftBottom = int(yCornerBL / rotLeftSideZ * h + h / 2)
  257.   local yPixelRightTop = int(yCornerTR / rotRightSideZ * h + h / 2)
  258.   local yPixelRightBottom = int(yCornerBR / rotRightSideZ * h + h / 2)
  259.  
  260.   local tex1 = 1 / rotLeftSideZ
  261.   local tex2 = 1 / rotRightSideZ
  262.   local tex3 = tex30 / rotLeftSideZ
  263.   local tex4 = tex40 / rotRightSideZ - tex3
  264.  
  265.   for x=xPixelLeftInt,xPixelRightInt-1 do
  266.     local pixelRotation = (x - xPixelLeft) / (xPixelRight - xPixelLeft)
  267.     local zWall = (tex1 + (tex2 - tex1) * pixelRotation)
  268.    
  269.     local xTexture = int((tex3 + tex4 * pixelRotation) / zWall)
  270.    
  271.     if zBufferWall[x+1] <= zWall then
  272.       zBufferWall[x+1] = zWall
  273.      
  274.       local yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation
  275.       local yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation
  276.      
  277.       local yPixelTopInt = int(yPixelTop)
  278.       local yPixelBottomInt = int(yPixelBottom)
  279.      
  280.       if yPixelTopInt < 0 then
  281.         yPixelTopInt = 0
  282.       end
  283.      
  284.       if yPixelBottomInt > h then
  285.         yPixelBottomInt = h
  286.       end
  287.      
  288.       for y=yPixelTopInt,yPixelBottomInt-1 do
  289.         local pixelRotationY = (y - yPixelTop) / (yPixelBottom - yPixelTop)
  290.         local yTexture = int(8 * pixelRotationY)
  291.        
  292.         plot(x+1,y+1,wallTexture[bit.band(xTexture, 7) + bit.band(yTexture, 7) * 8 + 1])
  293.        
  294.         zBuffer[x + y * w+1] = 1 / (tex1 + (tex2 - tex1) * pixelRotation) * 8
  295.       end
  296.      
  297.      
  298.     end
  299.   end
  300. end
  301.  
  302. function plotrend(x,y,col)
  303.   wrapp.setCursorPos(x,y)
  304.   wrapp.setBackgroundColor(col)
  305.   wrapp.write(" ")
  306. end
  307.  
  308. function plot(x,y,col)
  309.   pixels[(x-1)+(y-1)*w+1] = col
  310. end
  311.  
  312. function finalPlot()
  313.   for y=0,hl do
  314.     for x=0,wl do
  315.       local col = pixels[x+y*w+1]
  316.       plotrend(x+1,y+1,col)
  317.     end
  318.   end
  319. end
  320.  
  321. function draw()
  322.   floor()
  323. end
  324.  
  325. local moveMore = 1
  326.  
  327.   function input()
  328.     local inp = redstone.getBundledInput("back")
  329.     local back = colors.test(inp, colors.black)
  330.     local forward = colors.test(inp, colors.white)
  331.     local left = colors.test(inp, colors.lime)
  332.     local right = colors.test(inp, colors.green)
  333.    
  334.     local cosine = math.cos(toRadian(rotation))
  335.     local sine = math.sin(toRadian(rotation))
  336.    
  337.     local xMove = 0
  338.     local zMove = 0
  339.    
  340.     local rot = 0
  341.    
  342.     if back then
  343.       zMove = zMove - moveMore
  344.     end
  345.     if forward then
  346.       zMove = zMove + moveMore
  347.     end
  348.     if left then
  349.       rot = rot - 5
  350.     end
  351.     if right then
  352.       rot = rot + 5
  353.     end
  354.    
  355.     local doSleep = (xMove == 0 and zMove == 0 and rot == 0)
  356.    
  357.     if (doSleep) then
  358.       sleep(0.7)
  359.       moveMore = 1
  360.     else
  361.       moveMore = moveMore * 1.05
  362.       sleep(0.1)
  363.     end
  364.    
  365.     local xChange = xMove * cosine + zMove * sine
  366.     local zChange = zMove * cosine - xMove * sine
  367.     rotation = rotation + rot
  368.     if not canMoveSquare(xPos,zPos) then
  369.       resetPosition()
  370.     end
  371.     if canMoveSquare(xPos + xChange,zPos) then
  372.       xPos = xPos + xChange
  373.     end
  374.     if canMoveSquare(xPos,zPos + zChange) then
  375.       zPos = zPos + zChange
  376.     end
  377.   end
  378.  
  379.   function handleRednet()
  380.     local col = redstone.getBundledInput("left")
  381.  
  382.     if (1) then
  383.       xPos = 0
  384.       zPos = 0
  385.       rotation = 0
  386.     elseif (2) then
  387.       xPos = 0
  388.       zPos = 0
  389.     elseif (3) then
  390.       rotation = 0
  391.     end
  392.    
  393.     sleep(0.1)
  394.   end
  395.    
  396.   while true do
  397.     draw()
  398.     finalPlot()
  399.     input()
  400.   end
Advertisement
Add Comment
Please, Sign In to add comment