Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. ///LIGHTING METHODS\\\
  2.  
  3.     private Vector3i[] getNeighbours(int x, int y, int z, boolean includeLightSourceAndChunkEdges){
  4.         long startTime = System.nanoTime();
  5.         Vector3i[] neighbours = new Vector3i[6];
  6.        
  7.         neighbours[0] = new Vector3i(x, y, z - 1);//north
  8.         neighbours[1] = new Vector3i(x, y, z + 1);//south
  9.         neighbours[2] = new Vector3i(x + 1, y, z);//east
  10.         neighbours[3] = new Vector3i(x - 1, y, z);//west
  11.         neighbours[4] = new Vector3i(x, y + 1, z);//up
  12.         neighbours[5] = new Vector3i(x, y - 1, z);//down
  13.        
  14.         List<Vector3i> removePos = new ArrayList<>();
  15.        
  16.         for (int i = 0; i < 6; i++) {
  17.             Vector3i p = neighbours[i];
  18.             if(!includeLightSourceAndChunkEdges){
  19.                 if(p.x < 0 || p.y < 0 || p.z < 0) {
  20.                     removePos.add(p);
  21.                     continue;
  22.                 }
  23.                 if(p.x >= chunkSizeXZ || p.y >= chunkSizeY || p.z >= chunkSizeXZ) {
  24.                     removePos.add(p);
  25.                     continue;
  26.                 }
  27.                
  28.                 Block block = GetBlock(p.x, p.y, p.z);
  29.                 if(block.blockType() == new BlockLight().blockType() || block.blockType() != new BlockAir().blockType()){
  30.                     removePos.add(p);
  31.                     continue;
  32.                 }
  33.             }
  34.            
  35.             if(!hasNonAirNeighbour(p.x, p.y, p.z)){
  36.                 if(!hasNonAirNeighbourDiagonal(p.x, p.y, p.z)){
  37.                     removePos.add(p);
  38.                     continue;
  39.                 }
  40.             }
  41.            
  42.             //Logger.info("Didnt remove: " + p.x + ", " + p.z);
  43.         }
  44.        
  45.         Vector3i[] temp = new Vector3i[6 - removePos.size()];
  46.         int c = 0;
  47.         for (int n = 0; n < 6; n++) {
  48.             Vector3i t = neighbours[n];
  49.             if(removePos.contains(t))
  50.                 continue;
  51.            
  52.             temp[c] = t;
  53.             c++;
  54.         }
  55.         long endTime = System.nanoTime() - startTime;
  56.         if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> getNeighbours(" + includeLightSourceAndChunkEdges + ") -> Execution Time: " + (endTime / 1000000) + "ms");
  57.         return temp;
  58.     }
  59.    
  60.     private byte getHighestNeighbourLightLevel(Vector3i[] neighbours){
  61.         long startTime = System.nanoTime();
  62.         byte lightLvl = 1;
  63.         Vector3i neighbour;
  64.        
  65.         for (int i = 0; i < neighbours.length; i++) {
  66.             neighbour = neighbours[i];
  67.             byte tmpLvl = getLightLvl(neighbour.x, neighbour.y, neighbour.z);
  68.             if(tmpLvl > lightLvl){
  69.                 lightLvl = tmpLvl;
  70.             }
  71.         }
  72.         long endTime = System.nanoTime() - startTime;
  73.         if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> getHighestLightLevel() -> Execution Time: " + (endTime / 1000000) + "ms");
  74.        
  75.         return lightLvl;
  76.     }
  77.    
  78.     private boolean hasNonAirNeighbour(int x, int y, int z){
  79.         long startTime = System.nanoTime();
  80.        
  81.         Block block = GetBlock(x + 1, y, z);
  82.         if(block.blockType() > 0)
  83.             return true;
  84.        
  85.         block = GetBlock(x - 1, y, z);
  86.         if(block.blockType() > 0)
  87.             return true;
  88.        
  89.         block = GetBlock(x, y + 1, z);
  90.         if(block.blockType() > 0)
  91.             return true;
  92.        
  93.         block = GetBlock(x, y - 1, z);
  94.         if(block.blockType() > 0)
  95.             return true;
  96.        
  97.         block = GetBlock(x, y, z + 1);
  98.         if(block.blockType() > 0)
  99.             return true;
  100.        
  101.         block = GetBlock(x, y, z - 1);
  102.         if(block.blockType() > 0)
  103.             return true;
  104.  
  105.         long endTime = System.nanoTime() - startTime;
  106.         if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> hasNonAirNeighbour() -> Execution Time: " + (endTime / 1000000) + "ms");
  107.        
  108.         return false;
  109.     }
  110.    
  111.     private boolean hasNonAirNeighbourDiagonal(int x, int y, int z){
  112.         long startTime = System.nanoTime();
  113.        
  114.         Block block = GetBlock(x - 1, y + 1, z);
  115.         if(block.blockType() > 0)
  116.             return true;
  117.        
  118.         block = GetBlock(x - 1, y - 1, z);
  119.         if(block.blockType() > 0)
  120.             return true;
  121.        
  122.         block = GetBlock(x + 1, y - 1, z);
  123.         if(block.blockType() > 0)
  124.             return true;
  125.        
  126.         block = GetBlock(x + 1, y + 1, z);
  127.         if(block.blockType() > 0)
  128.             return true;
  129.        
  130.         block = GetBlock(x, y + 1, z + 1);
  131.         if(block.blockType() > 0)
  132.             return true;
  133.        
  134.         block = GetBlock(x, y - 1, z + 1);
  135.         if(block.blockType() > 0)
  136.             return true;
  137.        
  138.         block = GetBlock(x, y - 1, z - 1);
  139.         if(block.blockType() > 0)
  140.             return true;
  141.        
  142.         block = GetBlock(x, y + 1, z - 1);
  143.         if(block.blockType() > 0)
  144.             return true;
  145.        
  146.         long endTime = System.nanoTime() - startTime;
  147.         if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> hasNonAirNeighbourDiagonal() -> Execution Time: " + (endTime / 1000000) + "ms");
  148.        
  149.         return false;
  150.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement