ofekkfir

daskjdjla

Feb 26th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 70.83 KB | None | 0 0
  1. package bots;
  2. import pirates.*;
  3. import java.util.*;
  4. public class MyBot implements PirateBot{
  5.     static Location[] enemyPiratesLocations;
  6.     static Location[] myPiratesLocations;
  7.     Pirate[] defenders;
  8.     Pirate[] attackers;
  9.     Pirate[] helpers;
  10.     static int[] capsuleStatus;
  11.     static int[] enemyUsingMotherships;
  12.     static int[] defendersNearMotherships;
  13.     static int[] howManyPiratesPushEnemy;
  14.     static int[] helpersNearCapsule;
  15.     static int pushersCount=0;
  16.     static int upHelpersCount=0;
  17.     static int williamCount=0;
  18.     static int[] piratesGoingToCapsule;
  19.     static int[] piratesPushingCapsule;
  20.     static int[] piratesPushingAstroids;
  21.     static boolean upTRUEdownFalse;
  22.     int defAmountClose=0;
  23.     int defAmountFar=0;
  24.     int countFirstDefender=0;
  25.     static int leftright=-1;
  26.     boolean AsteroidInPlace=false;
  27.     static boolean isThereAnotherCapsuleClose=false;
  28.     //Location[] astroidsLocations;
  29.     public void doTurn(PirateGame game) //The turn function
  30.     {
  31.         leftright=-1;
  32.         defAmountClose=0;
  33.         defAmountFar=0;
  34.         upTRUEdownFalse=false;
  35.         isThereAnotherCapsuleClose=false;
  36.         if(game.turn==1) {
  37.             enemyPiratesLocations=new Location[game.getAllEnemyPirates().length];
  38.             myPiratesLocations=new Location[game.getAllMyPirates().length];
  39.             capsuleStatus=new int[game.getMyCapsules().length];
  40.             piratesGoingToCapsule= new int[game.getMyCapsules().length];
  41.             howManyPiratesPushEnemy= new int[game.getAllEnemyPirates().length];
  42.             helpersNearCapsule= new int[game.getMyCapsules().length];
  43.             piratesPushingCapsule= new int[game.getMyCapsules().length];
  44.             piratesPushingAstroids=new int[game.getAllAsteroids().length];
  45.         }
  46.         pushersCount=0;
  47.         upHelpersCount=0;
  48.         williamCount=0;
  49.         if(game.turn==1)
  50.         {
  51.             for(int i=0;i<game.getAllEnemyPirates().length;i++)
  52.             {
  53.                 enemyPiratesLocations[i]=game.getAllEnemyPirates()[i].getLocation();
  54.             }
  55.             for(int i=0;i<game.getAllMyPirates().length;i++)
  56.             {
  57.                 myPiratesLocations[i]=game.getAllMyPirates()[i].getLocation();
  58.             }
  59.             for(int i=0;i<capsuleStatus.length;i++)
  60.                 capsuleStatus[i]=0;
  61.             for(int i=0;i<helpersNearCapsule.length;i++)
  62.             {
  63.                 helpersNearCapsule[i]=0;
  64.             }
  65.             enemyUsingMotherships=new int[game.getEnemyMotherships().length];//      
  66.             defendersNearMotherships= new int[game.getEnemyMotherships().length];
  67.         }
  68.         //enemy using capsule===========================
  69.  
  70.         for(int i=0;i<enemyUsingMotherships.length;i++)//
  71.         {
  72.             enemyUsingMotherships[i]=0;
  73.         }
  74.         for(Pirate enemy:game.getEnemyLivingPirates())//  :        0 ,             ,    2    3       2
  75.         {
  76.             Mothership goingToMother=whichMotherGoingTo(enemy,400,game);//the cap where enemy is going
  77.             if(goingToMother!=null)
  78.             {
  79.                 int id= goingToMother.id;
  80.                 enemyUsingMotherships[id]++;
  81.             }
  82.         }
  83.         for(int i:enemyUsingMotherships)
  84.         {
  85.  
  86.         }
  87.         for(Capsule myCapsule:game.getMyCapsules())
  88.         {
  89.             if(myCapsule.getLocation().equals(myCapsule.initialLocation))
  90.                 capsuleStatus[myCapsule.id]=0;
  91.         }
  92.         //defenders near motherships==============================
  93.         for(int i=0;i<defendersNearMotherships.length;i++)//
  94.         {
  95.             defendersNearMotherships[i]=0;
  96.         }
  97.         for(int i=0;i<howManyPiratesPushEnemy.length;i++)
  98.             howManyPiratesPushEnemy[i]=0;
  99.         for(int i=0;i<piratesGoingToCapsule.length;i++)
  100.             piratesGoingToCapsule[i]=0;
  101.         for(Capsule capsule: game.getMyCapsules())//check how many helpers near capsule
  102.         {
  103.             for(Pirate pirate:game.getMyLivingPirates())
  104.             {
  105.                 if(pirate.distance(capsule)<=game.pushDistance+game.pushRange&&!pirate.getLocation().equals(capsule.getLocation()))
  106.                     helpersNearCapsule[capsule.id]++;
  107.             }
  108.         }
  109.         int numOfCapsNearMother=0;
  110.         for(Mothership mother:game.getMyMotherships()){
  111.             numOfCapsNearMother=0;
  112.             for(Capsule capsule:game.getMyCapsules())
  113.             {
  114.                 if(capsule.distance(mother)<3000)
  115.                 {
  116.                     numOfCapsNearMother++;
  117.                 }
  118.             }
  119.             if(numOfCapsNearMother>1)
  120.                 isThereAnotherCapsuleClose=true;
  121.         }
  122.         for(int i=0;i<piratesPushingCapsule.length;i++)
  123.         {
  124.             piratesPushingCapsule[i]=0;
  125.         }
  126.         for(int i=0;i<piratesPushingAstroids.length;i++)
  127.         {
  128.             piratesPushingAstroids[i]=0;
  129.         }
  130.         //  for(Pirate pirate: game.getMyLivingPirates())
  131.         //  {
  132.         //      if(pirate.hasCapsule())
  133.         //          capsuleStatus[pirate.capsule.id]=1;
  134.         //  }
  135.  
  136.         Player player= game.getEnemy();
  137.         Player player1 = game.getMyself();
  138.         String id= player.botName;
  139.         System.out.println(id);
  140.         //System.out.println(id);
  141.         if(id.equals("26078")){
  142.             Pirate attacker1 =game.getAllMyPirates()[0];
  143.             Pirate attacker2 =game.getAllMyPirates()[1];
  144.             Pirate attacker3 =game.getAllMyPirates()[8];
  145.             Pirate defender1 =game.getAllMyPirates()[2];
  146.             Pirate defender2 =game.getAllMyPirates()[3];
  147.             Pirate defender3 =game.getAllMyPirates()[4];
  148.             Pirate helper1 = game.getAllMyPirates()[5];
  149.             Pirate helper2 = game.getAllMyPirates()[6];
  150.             Pirate helper3 = game.getAllMyPirates()[7];
  151.             for (int i = 0; i < 2; i++) {
  152.                 game.getAllMyPirates()[i].sail(closestCapsuleAttack(game.getAllMyPirates()[i], game));
  153.                 if(game.getAllMyPirates()[i].hasCapsule()==true){
  154.                     game.getAllMyPirates()[i].sail(closestMother(game, game.getAllMyPirates()[i] ));
  155.                 }
  156.             }
  157.            
  158.             attacker3.sail(closestCapsuleAttack(attacker3, game));
  159.             if(attacker3.hasCapsule()==true)
  160.             {
  161.                 attacker3.sail(closestMother(game, attacker3));
  162.             }
  163.            
  164.            
  165.         }
  166.         else{
  167.             int helpersInTeam=1;
  168.             int defendersInTeam=2;
  169.             int attackersInTeam=1;
  170.             int groupSize=4;
  171.             int defCount=0;
  172.             int helpersCount=0;
  173.             int attackersCount=0;
  174.             int numOfGroupsByLength=game.getAllMyPirates().length/groupSize;//2
  175.  
  176.             int moduluLength=game.getAllMyPirates().length%groupSize;//
  177.             int defLength=defendersInTeam*numOfGroupsByLength;
  178.             int attackersLength=attackersInTeam*numOfGroupsByLength;
  179.             int helpersLength=helpersInTeam*numOfGroupsByLength;
  180.             if(moduluLength>=1&&numOfGroupsByLength>0)
  181.             {
  182.                 helpersLength++;
  183.             }
  184.             if(moduluLength>=2&&numOfGroupsByLength>0)
  185.             {
  186.                 defLength++;
  187.             }
  188.             if(moduluLength>=3&&numOfGroupsByLength>0)
  189.             {
  190.                 defLength++;
  191.             }
  192.             if(game.getAllMyPirates().length>=1&&game.getAllMyPirates().length<4)
  193.                 attackersLength++;
  194.             if(game.getAllMyPirates().length>=2&&game.getAllMyPirates().length<4)
  195.                 defLength++;
  196.             if(game.getAllMyPirates().length>=3&&game.getAllMyPirates().length<4)
  197.                 helpersLength++;
  198.             defenders= new Pirate[defLength];
  199.             attackers= new Pirate[attackersLength];
  200.             helpers= new Pirate[helpersLength];
  201.             if(numOfGroupsByLength>0)
  202.             {
  203.                 attackers[attackersCount]=game.getAllMyPirates()[0];
  204.                 attackersCount++;
  205.                 //          System.out.println("ATTACKER");
  206.                 defenders[defCount]=game.getAllMyPirates()[1];
  207.                 defCount++;
  208.                 //          System.out.println("DEFENDER");
  209.                 helpers[helpersCount]=game.getAllMyPirates()[2];
  210.                 helpersCount++;
  211.                 //          System.out.println("HELPER");
  212.                 defenders[defCount]=game.getAllMyPirates()[3];
  213.                 defCount++;
  214.                 //          System.out.println("DEFENDER");
  215.             }
  216.             for(int i=1;i<numOfGroupsByLength;i++)//
  217.             {
  218.                 for(int k=0;k<groupSize;k++)//
  219.                 {
  220.                     if(k==0)
  221.                     {
  222.                         helpers[helpersCount]=game.getAllMyPirates()[groupSize*i+k];
  223.                         helpersCount++;
  224.                         //                  System.out.println("HELPER");
  225.                     }
  226.                     if(k==1)
  227.                     {
  228.                         defenders[defCount]=game.getAllMyPirates()[groupSize*i+k];
  229.                         defCount++;
  230.                         //                  System.out.println("DEFENDER");
  231.                     }
  232.                     if(k==2)
  233.                     {
  234.                         defenders[defCount]=game.getAllMyPirates()[groupSize*i+k];
  235.                         defCount++;
  236.                         //                  System.out.println("DEFENDER");
  237.                     }
  238.                     if(k==3)
  239.                     {
  240.                         attackers[attackersCount]=game.getAllMyPirates()[groupSize*i+k];
  241.                         attackersCount++;
  242.                         //                  System.out.println("ATTACKER");
  243.                     }
  244.                 }
  245.             }
  246.             int modulu=game.getAllMyPirates().length%groupSize;//        
  247.             if(numOfGroupsByLength>0){
  248.  
  249.                 if(modulu>=1)
  250.                 {
  251.                     helpers[helpersCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize];
  252.                     helpersCount++;
  253.                     //              System.out.println("HELPER");
  254.                 }
  255.                 if(modulu>=2)
  256.                 {
  257.                     defenders[defCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize+1];
  258.                     defCount++;
  259.                     //              System.out.println("DEFENDER");
  260.                 }
  261.                 if(modulu>=3)
  262.                 {
  263.                     defenders[defCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize+2];
  264.                     defCount++;
  265.                     //              System.out.println("DEFENDER");
  266.                 }
  267.             }
  268.             else//    
  269.             {
  270.                 if(modulu>=1)
  271.                 {
  272.                     attackers[attackersCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize];
  273.                     attackersCount++;
  274.                 }
  275.                 if(modulu>=2)
  276.                 {
  277.                     defenders[defCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize+1];
  278.                     defCount++;
  279.                 }
  280.                 if(modulu>=3)
  281.                 {
  282.                     helpers[helpersCount]=game.getAllMyPirates()[numOfGroupsByLength*groupSize+2];
  283.                     helpersCount++;
  284.                 }
  285.             }
  286.             //      System.out.println(attackers.length);
  287.             //      System.out.println(defenders.length);
  288.             //      System.out.println(helpers.length);
  289.             //ATTACKER CODE
  290.             if(attackers.length==1&&game.getMyMotherships().length==0&&pushOut(attackers[0],game)==false)
  291.             {
  292.  
  293.             }
  294.             else if(game.getMyMotherships().length>0){
  295.                 for(int i=0;i<attackers.length;i++) {
  296.                     if(attackers[i].isAlive())
  297.                         attackerAction(attackers[i],game);
  298.                 }
  299.             }
  300.             //DEFENDER CODE
  301.             if(defenders.length>0){
  302.                 Capsule enemyCapsule= closestCapsuleToEnemy(game);
  303.                 Mothership EnemyBase= relevantEnemyMothership(enemyCapsule, game);
  304.                 int enemyMoveCapsuleX= enemyCapsule.getLocation().row;
  305.                 int enemyMoveCapsuleY= enemyCapsule.getLocation().col;
  306.  
  307.                 Location EnemyBaseDef= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,600,enemyCapsule);
  308.                 Location EnemyBaseDef2= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,1050,enemyCapsule);
  309.                 int defAmountClose=0;
  310.                 int defAmountFar=0;
  311.                 for(int i=0;i<defenders.length;i++) {
  312.                     if(id.equals("25774"))
  313.                         defenderAction(defenders[i],game);
  314.                     else if (id.equals("26075"))
  315.                     {
  316.                        
  317.                     }
  318.                     else
  319.                         defenderActionReal(defenders[i],game);
  320.                     //System.out.println(defenders.length);
  321.                     /*if(defenders[i].isAlive()&&pushOut(defenders[i],game)==false&&tryPushAstroidDefenders(defenders[i],game,enemyCapsule)==false) {
  322.                 //if(numberOfDefendersNearEnemyMothership(EnemyBase, defenders)[0]<2||(numberOfDefendersNearEnemyMothership(EnemyBase, defenders)[0]<=2&&defenders[i].distance(EnemyBase)<500&&defInside<2))
  323.                 if(defAmountClose<2)
  324.                 {
  325.                     defenders[i].sail(EnemyBaseDef);
  326.                     defAmountClose++;
  327.                 }
  328.                 else if(defAmountFar<2){
  329.                     if(isCloseToEnemy(defenders[i],game,1200,enemyCapsule)) {
  330.                         defenders[i].sail(enemyCapsule.getLocation());
  331.                     }
  332.                     else
  333.                         defenders[i].sail(EnemyBaseDef2);
  334.                     defAmountFar++;
  335.                 }
  336.             }*/
  337.                 }
  338.             }
  339.             //HELPERS CODE
  340.             if(helpers.length>0){
  341.  
  342.                 for(Pirate helper:helpers)
  343.                 {
  344.                     if(helper.isAlive())
  345.                         attackerAction(helper,game);
  346.                 }
  347.                 /*Pirate[] pushers= new Pirate[helpers.length/2];
  348.                 Pirate[] upHelpers= new Pirate[helpers.length/2];
  349.                 for(int i=0;i<helpers.length/2;i++)//zoogi
  350.                 {
  351.                     pushers[i]=helpers[i];
  352.                 }
  353.                 int helperLength=helpers.length;
  354.                 if(helperLength%2==1)
  355.                     helperLength=helpers.length-1;
  356.                 for(int i=helpers.length/2;i<helperLength;i++)//zoogi
  357.                 {
  358.                     upHelpers[i-helpers.length/2]=helpers[i];
  359.                 }
  360.                 for(int i=0;i<helpers.length/2;i++)
  361.                 {                
  362.                     //Capsule closestCap=closestCapsuleAttack(upHelpers[i],game);
  363.                     Location closestCap;
  364.                     if(closestAttacker(attackers,upHelpers[i],game).isAlive()==true)//where to go pirate or capsule
  365.                         closestCap=closestAttacker(attackers,upHelpers[i],game).getLocation();
  366.                     else
  367.                         closestCap=BestCapsule(upHelpers[i],game).getLocation();
  368.                     if(shouldIpushAstroid(game,upHelpers[i],closestCapsuleToEnemy(game))==false){
  369.  
  370.                         if(canPushCapsuleNearEnemyHelperIneedToPee(upHelpers[i],game)==false)
  371.                         {
  372.                             //UpHelperCode
  373.  
  374.                             if(isCapsuleOnCapsuleInitMotherLine(game,upHelpers[i])||pushers[i].distance(closestCap)<300)
  375.                             {
  376.                                 if(pushers[i].isAlive()==false&&pushers[i].distance(closestCap)>400)
  377.                                     upHelpers[i].sail(attackerFollowerMove(closestCap.getLocation().row,closestCap.getLocation().col,game,200,upHelpers[i]));
  378.                                 else{
  379.                                 //Location center= new Location(game.rows/2,game.cols/2);
  380.                                 //double upD= attackersMoveUpper(closestCap.getLocation().row,closestCap.getLocation().col,game,800,upHelpers[i]).distance(center);
  381.                                 //double lowD= attackersMoveLower(closestCap.getLocation().row,closestCap.getLocation().col,game,800,upHelpers[i]).distance(center);
  382.                                 if(upTRUEdownFalse)
  383.                                     upHelpers[i].sail(attackersMoveUpper(closestCap.getLocation().row,closestCap.getLocation().col,game,800,upHelpers[i] ));
  384.                                 else
  385.                                     upHelpers[i].sail(attackersMoveLower(closestCap.getLocation().row,closestCap.getLocation().col,game,800,upHelpers[i] ));
  386.  
  387.                                 }  
  388.                             }    
  389.                             else
  390.                             {
  391.                                 if(upHelpers[i].distance(closestCap)<400)
  392.                                 {
  393.                                     //Capsule capsule=closestCapsuleAttack(upHelpers[i],game);
  394.                                    // if(capsuleStatus[capsule.id]==2)
  395.                                   //      upHelpers[i].sail(attackerUpMother(capsule.initialLocation.row, capsule.initialLocation.col,game, closestMother(game,upHelpers[i])));
  396.                                 //    else
  397.                                         upHelpers[i].sail(attackerFollowerMove(closestCap.getLocation().row,closestCap.getLocation().col,game,200,upHelpers[i]));
  398.                                     //helper1.sail(capsule);
  399.                                 }
  400.                             }
  401.                         }//pusherCode
  402.                         else//if upHelperPushedCapsule
  403.                         {
  404.                             capsuleStatus[closestCapsuleAttack(upHelpers[i], game).id]=3;//upHelper pushed
  405.                         }
  406.                     }
  407.                     // 0-onInit 1- nearHelpers 2-pusherPushed to upHelper 3-up helperPushed 4-pusher pushed to Mother
  408.                         int canPushCap=canPushCapsuleNearEnemy(pushers[i], game,450,upHelpers);
  409.                         System.out.println(canPushCap);
  410.                         if(shouldIpushAstroid(game,pushers[i],closestCapsuleToEnemy(game))==false&&canPushCap==0)
  411.                         {
  412.                             pushers[i].sail(attackerFollowerMove(closestCap.getLocation().row,closestCap.getLocation().col,game,200,pushers[i]));
  413.                         }
  414.                         else if(canPushCap==2)
  415.                         {
  416.                             capsuleStatus[closestCapsuleAttack(pushers[i], game).id]=2;//pusherPushed to Helper
  417.                         }
  418.                         else if(canPushCap==4)
  419.                         {
  420.                             capsuleStatus[closestCapsuleAttack(pushers[i], game).id]=4;//pusherPushed to mother
  421.                         }
  422.  
  423.                 }*/
  424.  
  425.             }
  426.         }
  427.         for(int i=0;i<game.getAllEnemyPirates().length;i++)
  428.         {
  429.             enemyPiratesLocations[i]=game.getAllEnemyPirates()[i].getLocation();
  430.         }
  431.         for(int i=0;i<game.getAllMyPirates().length;i++)
  432.         {
  433.             myPiratesLocations[i]=game.getAllMyPirates()[i].getLocation();
  434.         }
  435.         for(int i=0;i<piratesGoingToCapsule.length;i++)
  436.         {
  437.             System.out.println(piratesGoingToCapsule[i]+" "+i);
  438.         }
  439.     }
  440.  
  441.     //functions
  442.     ////////////////////////////////////////////////////
  443.     public void attackerAction(Pirate pirate,PirateGame game)
  444.     {
  445.         Capsule goingToCap=null;
  446.         Capsule pushingCap=null;
  447.         if(game.getMyCapsules().length>1)
  448.             pushingCap=closestCapsuleNotYou(pirate,game);
  449.         else
  450.             pushingCap=game.getMyCapsules()[0];
  451.         if(shouldPushCapsule(pirate, pushingCap,game)==false)
  452.         {
  453.             if(pirate.capsule==null) //if doesnt have capsule
  454.             {
  455.                 if(shouldIpushAstroid(game,pirate,closestEnemyCapsuleReal(pirate.getLocation(),game))==false){
  456.                     Capsule bestCap= BestCapsule(pirate, game);//TRY FIRST BEST CAP
  457.                     if(bestCap!=null&&piratesGoingToCapsule[bestCap.id]==0)
  458.                     {
  459.                         Location safeloc=safteyLoc(pirate.getLocation(), bestCap.getLocation(), 500, game);
  460.                         pirate.sail(safeloc);
  461.                         piratesGoingToCapsule[bestCap.id]++;
  462.                         goingToCap=bestCap;
  463.                     }
  464.                     else {
  465.                         for(Capsule capsule:game.getMyCapsules())
  466.                         {
  467.                             if(capsule.getLocation().equals(capsule.initialLocation)&&piratesGoingToCapsule[capsule.id]==0)//if no one goes to capsule and in init go to capsule
  468.                             {
  469.                                 Location safeloc=safteyLoc(pirate.getLocation(), capsule.getLocation(), 500, game);
  470.                                 pirate.sail(safeloc);
  471.                                 piratesGoingToCapsule[capsule.id]++;
  472.                                 goingToCap=capsule;
  473.  
  474.                             }  
  475.                         }
  476.                     }
  477.                     if(goingToCap==null)//if doesnt have capsule and doesnt need to get one be closest helper
  478.                     {
  479.                         //Capsule[] capsulesHelpers=new Capsule[game.getMyCapsules().length];//lowestHelpersCapsules
  480.                         //for(int i=0;i<game.getMyCapsules().length;i++)
  481.                         //{
  482.                         int minID=0;
  483.                         int minNum=100;
  484.                         //if has smallest amount be on i place and change to high number
  485.                         for(int k=0;k<helpersNearCapsule.length;k++)
  486.                         {
  487.                             if(helpersNearCapsule[k]<minNum)
  488.                             {
  489.                                 minID=k;
  490.                                 minNum=helpersNearCapsule[k];
  491.                             }
  492.                         }
  493.                         Capsule mostNeededCap=game.getMyCapsules()[minID];
  494.                         //  }
  495.                         Pirate capholder=game.getMyLivingPirates()[0];
  496.                         for(Pirate capsuleHolder:game.getMyLivingPirates())
  497.                         {
  498.                             if(capsuleHolder.capsule==mostNeededCap)
  499.                                 capholder=capsuleHolder;
  500.                         }
  501.                         Mothership mother= BestMother(pirate, game);//go to be a helper to the most needed capsule (pusher or uphelper)
  502.                         if(helpersNearCapsule[mostNeededCap.id]>=1) {//up helper
  503.  
  504.                             Location safeUpHelper= safteyLoc(capholder.getLocation(), mother.getLocation(), game.pushDistance+capholder.maxSpeed, game);
  505.                             Location safeToHelper= safteyLoc(pirate.getLocation(),safeUpHelper,game.pirateMaxSpeed,game);
  506.                             pirate.sail(safeToHelper);
  507.  
  508.                         }
  509.                         else//pusher
  510.                         {
  511.  
  512.                             Location safeUpHelper= safteyLoc(capholder.getLocation(), mother.getLocation(), game.pirateMaxSpeed+capholder.maxSpeed, game);
  513.                             Location safeToHelper= safteyLoc(pirate.getLocation(),safeUpHelper,game.pirateMaxSpeed,game);
  514.                             pirate.sail(safeToHelper);
  515.  
  516.                         }
  517.  
  518.                     }
  519.                 }
  520.             }
  521.             else//if has capsule
  522.             {
  523.                 piratesGoingToCapsule[pirate.capsule.id]++;
  524.                 Location safeloc=null;
  525.                 Mothership bestMother= BestMother(pirate, game);
  526.                 /*if(isThereAnotherCapsuleClose){
  527.                     leftright++;
  528.                     if(leftright>=1){
  529.                         leftright=1;
  530.                         safeloc=safteyLocCloseCapsule(pirate.getLocation(), bestMother.getLocation(), 300,leftright, game);
  531.                     }
  532.                     else if(leftright==0)
  533.                     {
  534.                         safeloc=safteyLocCloseCapsule(pirate.getLocation(), bestMother.getLocation(), 300,leftright, game);
  535.                     }
  536.                 }
  537.                 else
  538.                 {*/
  539.                 safeloc=safteyLoc(pirate.getLocation(), bestMother.getLocation(), pirate.maxSpeed+50, game);
  540.                 //}
  541.  
  542.                 if(closestAsteroid(pirate,game)!=null&&pirate.canPush(closestAsteroid(pirate,game)))
  543.                 {
  544.                     if((closestAsteroid(pirate,game)).distance(bestMother)>200)
  545.                         pirate.push(closestAsteroid(pirate,game),bestMother.getLocation());
  546.                     else if(pushOutAster(pirate,game)==false)
  547.                         System.out.println("THERE IS NO WAY WE PRINT DAT ");
  548.                 }
  549.                 else
  550.                     pirate.sail(safeloc);
  551.             }
  552.         }
  553.     }
  554.     public boolean shouldPushCapsule(Pirate pirate,Capsule capsule,PirateGame game)
  555.     {
  556.         int enemyCounter=0;
  557.         boolean isInDanger=false;
  558.         for(Pirate enemy:game.getEnemyLivingPirates())
  559.         {
  560.             if(enemy.distance(pirate)<=game.pushRange+game.pirateMaxSpeed)
  561.             {
  562.                 enemyCounter++;
  563.                 if(enemyCounter>=2)
  564.                     isInDanger=true;
  565.             }
  566.         }
  567.         if(isInDanger==false)
  568.             return false;
  569.         Mothership mother= BestMother(pirate, game);
  570.         //Location safteyHelper=safteyLoc(capsule.getLocation(), mother.getLocation(), game.pushDistance+game.pushRange, game);//optimal helper position
  571.         for(Pirate pirate2:game.getMyLivingPirates())//find upHelpers
  572.         {
  573.             if(isSafeLoc(pirate2.getLocation(), game)&&pirate2.distance(capsule)<=game.pushRange+game.pushDistance&&pirate2.distance(capsule)>game.pirateMaxSpeed+100&&pirate2.distance(mother)<capsule.distance(mother))//if pirates safe and in range
  574.             {
  575.                 for(Pirate allyWithCap:game.getMyLivingPirates())//check for closestPirate with capsule and in pushRange
  576.                 {
  577.                     if(allyWithCap.capsule!=null&&pirate.canPush(allyWithCap))
  578.                     {
  579.                         piratesPushingCapsule[allyWithCap.capsule.id]++;
  580.                         if(piratesPushingCapsule[allyWithCap.capsule.id]<=1){
  581.                             pirate.push(allyWithCap,pirate2.getLocation());//if close "uphelper" return true and push toward him
  582.                             return true;
  583.                         }
  584.                     }
  585.                 }
  586.             }
  587.         }
  588.         for(Pirate allyWithCap:game.getMyLivingPirates())//push towrds mother if didnt push to helper
  589.         {
  590.             if(allyWithCap.capsule!=null&&pirate.canPush(allyWithCap))
  591.             {
  592.                 Location motherSafe= safteyLoc(pirate.getLocation(),mother.getLocation(),pirate.pushDistance+allyWithCap.maxSpeed,game);
  593.                 piratesPushingCapsule[allyWithCap.capsule.id]++;
  594.                 if(piratesPushingCapsule[allyWithCap.capsule.id]<=1){
  595.                     pirate.push(allyWithCap,mother);
  596.                     return true;
  597.                 }
  598.             }
  599.         }
  600.         return false;
  601.     }
  602.  
  603.     ////////////////////////////////////////////////////
  604.     public void defenderActionReal(Pirate pirate,PirateGame game) {
  605.  
  606.         Mothership enemyBestMother=null;
  607.         for(int i=0;i<enemyUsingMotherships.length;i++)//each capsule, finds the enemyBestCapsule, the one that enemy goes to and doesnt have enough defenders
  608.         {
  609.             //Mothership mother= relevantEnemyMothership(game.getEnemyCapsules()[i],game);
  610.             if(defendersNearMotherships[i]<enemyUsingMotherships[i]*2)
  611.             {
  612.                 enemyBestMother=game.getEnemyMotherships()[i];
  613.                 defendersNearMotherships[i]++;
  614.                 break;
  615.             }
  616.         }
  617.         if(enemyBestMother==null)
  618.         {
  619.             enemyBestMother=game.getEnemyMotherships()[0];//GET BEST CAPSULES Mothership(IN NEED TO DO)
  620.             defendersNearMotherships[0]++;
  621.         }
  622.         Asteroid asteroid=closestAsteroidAlive(pirate,game);
  623.         if(pirate.equals(closestDefenderToAstroid(game))&&asteroid!=null&&asteroid.getLocation().equals(asteroid.initialLocation))//CHECK FOR ASTROID ON BASE
  624.         {
  625.             if(pirate.canPush(asteroid)&&defendersNearMotherships[enemyBestMother.id]>=1)
  626.             {
  627.                 pirate.push(asteroid,enemyBestMother);
  628.             }
  629.             else
  630.             {
  631.                 Location astroSafe= trigoLocation(asteroid.getLocation().row,asteroid.getLocation().col,enemyBestMother.getLocation(),game.pushRange+asteroid.size,90);
  632.                 pirate.sail(safteyLoc(pirate.getLocation(),astroSafe,pirate.maxSpeed,game));
  633.             }
  634.         }
  635.         else{
  636.             if(asteroid!=null&&pirate.canPush(asteroid)&&asteroid.distance(enemyBestMother)<=170&&(asteroid.direction.row!=0||asteroid.direction.col!=0))
  637.             {
  638.                 pirate.push(asteroid,asteroid);
  639.             }
  640.             else
  641.             {
  642.                 if(IsStickybombOnMe(pirate, game)==true){
  643.                     Location StickyBombSuiciderLoc=safteyLocDef(pirate.getLocation(), closestEnemyCapsuleReal(pirate.getLocation(),game).getLocation(), pirate.maxSpeed, game);
  644.                     pirate.sail(StickyBombSuiciderLoc);
  645.                 }
  646.                 else{
  647.                 Capsule enemyBestCapsule= closestEnemyCapsuleReal(pirate.getLocation(),game);
  648.                 //System.out.println(enemyBestCapsule+" enemyBestCapsule");
  649.                 int enemyMoveCapsuleX= enemyBestCapsule.getLocation().row;
  650.                 int enemyMoveCapsuleY= enemyBestCapsule.getLocation().col;
  651.                 //System.out.println(defendersNearMotherships[mother.id]+" Num OF defenders");
  652.                 if(shouldPushToAstroid(pirate,game)==false&&pushOut(pirate,game)==false)//&&shouldIpushAstroid(game,pirate,enemyBestCapsule)==false)
  653.                 {
  654.                     //System.out.println("0"+" number");
  655.                     if(DefendersPushWormhole(pirate, game, enemyBestMother)==false) {
  656.                         //  System.out.println("1"+" number");
  657.                         if(isCloseToEnemy(pirate, game, 1200, enemyBestMother))
  658.                         {
  659.                             Location safeMove=safteyLocDef(pirate.getLocation(),enemyBestCapsule.getLocation(),pirate.maxSpeed,game);
  660.                             pirate.sail(safeMove);
  661.                         }
  662.                         else
  663.                         {
  664.                             Location safeMove;
  665.                             Location defMoveloc=defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,((defendersNearMotherships[enemyBestMother.id]-1)/2)*600+600,enemyBestCapsule);
  666.                             if(asteroid==null||asteroid.distance(enemyBestMother)>2500||(asteroid.direction.row==0&&asteroid.direction.col==0))//if asteroid is far
  667.                                 safeMove=safteyLocDef(pirate.getLocation(),defMoveloc,pirate.maxSpeed,game);
  668.                             else if(pirate.pushReloadTurns<=5)//close asteroid
  669.                                     safeMove=trigoLocation(enemyBestMother.getLocation().row,enemyBestMother.getLocation().col,asteroid.getLocation(),game.pushRange+asteroid.size,180);//safeMove=safteyLocDef(pirate.getLocation(),enemyBestMother.getLocation(),pirate.maxSpeed,game);
  670.                                 else
  671.                                     safeMove=safteyLocDef(pirate.getLocation(),defMoveloc,pirate.maxSpeed,game);
  672.                             pirate.sail(safeMove);
  673.                             }
  674.                         }
  675.                     }
  676.                 }
  677.             }
  678.         }
  679.     }
  680.     ////////////////////////////////////////////////////
  681.  
  682.     public void defenderActionNoAstroid(Pirate pirate, PirateGame game) {
  683.         Capsule[] enemyCapsules=game.getEnemyCapsules();//Vectors!!!!!!!!!
  684.         Capsule enemyBestCapsule=null;
  685.         //for(int i=0;i<game.getEnemyLivingPirates().length;i++)
  686.         //  {
  687.         //if(going_to_Capsule(game.getEnemyLivingPirates()[i], i, game)!=null)
  688.         //  {
  689.         //  enemyBestCapsule=going_to_Capsule(game.getEnemyLivingPirates()[i], i, game);
  690.         //  System.out.println(enemyBestCapsule);
  691.         //      break;
  692.         //  }
  693.         //  }
  694.         if(enemyBestCapsule==null)
  695.         {
  696.             enemyBestCapsule=enemyBestCapsule(game);
  697.         }
  698.         //enemyBestCapsule= enemyBestCapsule(game);//Vectors!!!!!!!!
  699.  
  700.  
  701.         Mothership enemyMothership=relevantEnemyMothership(enemyBestCapsule, game);
  702.         int enemyMoveCapsuleX= enemyBestCapsule.getLocation().row;
  703.         int enemyMoveCapsuleY= enemyBestCapsule.getLocation().col;
  704.  
  705.         Location EnemyBaseDef= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,600,enemyBestCapsule);
  706.         Location EnemyBaseDef2= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,1050,enemyBestCapsule);
  707.         if(pirate.isAlive()&&pushOut(pirate,game)==false&&DefendersPushWormholeFake(pirate,game,enemyMothership,enemyBestCapsule)==false)
  708.         {
  709.             if(defAmountClose<2)
  710.             {
  711.                 if(isCloseToEnemyFake(pirate,game,400,enemyBestCapsule)) {
  712.                     pirate.sail(enemyBestCapsule.getLocation());
  713.                 }
  714.                 else
  715.                     pirate.sail(EnemyBaseDef);
  716.                 defAmountClose++;
  717.             }
  718.             else if(defAmountFar<2){
  719.                 if(isCloseToEnemyFake(pirate,game,1200,enemyBestCapsule)) {
  720.                     pirate.sail(enemyBestCapsule.getLocation());
  721.                 }
  722.                 else
  723.                     pirate.sail(EnemyBaseDef2);
  724.                 defAmountFar++;
  725.             }
  726.         }
  727.     }
  728.     private boolean isCloseToEnemyFake(Pirate pirate, PirateGame game,int radius, Capsule enemyCapsule)
  729.     {
  730.         Mothership Enemymother=relevantEnemyMothership(enemyCapsule, game);
  731.         for (Pirate enemy: game.getEnemyLivingPirates())
  732.         {
  733.             if(pirate.distance(enemy)<radius&&pirate.distance(Enemymother)<1300)
  734.             {
  735.                 return true;
  736.             }
  737.         }
  738.         return false;
  739.     }
  740.     public boolean DefendersPushWormholeFake(Pirate pirate, PirateGame game,Mothership Emother,Capsule eCap)
  741.     {
  742.         if(game.getAllWormholes().length==0)
  743.             return false;
  744.         for(Capsule enemyCap:game.getEnemyCapsules()){
  745.             if(enemyCap.distance(Emother)<pirate.maxSpeed*game.pushMaxReloadTurns)
  746.                 return false;
  747.         }
  748.         Wormhole wormhole=closestWormhole(pirate, game);
  749.         if(wormhole==null)
  750.             return false;
  751.         if(pushOut(pirate, game)==false) {
  752.             if(wormhole.distance(Emother)<1400&&(wormhole.getLocation().row!=0&&wormhole.getLocation().col!=0&&wormhole.getLocation().row!=game.rows&&wormhole.getLocation().col!=game.cols))
  753.             {
  754.                 if(pushOutWormToCap(pirate,game,eCap)==false)
  755.                 {
  756.                     //if(pirate.distance(closestWormhole(pirate, game))-100>pirate.maxSpeed+50)
  757.                     //  pirate.sail(closestWormhole(pirate, game));
  758.                     //  else
  759.                     //  pirate.sail(Emother);
  760.                     if(pirate.distance(closestWormhole(pirate, game))-100>pirate.maxSpeed+50)
  761.                         pirate.sail(closestWormhole(pirate, game));
  762.                     else
  763.                     {
  764.                         pirate.sail(myPiratesLocations[pirate.id]);      
  765.                     }
  766.                 }
  767.                 return true;
  768.             }
  769.             return false;
  770.         }
  771.         return true;
  772.     }
  773.     public boolean DefendersPushWormhole(Pirate pirate, PirateGame game,Mothership Emother) {
  774.         if(game.getAllWormholes().length==0)
  775.             return false;
  776.         Wormhole wormhole=closestWormhole(pirate, game);
  777.         Capsule Ecap= closestCapsuleToEnemy(game);
  778.         if(wormhole.distance(Emother)<2200&&closestEnemyCapsuleReal(pirate.getLocation(),game).distance(pirate)>1500) {
  779.             if(pushOutWormToCap(pirate,game,Ecap)==false)
  780.             {
  781.                 pirate.sail(wormhole.getLocation().towards(pirate.getLocation(),290));
  782.                 //System.out.println(trigoLocation(wormhole.getLocation().row,wormhole.getLocation().col , Emother.getLocation(), game.pushRange+0.0, 0.0)+" Trigo");
  783.             }
  784.             return true;
  785.         }
  786.         return false;
  787.     }
  788.  
  789.     public Wormhole closestWormhole(Pirate pirate, PirateGame game)//real
  790.     {
  791.         Wormhole minWorm=null;
  792.         double minDist= game.rows*game.rows+game.cols*game.cols;
  793.         for(Wormhole worm: game.getAllWormholes())
  794.         {
  795.             if(worm.distance(pirate)<minDist)
  796.             {
  797.                 minDist= worm.distance(pirate);
  798.                 minWorm= worm;
  799.             }
  800.         }
  801.         return minWorm;
  802.     }
  803.     private boolean pushOutWormToCap(Pirate pirate,PirateGame game,Capsule Ecapsule)
  804.     {
  805.         for (Wormhole wormhole: game.getAllWormholes())
  806.         {
  807.             if(pirate.canPush(wormhole)){
  808.                 pirate.push(wormhole,Ecapsule);
  809.                 return true;
  810.             }
  811.         }
  812.         return false;
  813.     }
  814.     private boolean pushOutWorm(Pirate pirate, PirateGame game)
  815.     {
  816.         for (Wormhole wormhole: game.getAllWormholes()) {
  817.             //System.out.println(pirate.pushDistance);
  818.             // Check if the pirate can push the enemy.
  819.             if (pirate.canPush(wormhole)) {
  820.                 // Push enemy!
  821.                 if(distanceFromYasharWorm(game,wormhole)==1)
  822.                 {
  823.                     Location l1= new Location(0,wormhole.getLocation().col);
  824.                     pirate.push(wormhole,l1);
  825.                     //                    System.out.println(enemy.distance(pirate)+"dist enemy pirate");
  826.                     //                    System.out.println(enemy.distance(l1)+"dist enemy line");
  827.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l1);
  828.                 }
  829.                 if(distanceFromYasharWorm(game,wormhole)==2)
  830.                 {
  831.                     Location l2= new Location(wormhole.getLocation().row,0);
  832.                     pirate.push(wormhole,l2);
  833.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l2);
  834.                 }
  835.                 if(distanceFromYasharWorm(game,wormhole)==3)
  836.                 {
  837.                     Location l3= new Location(game.rows,wormhole.getLocation().col);
  838.                     pirate.push(wormhole,l3);
  839.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l3);
  840.                 }
  841.                 if(distanceFromYasharWorm(game,wormhole)==4)
  842.                 {
  843.                     Location l4= new Location(wormhole.getLocation().row,game.cols);
  844.                     pirate.push(wormhole,l4);
  845.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l4);
  846.                 }
  847.                 // Print a message.
  848.  
  849.  
  850.                 // Did push.
  851.                 return true;
  852.             }
  853.         }
  854.         return false;
  855.     }
  856.     private double distanceFromYasharWorm(PirateGame game, Wormhole wormhole){
  857.         int mindistance=game.rows;
  858.         int distFromX= wormhole.getLocation().row;
  859.         int distFromY= wormhole.getLocation().col;
  860.         int distFromX6400=game.rows-wormhole.getLocation().row;
  861.         int distFromY6400=game.cols-wormhole.getLocation().col;
  862.         mindistance=Math.min(Math.min(distFromX, distFromY), Math.min(distFromX6400,distFromY6400));
  863.         if(mindistance==distFromX)
  864.             return 1;
  865.         if(mindistance==distFromY)
  866.             return 2;
  867.         if(mindistance==distFromX6400)
  868.             return 3;
  869.         if(mindistance==distFromY6400)
  870.             return 4;
  871.         return 1;
  872.     }
  873.     public Capsule BestCapsule(Pirate pirate,PirateGame game)
  874.     {
  875.         double CapsuleGrade=0;
  876.         double MaxGrade=0;
  877.         Capsule bestCapsule=null;
  878.         double MaxDist=Math.sqrt(game.rows*game.rows+game.cols*game.cols);
  879.         for(Capsule capsule: game.getMyCapsules())
  880.         {
  881.             if(capsule.getLocation().equals(capsule.initialLocation))//check only capsules in init
  882.             {
  883.                 CapsuleGrade=100;
  884.                 //check closest
  885.                 double distanceFromPirate= pirate.distance(capsule);
  886.                 double distanceFromClosestMother=capsule.distance(closestMother(capsule.getLocation(),game));
  887.                 int defAmount=howManyPiratesNearLocation(capsule.getLocation(),game.getEnemyLivingPirates(),1300);
  888.                 int attackersUsingCapsule=0;
  889.                 for(Mothership mother: game.getMyMotherships())
  890.                 {
  891.                     attackersUsingCapsule+=numOfPiratesOnLine(capsule.getLocation(),mother.getLocation(),attackers,600);
  892.                 }
  893.                 int helpersCount=howManyPiratesNearLocation(pirate.getLocation(),helpers,800);
  894.                 //CapsuleGrade= (1.0/distanceFromPirate)*15000+(1.0/distanceFromClosestMother)*20000+(1/defAmount)*20+(1/attackersUsingCapsule)*7.5;
  895.                 CapsuleGrade-= (distanceFromPirate*16/MaxDist)*3+(distanceFromClosestMother*8/MaxDist)*4+defAmount*8+Math.pow(attackersUsingCapsule*2,2)-helpersCount*5;
  896.                 if(CapsuleGrade>MaxGrade)
  897.                 {
  898.                     bestCapsule=capsule;
  899.                     MaxGrade=CapsuleGrade;
  900.                 }
  901.             }
  902.         }
  903.         if(bestCapsule==null)
  904.             return game.getMyCapsules()[0];
  905.         return bestCapsule;
  906.     }
  907.     public Mothership BestMother(Pirate pirate,PirateGame game)
  908.     {
  909.         double MotherGrade=0;
  910.         double MaxGrade=0;
  911.         Mothership bestMother=null;
  912.         double MaxDist=Math.sqrt(game.rows*game.rows+game.cols*game.cols);
  913.         for(Mothership mother: game.getMyMotherships())
  914.         {
  915.             MotherGrade=100;
  916.             //check closest
  917.             double distanceFromPirate= pirate.distance(mother);
  918.             //double distanceFromClosestMother=mother.distance(closestMother(capsule.getLocation(),game));
  919.             int defAmount=howManyPiratesNearLocation(mother.getLocation(),game.getEnemyLivingPirates(),1300);
  920.             int helpersCount=howManyPiratesNearLocation(pirate.getLocation(),game.getMyLivingPirates()/*helpers*/,800);
  921.             //int attackersUsingCapsule=0;
  922.             //attackersUsingCapsule+=numOfPiratesOnLine(capsule.getLocation(),mother.getLocation(),game.getMyLivingPirates()/*attackers*/,600);
  923.             MotherGrade-= ((distanceFromPirate*10/MaxDist)*3+defAmount*6-helpersCount*6);
  924.             if(MotherGrade>MaxGrade)
  925.             {
  926.                 bestMother=mother;
  927.                 MaxGrade=MotherGrade;
  928.             }
  929.         }
  930.         return bestMother;
  931.     }
  932.     public int numOfPiratesOnLine(Location startingLoc, Location endingLoc, Pirate[] pirates,int radius)
  933.     {
  934.         //get y=mx+n
  935.         double m;
  936.         if(startingLoc.row-endingLoc.row==0)
  937.             m=0;
  938.         else
  939.             m= ((startingLoc.col-endingLoc.col)/(startingLoc.row-endingLoc.row));//shipooa(m)
  940.         double n= startingLoc.col-m*startingLoc.row;
  941.         int piratesCount=0;
  942.         for(Pirate pirate: pirates)
  943.         {
  944.             double Distance= Math.abs(-m*pirate.getLocation().row+pirate.getLocation().col-n)/Math.sqrt(m*m+1);
  945.             if(Distance<radius&&pirate.distance(endingLoc)<startingLoc.distance(endingLoc))
  946.                 piratesCount++;
  947.         }
  948.         return piratesCount;
  949.     }
  950.     private boolean tryPushAstroidAttack(Pirate pirate, PirateGame game,Mothership mother,Capsule capsule)
  951.     {
  952.         for (Asteroid asteroid : game.getLivingAsteroids()) {
  953.             if (pirate.canPush(asteroid))
  954.             {
  955.                 if(asteroid.distance(mother)<pirate.distance(mother)&&pirate.capsule!=null)
  956.                 {
  957.                     if(numOfPiratesOnLine(pirate.getLocation(),mother.getLocation(),game.getMyLivingPirates(),600)==1)
  958.                     {
  959.                         pirate.push(asteroid, mother);
  960.                         return true;
  961.                     }
  962.                     else
  963.                     {
  964.                         pushOutAster(pirate,game);
  965.                         return true;
  966.                     }
  967.                 }
  968.                 else if(pirate.capsule==null)
  969.                 {
  970.                     if(numOfPiratesOnLine(pirate.getLocation(),capsule.initialLocation,game.getMyLivingPirates(),600)==1)
  971.                     {
  972.                         pirate.push(asteroid, capsule);
  973.                         return true;
  974.                     }
  975.                     else
  976.                     {
  977.                         pushOutAster(pirate,game);
  978.                         return true;
  979.                     }
  980.                 }
  981.             }
  982.         }
  983.         return false;
  984.     }
  985.     public int howManyPiratesNearLocation(Location l1, Pirate[] pirates,int radius)
  986.     {
  987.         int helpersCount=0;
  988.         for(Pirate pirate2:pirates)
  989.         {
  990.             if(l1.distance(pirate2)<radius)
  991.                 helpersCount++;
  992.         }
  993.         return helpersCount;
  994.     }
  995.     public int howManyPiratesNearLocationAttacker(Location l1, Pirate[] pirates, int radius,Mothership mother)
  996.     {
  997.         int enemyCount=0;
  998.         for(Pirate pirate2:pirates)
  999.         {
  1000.             if(l1.distance(pirate2)<radius&&l1.distance(mother)>pirate2.distance(mother))
  1001.                 enemyCount++;
  1002.         }
  1003.         return enemyCount;
  1004.     }
  1005.     public Pirate closestPirate(Location l1, Pirate[] pirates,PirateGame game)
  1006.     {
  1007.         Pirate minPirate=null;
  1008.         int minDist= game.rows*game.rows+game.cols*game.cols;
  1009.         for(Pirate pirate:pirates)
  1010.         {
  1011.             if(pirate.distance(l1)<minDist&&!pirate.getLocation().equals(l1))
  1012.                 minPirate=pirate;
  1013.         }
  1014.         return minPirate;
  1015.     }
  1016.     public Mothership closestMother(Location distFrom, PirateGame game)
  1017.     {
  1018.         Mothership minMother=null;
  1019.         int minDist=game.rows*game.rows+game.cols*game.cols;
  1020.         for(Mothership mother: game.getMyMotherships())
  1021.         {
  1022.             if(distFrom.distance(mother)<minDist)
  1023.             {
  1024.                 minDist=distFrom.distance(mother);
  1025.                 minMother=mother;
  1026.             }
  1027.         }
  1028.         return minMother;
  1029.     }
  1030.  
  1031.     private int canPushCapsuleNearEnemy(Pirate pirate, PirateGame game,int radius,Pirate[] upHelpers){
  1032.         Capsule capsule= closestCapsuleAttack(pirate, game);
  1033.         int enemyCounter=0;
  1034.         for (Pirate enemy: game.getEnemyLivingPirates())
  1035.         {
  1036.             if(enemy.distance(capsule)<radius&&enemy.pushReloadTurns<=1)
  1037.             {
  1038.                 enemyCounter++;
  1039.                 if(enemyCounter>=2)
  1040.                     return tryPushCapsuleStart(pirate,game,upHelpers);
  1041.             }
  1042.         }
  1043.         return 0;
  1044.     }
  1045.     private int tryPushCapsuleStart(Pirate pusher, PirateGame game,Pirate[] upHelpers)// 0-onInit 1- nearHelpers 2-pusherPushed to upHelper 3-up helperPushed 4-pusher pushed to Mother
  1046.     {
  1047.         Pirate helper1= closestUpHelper(game,pusher,upHelpers);//helper1
  1048.         //Pirate helper2= game.getAllMyPirates()[1];//helper2
  1049.         int enemyCounter=0;
  1050.         for (Pirate ally: game.getMyLivingPirates()){
  1051.             for(Pirate enemy: game.getEnemyLivingPirates()){
  1052.                 if(ally.capsule!=null&&enemy.canPush(ally)){
  1053.                     enemyCounter++;
  1054.                     if(enemyCounter>=2)
  1055.                         return 0;
  1056.                 }
  1057.             }
  1058.             if(helper1!=null&&helper1.distance(ally)<=game.pushDistance+game.pirateMaxSpeed&&ally.capsule!=null&&pusher.canPush(ally))//if upHelpers canpush
  1059.             {
  1060.                 pusher.push(ally,helper1.getLocation());
  1061.                 return 2;
  1062.             }
  1063.             else if(ally.capsule!=null&&pusher.canPush(ally)){//pushed to mother
  1064.                 pusher.push(ally,closestMother(game, pusher));
  1065.                 return 4;
  1066.             }
  1067.         }
  1068.         return 0;
  1069.     }//end of tryPushCapsuleStart
  1070.  
  1071.     public boolean shouldIpushAstroid(PirateGame game,Pirate pirate,Capsule enemyCapsule)
  1072.     {
  1073.         Location defRow1;
  1074.         if(game.getEnemyCapsules().length==0)
  1075.             return false;
  1076.         Location EnemyBase= relevantEnemyMothership(enemyCapsule, game).getLocation();
  1077.         double DistanceCapMother= enemyCapsule.distance(EnemyBase);
  1078.         if(game.getLivingAsteroids().length==0)
  1079.             return false;
  1080.         if(enemyCapsule.getLocation().equals(enemyCapsule.initialLocation))
  1081.             return false;
  1082.         Asteroid aster= game.getLivingAsteroids()[0];
  1083.         for(int i=(int)(DistanceCapMother);i>0;i-=game.pirateMaxSpeed) {
  1084.             int enemyY= enemyCapsule.getLocation().col;
  1085.             int enemyX= enemyCapsule.getLocation().row;
  1086.             double zavit=(enemyY-EnemyBase.col+0.0)/(enemyX-EnemyBase.row);
  1087.             double zavitEnemyBase= Math.atan(zavit);
  1088.             int radius= i;
  1089.             if(enemyX<EnemyBase.row)
  1090.             {
  1091.                 zavitEnemyBase=Math.PI+zavitEnemyBase;
  1092.             }
  1093.             int xRet=0;
  1094.             xRet= (int)(EnemyBase.row+radius*Math.cos(zavitEnemyBase));
  1095.             int yRet=0;
  1096.             yRet= (int)(EnemyBase.col+radius*Math.sin(zavitEnemyBase));
  1097.             defRow1= new Location(xRet,yRet);
  1098.             int Dcapsule=(int)(Math.sqrt(Math.pow(defRow1.row-enemyCapsule.getLocation().row, 2)+Math.pow(defRow1.col-enemyCapsule.getLocation().col, 2)));
  1099.             int Daster=(int)(Math.sqrt(Math.pow(defRow1.row-aster.getLocation().row, 2)+Math.pow(defRow1.col-aster.getLocation().col, 2))-game.pushRange);
  1100.             int capMaxSpeed= game.pirateMaxSpeed;
  1101.             int astroidMaxSpeed= aster.speed;
  1102.             int capTurns= Dcapsule/capMaxSpeed;
  1103.             int astroidTurns= Daster/astroidMaxSpeed;
  1104.             if(capTurns==astroidTurns&&pirate.canPush(aster))
  1105.             {
  1106.                 Location l1= new Location(defRow1.row,defRow1.col);
  1107.                 pirate.push(aster, l1);
  1108.                 return true;
  1109.             }
  1110.         }
  1111.         return false;
  1112.     }
  1113.  
  1114.     private Pirate closestUpHelper(PirateGame game, Pirate pirate,Pirate[] upHelpers)
  1115.     {
  1116.         Pirate minPirate=null;
  1117.         int minDis= game.rows*game.rows+game.cols*game.cols;
  1118.         for(int i=0;i<upHelpers.length;i++)
  1119.         {
  1120.             if(upHelpers[i].distance(pirate)<minDis)
  1121.             {
  1122.                 minDis=upHelpers[i].distance(pirate);
  1123.                 minPirate=upHelpers[i];
  1124.             }
  1125.         }
  1126.         return minPirate;
  1127.     }
  1128.     private Location attackerFollowerMove(double capsuleX, double capsuleY,PirateGame game,double radius,Pirate pirate)
  1129.     {
  1130.         radius=game.pirateMaxSpeed;
  1131.         Location mother= closestMother(game, pirate).getLocation();
  1132.         double zavit=(capsuleY-mother.col)/(capsuleX-mother.row);
  1133.         double zavitEnemyBase;
  1134.         zavitEnemyBase= (Math.atan(zavit));
  1135.         if(capsuleX<mother.row)
  1136.         {
  1137.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1138.         }
  1139.         int xRet=0;
  1140.         xRet= (int)(capsuleX-radius*Math.cos(zavitEnemyBase));
  1141.         int yRet=0;
  1142.         yRet= (int)(capsuleY-radius*Math.sin(zavitEnemyBase));
  1143.  
  1144.         Location defRow1= new Location(xRet,yRet);
  1145.         return defRow1;
  1146.     }
  1147.     private Location attackersMoveUpper(double capsuleX, double capsuleY,PirateGame game,double radius,Pirate pirate){
  1148.  
  1149.         //radius=pirate.pushDistance+game.pirateMaxSpeed;
  1150.         radius= 743.303;
  1151.         Location mother= closestMother(game, pirate).getLocation();
  1152.         double zavit=(capsuleY-mother.col)/(capsuleX-mother.row);
  1153.         double zavitEnemyBase=(Math.atan(zavit)+Math.asin(550.0/800));
  1154.  
  1155.         if(capsuleX<mother.row)
  1156.         {
  1157.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1158.         }
  1159.         int xRet=0;
  1160.         xRet= (int)(capsuleX-radius*Math.cos(zavitEnemyBase));
  1161.         int yRet=0;
  1162.         yRet= (int)(capsuleY-radius*Math.sin(zavitEnemyBase));
  1163.  
  1164.         Location defRow1= new Location(xRet,yRet);
  1165.  
  1166.         return defRow1;
  1167.     }
  1168.     private Location attackersMoveLower(double capsuleX, double capsuleY,PirateGame game,double radius,Pirate pirate){
  1169.  
  1170.         //radius=pirate.pushDistance+game.pirateMaxSpeed;
  1171.         radius= 743.303;
  1172.         Location mother= closestMother(game, pirate).getLocation();
  1173.         double zavit=(capsuleY-mother.col)/(capsuleX-mother.row);
  1174.         double zavitEnemyBase=(Math.atan(zavit)-Math.asin(550.0/800));
  1175.  
  1176.         if(capsuleX<mother.row)
  1177.         {
  1178.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1179.         }
  1180.         int xRet=0;
  1181.         xRet= (int)(capsuleX-radius*Math.cos(zavitEnemyBase));
  1182.         int yRet=0;
  1183.         yRet= (int)(capsuleY-radius*Math.sin(zavitEnemyBase));
  1184.  
  1185.         Location defRow1= new Location(xRet,yRet);
  1186.  
  1187.         return defRow1;
  1188.     }
  1189.     private boolean isCapsuleOnCapsuleInitMotherLine(PirateGame game,Pirate pirate){
  1190.         Mothership mother= closestMother(game,pirate);
  1191.         Capsule capsule= closestCapsule(pirate,game);
  1192.         double shipoaCapMother;
  1193.         if(capsule.getLocation().col-mother.getLocation().col==0)
  1194.             shipoaCapMother=0;
  1195.         else
  1196.             shipoaCapMother=(capsule.getLocation().row-mother.getLocation().row)/(capsule.getLocation().col-mother.getLocation().col);
  1197.         double shipoaCapInitMother=(capsule.initialLocation.row-mother.getLocation().row)/(capsule.initialLocation.col-mother.getLocation().col);
  1198.         if(Math.abs(shipoaCapInitMother-shipoaCapMother)<0.1){
  1199.             return true;
  1200.         }
  1201.         return false;
  1202.     }
  1203.  
  1204.     private boolean canPushCapsuleNearEnemyHelperIneedToPee(Pirate pirate, PirateGame game){
  1205.         Capsule capsule= closestCapsuleAttack(pirate, game);
  1206.         int enemyCounter=0;
  1207.         for (Pirate enemy: game.getEnemyLivingPirates())
  1208.         {
  1209.             if((enemy.distance(capsule)<450&&enemy.pushReloadTurns<=1)||(capsule.distance(closestMother(game, pirate))<=800))
  1210.             {
  1211.                 enemyCounter++;
  1212.                 if(enemyCounter>=2&&tryPushCapsuleToMother(pirate,game)==true)
  1213.                     return true;
  1214.             }
  1215.         }
  1216.         return false;
  1217.     }
  1218.     private boolean tryPushCapsuleToMother(Pirate pirate, PirateGame game)
  1219.     {
  1220.         Location l3=new Location(0,0);
  1221.         // Go over all enemies.
  1222.         Mothership mother= closestMother(game, pirate);
  1223.         l3=mother.getLocation();
  1224.         for (Pirate ally: game.getMyLivingPirates()) {
  1225.             // Check if the pirate can push the enemy.
  1226.             if (ally.capsule!=null&&pirate.canPush(ally))
  1227.             {
  1228.                 // Push enemy!
  1229.  
  1230.                 pirate.push(ally, l3);
  1231.  
  1232.                 // Print a message.
  1233.                 //              System.out.println("pirate " + pirate + " pushes " + ally + " towards " + ally.initialLocation);
  1234.  
  1235.                 // Did push.
  1236.                 return true;
  1237.             }
  1238.         }
  1239.  
  1240.         // Didn't push.
  1241.         return false;
  1242.     }
  1243.     private Capsule closestCapsule(Pirate pirate,PirateGame game)
  1244.     {
  1245.         int capDist=pirate.distance(game.getMyCapsules()[0]);
  1246.         Capsule minCap=game.getMyCapsules()[0];
  1247.         for(int i=0;i<game.getMyCapsules().length;i++)
  1248.         {
  1249.             if(isArmedCapsule(game.getMyCapsules()[i],game)==false)
  1250.             {
  1251.                 if(capDist>pirate.distance(game.getMyCapsules()[i]))
  1252.                 {
  1253.                     minCap=game.getMyCapsules()[i];
  1254.                     capDist=pirate.distance(game.getMyCapsules()[i]);
  1255.                 }
  1256.             }
  1257.         }
  1258.         return minCap;
  1259.     }
  1260.     private Capsule closestEnemyCapsule(Pirate pirate,PirateGame game,int half)
  1261.     {
  1262.         int capDist=pirate.distance(game.getEnemyCapsules()[0]);
  1263.         Capsule minCap=game.getEnemyCapsules()[0];
  1264.         for(int i=0;i<game.getEnemyCapsules().length;i++)
  1265.         {
  1266.             if(capDist>pirate.distance(game.getEnemyCapsules()[i]))
  1267.             {
  1268.                 if(half==1&&game.getEnemyCapsules()[i].getLocation().col<game.cols/2)
  1269.                 {
  1270.                     minCap=game.getEnemyCapsules()[i];
  1271.                     capDist=pirate.distance(game.getEnemyCapsules()[i]);
  1272.                 }
  1273.                 if(half==2&&game.getEnemyCapsules()[i].getLocation().col>game.cols/2)
  1274.                 {
  1275.                     minCap=game.getEnemyCapsules()[i];
  1276.                     capDist=pirate.distance(game.getEnemyCapsules()[i]);
  1277.                 }
  1278.             }
  1279.         }
  1280.  
  1281.         return minCap;
  1282.     }
  1283.     private Pirate closestAttacker(Pirate[] attackers,Pirate helper,PirateGame game)
  1284.     {
  1285.         int attDist=game.rows*game.rows+game.cols*game.cols;
  1286.         Pirate minAttacker=null;
  1287.         for(int i=0;i<attackers.length;i++)
  1288.         {
  1289.             if(attDist>helper.distance(attackers[i]))
  1290.             {
  1291.                 minAttacker=attackers[i];
  1292.                 attDist=helper.distance(attackers[i]);
  1293.             }
  1294.         }
  1295.         return minAttacker;
  1296.     }
  1297.     private boolean isArmedCapsule(Capsule capsule,PirateGame game)
  1298.     {
  1299.         int PeopleCount=0;
  1300.         for(Pirate ally : game.getMyLivingPirates())
  1301.         {
  1302.             if(capsule.distance(ally)<1000)
  1303.                 PeopleCount++;
  1304.         }
  1305.         if(PeopleCount>=2)
  1306.             return true;
  1307.         return false;
  1308.  
  1309.     }
  1310.     private boolean isThereAnotherDefenderPushing(Pirate enemy, Pirate defender)
  1311.     {
  1312.  
  1313.         for(Pirate pirate:defenders)
  1314.         {
  1315.             if(!defender.equals(pirate)&&pirate.canPush(enemy))
  1316.                 return true;
  1317.         }
  1318.         return false;
  1319.     }
  1320.     private boolean pushOut(Pirate pirate, PirateGame game)
  1321.     {
  1322.         for (Pirate enemy: game.getEnemyLivingPirates()) {
  1323.  
  1324.             if (pirate.canPush(enemy)&&enemy.capsule!=null&&isThereAnotherDefenderPushing(enemy,pirate)) {
  1325.  
  1326.                 howManyPiratesPushEnemy[enemy.id]++;
  1327.                 if(howManyPiratesPushEnemy[enemy.id]<=enemy.numPushesForCapsuleLoss){
  1328.                     if(distanceFromYashar(enemy,game)==1)
  1329.                     {
  1330.                         Location l1= new Location(0,enemy.getLocation().col);
  1331.                         pirate.push(enemy,l1);
  1332.  
  1333.                     }
  1334.                     if(distanceFromYashar(enemy,game)==2)
  1335.                     {
  1336.                         Location l2= new Location(enemy.getLocation().row,0);
  1337.                         pirate.push(enemy,l2);
  1338.                     }
  1339.                     if(distanceFromYashar(enemy,game)==3)
  1340.                     {
  1341.                         Location l3= new Location(game.rows,enemy.getLocation().col);
  1342.                         pirate.push(enemy,l3);
  1343.  
  1344.                     }
  1345.                     if(distanceFromYashar(enemy,game)==4)
  1346.                     {
  1347.                         Location l4= new Location(enemy.getLocation().row,game.cols);
  1348.                         pirate.push(enemy,l4);
  1349.  
  1350.                     }
  1351.                     // Print a message.
  1352.  
  1353.  
  1354.                     // Did push.
  1355.                     return true;
  1356.                 }
  1357.             }
  1358.         }
  1359.         return false;
  1360.     }
  1361.  
  1362.     private double distanceFromYashar(Pirate pirate,PirateGame game){
  1363.         int mindistance=game.rows;
  1364.         int distFromX= pirate.getLocation().row;
  1365.         int distFromY= pirate.getLocation().col;
  1366.         int distFromX6400=game.rows-pirate.getLocation().row;
  1367.         int distFromY6400=game.cols-pirate.getLocation().col;
  1368.         mindistance=Math.min(Math.min(distFromX, distFromY), Math.min(distFromX6400,distFromY6400));
  1369.         if(mindistance==distFromX)
  1370.             return 1;
  1371.         if(mindistance==distFromY)
  1372.             return 2;
  1373.         if(mindistance==distFromX6400)
  1374.             return 3;
  1375.         if(mindistance==distFromY6400)
  1376.             return 4;
  1377.         return 1;
  1378.     }
  1379.  
  1380.     private Mothership relevantEnemyMothership(Capsule enemyCapsule, PirateGame game) {
  1381.         Mothership minBase=null;
  1382.         int minDistance=game.rows*game.rows+game.cols*game.cols;
  1383.         for(int i=0;i<game.getEnemyMotherships().length;i++)
  1384.         {
  1385.             if(game.getEnemyMotherships()[i].distance(enemyCapsule)<minDistance)
  1386.             {
  1387.                 minDistance=game.getEnemyMotherships()[i].distance(enemyCapsule);
  1388.                 minBase=game.getEnemyMotherships()[i];
  1389.             }
  1390.         }
  1391.         return minBase;
  1392.     }
  1393.     private Capsule closestCapsuleToEnemy(PirateGame game)
  1394.     {
  1395.         int minDis=game.rows*game.rows+game.cols*game.cols;
  1396.         Capsule minCapsule=null;
  1397.         if(game.getEnemyCapsules().length==0)
  1398.             return null;
  1399.         if(game.getEnemyLivingPirates().length>0){
  1400.             for(int i=0;i<game.getEnemyCapsules().length;i++) {
  1401.                 for(Pirate enemy: game.getEnemyLivingPirates())
  1402.                 {
  1403.                     if(enemy.distance(game.getEnemyCapsules()[i])<minDis)
  1404.                     {
  1405.                         minDis=enemy.distance(game.getEnemyCapsules()[i]);
  1406.                         minCapsule=game.getEnemyCapsules()[i];
  1407.                     }
  1408.                 }
  1409.             }
  1410.         }
  1411.         else
  1412.             minCapsule=game.getEnemyCapsules()[0];
  1413.         return minCapsule;
  1414.     }
  1415.  
  1416.     private int[] numberOfDefendersNearEnemyMothership(Mothership EnemyBase, Pirate[] defenders) {
  1417.         int[] count= new int[2];
  1418.         int countClose=0;
  1419.         int countFar=0;
  1420.         for(int i=0;i<defenders.length;i++) {
  1421.             if(defenders[i].distance(EnemyBase)<=500) {
  1422.                 countClose++;
  1423.             }
  1424.             if(defenders[i].distance(EnemyBase)<1300&&defenders[i].distance(EnemyBase)>500) {
  1425.                 countFar++;
  1426.             }
  1427.         }
  1428.         count[0]=countClose;
  1429.         count[1]=countFar;
  1430.         return count;
  1431.     }
  1432.     private Location defMove(double enemyX, double enemyY,PirateGame game,double radius, Capsule enemyCapsule){
  1433.         Location EnemyBase= relevantEnemyMothership(enemyCapsule, game).getLocation();
  1434.         double zavit=(enemyY-EnemyBase.col)/(enemyX-EnemyBase.row);
  1435.         double zavitEnemyBase= Math.atan(zavit);
  1436.         if(enemyX<EnemyBase.row)
  1437.         {
  1438.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1439.         }
  1440.         int xRet=0;
  1441.         xRet= (int)(EnemyBase.row+radius*Math.cos(zavitEnemyBase));
  1442.         int yRet=0;
  1443.         yRet= (int)(EnemyBase.col+radius*Math.sin(zavitEnemyBase));
  1444.  
  1445.         Location defRow1= new Location(xRet,yRet);
  1446.         return defRow1;
  1447.     }
  1448.     private boolean isCloseToEnemy(Pirate pirate, PirateGame game,int radius, Mothership mother)
  1449.     {
  1450.         for (Pirate enemy: game.getEnemyLivingPirates())
  1451.         {
  1452.             if(pirate.distance(enemy)<radius&&pirate.distance(mother)<1500&&enemy.capsule!=null)
  1453.             {
  1454.                 return true;
  1455.             }
  1456.         }
  1457.         return false;
  1458.     }
  1459.  
  1460.     private Mothership closestMother(PirateGame game,Pirate pirate)
  1461.     {
  1462.         Mothership minMother=null;
  1463.         int minDis= game.rows*game.rows;
  1464.         for(int i=0;i<game.getMyMotherships().length;i++)
  1465.         {
  1466.             if(game.getMyMotherships()[i].distance(pirate)<minDis)
  1467.             {
  1468.                 minDis=game.getMyMotherships()[i].distance(pirate);
  1469.                 minMother=game.getMyMotherships()[i];
  1470.             }
  1471.         }
  1472.         return minMother;
  1473.     }
  1474.     private Capsule closestCapsuleAttack(Pirate pirate,PirateGame game)
  1475.     {
  1476.         int capDist=game.rows*game.cols;
  1477.         Capsule minCap=null;
  1478.         for(int i=0;i<game.getMyCapsules().length;i++)
  1479.         {
  1480.             if(capDist>pirate.distance(game.getMyCapsules()[i]))
  1481.             {
  1482.  
  1483.                 minCap=game.getMyCapsules()[i];
  1484.                 capDist=pirate.distance(game.getMyCapsules()[i]);
  1485.  
  1486.             }
  1487.  
  1488.         }
  1489.         return minCap;
  1490.     }
  1491.     private boolean IsDefenderOnCapsuleMotherLine(Capsule capsule,Mothership mother,Pirate pirate){
  1492.         double shipoaCapMother=(capsule.getLocation().row-mother.getLocation().row)/(capsule.getLocation().col-mother.getLocation().col);
  1493.         double shipoaaHelperCapsule=(pirate.getLocation().row-capsule.getLocation().row)/(pirate.getLocation().col-capsule.getLocation().col);
  1494.         if(Math.abs(shipoaaHelperCapsule-shipoaCapMother)<0.1){
  1495.             return true;
  1496.         }
  1497.         return false;
  1498.     }
  1499.     private boolean tryPushAstroidAttack2(Pirate pirate, PirateGame game, Mothership mother,Capsule capsule)
  1500.     {
  1501.         for (Asteroid asteroid : game.getLivingAsteroids()) {
  1502.             // Check if the pirate can push the asteroid
  1503.             if (pirate.canPush(asteroid)) {
  1504.                 // Push asteroid!
  1505.                 if(asteroid.distance(mother)<pirate.distance(mother)){
  1506.                     pirate.push(asteroid, mother);
  1507.                 }
  1508.                 else
  1509.                 {
  1510.                     if(pirate.capsule==null){
  1511.                         int randomX= (int)(Math.random()*game.rows);
  1512.                         int randomY= (int)(Math.random()*pirate.getLocation().col);
  1513.                         Location loc= new Location(pirate.getLocation().row,0);
  1514.                         pirate.push(asteroid, loc);
  1515.                     }
  1516.                 }
  1517.                 // Print a message
  1518.                 //System.out.println("pirate " + pirate + " pushes " + asteroid + " towards " + game.getEnemyCapsules()[0]);
  1519.  
  1520.                 // Did push
  1521.                 return true;
  1522.             }
  1523.         }
  1524.         return false;
  1525.     }
  1526.     private boolean tryPushAstroidDefenders(Pirate pirate, PirateGame game, Capsule capsule)
  1527.     {
  1528.         if( game.getLivingAsteroids().length==0)
  1529.             return false;
  1530.         for (Asteroid asteroid : game.getLivingAsteroids()) {
  1531.             // Check if the pirate can push the asteroid
  1532.             if (pirate.canPush(asteroid)) {
  1533.                 // Push asteroid!
  1534.  
  1535.                 pirate.push(asteroid, capsule);
  1536.  
  1537.                 // Print a message
  1538.                 //                System.out.println("pirate " + pirate + " pushes " + asteroid + " towards " + game.getEnemyCapsules()[0]);
  1539.  
  1540.                 // Did push
  1541.                 return true;
  1542.             }
  1543.         }
  1544.         return false;
  1545.     }
  1546.     private boolean isCloseToAstroid(Pirate pirate, PirateGame game,Mothership mother)
  1547.     {
  1548.         for (Asteroid asteroid : game.getLivingAsteroids()) {
  1549.             // Check if the pirate can push the asteroid
  1550.             if (asteroid.distance(pirate)<650&&asteroid.distance(mother)<pirate.distance(mother)&&pirate.capsule!=null) {
  1551.                 // Push asteroid!
  1552.                 //pirate.push(asteroid, capsule);
  1553.  
  1554.                 // Print a message
  1555.                 //System.out.println("pirate " + pirate + " pushes " + asteroid + " towards " + game.getEnemyCapsules()[0]);
  1556.  
  1557.                 // Did push
  1558.                 return true;
  1559.             }
  1560.             if(asteroid.distance(pirate)<650&&asteroid.distance(mother)>pirate.distance(mother)&&pirate.capsule==null)
  1561.                 return true;
  1562.         }
  1563.         return false;
  1564.     }
  1565.     private boolean pushOutAster(Pirate pirate, PirateGame game)
  1566.     {
  1567.         for (Asteroid asteroid: game.getLivingAsteroids()) {
  1568.             //System.out.println(pirate.pushDistance);
  1569.             // Check if the pirate can push the enemy.
  1570.             if (pirate.canPush(asteroid)) {
  1571.                 // Push enemy!
  1572.                 if(distanceFromYasharAster(game,asteroid)==1)
  1573.                 {
  1574.                     Location l1= new Location(0,asteroid.getLocation().col);
  1575.                     pirate.push(asteroid,l1);
  1576.                     //                    System.out.println(enemy.distance(pirate)+"dist enemy pirate");
  1577.                     //                    System.out.println(enemy.distance(l1)+"dist enemy line");
  1578.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l1);
  1579.                 }
  1580.                 if(distanceFromYasharAster(game,asteroid)==2)
  1581.                 {
  1582.                     Location l2= new Location(asteroid.getLocation().row,0);
  1583.                     pirate.push(asteroid,l2);
  1584.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l2);
  1585.                 }
  1586.                 if(distanceFromYasharAster(game,asteroid)==3)
  1587.                 {
  1588.                     Location l3= new Location(game.rows,asteroid.getLocation().col);
  1589.                     pirate.push(asteroid,l3);
  1590.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l3);
  1591.                 }
  1592.                 if(distanceFromYasharAster(game,asteroid)==4)
  1593.                 {
  1594.                     Location l4= new Location(asteroid.getLocation().row,game.cols);
  1595.                     pirate.push(asteroid,l4);
  1596.                     //                    System.out.println("pirate " + pirate + " pushes " + enemy + " towards " + l4);
  1597.                 }
  1598.                 // Print a message.
  1599.  
  1600.  
  1601.                 // Did push.
  1602.                 return true;
  1603.             }
  1604.         }
  1605.         return false;
  1606.     }
  1607.     private double distanceFromYasharAster(PirateGame game, Asteroid asteroid){
  1608.         int mindistance=game.rows;
  1609.         int distFromX= asteroid.getLocation().row;
  1610.         int distFromY= asteroid.getLocation().col;
  1611.         int distFromX6400=game.rows-asteroid.getLocation().row;
  1612.         int distFromY6400=game.cols-asteroid.getLocation().col;
  1613.         mindistance=Math.min(Math.min(distFromX, distFromY), Math.min(distFromX6400,distFromY6400));
  1614.         if(mindistance==distFromX)
  1615.             return 1;
  1616.         if(mindistance==distFromY)
  1617.             return 2;
  1618.         if(mindistance==distFromX6400)
  1619.             return 3;
  1620.         if(mindistance==distFromY6400)
  1621.             return 4;
  1622.         return 1;
  1623.     }
  1624.     private boolean tryPush(Pirate pirate,PirateGame game)
  1625.     {
  1626.         Location l1= new Location(pirate.getLocation().row,6400);
  1627.         for(Wormhole worm:game.getAllWormholes())
  1628.         {
  1629.             if(pirate.canPush(worm))
  1630.             {
  1631.                 pirate.push(worm,l1);
  1632.                 return true;
  1633.             }
  1634.         }
  1635.         return false;
  1636.     }
  1637.     private int MaxGvool(int m, int b, PirateGame game){
  1638.         int maxGvool;
  1639.         int x=0;
  1640.         if(m==0)
  1641.             return game.cols;
  1642.         else
  1643.             x = (game.cols-b)/m;
  1644.         if(x>game.cols || x<0 ){
  1645.             maxGvool = game.cols;
  1646.         }
  1647.         else{
  1648.             maxGvool = m*x + b;
  1649.         }
  1650.         return maxGvool;
  1651.     }
  1652.     private static void Prev_Loc(PirateGame game){
  1653.         Pirate[] pir = game.getEnemyLivingPirates();
  1654.         Location[] Pirate_LOC = new Location[pir.length];
  1655.         for(int i =0; i<Pirate_LOC.length; i++){
  1656.             Pirate_LOC[i] = pir[i].getLocation();
  1657.         }
  1658.     }
  1659.     private Capsule going_to_Capsule(Pirate p1, int index, PirateGame game){
  1660.         //Variables
  1661.         Location loc2 = p1.getLocation();
  1662.         Location loc1 = enemyPiratesLocations[index];
  1663.         int m;
  1664.         if(loc1.col-loc2.col==0)
  1665.             m=0;
  1666.         else
  1667.             m = (loc1.row - loc2.row)/ (loc1.col-loc2.col);
  1668.         int b = loc1.col - (loc1.row);
  1669.         int xInit = 0;
  1670.         if(m==0)
  1671.             xInit=0;
  1672.         else
  1673.             xInit=(-1)*(b/m);
  1674.         //(-1)*(b/m);
  1675.         int yy;
  1676.  
  1677.         // Checks the location ID
  1678.         Capsule[] Capsules_List = game.getEnemyCapsules();
  1679.         for(int i=0; i<Capsules_List.length;i++){
  1680.             Capsule cap = Capsules_List[i];
  1681.             for(int xx = xInit; xx<MaxGvool(m,b, game); xx++){
  1682.                 yy = m*xx + b;
  1683.                 if((yy == cap.initialLocation.row) && (xx == cap.initialLocation.col)){
  1684.  
  1685.                     return cap;
  1686.                 }
  1687.             }
  1688.  
  1689.         }
  1690.         return null;
  1691.     }
  1692.     ///////////////////////////Mothership///////////////////////////////////
  1693.     private Mothership going_to_Mothership(Pirate p1, PirateGame game){
  1694.         Location loc1 = p1.getLocation();
  1695.         Location loc2 = p1.initialLocation;
  1696.         int m = (loc1.row - loc2.row)/ (loc1.col-loc2.col);
  1697.         int b = loc1.col - (loc1.row);
  1698.         int xInit = (-1)*(b/m);
  1699.         int yy;
  1700.  
  1701.         //Checks the location ID
  1702.  
  1703.         Mothership[] Mothership_list = game.getEnemyMotherships();
  1704.         for(int i=0;i<game.getAllMotherships().length;i++){
  1705.             Mothership ship = Mothership_list[i];
  1706.             for(int xx = xInit; xx<MaxGvool(m,b, game); xx++){
  1707.                 yy = m*xx + b;
  1708.                 if((yy == ship.location.row) && (xx == ship.location.col)){
  1709.                     return ship;
  1710.                 }
  1711.             }
  1712.  
  1713.         }
  1714.         return null;
  1715.     }
  1716.     private Capsule enemyBestCapsule(PirateGame game) {
  1717.         int minDis=game.rows*game.rows+game.cols*game.cols;
  1718.         Capsule minCapsule=game.getEnemyCapsules()[0];
  1719.         for(int i=0;i<game.getEnemyCapsules().length;i++) {
  1720.             for(Mothership enemy: game.getEnemyMotherships())
  1721.             {
  1722.                 if(enemy.distance(game.getEnemyCapsules()[i])<minDis)
  1723.                 {
  1724.                     minDis=enemy.distance(game.getEnemyCapsules()[i]);
  1725.                     minCapsule=game.getEnemyCapsules()[i];
  1726.                 }
  1727.             }
  1728.         }
  1729.         return minCapsule;
  1730.     }
  1731.     public Wormhole closestWormholeActiveSpagetti(Pirate pirate, PirateGame game)//real
  1732.     {
  1733.         Wormhole minWorm=null;
  1734.         double minDist= game.rows*game.rows+game.cols*game.cols;
  1735.         for(Wormhole worm: game.getActiveWormholes())
  1736.         {
  1737.             if(worm.distance(pirate)<minDist)
  1738.             {
  1739.                 minDist= worm.distance(pirate);
  1740.                 minWorm= worm;
  1741.             }
  1742.         }
  1743.         return minWorm;
  1744.     }
  1745.     private boolean pushOutNOCAPSULE(Pirate pirate, PirateGame game)
  1746.     {
  1747.         for (Pirate enemy: game.getEnemyLivingPirates()) {
  1748.             // Check if the pirate can push the enemy.
  1749.             if (pirate.canPush(enemy)) {
  1750.                 // Push enemy!
  1751.                 if(distanceFromYashar(enemy,game)==1)
  1752.                 {
  1753.                     Location l1= new Location(0,pirate.getLocation().col);
  1754.                     pirate.push(enemy,l1);
  1755.                 }
  1756.                 if(distanceFromYashar(enemy,game)==2)
  1757.                 {
  1758.                     Location l2= new Location(pirate.getLocation().row,0);
  1759.                     pirate.push(enemy,l2);
  1760.                 }
  1761.                 if(distanceFromYashar(enemy,game)==3)
  1762.                 {
  1763.                     Location l3= new Location(6400,pirate.getLocation().col);
  1764.                     pirate.push(enemy,l3);
  1765.                 }
  1766.                 if(distanceFromYashar(enemy,game)==4)
  1767.                 {
  1768.                     Location l4= new Location(pirate.getLocation().row,6400);
  1769.                     pirate.push(enemy,l4);
  1770.                 }
  1771.                 // Print a message.
  1772.  
  1773.  
  1774.                 // Did push.
  1775.                 return true;
  1776.             }
  1777.         }
  1778.         return false;
  1779.     }
  1780.     private boolean pushNoCapDown(Pirate pirate, PirateGame game)
  1781.     {
  1782.         for(Pirate enemy: game.getEnemyLivingPirates())
  1783.         {
  1784.             if(pirate.canPush(enemy)&&enemy.capsule!=null)
  1785.             {
  1786.                 pirate.push(enemy,new Location(6400,3200));
  1787.                 return true;
  1788.             }
  1789.         }
  1790.         return false;
  1791.     }
  1792.     public boolean pushWilliamEnemy(Pirate pirate,Capsule capsule,PirateGame game)
  1793.     {
  1794.         if(capsule==null)
  1795.         {
  1796.             if(pushOutNOCAPSULE(pirate,game)==false)
  1797.                 return false;
  1798.             else
  1799.                 return true;
  1800.         }
  1801.         else{
  1802.             for(Pirate enemy:game.getEnemyLivingPirates())
  1803.             {
  1804.                 if(pirate.canPush(enemy))
  1805.                 {
  1806.                     pirate.push(enemy,capsule.initialLocation);
  1807.                     return true;
  1808.                 }
  1809.             }
  1810.         }
  1811.         return false;
  1812.  
  1813.     }
  1814.     public Location attackerUpMother(double myCapInitX, double myCapInitY,PirateGame game, Mothership mother)
  1815.     {  
  1816.         double radius= game.pushDistance;
  1817.         double zavit=(myCapInitY-mother.getLocation().col)/(myCapInitX-mother.getLocation().row);
  1818.         double zavitEnemyBase= Math.atan(zavit)-Math.PI/2.0;
  1819.         if(myCapInitX<mother.getLocation().row)
  1820.         {
  1821.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1822.         }
  1823.         int xRet=0;
  1824.         xRet= (int)(mother.getLocation().row+radius*Math.cos(zavitEnemyBase));
  1825.         int yRet=0;
  1826.         yRet= (int)(mother.getLocation().col+radius*Math.sin(zavitEnemyBase));
  1827.  
  1828.         Location defRow1= new Location(xRet,yRet);
  1829.         return defRow1;
  1830.     }
  1831.     public Location attackerLowerMother(double myCapInitX, double myCapInitY,PirateGame game, Mothership mother)
  1832.     {  
  1833.         double radius= game.pushDistance;
  1834.         double zavit=(myCapInitY-mother.getLocation().col)/(myCapInitX-mother.getLocation().row);
  1835.         double zavitEnemyBase= Math.atan(zavit)+Math.PI/2.0;
  1836.         if(myCapInitX<mother.getLocation().row)
  1837.         {
  1838.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1839.         }
  1840.         int xRet=0;
  1841.         xRet= (int)(mother.getLocation().row+radius*Math.cos(zavitEnemyBase));
  1842.         int yRet=0;
  1843.         yRet= (int)(mother.getLocation().col+radius*Math.sin(zavitEnemyBase));
  1844.  
  1845.         Location defRow1= new Location(xRet,yRet);
  1846.         return defRow1;
  1847.     }
  1848.     private Location trigoLocation(int nearLocX, int nearLocY,Location diractionLoc,double radius,double angle){
  1849.         double zavit=(nearLocY-diractionLoc.col+0.0)/(nearLocX+0.0-diractionLoc.row);
  1850.         if(nearLocX+0.0-diractionLoc.row==0)
  1851.             zavit=0;
  1852.         double zavitEnemyBase=(Math.atan(zavit)+(Math.PI/180.0)*angle+0.0);
  1853.         if(nearLocX<diractionLoc.row)
  1854.         {
  1855.             zavitEnemyBase=Math.PI+zavitEnemyBase;
  1856.         }
  1857.         int xRet=0;
  1858.         xRet= (int)(nearLocX-radius*Math.cos(zavitEnemyBase));
  1859.         int yRet=0;
  1860.         yRet= (int)(nearLocY-radius*Math.sin(zavitEnemyBase));
  1861.  
  1862.         Location defRow1= new Location(xRet,yRet);
  1863.         return defRow1;
  1864.     }
  1865.     private Mothership whichMotherGoingTo(Pirate pirate, int radius, PirateGame game)//IMPORTANT SHIT
  1866.     {
  1867.         //get pirate index in game.getmyallpirates
  1868.         int i=0;//the index
  1869.         for(i=0; i<game.getAllEnemyPirates().length;i++)
  1870.         {
  1871.             if(pirate.equals(game.getAllEnemyPirates()[i]))
  1872.                 break;
  1873.         }
  1874.         Location prevLoc= enemyPiratesLocations[i];
  1875.         Location currentLoc= pirate.getLocation();
  1876.         double m;
  1877.         int x1= prevLoc.row;
  1878.         int y1= prevLoc.col;
  1879.         int x2=currentLoc.row;
  1880.         int y2= currentLoc.col;
  1881.         for(Mothership mother: game.getEnemyMotherships())
  1882.         {
  1883.             int x3=mother.getLocation().row;
  1884.             int y3= mother.getLocation().col;
  1885.             if(x2-x1==0)
  1886.                 m=9999;
  1887.             else
  1888.                 m=(y2-y1+0.0)/(x2-x1);
  1889.             double n= y1-m*x1;
  1890.             double D= Math.abs(-m*x3+y3-n)/Math.sqrt(m*m+1);
  1891.             if(D<radius&&mother.distance(currentLoc)<mother.distance(prevLoc))
  1892.             {
  1893.                 return mother;
  1894.             }
  1895.         }
  1896.         return null;
  1897.     }
  1898.     private Capsule closestEnemyCapsuleReal(Location pirate,PirateGame game)
  1899.     {
  1900.         int minDis=game.rows*game.rows+game.cols*game.cols;
  1901.         Capsule minCapsule=null;
  1902.         for(int i=0;i<game.getEnemyCapsules().length;i++) {
  1903.  
  1904.             if(pirate.distance(game.getEnemyCapsules()[i])<minDis)
  1905.             {
  1906.                 minDis=pirate.distance(game.getEnemyCapsules()[i]);
  1907.                 minCapsule=game.getEnemyCapsules()[i];
  1908.             }
  1909.  
  1910.         }
  1911.         return minCapsule;
  1912.     }
  1913.     public void defenderAction(Pirate pirate, PirateGame game) {
  1914.         Capsule[] enemyCapsules=game.getEnemyCapsules();//Vectors!!!!!!!!!
  1915.         Capsule enemyBestCapsule=null;
  1916.         //for(int i=0;i<game.getEnemyLivingPirates().length;i++)
  1917.         //  {
  1918.         //if(going_to_Capsule(game.getEnemyLivingPirates()[i], i, game)!=null)
  1919.         //  {
  1920.         //  enemyBestCapsule=going_to_Capsule(game.getEnemyLivingPirates()[i], i, game);
  1921.         //  System.out.println(enemyBestCapsule);
  1922.         //      break;
  1923.         //  }
  1924.         //  }
  1925.         if(enemyBestCapsule==null)
  1926.         {
  1927.             enemyBestCapsule=enemyBestCapsule(game);
  1928.         }
  1929.         //enemyBestCapsule= enemyBestCapsule(game);//Vectors!!!!!!!!
  1930.  
  1931.  
  1932.         Mothership enemyMothership=relevantEnemyMothership(enemyBestCapsule, game);
  1933.         int enemyMoveCapsuleX= enemyBestCapsule.getLocation().row;
  1934.         int enemyMoveCapsuleY= enemyBestCapsule.getLocation().col;
  1935.  
  1936.         Location EnemyBaseDef= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,600,enemyBestCapsule);
  1937.         Location EnemyBaseDef2= defMove(enemyMoveCapsuleX,enemyMoveCapsuleY,game,1050,enemyBestCapsule);
  1938.         if(pirate.isAlive()&&pushOut(pirate,game)==false&&tryPushAstroidDefenders(pirate,game,enemyBestCapsule)==false&&DefendersPushWormholeFake(pirate,game,enemyMothership,enemyBestCapsule)==false)
  1939.         {
  1940.             if(defAmountClose<2)
  1941.             {
  1942.                 if(isCloseToEnemyFake(pirate,game,400,enemyBestCapsule)) {
  1943.                     pirate.sail(enemyBestCapsule.getLocation());
  1944.                 }
  1945.                 else
  1946.                     pirate.sail(EnemyBaseDef);
  1947.                 defAmountClose++;
  1948.             }
  1949.             else if(defAmountFar<2){
  1950.                 if(isCloseToEnemyFake(pirate,game,1200,enemyBestCapsule)) {
  1951.                     pirate.sail(enemyBestCapsule.getLocation());
  1952.                 }
  1953.                 else
  1954.                     pirate.sail(EnemyBaseDef2);
  1955.                 defAmountFar++;
  1956.             }
  1957.         }
  1958.     }
  1959.     public Asteroid closestAsteroid(Pirate pirate, PirateGame game)//real
  1960.     {
  1961.         Asteroid minAsteroid=null;
  1962.         if(game.getAllAsteroids().length==0)
  1963.             return null;
  1964.         double minDist= game.rows*game.rows+game.cols*game.cols;
  1965.         for(Asteroid asteroid: game.getAllAsteroids())
  1966.         {
  1967.             if(asteroid.distance(pirate)<minDist)
  1968.             {
  1969.                 minDist= asteroid.distance(pirate);
  1970.                 minAsteroid= asteroid;
  1971.             }
  1972.         }
  1973.         return minAsteroid;
  1974.     }
  1975.     public Asteroid closestAsteroidAlive(Pirate pirate, PirateGame game)//real
  1976.     {
  1977.         Asteroid minAsteroid=null;
  1978.         if(game.getAllAsteroids().length==0)
  1979.             return null;
  1980.         double minDist= game.rows*game.rows+game.cols*game.cols;
  1981.         for(Asteroid asteroid: game.getLivingAsteroids())
  1982.         {
  1983.             if(asteroid.distance(pirate)<minDist)
  1984.             {
  1985.                 minDist= asteroid.distance(pirate);
  1986.                 minAsteroid= asteroid;
  1987.             }
  1988.         }
  1989.         return minAsteroid;
  1990.     }
  1991.     /*public Location safteyLocCloseCapsule(Location pirate,Location diractionLoc,double radius,int leftRight,PirateGame game)
  1992.     {
  1993.         int[] degrees=new int[360];
  1994.         degrees[0]=0;
  1995.         if(leftRight==1){
  1996.             for(int i=1;i<180;i+=2)//creating the degrees array
  1997.             {
  1998.                 degrees[i]=i;
  1999.                 degrees[i+180]= -1*i;
  2000.             }
  2001.         }
  2002.         else if (leftRight==0)
  2003.         {
  2004.             for(int i=1;i<180;i+=2)//creating the degrees array
  2005.             {
  2006.                 degrees[i]=-1*i;
  2007.                 degrees[i+180]= i;
  2008.             }
  2009.         }
  2010.         for(int i=0;i<degrees.length;i++)
  2011.         {
  2012.             if(isSafeLoc(trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]),game))
  2013.                 return trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]);
  2014.         }
  2015.         return trigoLocation(pirate.getLocation().row,pirate.getLocation().col,diractionLoc,radius,180.0);
  2016.     }
  2017.      */
  2018.     public Location safteyLocDef(Location pirate,Location diractionLoc,double radius,PirateGame game)
  2019.     {
  2020.         //radius=game.pirateMaxSpeed;
  2021.         int[] degrees=new int[360];
  2022.         degrees[0]=0;
  2023.         int degreesCounter=1;
  2024.         for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  2025.         {
  2026.             degrees[i]=degreesCounter;
  2027.             degrees[i+1]=degreesCounter*-1;
  2028.             degreesCounter++;
  2029.         }
  2030.         for(int i=0;i<degrees.length;i++)
  2031.         {
  2032.  
  2033.             if(isSafeLocDef(trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]),game))
  2034.                 return trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]);
  2035.         }
  2036.         return trigoLocation(pirate.getLocation().row,pirate.getLocation().col,diractionLoc,radius,0.0);
  2037.     }
  2038.  
  2039.     public Location safteyLoc(Location pirate,Location diractionLoc,double radius,PirateGame game)
  2040.     {
  2041.         //radius=game.pirateMaxSpeed;
  2042.         int[] degrees=new int[360];
  2043.         degrees[0]=0;
  2044.         int degreesCounter=1;
  2045.         for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  2046.         {
  2047.             degrees[i]=degreesCounter;
  2048.             degrees[i+1]=degreesCounter*-1;
  2049.             degreesCounter++;
  2050.         }
  2051.         for(int i=0;i<degrees.length;i++)
  2052.         {
  2053.  
  2054.             if(isSafeLoc(trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]),game))
  2055.                 return trigoLocation(pirate.row,pirate.col,diractionLoc,radius,degrees[i]);
  2056.         }
  2057.         return trigoLocation(pirate.getLocation().row,pirate.getLocation().col,diractionLoc,radius,0.0);
  2058.     }
  2059.  
  2060.     public boolean isSafeLoc(Location loc,PirateGame game)
  2061.     {
  2062.         int enemyCount=0;
  2063.         for(Mothership mother: game.getAllMotherships())
  2064.         {
  2065.             if(mother.distance(loc)<game.pirateMaxSpeed)
  2066.                 return true;
  2067.         }
  2068.         for(Pirate enemy: game.getEnemyLivingPirates())//enemy saftey
  2069.         {
  2070.             if(enemy.distance(loc)<=550&&enemy.pushReloadTurns<=1)
  2071.             {
  2072.                 enemyCount++;
  2073.                 if(enemyCount>=2)
  2074.                     return false;
  2075.             }
  2076.         }
  2077.         if(loc.row<0||loc.col<0||loc.row>game.rows||loc.col>game.cols)
  2078.             return false;
  2079.         //check astroid
  2080.         for(Asteroid astroid:game.getLivingAsteroids())
  2081.         {
  2082.             int dirX=astroid.getLocation().row+(int)(astroid.direction.row*1);
  2083.             int dirY=astroid.getLocation().col+(int)(astroid.direction.col*1);
  2084.             Location asterDiraction= new Location(dirX,dirY);
  2085.             if(asterDiraction.distance(loc)<=astroid.size+200)
  2086.                 return false;
  2087.         }
  2088.         //check wormhole
  2089.         for(Wormhole wormhole:game.getAllWormholes())
  2090.         {
  2091.             if(wormhole.turnsToReactivate<=1&&wormhole.distance(loc)<wormhole.size)
  2092.                 return false;
  2093.         }
  2094.         return true;
  2095.     }
  2096.     public boolean isSafeLocDef(Location loc,PirateGame game)
  2097.     {
  2098.         int enemyCount=0;
  2099.         if(loc.row<0||loc.col<0||loc.row>game.rows||loc.col>game.cols)
  2100.             return false;
  2101.         //check astroid
  2102.         for(Asteroid astroid:game.getLivingAsteroids())
  2103.         {
  2104.             int dirX=astroid.getLocation().row+(int)(astroid.direction.row*1);
  2105.             int dirY=astroid.getLocation().col+(int)(astroid.direction.col*1);
  2106.             Location asterDiraction= new Location(dirX,dirY);
  2107.             if(asterDiraction.distance(loc)<=astroid.size+200)
  2108.                 return false;
  2109.         }
  2110.         //check wormhole
  2111.         return true;
  2112.     }
  2113.     public Capsule closestCapsuleNotYou(Pirate pirate,PirateGame game)
  2114.     {
  2115.         int capDist=game.rows*game.rows+game.cols*game.cols;
  2116.         Capsule minCap=null;
  2117.         if(game.getMyCapsules().length==0)
  2118.             return null;
  2119.         for(int i=0;i<game.getMyCapsules().length;i++)
  2120.         {
  2121.  
  2122.             if(capDist>pirate.distance(game.getMyCapsules()[i])&&!pirate.getLocation().equals(game.getMyCapsules()[i].getLocation()))
  2123.             {
  2124.                 minCap=game.getMyCapsules()[i];
  2125.                 capDist=pirate.distance(game.getMyCapsules()[i]);
  2126.             }
  2127.         }
  2128.         return minCap;
  2129.     }
  2130.     public Pirate closestDefenderToAstroid(PirateGame game)
  2131.     {
  2132.         if(defenders.length==0)
  2133.             return null;
  2134.         if(game.getLivingAsteroids().length==0)
  2135.             return null;
  2136.         int minDis=game.rows*game.rows+game.cols*game.cols;
  2137.         Pirate minPirate=null;
  2138.         for(int i=0;i<defenders.length;i++)
  2139.         {
  2140.             if(defenders[i].distance(closestAsteroidAlive(defenders[i],game))<minDis)
  2141.             {
  2142.                 minDis=defenders[i].distance(closestAsteroidAlive(defenders[i],game));
  2143.                 minPirate=defenders[i];
  2144.             }
  2145.         }
  2146.         return minPirate;
  2147.     }
  2148.     public boolean shouldPushToAstroid(Pirate pirate, PirateGame game)
  2149.     {
  2150.         Asteroid astroid= closestAsteroidAlive(pirate,game);
  2151.         if(astroid==null)
  2152.             return false;
  2153.         for(Pirate enemy:game.getEnemyLivingPirates())
  2154.         {
  2155.             if(astroid.distance(enemy)-astroid.size<=pirate.pushDistance&&pirate.canPush(enemy))
  2156.             {
  2157.                 piratesPushingAstroids[astroid.id]++;
  2158.                 if(piratesPushingAstroids[astroid.id]<=1){
  2159.                     pirate.push(enemy,astroid);
  2160.                     return true;
  2161.                 }
  2162.             }
  2163.         }
  2164.         return false;
  2165.     }
  2166.     public boolean IsStickybombOnMe(Pirate pirate, PirateGame game){
  2167.         for(StickyBomb stickybomb: game.getAllStickyBombs()){
  2168.             if(stickybomb.carrier==pirate){
  2169.                 return true;
  2170.             }
  2171.         }
  2172.             return false;
  2173.        
  2174.     }
  2175. }
Advertisement
Add Comment
Please, Sign In to add comment