Advertisement
Corosus

Untitled

Feb 5th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. package hostileworlds.ai.jobs;
  2.  
  3. import net.minecraft.pathfinding.PathEntity;
  4. import net.minecraft.util.ChunkCoordinates;
  5. import net.minecraft.util.MovingObjectPosition;
  6. import CoroAI.PFQueue;
  7. import CoroAI.componentAI.jobSystem.JobBase;
  8. import CoroAI.componentAI.jobSystem.JobManager;
  9. import CoroAI.entity.EnumActState;
  10. import CoroAI.entity.EnumJobState;
  11.  
  12. public class JobPathDigger extends JobGroupHorde {
  13.    
  14.     //Threading and pathfind simulation variables
  15.     public int pfReturnTimeout = 0;
  16.     public boolean waitingForPath = false;
  17.     public boolean waitingForThreadPath = false;
  18.     public PathEntity pathReturned = null;
  19.     public boolean tryPathToTarget = true;
  20.     public boolean hasPathToTarget = false;
  21.  
  22.     //Dig state variables
  23.     public int simuDigCount;
  24.     public ChunkCoordinates lastMinedCoord = null;
  25.     public EnumJobState digState = EnumJobState.IDLE;
  26.     public ChunkCoordinates bestDigCoord = null;
  27.    
  28.     public JobPathDigger(JobManager jm) {
  29.         super(jm);
  30.     }
  31.    
  32.     @Override
  33.     public boolean shouldExecute() {
  34.         return true;
  35.     }
  36.    
  37.     @Override
  38.     public boolean shouldContinue() {
  39.         return false;
  40.     }
  41.    
  42.     @Override
  43.     public void onIdleTick() {
  44.        
  45.     }
  46.    
  47.     @Override
  48.     public void tick() {
  49.         //super.tick();
  50.        
  51.         //w1 = waiting on good angle, skipped for above target digs
  52.         //w2 =
  53.         if (pfReturnTimeout > 0) {
  54.             pfReturnTimeout--;
  55.         } else {
  56.             if (waitingForPath) {
  57.                 waitingForPath = false;
  58.             }
  59.         }
  60.        
  61.         if (((JobGroupHorde)jm.getPrimaryJob()).attackCoord == null) return;
  62.        
  63.         ChunkCoordinates attackCoord = ((JobGroupHorde)jm.getPrimaryJob()).attackCoord;
  64.        
  65.         if (this.state == EnumJobState.IDLE) {
  66.            
  67.             if (!hasPathToTarget) {
  68.                 if (!waitingForPath) {
  69.                     if (tryPathToTarget) {
  70.                         if (ent.onGround) {
  71.                             waitingForPath = true;
  72.                            
  73.                             pathReturned = null;
  74.                             pfReturnTimeout = 40;
  75.                            
  76.                             waitingForThreadPath = true;
  77.                             PFQueue.tryPath(ent, attackCoord.posX, attackCoord.posY, attackCoord.posZ, entInt.getAIAgent().maxPFRange, 0);
  78.                         }
  79.                     }
  80.                 } else {
  81.                     if (!waitingForThreadPath) {
  82.                         if (pathReturned != null) {
  83.                             float dist = (float) Math.sqrt(attackCoord.getDistanceSquared(pathReturned.getFinalPathPoint().xCoord, pathReturned.getFinalPathPoint().yCoord, pathReturned.getFinalPathPoint().zCoord));
  84.                            
  85.                             System.out.println(dist);
  86.                             System.out.println(pathReturned.getCurrentPathLength());
  87.                            
  88.                             if (dist < 4F) {
  89.                                 hasPathToTarget = true;
  90.                             } else {
  91.                                 hasPathToTarget = false;
  92.                             }
  93.                            
  94.                             tryPathToTarget = false; //now that we know we must dig, logic later on must set this to true to reset the loop
  95.                         } else {
  96.                             System.out.println("no path");
  97.                             hasPathToTarget = false;
  98.                             waitingForPath = false;
  99.                         }
  100.                     }
  101.                    
  102.                    
  103.                 }
  104.             }
  105.            
  106.             if (!tryPathToTarget) {
  107.                 if (hasPathToTarget) {
  108.                     super.tick();
  109.                     return;
  110.                 } else {
  111.                     this.setJobState(EnumJobState.W1);
  112.                 }
  113.             }
  114.         } else if (this.state == EnumJobState.W1) {
  115.             if (this.entInt.getAIAgent().currentAction == EnumActState.IDLE) {
  116.                 if (isDiggableAngle()) {
  117.                     this.setJobState(EnumJobState.W2);
  118.                 } else {
  119.                     this.entInt.getAIAgent().updateWanderPath();
  120.                 }
  121.             }
  122.         } else if (this.state == EnumJobState.W2) {
  123.             //simu dig #1
  124.             if (simuDigCount == 0) {
  125.                 //strait dig test
  126.                 if (true) { //skipping a huge sensing step here
  127.                    
  128.                     //temp since W2 should set this
  129.                     bestDigCoord = ((JobGroupHorde)jm.getPrimaryJob()).attackCoord;
  130.                    
  131.                     this.setJobState(EnumJobState.W3);
  132.                 }
  133.             } else if (simuDigCount < 5) {
  134.                
  135.             }
  136.         } else if (this.state == EnumJobState.W3) {
  137.             //DIG!
  138.             entInt.getAIAgent().setState(EnumActState.WALKING);
  139.             this.walkingTimeout = 600;
  140.            
  141.             if (bestDigCoord != null && ent.getDistance(bestDigCoord.posX, bestDigCoord.posY, bestDigCoord.posZ) >= 1F) {
  142.                
  143.                 int offset = 0;
  144.                
  145.                 if (digState == EnumJobState.W1) offset = -1;
  146.                
  147.                 MovingObjectPosition aim = entInt.getAIAgent().rayTrace(1F, 1 + offset);
  148.                
  149.                 if (aim != null) {
  150.                     int id = ent.worldObj.getBlockId(aim.blockX, aim.blockY, aim.blockZ);
  151.                    
  152.                     if (id != 0) {
  153.                        
  154.                         System.out.println(ent.rotationYaw);
  155.                        
  156.                         if (ent.worldObj.getBlockTileEntity(aim.blockX, aim.blockY, aim.blockZ) == null) {
  157.                             ent.worldObj.setBlock(aim.blockX, aim.blockY, aim.blockZ, 0);
  158.  
  159.                             lastMinedCoord = new ChunkCoordinates(aim.blockX, aim.blockY, aim.blockZ);
  160.                            
  161.                             if (digState == EnumJobState.IDLE) {
  162.                                 digState = EnumJobState.W1;
  163.                             } else {
  164.                                 digState = EnumJobState.IDLE;
  165.                             }
  166.  
  167.                            
  168.                            
  169.                             ent.swingItem();
  170.                         }
  171.                     }
  172.                 } else {
  173.                    
  174.                    
  175.                    
  176.                     if (ent.onGround && ent.isCollidedHorizontally && ent.boundingBox.minY < bestDigCoord.posY) {
  177.                         //ent.jump();
  178.                     }
  179.                 }
  180.                
  181.                 if (lastMinedCoord != null) {
  182.                     entInt.getAIAgent().faceCoord(lastMinedCoord, 180, 180);
  183.                 } else {
  184.                     entInt.getAIAgent().faceCoord(bestDigCoord, 180, 180);
  185.                 }
  186.                
  187.                 ent.moveFlying((ent.worldObj.rand.nextFloat() * 0.2F) - 0.1F, 0.5F, 0.2F);
  188.                
  189.                 //System.out.println(aim);
  190.                
  191.                 entInt.getAIAgent().entityToAttack = null;
  192.             }
  193.         } else if (this.state == EnumJobState.W4) {
  194.            
  195.         }
  196.        
  197.         System.out.println(state);
  198.        
  199.         //if below
  200.     }
  201.    
  202.     public boolean isDiggableAngle() {
  203.         if (ent.posY > ((JobGroupHorde)jm.getPrimaryJob()).attackCoord.posY) {
  204.             return true;
  205.         } else if (getVerticalAngle() > 50) {
  206.             return true;
  207.         }
  208.         return false;
  209.     }
  210.    
  211.     public float getVerticalAngle() {
  212.         return 60F; //fake a safe angle for now
  213.     }
  214.    
  215.     public void setPathToEntity(PathEntity pathentity)
  216.     {
  217.         if (waitingForThreadPath) {
  218.             pathReturned = pathentity;
  219.             waitingForThreadPath = false;
  220.         } else {
  221.             entInt.getAIAgent().setPathToEntityForce(pathentity);
  222.         }
  223.     }
  224.    
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement