Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bots;
- import elf_kingdom.*;
- public class MyBot implements SkillzBot {
- /*
- Version: enemy invisibility check
- TO DO: INVISIBILITY
- */
- static Location[] enemyElvesLocations;
- static Location[] myElvesLocations;
- static boolean isElfTryingToBuildPortal;
- static Location enemyAttackingLoc;
- static int ActiontryToAttackEnemyElf=0;
- static int ActiontryToBuildPortalCauseCan=1;
- static int ActionDefPortal=2;
- static int ActionDestroyEnemyManaFountains=3;
- static int ActionEnemyAttackingPortal=4;
- static int ActionEnemyElfAttackingA=5;
- static int ActionBuildDefManaFountains=6;
- static int ActionEnemyElfAttacking=7;
- static int ActionBuildManaFountains=8;
- static int ActionBuildAttackingPortals=10;
- static int ActionBuildAttackPortalWhenCan=11;
- static int ActionDestroyEnemyBuildings=9;
- static int ActionAttackEnemyCastle=12;
- static int NumberOfActions=12;
- static int numberOfTornadoes=0;
- static Elf[] enemyLivingElves;
- static String botName;
- public void doTurn(Game game) {
- System.out.println("number Of Tornadoes: "+numberOfTornadoes);
- //System.out.println("havradim adumin");
- //System.out.println("hasheleg hu rah");
- //System.out.println("haindex nimza mehuz legvulut hamaarah");
- int counter=0;
- for(Elf elf:game.getEnemyLivingElves())
- {
- if(game.inMap(elf))
- counter++;
- }
- enemyLivingElves=new Elf[counter];
- ////System.out.println(enemyLivingElves.length+" LENGTH");
- counter=0;
- for(int i=0;i<game.getEnemyLivingElves().length;i++)
- {
- if(game.inMap(game.getEnemyLivingElves()[i]))
- {
- enemyLivingElves[counter]=game.getEnemyLivingElves()[i];
- counter++;
- }
- }
- //for(Elf elf:enemyLivingElves)
- // //System.out.print(elf+",");
- //////System.out.println();
- ////System.out.println(whereToBuildManaFountain(game)+" where to build mana fountain");
- ////System.out.println("isItSmartToBuildManaFountain :"+isItSmartToBuildManaFountain(whereToBuildManaFountain(game),game));
- for(int i=0;i<=NumberOfActions;i++)
- System.out.println(numberOfElvesNeededForAction(i,game)+" action number "+i);
- if(game.turn==1)
- {
- turnOne(game);
- }
- if(EnemyThreatningPortal(game)!=null)
- enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
- else
- enemyAttackingLoc=null;
- isElfTryingToBuildPortal=false;
- //////System.out.println("Elves:");
- handleElvesNew(game);
- //////System.out.println("");
- //////System.out.println("Portals:");
- handlePortals(game);
- //END OF TURN RESET LOCATIONS OF ELVES
- for(int i=0;i<game.getAllEnemyElves().length;i++)//update enemyPirates Loc
- {
- if(game.getAllEnemyElves()[i].isAlive()&&game.getAllEnemyElves()[i].getLocation().row==-50&&game.getAllEnemyElves()[i].getLocation().col==-50)
- {
- Elf enemy=game.getAllEnemyElves()[i];
- ManaFountain mana=manaEnemyGoingTo(enemy,game);
- if(mana!=null)
- enemyElvesLocations[i]=enemyElvesLocations[i].towards(mana,enemy.maxSpeed);
- System.out.println(enemy+" is invisible");
- }
- else
- enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
- }
- for(int i=0;i<game.getAllMyElves().length;i++)//update myPirates Loc
- {
- myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
- }
- }
- public static void turnOne(Game game)
- {
- enemyElvesLocations=new Location[game.getAllEnemyElves().length];
- myElvesLocations=new Location[game.getAllMyElves().length];
- for(int i=0;i<game.getAllEnemyElves().length;i++)
- {
- enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
- }
- for(int i=0;i<game.getAllMyElves().length;i++)
- {
- myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
- }
- }
- private void handlePortals(Game game) {
- for(Portal portal: game.getMyPortals())
- {
- IceOrFire(portal,game);
- }
- }
- public static void swapElves(Elf elf,Elf[] elvesForActions,int counter,int actionNum,Game game)
- {
- if(counter==elvesForActions.length)
- {
- return;
- }
- else if(elvesForActions[counter]==null)
- {
- elvesForActions[counter]=elf;
- return;
- }
- else if(elvesForActions[counter]!=elf&&isElfBetterAtAction(elf,elvesForActions[counter],actionNum,game))
- {
- Elf keeper=elvesForActions[counter];
- elvesForActions[counter]=elf;
- swapElves(keeper,elvesForActions,0,actionNum,game);
- }
- else
- {
- swapElves(elf,elvesForActions,counter+1,actionNum,game);
- }
- }
- public static void elvesDoAction(Elf[] elves,int action,Game game)
- {
- for(int i=0;i<elves.length;i++)
- {
- if(elves[i]!=null)
- {
- Elf elf=elves[i];
- if(!elf.alreadyActed) {
- if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.maxSpeed*10+game.castleSize&&action!=ActionDestroyEnemyManaFountains)
- isElfTryingToBuildPortal=true;
- System.out.println(elf+" is it smart "+isItSmartToBuildPortal(elf,game)+" is it safe "+isItSafeToBuildPortal(elf,game));
- if(elf.isBuilding)
- System.out.println(elf+" elf is building");
- ///**0**\\\ ---PRIVATE
- //if there is no enemy close to casle: Try to attack an Elf close to you\\
- else if(tryToAttackElf(elf,game))
- {
- attackEnemyElf(elf,game);
- System.out.println(elf+" Elf Attacking");
- }
- ////**1**\\\\---PRIVATE
- //if it is safe and smart and has mana build a portal
- //Longest line
- else if(!(elf.maxSpeed>game.elfMaxSpeed)&&isThereDefPortals(game)&&isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.canBuildPortal()&&game.getMyMana()>=(game.portalCost+game.lavaGiantCost-game.getMyself().manaPerTurn)&&(!shouldIbuildManaFountain(game)||!isItSmartToBuildManaFountain(whereToBuildManaFountain(game),game)||game.getMyMana()>game.manaFountainCost+game.getMyself().manaPerTurn))
- {
- System.out.println(elf+" elf chose to build portal cause he has a lot of mana");
- elf.buildPortal();
- }
- else if(areTheyCloseFountainsToElf(elf,game)&&action!=ActionDestroyEnemyManaFountains)
- tryToDestroyEnemyCloseFountain(elf,game);
- else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.maxSpeed*10+game.castleSize&& elf.canBuildPortal()&&action!=ActionDestroyEnemyManaFountains)
- {
- System.out.println(elf+" elf chose to build an attacking portal cause he is close");
- elf.buildPortal();
- }
- else if(game.getEnemyCastle().currentHealth==75&&game.getMyself().manaPerTurn==0&&game.getMyself().mana==0)
- {
- if(areTheyCloseFountainsToElf(elf,game))
- tryToDestroyEnemyCloseFountain(elf,game);
- else
- elf.attack(game.getEnemyLavaGiants()[0]);
- }
- ////**2**\\\\
- //if there is no portal defending go build one
- else if(action==ActionDefPortal)
- {
- System.out.println(elf+" elf chose to build def portal");
- buildDefPortal(elf,game);
- }
- ////**3**\\\\
- //Is there is a Close Enemy Portal To Castle\\
- else if(action==ActionEnemyAttackingPortal)
- {
- System.out.println(elf+" elf chose to destroy enemy elves attacking portal");
- destroyEnemysAttackingPortal(elf,game);
- }
- ////**4**\\\\
- //checking if enemy elf is on his way to attack our castle
- else if(action==ActionEnemyElfAttacking||action==ActionEnemyElfAttackingA)//ACTION THAT DOES STUFF!!!!
- {
- GoingToEnemyAttackingElf(elf,game);
- System.out.println(elf+" going to def cause enemy elf attacking");
- }
- else if(isTherePortalNearEnemyCloseSpawnToCastle(game)&&enemyLivingElves.length==0){
- buildPortalToFuckEnemyElfWhenSpawned(elf, game);
- }
- ////**5**\\\\
- //if there is no attack portal: Go build one\\
- else if(action==ActionBuildAttackingPortals)
- {
- System.out.println(elf+" elf chose to build attacking portal");
- tryToBuildAttackingPortal(elf,game);
- }
- ////**6**\\\\
- //if you are close to enemys castle and you can build portal, build one
- else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost&&action!=ActionDestroyEnemyManaFountains)
- {
- System.out.println(elf+" elf chose to build an attacking portal cause he can");
- elf.buildPortal();
- }
- ////**7**\\\\
- //if enemy has portals, try to destroy them
- else if(action==ActionDestroyEnemyBuildings)
- {
- System.out.println(elf+" elf chose to destroy enemys buildings");
- destroyEnemysBuilding(elf,game);
- }
- else if(action==ActionBuildManaFountains)
- {
- System.out.println(elf+" trying to build a mana fountain");
- tryToBuildManaFountain(elf,game);
- }
- else if(action==ActionDestroyEnemyManaFountains)
- {
- System.out.println(elf+" destroying enemy mana founctain");
- destroyEnemysFountains(elf,game);
- }
- else if(action==ActionBuildDefManaFountains)
- {
- System.out.println(elf+" building a portal to def mana");
- buildDefManaFountains(elf,game);
- }
- ////**8**\\\\
- else//attack enemys castle at last
- {
- System.out.println(elf+" elf chose to attack the castle");
- tryToAttackEnemyCastle(elf,game);
- }
- }
- else
- {
- ////System.out.println(elf+" allready acted");
- }
- }
- }
- }
- public static void elvesDoChallenges(Elf[] elves, Game game)
- {
- for(int i=0;i<elves.length;i++)
- {
- if(elves[i]!=null)
- {
- Elf elf=elves[i];
- if(!elf.alreadyActed) {
- if(elf.isBuilding)
- System.out.println(elf+" elf is building");
- ////Trap\\\\
- else if(botName == "Trap") {
- if(game.turn == 3 || game.turn == 10 || game.turn == 17) elf.castInvisibility();
- }
- else if(game.turn == 1 && game.getEnemyIceTrolls().length == 12 && game.elfMaxSpeed == 0 && game.lavaGiantMaxSpeed == 0) {
- for(int iceTrollNum = 0; iceTrollNum < game.getEnemyIceTrolls().length ; iceTrollNum++) {
- if(game.getEnemyIceTrolls()[iceTrollNum].distance(game.getAllMyElves()[0]) < 650 || game.getEnemyIceTrolls()[iceTrollNum].distance(game.getAllMyElves()[0]) > 750) {
- break;
- }
- if(iceTrollNum == game.getEnemyIceTrolls().length - 1) {
- botName = "Trap";
- }
- }
- }
- ///HoraMana\\\\
- else if(botName == "HoraMana")
- {
- if(!elf.invisible && elf.canCastInvisibility())
- elf.castInvisibility();
- else if(elf.inAttackRange(game.getEnemyCastle()))
- elf.attack(game.getEnemyCastle());
- else
- elf.moveTo(game.getEnemyCastle());
- }
- else if(game.turn == 1 && game.getAllEnemyElves().length == 18)
- {
- for(int enElfNum = 0; enElfNum < game.getAllEnemyElves().length; enElfNum++)
- {
- if(game.getAllEnemyElves()[enElfNum].distance(game.getMyPortals()[0]) > 100)
- break;
- if(enElfNum == game.getAllEnemyElves().length - 1)
- {
- botName = "HoraMana";
- elf.moveTo(game.getEnemyCastle());
- }
- }
- }
- else if(game.elfMaxSpeed == 5 && game.defaultManaPerTurn == 10)
- {
- if(elf.distance(game.getMyCastle()) - game.castleSize > game.portalSize)
- {
- if(elf.canBuildPortal()){
- ////System.out.println("Chanlange");
- elf.buildPortal();
- }
- }
- else if(elf.currentSpells.length > 0)
- elf.moveTo(game.getEnemyCastle());
- else if(elf.canCastSpeedUp())
- elf.castSpeedUp();
- }
- }
- }
- }
- }
- public static void handleElvesNew(Game game)
- {
- Elf[] elvesNotUsed=new Elf[game.getMyLivingElves().length];
- boolean hasElvesForAction=false;
- for(int i=0;i<elvesNotUsed.length;i++)
- {
- elvesNotUsed[i]=game.getMyLivingElves()[i];
- }
- boolean flag=false;
- int numOfElvesForDestroyEnemyMana=numberOfElvesNeededForAction(ActionDestroyEnemyManaFountains,game);
- int numOfElvesForEnemyElfAttackig=numberOfElvesNeededForAction(ActionEnemyElfAttacking,game);
- for(int i=0;i<=NumberOfActions;i++)//MAIN FOR 8 is number of actions
- {
- if(i!=ActiontryToAttackEnemyElf&&i!=ActiontryToBuildPortalCauseCan&&i!=ActionBuildAttackPortalWhenCan)//PRIVATE FUNCS
- {
- int numbOfElvesNeededForAction=numberOfElvesNeededForAction(i,game);
- if(numbOfElvesNeededForAction>0)
- {
- Elf[] elvesForActions=new Elf[numbOfElvesNeededForAction];
- for(int n=0;n<elvesNotUsed.length;n++)
- {
- if(elvesNotUsed[n]!=null)
- {
- /*Elf elf=elvesNotUsed[n];
- if(elf.maxSpeed>game.elfMaxSpeed||(numOfElvesForDestroyEnemyMana>0&&ShouldElfUseSpeedUp(elf,getEnemyBestManaFountain(elf,game).getLocation(),game)))
- {
- if(numOfElvesForEnemyElfAttackig>0||numOfElvesForDestroyEnemyMana>0)
- {
- if(i!=ActionEnemyElfAttacking&&i!=ActionDestroyEnemyManaFountains)
- {
- flag=true;
- }
- else if(i==ActionDestroyEnemyManaFountains)
- {
- flag=true;
- destroyEnemysFountains(elf,game);
- elvesNotUsed[n]=null;
- }
- else if(i==ActionEnemyElfAttacking)
- {
- flag=true;
- GoingToEnemyAttackingElf(elf,game);
- elvesNotUsed[n]=null;
- }
- }
- //do nothing with this elf
- }
- if(flag==false)*/
- swapElves(elvesNotUsed[n],elvesForActions,0,i,game);
- }
- }
- for(int k=0;k<elvesNotUsed.length;k++)
- {
- for(Elf elfUsed:elvesForActions)
- {
- if(elvesNotUsed[k]==elfUsed)
- {
- elvesNotUsed[k]=null;
- }
- }
- }
- //////System.out.println("action numba "+i);
- //for(int k=0;k<elvesForActions.length;k++)
- // //System.out.print(elvesForActions[k]+",");
- //////System.out.println();
- //for(int k=0;k<elvesNotUsed.length;k++)
- // //System.out.print(elvesNotUsed[k]+",");
- //////System.out.println();
- elvesDoChallenges(elvesForActions, game);
- elvesDoAction(elvesForActions,i,game);
- }
- }
- }
- //for(int i=0;i<elvesNotUsed.length;i++)
- //{
- elvesDoAction(elvesNotUsed,NumberOfActions,game);
- //if(elvesNotUsed[i]!=null)
- //{
- // tryToAttackEnemyCastle(elvesNotUsed[i],game);//deafult action
- //}
- //}
- }
- public static double turnsToGetToLoc(Elf elf,Location location){
- return elf.distance(location)/elf.maxSpeed+elf.turnsToRevive*1.5;
- }
- public static boolean isElfBetterAtAction(Elf elf,Elf elfCompare,int action,Game game)
- {
- double difference=1.5;
- if(action==ActionDefPortal) {//2
- Location loc=whereElfShouldBuildDefPortal(game);
- if(turnsToGetToLoc(elf,loc)+difference<turnsToGetToLoc(elfCompare,loc))
- return true;
- return false;
- }
- else if(action==ActionEnemyAttackingPortal) {//3
- //if elf needs help ++
- //&& other elf is far to another action's needed positions
- Portal port=closestEnemyPortalToOurCastle(game);
- if(turnsToGetToLoc(elf,port.getLocation())+difference<turnsToGetToLoc(elfCompare,port.getLocation()))
- {
- return true;
- }
- return false;
- }
- else if(action==ActionEnemyElfAttacking) {//4
- Elf enemyElf=isThereEnemyElfAttacking(15,game);
- if(turnsToGetToLoc(elf,enemyElf.getLocation())+difference<turnsToGetToLoc(elfCompare,enemyElf.getLocation()))
- {
- return true;
- }
- return false;
- }
- else if(action==ActionEnemyElfAttackingA)
- {
- Elf enemyElf=isThereEnemyElfAttacking(4,game);
- if(turnsToGetToLoc(elf,enemyElf.getLocation())+difference<turnsToGetToLoc(elfCompare,enemyElf.getLocation()))
- {
- return true;
- }
- return false;
- }
- else if(action==ActionBuildAttackingPortals) {//if we dont have an attacking portal 5
- Location whereToBuildPort1=whereElfShouldBuildAttackPortal(elf,game);
- Location whereToBuildPort2=whereElfShouldBuildAttackPortal(elfCompare,game);
- if(turnsToGetToLoc(elf,whereToBuildPort1)+difference<turnsToGetToLoc(elfCompare,whereToBuildPort2))
- return true;
- return false;
- }
- else if(action==ActionDestroyEnemyBuildings) {//7
- Location l1=closestEnemyBuildingToMapObject(elf,game).getLocation();
- Location l2=closestEnemyBuildingToMapObject(elfCompare,game).getLocation();
- if(turnsToGetToLoc(elf,l1)+difference<turnsToGetToLoc(elfCompare,l2))
- return true;
- return false;
- }
- else if(action==ActionBuildManaFountains) {//9
- Location l1=whereToBuildManaFountain(game);
- if(turnsToGetToLoc(elf,l1)+difference<turnsToGetToLoc(elfCompare,l1))
- return true;
- return false;
- }
- else if(action==ActionDestroyEnemyManaFountains)
- {
- Location neededToDestroyEnemyMana=getEnemyBestManaFountain(game).getLocation();
- Location neededToDestroyEnemyMana2=getEnemyBestManaFountain(game).getLocation();
- if(turnsToGetToLoc(elf,neededToDestroyEnemyMana)+difference<turnsToGetToLoc(elfCompare,neededToDestroyEnemyMana2))
- return true;
- return false;
- }
- else if(action==ActionBuildDefManaFountains)
- {
- Location whereToDefManaPort=whereToDefMana(game);
- if(turnsToGetToLoc(elf,whereToDefManaPort)+difference<turnsToGetToLoc(elfCompare,whereToDefManaPort))
- return true;
- return false;
- }
- return false;
- }
- public static Location whereToDefMana(Game game)
- {
- if(game.getMyManaFountains().length==0)
- return null;
- Location min=null;
- boolean flag=false;
- int rad=game.manaFountainSize+game.portalSize+10;
- int[] degrees=new int[90];
- degrees[0]=0;
- int degreesCounter=1;
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- for(ManaFountain mana:game.getMyManaFountains())
- {
- flag=false;
- for(int i=0;i<degrees.length-1&&!flag;i++)
- {
- Location loc=trigoLocation(mana.getLocation().row,mana.getLocation().col,game.getEnemyCastle().getLocation(),rad,degrees[i]);
- Location port=closestAllyPortalToLoc(loc,game);
- if(game.canBuildPortalAt(loc)&&(port==null||port.distance(loc)>game.portalSize*3))
- {
- if(min==null)
- {
- flag=true;
- min=loc;
- }
- else if(loc.distance(game.getMyCastle())<min.distance(game.getMyCastle()))
- {
- flag=true;
- min=loc;
- }
- }
- }
- }
- return min;
- }
- public static void buildDefManaFountains(Elf elf,Game game)
- {
- Location loc=whereToDefMana(game);
- if(loc==null)
- return;
- if(elf.distance(loc)<elf.maxSpeed*1.5&&elf.canBuildPortal())
- elf.buildPortal();
- else
- elf.moveTo(safteyLoc(elf.getLocation(),loc,elf.maxSpeed,game));
- }
- public static Building closestEnemyBuildingToMapObject(MapObject object,Game game)
- {
- //////System.out.println(object+" OBJECT?!?!?!?!?!");
- if(game.getEnemyPortals().length==0&&game.getEnemyManaFountains().length==0)
- return null;
- Building mBuilding=null;
- if(game.getEnemyManaFountains().length>0)
- mBuilding=game.getEnemyManaFountains()[0];
- else
- mBuilding=game.getEnemyPortals()[0];
- for(Portal portal:game.getEnemyPortals())
- {
- if(portal.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
- mBuilding=portal;
- }
- for(ManaFountain Fountain:game.getEnemyManaFountains())
- {
- if(Fountain.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
- mBuilding=Fountain;
- }
- return mBuilding;
- }
- public static boolean areTheyCloseFountainsToElf(Elf elf,Game game)
- {
- for(ManaFountain mana:game.getEnemyManaFountains())
- {
- if(elf.inAttackRange(mana))
- {
- return true;
- }
- else if(elf.distance(mana)<=elf.maxSpeed*3)
- {
- return true;
- }
- else if(mana.distance(game.getMyCastle())<=mana.size*5)
- {
- return true;
- }
- }
- return false;
- }
- public static void tryToDestroyEnemyCloseFountain(Elf elf,Game game)
- {
- for(ManaFountain mana:game.getEnemyManaFountains())
- {
- if(elf.inAttackRange(mana))
- {
- elf.attack(mana);
- return;
- }
- else if(elf.distance(mana)<=elf.maxSpeed*3)
- {
- elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed,game));
- return;
- }
- else if(mana.distance(game.getMyCastle())<=mana.size*5)
- {
- elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed,game));
- return;
- }
- }
- }
- public static void buildDefPortal(Elf elf,Game game)
- {
- isElfTryingToBuildPortal=true;
- Location whereToBuildPort=whereElfShouldBuildDefPortal(game);
- if(elf.distance(whereToBuildPort)<=elf.maxSpeed*1.5 &&elf.canBuildPortal()&&!(elf.isBuilding)&&game.getMyMana()>=(game.portalCost))
- {
- ////System.out.println("building def portal");
- elf.buildPortal();
- }
- else
- {
- //////System.out.println("going to build def portal loc");
- if(elf.distance(whereToBuildPort)<elf.maxSpeed*2&&isSafeLoc(elf.getLocation(),game))
- elf.moveTo(whereToBuildPort);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
- }
- }
- public static boolean ShouldElfUseSpeedUpFORELF(Elf elf,Location loc,Game game){
- double TurnsToMana= elf.distance(loc)/(game.speedUpMultiplier*elf.maxSpeed)+1;
- if(TurnsToMana>game.speedUpExpirationTurns/2&&elf.currentHealth>=TurnsToMana){
- return true;
- }
- return false;
- }
- public static boolean ShouldElfUseSpeedUp(Elf elf,Location loc,Game game){
- double TurnsToMana= elf.distance(loc)/(game.speedUpMultiplier*elf.maxSpeed)+1;
- if(TurnsToMana<=game.speedUpExpirationTurns+2&&elf.currentHealth>=TurnsToMana){
- return true;
- }
- return false;
- }
- public static void destroyEnemysAttackingPortal(Elf elf,Game game)
- {
- Building p=closestEnemyPortalToOurCastle(game);
- Building b=closestEnemyBuildingToMapObject(elf,game);
- if(elf.distance(b)<=elf.attackRange+b.size)
- p=b;
- //if(ClosestEnemyElfToOurCastle(game)!=null&&ClosestEnemyElfToOurCastle(game).distance(elf)<elf.distance(p)&&isThereEnemyElfAttacking(game)!=null)
- //{
- // GoingToEnemyAttackingElf(elf,game);
- //////System.out.println("going to def cause enemy elf attacking");
- //}
- //if can attack: Attack!
- if(elf.inAttackRange(p)&&isItSafeToAttackEnemyBuilding(elf,p,game))//&&!elf.invisible)
- {
- elf.attack(p);
- }
- //if can't attack: move to portal
- else if(!(elf.isBuilding))
- {
- if(!(elf.maxSpeed>game.elfMaxSpeed)&&isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.canBuildPortal()&&game.getMyMana()>(game.portalCost+10)&&NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game)<2)
- {
- ////System.out.println("elf chose to build portal to destroy enemy's portal");
- elf.buildPortal();
- }
- else
- {
- //elf.moveTo(safteyLoc(elf.getLocation(),p.getLocation(),elf.maxSpeed,game));
- //Location whereToAttackEnemyPortal=whereElfShouldAttackBuilding(elf,p,game);
- Location safeLoc=safteyLoc(elf.getLocation(),p.getLocation(),elf.maxSpeed,game);
- elf.moveTo(safeLoc);
- /*if(elf.maxSpeed>game.elfMaxSpeed)
- {
- //////System.out.println(whereToAttackEnemyPortal+" WHERE TO ATTACK ENEMY PORTAL LOC");
- elf.moveTo(safeLoc);
- }
- else
- {
- if(safeLoc.distance(p)>elf.distance(p)&&elf.canCastSpeedUp()&&ShouldElfUseSpeedUp(elf,p.getLocation(),game))
- elf.castSpeedUp();
- else
- elf.moveTo(safeLoc);
- }*/
- }
- }
- }
- public static void tryToBuildAttackingPortal(Elf elf,Game game)
- {
- isElfTryingToBuildPortal=true;
- Location whereToBuildPort;
- int num=numOfEnoughClosePortalToEnemyCastle(game);
- if(num==0)
- whereToBuildPort=whereElfShouldBuildAttackPortal(elf,game);
- else
- whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
- //////System.out.println(isItSafeToBuildPortal(elf,game)+" FOR FUCKS SAKE IT IS SAFE TO BUILD A GOD DARN PORTAL");
- if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost)
- {
- elf.buildPortal();
- }
- else if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
- {
- if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
- {
- if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
- {
- elf.buildPortal();
- //////System.out.println("elf is building a portal");
- }
- else
- {
- //////System.out.println("elf is moving safetly to the loc");
- if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
- elf.moveTo(whereToBuildPort);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
- }
- }
- else
- {
- whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
- if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
- {
- if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
- {
- if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
- {
- elf.buildPortal();
- //////System.out.println("elf is building a portal");
- }
- }
- else
- {
- //////System.out.println("elf is moving safetly to the loc");
- if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
- elf.moveTo(whereToBuildPort);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
- }
- }
- else
- {
- //////System.out.println("elf is moving safetly to the loc");
- elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
- }
- }
- }
- else if(!elf.isBuilding)
- {
- //////System.out.println("elf is moving to the port loc");
- if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
- elf.moveTo(whereToBuildPort);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
- }
- }
- public static void destroyEnemysBuilding(Elf elf,Game game)
- {
- Building enemyBuilding=closestEnemyBuildingToMapObject(elf,game);
- if(isItSafeToAttackEnemyBuilding(elf,enemyBuilding,game)&&elf.inAttackRange(enemyBuilding))
- {
- //////System.out.println("ATTACKING ENEMY PORTAL");
- elf.attack(enemyBuilding);
- }
- //if not in Attack Range: move there
- else
- {
- //////System.out.println("MOVING SAFTLEY TO ENEMYS CLOSEST PORTAL");
- elf.moveTo(safteyLoc(elf.getLocation(),enemyBuilding.getLocation(),elf.maxSpeed,game));
- }
- }
- public static boolean isTherePortalNearEnemyCloseSpawnToCastle(Game game){
- int count=0;
- Elf CloseSpawiningElf=null;
- for(Elf enemyElf:game.getAllEnemyElves()){
- if(enemyElf.initialLocation.distance(game.getMyCastle())-game.getMyCastle().size-game.elfAttackRange<game.elfMaxSpeed*15){
- CloseSpawiningElf=enemyElf;
- }
- }
- if(CloseSpawiningElf==null)
- return false;
- for(Portal portal:game.getAllPortals()){
- if(portal.distance(CloseSpawiningElf.initialLocation)<game.iceTrollMaxSpeed+game.iceTrollAttackRange){
- count++;
- }
- }
- if(count==0)
- return true;
- return false;
- }
- public static void buildPortalToFuckEnemyElfWhenSpawned(Elf elf, Game game){
- Location PortalLocation=null;
- for(Elf enemyElf:game.getAllEnemyElves()){
- if(enemyElf.initialLocation.distance(game.getMyCastle())-game.getMyCastle().size-game.elfAttackRange<game.elfMaxSpeed*15){
- PortalLocation=enemyElf.initialLocation;
- }
- }
- ////System.out.println(PortalLocation);
- if(PortalLocation!=null){
- if(elf.distance(PortalLocation)<=elf.maxSpeed*1.5&&elf.canBuildPortal()&&game.getMyMana()>=game.manaFountainCost)
- {
- ////System.out.println("building spawn portal");
- elf.buildPortal();
- }
- else
- {
- ////System.out.println("going spawn portaln");
- if(elf.distance(PortalLocation)<=elf.maxSpeed*2)
- elf.moveTo(PortalLocation);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),PortalLocation,elf.maxSpeed,game));
- }
- }
- }
- public static void tryToAttackEnemyCastle(Elf elf,Game game)
- {
- //if elf in Attack Range: attack enemy Casle
- if (elf.inAttackRange(game.getEnemyCastle())&&(isSafeLoc(elf.getLocation(),game)||game.getEnemyCastle().currentHealth<=elf.attackMultiplier*elf.currentHealth))
- {
- elf.attack(game.getEnemyCastle());
- //.out.println("elf " + elf + " attacks " + game.getEnemyCastle());
- }
- //go back safely to My casle
- else {
- //////System.out.println("moving saftley back to Base");
- elf.moveTo(safteyLoc(elf.getLocation(),game.getEnemyCastle().getLocation(),elf.maxSpeed,game));
- ////////System.out.println("elf " + elf + " moves to " + game.getEnemyCastle());
- }
- }
- public static Portal portalToDestroy(Game game){
- if(closestEnemyPortalToOurCastle(game)==null)
- return null;
- Portal OEnemyPortal=game.getEnemyPortals()[0];
- for(Portal EnemyPortal : game.getEnemyPortals()){
- if(EnemyPortal.distance(game.getMyCastle())<EnemyPortal.distance(game.getEnemyCastle())){
- if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
- OEnemyPortal=EnemyPortal;
- }
- }
- }
- ////System.out.println(OEnemyPortal);
- if(OEnemyPortal.distance(game.getMyCastle())<OEnemyPortal.distance(game.getEnemyCastle()))
- return OEnemyPortal;
- else
- return null;
- }
- public static int numberOfElvesNeededForAction(int action, Game game) {
- /*Actions: NOT BY PRIORITY!!!
- *0: Attack enemy elf
- *1: Build on the way portal (A lot of mana)
- *2: Build defense portal
- *3: Destroy enemys attacking portal
- *4: Going to enemy attacking elf
- *5: Build attacking portal --> (first side?)
- *6: Building good portal
- *7: Destroy enemy portals (Any enemy portal)
- *8: Attack enemy castle
- *9: Build mana fountains
- *10: Destroy enemy mana fountains
- *
- */
- if(action==ActiontryToAttackEnemyElf||action==ActiontryToBuildPortalCauseCan||action==ActionBuildAttackPortalWhenCan)
- {
- //////System.out.println("CALLING A PRIVATE ACTION ");
- return 0;
- }
- else if(action==ActionDefPortal) {//2
- // if elf needs help ++
- // && other elf is far to another action's needed positions
- if(isThereDefPortals(game))return 0;
- else return 1;
- }
- else if(action==ActionEnemyAttackingPortal) {//3
- //if elf needs help ++
- //&& other elf is far to another action's needed positions
- int count=0;
- Portal EnemyPortal=closestEnemyPortalToOurCastle(game);
- if(EnemyPortal==null||!isThereClosePortalToCastle(game))
- return 0;
- for(Elf EnemyElf:enemyLivingElves) {
- if(EnemyElf.distance(EnemyPortal)<EnemyElf.maxSpeed*5)//&&isObjectGoingForwardMe(EnemyElf,EnemyPortal,game)) {
- count++;
- }
- if(EnemyThreatningPortal(game)!=null)
- return 1;
- return count;
- }
- else if(action==ActionEnemyElfAttacking) {
- // WHEN ICE WALL IS READY CHANGE
- // if elf needs help 1++ (until elf doesn't need help)
- // && other elf is not attacking (building attack portal);
- int dangerousElves = 0;
- Elf minElf = null;
- for(Elf enemyElf: game.getEnemyLivingElves()) {
- Location locOfEnemyElf=enemyElf.getLocation();
- if(!game.inMap(enemyElf))
- locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
- if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=15) {
- dangerousElves++;
- if(game.getMyLivingElves().length>0)
- {
- minElf=game.getMyLivingElves()[0];
- for(Elf elf:game.getMyLivingElves())
- {
- if(elf.distance(locOfEnemyElf)<minElf.distance(locOfEnemyElf))
- {
- minElf=elf;
- }
- }
- if(minElf.currentHealth<enemyElf.currentHealth)
- dangerousElves++;
- }
- }
- }
- return dangerousElves;
- }
- else if(action==ActionEnemyElfAttackingA) {
- // WHEN ICE WALL IS READY CHANGE
- // if elf needs help 1++ (until elf doesn't need help)
- // && other elf is not attacking (building attack portal);
- int dangerousElves = 0;
- Elf minElf = null;
- for(Elf enemyElf: game.getEnemyLivingElves()) {
- Location locOfEnemyElf=enemyElf.getLocation();
- if(!game.inMap(enemyElf))
- locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
- if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=4) {
- dangerousElves++;
- if(game.getMyLivingElves().length>0)
- {
- minElf=game.getMyLivingElves()[0];
- for(Elf elf:game.getMyLivingElves())
- {
- if(elf.distance(locOfEnemyElf)<minElf.distance(locOfEnemyElf))
- {
- minElf=elf;
- }
- }
- if(minElf.currentHealth<enemyElf.currentHealth)
- dangerousElves++;
- }
- }
- }
- return dangerousElves;
- }
- else if(action==ActionBuildAttackingPortals) {
- //if we dont have an attacking portal
- // MAYBE DO THIS boolean enemyCastleAlmostDead = false;
- //*** CHEN DOES THIS but if there are left elves we add them to attack
- //if there are no attacking portals summon one elf
- if((numOfEnoughClosePortalToEnemyCastle(game)==0))
- return game.getAllMyElves().length;
- //if there is atting portal and there is not many elfs return 0
- else if(numOfEnoughClosePortalToEnemyCastle(game)==1) {
- return 1;
- }
- else
- return 0;
- }
- else if(action==ActionDestroyEnemyBuildings) {
- return game.getEnemyManaFountains().length+game.getEnemyPortals().length;
- }
- else if(action==ActionBuildManaFountains){
- if(!shouldIbuildManaFountain(game))
- return 0;
- else
- return 1;
- }
- else if(action==ActionDestroyEnemyManaFountains)
- {
- if(getEnemyBestManaFountain(game)!=null)
- return 1;
- else
- return 0;
- }
- else if(action==ActionBuildDefManaFountains)
- {
- if(whereToDefMana(game)!=null)
- return 1;
- return 0;
- }
- return 0;
- }
- public static GameObject[] enemysCloseToLoc(Building build,Game game)
- {
- int length=game.getEnemyIceTrolls().length+game.getEnemyLivingElves().length;
- GameObject[] arr=new GameObject[length];
- int counter=0;
- for(IceTroll ice:game.getEnemyIceTrolls())
- {
- if(ice.distance(build)<=ice.maxSpeed*6+build.size)
- {
- arr[counter]=ice;
- counter++;
- }
- }
- for(Elf elf:game.getEnemyLivingElves())
- {
- if(elf.distance(build)<=elf.maxSpeed*6+build.size)
- {
- arr[counter]=elf;
- counter++;
- }
- }
- counter=0;
- for(GameObject obj:arr)
- {
- if(obj!=null)
- counter++;
- }
- GameObject[] ret=new GameObject[counter];
- for(int i=0;i<ret.length;i++)
- {
- ret[i]=arr[i];
- }
- return ret;
- }
- public static Location whereElfShouldAttackBuilding(Elf elf,Building build,Game game)
- {
- GameObject[] arr=enemysCloseToLoc(build,game);
- int x=0;
- int y=0;
- for(GameObject obj:arr)
- {
- //System.out.print(obj+",");
- if(x==0&&y==0)
- {
- x=obj.getLocation().row;
- y=obj.getLocation().col;
- }
- else
- {
- x=(x+obj.getLocation().row)/2;
- y=(y+obj.getLocation().col)/2;
- }
- }
- //System.out.println();
- Location middle=new Location(x,y);
- Location ret=trigoLocation(build.getLocation().row,build.getLocation().col,middle,(build.size+elf.attackRange),180);
- if((x!=0||y!=0)&&game.inMap(ret))
- {
- //System.out.println(ret);
- return ret;
- }
- else
- {
- int[] degrees=new int[180];
- degrees[0]=0;
- int degreesCounter=1;
- Castle myCastle=game.getMyCastle();
- int startingRad=game.portalSize+elf.attackRange/2;
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- boolean found=false;
- double MaxDist=build.size+elf.attackRange*3;
- Location locClose=build.getLocation();
- for(int radius=startingRad;radius<MaxDist;radius+=elf.attackRange)
- {
- for(int i=0;i<degrees.length;i++)
- {
- Location loc=trigoLocation(locClose.row,locClose.col,elf.getLocation(),radius,degrees[i]);
- if(isSafeLoc(loc,game))
- {
- return loc;
- }
- }
- }
- return locClose;
- }
- }
- public static Elf closestElfToEnemyManaFountain(ManaFountain mana,Game game){
- if(game.getMyLivingElves().length==0||game.getEnemyManaFountains().length==0)
- return null;
- Elf OriginalEnemyElf=game.getMyLivingElves()[0];
- for(Elf Elf:game.getMyLivingElves()) {
- if(Elf.distance(mana)<=OriginalEnemyElf.distance(mana)) {
- OriginalEnemyElf=Elf;
- }
- }
- return OriginalEnemyElf;
- }
- public static ManaFountain getEnemyBestManaFountain(Game game)
- {
- if(game.getEnemyManaFountains().length==0)
- return null;
- int[] numberOfEnemies=new int[game.getEnemyManaFountains().length];
- for(int i=0;i<game.getEnemyManaFountains().length;i++)
- {
- numberOfEnemies[i]=0;
- for(Elf EnemyElf:game.getEnemyLivingElves()){
- if(EnemyElf.distance(game.getEnemyManaFountains()[i])<EnemyElf.maxSpeed*10&&game.getEnemyManaFountains()[i].currentHealth>game.manaFountainMaxHealth/2){
- numberOfEnemies[i]++;
- }
- }
- for(IceTroll EnemyIce:game.getEnemyIceTrolls()){
- if(EnemyIce.distance(game.getEnemyManaFountains()[i])<EnemyIce.maxSpeed*10&&EnemyIce.currentHealth>5&&game.getEnemyManaFountains()[i].currentHealth>game.manaFountainMaxHealth/2){
- numberOfEnemies[i]++;
- }
- }
- }
- int[] countPortals=new int[game.getEnemyManaFountains().length];
- ManaFountain min=game.getEnemyManaFountains()[0];
- int index=0;
- for(Portal ePortal:game.getEnemyPortals())
- {
- for(int i=0;i<game.getEnemyManaFountains().length;i++)
- {
- if(min.distance(ePortal)>game.getEnemyManaFountains()[i].distance(ePortal)){
- min=game.getEnemyManaFountains()[i];
- index=i;
- }
- }
- countPortals[index]++;
- }
- ManaFountain best=game.getEnemyManaFountains()[0];
- int minIndex=0;
- for(int i=0;i<numberOfEnemies.length;i++){
- if(numberOfEnemies[i]<numberOfEnemies[minIndex]){
- best=game.getEnemyManaFountains()[i];
- minIndex=i;
- }
- /* else if(numberOfEnemies[i]==numberOfEnemies[minIndex]&&countPortals[i]<countPortals[minIndex]) {
- best=game.getEnemyManaFountains()[i];
- minIndex=i;
- }*/
- }
- if(numberOfEnemies[minIndex]<3||best.currentHealth<game.manaFountainMaxHealth/2){
- return best;
- }
- return null;
- }
- public static void destroyEnemysFountains(Elf elf,Game game)
- {
- ManaFountain min=getEnemyBestManaFountain(game);
- Location loc=whereElfShouldAttackBuilding(elf,min,game);
- boolean isSpeed=(elf.maxSpeed>game.elfMaxSpeed);
- if(elf.inAttackRange(min)&&isItSafeToAttackEnemyBuilding(elf,min,game)&&(!isSpeed||isSpeed&&elf.distance(loc)<elf.maxSpeed*1.1))
- elf.attack(min);
- else
- {
- if(elf.distance(min)/elf.maxSpeed>=game.speedUpExpirationTurns+2&&!isSpeed&&elf.canCastSpeedUp()&&ShouldElfUseSpeedUpFORELF(elf,min.getLocation(),game))
- elf.castSpeedUp();
- else if(isSpeed)
- {
- elf.moveTo(safteyLoc(elf.getLocation(),loc,elf.maxSpeed,game));
- }
- else
- elf.moveTo(safteyLoc(elf.getLocation(),min.getLocation(),elf.maxSpeed,game));
- }
- }
- public static void tryToBuildManaFountain(Elf elf, Game game) {
- Location manaFountainBuildLoc=whereToBuildManaFountain(game);
- ////System.out.println(elf.canBuildManaFountain());
- if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*1.5&&elf.canBuildManaFountain()&&game.getMyMana()>=game.manaFountainCost)
- {
- ////System.out.println("building mana fountain");
- elf.buildManaFountain();
- }
- else
- {
- ////System.out.println("going to build mana fountain");
- if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*2)
- elf.moveTo(manaFountainBuildLoc);
- else
- elf.moveTo(safteyLoc(elf.getLocation(),manaFountainBuildLoc,elf.maxSpeed,game));
- }
- }
- public static boolean isElfBuildingAt(Location loc,Game game)
- {
- for(Elf elf:game.getMyLivingElves())
- {
- if(elf.distance(loc)<=elf.maxSpeed&&elf.isBuilding)
- return true;
- }
- return false;
- }
- public static boolean shouldIbuildManaFountain(Game game) {
- Location locationToBuildManaFountain=whereToBuildManaFountain(game);
- if(!game.canBuildManaFountainAt(locationToBuildManaFountain)||game.getMyManaFountains().length>game.getMyPortals().length){
- ////System.out.println("cant build at mana loc ");
- return false;
- }
- if(locationToBuildManaFountain!=null&&isItSmartToBuildManaFountain(locationToBuildManaFountain,game)&&(game.getMyself().manaPerTurn*7>=game.manaFountainCost-game.getMyself().mana||ShouldSaveManaForManaFountain(game))) {
- if((game.getMyself().manaPerTurn<=game.getEnemy().manaPerTurn||game.getMyself().manaPerTurn==0)) {
- return true;
- }
- else if(game.getMyself().manaPerTurn>game.getEnemy().manaPerTurn) {
- if(game.getMyPortals().length>game.getMyManaFountains().length&&game.getMyMana()>game.manaFountainCost*1.5) { //note to change
- return true;
- }
- }
- }
- return false;
- }
- public static boolean isItSmartToBuildManaFountain(Location buildLocation, Game game){
- //wiil the enemy destroy it:
- boolean flag=false;
- for(Elf enemyElf:enemyLivingElves){
- flag=false;
- if(enemyElf.distance(buildLocation)<=game.elfAttackRange+game.manaFountainSize+enemyElf.maxSpeed*(game.manaFountainBuildingDuration+4)){
- for(Portal portal:game.getMyPortals()){
- if(portal.distance(enemyElf)<buildLocation.distance(enemyElf)){
- flag=true;
- }
- }
- if(!flag)
- return false;
- }
- }
- return true;
- }
- public static Location whereToBuildManaFountain(Game game) {
- Location whereToBuildManaFountain=null;
- for(Portal portal:game.getMyPortals()) {
- if(portal.distance(game.getMyCastle().getLocation())<game.manaFountainSize*5+game.getMyCastle().size) {
- whereToBuildManaFountain=trigoLocation(portal.getLocation().row,portal.getLocation().col,game.getEnemyCastle().getLocation(),portal.size+game.manaFountainSize+1,180);
- if(game.canBuildManaFountainAt(whereToBuildManaFountain)&&!isElfBuildingAt(whereToBuildManaFountain,game)) {
- return whereToBuildManaFountain;
- }
- }
- }
- //If there are no portals:
- int[] degrees=new int[360];
- degrees[0]=180;
- int degreesCounter=1;
- Castle myCastle=game.getMyCastle();
- int startingRad=20+myCastle.size;
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- boolean aretheredefportals=isThereDefPortals(game);
- double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
- Location locClose= game.getEnemyCastle().getLocation();
- for(int radius=startingRad;radius<MaxDist;radius+=game.manaFountainSize)
- {
- for(int i=0;i<degrees.length;i++)
- {
- Location loc=trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
- if(game.canBuildManaFountainAt(loc)&&!isElfBuildingAt(loc,game))
- {
- if(!aretheredefportals&&loc.distance(whereElfShouldBuildDefPortal(game))>game.portalSize+game.manaFountainSize+10||aretheredefportals)
- return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
- }
- }
- }
- return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
- }
- public static Elf closestEnemyElfToObject(MapObject object, Game game) {
- if(enemyLivingElves.length==0||object==null)
- return null;
- Elf mElf=enemyLivingElves[0];
- for(Elf EnemyElf :enemyLivingElves)
- {
- if(EnemyElf.distance(object)+EnemyElf.maxSpeed*1.5<=mElf.distance(object))
- {
- mElf=EnemyElf;
- }
- }
- return mElf;
- }
- public static Location whereElfShouldBuildDefPortal(Game game)
- {
- int[] degrees=new int[20];
- degrees[0]=0;
- int degreesCounter=1;
- Castle myCastle=game.getMyCastle();
- double startingRad=game.portalSize+myCastle.size+1.5;
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- boolean found=false;
- double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
- Location locClose;
- if(EnemyThreatningPortal(game)!=null&&EnemyThreatningPortal(game).distance(game.getMyCastle())<=myCastle.distance(game.getEnemyCastle())/2)
- locClose= EnemyThreatningPortal(game).getLocation();
- else
- locClose=game.getEnemyCastle().getLocation();
- for(double radius=startingRad;radius<MaxDist;radius+=game.portalSize)
- {
- for(int i=0;i<degrees.length;i++)
- {
- if(game.canBuildPortalAt(trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i])))
- {
- return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
- }
- }
- }
- return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
- }
- public static boolean isThereDefPortals(Game game)
- {
- //int counter=0;
- //for(Portal portal:game.getMyPortals())
- //{
- // if(portal.distance(game.getMyCastle())-game.castleSize-game.lavaGiantAttackRange<=game.lavaGiantMaxSpeed*5)
- // counter++;
- //}
- Location locClose=null;
- if(EnemyThreatningPortal(game)!=null&&EnemyThreatningPortal(game).distance(game.getMyCastle())<=game.getMyCastle().distance(game.getEnemyCastle())/2)
- locClose= EnemyThreatningPortal(game).getLocation();
- else
- locClose=game.getEnemyCastle().getLocation();
- Location loc=game.getMyCastle().getLocation().towards(locClose,game.portalSize+game.getMyCastle().size+5);
- Location port=closestAllyPortalToLoc(loc,game);
- ////System.out.println(loc+" WHERE TO BUILD DEF PORTAL");
- if(!game.canBuildPortalAt(loc))
- return true;
- if(port==null)
- return false;
- if(port.distance(loc)<game.portalSize)
- return true;
- return false;
- }
- public static boolean isItSmartToBuildPortal(Elf elf, Game game)
- {
- for(Elf enemyElf: enemyLivingElves)
- {
- if((enemyElf.distance(elf)-enemyElf.attackRange)/enemyElf.maxSpeed<=game.iceTrollSummoningDuration+game.portalBuildingDuration&&enemyElf.currentHealth>game.portalMaxHealth*enemyElf.attackMultiplier)
- {
- return false;
- }
- if(enemyElf.distance(elf)<=enemyElf.maxSpeed*2+enemyElf.attackRange)
- return false;
- if(elf.maxSpeed>game.elfMaxSpeed)
- return false;
- }
- for(Tornado enTorn : game.getEnemyTornadoes()) {
- if(enTorn.currentHealth / game.tornadoSuffocationPerTurn >= game.portalBuildingDuration + (enTorn.distance(elf) - game.portalSize - enTorn.attackRange) / game.tornadoMaxSpeed) {
- return false;
- }
- }
- for(Portal portal:game.getMyPortals())
- {
- if(portal.distance(elf)<=portal.size*4&&elf.distance(game.getEnemyCastle())>elf.distance(game.getMyCastle()))
- return false;
- }
- if(ShouldSaveManaForManaFountain(game))
- return false;
- ////////System.out.println("smart building portal");
- return true;
- }
- public static boolean isItSafeToBuildPortal(Elf elf,Game game)
- {
- int enemyCounter=0;
- for(Elf enemyElf: enemyLivingElves)
- {
- if(enemyElf.distance(elf)<=enemyElf.maxSpeed*3+enemyElf.attackRange)
- {
- enemyCounter+=elf.attackMultiplier;
- }
- }
- for(IceTroll enemyIce: game.getEnemyIceTrolls())
- {
- if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*3)
- {
- enemyCounter+=enemyIce.attackMultiplier;
- }
- }
- if(enemyCounter==0)
- return true;
- if(elf.currentHealth-enemyCounter*1.3>game.portalBuildingDuration)
- return true;
- return false;
- }
- public static boolean isItSafeToAttackEnemyBuilding(Elf elf,Building building,Game game)
- {
- int enemyCounter=0;
- for(Elf enemyElf: enemyLivingElves)
- {
- if(enemyElf.distance(elf)<=enemyElf.maxSpeed+enemyElf.attackRange)
- {
- enemyCounter+=elf.attackMultiplier;
- }
- }
- for(IceTroll enemyIce: game.getEnemyIceTrolls())
- {
- if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed)
- {
- enemyCounter+=enemyIce.attackMultiplier;
- }
- }
- if(enemyCounter==0)
- return true;
- if(elf.currentHealth-enemyCounter>building.currentHealth+2)
- return true;
- return false;
- }
- public static Location whereElfShouldBuildAttackPortalSides(Elf elf,Game game)
- {
- int[] degrees=new int[360];
- degrees[0]=180;
- int degreesCounter=179;
- Castle enemyCastle=game.getEnemyCastle();
- int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter--;
- }
- boolean found=false;
- int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
- for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
- {
- for(int i=1;i+1<degrees.length;i+=2)
- {
- Location loc1=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i]);
- Location loc2=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i+1]);
- if(game.canBuildPortalAt(loc1)&&game.canBuildPortalAt(loc2))
- {
- if(elf.distance(loc1)<elf.distance(loc2))
- return loc1;
- else
- return loc2;
- }
- }
- }
- startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
- MaxDist=(int)(game.getMyCastle().distance(enemyCastle)/1.5);
- for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
- {
- for(int i=1;i+1<degrees.length;i+=2)
- {
- Location loc1=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i]);
- Location loc2=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i+1]);
- if(game.canBuildPortalAt(loc1))
- return loc1;
- if(game.canBuildPortalAt(loc2))
- return loc2;
- }
- }
- ////System.out.println("NO WHERE TO BUILD ATTACKING PORT");
- return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
- }
- public static Location whereElfShouldBuildAttackPortal(Elf elf,Game game)
- {
- int[] degrees=new int[360];
- degrees[0]=0;
- int degreesCounter=1;
- Castle enemyCastle=game.getEnemyCastle();
- int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- boolean found=false;
- int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
- for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
- {
- for(int i=0;i<degrees.length;i++)
- {
- if(game.canBuildPortalAt(trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i])))
- {
- return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i]);
- }
- }
- }
- return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
- }
- public static Portal getClosestEnemyPortal(Elf elf,Game game)
- {
- if(game.getEnemyPortals().length==0)
- return null;
- Portal minP=game.getEnemyPortals()[0];
- for(Portal p : game.getEnemyPortals())
- {
- if(elf.distance(p)<elf.distance(minP))
- {
- minP=p;
- }
- }
- return minP;
- }
- public static Location whereFightWillHappen(Elf elf,Game game)
- {
- int xCenterOfFight=elf.getLocation().row;
- int yCenterOffFight=elf.getLocation().col;
- for(Elf allyElf : game.getMyLivingElves())
- {
- if(allyElf!=elf&&allyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
- {
- xCenterOfFight=(xCenterOfFight+allyElf.getLocation().row)/2;
- yCenterOffFight=(yCenterOffFight+allyElf.getLocation().col)/2;
- }
- }
- for(Elf enemyElf : enemyLivingElves)
- {
- if(enemyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
- {
- xCenterOfFight=(xCenterOfFight+enemyElf.getLocation().row)/2;
- yCenterOffFight=(yCenterOffFight+enemyElf.getLocation().col)/2;
- }
- }
- Location retLoc=new Location(xCenterOfFight,yCenterOffFight);
- return retLoc;
- }
- public static boolean isIceGoingOnElf(IceTroll ice,Game game)
- {
- if(enemyLivingElves.length==0)
- return false;
- GameObject minObj=enemyLivingElves[0];
- for(Elf elf:enemyLivingElves)
- {
- if(ice.distance(elf)<ice.distance(minObj))
- minObj=elf;
- }
- for(Creature creat:game.getEnemyCreatures())
- {
- if(creat.distance(ice)<minObj.distance(ice))
- return false;
- }
- return true;
- }
- public static boolean isWorthAttacking(Elf elf,double agroLevel, Game game)
- {
- int allyElvesAround=1;
- int enemyElvesAround=0;
- int allyLivesAround=elf.currentHealth;
- int enemyLivesAround=0;
- int isFightUrgent = 0;
- Location fightLoc=whereFightWillHappen(elf,game);
- if(fightLoc.distance(game.getMyCastle()) - game.getMyCastle().size <= game.elfMaxSpeed * 2 + game.elfAttackRange)
- isFightUrgent = 1;
- if(elf.distance(fightLoc)>elf.maxSpeed*5+elf.attackRange)
- return false;
- for(Elf enemyElf : enemyLivingElves)
- {
- if(enemyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
- {
- enemyElvesAround++;
- enemyLivesAround+=enemyElf.currentHealth;
- }
- }
- for(Elf allyElf : game.getMyLivingElves())
- {
- if(allyElf!=elf)
- {
- if(allyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
- {
- allyElvesAround++;
- allyLivesAround+=allyElf.currentHealth;
- }
- }
- }
- if(enemyLivesAround>game.elfMaxHealth&&allyLivesAround * (1 + (isFightUrgent * 0.4))<=enemyLivesAround)
- return false;
- for(IceTroll allyIce : game.getMyIceTrolls())
- {
- if(allyIce.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange&&isIceGoingOnElf(allyIce,game))
- {
- if(allyIce.currentHealth-(allyIce.distance(fightLoc)/allyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth)
- {
- allyElvesAround++;
- allyLivesAround+=allyIce.currentHealth;
- }
- }
- }
- for(IceTroll enemyIce : game.getEnemyIceTrolls())
- {
- if(enemyIce.currentHealth-(enemyIce.distance(fightLoc)/enemyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth||enemyIce.attackRange>=enemyIce.distance(fightLoc))
- {
- enemyElvesAround++;
- enemyLivesAround+=enemyIce.currentHealth;
- }
- }
- if(allyElvesAround>=enemyElvesAround&&allyLivesAround*game.elfAttackMultiplier * (1 + (isFightUrgent * 0.4)) >enemyLivesAround*game.elfAttackMultiplier+agroLevel)
- {
- ////System.out.println("WORTH ATTACKING "+elf);
- return true;
- }
- return false;
- }
- public static int indexOfEnemyElf(Elf enemy,Game game)
- {
- for(int i=0;i<game.getAllEnemyElves().length;i++)
- {
- if(game.getAllEnemyElves()[i]==enemy)
- return i;
- }
- return 0;
- }
- public static Elf isThereEnemyElfAttacking(int turns, Game game)
- {
- Elf minElf=null;
- for(Elf enemyElf:game.getEnemyLivingElves())
- {
- Location locOfEnemyElf=enemyElf.getLocation();
- if(!game.inMap(enemyElf))
- locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
- if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=turns)
- {
- if(minElf==null)
- minElf=enemyElf;
- else
- {
- if(minElf.distance(game.getMyCastle())>locOfEnemyElf.distance(game.getMyCastle()))
- minElf=enemyElf;
- }
- }
- }
- System.out.println(minElf+" ENEMY ELF ATTACKING");
- return minElf;
- }
- public static void GoingToEnemyAttackingElf(Elf elf,Game game) {
- Elf enemyElf=isThereEnemyElfAttacking(15,game);
- Location locOfEnemyElf=enemyElf.getLocation();
- if(!game.inMap(enemyElf))
- locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
- if(elf.inAttackRange(enemyElf)&&isWorthAttacking(elf,-1,game))
- elf.attack(enemyElf);
- else if(isWorthAttacking(elf,-1,game)&&elf.distance(locOfEnemyElf)<=elf.attackRange+elf.maxSpeed)
- elf.moveTo(locOfEnemyElf);
- else
- {
- int manaToSave=0;
- for(Portal APortal: game.getMyPortals()){
- if(isThereEnemyElfNearPortal(APortal,game)&&!AttackingPortalsSummonigFire(APortal,game))
- manaToSave=game.iceTrollCost;
- }
- if(elf.distance(locOfEnemyElf)/(game.speedUpMultiplier*elf.maxSpeed)>=game.speedUpExpirationTurns&&elf.maxSpeed<=game.elfMaxSpeed&&elf.canCastSpeedUp()&&ShouldElfUseSpeedUpFORELF(elf,locOfEnemyElf,game)&&game.getMyMana()>manaToSave+game.getMyself().manaPerTurn)
- elf.castSpeedUp();
- else
- elf.moveTo(safteyLoc(elf.getLocation(),locOfEnemyElf,elf.maxSpeed,game));
- }
- }
- public static Elf elfToFocus(Elf elf,Game game)
- {
- if(enemyLivingElves.length==0)
- return null;
- Elf elfToAttack=null;
- Location fightLocation=whereFightWillHappen(elf,game);
- for(Elf Eelf:enemyLivingElves)
- {
- if(elfToAttack==null)
- {
- if(Eelf.distance(fightLocation)<=Eelf.maxSpeed*3)
- elfToAttack=Eelf;
- }
- else if(Eelf.currentHealth<=elfToAttack.currentHealth-2&&Eelf.distance(fightLocation)<=Eelf.maxSpeed*5&&Eelf.distance(fightLocation)-Eelf.maxSpeed*3<elfToAttack.distance(fightLocation))
- elfToAttack=Eelf;
- }
- return elfToAttack;
- }
- public static boolean tryToAttackElf(Elf elf,Game game)
- {
- if(!isWorthAttacking(elf,5,game))
- return false;
- boolean attacked=false;
- Elf enemyElf=elfToFocus(elf,game);
- if(enemyElf==null)
- return false;
- if(elf.inAttackRange(enemyElf))
- {
- attacked=true;
- }
- //EXPLAIN!
- else if(enemyElf.distance(elf)<game.elfMaxSpeed*5+elf.attackRange&&isEnemyElfAttackingOurElf(elf,enemyElf,game)==true || enemyElf.distance(elf)<elf.maxSpeed ||(enemyElf.distance(elf)<=game.elfMaxSpeed*2+game.elfAttackRange&&isObjectGoingForwardMe(enemyElf,elf,game)))
- {
- if(enemyElf.distance(elf)<game.elfMaxSpeed*5+elf.attackRange&&isEnemyElfAttackingOurElf(elf,enemyElf,game)==true)
- System.out.println("helping other elf "+elf+" "+enemyElf);
- else if(enemyElf.distance(elf)<=game.elfMaxSpeed+game.elfAttackRange)
- System.out.println("VERY CLOSE "+elf+" "+enemyElf);
- else
- System.out.println("close and going forward me "+elf+" "+enemyElf);
- if(!(elf.distance(game.getMyCastle())>elf.distance(game.getEnemyCastle())&&isThereEnemyElfAttacking(15,game)!=null))
- {
- if(closestEnemyPortalToOurCastle(game)!=null)
- {
- if(enemyElf.distance(game.getMyCastle())<closestEnemyPortalToOurCastle(game).distance(game.getMyCastle()))
- {
- //if(getEnemyAttackingElf)
- attacked=true;
- }
- }
- else
- attacked=true;
- }
- }
- return attacked;
- }
- public static void attackEnemyElf(Elf elf,Game game) {
- Elf enemyElf=elfToFocus(elf,game);
- if(elf.inAttackRange(enemyElf))
- {
- //////System.out.println("in Attack Range so attacking");
- elf.attack(enemyElf);
- }
- else {
- //////System.out.println("moving to attack");
- elf.moveTo(enemyElf);
- }
- }
- public static boolean isEnemyElfAttackingOurElf(Elf elfWantsToHelp,Elf enemyElf,Game game)
- {
- for(Elf elf : game.getMyLivingElves())
- {
- if(elfWantsToHelp!=elf&&enemyElf.distance(elf)<=elf.maxSpeed+elf.attackRange&&isObjectGoingForwardMe(enemyElf,elf,game))
- return true;
- }
- return false;
- }
- /*public static boolean isThereClosePortalToCastle(Game game)
- {
- boolean isThereClosePortal=false;
- for(Portal portal :game.getEnemyPortals())
- {
- int turnsToFire=(portal.distance(game.getMyCastle())-game.castleSize-game.lavaGiantAttackRange)/game.lavaGiantMaxSpeed;
- if(game.lavaGiantMaxHealth-turnsToFire>4)
- {
- isThereClosePortal=true;
- }
- }
- return isThereClosePortal;
- }*/
- public static boolean isThereClosePortalToCastle(Game game)
- {
- boolean isThereClosePortal=false;
- for(Portal portal :game.getEnemyPortals())
- {
- if(portal.distance(game.getMyCastle())/(portal.distance(game.getEnemyCastle())+0.1)<=1.3)
- {
- isThereClosePortal=true;
- }
- }
- return isThereClosePortal;
- }
- public static Portal closestEnemyPortalToOurCastle(Game game)
- {
- if(game.getEnemyPortals().length==0)
- return null;
- Portal mPortal=game.getEnemyPortals()[0];
- for(Portal portal :game.getEnemyPortals())
- {
- if(portal.distance(game.getMyCastle())<=mPortal.distance(game.getMyCastle()))
- {
- mPortal=portal;
- }
- }
- return mPortal;
- }
- public static int numOfEnoughClosePortalToEnemyCastle(Game game)
- {
- int counter=0;
- for(Portal portal :game.getMyPortals())
- {
- if(portal.distance(game.getEnemyCastle())<=portal.distance(game.getMyCastle()))
- {
- counter++;
- }
- }
- return counter;
- }
- public static boolean isSafeLocForSike(Location loc, Location preLoc,Game game)
- {
- int enemyCount=0;
- for(Elf enemy: enemyLivingElves)//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
- // //////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
- {
- int allycount=0;
- for(Elf elf : game.getMyLivingElves())
- {
- if(elf.distance(loc)<=elf.attackRange*1.1)
- allycount++;
- }
- enemyCount++;
- //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
- return false;
- }
- }
- for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
- // //////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
- {
- enemyCount++;
- if(enemyCount>=1)
- return false;
- }
- }
- if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
- return false;
- return true;
- }
- public static boolean isSafeLoc(Location loc,Game game)
- {
- int enemyCount=0;
- /*for(Mothership mother: game.getAllMotherships())
- {
- if(mother.distance(loc)<game.pirateMaxSpeed)
- return true;
- }//WTF*/
- for(Elf enemy: enemyLivingElves)//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
- // //////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
- {
- int allycount=0;
- for(Elf elf : game.getMyLivingElves())
- {
- if(elf.distance(loc)<=elf.attackRange*1.1+enemy.maxSpeed)
- allycount++;
- }
- enemyCount++;
- if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
- return false;
- }
- }
- for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
- // //////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
- {
- enemyCount++;
- if(enemyCount>=1)
- return false;
- }
- }
- if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
- return false;
- //if(loc.col<0+game.elfMaxSpeed*6||loc.row<0+game.elfMaxSpeed*6||loc.row>game.rows-game.elfMaxSpeed*6||loc.col>game.cols-game.elfMaxSpeed*6)
- //{
- // return false;
- //}
- //if(loc.row<0.6*game.elfAttackRange||loc.col<0.6*game.elfAttackRange||loc.row>game.rows-0.6*game.elfAttackRange||loc.col>game.cols-0.6*game.elfAttackRange)
- // return false;
- return true;
- }
- public static Location trigoLocation(int nearLocX, int nearLocY,Location diractionLoc,double radius,double angle){
- double zavit=(nearLocY-diractionLoc.col+0.0)/(nearLocX+0.0-diractionLoc.row);
- if(nearLocX+0.0-diractionLoc.row==0)
- {
- if(nearLocY>=diractionLoc.col)
- zavit=90;
- else
- zavit=-90;
- }
- double zavitEnemyBase=(Math.atan(zavit)+(Math.PI/180.0)*angle+0.0);
- if(nearLocX<diractionLoc.row)
- {
- zavitEnemyBase=Math.PI+zavitEnemyBase;
- }
- int xRet=0;
- xRet= (int)(nearLocX-radius*Math.cos(zavitEnemyBase));
- int yRet=0;
- yRet= (int)(nearLocY-radius*Math.sin(zavitEnemyBase));
- Location defRow1= new Location(xRet,yRet);
- return defRow1;
- }
- public static double safteyRate(Location loc,Location preLoc,Game game)
- {
- double enemyCount=0;
- for(Elf enemy: enemyLivingElves)//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
- // ////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
- {
- enemyCount+=enemyNextLoc.distance(loc);
- //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
- }
- }
- for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
- {
- double enemySpeed= enemy.maxSpeed;
- Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
- // ////System.out.println(enemySpeed+" id "+enemy.id);
- if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
- {
- enemyCount+=enemyNextLoc.distance(loc);
- }
- }
- if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
- return 0;//game.getEnemyIceTrolls().length+enemyLivingElves.length;
- return enemyCount;
- }
- public static Location safteyLoc(Location elf,Location diractionLoc,int radius2,Game game)
- {
- //if(elf.distance(diractionLoc)<radius2)
- // radius2=elf.distance(diractionLoc);
- if(diractionLoc.row<=0||diractionLoc.col<=0||diractionLoc.col>=game.cols||diractionLoc.row>=game.rows)
- return diractionLoc;
- //radius=game.pirateMaxSpeed;
- int[] degrees=new int[360];
- degrees[0]=0;
- int degreesCounter=1;
- //radius=game.elfMaxSpeed;
- for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
- {
- degrees[i]=degreesCounter;
- degrees[i+1]=degreesCounter*-1;
- degreesCounter++;
- }
- Location loc=trigoLocation(elf.row,elf.col,diractionLoc,radius2,degrees[0]);
- double safteyRateInt=safteyRate(diractionLoc,elf,game);
- for(int radius=radius2;radius<=radius2*2;radius+=50)
- {
- for(int i=0;i<degrees.length;i++)
- {
- Location locToCheck=trigoLocation(elf.row,elf.col,diractionLoc,radius,degrees[i]);
- if(isSafeLocForSike(locToCheck,elf,game))
- {
- //if(isSafeLocForSike(trigoLocation(elf.row,elf.col,diractionLoc,radius+50,degrees[i]),elf,game))
- //{
- //////System.out.println(degrees[i]);
- if(closestAllyPortalToLoc(elf,game)!=null&&Math.abs(degrees[i])>90&&elf.distance(closestAllyPortalToLoc(elf,game))>=game.elfMaxSpeed*1.5)
- {
- if(diractionLoc==closestAllyPortalToLoc(elf,game))
- return locToCheck;
- else
- return safteyLoc(elf,closestAllyPortalToLoc(elf,game),game.elfMaxSpeed,game);
- }
- return locToCheck;
- //}
- }
- double safteyRateCurrent=safteyRate(locToCheck,elf,game);
- if(safteyRateInt<safteyRateCurrent)
- {
- loc=locToCheck;
- safteyRateInt=safteyRateCurrent;
- }
- }
- }
- System.out.println(elf+" choses most safe loc");
- return loc;
- }
- public static Location closestAllyPortalToLoc(Location loc,Game game)
- {
- if(game.getMyPortals().length==0)
- return null;
- Portal maxPort=game.getMyPortals()[0];
- for(Portal portal:game.getMyPortals())
- {
- if(portal.distance(loc)<maxPort.distance(loc))
- maxPort=portal;
- }
- return maxPort.getLocation();
- }
- public static boolean isItSmartToSummon(Portal portal,Game game)
- {
- int counter=0;
- for(Elf elf:enemyLivingElves)
- {
- if(elf.inAttackRange(portal))
- counter+=elf.attackMultiplier;
- }
- for(Tornado torn:game.getEnemyTornadoes())
- {
- if(torn.distance(portal)<=torn.attackRange+portal.size)
- counter+=torn.attackMultiplier;
- }
- if(counter*game.iceTrollSummoningDuration+1>=portal.currentHealth)
- return false;
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- public static void IceOrFire(Portal portal, Game game){
- //System.out.println();
- //System.out.println("Should Summon Attack Tornado: "+ ShouldSummonAttackTornado(portal, game)+" portal: "+ portal);
- //////System.out.println();
- //////System.out.println(portal);
- //////System.out.println();
- boolean acted=false;
- if(!isItSmartToSummon(portal,game))
- return;
- if(game.getEnemyCastle().currentHealth<10){
- //////System.out.println("Less than 10 life");
- if(portal==closestPortalToEnemyCastle(game)&&portal.canSummonLavaGiant()){
- portal.summonLavaGiant();
- acted=true;
- }
- }
- ////**1**\\\\
- //if There is Enemy Elf Near Portal: summon Ice Troll
- ////////////////////////////////PLAY! MAYBE ANOTHE FUNCTION WITH HARSHER CONDITIONS///////////////////////////////////
- double numberOfIceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game);
- //if(IcePortal(game,portal))
- //{
- ////System.out.println("number of ice to go: "+numberOfIceToGo);
- //}
- ////////System.out.println("number of ice needed for defense: "+NumberOfIceNeededForDefense(game));
- if(!acted&&isThereEnemyElfMovingToMana(portal,game)&&allyIceAroundLoc(portal.getLocation(),game)<=2)
- {
- System.out.println(portal+" def from enemy rushing to mana");
- if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost)
- {
- if(portal.canSummonIceTroll()){
- ////System.out.println("I summon ice close enemy elf: "+portal);
- portal.summonIceTroll();
- acted=true;
- }
- }
- }
- else if(!acted&&isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)&&numberOfIceToGo<3) {
- acted=true;
- if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost)
- {
- if(portal.canSummonIceTroll()){
- ////System.out.println("I summon ice close enemy elf: "+portal);
- portal.summonIceTroll();
- acted=true;
- }
- }
- }
- ////**3**\\\\
- else if(!acted&&IcePortal(game,portal)&&(numberOfIceToGo>1.5||game.getMyMana()>=game.manaFountainCost+game.portalCost+game.iceTrollCost&&numberOfIceToGo>1)) {
- if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost||(numberOfIceToGo>=3&&game.getMyMana()>=game.iceTrollCost+10)){//||(isEnemyElfAttackingMyCastle(game) && game.getMyMana() >= game.iceTrollCost)) {
- if(portal.canSummonIceTroll()){
- ////System.out.println("I summon ice enemy attacking my castle: "+portal);
- portal.summonIceTroll();
- acted=true;
- }
- }
- }
- else if(!acted&&shouldSummonDefTornado(portal,game)){
- if(game.getMyMana()>=+game.tornadoCost&&portal.canSummonTornado()){
- //System.out.println(portal+" Portal chose to summon defend tornado");
- numberOfTornadoes++;
- portal.summonTornado();
- acted=true;
- }
- }
- else if(!acted&&ShouldSummonAttackTornado(portal,game)>1&&portal==AttackTornadoPortal(game)){
- if(game.getMyMana()>=handleMana(portal,game)+game.tornadoCost||ShouldSummonAttackTornado(portal,game)>2&&game.getMyMana()>=game.tornadoCost){
- if(portal.canSummonTornado()) {
- //System.out.println(portal+" Portal chose to summon attack tornado");
- numberOfTornadoes++;
- portal.summonTornado();
- acted=true;
- }
- }
- }
- else if(!acted&&isPortalNeededToHelpAllyElf(portal,game)&&allyIceAroundLoc(enemyAttackingLoc,game)>NumberOfIceNeededForDefense(game))
- {
- if(game.getMyMana()>=handleMana(portal,game)+game.lavaGiantCost)
- if(portal.canSummonLavaGiant())
- {
- //System.out.println("Sumonned lava to help elf: " +portal);
- portal.summonLavaGiant();
- acted=true;
- }
- }
- ///**2**\\\\
- else if(!acted&&FirePortal(game, portal)&&numberOfIceToGo<1.5)
- {
- if(game.getMyMana() >= handleMana(portal,game)+game.lavaGiantCost)
- {
- if(portal.canSummonLavaGiant()){
- //System.out.println("I summon fire to attack: "+ portal);
- portal.summonLavaGiant();
- acted=true;
- }
- }
- }
- }
- public static Portal DefTornadoPortal(Game game){
- Portal OriginalPortal=game.getMyPortals()[0];
- boolean flag=true;
- for(Portal portal:game.getMyPortals())
- {
- flag=true;
- for(Tornado tornado:game.getMyTornadoes())
- {
- if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(portal,game))
- {
- flag=false;
- }
- }
- if(portal.distance(closestEnemyBuildingToObject(portal,game))<OriginalPortal.distance(closestEnemyBuildingToObject(OriginalPortal,game))&&flag)
- {
- OriginalPortal=portal;
- }
- }
- flag=true;
- for(Tornado tornado:game.getMyTornadoes())
- {
- if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(OriginalPortal,game))
- {
- flag=false;
- }
- }
- if(flag)
- {
- //System.out.println("Defend tornado Portal: "+OriginalPortal);
- return OriginalPortal;
- }
- return null;
- }
- public static Portal AttackTornadoPortal(Game game){
- Portal cmpPortal=game.getMyPortals()[0];
- boolean flag=true;
- for(Portal portal:game.getMyPortals()){
- flag=true;
- for(Tornado tornado:game.getMyTornadoes())
- {
- if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(portal,game))
- {
- flag=false;
- }
- }
- if(ShouldSummonAttackTornado(portal,game)>ShouldSummonAttackTornado(cmpPortal,game)&&flag)
- {
- cmpPortal=portal;
- }
- else if(ShouldSummonAttackTornado(portal,game)==ShouldSummonAttackTornado(cmpPortal,game)&&flag)
- {
- if(portal.distance(closestEnemyBuildingToMapObject(portal,game))<cmpPortal.distance(closestEnemyBuildingToMapObject(cmpPortal,game)))
- {
- cmpPortal=portal;
- }
- }
- }
- for(Tornado tornado:game.getMyTornadoes()){
- if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(cmpPortal,game))
- {
- flag=false;
- }
- }
- if(ShouldSummonAttackTornado(cmpPortal,game)>0&&flag)
- {
- //System.out.println("Attack Tornado portal: "+cmpPortal);
- return cmpPortal;
- }
- return null;
- }
- public static boolean isObjectGoingForwardMe(Elf enemy, GameObject obj,Game game)
- {
- if(!enemy.isAlive())
- return false;
- boolean returned=false;
- for(int i=0;i<game.getAllEnemyElves().length;i++)
- {
- if(game.getAllEnemyElves()[i]==enemy)
- {
- if(enemyElvesLocations[i].getLocation().row==enemy.getLocation().row)
- {
- //System.out.println(" THIS IS IT THIS IS IT THIS IS IT "+enemy+" "+obj);
- returned= true;
- }
- else if(enemyElvesLocations[i].distance(obj)>=enemy.distance(obj)+10)
- returned= true;
- else
- returned= false;
- }
- }
- return returned;
- }
- public static boolean isItWorthSummoningIceToDefPortal(Portal portal,Elf enemy,Game game)
- {
- boolean hasSpeed=(enemy.maxSpeed>game.elfMaxSpeed);
- int counter=0;
- for(IceTroll ice:game.getMyIceTrolls())//elf is fucked
- {
- if(ice.distance(enemy)<=ice.attackRange)
- {
- //////System.out.println(portal+" it is not worth summoning ice to def this portal");
- counter++;
- }
- }
- if(counter>1)
- return false;
- if(enemy.distance(portal)-portal.size-enemy.attackRange<=enemy.maxSpeed*2)
- return true;
- for(IceTroll ice:game.getMyIceTrolls())
- {
- if(ice.distance(portal)<portal.distance(enemy)&&ice.distance(enemy)<portal.distance(enemy)&&ice.currentHealth>game.iceTrollSummoningDuration+2)
- {
- System.out.println(portal+" it is not worth summoning ice to def this portal");
- return false;
- }
- }
- return true;
- }
- public static ManaFountain manaEnemyGoingTo(Elf enemy,Game game)
- {
- if(game.getMyManaFountains().length==0)
- return null;
- int index=0;
- for(index=0;index<game.getAllEnemyElves().length;index++)
- {
- if(game.getAllEnemyElves()[index]==enemy)
- break;
- }
- ManaFountain minMana=game.getMyManaFountains()[0];
- double minDis=ObjectdistanceFromALine(minMana.getLocation(),enemyElvesLocations[index].getLocation(),enemy.getLocation());
- for(ManaFountain mana:game.getMyManaFountains())
- {
- double dis=ObjectdistanceFromALine(mana.getLocation(),enemyElvesLocations[index],enemy.getLocation());
- if(dis<minDis)
- {
- minDis=dis;
- minMana=mana;
- }
- }
- if(minDis<=enemy.maxSpeed*3)
- return minMana;
- return null;
- }
- public static boolean isThereEnemyElfMovingToMana(Portal portal,Game game)
- {
- if(game.getMyManaFountains().length==0)
- return false;
- for(Elf enemyElf: enemyLivingElves) {
- double distance=portal.distance(enemyElf)-portal.size-enemyElf.attackRange;
- double timeForEnemyElfToGetToPortal= distance/enemyElf.maxSpeed;
- double distancePortFromSpot=portal.size+enemyElf.attackRange-game.iceTrollAttackRange;
- int mana=game.getMyself().manaPerTurn;
- if(mana==0)
- mana=5;
- double turnsToStartSummoningIce=(-game.getMyMana()+(game.iceTrollCost))/mana;
- if(game.getMyMana()>=game.iceTrollCost)
- turnsToStartSummoningIce=0;
- double timeForIceToGetToPortal=turnsToStartSummoningIce+game.iceTrollSummoningDuration+distancePortFromSpot/game.iceTrollMaxSpeed;
- ManaFountain manaF=manaEnemyGoingTo(enemyElf,game);
- if(timeForIceToGetToPortal+2>=timeForEnemyElfToGetToPortal&&(isObjectGoingForwardMe(enemyElf,portal,game)||enemyElf.maxSpeed>game.elfMaxSpeed)&&(isItWorthSummoningIceToDefPortal(portal,enemyElf,game)||enemyElf.maxSpeed>game.elfMaxSpeed||manaF!=null))
- {
- if(manaF!=null)
- {
- Location enemyAttackingLoc=manaF.getLocation().towards(enemyElf.getLocation(),enemyElf.attackRange+manaF.size);
- double minDist=portal.distance(enemyAttackingLoc);
- boolean flag=true;
- for(Portal port:game.getMyPortals())
- {
- if(port!=portal&&port.distance(enemyAttackingLoc)<minDist)
- flag=false;
- }
- if(flag==true)
- {
- System.out.println("ELF MOVING TO MANA "+enemyElf+portal);
- return true;
- }
- }
- }
- }
- return false;
- }
- public static boolean isThereEnemyElfNearPortal(Portal portal, Game game) {
- for(Elf enemyElf: enemyLivingElves) {
- double distance=portal.distance(enemyElf)-portal.size-enemyElf.attackRange;
- double timeForEnemyElfToGetToPortal= distance/enemyElf.maxSpeed;
- double distancePortFromSpot=portal.size+enemyElf.attackRange-game.iceTrollAttackRange;
- int mana=game.getMyself().manaPerTurn;
- if(mana==0)
- mana=5;
- double turnsToStartSummoningIce=(-game.getMyMana()+(game.iceTrollCost))/mana;
- if(game.getMyMana()>=game.iceTrollCost)
- turnsToStartSummoningIce=0;
- double timeForIceToGetToPortal=turnsToStartSummoningIce+game.iceTrollSummoningDuration+distancePortFromSpot/game.iceTrollMaxSpeed;
- if(timeForIceToGetToPortal+2>=timeForEnemyElfToGetToPortal&&isObjectGoingForwardMe(enemyElf,portal,game)&&isItWorthSummoningIceToDefPortal(portal,enemyElf,game))
- {
- double minDist=portal.distance(enemyElf);
- boolean flag=true;
- for(Portal port:game.getMyPortals())
- {
- if(port!=portal&&port.distance(enemyElf)<minDist&&isObjectGoingForwardMe(enemyElf,port,game))
- flag=false;
- }
- if(flag==true)
- {
- System.out.println("CLOSE ELF "+enemyElf+portal);
- return true;
- }
- }
- }
- return false;
- }
- public static int getIndexByPortal(Portal portal, Game game) {
- int index=0;
- for(int i=0;i<game.getMyPortals().length;i++) {
- if(game.getMyPortals()[i]==portal)
- index=i;
- }
- return index;
- }
- public static int allyIceAroundLoc(Location location, Game game) {
- if(EnemyThreatningPortal(game)==null){
- return game.getMyIceTrolls().length;
- }
- int allyIceAroundLoc=0;
- for(IceTroll Ice:game.getMyIceTrolls()) {
- ////////System.out.println("ObjectdistanceFromALine: "+ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle()));
- if(Ice.distance(location)<Ice.maxSpeed*4/*&&ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle())<game.iceTrollMaxSpeed*4*/&&Ice.currentHealth>3) {
- allyIceAroundLoc++;
- }
- }
- return allyIceAroundLoc;
- }
- public static double ObjectdistanceFromALine(Location object, Location objectOnLine1 , Location objectOnLine2){
- //y=mx+b
- if(object==null||objectOnLine1==null||objectOnLine2==null)
- return 0;
- double m=getSlopeByTwoMapObjects(objectOnLine1,objectOnLine2);
- double b=objectOnLine1.col-m*objectOnLine1.row;
- double distance=Math.abs(-m*object.row+object.col-b)/Math.sqrt(m*m+1);
- return distance;
- }
- public static double ObjectdistanceFromALine(GameObject object, GameObject objectOnLine1 , GameObject objectOnLine2){
- //y=mx+b
- if(object==null||objectOnLine1==null||objectOnLine2==null)
- return 0;
- double m=getSlopeByTwoMapObjects(objectOnLine1,objectOnLine2);
- double b=objectOnLine1.getLocation().col-m*objectOnLine1.getLocation().row;
- double distance=Math.abs(-m*object.getLocation().row+object.getLocation().col-b)/Math.sqrt(m*m+1);
- return distance;
- }
- public static boolean isEnemyElfAttackingMyCastle(Game game){
- for(Elf elf : enemyLivingElves)
- {
- if(elf.distance(game.getMyCastle())<=elf.attackRange+elf.maxSpeed*5+game.getMyCastle().size)
- {
- //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
- //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
- //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
- return true;
- }
- }
- return false;
- }
- public static int EnemyLavaAttackingMyCastle(Game game) {
- int lavaCounter=0;
- if(EnemyThreatningPortal(game)!=null)
- {
- enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
- for(LavaGiant lava:game.getEnemyLavaGiants())
- {
- if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
- lavaCounter++;
- }
- }
- else
- {
- for(LavaGiant lava:game.getEnemyLavaGiants())
- {
- if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
- lavaCounter++;
- }
- }
- //////System.out.println("Lava Counter: "+lavaCounter);
- if(lavaCounter>2) {
- ////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
- //////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
- //////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
- }
- return lavaCounter;
- }
- //Fire\\
- public static boolean FirePortal(Game game, Portal portal) {
- if(portal==closestPortalToEnemyCastle(game)&&isItWorthSummoningFire(portal,game)>=10&&NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc,game)<3||game.getMyMana()>game.manaFountainCost+game.portalCost+game.iceTrollCost*3&&portal==closestPortalToEnemyCastle(game)) {
- if(portal.canSummonLavaGiant()) {
- return true;
- }
- }
- return false;
- }
- public static Portal closestPortalToEnemyCastle(Game game){
- if(game.getMyPortals().length==0)
- return null;
- Portal mPortal=game.getMyPortals()[0];
- for(Portal portal :game.getMyPortals())
- {
- if(portal.distance(game.getEnemyCastle())<=mPortal.distance(game.getEnemyCastle())&&portal.canSummonLavaGiant())
- {
- mPortal=portal;
- }
- }
- return mPortal;
- }
- public static double isItWorthSummoningFire(Portal portal, Game game){
- return game.lavaGiantMaxHealth-(portal.distance(game.getEnemyCastle())-game.lavaGiantAttackRange-game.getEnemyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn-numOfEnemyIceDefenders(portal,game);
- }
- public static int numOfEnemyIceDefenders(Portal portal, Game game){
- Location OurAttackingLoc=game.getEnemyCastle().getLocation().towards(portal,game.lavaGiantAttackRange+game.getEnemyCastle().size);
- int counter=0;
- for(IceTroll ice: game.getEnemyIceTrolls())
- {
- if(ice.distance(OurAttackingLoc)<game.iceTrollMaxSpeed*5+game.iceTrollAttackRange)
- counter++;
- }
- return counter;
- }
- public static Building[] getEnemyBuildings(Game game){
- int length=game.getEnemyPortals().length+game.getEnemyManaFountains().length;
- Building[] enemyBuildings=new Building[length];
- for(int i=0;i<game.getEnemyPortals().length;i++)
- {
- enemyBuildings[i]=game.getEnemyPortals()[i];
- }
- for(int j=0;j<game.getEnemyManaFountains().length;j++)
- {
- enemyBuildings[j+game.getEnemyPortals().length]=game.getEnemyManaFountains()[j];
- }
- return enemyBuildings;
- }
- public static Building closestEnemyBuildingToObjectForTornado(GameObject object,Building[] CheckedBuildings, Game game){
- if(getEnemyBuildings(game).length<2||CheckedBuildings.length==getEnemyBuildings(game).length){
- return null;
- }
- boolean flag=true;
- Building OriginalEnemyBuilding;
- if(object!=getEnemyBuildings(game)[0])
- OriginalEnemyBuilding=getEnemyBuildings(game)[0];
- else
- OriginalEnemyBuilding=getEnemyBuildings(game)[1];
- for(Building enemyBuilding:getEnemyBuildings(game)){
- ////System.out.println("enemyBuilding: "+enemyBuilding);
- flag=true;
- for(int i=0;i<CheckedBuildings.length;i++){
- if(enemyBuilding==CheckedBuildings[i])
- flag=false;
- }
- if(object.distance(enemyBuilding)<object.distance(OriginalEnemyBuilding)&&flag==true){
- ////System.out.println("Original: "+OriginalEnemyBuilding+" enemy: "+enemyBuilding);
- OriginalEnemyBuilding=enemyBuilding;
- }
- }
- flag=true;
- for(int i=0;i<CheckedBuildings.length;i++){
- if(OriginalEnemyBuilding==CheckedBuildings[i])
- flag=false;
- }
- if(flag==true)
- return OriginalEnemyBuilding;
- else
- return null;
- }
- public static int numberOfBuildingsTornadoCanDestroy(Building nextBuilding, int numberOfBuildings,int distanceWent,int turnsToDestroy,int numberOfIce ,Building[] CheckedBuildings, int i, Game game){
- if(nextBuilding==null)
- return numberOfBuildings;
- Building currentBuilding=nextBuilding;
- ////System.out.println("current Building: " + currentBuilding);
- ////System.out.println("CheckedBuildings[i]: "+CheckedBuildings[i]);
- CheckedBuildings[i]=currentBuilding;
- ////System.out.println("CheckedBuildings[i]: "+CheckedBuildings[i]);
- nextBuilding=closestEnemyBuildingToObjectForTornado(nextBuilding,CheckedBuildings,game);
- ////System.out.println("next Building: "+ nextBuilding);
- if(nextBuilding==currentBuilding||nextBuilding==null)
- return numberOfBuildings;
- for(IceTroll ice:game.getEnemyIceTrolls())
- {
- if((ice.distance(nextBuilding)<currentBuilding.distance(nextBuilding)&&ice.distance(currentBuilding)<currentBuilding.distance(nextBuilding))||(ice.distance(nextBuilding)<ice.maxSpeed*1.5+nextBuilding.size||ice.distance(currentBuilding)<ice.maxSpeed*1.5+currentBuilding.size)&&ice.currentHealth>10)
- {
- numberOfIce++;
- }
- }
- boolean flag=true;
- for(Elf elf:game.getMyLivingElves()){
- if(elf.inAttackRange(nextBuilding))
- flag=false;
- }
- ////System.out.println("Number of ice: "+numberOfIce);
- numberOfIce=numberOfIce*game.iceTrollAttackMultiplier*2;
- turnsToDestroy+=(nextBuilding.currentHealth/game.tornadoAttackMultiplier);
- distanceWent+=currentBuilding.distance(nextBuilding);
- if(distanceWent<game.tornadoMaxSpeed*(game.tornadoMaxHealth-turnsToDestroy-numberOfIce)+game.tornadoAttackRange+nextBuilding.size&&flag)
- {
- ////System.out.println("numberOfBuildings++");
- numberOfBuildings++;
- }
- return numberOfBuildingsTornadoCanDestroy(nextBuilding,numberOfBuildings,distanceWent,turnsToDestroy,0,CheckedBuildings,i+1,game);
- }
- public static int ShouldSummonAttackTornado(Portal portal, Game game){
- Building[] CheckedBuildings=new Building[getEnemyBuildings(game).length+1];
- int numberOfBuildings=numberOfBuildingsTornadoCanDestroy(portal,0,0,0,0,CheckedBuildings,0,game);
- return numberOfBuildings;
- }
- //Ice\\
- public static boolean IcePortal(Game game, Portal portal) {
- if(getEnemyAttackingElf(10,game)!=null) {
- if(portal==ClosestPortalToEnemyElf(game,ClosestEnemyElfToOurCastle(game))&&isObjectGoingForwardMe(ClosestEnemyElfToOurCastle(game),game.getMyCastle(),game)) {
- if(portal.canSummonIceTroll()){
- //////System.out.println("This portal is ice for enemy attacking elf: "+portal);
- return true;
- }
- }
- }
- if(allyIceAroundLoc(enemyAttackingLoc,game)<NumberOfIceNeededForDefense(game)){
- if(portal==PortalToDefenseFire(game))
- if(portal.canSummonIceTroll()){
- //////System.out.println("This portal is ice for enemy attacking Lava: "+portal);
- return true;
- }
- }
- return false;
- }
- public static Portal[] getMyDefensePortals(Game game) {
- int DefensePortals=0;
- for(Portal portal : game.getMyPortals()) {
- if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle())) {
- DefensePortals++;
- }
- }
- Portal[] DefensePortals1=new Portal[DefensePortals];
- for(int i=0;i<DefensePortals1.length;i++) {
- for(Portal portal : game.getMyPortals()) {
- if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle()))
- DefensePortals1[i]=portal;
- }
- }
- return DefensePortals1;
- }
- public static Elf getEnemyAttackingElf(int turns, Game game) {
- if(enemyLivingElves.length==0)
- return null;
- Elf elf=enemyLivingElves[0];
- for(Elf EnemyElf: enemyLivingElves) {
- if(EnemyElf.maxSpeed*turns>EnemyElf.distance(game.getMyCastle())) {
- if(EnemyElf.distance(game.getMyCastle())<elf.distance(game.getMyCastle())) {
- elf=EnemyElf;
- }
- }
- }
- if(elf.maxSpeed*turns>elf.distance(game.getMyCastle()))
- return elf;
- return null;
- }
- public static Portal PortalToDefenseFire(Game game) {
- if(game.getMyPortals().length==0||game.getEnemyPortals().length==0)
- return null;
- Portal SPortal=game.getMyPortals()[0];
- for(Portal portal : game.getMyPortals()) {
- if(portal.distance(game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game), game.lavaGiantAttackRange+game.getMyCastle().size))<SPortal.distance(game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game), game.lavaGiantAttackRange+game.getMyCastle().size)))
- SPortal=portal;
- }
- // //////System.out.println("This is the portal to defense from: "+EnemyThreatningPortal(game));
- // //////System.out.println("This is the Portal to defense Fire: "+SPortal);
- return SPortal;
- }
- public static Elf ClosestEnemyElfToOurCastle(Game game) {
- if(enemyLivingElves.length==0)
- return null;
- Elf OriginalEnemyElf=enemyLivingElves[0];
- for(Elf EnemyElf:enemyLivingElves) {
- if(EnemyElf.distance(game.getMyCastle())<=OriginalEnemyElf.distance(game.getMyCastle())) {
- OriginalEnemyElf=EnemyElf;
- }
- }
- return OriginalEnemyElf;
- }
- public static Portal ClosestPortalToEnemyElf(Game game, Elf elf) {
- if(game.getMyPortals().length==0)
- return null;
- Portal OriginalPortal=game.getMyPortals()[0];
- for(Portal portal:game.getMyPortals()) {
- if(portal.distance(elf)<=OriginalPortal.distance(elf))
- OriginalPortal=portal;
- }
- return OriginalPortal;
- }
- public static boolean isThereCloseEnemyPortalToCastle(Game game){
- boolean isThereClosePortal=false;
- for(Portal portal :game.getEnemyPortals())
- {
- if(game.lavaGiantMaxHealth-(portal.distance(game.getMyCastle())/game.lavaGiantMaxSpeed)/game.lavaGiantSuffocationPerTurn-allyIceAroundLoc(enemyAttackingLoc,game)>=3)
- {
- isThereClosePortal=true;
- }
- }
- return isThereClosePortal;
- }
- public static Portal EnemyThreatningPortal(Game game) {
- if(closestEnemyPortalToOurCastle(game)==null)
- return null;
- Portal OEnemyPortal=game.getEnemyPortals()[0];
- for(Portal EnemyPortal : game.getEnemyPortals()){
- if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
- OEnemyPortal=EnemyPortal;
- if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)){
- }
- }
- }
- if(getEnemyLavaGiantsByPortal(game,OEnemyPortal)==0)
- return closestEnemyPortalToOurCastle(game);
- ////////System.out.println("EnemyThreatningPortal: "+OEnemyPortal+" Lava for this portal: "+getEnemyLavaGiantsByPortal(game,OEnemyPortal));
- return OEnemyPortal;
- }
- public static Building closestEnemyBuildingToObject(GameObject object, Game game){
- if(getEnemyBuildings(game).length==0){
- return null;
- }
- Building OriginalEnemyBuilding=getEnemyBuildings(game)[0];
- for(Building enemyBuilding:getEnemyBuildings(game)){
- if(object.distance(enemyBuilding)<object.distance(OriginalEnemyBuilding))
- OriginalEnemyBuilding=enemyBuilding;
- }
- return OriginalEnemyBuilding;
- }
- public static boolean shouldSummonDefTornado(Portal originPortal, Game game) {
- int enIceAroundPortal = 0;
- int allyIceAroundPortal = 0;
- int enElvesAroundPortal = 0;
- int allyElvesAroundPortal = 0;
- boolean flag=true;
- if(originPortal == null)
- return false;
- Building closestBuilding = closestEnemyBuildingToObject(originPortal, game);
- if(closestBuilding != null){
- if(closestBuilding instanceof Portal) {
- if(closestBuilding.distance(game.getMyCastle()) < game.lavaGiantMaxSpeed * 8 + game.lavaGiantAttackRange && closestBuilding.distance(originPortal) / game.tornadoMaxSpeed < game.tornadoMaxHealth / game.tornadoSuffocationPerTurn - (closestBuilding.currentHealth / game.tornadoAttackMultiplier / 1.5)) {
- enIceAroundPortal = 0;
- allyIceAroundPortal = 0;
- enElvesAroundPortal = 0;
- allyElvesAroundPortal = 0;
- for(IceTroll ice : game.getEnemyIceTrolls()) {
- if(ice.distance(closestBuilding) <= game.iceTrollMaxSpeed * 4) {
- enIceAroundPortal++;
- }
- }
- for(IceTroll ice : game.getMyIceTrolls()) {
- if(ice.distance(closestBuilding) <= game.iceTrollMaxSpeed * 4) {
- enIceAroundPortal++;
- }
- }
- for(Elf elf : game.getEnemyLivingElves()) {
- if(elf.distance(closestBuilding) <= elf.maxSpeed * 4) {
- enElvesAroundPortal++;
- }
- }
- for(Elf elf : game.getMyLivingElves()) {
- if(elf.distance(closestBuilding) <= elf.maxSpeed * 4) {
- allyElvesAroundPortal++;
- }
- }
- for(Tornado tornado:game.getMyTornadoes())
- {
- if(closestEnemyBuildingToMapObject(tornado,game)==closestBuilding)
- {
- flag=false;
- }
- }
- if(flag&&(allyElvesAroundPortal == 0 || enIceAroundPortal - allyIceAroundPortal > 2 || enElvesAroundPortal > allyElvesAroundPortal || game.getEnemyMana() > game.iceTrollCost * 2.3)) {
- return true;
- }
- }
- }
- }
- return false;
- }
- public static double getSlopeByTwoMapObjects(MapObject FistObject,MapObject SecondObject) {
- double Slope;
- double DeltaY;
- double DeltaX;
- DeltaY=FistObject.getLocation().col-SecondObject.getLocation().col;
- DeltaX=FistObject.getLocation().row-SecondObject.getLocation().row;
- Slope=DeltaY/DeltaX;
- return Slope;
- }
- public static double getSlopeByTwoMapObjects(Location FistObject,Location SecondObject) {
- double Slope;
- double DeltaY;
- double DeltaX;
- DeltaY=FistObject.col-SecondObject.col;
- DeltaX=FistObject.row-SecondObject.row;
- Slope=DeltaY/DeltaX;
- return Slope;
- }
- public static int getEnemyLavaGiantsByPortal(Game game, Portal portal) {
- if(portal==null)
- return 0;
- double slopePC=getSlopeByTwoMapObjects(portal,game.getMyCastle());
- int lavaGiantsOnSlope=0;
- for(LavaGiant LavaGiant : game.getEnemyLavaGiants()) {
- if(Math.abs(getSlopeByTwoMapObjects(LavaGiant,game.getMyCastle())-slopePC)<0.5) {
- lavaGiantsOnSlope++;
- }
- }
- return lavaGiantsOnSlope;
- }
- public static boolean isElfInNeedOfFire(Elf elf,Portal portal,Game game)
- {
- for(IceTroll enemyIce: game.getEnemyIceTrolls())
- {
- if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*2&&enemyIce.currentHealth/enemyIce.suffocationPerTurn>=portal.distance(elf)/game.lavaGiantMaxSpeed)
- {
- return true;
- }
- }
- return false;
- }
- public static boolean isPortalNeededToHelpAllyElf(Portal portal,Game game)
- {
- if(game.getMyLivingElves().length==0)
- return false;
- Elf elfToHelp=null;
- for(Elf elf:game.getMyLivingElves())
- {
- if(isElfInNeedOfFire(elf,portal,game)==true)
- {
- elfToHelp=elf;
- }
- }
- if(elfToHelp==null)
- {
- return false;
- }
- Portal bestP=null;
- double minDist=game.rows*game.rows;//biggy
- for(Portal myPort:game.getMyPortals())
- {
- if(myPort.distance(game.getEnemyCastle())>elfToHelp.distance(game.getEnemyCastle()))
- {
- //double elfDistLineFromIce=ObjectdistanceFromALine(elfToHelp,iceToDef,);
- double mMyPortCastle=getSlopeByTwoMapObjects(myPort,game.getEnemyCastle());
- double mElfCastle=getSlopeByTwoMapObjects(myPort,elfToHelp);
- mMyPortCastle=Math.atan(mMyPortCastle);
- mElfCastle=Math.atan(mMyPortCastle);
- if(Math.abs(mMyPortCastle-mElfCastle)<=Math.PI/6 && myPort.distance(elfToHelp)<minDist && game.lavaGiantSummoningDuration<elfToHelp.currentHealth)
- {
- minDist=myPort.distance(elfToHelp);
- bestP=myPort;
- }
- }
- }
- ////System.out.println("THIS ELF NEEDS HELP "+elfToHelp+" best portal "+bestP);
- if(bestP!=null&&bestP==portal)
- {
- ////System.out.println(bestP+" "+elfToHelp+" "+" this portal SHOULD SUMMON FIRE TO HELP ELF");
- return true;
- }
- return false;
- }
- public static boolean AttackingPortalsSummonigFire(Portal port,Game game){
- int countSuperAttackingPortals=0;
- boolean flag=false;
- for(Portal portal:game.getMyPortals()){
- if(port.distance(portal)<=portal.size*4&&portal!=port)
- {
- if(portal.distance(game.getEnemyCastle())<portal.distance(game.getMyCastle()));
- flag=true;
- }
- if(!flag&&portal.distance(game.getEnemyCastle())<=game.lavaGiantMaxSpeed*8+game.lavaGiantAttackRange+game.getEnemyCastle().size)
- {
- countSuperAttackingPortals++;
- }
- }
- if(countSuperAttackingPortals>1)//&&flag==true)
- return true;
- else
- return false;
- }
- public static double NumberOfIceNeededForDefense(Game game){
- if(EnemyThreatningPortal(game)!=null){
- double FinalLavaLife;
- double NumberOfIceNeededForDefense=0;
- if(game.getEnemyLavaGiants().length==0)
- return 0;
- LavaGiant maxLava=game.getEnemyLavaGiants()[0];
- for(LavaGiant lava:game.getEnemyLavaGiants())
- {
- if(lava.distance(game.getMyCastle())>maxLava.distance(game.getMyCastle()))
- maxLava=lava;
- }
- FinalLavaLife=maxLava.currentHealth-(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange)))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn;
- //////System.out.println(FinalLavaLife+" "+maxLava+" life that maxLava will have when close to my castle "+maxLava.currentHealth);
- NumberOfIceNeededForDefense=(FinalLavaLife*0.8)/3;
- //if(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>game.iceTrollSummoningDuration*NumberOfIceNeededForDefense)
- // return 0;
- //if(game.getEnemyLavaGiants().length<3)
- // return EnemyLavaAttackingMyCastle(game);
- return NumberOfIceNeededForDefense;
- }
- return 0;
- }
- public static int getPortalImportance(Portal portal, Game game) {
- int importance;
- if(isThereEnemyElfMovingToMana(portal,game)) {
- importance=6;
- }
- if(isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)) {
- importance=5;
- }
- else if(IcePortal(game,portal)) {
- importance=4;
- }
- else if(ShouldSummonAttackTornado(portal,game)>1){
- importance=3;
- }
- else if(isPortalNeededToHelpAllyElf(portal,game)) {
- importance=2;
- }
- else if(FirePortal(game, portal)) {
- importance=1;
- }
- else
- importance=0;
- return importance;
- }
- public static boolean ShouldSaveManaForManaFountain(Game game){
- Location locationToBuildManaFountain=whereToBuildManaFountain(game);
- if(game.getMyLivingElves().length==0||locationToBuildManaFountain==null||!game.canBuildManaFountainAt(locationToBuildManaFountain)||game.getMyManaFountains().length>game.getMyPortals().length){
- return false;
- }
- if(game.getMyself().manaPerTurn<=game.getEnemy().manaPerTurn||game.getMyself().manaPerTurn==0){
- if(game.getEnemyLivingElves().length==0)
- return true;
- }
- return false;
- }
- public static int handleMana(Portal portal, Game game) {
- boolean EnemyElfNearPortal=false;
- for(Portal APortal: game.getMyPortals()){
- if(isThereEnemyElfNearPortal(APortal,game)&&!AttackingPortalsSummonigFire(APortal,game))
- EnemyElfNearPortal=true;
- }
- double iceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc,game);
- int ManaSave = 0;
- int PortalImportance=getPortalImportance(portal,game);
- for(int i=getIndexByPortal(portal,game);i<game.getMyPortals().length;i++) {
- ////System.out.println("Portal: "+portal+" Portal importance: "+PortalImportance);
- int portImportance=getPortalImportance(game.getMyPortals()[i],game);
- if(PortalImportance<portImportance) {
- if(portImportance==6){
- //////System.out.println("Saving for Close elf");
- ManaSave+=game.iceTrollCost;
- }
- else if(portImportance==5){
- //////System.out.println("Saving for Close elf");
- ManaSave+=game.iceTrollCost;
- }
- else if(portImportance==4){
- //////System.out.println("Saving for defense");
- ManaSave+=game.iceTrollCost;
- }
- else if(portImportance==3){
- ManaSave+=game.tornadoCost;
- }
- else if(portImportance==2){
- //////System.out.println("Saving for Lava help");
- ManaSave+=game.lavaGiantCost;
- }
- else if(portImportance==1){
- //////System.out.println("Saving mana to attack");
- ManaSave+=game.lavaGiantCost;
- }
- else
- ManaSave+=0;
- }
- }
- if(ShouldSaveManaForManaFountain(game)){//&&!EnemyElfNearPortal&&(iceToGo<1.5||game.getMyself().manaPerTurn==0)){
- ////System.out.println("Saving for mana fountain");
- ManaSave+=game.manaFountainCost;
- }
- if(EnemyElfNearPortal&&ManaSave<game.iceTrollCost&&PortalImportance<3){
- //////System.out.println("Saving for near elf");
- ManaSave+=game.iceTrollCost;
- }
- if(!isThereDefPortals(game)&&isElfTryingToBuildPortal){
- //////System.out.println("Saving for defense portal");
- ManaSave+=game.portalCost;
- }
- if(!EnemyElfNearPortal&&isElfTryingToBuildPortal&&iceToGo<2.1) {
- //////System.out.println("Saving for attack portal");
- ManaSave+=game.portalCost-game.getMyself().manaPerTurn;
- }
- //System.out.println("ManaSave= "+ManaSave+" Portal: "+portal);
- return ManaSave;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement