Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local wrapp = peripheral.wrap("front")
- function getbuf(s)
- local buf = {}
- for i=1,s do
- buf[i] = 0
- end
- return buf
- end
- function toDegree(rad)
- return (180 / math.pi) * rad
- end
- function toRadian(deg)
- return (math.pi / 180) * deg
- end
- function int(doub)
- return math.floor(doub)
- end
- local w,h = wrapp.getSize()
- local wl = w-1
- local hl = h-1
- local pixels = getbuf(w*h)
- local zBuffer = getbuf(w*h)
- local zBufferWall = getbuf(w)
- local c0 = colors.black
- local c1 = colors.gray
- local c2 = colors.lightGray
- local c3 = colors.white
- local texture = {
- c0, c0, c0, c1, c1, c1, c1, c3,
- c0, c0, c0, c1, c1, c1, c1, c3,
- c0, c0, c0, c1, c1, c1, c1, c3,
- c1, c1, c1, c2, c2, c2, c2, c3,
- c1, c1, c1, c2, c2, c2, c2, c3,
- c1, c1, c1, c2, c2, c2, c2, c3,
- c1, c1, c1, c2, c2, c2, c2, c3,
- c3, c3, c3, c3, c3, c3, c3, c3}
- local c10 = colors.lightBlue
- local c11 = colors.cyan
- local wallTexture = {
- c10, c10, c11, c11, c10, c10, c11, c11,
- c10, c10, c11, c11, c10, c10, c11, c11,
- c11, c11, c10, c10, c11, c11, c10, c10,
- c11, c11, c10, c10, c11, c11, c10, c10,
- c10, c10, c11, c11, c10, c10, c11, c11,
- c10, c10, c11, c11, c10, c10, c11, c11,
- c11, c11, c10, c10, c11, c11, c10, c10,
- c11, c11, c10, c10, c11, c11, c10, c10
- }
- local floorPos = 8
- local ceilPos = 8
- local rotation = 0
- local xPos = 0
- local zPos = 0
- local squareSize = 6
- local resetDot = 12
- function resetPosition()
- xPos = resetDot
- zPos = resetDot
- end
- local level = {
- 0, 0, 0, 1, 1, 1, 0, 0,
- 0, 0, 0, 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0, 1, 0, 0,
- 0, 0, 1, 1, 1, 1, 0, 0
- }
- local levelW = 8
- local levelH = 8
- function canMove(x,y)
- return level[math.floor(x/8)+math.floor(y/8)*levelW+1] == 0
- end
- function canMoveSquare(x,y)
- local minX = canMove(x-squareSize,y)
- local minZ = canMove(x,y-squareSize)
- local maxX = canMove(x+squareSize,y)
- local maxZ = canMove(x,y+squareSize)
- return minX and minZ and maxX and maxZ
- end
- function floor()
- wrapp.setTextScale(0.5)
- for x=1,w do
- zBufferWall[x] = 0
- end
- local cosine = math.cos(toRadian(rotation))
- local sine = math.sin(toRadian(rotation))
- for y=0,hl do
- local ceiling = (y - h / 2.0) / h
- local z = (floorPos) / ceiling
- if (ceiling < 0) then
- z = (ceilPos) / -ceiling
- end
- for x=0,wl do
- local depth = (x - w / 2.0) / h
- depth = depth * z
- local xx = depth * cosine + z * sine
- local yy = z * cosine - depth * sine
- local xPix = math.floor(xx + xPos)
- local yPix = math.floor(yy + zPos)
- -- wrapp.setCursorPos(x+1,y+1)
- local bitX = bit.band(xPix, 7)
- local bitY = bit.band(yPix, 7)
- zBuffer[x + y * w+1] = z
- local tind = (bitX) + (bitY) * 8
- local col = texture[tind+1]
- plot(x+1,y+1,col)
- -- print("BitX: "..bitX.." BitY: "..bitY.." X: "..x.." Y: "..y.." xPix: "..xPix.." yPix: "..yPix.." Tind: "..tind)
- -- print(xPix.." "..yPix.." "..x.." "..y.." "..tind.." ")
- -- wrapp.setBackgroundColor(col)
- -- wrapp.write(" ")
- end
- end
- local size=w
- for xBlock=-size,size do
- for zBlock=-size,size do
- local block = lvlCreate(xBlock, zBlock)
- local east = lvlCreate(xBlock + 1, zBlock)
- local south = lvlCreate(xBlock, zBlock + 1)
- if block==1 then
- if east==0 then
- drawWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1, 0)
- drawWall(xBlock + 1, xBlock + 1, zBlock, zBlock + 1, 0.5)
- end
- if south==0 then
- drawWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1, 0)
- drawWall(xBlock + 1, xBlock, zBlock + 1, zBlock + 1, 0.5)
- end
- else
- if east==1 then
- drawWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock, 0)
- drawWall(xBlock + 1, xBlock + 1, zBlock + 1, zBlock, 0.5)
- end
- if south==1 then
- drawWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1, 0)
- drawWall(xBlock, xBlock + 1, zBlock + 1, zBlock + 1, 0.5)
- end
- end
- end
- end
- end
- function lvlCreate(x,z)
- if x < 0 or z < 0 or x >= levelW or z >= levelH then
- return 1
- end
- return level[x+z*levelW+1]
- end
- function drawWall(xLeft, xRight, zDistanceLeft, zDistanceRight, yHeight)
- local forward = zPos
- local right = xPos
- local cosine = math.cos(toRadian(rotation))
- local sine = math.sin(toRadian(rotation))
- local walking = 0
- local up = 0
- local walkCorrect = -0.062
- local upCorrect = 0.062
- local rightCorrect = 0.062
- local forwardCorrect = 0.062
- local xcLeft = ((xLeft / 2) - (right * rightCorrect)) * 2
- local zcLeft = ((zDistanceLeft / 2) - (forward * forwardCorrect)) * 2
- local rotLeftSideX = xcLeft * cosine - zcLeft * sine
- local yCornerTL = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
- local yCornerBL = ((0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
- local rotLeftSideZ = zcLeft * cosine + xcLeft * sine
- local xcRight = ((xRight / 2) - (right * rightCorrect)) * 2
- local zcRight = ((zDistanceRight / 2) - (forward * forwardCorrect)) * 2
- local rotRightSideX = xcRight * cosine - zcRight * sine
- local yCornerTR = ((-yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
- local yCornerBR = ((0.5 - yHeight) - (-up * upCorrect + (walking * walkCorrect))) * 2
- local rotRightSideZ = zcRight * cosine + xcRight * sine
- local tex30 = 0
- local tex40 = 8
- local clip = 0.5
- if rotLeftSideZ < clip and rotRightSideZ < clip then
- return
- end
- if rotLeftSideZ < clip then
- local clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ)
- rotLeftSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0
- rotLeftSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0
- tex30 = tex30 + (tex40 - tex30) * clip0
- end
- if rotRightSideZ < clip then
- local clip0 = (clip - rotLeftSideZ) / (rotRightSideZ - rotLeftSideZ)
- rotRigthSideZ = rotLeftSideZ + (rotRightSideZ - rotLeftSideZ) * clip0
- rotRightSideX = rotLeftSideX + (rotRightSideX - rotLeftSideX) * clip0
- tex40 = tex30 + (tex40 - tex30) * clip0
- end
- local xPixelLeft = (rotLeftSideX / rotLeftSideZ * h + w / 2)
- local xPixelRight = (rotRightSideX / rotRightSideZ * h + w / 2)
- if xPixelLeft >= xPixelRight then
- return
- end
- local xPixelLeftInt = int(xPixelLeft)
- local xPixelRightInt = int(xPixelRight)
- if xPixelLeftInt < 0 then
- xPixelLeftInt = 0
- end
- if xPixelRightInt > w then
- xPixelRightInt = w
- end
- local yPixelLeftTop = int(yCornerTL / rotLeftSideZ * h + h / 2)
- local yPixelLeftBottom = int(yCornerBL / rotLeftSideZ * h + h / 2)
- local yPixelRightTop = int(yCornerTR / rotRightSideZ * h + h / 2)
- local yPixelRightBottom = int(yCornerBR / rotRightSideZ * h + h / 2)
- local tex1 = 1 / rotLeftSideZ
- local tex2 = 1 / rotRightSideZ
- local tex3 = tex30 / rotLeftSideZ
- local tex4 = tex40 / rotRightSideZ - tex3
- for x=xPixelLeftInt,xPixelRightInt-1 do
- local pixelRotation = (x - xPixelLeft) / (xPixelRight - xPixelLeft)
- local zWall = (tex1 + (tex2 - tex1) * pixelRotation)
- local xTexture = int((tex3 + tex4 * pixelRotation) / zWall)
- if zBufferWall[x+1] <= zWall then
- zBufferWall[x+1] = zWall
- local yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation
- local yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation
- local yPixelTopInt = int(yPixelTop)
- local yPixelBottomInt = int(yPixelBottom)
- if yPixelTopInt < 0 then
- yPixelTopInt = 0
- end
- if yPixelBottomInt > h then
- yPixelBottomInt = h
- end
- for y=yPixelTopInt,yPixelBottomInt-1 do
- local pixelRotationY = (y - yPixelTop) / (yPixelBottom - yPixelTop)
- local yTexture = int(8 * pixelRotationY)
- plot(x+1,y+1,wallTexture[bit.band(xTexture, 7) + bit.band(yTexture, 7) * 8 + 1])
- zBuffer[x + y * w+1] = 1 / (tex1 + (tex2 - tex1) * pixelRotation) * 8
- end
- end
- end
- end
- function plotrend(x,y,col)
- wrapp.setCursorPos(x,y)
- wrapp.setBackgroundColor(col)
- wrapp.write(" ")
- end
- function plot(x,y,col)
- pixels[(x-1)+(y-1)*w+1] = col
- end
- function finalPlot()
- for y=0,hl do
- for x=0,wl do
- local col = pixels[x+y*w+1]
- plotrend(x+1,y+1,col)
- end
- end
- end
- function draw()
- floor()
- end
- local moveMore = 1
- function input()
- local inp = redstone.getBundledInput("back")
- local back = colors.test(inp, colors.black)
- local forward = colors.test(inp, colors.white)
- local left = colors.test(inp, colors.lime)
- local right = colors.test(inp, colors.green)
- local cosine = math.cos(toRadian(rotation))
- local sine = math.sin(toRadian(rotation))
- local xMove = 0
- local zMove = 0
- local rot = 0
- if back then
- zMove = zMove - moveMore
- end
- if forward then
- zMove = zMove + moveMore
- end
- if left then
- rot = rot - 5
- end
- if right then
- rot = rot + 5
- end
- local doSleep = (xMove == 0 and zMove == 0 and rot == 0)
- if (doSleep) then
- sleep(0.7)
- moveMore = 1
- else
- moveMore = moveMore * 1.05
- sleep(0.1)
- end
- local xChange = xMove * cosine + zMove * sine
- local zChange = zMove * cosine - xMove * sine
- rotation = rotation + rot
- if not canMoveSquare(xPos,zPos) then
- resetPosition()
- end
- if canMoveSquare(xPos + xChange,zPos) then
- xPos = xPos + xChange
- end
- if canMoveSquare(xPos,zPos + zChange) then
- zPos = zPos + zChange
- end
- end
- function handleRednet()
- local col = redstone.getBundledInput("left")
- if (1) then
- xPos = 0
- zPos = 0
- rotation = 0
- elseif (2) then
- xPos = 0
- zPos = 0
- elseif (3) then
- rotation = 0
- end
- sleep(0.1)
- end
- while true do
- draw()
- finalPlot()
- input()
- end
Advertisement
Add Comment
Please, Sign In to add comment