Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. private void updateLightMap(Block[][][] blocks){
  2.         lightMap = new byte[chunkSizeXZ][chunkSizeY][chunkSizeXZ];
  3. //      Logger.info("Updating Voxel Lightmap.");
  4.         Stack<Vector3i> litPoints = new Stack<Vector3i>();
  5.        
  6.         for (int x = 0; x < chunkSizeXZ; x++) {
  7.             for (int y = 0; y < chunkSizeY; y++) {
  8.                 for (int z = 0; z < chunkSizeXZ; z++) {
  9.                     Block block = blocks[x][y][z];
  10.                     if(block.LightOutput() > 0){
  11.                         //Logger.info("Light Source: " + x + ", " + z);
  12. //                        litPoints.push(new Vector3i(x, y, z));
  13.                         lightMap[x][y][z] = block.LightOutput();
  14. //                        
  15.                         litPoints.push(new Vector3i(x + 1, y, z));
  16.                         litPoints.push(new Vector3i(x - 1, y, z));
  17.                         litPoints.push(new Vector3i(x, y + 1, z));
  18.                         litPoints.push(new Vector3i(x, y - 1, z));
  19.                         litPoints.push(new Vector3i(x, y, z + 1));
  20.                         litPoints.push(new Vector3i(x, y, z - 1));
  21.                     }else if(x == chunkSizeXZ - 1 || z == chunkSizeXZ - 1 || x == 0 || z == 0){
  22.                         litPoints.push(new Vector3i(x, y, z));
  23.                     }
  24.                 }
  25.             }
  26.         }  
  27.        
  28.         byte[][][] visited = new byte[chunkSizeXZ][chunkSizeY][chunkSizeXZ];
  29.        
  30.         Vector3i point;
  31.         int x, y, z;
  32.         long startTimeLoopPoints = System.nanoTime();
  33.         while(!litPoints.isEmpty()){
  34.             point = litPoints.pop();
  35.            
  36.             x = point.x;
  37.             y = point.y;
  38.             z = point.z;
  39.            
  40.             if(outOfChunkBounds(x, y, z) || visited[x][y][z] > 1)
  41.                 continue;
  42.            
  43.             if(blocks[x][y][z].LightOutput() <= 1)
  44.                 if(blocks[x][y][z].blockType() != 0)
  45.                 continue;
  46.            
  47.             if(!hasNonAirNeighbour(x, y, z) && !hasNonAirNeighbourDiagonal(x, y, z))
  48.                 continue;
  49.            
  50.             byte highestLightLvl = getHighestNeighbourLightLevel(x, y, z);
  51.            
  52.             if(highestLightLvl <= 1) continue;
  53.            
  54.             setLightLvl(x, y, z, (byte)(highestLightLvl - 1));
  55.  
  56.             visited[x][y][z]++;
  57.             //TODO: Check and add neighbours
  58.             litPoints.push(new Vector3i(x + 1, y, z));
  59.             litPoints.push(new Vector3i(x - 1, y, z));
  60.             litPoints.push(new Vector3i(x, y + 1, z));
  61.             litPoints.push(new Vector3i(x, y - 1, z));
  62.             litPoints.push(new Vector3i(x, y, z + 1));
  63.             litPoints.push(new Vector3i(x, y, z - 1));
  64.         }
  65.         long estimatedTimeLoopPoints = System.nanoTime() - startTimeLoopPoints;
  66.         Logger.debug("Chunk(" + pos + ") -> updateLightMap -> Loop Points -> Execution Time: " + (estimatedTimeLoopPoints / 1000000) + "ms");
  67. //      Logger.info("Complete Light Map Algorithm!");
  68.     }
  69.  
  70.  
  71.  
  72. private byte getHighestNeighbourLightLevel(int x, int y, int z){
  73.         long startTime = System.nanoTime();
  74.         byte lightLvl = 1;
  75.        
  76.         byte tmpLvl = getLightLvl(x + 1, y, z);
  77.         if(tmpLvl > lightLvl)
  78.             lightLvl = tmpLvl;
  79.        
  80.         tmpLvl = getLightLvl(x - 1, y, z);
  81.         if(tmpLvl > lightLvl)
  82.             lightLvl = tmpLvl;
  83.        
  84.         tmpLvl = getLightLvl(x, y + 1, z);
  85.         if(tmpLvl > lightLvl)
  86.             lightLvl = tmpLvl;
  87.        
  88.         tmpLvl = getLightLvl(x, y - 1, z);
  89.         if(tmpLvl > lightLvl)
  90.             lightLvl = tmpLvl;
  91.        
  92.         tmpLvl = getLightLvl(x, y, z + 1);
  93.         if(tmpLvl > lightLvl)
  94.             lightLvl = tmpLvl;
  95.        
  96.         tmpLvl = getLightLvl(x, y, z - 1);
  97.         if(tmpLvl > lightLvl)
  98.             lightLvl = tmpLvl;
  99.        
  100. //      if(getLightLvl(x, y, z) > lightLvl)
  101. //          lightLvl = tmpLvl;
  102.        
  103.         long endTime = System.nanoTime() - startTime;
  104.         if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> getHighestLightLevel() -> Execution Time: " + (endTime / 1000000) + "ms");
  105.        
  106.         return lightLvl;
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement