Corosus

Untitled

Jun 6th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package formation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.EntityLiving;
  9. import net.minecraft.util.ChunkCoordinates;
  10. import net.minecraft.util.MathHelper;
  11. import net.minecraft.util.Vec3;
  12. import CoroAI.componentAI.ICoroAI;
  13. import CoroAI.forge.CoroAI;
  14.  
  15. public class Formation {
  16.  
  17.     public double distMax = 128D;
  18.     public List<ICoroAI> listEntities = new ArrayList<ICoroAI>();
  19.     //Used for updating where the formation wants to go
  20.     public ICoroAI leader;
  21.     public EntityLiving leaderEnt;
  22.    
  23.     //Leader based stuff
  24.     public Vec3 targ = null;
  25.    
  26.     public Vec3 pos = null;
  27.     public double yaw;
  28.     //rename this to shared target, other entities can set it if its null
  29.     public EntityLiving leaderTarget;
  30.     public double smoothYaw;
  31.    
  32.     public double speedSlowest = 0.28D;
  33.     public PathNavigateFormation pathNav = null;
  34.    
  35.     public int sizeColumns = 10;
  36.     public int sizeRows = 10;
  37.    
  38.     public static Formation newFormation(ICoroAI parLeader, ICoroAI secondEnt) {
  39.         Formation fm = new Formation();
  40.         fm.joinLeader(parLeader);
  41.         if (secondEnt != null) fm.join(secondEnt);
  42.         fm.pathNav = new PathNavigateFormation(fm.leaderEnt.width, fm.leaderEnt.height, fm.leaderEnt.worldObj, 256);
  43.         System.out.println("new formation: " + fm);
  44.         return fm;
  45.     }
  46.    
  47.     public Formation() {
  48.        
  49.     }
  50.    
  51.     public void cleanup() {
  52.         listEntities.clear();
  53.     }
  54.    
  55.     //used for maintaining the active list of entities, if ones stray out and dont follow protocal and call leave, this could trim them out based on range
  56.     public void tickUpdate() {
  57.         //every n ticks
  58.         //check list for
  59.         //- distant entities
  60.         //-
  61.        
  62.         sizeColumns = 10;
  63.         sizeRows = 10;
  64.        
  65.    
  66.         if (leaderTarget != null && leaderTarget.isDead) leaderTarget = null;
  67.        
  68.         if (leaderEnt != null) {
  69.             pos = leaderEnt.getPosition(1F);
  70.             yaw = leaderEnt.rotationYaw;
  71.             if (leaderTarget == null) {
  72.                 leaderTarget = leaderEnt.getAttackTarget();
  73.             }
  74.            
  75.             float bestMove = MathHelper.wrapAngleTo180_float((float) (smoothYaw - yaw));
  76.             float camRotateSpeed = 0.6F/*bestMove * 0.03F*/;
  77.             if (camRotateSpeed < 0.03F) camRotateSpeed = 0F;
  78.             if (bestMove > camRotateSpeed * 1) {
  79.                 if (bestMove < camRotateSpeed * 2) {
  80.                     smoothYaw = yaw;
  81.                 } else {
  82.                     smoothYaw -= camRotateSpeed;
  83.                 }
  84.             } else if (bestMove < camRotateSpeed * 1) {
  85.                 if (bestMove > camRotateSpeed * 2) {
  86.                     smoothYaw = yaw;
  87.                 } else {
  88.                     smoothYaw += camRotateSpeed;
  89.                 }
  90.                
  91.             }
  92.             while (smoothYaw >= 180.0F) smoothYaw -= 360.0F;
  93.             while (smoothYaw < -180.0F) smoothYaw += 360.0F;
  94.            
  95.             if (leaderEnt.worldObj.getWorldTime() % 40 == 0) {
  96.                 for (int i = 0; i < listEntities.size(); i++) {
  97.                     ICoroAI ent = listEntities.get(i);
  98.                    
  99.                     if (ent != null) {
  100.                         if (((EntityLiving)ent).isDead || ((EntityLiving)ent).getDistance(pos.xCoord, pos.yCoord, pos.zCoord) > distMax) {
  101.                             leave(ent);
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.    
  109.     public void setDestination(ChunkCoordinates coords) {
  110.         System.out.println("TODO");
  111.     }
  112.    
  113.     //this should be called by entities already added, to get their position thats managed by this class
  114.     public Vec3 getPosition(Entity ent) {
  115.         if (listEntities.contains(ent)) {
  116.             if (pos != null) {
  117.                 int squareSize = (int) Math.sqrt(listEntities.size());
  118.                
  119.                 sizeColumns = squareSize;
  120.                
  121.                 int index = listEntities.indexOf(ent);
  122.                 int row = index / sizeColumns;
  123.                 int col = index % sizeColumns;
  124.                
  125.                 int rows = listEntities.size() / sizeColumns;
  126.                
  127.                 double spacing = 1.5D/* + Math.sin(ent.worldObj.getWorldTime() * 0.1D)*/;
  128.                
  129.                 double adjAngle = 0D;//listEntities.indexOf(ent) % 2 == 0 ? 0D : 180D;
  130.                 double dist = spacing * row - ((spacing*rows)/2);//(listEntities.indexOf(ent));
  131.                 adjAngle -= 90D;
  132.                
  133.                 //depth wise, rows
  134.                 double posX = Math.cos((-smoothYaw + adjAngle) * 0.01745329D) * dist;
  135.                 double posZ = Math.sin((-smoothYaw + adjAngle) * 0.01745329D) * dist;
  136.                
  137.                 dist = (spacing*col) - ((spacing*(sizeColumns+1))/2);
  138.                 adjAngle += 90D;
  139.                
  140.                 //length wise, colums
  141.                 posX += Math.cos((-smoothYaw + adjAngle) * 0.01745329D) * dist;
  142.                 posZ += Math.sin((-smoothYaw + adjAngle) * 0.01745329D) * dist;
  143.                
  144.                
  145.                
  146.                 posX = pos.xCoord - posX;
  147.                 double posY = (pos.yCoord/* - 0.3D - Math.sin((center.rotationPitch) / 180.0F * 3.1415927F) * dist*/);
  148.                 posZ = pos.zCoord + posZ;
  149.                
  150.                 Vec3 tryPos = Vec3.createVectorHelper(posX, posY, posZ);
  151.                
  152.                 int tryID = ent.worldObj.getBlockId((int)tryPos.xCoord, (int)tryPos.yCoord + 1, (int)tryPos.zCoord);
  153.                
  154.                 //if clear (check ent height too), if not return center formation for safety
  155.                 if (tryID == 0 || !Block.blocksList[tryID].blockMaterial.isSolid()) {
  156.                    
  157.                 } else {
  158.                     return pos;
  159.                 }
  160.                
  161.                 //to fix movehelper trying to make them jump, might need a better fix
  162.                 tryPos = Vec3.createVectorHelper(posX, ent.posY, posZ);
  163.                 return tryPos;
  164.             } else {
  165.                 //System.out.println("DO I REPEAT FOR SAME ENT?!" + ent);
  166.                 return null;
  167.             }
  168.         } else {
  169.             return null;
  170.         }
  171.     }
  172.    
  173.     public void joinLeader(ICoroAI ent) {
  174.         join(ent);
  175.         leader = ent;
  176.         leaderEnt = (EntityLiving)ent;
  177.         System.out.println("Formation: leader assigned to " + this);
  178.     }
  179.    
  180.     public void join(ICoroAI ent) {
  181.         listEntities.add(ent);
  182.         ent.getAIAgent().activeFormation = this;
  183.         System.out.println("Formation: ent joined " + this);
  184.     }
  185.    
  186.     public void leave(ICoroAI ent) {
  187.         listEntities.remove(ent);
  188.         ent.getAIAgent().activeFormation = null;
  189.         checkLeader(ent);
  190.     }
  191.    
  192.     public void checkLeader(ICoroAI ent) {
  193.         if (ent == leader && listEntities.size() > 0) {
  194.             System.out.println("Formation: new leader assigned!");
  195.             leader = listEntities.get(0);
  196.             leaderEnt = (EntityLiving)leader;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment