Package Hexel.blocks.rules

Source Code of Hexel.blocks.rules.TreeRules

package Hexel.blocks.rules;

import java.util.ArrayList;
import java.util.Random;

import Hexel.blocks.BlockDelta;
import Hexel.blocks.BlockRule;
import Hexel.blocks.BlockRules;
import Hexel.blocks.BlockSimulator;
import Hexel.blocks.BlockSimulator.BlockDeltaAdder;
import Hexel.blocks.FreqBlockRule;
import Hexel.blocks.ProbabilisticFreqBlockRule;
import Hexel.blocks.types.Block;
import Hexel.blocks.types.BlockEmpty;
import Hexel.blocks.types.BlockLeaf;
import Hexel.blocks.types.BlockWood;
import Hexel.blocks.types.BlockWoodData;
import Hexel.chunk.Chunk;
import Hexel.chunk.Chunks;
import Hexel.chunk.HighestBlockChunk;
import Hexel.math.HexGeometry;
import Hexel.math.Vector2i;
import Hexel.math.Vector3i;
import Hexel.util.Container;
import Hexel.util.Util;

public class TreeRules {
 
  // empty block -> wood block
  //    calculate neighbor set
 
  private static Vector3i tmp3i = new Vector3i();
 
  public static void addRules(ArrayList<BlockRule> woodRules, ArrayList<BlockRule> leafRules){
    for (int _neighborIndex = 0; _neighborIndex < 5; _neighborIndex++){
      final int neighborIndex = _neighborIndex;
     
//      ==========BRANCHES=========
      woodRules.add(new ProbabilisticFreqBlockRule(1000, .5){

        @Override
        public boolean canApply(int bx, int by, int bz, boolean fastMode, Block b, Chunk c,
            HighestBlockChunk hbc, Chunks chunks, int step, BlockDeltaAdder blockDeltaAdder) {
          Vector3i[] neighbors = HexGeometry.getAllNeighbors(bx);
          Vector3i neighbor = neighbors[neighborIndex];
          BlockWood wb = (BlockWood) b;
          if (!wb.data.alive)
            return false;
          Block nb = chunks.getBlock(bx + neighbor.x, by + neighbor.y, bz + neighbor.z, tmp3i, c);
          if (!(nb instanceof BlockEmpty || nb instanceof BlockLeaf))
            return false;
         
          int nx = bx + neighbor.x;
          int ny = by + neighbor.y;
          int nz = bz + neighbor.z;
         
          boolean isBranch = nx != bx || ny != by;

          if (nz < bz)
            return false;
          if(wb.data.level < 4 && isBranch)
            return false;
 
          int h = 1;
          h = 31*h + nx;
          h = 31*h + ny;
          h = 31*h + nz;
         
          if (wb.data.level != 0){
            double chanceGrow = 0;
            if (isBranch){
              chanceGrow =   .5*((wb.data.level-wb.data.branchLevel)%2) +
                        .05*(wb.data.level-10)
                       )
                       *(1-wb.data.branchLevel*.25)
                       *(1-wb.data.branches*.25);
            }
            else {
              chanceGrow = 3.0 - .1*wb.data.level*(wb.data.branchLevel*10000+1);
            }
            if (Util.hashToDouble(h) > chanceGrow)
              return false;
          }
         
          return true;
        }

        @Override
        public int probabilisticRun(int bx, int by, int bz, boolean fastMode, Block b, Chunk c,
            HighestBlockChunk hbc, Chunks chunks, int step, BlockDeltaAdder blockDeltaAdder) {
          BlockWood wb = (BlockWood) b;
         
          Vector3i[] neighbors = HexGeometry.getAllNeighbors(bx);
          Vector3i neighbor = neighbors[neighborIndex];
         
          int nx = bx + neighbor.x;
          int ny = by + neighbor.y;
          int nz = bz + neighbor.z;
         
          boolean isBranch = nx != bx || ny != by;
         
          tmp3i.x = nx;
          tmp3i.y = ny;
          tmp3i.z = nz;
          if (blockDeltaAdder.hasBlockDelta(tmp3i)){
            return 1;
          }
          tmp3i.x = bx;
          tmp3i.y = by;
          tmp3i.z = bz;
          if (blockDeltaAdder.hasBlockDelta(tmp3i)){
            return 1;
          }

          BlockWood grownWB;
          BlockWood newWB;
          if (isBranch){
            grownWB = BlockWood.Make(new BlockWoodData(BlockWood.MAX_HEALTH, wb.data.level+1, wb.data.branchLevel+1, 0, true));
            newWB = BlockWood.Make(new BlockWoodData(BlockWood.MAX_HEALTH, wb.data.level, wb.data.branchLevel, wb.data.branches+1, true));
          }
          else {
            grownWB = BlockWood.Make(new BlockWoodData(BlockWood.MAX_HEALTH, wb.data.level+1, wb.data.branchLevel, 0, true));
            newWB = BlockWood.Make(new BlockWoodData(BlockWood.MAX_HEALTH, wb.data.level, wb.data.branchLevel, wb.data.branches, true));
          }
          blockDeltaAdder.addBlockDelta(BlockDelta.Aquire(nx, ny, nz, grownWB, chunks.getStepsToSim(bx, by, bz, tmp3i, c)));
          blockDeltaAdder.addBlockDelta(BlockDelta.Aquire(bx, by, bz, newWB, chunks.getStepsToSim(bx, by, bz, tmp3i, c)));
          return BlockSimulator.DONT_RUN;
        }
      });
//
      //==========LEAFS=========
      woodRules.add(new ProbabilisticFreqBlockRule(500, .5){
       
        @Override
        public boolean canApply(int bx, int by, int bz, boolean fastMode, Block b, Chunk c,
            HighestBlockChunk hbc, Chunks chunks, int step, BlockDeltaAdder blockDeltaAdder) {
          Vector3i[] neighbors = HexGeometry.getAllNeighbors(bx);
          Vector3i neighbor = neighbors[neighborIndex];
          BlockWood wb = (BlockWood) b;
          if (!wb.data.alive)
            return false;
          if(wb.data.level < 4)
            return false;
          if (!(chunks.getBlock(bx + neighbor.x, by + neighbor.y, bz + neighbor.z, tmp3i, c) instanceof BlockEmpty))
            return false;
         
          int nx = bx + neighbor.x;
          int ny = by + neighbor.y;
          int nz = bz + neighbor.z;
 
          int h = 1;
          h = 31*h + nx;
          h = 31*h + ny;
          h = 31*h + nz;
          if (wb.data.level != 0){
            double chanceGrow = 0;
            chanceGrow = Math.min(Math.max(.1*(wb.data.level-5), 0), .75) + Math.min(.5*wb.data.branchLevel, .75);
            if (Util.hashToDouble(h) > chanceGrow)
              return false;
          }
          return true;
        }

        @Override
        public int probabilisticRun(int bx, int by, int bz, boolean fastMode, Block b, Chunk c,
            HighestBlockChunk hbc, Chunks chunks, int step, BlockDeltaAdder blockDeltaAdder) {
          BlockWood wb = (BlockWood) b;
         
          Vector3i[] neighbors = HexGeometry.getAllNeighbors(bx);
          Vector3i neighbor = neighbors[neighborIndex];
         
          int nx = bx + neighbor.x;
          int ny = by + neighbor.y;
          int nz = bz + neighbor.z;
         
          tmp3i.x = nx;
          tmp3i.y = ny;
          tmp3i.z = nz;
          if (blockDeltaAdder.hasBlockDelta(tmp3i)){
            return 1;
          }
          tmp3i.x = bx;
          tmp3i.y = by;
          tmp3i.z = bz;
          if (blockDeltaAdder.hasBlockDelta(tmp3i)){
            return 1;
          }

          BlockLeaf leaf = BlockLeaf.Make(0, BlockLeaf.MAX_HEALTH);
          blockDeltaAdder.addBlockDelta(BlockDelta.Aquire(nx, ny, nz, leaf, chunks.getStepsToSim(bx, by, bz, tmp3i, c)));
          return BlockSimulator.DONT_RUN;
        }
      });
    }
  }
}
TOP

Related Classes of Hexel.blocks.rules.TreeRules

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.