Advertisement
Xelostar

ThreeD_API_v0.5.1_Demo1

Jul 22nd, 2018
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1.  
  2. -- Made by Xelostar: https://www.youtube.com/channel/UCDE2STpSWJrIUyKtiYGeWxw
  3.  
  4. local path = "/"..shell.dir()
  5.  
  6. os.loadAPI(path.."/ThreeD")
  7. os.loadAPI(path.."/bufferAPI")
  8. os.loadAPI(path.."/blittle")
  9. os.loadAPI(path.."/noise")
  10.  
  11. local function generateSeed()
  12.     local preSeed = 0
  13.     local webPage = http.get("https://www.random.org/cgi-bin/randbyte?nbytes=7&format=d")
  14.  
  15.     if (webPage) then
  16.         local rawSeed = webPage.readAll()
  17.         for byteString in rawSeed:gmatch("%d+") do
  18.             preSeed = preSeed * 256 + tonumber(byteString)
  19.         end
  20.     else
  21.         preSeed = os.time()
  22.     end
  23.  
  24.     return preSeed - 1
  25. end
  26.  
  27. local function generateWorld(seed, rows, columns, maxHeight, relativeSnowHeight)
  28.     local objects = {}
  29.     local models = {}
  30.  
  31.     local xOffset = -(4 + 8/4)
  32.     local zOffset = -(4 * math.pow(0.75, 0.5))
  33.  
  34.     math.randomseed(seed)
  35.     for chunkX = 0, columns - 1 do
  36.         for chunkZ = 0, rows - 1 do
  37.             local mapNoise1 = noise.createNoise(8, chunkX, chunkZ, seed)
  38.             local mapNoise2 = noise.createNoise(8, chunkX + 1, chunkZ, seed)
  39.             local mapNoise3 = noise.createNoise(8, chunkX, chunkZ + 1, seed)
  40.             local mapNoise4 = noise.createNoise(8, chunkX + 1, chunkZ + 1, seed)
  41.  
  42.             local map = {}
  43.             for a = 1, 8 do
  44.                 for b = 1, 8 do
  45.                     local height1 = mapNoise1[a][b]*maxHeight - 0.5*maxHeight
  46.                     local height2 = 0
  47.                     if (a < 8) then
  48.                         height2 = mapNoise1[a+1][b]*maxHeight - 0.5*maxHeight
  49.                     else
  50.                         height2 = mapNoise2[1][b]*maxHeight - 0.5*maxHeight
  51.                     end
  52.                     local height3 = 0
  53.                     if (b < 8) then
  54.                         height3 = mapNoise1[a][b+1]*maxHeight - 0.5*maxHeight
  55.                     else
  56.                         height3 = mapNoise3[a][1]*maxHeight - 0.5*maxHeight
  57.                     end
  58.                     local height4 = 0
  59.                     if (a == 8 and b == 8) then
  60.                         height4 = mapNoise4[1][1]*maxHeight - 0.5*maxHeight
  61.                     elseif (a == 8) then
  62.                         height4 = mapNoise2[1][b+1]*maxHeight - 0.5*maxHeight
  63.                     elseif (b == 8) then
  64.                         height4 = mapNoise3[a+1][1]*maxHeight - 0.5*maxHeight
  65.                     else
  66.                         height4 = mapNoise1[a+1][b+1]*maxHeight - 0.5*maxHeight
  67.                     end
  68.  
  69.                     local c1 = colors.lime
  70.                     local c2 = colors.green
  71.  
  72.                     local snowHeight = relativeSnowHeight * maxHeight - 0.5*maxHeight
  73.  
  74.                     if (height1 >= snowHeight or height2 >= snowHeight or height3 >= snowHeight) then
  75.                         c1 = colors.white
  76.                     end
  77.                     if (height2 >= snowHeight or height3 >= snowHeight or height4 >= snowHeight) then
  78.                         c2 = colors.lightGray
  79.                     end
  80.                    
  81.                     map[#map+1] = {
  82.                         x1 = xOffset + b/2 + a+1, y1 = height2, z1 = zOffset + b*math.pow(0.75, 0.5),
  83.                         x2 = xOffset + b/2 + a, y2 = height1, z2 = zOffset + b*math.pow(0.75, 0.5),
  84.                         x3 = xOffset + b/2 + a+0.5, y3 = height3, z3 = zOffset + (b+1)*math.pow(0.75, 0.5),
  85.                         c = c1
  86.                     }
  87.  
  88.                     map[#map+1] = {
  89.                         x1 = xOffset + b/2 + a+0.5, y1 = height3, z1 = zOffset + (b+1)*math.pow(0.75, 0.5),
  90.                         x2 = xOffset + b/2 + a+1.5, y2 = height4, z2 = zOffset + (b+1)*math.pow(0.75, 0.5),
  91.                         x3 = xOffset + b/2 + a+1, y3 = height2, z3 = zOffset + b*math.pow(0.75, 0.5),
  92.                         c = c2
  93.                     }
  94.                 end
  95.             end
  96.             objects[#objects+1] = {model = "chunk"..chunkX..";"..chunkZ, x = chunkX * 8 + chunkZ * 4, y = 0, z = chunkZ * 8 * math.pow(0.75, 0.5)}
  97.             models[#models+1] = {modelname = "chunk"..chunkX..";"..chunkZ, model = map}
  98.         end
  99.     end
  100.  
  101.     return objects, models
  102. end
  103.  
  104. local playerX = 0
  105. local playerY = 0
  106. local playerZ = 0
  107.  
  108. local playerSpeed = 3
  109. local playerTurnSpeed = 180
  110. local keysDown = {}
  111. local blockThickness = 0.9
  112.  
  113. local FoV = 90
  114.  
  115. local playerDirectionHor = 30
  116. local playerDirectionVer = -45
  117.  
  118. local screenWidth, screenHeight = term.getSize()
  119.  
  120. local backgroundColor1 = colors.lightBlue
  121. local backgroundColor2 = colors.lightBlue
  122.  
  123. local ThreeDFrame = ThreeD.newFrame(1, 1, screenWidth, screenHeight, FoV, playerX, playerY + 0.5, playerZ, playerDirectionVer, playerDirectionHor, path.."/models")
  124. ThreeDFrame:saveSortedModels(true)
  125. local blittleOn = true
  126. ThreeDFrame:useBLittle(blittleOn)
  127.  
  128. local totalTime = 0
  129.  
  130. local seed = generateSeed()
  131. local chunkRows = 1
  132. local chunkCollumns = 1
  133. local maxHeight = 8
  134. local relativeSnowHeight = 0.70
  135.  
  136. local objects, models = generateWorld(seed, chunkRows, chunkCollumns, maxHeight, relativeSnowHeight)
  137.  
  138. for i = 1, #models do
  139.     ThreeDFrame:loadModel(models[i].modelname, models[i].model)
  140. end
  141.  
  142. local function rendering()
  143.     while true do
  144.         ThreeDFrame:loadGround(backgroundColor1)
  145.         ThreeDFrame:loadSky(backgroundColor2)
  146.         ThreeDFrame:loadObjects(objects)
  147.         ThreeDFrame:drawBuffer()
  148.  
  149.         os.queueEvent("FakeEvent")
  150.         os.pullEvent("FakeEvent")
  151.     end
  152. end
  153.  
  154. local function free(x, y, z)
  155.     for _, object in pairs(objects) do
  156.         if (object.solid == true) then
  157.             if (x >= object.x - blockThickness and x <= object.x + blockThickness) then
  158.                 if (y >= object.y - 1 and y <= object.y + 0.5 + 0.2) then
  159.                     if (z >= object.z - blockThickness and z <= object.z + blockThickness) then
  160.                         return false
  161.                     end
  162.                 end
  163.             end
  164.         end
  165.     end
  166.  
  167.     return true
  168. end
  169.  
  170. local function inputPlayer(time)
  171.     local dx = 0
  172.     local dy = 0
  173.     local dz = 0
  174.  
  175.     if (keysDown[keys.left]) then
  176.         playerDirectionHor = playerDirectionHor - playerTurnSpeed * time
  177.         if (playerDirectionHor <= -180) then
  178.             playerDirectionHor = playerDirectionHor + 360
  179.         end
  180.     end
  181.     if (keysDown[keys.right]) then
  182.         playerDirectionHor = playerDirectionHor + playerTurnSpeed * time
  183.         if (playerDirectionHor >= 180) then
  184.             playerDirectionHor = playerDirectionHor - 360
  185.         end
  186.     end
  187.     if (keysDown[keys.down]) then
  188.         playerDirectionVer = playerDirectionVer - playerTurnSpeed * time
  189.         if (playerDirectionVer < -80) then
  190.             playerDirectionVer = -80
  191.         end
  192.     end
  193.     if (keysDown[keys.up]) then
  194.         playerDirectionVer = playerDirectionVer + playerTurnSpeed * time
  195.         if (playerDirectionVer > 80) then
  196.             playerDirectionVer = 80
  197.         end
  198.     end
  199.     if (keysDown[keys.w]) then
  200.         dx = playerSpeed * math.cos(math.rad(playerDirectionHor)) + dx
  201.         dz = playerSpeed * math.sin(math.rad(playerDirectionHor)) + dz
  202.     end
  203.     if (keysDown[keys.s]) then
  204.         dx = -playerSpeed * math.cos(math.rad(playerDirectionHor)) + dx
  205.         dz = -playerSpeed * math.sin(math.rad(playerDirectionHor)) + dz
  206.     end
  207.     if (keysDown[keys.a]) then
  208.         dx = playerSpeed * math.cos(math.rad(playerDirectionHor - 90)) + dx
  209.         dz = playerSpeed * math.sin(math.rad(playerDirectionHor - 90)) + dz
  210.     end
  211.     if (keysDown[keys.d]) then
  212.         dx = playerSpeed * math.cos(math.rad(playerDirectionHor + 90)) + dx
  213.         dz = playerSpeed * math.sin(math.rad(playerDirectionHor + 90)) + dz
  214.     end
  215.    
  216.     if (keysDown[keys.space]) then
  217.         dy = playerSpeed + dy
  218.     end
  219.     if (keysDown[keys.leftShift]) then
  220.         dy = -playerSpeed + dy
  221.     end
  222.    
  223.     if (free(playerX + dx * time, playerY, playerZ) == true) then
  224.         playerX = playerX + dx * time
  225.     end
  226.     if (free(playerX, playerY + dy * time, playerZ) == true) then
  227.         playerY = playerY + dy * time
  228.     end
  229.     if (free(playerX, playerY, playerZ + dz * time) == true) then
  230.         playerZ = playerZ + dz * time
  231.     end
  232.  
  233.     ThreeDFrame:setCamera(playerX, playerY + 0.5, playerZ, playerDirectionHor, playerDirectionVer)
  234. end
  235.  
  236. local function keyInput()
  237.     while true do
  238.         local event, key, x, y = os.pullEventRaw()
  239.  
  240.         if (event == "key") then
  241.             keysDown[key] = true
  242.             if (key == keys.g) then
  243.                 if (blittleOn == false) then
  244.                     blittleOn = true
  245.                     ThreeDFrame:useBLittle(true)
  246.                 else
  247.                     blittleOn = false
  248.                     ThreeDFrame:useBLittle(false)
  249.                 end
  250.             end
  251.         elseif (event == "key_up") then
  252.             keysDown[key] = nil
  253.         end
  254.     end
  255. end
  256.  
  257. local function updateGame(time)
  258.     totalTime = totalTime + time
  259. end
  260.  
  261. local function gameUpdate()
  262.     local timeFromLastUpdate = os.clock()
  263.     local avgUpdateSpeed = 0
  264.     local updateCount = 0
  265.     local timeOff = 0
  266.  
  267.     while true do
  268.         local currentTime = os.clock()
  269.         if (currentTime > timeFromLastUpdate) then
  270.             updateGame(currentTime - timeFromLastUpdate - timeOff)
  271.             inputPlayer(currentTime - timeFromLastUpdate - timeOff)
  272.             avgUpdateSpeed = (currentTime - timeFromLastUpdate) / (updateCount + 1)
  273.             updateCount = 0
  274.             timeOff = 0
  275.         else
  276.             updateGame(avgUpdateSpeed)
  277.             inputPlayer(avgUpdateSpeed)
  278.             newTimeOff = timeOff + avgUpdateSpeed
  279.             newUpdateCount = updateCount + 1
  280.         end
  281.         timeFromLastUpdate = currentTime
  282.  
  283.         sleep(0)
  284.     end
  285. end
  286.  
  287. parallel.waitForAll(keyInput, gameUpdate, rendering)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement