Advertisement
klassekatze

AABBChunk.java

Aug 2nd, 2023
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.26 KB | None | 0 0
  1. package com.klassekatze.mineassistant.hpathmgr;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.util.math.BlockPos;
  9. import net.minecraft.world.IBlockAccess;
  10.  
  11. import net.minecraft.world.ChunkCache;
  12.  
  13. public class AABBChunk {
  14.  
  15.     IBlockAccess blockCache = null;
  16.     public BlockPos pointOfOrigin = null;
  17.  
  18.     public ArrayList<I_AABB_base> AABBCache;
  19.     public ArrayList<I_AABB_base> AABBCache_airlike = new ArrayList<I_AABB_base>();
  20.     public ArrayList<I_AABB_base> AABBCache_airlike_with_ground = new ArrayList<I_AABB_base>();
  21.  
  22.     boolean isDirty = false;
  23.     public void setDirty()
  24.     {
  25.         isDirty = true;
  26.     }
  27.  
  28.     int MAX_STEPPABLE_Y = 1;
  29.     void updateAirlikeNeighborLists()//todo logic to incorporate other chunks...
  30.     {
  31.         regenIfDirty();
  32.  
  33.         for(int i = 0; i < AABBCache.size(); i++)
  34.         {
  35.             I_AABB_base a = AABBCache.get(i);
  36.            
  37.             //if(a.neighbors_steppable != null)a.neighbors_steppable.clear();
  38.             //e/lse a.neighbors_steppable = new ArrayList<I_AABB_base>();
  39.            
  40.             if(a.neighbors_all_airlike != null)a.neighbors_all_airlike.clear();
  41.             else a.neighbors_all_airlike = new ArrayList<I_AABB_base>();
  42.             if(a.airlike)
  43.             {
  44.                 for(int n = 0; n < AABBCache.size(); n++)
  45.                 {
  46.                     I_AABB_base b = AABBCache.get(n);
  47.                     if(b != a && b.airlike && is_aabb_adjacent(a, b))
  48.                     {
  49.                         a.neighbors_all_airlike.add(b);
  50.                         /*if(
  51.                                 a.Max.y() - b.Min.y() <= MAX_STEPPABLE_Y ||
  52.                                 b.Max.y() - a.Min.y() <= MAX_STEPPABLE_Y
  53.                                 )
  54.                         {
  55.                             a.neighbors_steppable.add(b);
  56.                         }*/
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     void updateHasGroundFlags()
  63.     {
  64.         for(int i = 0; i < AABBCache.size(); i++)
  65.         {
  66.             I_AABB_base a = AABBCache.get(i);
  67.             a.has_any_nonairlike_bottom = false;
  68.             for(int n = 0; n < AABBCache.size(); n++)
  69.             {
  70.                 I_AABB_base b = AABBCache.get(n);
  71.                 if(this.is_aabb_groundfor(a, b))
  72.                 {
  73.                     a.has_any_nonairlike_bottom = true;
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     public boolean regenIfDirty()
  81.     {
  82.         if(isDirty)
  83.         {
  84.             isDirty = false;
  85.             //regenhere
  86.            
  87.             blockCache = new ChunkCache(Minecraft.getMinecraft().world, pointOfOrigin, pointOfOrigin.add(15, 15, 15), 0);
  88.             //after some thought, this is probably a waste to keep in memory most of the time
  89.             //it literally doubles chunks in memory in a way
  90.            
  91.            
  92.            
  93.             AccessRotator access = new AccessRotator(blockCache,pointOfOrigin);
  94.             //.access.if(blockCache.getWorldType())
  95.            
  96.             ArrayList<I_AABB_base> aabbs = get_AABBs_from_volume(access);
  97.            
  98.             blockCache = null;//drop it now.
  99.            
  100.             AABBCache = aabbs;
  101.             //updateHasGroundFlags();
  102.             //updateAirlikeNeighborLists();
  103.            
  104.             /*AABBCache_airlike.clear();
  105.             AABBCache_airlike_with_ground.clear();
  106.             for(int i = 0; i < AABBCache.size(); i++)
  107.             {
  108.                 I_AABB_base a = AABBCache.get(i);
  109.                 if(a.airlike)AABBCache_airlike.add(a);
  110.                 if(a.has_any_nonairlike_bottom)AABBCache_airlike_with_ground.add(a);
  111.             }*/
  112.            
  113.            
  114.            
  115.            
  116.             return true;
  117.         }
  118.         return false;
  119.     }
  120.  
  121.  
  122.  
  123.     /*public VoxelChunk() {
  124.         // TODO Auto-generated constructor stub
  125.     }*/
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.     I_AABB_base IVox_AABBfor(Block voxel, int x, int y, int z, int l, int w, int h)
  141.     {
  142.         //IVox_AABB v = IVec_AABBfor(x, y, z, l, w, h);
  143.         I_AABB_base v = new I_AABB_base();// = IVox_AABB();
  144.         v.Min = new SimpleMutableBlockPos(x,y,z);//glm::ivec3(x, y, z);
  145.         v.Max = new SimpleMutableBlockPos(x+l,y+w,z+h);//glm::ivec3(x + l, y + w, z + h);
  146.         v.airlike = voxel.getDefaultState().getMaterial() == Material.AIR;//isAir(state, , pos)
  147.         //v.mVoxel = voxel;
  148.         return v;
  149.     }
  150.  
  151.  
  152.     boolean sweep_volume_solid_aabb(AccessRotator Volume, RawVoxelVolume<I_AABB_base> ownfield, I_AABB_base aabb, boolean material_limit)
  153.     {
  154.  
  155.         int min_x = aabb.Min.getX();//x;
  156.         int min_y = aabb.Min.getY();//y;
  157.         int min_z = aabb.Min.getZ();//z;
  158.  
  159.         int max_x = aabb.Max.getX();//x;
  160.         int max_y = aabb.Max.getY();//y;
  161.         int max_z = aabb.Max.getZ();//z;
  162.         //PolyVox::Region enclosure = ownfield->getEnclosingRegion();
  163.         //if (min_x < enclosure.getLowerX() || min_y < enclosure.getLowerY() || min_z < enclosure.getLowerZ())return false;
  164.         //if (max_x > enclosure.getUpperX() || max_y > enclosure.getUpperY() || max_z > enclosure.getUpperZ())return false;
  165.  
  166.         for (int x = min_x; x < max_x; x++)
  167.         {
  168.             for (int y = min_y; y < max_y; y++)
  169.             {
  170.                 for (int z = min_z; z < max_z; z++)
  171.                 {
  172.                     //if (material_limit && Volume->getVoxel(x, y, z).getMaterial() == 0)return false;
  173.                     //if(Volume.getVoxel(x, y, z).getDefaultState().getMaterial() == Material.AIR)return false;
  174.                     if (ownfield.getVoxel(x, y, z) != null)return false;
  175.                     //if (material_limit && Volume->getVoxel(x, y, z) != aabb.mVoxel)return false;
  176.                 }
  177.             }
  178.         }
  179.         return true;
  180.     }
  181.  
  182.     boolean is_aabb_adjacent(I_AABB_base a, I_AABB_base b)
  183.     {
  184.         if(
  185.                 a.Max.x() == b.Min.x() ||
  186.                 a.Max.y() == b.Min.y() ||
  187.                 a.Max.z() == b.Min.z() ||
  188.                 b.Max.x() == a.Min.x() ||
  189.                 b.Max.y() == a.Min.y() ||
  190.                 b.Max.z() == a.Min.z()
  191.                 )
  192.         {
  193.             return true;
  194.         }
  195.  
  196.         return false;
  197.     }
  198.    
  199.     boolean is_aabb_groundfor(I_AABB_base a, I_AABB_base b)
  200.     {
  201.         if(b.Max.y() == a.Min.y() && b.airlike == false && a.airlike == true)
  202.         {
  203.             return true;
  204.         }
  205.  
  206.         return false;
  207.     }
  208.     /*.boolean is_aabb_adjacency_within_offset(I_AABB_base a, I_AABB_base b, BlockPos offset)
  209.     {
  210.         if(
  211.                 a.Max.x() - b.Min.x() <= offset.getX() ||
  212.                 a.Max.y() - b.Min.y() <= offset.getY() ||
  213.                 a.Max.z() - b.Min.z() <= offset.getZ() ||
  214.                 b.Max.x() - a.Min.x() <= offset.getX() ||
  215.                 b.Max.y() - a.Min.y() <= offset.getY() ||
  216.                 b.Max.z() - a.Min.z() <= offset.getZ()
  217.                 )
  218.         {
  219.             return true;
  220.         }
  221.         return false;
  222.     }*/
  223.  
  224.  
  225.     boolean sweep_volume_solid_aabb_markowned(RawVoxelVolume<I_AABB_base> ownfield, I_AABB_base aabb)
  226.     {
  227.         int min_x = aabb.Min.x();
  228.         int min_y = aabb.Min.y();
  229.         int min_z = aabb.Min.z();
  230.  
  231.         int max_x = aabb.Max.x();
  232.         int max_y = aabb.Max.y();
  233.         int max_z = aabb.Max.z();
  234.         for (int x = min_x; x < max_x; x++)
  235.         {
  236.             for (int y = min_y; y < max_y; y++)
  237.             {
  238.                 for (int z = min_z; z < max_z; z++)
  239.                 {
  240.                     ownfield.setVoxel(x, y, z, aabb);
  241.                 }
  242.             }
  243.         }
  244.         return true;
  245.     }
  246.  
  247.     ArrayList<I_AABB_base> generate_aabbs_from_voxels(AccessRotator accessor, boolean only_solids)
  248.     {
  249.  
  250.         RawVoxelVolume<I_AABB_base> aabb_voxel_claims = new RawVoxelVolume<I_AABB_base>(accessor.getHeight(), accessor.getWidth(), accessor.getDepth(), null);
  251.  
  252.  
  253.         //.getClass().OwnField aabb_voxel_claims = OwnField(accessor->getHeight(), accessor->getWidth(), accessor->getDepth(), nullptr);
  254.         //establish owner data - so as we iterate a AABB can claim voxels it includes and we can quickly find owners without reiterating...
  255.         //its easy and it should be faster, albeit (irrelevantly) more memory
  256.  
  257.         ArrayList<I_AABB_base> aabbs = new ArrayList<I_AABB_base>();
  258.         aabbs.clear();
  259.  
  260.         for (int z = 0; z < accessor.getDepth(); z++)
  261.         {
  262.             for (int y = 0; y < accessor.getWidth(); y++)
  263.             {
  264.                 for (int x = 0; x < accessor.getHeight(); x++)
  265.                 {
  266.                     //alright so grab current block.
  267.  
  268.                     Block voxel = accessor.getVoxel(new BlockPos(x, y, z));
  269.                     I_AABB_base cur_voxel_box = IVox_AABBfor(voxel, x, y, z,1,1,1);//create a 1,1,1 wide box at x,y,z
  270.                     boolean keep = sweep_volume_solid_aabb(accessor, aabb_voxel_claims, cur_voxel_box, only_solids);//check if it is solid and unowned
  271.  
  272.                     if (keep)//we have a starting voxel
  273.                     {
  274.                         int stored_axis_min;
  275.  
  276.  
  277.                         stored_axis_min = cur_voxel_box.Min.x();
  278.                         do
  279.                         {
  280.                             //cur_voxel_box.Max.x += 1;
  281.                             //cur_voxel_box.Min.x += 1;
  282.                             cur_voxel_box.Max.addX(1);
  283.                             cur_voxel_box.Min.addX(1);
  284.  
  285.  
  286.  
  287.                             //shift forward one
  288.                             keep = sweep_volume_solid_aabb(accessor, aabb_voxel_claims, cur_voxel_box, only_solids) && cur_voxel_box.Max.x() < accessor.getHeight();
  289.                         } while (keep);
  290.                         //shift failed, backstep one to legit
  291.                         cur_voxel_box.Max.subX(1);
  292.                         //restore min to encompass all legit slices
  293.                         cur_voxel_box.Min.setX(stored_axis_min);
  294.  
  295.                         stored_axis_min = cur_voxel_box.Min.y();
  296.                         do
  297.                         {
  298.                             //cur_voxel_box.Max.y += 1;
  299.                             //cur_voxel_box.Min.y += 1;
  300.                             cur_voxel_box.Max.addY(1);
  301.                             cur_voxel_box.Min.addY(1);
  302.                             //shift forward one
  303.                             keep = sweep_volume_solid_aabb(accessor, aabb_voxel_claims, cur_voxel_box, only_solids) && cur_voxel_box.Max.y() < accessor.getWidth();
  304.                         } while (keep);
  305.                         //shift failed, backstep one to legit
  306.                         cur_voxel_box.Max.subY(1);
  307.                         //restore min to encompass all legit slices
  308.                         cur_voxel_box.Min.setY(stored_axis_min);
  309.  
  310.                         stored_axis_min = cur_voxel_box.Min.z();
  311.                         do
  312.                         {
  313.                             cur_voxel_box.Max.addZ(1);
  314.                             cur_voxel_box.Min.addZ(1);
  315.                             //shift forward one
  316.                             keep = sweep_volume_solid_aabb(accessor, aabb_voxel_claims, cur_voxel_box, only_solids) && cur_voxel_box.Max.z() < accessor.getDepth();
  317.                         } while (keep);
  318.                         //shift failed, backstep one to legit
  319.                         cur_voxel_box.Max.subZ(1);
  320.                         //restore min to encompass all legit slices
  321.                         cur_voxel_box.Min.setZ(stored_axis_min);
  322.  
  323.  
  324.  
  325.                         sweep_volume_solid_aabb_markowned(aabb_voxel_claims, cur_voxel_box);//mark all enciompassed voxels as owned (by this aabb)
  326.                         //mark as owned.
  327.  
  328.                         //now add to list
  329.                         //set_void_exposure_aabb(accessor, cur_voxel_box);
  330.                         aabbs.add(cur_voxel_box);
  331.  
  332.                     }
  333.                 }
  334.             }
  335.         }
  336.         return aabbs;
  337.     }
  338.  
  339.     ArrayList<I_AABB_base> derotate_aabbs(ArrayList<I_AABB_base> aabbs, int width, int rot)
  340.     {
  341.         ArrayList<I_AABB_base> ret = new ArrayList<I_AABB_base>();
  342.         for (int i = 0; i < aabbs.size(); i++)
  343.         {
  344.             //le sigh
  345.             I_AABB_base aabb = aabbs.get(i);//[i];
  346.  
  347.             //reverse dpesn't work. it seems that the accessor flipped things in this specific case???
  348.             BlockPos a = AccessRotator.BlockPosRotator.rotate_ivec3(aabb.Min.subtract(pointOfOrigin), width, rot, false).add(pointOfOrigin);
  349.             BlockPos b = AccessRotator.BlockPosRotator.rotate_ivec3(aabb.Max.subtract(pointOfOrigin), width, rot, false).add(pointOfOrigin);
  350.  
  351.             aabb.Min.setPos(Math.min(a.getX(), b.getX()),Math.min(a.getY(), b.getY()),Math.min(a.getZ(), b.getZ()));
  352.             aabb.Max.setPos(Math.max(a.getX(), b.getX()),Math.max(a.getY(), b.getY()),Math.max(a.getZ(), b.getZ()));
  353.             //aabb.Min = glm::ivec3(glm::min(a.x, b.x), glm::min(a.y, b.y), glm::min(a.z, b.z));
  354.             //aabb.Max = glm::ivec3(glm::max(a.x, b.x), glm::max(a.y, b.y), glm::max(a.z, b.z));
  355.             if (rot > 2)
  356.             {
  357.                 aabb.Min.addX(1);
  358.                 aabb.Min.addY(1);
  359.                 aabb.Min.addZ(1);
  360.  
  361.                 aabb.Max.addX(1);
  362.                 aabb.Max.addY(1);
  363.                 aabb.Max.addZ(1);
  364.  
  365.                 //aabb.Min = aabb.Min.add(1,1,1);// += 1;
  366.                 //aabb.Max = aabb.Max.add(1,1,1);//aabb.Max// += 1;
  367.             }
  368.             ret.add(aabb);
  369.             //thinkin this maybe? eh.
  370.         }
  371.         return ret;
  372.     }
  373.  
  374.  
  375.     ArrayList<I_AABB_base> get_AABBs_from_volume(AccessRotator Volume)
  376.     {
  377.         ArrayList<I_AABB_base> fin_aabbs = new ArrayList<I_AABB_base>();
  378.         //for (int rot = 0; rot < 6; rot++)
  379.         int rot = 0;
  380.         {
  381.             Volume.setRotation(rot);
  382.  
  383.             ArrayList<I_AABB_base> aabbs = generate_aabbs_from_voxels(Volume, true);
  384.            
  385.             if (rot > 0)aabbs = derotate_aabbs(aabbs, 16, rot);
  386.  
  387.             if (fin_aabbs.size() == 0 || (aabbs.size() < fin_aabbs.size() && aabbs.size() > 0))
  388.             {
  389.                 fin_aabbs = aabbs;
  390.             }
  391.         }
  392.         //mVolumeAABB = fin_aabbs;
  393.         return fin_aabbs;
  394.     }
  395. }
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement