Advertisement
Guest User

nice code

a guest
Mar 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 109.41 KB | None | 0 0
  1. package bots;
  2. import elf_kingdom.*;
  3. public class MyBot implements SkillzBot {
  4. /*
  5. Version: enemy invisibility check
  6. TO DO: INVISIBILITY
  7. */
  8.  
  9. static Location[] enemyElvesLocations;
  10. static Location[] myElvesLocations;
  11. static boolean isElfTryingToBuildPortal;
  12. static Location enemyAttackingLoc;
  13.  
  14. static int ActiontryToAttackEnemyElf=0;
  15. static int ActiontryToBuildPortalCauseCan=1;
  16. static int ActionDefPortal=2;
  17. static int ActionDestroyEnemyManaFountains=3;
  18. static int ActionEnemyAttackingPortal=4;
  19. static int ActionEnemyElfAttackingA=5;
  20. static int ActionBuildDefManaFountains=6;
  21. static int ActionEnemyElfAttacking=7;
  22. static int ActionBuildManaFountains=8;
  23. static int ActionBuildAttackingPortals=10;
  24. static int ActionBuildAttackPortalWhenCan=11;
  25. static int ActionDestroyEnemyBuildings=9;
  26. static int ActionAttackEnemyCastle=12;
  27.  
  28. static int NumberOfActions=12;
  29.  
  30. static int numberOfTornadoes=0;
  31. static Elf[] enemyLivingElves;
  32. static String botName;
  33.  
  34. public void doTurn(Game game) {
  35. System.out.println("number Of Tornadoes: "+numberOfTornadoes);
  36. //System.out.println("havradim adumin");
  37. //System.out.println("hasheleg hu rah");
  38. //System.out.println("haindex nimza mehuz legvulut hamaarah");
  39. int counter=0;
  40. for(Elf elf:game.getEnemyLivingElves())
  41. {
  42. if(game.inMap(elf))
  43. counter++;
  44. }
  45. enemyLivingElves=new Elf[counter];
  46. ////System.out.println(enemyLivingElves.length+" LENGTH");
  47. counter=0;
  48. for(int i=0;i<game.getEnemyLivingElves().length;i++)
  49. {
  50. if(game.inMap(game.getEnemyLivingElves()[i]))
  51. {
  52. enemyLivingElves[counter]=game.getEnemyLivingElves()[i];
  53. counter++;
  54. }
  55. }
  56. //for(Elf elf:enemyLivingElves)
  57. // //System.out.print(elf+",");
  58. //////System.out.println();
  59. ////System.out.println(whereToBuildManaFountain(game)+" where to build mana fountain");
  60. ////System.out.println("isItSmartToBuildManaFountain :"+isItSmartToBuildManaFountain(whereToBuildManaFountain(game),game));
  61. for(int i=0;i<=NumberOfActions;i++)
  62. System.out.println(numberOfElvesNeededForAction(i,game)+" action number "+i);
  63. if(game.turn==1)
  64. {
  65. turnOne(game);
  66. }
  67. if(EnemyThreatningPortal(game)!=null)
  68. enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
  69. else
  70. enemyAttackingLoc=null;
  71. isElfTryingToBuildPortal=false;
  72. //////System.out.println("Elves:");
  73. handleElvesNew(game);
  74. //////System.out.println("");
  75. //////System.out.println("Portals:");
  76. handlePortals(game);
  77. //END OF TURN RESET LOCATIONS OF ELVES
  78. for(int i=0;i<game.getAllEnemyElves().length;i++)//update enemyPirates Loc
  79. {
  80. if(game.getAllEnemyElves()[i].isAlive()&&game.getAllEnemyElves()[i].getLocation().row==-50&&game.getAllEnemyElves()[i].getLocation().col==-50)
  81. {
  82. Elf enemy=game.getAllEnemyElves()[i];
  83. ManaFountain mana=manaEnemyGoingTo(enemy,game);
  84. if(mana!=null)
  85. enemyElvesLocations[i]=enemyElvesLocations[i].towards(mana,enemy.maxSpeed);
  86. System.out.println(enemy+" is invisible");
  87. }
  88. else
  89. enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
  90. }
  91. for(int i=0;i<game.getAllMyElves().length;i++)//update myPirates Loc
  92. {
  93. myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
  94. }
  95. }
  96. public static void turnOne(Game game)
  97. {
  98. enemyElvesLocations=new Location[game.getAllEnemyElves().length];
  99. myElvesLocations=new Location[game.getAllMyElves().length];
  100.  
  101. for(int i=0;i<game.getAllEnemyElves().length;i++)
  102. {
  103. enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
  104. }
  105. for(int i=0;i<game.getAllMyElves().length;i++)
  106. {
  107. myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
  108. }
  109. }
  110.  
  111. private void handlePortals(Game game) {
  112. for(Portal portal: game.getMyPortals())
  113. {
  114. IceOrFire(portal,game);
  115. }
  116. }
  117. public static void swapElves(Elf elf,Elf[] elvesForActions,int counter,int actionNum,Game game)
  118. {
  119. if(counter==elvesForActions.length)
  120. {
  121. return;
  122. }
  123. else if(elvesForActions[counter]==null)
  124. {
  125. elvesForActions[counter]=elf;
  126. return;
  127. }
  128. else if(elvesForActions[counter]!=elf&&isElfBetterAtAction(elf,elvesForActions[counter],actionNum,game))
  129. {
  130. Elf keeper=elvesForActions[counter];
  131. elvesForActions[counter]=elf;
  132. swapElves(keeper,elvesForActions,0,actionNum,game);
  133. }
  134. else
  135. {
  136. swapElves(elf,elvesForActions,counter+1,actionNum,game);
  137. }
  138. }
  139.  
  140. public static void elvesDoAction(Elf[] elves,int action,Game game)
  141. {
  142. for(int i=0;i<elves.length;i++)
  143. {
  144. if(elves[i]!=null)
  145. {
  146. Elf elf=elves[i];
  147. if(!elf.alreadyActed) {
  148. if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.maxSpeed*10+game.castleSize&&action!=ActionDestroyEnemyManaFountains)
  149. isElfTryingToBuildPortal=true;
  150. System.out.println(elf+" is it smart "+isItSmartToBuildPortal(elf,game)+" is it safe "+isItSafeToBuildPortal(elf,game));
  151. if(elf.isBuilding)
  152. System.out.println(elf+" elf is building");
  153. ///**0**\\\ ---PRIVATE
  154. //if there is no enemy close to casle: Try to attack an Elf close to you\\
  155. else if(tryToAttackElf(elf,game))
  156. {
  157. attackEnemyElf(elf,game);
  158. System.out.println(elf+" Elf Attacking");
  159. }
  160.  
  161. ////**1**\\\\---PRIVATE
  162. //if it is safe and smart and has mana build a portal
  163. //Longest line
  164. 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))
  165. {
  166. System.out.println(elf+" elf chose to build portal cause he has a lot of mana");
  167. elf.buildPortal();
  168. }
  169.  
  170. else if(areTheyCloseFountainsToElf(elf,game)&&action!=ActionDestroyEnemyManaFountains)
  171. tryToDestroyEnemyCloseFountain(elf,game);
  172. else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.maxSpeed*10+game.castleSize&& elf.canBuildPortal()&&action!=ActionDestroyEnemyManaFountains)
  173. {
  174. System.out.println(elf+" elf chose to build an attacking portal cause he is close");
  175. elf.buildPortal();
  176. }
  177.  
  178. else if(game.getEnemyCastle().currentHealth==75&&game.getMyself().manaPerTurn==0&&game.getMyself().mana==0)
  179. {
  180. if(areTheyCloseFountainsToElf(elf,game))
  181. tryToDestroyEnemyCloseFountain(elf,game);
  182. else
  183. elf.attack(game.getEnemyLavaGiants()[0]);
  184. }
  185. ////**2**\\\\
  186. //if there is no portal defending go build one
  187. else if(action==ActionDefPortal)
  188. {
  189. System.out.println(elf+" elf chose to build def portal");
  190. buildDefPortal(elf,game);
  191. }
  192. ////**3**\\\\
  193. //Is there is a Close Enemy Portal To Castle\\
  194. else if(action==ActionEnemyAttackingPortal)
  195. {
  196. System.out.println(elf+" elf chose to destroy enemy elves attacking portal");
  197. destroyEnemysAttackingPortal(elf,game);
  198. }
  199. ////**4**\\\\
  200. //checking if enemy elf is on his way to attack our castle
  201. else if(action==ActionEnemyElfAttacking||action==ActionEnemyElfAttackingA)//ACTION THAT DOES STUFF!!!!
  202. {
  203. GoingToEnemyAttackingElf(elf,game);
  204. System.out.println(elf+" going to def cause enemy elf attacking");
  205. }
  206. else if(isTherePortalNearEnemyCloseSpawnToCastle(game)&&enemyLivingElves.length==0){
  207. buildPortalToFuckEnemyElfWhenSpawned(elf, game);
  208. }
  209. ////**5**\\\\
  210. //if there is no attack portal: Go build one\\
  211. else if(action==ActionBuildAttackingPortals)
  212. {
  213. System.out.println(elf+" elf chose to build attacking portal");
  214. tryToBuildAttackingPortal(elf,game);
  215. }
  216. ////**6**\\\\
  217. //if you are close to enemys castle and you can build portal, build one
  218. else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost&&action!=ActionDestroyEnemyManaFountains)
  219. {
  220. System.out.println(elf+" elf chose to build an attacking portal cause he can");
  221. elf.buildPortal();
  222. }
  223. ////**7**\\\\
  224. //if enemy has portals, try to destroy them
  225. else if(action==ActionDestroyEnemyBuildings)
  226. {
  227. System.out.println(elf+" elf chose to destroy enemys buildings");
  228. destroyEnemysBuilding(elf,game);
  229. }
  230. else if(action==ActionBuildManaFountains)
  231. {
  232. System.out.println(elf+" trying to build a mana fountain");
  233. tryToBuildManaFountain(elf,game);
  234. }
  235. else if(action==ActionDestroyEnemyManaFountains)
  236. {
  237. System.out.println(elf+" destroying enemy mana founctain");
  238. destroyEnemysFountains(elf,game);
  239. }
  240. else if(action==ActionBuildDefManaFountains)
  241. {
  242. System.out.println(elf+" building a portal to def mana");
  243. buildDefManaFountains(elf,game);
  244. }
  245. ////**8**\\\\
  246. else//attack enemys castle at last
  247. {
  248. System.out.println(elf+" elf chose to attack the castle");
  249. tryToAttackEnemyCastle(elf,game);
  250. }
  251. }
  252. else
  253. {
  254. ////System.out.println(elf+" allready acted");
  255. }
  256. }
  257. }
  258. }
  259. public static void elvesDoChallenges(Elf[] elves, Game game)
  260. {
  261. for(int i=0;i<elves.length;i++)
  262. {
  263. if(elves[i]!=null)
  264. {
  265. Elf elf=elves[i];
  266. if(!elf.alreadyActed) {
  267. if(elf.isBuilding)
  268. System.out.println(elf+" elf is building");
  269.  
  270. ////Trap\\\\
  271. else if(botName == "Trap") {
  272. if(game.turn == 3 || game.turn == 10 || game.turn == 17) elf.castInvisibility();
  273. }
  274. else if(game.turn == 1 && game.getEnemyIceTrolls().length == 12 && game.elfMaxSpeed == 0 && game.lavaGiantMaxSpeed == 0) {
  275. for(int iceTrollNum = 0; iceTrollNum < game.getEnemyIceTrolls().length ; iceTrollNum++) {
  276. if(game.getEnemyIceTrolls()[iceTrollNum].distance(game.getAllMyElves()[0]) < 650 || game.getEnemyIceTrolls()[iceTrollNum].distance(game.getAllMyElves()[0]) > 750) {
  277. break;
  278. }
  279. if(iceTrollNum == game.getEnemyIceTrolls().length - 1) {
  280. botName = "Trap";
  281. }
  282. }
  283. }
  284. ///HoraMana\\\\
  285. else if(botName == "HoraMana")
  286. {
  287. if(!elf.invisible && elf.canCastInvisibility())
  288. elf.castInvisibility();
  289. else if(elf.inAttackRange(game.getEnemyCastle()))
  290. elf.attack(game.getEnemyCastle());
  291. else
  292. elf.moveTo(game.getEnemyCastle());
  293. }
  294. else if(game.turn == 1 && game.getAllEnemyElves().length == 18)
  295. {
  296. for(int enElfNum = 0; enElfNum < game.getAllEnemyElves().length; enElfNum++)
  297. {
  298. if(game.getAllEnemyElves()[enElfNum].distance(game.getMyPortals()[0]) > 100)
  299. break;
  300. if(enElfNum == game.getAllEnemyElves().length - 1)
  301. {
  302. botName = "HoraMana";
  303. elf.moveTo(game.getEnemyCastle());
  304. }
  305. }
  306. }
  307. else if(game.elfMaxSpeed == 5 && game.defaultManaPerTurn == 10)
  308. {
  309. if(elf.distance(game.getMyCastle()) - game.castleSize > game.portalSize)
  310. {
  311. if(elf.canBuildPortal()){
  312. ////System.out.println("Chanlange");
  313. elf.buildPortal();
  314. }
  315. }
  316. else if(elf.currentSpells.length > 0)
  317. elf.moveTo(game.getEnemyCastle());
  318. else if(elf.canCastSpeedUp())
  319. elf.castSpeedUp();
  320. }
  321. }
  322. }
  323. }
  324. }
  325. public static void handleElvesNew(Game game)
  326. {
  327. Elf[] elvesNotUsed=new Elf[game.getMyLivingElves().length];
  328. boolean hasElvesForAction=false;
  329. for(int i=0;i<elvesNotUsed.length;i++)
  330. {
  331. elvesNotUsed[i]=game.getMyLivingElves()[i];
  332. }
  333. boolean flag=false;
  334. int numOfElvesForDestroyEnemyMana=numberOfElvesNeededForAction(ActionDestroyEnemyManaFountains,game);
  335. int numOfElvesForEnemyElfAttackig=numberOfElvesNeededForAction(ActionEnemyElfAttacking,game);
  336. for(int i=0;i<=NumberOfActions;i++)//MAIN FOR 8 is number of actions
  337. {
  338. if(i!=ActiontryToAttackEnemyElf&&i!=ActiontryToBuildPortalCauseCan&&i!=ActionBuildAttackPortalWhenCan)//PRIVATE FUNCS
  339. {
  340. int numbOfElvesNeededForAction=numberOfElvesNeededForAction(i,game);
  341. if(numbOfElvesNeededForAction>0)
  342. {
  343. Elf[] elvesForActions=new Elf[numbOfElvesNeededForAction];
  344. for(int n=0;n<elvesNotUsed.length;n++)
  345. {
  346. if(elvesNotUsed[n]!=null)
  347. {
  348. /*Elf elf=elvesNotUsed[n];
  349. if(elf.maxSpeed>game.elfMaxSpeed||(numOfElvesForDestroyEnemyMana>0&&ShouldElfUseSpeedUp(elf,getEnemyBestManaFountain(elf,game).getLocation(),game)))
  350. {
  351. if(numOfElvesForEnemyElfAttackig>0||numOfElvesForDestroyEnemyMana>0)
  352. {
  353. if(i!=ActionEnemyElfAttacking&&i!=ActionDestroyEnemyManaFountains)
  354. {
  355. flag=true;
  356. }
  357. else if(i==ActionDestroyEnemyManaFountains)
  358. {
  359. flag=true;
  360. destroyEnemysFountains(elf,game);
  361. elvesNotUsed[n]=null;
  362. }
  363. else if(i==ActionEnemyElfAttacking)
  364. {
  365. flag=true;
  366. GoingToEnemyAttackingElf(elf,game);
  367. elvesNotUsed[n]=null;
  368.  
  369. }
  370. }
  371. //do nothing with this elf
  372. }
  373. if(flag==false)*/
  374. swapElves(elvesNotUsed[n],elvesForActions,0,i,game);
  375. }
  376. }
  377. for(int k=0;k<elvesNotUsed.length;k++)
  378. {
  379. for(Elf elfUsed:elvesForActions)
  380. {
  381. if(elvesNotUsed[k]==elfUsed)
  382. {
  383. elvesNotUsed[k]=null;
  384. }
  385. }
  386. }
  387. //////System.out.println("action numba "+i);
  388. //for(int k=0;k<elvesForActions.length;k++)
  389. // //System.out.print(elvesForActions[k]+",");
  390. //////System.out.println();
  391. //for(int k=0;k<elvesNotUsed.length;k++)
  392. // //System.out.print(elvesNotUsed[k]+",");
  393. //////System.out.println();
  394. elvesDoChallenges(elvesForActions, game);
  395. elvesDoAction(elvesForActions,i,game);
  396. }
  397. }
  398. }
  399. //for(int i=0;i<elvesNotUsed.length;i++)
  400. //{
  401. elvesDoAction(elvesNotUsed,NumberOfActions,game);
  402. //if(elvesNotUsed[i]!=null)
  403. //{
  404. // tryToAttackEnemyCastle(elvesNotUsed[i],game);//deafult action
  405. //}
  406. //}
  407. }
  408.  
  409.  
  410. public static double turnsToGetToLoc(Elf elf,Location location){
  411. return elf.distance(location)/elf.maxSpeed+elf.turnsToRevive*1.5;
  412. }
  413.  
  414. public static boolean isElfBetterAtAction(Elf elf,Elf elfCompare,int action,Game game)
  415. {
  416. double difference=1.5;
  417. if(action==ActionDefPortal) {//2
  418. Location loc=whereElfShouldBuildDefPortal(game);
  419. if(turnsToGetToLoc(elf,loc)+difference<turnsToGetToLoc(elfCompare,loc))
  420. return true;
  421. return false;
  422. }
  423. else if(action==ActionEnemyAttackingPortal) {//3
  424. //if elf needs help ++
  425. //&& other elf is far to another action's needed positions
  426. Portal port=closestEnemyPortalToOurCastle(game);
  427. if(turnsToGetToLoc(elf,port.getLocation())+difference<turnsToGetToLoc(elfCompare,port.getLocation()))
  428. {
  429. return true;
  430. }
  431. return false;
  432. }
  433. else if(action==ActionEnemyElfAttacking) {//4
  434. Elf enemyElf=isThereEnemyElfAttacking(15,game);
  435. if(turnsToGetToLoc(elf,enemyElf.getLocation())+difference<turnsToGetToLoc(elfCompare,enemyElf.getLocation()))
  436. {
  437. return true;
  438. }
  439. return false;
  440. }
  441. else if(action==ActionEnemyElfAttackingA)
  442. {
  443. Elf enemyElf=isThereEnemyElfAttacking(4,game);
  444. if(turnsToGetToLoc(elf,enemyElf.getLocation())+difference<turnsToGetToLoc(elfCompare,enemyElf.getLocation()))
  445. {
  446. return true;
  447. }
  448. return false;
  449.  
  450. }
  451. else if(action==ActionBuildAttackingPortals) {//if we dont have an attacking portal 5
  452. Location whereToBuildPort1=whereElfShouldBuildAttackPortal(elf,game);
  453. Location whereToBuildPort2=whereElfShouldBuildAttackPortal(elfCompare,game);
  454. if(turnsToGetToLoc(elf,whereToBuildPort1)+difference<turnsToGetToLoc(elfCompare,whereToBuildPort2))
  455. return true;
  456. return false;
  457. }
  458. else if(action==ActionDestroyEnemyBuildings) {//7
  459. Location l1=closestEnemyBuildingToMapObject(elf,game).getLocation();
  460. Location l2=closestEnemyBuildingToMapObject(elfCompare,game).getLocation();
  461. if(turnsToGetToLoc(elf,l1)+difference<turnsToGetToLoc(elfCompare,l2))
  462. return true;
  463. return false;
  464. }
  465. else if(action==ActionBuildManaFountains) {//9
  466. Location l1=whereToBuildManaFountain(game);
  467. if(turnsToGetToLoc(elf,l1)+difference<turnsToGetToLoc(elfCompare,l1))
  468. return true;
  469. return false;
  470. }
  471. else if(action==ActionDestroyEnemyManaFountains)
  472. {
  473. Location neededToDestroyEnemyMana=getEnemyBestManaFountain(game).getLocation();
  474. Location neededToDestroyEnemyMana2=getEnemyBestManaFountain(game).getLocation();
  475. if(turnsToGetToLoc(elf,neededToDestroyEnemyMana)+difference<turnsToGetToLoc(elfCompare,neededToDestroyEnemyMana2))
  476. return true;
  477. return false;
  478. }
  479. else if(action==ActionBuildDefManaFountains)
  480. {
  481. Location whereToDefManaPort=whereToDefMana(game);
  482. if(turnsToGetToLoc(elf,whereToDefManaPort)+difference<turnsToGetToLoc(elfCompare,whereToDefManaPort))
  483. return true;
  484. return false;
  485. }
  486. return false;
  487. }
  488. public static Location whereToDefMana(Game game)
  489. {
  490. if(game.getMyManaFountains().length==0)
  491. return null;
  492. Location min=null;
  493. boolean flag=false;
  494. int rad=game.manaFountainSize+game.portalSize+10;
  495. int[] degrees=new int[90];
  496. degrees[0]=0;
  497. int degreesCounter=1;
  498. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  499. {
  500. degrees[i]=degreesCounter;
  501. degrees[i+1]=degreesCounter*-1;
  502. degreesCounter++;
  503. }
  504. for(ManaFountain mana:game.getMyManaFountains())
  505. {
  506. flag=false;
  507. for(int i=0;i<degrees.length-1&&!flag;i++)
  508. {
  509. Location loc=trigoLocation(mana.getLocation().row,mana.getLocation().col,game.getEnemyCastle().getLocation(),rad,degrees[i]);
  510. Location port=closestAllyPortalToLoc(loc,game);
  511. if(game.canBuildPortalAt(loc)&&(port==null||port.distance(loc)>game.portalSize*3))
  512. {
  513. if(min==null)
  514. {
  515. flag=true;
  516. min=loc;
  517. }
  518. else if(loc.distance(game.getMyCastle())<min.distance(game.getMyCastle()))
  519. {
  520. flag=true;
  521. min=loc;
  522. }
  523. }
  524. }
  525. }
  526. return min;
  527. }
  528. public static void buildDefManaFountains(Elf elf,Game game)
  529. {
  530. Location loc=whereToDefMana(game);
  531. if(loc==null)
  532. return;
  533. if(elf.distance(loc)<elf.maxSpeed*1.5&&elf.canBuildPortal())
  534. elf.buildPortal();
  535. else
  536. elf.moveTo(safteyLoc(elf.getLocation(),loc,elf.maxSpeed,game));
  537. }
  538. public static Building closestEnemyBuildingToMapObject(MapObject object,Game game)
  539. {
  540. //////System.out.println(object+" OBJECT?!?!?!?!?!");
  541. if(game.getEnemyPortals().length==0&&game.getEnemyManaFountains().length==0)
  542. return null;
  543. Building mBuilding=null;
  544. if(game.getEnemyManaFountains().length>0)
  545. mBuilding=game.getEnemyManaFountains()[0];
  546. else
  547. mBuilding=game.getEnemyPortals()[0];
  548. for(Portal portal:game.getEnemyPortals())
  549. {
  550. if(portal.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
  551. mBuilding=portal;
  552. }
  553. for(ManaFountain Fountain:game.getEnemyManaFountains())
  554. {
  555. if(Fountain.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
  556. mBuilding=Fountain;
  557. }
  558. return mBuilding;
  559. }
  560. public static boolean areTheyCloseFountainsToElf(Elf elf,Game game)
  561. {
  562. for(ManaFountain mana:game.getEnemyManaFountains())
  563. {
  564. if(elf.inAttackRange(mana))
  565. {
  566. return true;
  567. }
  568. else if(elf.distance(mana)<=elf.maxSpeed*3)
  569. {
  570. return true;
  571. }
  572. else if(mana.distance(game.getMyCastle())<=mana.size*5)
  573. {
  574. return true;
  575. }
  576. }
  577. return false;
  578. }
  579. public static void tryToDestroyEnemyCloseFountain(Elf elf,Game game)
  580. {
  581. for(ManaFountain mana:game.getEnemyManaFountains())
  582. {
  583. if(elf.inAttackRange(mana))
  584. {
  585. elf.attack(mana);
  586. return;
  587. }
  588. else if(elf.distance(mana)<=elf.maxSpeed*3)
  589. {
  590. elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed,game));
  591. return;
  592. }
  593. else if(mana.distance(game.getMyCastle())<=mana.size*5)
  594. {
  595. elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed,game));
  596. return;
  597. }
  598. }
  599. }
  600.  
  601. public static void buildDefPortal(Elf elf,Game game)
  602. {
  603. isElfTryingToBuildPortal=true;
  604. Location whereToBuildPort=whereElfShouldBuildDefPortal(game);
  605. if(elf.distance(whereToBuildPort)<=elf.maxSpeed*1.5 &&elf.canBuildPortal()&&!(elf.isBuilding)&&game.getMyMana()>=(game.portalCost))
  606. {
  607. ////System.out.println("building def portal");
  608. elf.buildPortal();
  609. }
  610. else
  611. {
  612. //////System.out.println("going to build def portal loc");
  613. if(elf.distance(whereToBuildPort)<elf.maxSpeed*2&&isSafeLoc(elf.getLocation(),game))
  614. elf.moveTo(whereToBuildPort);
  615. else
  616. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
  617. }
  618. }
  619. public static boolean ShouldElfUseSpeedUpFORELF(Elf elf,Location loc,Game game){
  620. double TurnsToMana= elf.distance(loc)/(game.speedUpMultiplier*elf.maxSpeed)+1;
  621. if(TurnsToMana>game.speedUpExpirationTurns/2&&elf.currentHealth>=TurnsToMana){
  622. return true;
  623. }
  624. return false;
  625. }
  626. public static boolean ShouldElfUseSpeedUp(Elf elf,Location loc,Game game){
  627. double TurnsToMana= elf.distance(loc)/(game.speedUpMultiplier*elf.maxSpeed)+1;
  628. if(TurnsToMana<=game.speedUpExpirationTurns+2&&elf.currentHealth>=TurnsToMana){
  629. return true;
  630. }
  631. return false;
  632. }
  633. public static void destroyEnemysAttackingPortal(Elf elf,Game game)
  634. {
  635. Building p=closestEnemyPortalToOurCastle(game);
  636. Building b=closestEnemyBuildingToMapObject(elf,game);
  637. if(elf.distance(b)<=elf.attackRange+b.size)
  638. p=b;
  639. //if(ClosestEnemyElfToOurCastle(game)!=null&&ClosestEnemyElfToOurCastle(game).distance(elf)<elf.distance(p)&&isThereEnemyElfAttacking(game)!=null)
  640. //{
  641. // GoingToEnemyAttackingElf(elf,game);
  642. //////System.out.println("going to def cause enemy elf attacking");
  643. //}
  644. //if can attack: Attack!
  645. if(elf.inAttackRange(p)&&isItSafeToAttackEnemyBuilding(elf,p,game))//&&!elf.invisible)
  646. {
  647. elf.attack(p);
  648. }
  649. //if can't attack: move to portal
  650. else if(!(elf.isBuilding))
  651. {
  652. if(!(elf.maxSpeed>game.elfMaxSpeed)&&isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.canBuildPortal()&&game.getMyMana()>(game.portalCost+10)&&NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game)<2)
  653. {
  654. ////System.out.println("elf chose to build portal to destroy enemy's portal");
  655. elf.buildPortal();
  656. }
  657. else
  658. {
  659. //elf.moveTo(safteyLoc(elf.getLocation(),p.getLocation(),elf.maxSpeed,game));
  660. //Location whereToAttackEnemyPortal=whereElfShouldAttackBuilding(elf,p,game);
  661. Location safeLoc=safteyLoc(elf.getLocation(),p.getLocation(),elf.maxSpeed,game);
  662. elf.moveTo(safeLoc);
  663. /*if(elf.maxSpeed>game.elfMaxSpeed)
  664. {
  665. //////System.out.println(whereToAttackEnemyPortal+" WHERE TO ATTACK ENEMY PORTAL LOC");
  666. elf.moveTo(safeLoc);
  667. }
  668. else
  669. {
  670. if(safeLoc.distance(p)>elf.distance(p)&&elf.canCastSpeedUp()&&ShouldElfUseSpeedUp(elf,p.getLocation(),game))
  671. elf.castSpeedUp();
  672. else
  673. elf.moveTo(safeLoc);
  674. }*/
  675. }
  676. }
  677. }
  678. public static void tryToBuildAttackingPortal(Elf elf,Game game)
  679. {
  680. isElfTryingToBuildPortal=true;
  681. Location whereToBuildPort;
  682. int num=numOfEnoughClosePortalToEnemyCastle(game);
  683. if(num==0)
  684. whereToBuildPort=whereElfShouldBuildAttackPortal(elf,game);
  685. else
  686. whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
  687. //////System.out.println(isItSafeToBuildPortal(elf,game)+" FOR FUCKS SAKE IT IS SAFE TO BUILD A GOD DARN PORTAL");
  688.  
  689. if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost)
  690. {
  691. elf.buildPortal();
  692. }
  693. else if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
  694. {
  695. if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
  696. {
  697. if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
  698. {
  699. elf.buildPortal();
  700. //////System.out.println("elf is building a portal");
  701. }
  702. else
  703. {
  704. //////System.out.println("elf is moving safetly to the loc");
  705. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  706. elf.moveTo(whereToBuildPort);
  707. else
  708. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
  709. }
  710. }
  711. else
  712. {
  713. whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
  714. if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
  715. {
  716. if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
  717. {
  718. if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
  719. {
  720. elf.buildPortal();
  721. //////System.out.println("elf is building a portal");
  722. }
  723. }
  724. else
  725. {
  726. //////System.out.println("elf is moving safetly to the loc");
  727. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  728. elf.moveTo(whereToBuildPort);
  729. else
  730. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
  731. }
  732. }
  733. else
  734. {
  735. //////System.out.println("elf is moving safetly to the loc");
  736. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
  737. }
  738.  
  739. }
  740. }
  741. else if(!elf.isBuilding)
  742. {
  743. //////System.out.println("elf is moving to the port loc");
  744. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  745. elf.moveTo(whereToBuildPort);
  746. else
  747. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed,game));
  748. }
  749. }
  750. public static void destroyEnemysBuilding(Elf elf,Game game)
  751. {
  752. Building enemyBuilding=closestEnemyBuildingToMapObject(elf,game);
  753. if(isItSafeToAttackEnemyBuilding(elf,enemyBuilding,game)&&elf.inAttackRange(enemyBuilding))
  754. {
  755. //////System.out.println("ATTACKING ENEMY PORTAL");
  756. elf.attack(enemyBuilding);
  757. }
  758. //if not in Attack Range: move there
  759. else
  760. {
  761. //////System.out.println("MOVING SAFTLEY TO ENEMYS CLOSEST PORTAL");
  762. elf.moveTo(safteyLoc(elf.getLocation(),enemyBuilding.getLocation(),elf.maxSpeed,game));
  763. }
  764. }
  765. public static boolean isTherePortalNearEnemyCloseSpawnToCastle(Game game){
  766. int count=0;
  767. Elf CloseSpawiningElf=null;
  768. for(Elf enemyElf:game.getAllEnemyElves()){
  769. if(enemyElf.initialLocation.distance(game.getMyCastle())-game.getMyCastle().size-game.elfAttackRange<game.elfMaxSpeed*15){
  770. CloseSpawiningElf=enemyElf;
  771. }
  772. }
  773. if(CloseSpawiningElf==null)
  774. return false;
  775. for(Portal portal:game.getAllPortals()){
  776. if(portal.distance(CloseSpawiningElf.initialLocation)<game.iceTrollMaxSpeed+game.iceTrollAttackRange){
  777. count++;
  778. }
  779. }
  780. if(count==0)
  781. return true;
  782. return false;
  783. }
  784. public static void buildPortalToFuckEnemyElfWhenSpawned(Elf elf, Game game){
  785. Location PortalLocation=null;
  786. for(Elf enemyElf:game.getAllEnemyElves()){
  787. if(enemyElf.initialLocation.distance(game.getMyCastle())-game.getMyCastle().size-game.elfAttackRange<game.elfMaxSpeed*15){
  788. PortalLocation=enemyElf.initialLocation;
  789. }
  790. }
  791. ////System.out.println(PortalLocation);
  792. if(PortalLocation!=null){
  793. if(elf.distance(PortalLocation)<=elf.maxSpeed*1.5&&elf.canBuildPortal()&&game.getMyMana()>=game.manaFountainCost)
  794. {
  795. ////System.out.println("building spawn portal");
  796. elf.buildPortal();
  797. }
  798. else
  799. {
  800. ////System.out.println("going spawn portaln");
  801. if(elf.distance(PortalLocation)<=elf.maxSpeed*2)
  802. elf.moveTo(PortalLocation);
  803. else
  804. elf.moveTo(safteyLoc(elf.getLocation(),PortalLocation,elf.maxSpeed,game));
  805. }
  806. }
  807. }
  808. public static void tryToAttackEnemyCastle(Elf elf,Game game)
  809. {
  810. //if elf in Attack Range: attack enemy Casle
  811. if (elf.inAttackRange(game.getEnemyCastle())&&(isSafeLoc(elf.getLocation(),game)||game.getEnemyCastle().currentHealth<=elf.attackMultiplier*elf.currentHealth))
  812. {
  813. elf.attack(game.getEnemyCastle());
  814. //.out.println("elf " + elf + " attacks " + game.getEnemyCastle());
  815. }
  816. //go back safely to My casle
  817. else {
  818. //////System.out.println("moving saftley back to Base");
  819. elf.moveTo(safteyLoc(elf.getLocation(),game.getEnemyCastle().getLocation(),elf.maxSpeed,game));
  820. ////////System.out.println("elf " + elf + " moves to " + game.getEnemyCastle());
  821. }
  822. }
  823. public static Portal portalToDestroy(Game game){
  824. if(closestEnemyPortalToOurCastle(game)==null)
  825. return null;
  826. Portal OEnemyPortal=game.getEnemyPortals()[0];
  827. for(Portal EnemyPortal : game.getEnemyPortals()){
  828. if(EnemyPortal.distance(game.getMyCastle())<EnemyPortal.distance(game.getEnemyCastle())){
  829. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
  830. OEnemyPortal=EnemyPortal;
  831. }
  832. }
  833. }
  834. ////System.out.println(OEnemyPortal);
  835. if(OEnemyPortal.distance(game.getMyCastle())<OEnemyPortal.distance(game.getEnemyCastle()))
  836. return OEnemyPortal;
  837. else
  838. return null;
  839. }
  840.  
  841. public static int numberOfElvesNeededForAction(int action, Game game) {
  842. /*Actions: NOT BY PRIORITY!!!
  843. *0: Attack enemy elf
  844. *1: Build on the way portal (A lot of mana)
  845. *2: Build defense portal
  846. *3: Destroy enemys attacking portal
  847. *4: Going to enemy attacking elf
  848. *5: Build attacking portal --> (first side?)
  849. *6: Building good portal
  850. *7: Destroy enemy portals (Any enemy portal)
  851. *8: Attack enemy castle
  852. *9: Build mana fountains
  853. *10: Destroy enemy mana fountains
  854. *
  855. */
  856. if(action==ActiontryToAttackEnemyElf||action==ActiontryToBuildPortalCauseCan||action==ActionBuildAttackPortalWhenCan)
  857. {
  858. //////System.out.println("CALLING A PRIVATE ACTION ");
  859. return 0;
  860. }
  861. else if(action==ActionDefPortal) {//2
  862. // if elf needs help ++
  863. // && other elf is far to another action's needed positions
  864. if(isThereDefPortals(game))return 0;
  865. else return 1;
  866. }
  867. else if(action==ActionEnemyAttackingPortal) {//3
  868. //if elf needs help ++
  869. //&& other elf is far to another action's needed positions
  870. int count=0;
  871. Portal EnemyPortal=closestEnemyPortalToOurCastle(game);
  872. if(EnemyPortal==null||!isThereClosePortalToCastle(game))
  873. return 0;
  874. for(Elf EnemyElf:enemyLivingElves) {
  875. if(EnemyElf.distance(EnemyPortal)<EnemyElf.maxSpeed*5)//&&isObjectGoingForwardMe(EnemyElf,EnemyPortal,game)) {
  876. count++;
  877. }
  878. if(EnemyThreatningPortal(game)!=null)
  879. return 1;
  880. return count;
  881. }
  882. else if(action==ActionEnemyElfAttacking) {
  883. // WHEN ICE WALL IS READY CHANGE
  884. // if elf needs help 1++ (until elf doesn't need help)
  885. // && other elf is not attacking (building attack portal);
  886. int dangerousElves = 0;
  887. Elf minElf = null;
  888. for(Elf enemyElf: game.getEnemyLivingElves()) {
  889. Location locOfEnemyElf=enemyElf.getLocation();
  890. if(!game.inMap(enemyElf))
  891. locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
  892. if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=15) {
  893. dangerousElves++;
  894. if(game.getMyLivingElves().length>0)
  895. {
  896. minElf=game.getMyLivingElves()[0];
  897.  
  898. for(Elf elf:game.getMyLivingElves())
  899. {
  900. if(elf.distance(locOfEnemyElf)<minElf.distance(locOfEnemyElf))
  901. {
  902. minElf=elf;
  903. }
  904. }
  905. if(minElf.currentHealth<enemyElf.currentHealth)
  906. dangerousElves++;
  907. }
  908. }
  909. }
  910. return dangerousElves;
  911. }
  912. else if(action==ActionEnemyElfAttackingA) {
  913. // WHEN ICE WALL IS READY CHANGE
  914. // if elf needs help 1++ (until elf doesn't need help)
  915. // && other elf is not attacking (building attack portal);
  916. int dangerousElves = 0;
  917. Elf minElf = null;
  918. for(Elf enemyElf: game.getEnemyLivingElves()) {
  919. Location locOfEnemyElf=enemyElf.getLocation();
  920. if(!game.inMap(enemyElf))
  921. locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
  922. if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=4) {
  923. dangerousElves++;
  924. if(game.getMyLivingElves().length>0)
  925. {
  926. minElf=game.getMyLivingElves()[0];
  927. for(Elf elf:game.getMyLivingElves())
  928. {
  929. if(elf.distance(locOfEnemyElf)<minElf.distance(locOfEnemyElf))
  930. {
  931. minElf=elf;
  932. }
  933. }
  934. if(minElf.currentHealth<enemyElf.currentHealth)
  935. dangerousElves++;
  936. }
  937. }
  938. }
  939. return dangerousElves;
  940. }
  941. else if(action==ActionBuildAttackingPortals) {
  942. //if we dont have an attacking portal
  943. // MAYBE DO THIS boolean enemyCastleAlmostDead = false;
  944. //*** CHEN DOES THIS but if there are left elves we add them to attack
  945. //if there are no attacking portals summon one elf
  946. if((numOfEnoughClosePortalToEnemyCastle(game)==0))
  947. return game.getAllMyElves().length;
  948. //if there is atting portal and there is not many elfs return 0
  949. else if(numOfEnoughClosePortalToEnemyCastle(game)==1) {
  950. return 1;
  951. }
  952. else
  953. return 0;
  954. }
  955. else if(action==ActionDestroyEnemyBuildings) {
  956. return game.getEnemyManaFountains().length+game.getEnemyPortals().length;
  957. }
  958. else if(action==ActionBuildManaFountains){
  959. if(!shouldIbuildManaFountain(game))
  960. return 0;
  961. else
  962. return 1;
  963. }
  964. else if(action==ActionDestroyEnemyManaFountains)
  965. {
  966. if(getEnemyBestManaFountain(game)!=null)
  967. return 1;
  968. else
  969. return 0;
  970. }
  971. else if(action==ActionBuildDefManaFountains)
  972. {
  973. if(whereToDefMana(game)!=null)
  974. return 1;
  975. return 0;
  976. }
  977. return 0;
  978. }
  979. public static GameObject[] enemysCloseToLoc(Building build,Game game)
  980. {
  981. int length=game.getEnemyIceTrolls().length+game.getEnemyLivingElves().length;
  982. GameObject[] arr=new GameObject[length];
  983. int counter=0;
  984. for(IceTroll ice:game.getEnemyIceTrolls())
  985. {
  986. if(ice.distance(build)<=ice.maxSpeed*6+build.size)
  987. {
  988. arr[counter]=ice;
  989. counter++;
  990. }
  991. }
  992. for(Elf elf:game.getEnemyLivingElves())
  993. {
  994. if(elf.distance(build)<=elf.maxSpeed*6+build.size)
  995. {
  996. arr[counter]=elf;
  997. counter++;
  998. }
  999. }
  1000. counter=0;
  1001. for(GameObject obj:arr)
  1002. {
  1003. if(obj!=null)
  1004. counter++;
  1005. }
  1006. GameObject[] ret=new GameObject[counter];
  1007. for(int i=0;i<ret.length;i++)
  1008. {
  1009. ret[i]=arr[i];
  1010. }
  1011. return ret;
  1012. }
  1013. public static Location whereElfShouldAttackBuilding(Elf elf,Building build,Game game)
  1014. {
  1015. GameObject[] arr=enemysCloseToLoc(build,game);
  1016. int x=0;
  1017. int y=0;
  1018. for(GameObject obj:arr)
  1019. {
  1020. //System.out.print(obj+",");
  1021. if(x==0&&y==0)
  1022. {
  1023. x=obj.getLocation().row;
  1024. y=obj.getLocation().col;
  1025. }
  1026. else
  1027. {
  1028. x=(x+obj.getLocation().row)/2;
  1029. y=(y+obj.getLocation().col)/2;
  1030. }
  1031. }
  1032. //System.out.println();
  1033. Location middle=new Location(x,y);
  1034. Location ret=trigoLocation(build.getLocation().row,build.getLocation().col,middle,(build.size+elf.attackRange),180);
  1035. if((x!=0||y!=0)&&game.inMap(ret))
  1036. {
  1037. //System.out.println(ret);
  1038. return ret;
  1039. }
  1040. else
  1041. {
  1042. int[] degrees=new int[180];
  1043. degrees[0]=0;
  1044. int degreesCounter=1;
  1045. Castle myCastle=game.getMyCastle();
  1046. int startingRad=game.portalSize+elf.attackRange/2;
  1047. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1048. {
  1049. degrees[i]=degreesCounter;
  1050. degrees[i+1]=degreesCounter*-1;
  1051. degreesCounter++;
  1052. }
  1053. boolean found=false;
  1054. double MaxDist=build.size+elf.attackRange*3;
  1055. Location locClose=build.getLocation();
  1056. for(int radius=startingRad;radius<MaxDist;radius+=elf.attackRange)
  1057. {
  1058. for(int i=0;i<degrees.length;i++)
  1059. {
  1060. Location loc=trigoLocation(locClose.row,locClose.col,elf.getLocation(),radius,degrees[i]);
  1061. if(isSafeLoc(loc,game))
  1062. {
  1063. return loc;
  1064. }
  1065. }
  1066. }
  1067. return locClose;
  1068. }
  1069.  
  1070. }
  1071. public static Elf closestElfToEnemyManaFountain(ManaFountain mana,Game game){
  1072. if(game.getMyLivingElves().length==0||game.getEnemyManaFountains().length==0)
  1073. return null;
  1074. Elf OriginalEnemyElf=game.getMyLivingElves()[0];
  1075. for(Elf Elf:game.getMyLivingElves()) {
  1076. if(Elf.distance(mana)<=OriginalEnemyElf.distance(mana)) {
  1077. OriginalEnemyElf=Elf;
  1078. }
  1079. }
  1080. return OriginalEnemyElf;
  1081. }
  1082.  
  1083. public static ManaFountain getEnemyBestManaFountain(Game game)
  1084. {
  1085. if(game.getEnemyManaFountains().length==0)
  1086. return null;
  1087. int[] numberOfEnemies=new int[game.getEnemyManaFountains().length];
  1088. for(int i=0;i<game.getEnemyManaFountains().length;i++)
  1089. {
  1090. numberOfEnemies[i]=0;
  1091. for(Elf EnemyElf:game.getEnemyLivingElves()){
  1092. if(EnemyElf.distance(game.getEnemyManaFountains()[i])<EnemyElf.maxSpeed*10&&game.getEnemyManaFountains()[i].currentHealth>game.manaFountainMaxHealth/2){
  1093. numberOfEnemies[i]++;
  1094. }
  1095. }
  1096. for(IceTroll EnemyIce:game.getEnemyIceTrolls()){
  1097. if(EnemyIce.distance(game.getEnemyManaFountains()[i])<EnemyIce.maxSpeed*10&&EnemyIce.currentHealth>5&&game.getEnemyManaFountains()[i].currentHealth>game.manaFountainMaxHealth/2){
  1098. numberOfEnemies[i]++;
  1099. }
  1100. }
  1101. }
  1102. int[] countPortals=new int[game.getEnemyManaFountains().length];
  1103. ManaFountain min=game.getEnemyManaFountains()[0];
  1104. int index=0;
  1105. for(Portal ePortal:game.getEnemyPortals())
  1106. {
  1107. for(int i=0;i<game.getEnemyManaFountains().length;i++)
  1108. {
  1109. if(min.distance(ePortal)>game.getEnemyManaFountains()[i].distance(ePortal)){
  1110. min=game.getEnemyManaFountains()[i];
  1111. index=i;
  1112. }
  1113. }
  1114. countPortals[index]++;
  1115. }
  1116.  
  1117. ManaFountain best=game.getEnemyManaFountains()[0];
  1118. int minIndex=0;
  1119. for(int i=0;i<numberOfEnemies.length;i++){
  1120. if(numberOfEnemies[i]<numberOfEnemies[minIndex]){
  1121. best=game.getEnemyManaFountains()[i];
  1122. minIndex=i;
  1123. }
  1124. /* else if(numberOfEnemies[i]==numberOfEnemies[minIndex]&&countPortals[i]<countPortals[minIndex]) {
  1125. best=game.getEnemyManaFountains()[i];
  1126. minIndex=i;
  1127. }*/
  1128. }
  1129. if(numberOfEnemies[minIndex]<3||best.currentHealth<game.manaFountainMaxHealth/2){
  1130. return best;
  1131. }
  1132. return null;
  1133. }
  1134. public static void destroyEnemysFountains(Elf elf,Game game)
  1135. {
  1136. ManaFountain min=getEnemyBestManaFountain(game);
  1137. Location loc=whereElfShouldAttackBuilding(elf,min,game);
  1138. boolean isSpeed=(elf.maxSpeed>game.elfMaxSpeed);
  1139. if(elf.inAttackRange(min)&&isItSafeToAttackEnemyBuilding(elf,min,game)&&(!isSpeed||isSpeed&&elf.distance(loc)<elf.maxSpeed*1.1))
  1140. elf.attack(min);
  1141. else
  1142. {
  1143. if(elf.distance(min)/elf.maxSpeed>=game.speedUpExpirationTurns+2&&!isSpeed&&elf.canCastSpeedUp()&&ShouldElfUseSpeedUpFORELF(elf,min.getLocation(),game))
  1144. elf.castSpeedUp();
  1145. else if(isSpeed)
  1146. {
  1147. elf.moveTo(safteyLoc(elf.getLocation(),loc,elf.maxSpeed,game));
  1148. }
  1149. else
  1150. elf.moveTo(safteyLoc(elf.getLocation(),min.getLocation(),elf.maxSpeed,game));
  1151. }
  1152. }
  1153.  
  1154.  
  1155.  
  1156. public static void tryToBuildManaFountain(Elf elf, Game game) {
  1157. Location manaFountainBuildLoc=whereToBuildManaFountain(game);
  1158. ////System.out.println(elf.canBuildManaFountain());
  1159. if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*1.5&&elf.canBuildManaFountain()&&game.getMyMana()>=game.manaFountainCost)
  1160. {
  1161. ////System.out.println("building mana fountain");
  1162. elf.buildManaFountain();
  1163. }
  1164. else
  1165. {
  1166. ////System.out.println("going to build mana fountain");
  1167. if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*2)
  1168. elf.moveTo(manaFountainBuildLoc);
  1169. else
  1170. elf.moveTo(safteyLoc(elf.getLocation(),manaFountainBuildLoc,elf.maxSpeed,game));
  1171. }
  1172. }
  1173. public static boolean isElfBuildingAt(Location loc,Game game)
  1174. {
  1175. for(Elf elf:game.getMyLivingElves())
  1176. {
  1177. if(elf.distance(loc)<=elf.maxSpeed&&elf.isBuilding)
  1178. return true;
  1179. }
  1180. return false;
  1181. }
  1182. public static boolean shouldIbuildManaFountain(Game game) {
  1183. Location locationToBuildManaFountain=whereToBuildManaFountain(game);
  1184. if(!game.canBuildManaFountainAt(locationToBuildManaFountain)||game.getMyManaFountains().length>game.getMyPortals().length){
  1185. ////System.out.println("cant build at mana loc ");
  1186. return false;
  1187. }
  1188. if(locationToBuildManaFountain!=null&&isItSmartToBuildManaFountain(locationToBuildManaFountain,game)&&(game.getMyself().manaPerTurn*7>=game.manaFountainCost-game.getMyself().mana||ShouldSaveManaForManaFountain(game))) {
  1189. if((game.getMyself().manaPerTurn<=game.getEnemy().manaPerTurn||game.getMyself().manaPerTurn==0)) {
  1190. return true;
  1191. }
  1192. else if(game.getMyself().manaPerTurn>game.getEnemy().manaPerTurn) {
  1193. if(game.getMyPortals().length>game.getMyManaFountains().length&&game.getMyMana()>game.manaFountainCost*1.5) { //note to change
  1194. return true;
  1195. }
  1196. }
  1197. }
  1198. return false;
  1199. }
  1200. public static boolean isItSmartToBuildManaFountain(Location buildLocation, Game game){
  1201. //wiil the enemy destroy it:
  1202. boolean flag=false;
  1203. for(Elf enemyElf:enemyLivingElves){
  1204. flag=false;
  1205. if(enemyElf.distance(buildLocation)<=game.elfAttackRange+game.manaFountainSize+enemyElf.maxSpeed*(game.manaFountainBuildingDuration+4)){
  1206. for(Portal portal:game.getMyPortals()){
  1207. if(portal.distance(enemyElf)<buildLocation.distance(enemyElf)){
  1208. flag=true;
  1209. }
  1210. }
  1211. if(!flag)
  1212. return false;
  1213. }
  1214. }
  1215. return true;
  1216. }
  1217.  
  1218. public static Location whereToBuildManaFountain(Game game) {
  1219. Location whereToBuildManaFountain=null;
  1220. for(Portal portal:game.getMyPortals()) {
  1221. if(portal.distance(game.getMyCastle().getLocation())<game.manaFountainSize*5+game.getMyCastle().size) {
  1222. whereToBuildManaFountain=trigoLocation(portal.getLocation().row,portal.getLocation().col,game.getEnemyCastle().getLocation(),portal.size+game.manaFountainSize+1,180);
  1223. if(game.canBuildManaFountainAt(whereToBuildManaFountain)&&!isElfBuildingAt(whereToBuildManaFountain,game)) {
  1224. return whereToBuildManaFountain;
  1225. }
  1226. }
  1227. }
  1228. //If there are no portals:
  1229. int[] degrees=new int[360];
  1230. degrees[0]=180;
  1231. int degreesCounter=1;
  1232. Castle myCastle=game.getMyCastle();
  1233. int startingRad=20+myCastle.size;
  1234. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1235. {
  1236. degrees[i]=degreesCounter;
  1237. degrees[i+1]=degreesCounter*-1;
  1238. degreesCounter++;
  1239. }
  1240. boolean aretheredefportals=isThereDefPortals(game);
  1241. double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
  1242. Location locClose= game.getEnemyCastle().getLocation();
  1243. for(int radius=startingRad;radius<MaxDist;radius+=game.manaFountainSize)
  1244. {
  1245. for(int i=0;i<degrees.length;i++)
  1246. {
  1247. Location loc=trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  1248. if(game.canBuildManaFountainAt(loc)&&!isElfBuildingAt(loc,game))
  1249. {
  1250. if(!aretheredefportals&&loc.distance(whereElfShouldBuildDefPortal(game))>game.portalSize+game.manaFountainSize+10||aretheredefportals)
  1251. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  1252. }
  1253. }
  1254. }
  1255. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
  1256. }
  1257.  
  1258.  
  1259.  
  1260. public static Elf closestEnemyElfToObject(MapObject object, Game game) {
  1261. if(enemyLivingElves.length==0||object==null)
  1262. return null;
  1263. Elf mElf=enemyLivingElves[0];
  1264. for(Elf EnemyElf :enemyLivingElves)
  1265. {
  1266. if(EnemyElf.distance(object)+EnemyElf.maxSpeed*1.5<=mElf.distance(object))
  1267. {
  1268. mElf=EnemyElf;
  1269. }
  1270. }
  1271. return mElf;
  1272. }
  1273.  
  1274.  
  1275.  
  1276. public static Location whereElfShouldBuildDefPortal(Game game)
  1277. {
  1278. int[] degrees=new int[20];
  1279. degrees[0]=0;
  1280. int degreesCounter=1;
  1281. Castle myCastle=game.getMyCastle();
  1282. double startingRad=game.portalSize+myCastle.size+1.5;
  1283. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1284. {
  1285. degrees[i]=degreesCounter;
  1286. degrees[i+1]=degreesCounter*-1;
  1287. degreesCounter++;
  1288. }
  1289. boolean found=false;
  1290. double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
  1291. Location locClose;
  1292. if(EnemyThreatningPortal(game)!=null&&EnemyThreatningPortal(game).distance(game.getMyCastle())<=myCastle.distance(game.getEnemyCastle())/2)
  1293. locClose= EnemyThreatningPortal(game).getLocation();
  1294. else
  1295. locClose=game.getEnemyCastle().getLocation();
  1296. for(double radius=startingRad;radius<MaxDist;radius+=game.portalSize)
  1297. {
  1298. for(int i=0;i<degrees.length;i++)
  1299. {
  1300. if(game.canBuildPortalAt(trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i])))
  1301. {
  1302. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  1303. }
  1304. }
  1305. }
  1306. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
  1307. }
  1308. public static boolean isThereDefPortals(Game game)
  1309. {
  1310. //int counter=0;
  1311. //for(Portal portal:game.getMyPortals())
  1312. //{
  1313. // if(portal.distance(game.getMyCastle())-game.castleSize-game.lavaGiantAttackRange<=game.lavaGiantMaxSpeed*5)
  1314. // counter++;
  1315. //}
  1316. Location locClose=null;
  1317. if(EnemyThreatningPortal(game)!=null&&EnemyThreatningPortal(game).distance(game.getMyCastle())<=game.getMyCastle().distance(game.getEnemyCastle())/2)
  1318. locClose= EnemyThreatningPortal(game).getLocation();
  1319. else
  1320. locClose=game.getEnemyCastle().getLocation();
  1321. Location loc=game.getMyCastle().getLocation().towards(locClose,game.portalSize+game.getMyCastle().size+5);
  1322. Location port=closestAllyPortalToLoc(loc,game);
  1323. ////System.out.println(loc+" WHERE TO BUILD DEF PORTAL");
  1324. if(!game.canBuildPortalAt(loc))
  1325. return true;
  1326. if(port==null)
  1327. return false;
  1328. if(port.distance(loc)<game.portalSize)
  1329. return true;
  1330.  
  1331. return false;
  1332. }
  1333. public static boolean isItSmartToBuildPortal(Elf elf, Game game)
  1334. {
  1335. for(Elf enemyElf: enemyLivingElves)
  1336. {
  1337. if((enemyElf.distance(elf)-enemyElf.attackRange)/enemyElf.maxSpeed<=game.iceTrollSummoningDuration+game.portalBuildingDuration&&enemyElf.currentHealth>game.portalMaxHealth*enemyElf.attackMultiplier)
  1338. {
  1339. return false;
  1340. }
  1341. if(enemyElf.distance(elf)<=enemyElf.maxSpeed*2+enemyElf.attackRange)
  1342. return false;
  1343. if(elf.maxSpeed>game.elfMaxSpeed)
  1344. return false;
  1345. }
  1346. for(Tornado enTorn : game.getEnemyTornadoes()) {
  1347. if(enTorn.currentHealth / game.tornadoSuffocationPerTurn >= game.portalBuildingDuration + (enTorn.distance(elf) - game.portalSize - enTorn.attackRange) / game.tornadoMaxSpeed) {
  1348. return false;
  1349. }
  1350. }
  1351. for(Portal portal:game.getMyPortals())
  1352. {
  1353. if(portal.distance(elf)<=portal.size*4&&elf.distance(game.getEnemyCastle())>elf.distance(game.getMyCastle()))
  1354. return false;
  1355. }
  1356. if(ShouldSaveManaForManaFountain(game))
  1357. return false;
  1358. ////////System.out.println("smart building portal");
  1359. return true;
  1360. }
  1361. public static boolean isItSafeToBuildPortal(Elf elf,Game game)
  1362. {
  1363. int enemyCounter=0;
  1364. for(Elf enemyElf: enemyLivingElves)
  1365. {
  1366. if(enemyElf.distance(elf)<=enemyElf.maxSpeed*3+enemyElf.attackRange)
  1367. {
  1368. enemyCounter+=elf.attackMultiplier;
  1369. }
  1370. }
  1371. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  1372. {
  1373. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*3)
  1374. {
  1375. enemyCounter+=enemyIce.attackMultiplier;
  1376. }
  1377. }
  1378. if(enemyCounter==0)
  1379. return true;
  1380. if(elf.currentHealth-enemyCounter*1.3>game.portalBuildingDuration)
  1381. return true;
  1382. return false;
  1383. }
  1384. public static boolean isItSafeToAttackEnemyBuilding(Elf elf,Building building,Game game)
  1385. {
  1386. int enemyCounter=0;
  1387. for(Elf enemyElf: enemyLivingElves)
  1388. {
  1389. if(enemyElf.distance(elf)<=enemyElf.maxSpeed+enemyElf.attackRange)
  1390. {
  1391. enemyCounter+=elf.attackMultiplier;
  1392. }
  1393. }
  1394. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  1395. {
  1396. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed)
  1397. {
  1398. enemyCounter+=enemyIce.attackMultiplier;
  1399. }
  1400. }
  1401. if(enemyCounter==0)
  1402. return true;
  1403. if(elf.currentHealth-enemyCounter>building.currentHealth+2)
  1404. return true;
  1405. return false;
  1406. }
  1407. public static Location whereElfShouldBuildAttackPortalSides(Elf elf,Game game)
  1408. {
  1409. int[] degrees=new int[360];
  1410. degrees[0]=180;
  1411. int degreesCounter=179;
  1412. Castle enemyCastle=game.getEnemyCastle();
  1413. int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
  1414. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1415. {
  1416. degrees[i]=degreesCounter;
  1417. degrees[i+1]=degreesCounter*-1;
  1418. degreesCounter--;
  1419. }
  1420. boolean found=false;
  1421. int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
  1422. for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
  1423. {
  1424. for(int i=1;i+1<degrees.length;i+=2)
  1425. {
  1426. Location loc1=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i]);
  1427. Location loc2=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i+1]);
  1428. if(game.canBuildPortalAt(loc1)&&game.canBuildPortalAt(loc2))
  1429. {
  1430. if(elf.distance(loc1)<elf.distance(loc2))
  1431. return loc1;
  1432. else
  1433. return loc2;
  1434. }
  1435. }
  1436. }
  1437. startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
  1438. MaxDist=(int)(game.getMyCastle().distance(enemyCastle)/1.5);
  1439. for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
  1440. {
  1441. for(int i=1;i+1<degrees.length;i+=2)
  1442. {
  1443. Location loc1=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i]);
  1444. Location loc2=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i+1]);
  1445. if(game.canBuildPortalAt(loc1))
  1446. return loc1;
  1447. if(game.canBuildPortalAt(loc2))
  1448. return loc2;
  1449. }
  1450. }
  1451. ////System.out.println("NO WHERE TO BUILD ATTACKING PORT");
  1452. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
  1453. }
  1454. public static Location whereElfShouldBuildAttackPortal(Elf elf,Game game)
  1455. {
  1456. int[] degrees=new int[360];
  1457. degrees[0]=0;
  1458. int degreesCounter=1;
  1459. Castle enemyCastle=game.getEnemyCastle();
  1460. int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
  1461. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1462. {
  1463. degrees[i]=degreesCounter;
  1464. degrees[i+1]=degreesCounter*-1;
  1465. degreesCounter++;
  1466. }
  1467. boolean found=false;
  1468. int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
  1469. for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
  1470. {
  1471. for(int i=0;i<degrees.length;i++)
  1472. {
  1473. if(game.canBuildPortalAt(trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i])))
  1474. {
  1475. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i]);
  1476. }
  1477. }
  1478. }
  1479. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
  1480. }
  1481. public static Portal getClosestEnemyPortal(Elf elf,Game game)
  1482. {
  1483. if(game.getEnemyPortals().length==0)
  1484. return null;
  1485. Portal minP=game.getEnemyPortals()[0];
  1486. for(Portal p : game.getEnemyPortals())
  1487. {
  1488. if(elf.distance(p)<elf.distance(minP))
  1489. {
  1490. minP=p;
  1491. }
  1492. }
  1493. return minP;
  1494. }
  1495. public static Location whereFightWillHappen(Elf elf,Game game)
  1496. {
  1497. int xCenterOfFight=elf.getLocation().row;
  1498. int yCenterOffFight=elf.getLocation().col;
  1499. for(Elf allyElf : game.getMyLivingElves())
  1500. {
  1501. if(allyElf!=elf&&allyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
  1502. {
  1503. xCenterOfFight=(xCenterOfFight+allyElf.getLocation().row)/2;
  1504. yCenterOffFight=(yCenterOffFight+allyElf.getLocation().col)/2;
  1505. }
  1506. }
  1507. for(Elf enemyElf : enemyLivingElves)
  1508. {
  1509. if(enemyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
  1510. {
  1511. xCenterOfFight=(xCenterOfFight+enemyElf.getLocation().row)/2;
  1512. yCenterOffFight=(yCenterOffFight+enemyElf.getLocation().col)/2;
  1513. }
  1514. }
  1515. Location retLoc=new Location(xCenterOfFight,yCenterOffFight);
  1516. return retLoc;
  1517. }
  1518.  
  1519. public static boolean isIceGoingOnElf(IceTroll ice,Game game)
  1520. {
  1521. if(enemyLivingElves.length==0)
  1522. return false;
  1523. GameObject minObj=enemyLivingElves[0];
  1524. for(Elf elf:enemyLivingElves)
  1525. {
  1526. if(ice.distance(elf)<ice.distance(minObj))
  1527. minObj=elf;
  1528. }
  1529. for(Creature creat:game.getEnemyCreatures())
  1530. {
  1531. if(creat.distance(ice)<minObj.distance(ice))
  1532. return false;
  1533. }
  1534. return true;
  1535. }
  1536. public static boolean isWorthAttacking(Elf elf,double agroLevel, Game game)
  1537. {
  1538. int allyElvesAround=1;
  1539. int enemyElvesAround=0;
  1540. int allyLivesAround=elf.currentHealth;
  1541. int enemyLivesAround=0;
  1542. int isFightUrgent = 0;
  1543. Location fightLoc=whereFightWillHappen(elf,game);
  1544. if(fightLoc.distance(game.getMyCastle()) - game.getMyCastle().size <= game.elfMaxSpeed * 2 + game.elfAttackRange)
  1545. isFightUrgent = 1;
  1546.  
  1547. if(elf.distance(fightLoc)>elf.maxSpeed*5+elf.attackRange)
  1548. return false;
  1549. for(Elf enemyElf : enemyLivingElves)
  1550. {
  1551. if(enemyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
  1552. {
  1553. enemyElvesAround++;
  1554. enemyLivesAround+=enemyElf.currentHealth;
  1555. }
  1556. }
  1557. for(Elf allyElf : game.getMyLivingElves())
  1558. {
  1559. if(allyElf!=elf)
  1560. {
  1561. if(allyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
  1562. {
  1563. allyElvesAround++;
  1564. allyLivesAround+=allyElf.currentHealth;
  1565. }
  1566. }
  1567. }
  1568. if(enemyLivesAround>game.elfMaxHealth&&allyLivesAround * (1 + (isFightUrgent * 0.4))<=enemyLivesAround)
  1569. return false;
  1570.  
  1571. for(IceTroll allyIce : game.getMyIceTrolls())
  1572. {
  1573. if(allyIce.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange&&isIceGoingOnElf(allyIce,game))
  1574. {
  1575. if(allyIce.currentHealth-(allyIce.distance(fightLoc)/allyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth)
  1576. {
  1577. allyElvesAround++;
  1578. allyLivesAround+=allyIce.currentHealth;
  1579. }
  1580. }
  1581. }
  1582.  
  1583. for(IceTroll enemyIce : game.getEnemyIceTrolls())
  1584. {
  1585. if(enemyIce.currentHealth-(enemyIce.distance(fightLoc)/enemyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth||enemyIce.attackRange>=enemyIce.distance(fightLoc))
  1586. {
  1587. enemyElvesAround++;
  1588. enemyLivesAround+=enemyIce.currentHealth;
  1589. }
  1590. }
  1591. if(allyElvesAround>=enemyElvesAround&&allyLivesAround*game.elfAttackMultiplier * (1 + (isFightUrgent * 0.4)) >enemyLivesAround*game.elfAttackMultiplier+agroLevel)
  1592. {
  1593. ////System.out.println("WORTH ATTACKING "+elf);
  1594. return true;
  1595. }
  1596. return false;
  1597. }
  1598. public static int indexOfEnemyElf(Elf enemy,Game game)
  1599. {
  1600. for(int i=0;i<game.getAllEnemyElves().length;i++)
  1601. {
  1602. if(game.getAllEnemyElves()[i]==enemy)
  1603. return i;
  1604. }
  1605. return 0;
  1606. }
  1607. public static Elf isThereEnemyElfAttacking(int turns, Game game)
  1608. {
  1609. Elf minElf=null;
  1610. for(Elf enemyElf:game.getEnemyLivingElves())
  1611. {
  1612. Location locOfEnemyElf=enemyElf.getLocation();
  1613. if(!game.inMap(enemyElf))
  1614. locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
  1615. if((locOfEnemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=turns)
  1616. {
  1617. if(minElf==null)
  1618. minElf=enemyElf;
  1619. else
  1620. {
  1621. if(minElf.distance(game.getMyCastle())>locOfEnemyElf.distance(game.getMyCastle()))
  1622. minElf=enemyElf;
  1623. }
  1624. }
  1625. }
  1626. System.out.println(minElf+" ENEMY ELF ATTACKING");
  1627. return minElf;
  1628. }
  1629. public static void GoingToEnemyAttackingElf(Elf elf,Game game) {
  1630. Elf enemyElf=isThereEnemyElfAttacking(15,game);
  1631. Location locOfEnemyElf=enemyElf.getLocation();
  1632. if(!game.inMap(enemyElf))
  1633. locOfEnemyElf=enemyElvesLocations[indexOfEnemyElf(enemyElf,game)];
  1634. if(elf.inAttackRange(enemyElf)&&isWorthAttacking(elf,-1,game))
  1635. elf.attack(enemyElf);
  1636. else if(isWorthAttacking(elf,-1,game)&&elf.distance(locOfEnemyElf)<=elf.attackRange+elf.maxSpeed)
  1637. elf.moveTo(locOfEnemyElf);
  1638. else
  1639. {
  1640. int manaToSave=0;
  1641. for(Portal APortal: game.getMyPortals()){
  1642. if(isThereEnemyElfNearPortal(APortal,game)&&!AttackingPortalsSummonigFire(APortal,game))
  1643. manaToSave=game.iceTrollCost;
  1644. }
  1645. 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)
  1646. elf.castSpeedUp();
  1647. else
  1648. elf.moveTo(safteyLoc(elf.getLocation(),locOfEnemyElf,elf.maxSpeed,game));
  1649. }
  1650. }
  1651.  
  1652. public static Elf elfToFocus(Elf elf,Game game)
  1653. {
  1654. if(enemyLivingElves.length==0)
  1655. return null;
  1656. Elf elfToAttack=null;
  1657. Location fightLocation=whereFightWillHappen(elf,game);
  1658. for(Elf Eelf:enemyLivingElves)
  1659. {
  1660. if(elfToAttack==null)
  1661. {
  1662. if(Eelf.distance(fightLocation)<=Eelf.maxSpeed*3)
  1663. elfToAttack=Eelf;
  1664. }
  1665. else if(Eelf.currentHealth<=elfToAttack.currentHealth-2&&Eelf.distance(fightLocation)<=Eelf.maxSpeed*5&&Eelf.distance(fightLocation)-Eelf.maxSpeed*3<elfToAttack.distance(fightLocation))
  1666. elfToAttack=Eelf;
  1667. }
  1668. return elfToAttack;
  1669. }
  1670. public static boolean tryToAttackElf(Elf elf,Game game)
  1671. {
  1672. if(!isWorthAttacking(elf,5,game))
  1673. return false;
  1674. boolean attacked=false;
  1675. Elf enemyElf=elfToFocus(elf,game);
  1676. if(enemyElf==null)
  1677. return false;
  1678. if(elf.inAttackRange(enemyElf))
  1679. {
  1680. attacked=true;
  1681. }
  1682. //EXPLAIN!
  1683. 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)))
  1684. {
  1685. if(enemyElf.distance(elf)<game.elfMaxSpeed*5+elf.attackRange&&isEnemyElfAttackingOurElf(elf,enemyElf,game)==true)
  1686. System.out.println("helping other elf "+elf+" "+enemyElf);
  1687. else if(enemyElf.distance(elf)<=game.elfMaxSpeed+game.elfAttackRange)
  1688. System.out.println("VERY CLOSE "+elf+" "+enemyElf);
  1689. else
  1690. System.out.println("close and going forward me "+elf+" "+enemyElf);
  1691.  
  1692. if(!(elf.distance(game.getMyCastle())>elf.distance(game.getEnemyCastle())&&isThereEnemyElfAttacking(15,game)!=null))
  1693. {
  1694. if(closestEnemyPortalToOurCastle(game)!=null)
  1695. {
  1696. if(enemyElf.distance(game.getMyCastle())<closestEnemyPortalToOurCastle(game).distance(game.getMyCastle()))
  1697. {
  1698. //if(getEnemyAttackingElf)
  1699. attacked=true;
  1700. }
  1701. }
  1702. else
  1703. attacked=true;
  1704. }
  1705. }
  1706. return attacked;
  1707. }
  1708.  
  1709. public static void attackEnemyElf(Elf elf,Game game) {
  1710. Elf enemyElf=elfToFocus(elf,game);
  1711. if(elf.inAttackRange(enemyElf))
  1712. {
  1713. //////System.out.println("in Attack Range so attacking");
  1714. elf.attack(enemyElf);
  1715. }
  1716. else {
  1717. //////System.out.println("moving to attack");
  1718. elf.moveTo(enemyElf);
  1719. }
  1720.  
  1721. }
  1722.  
  1723. public static boolean isEnemyElfAttackingOurElf(Elf elfWantsToHelp,Elf enemyElf,Game game)
  1724. {
  1725. for(Elf elf : game.getMyLivingElves())
  1726. {
  1727. if(elfWantsToHelp!=elf&&enemyElf.distance(elf)<=elf.maxSpeed+elf.attackRange&&isObjectGoingForwardMe(enemyElf,elf,game))
  1728. return true;
  1729. }
  1730. return false;
  1731. }
  1732. /*public static boolean isThereClosePortalToCastle(Game game)
  1733. {
  1734. boolean isThereClosePortal=false;
  1735. for(Portal portal :game.getEnemyPortals())
  1736. {
  1737. int turnsToFire=(portal.distance(game.getMyCastle())-game.castleSize-game.lavaGiantAttackRange)/game.lavaGiantMaxSpeed;
  1738. if(game.lavaGiantMaxHealth-turnsToFire>4)
  1739. {
  1740. isThereClosePortal=true;
  1741. }
  1742. }
  1743. return isThereClosePortal;
  1744. }*/
  1745. public static boolean isThereClosePortalToCastle(Game game)
  1746. {
  1747. boolean isThereClosePortal=false;
  1748. for(Portal portal :game.getEnemyPortals())
  1749. {
  1750. if(portal.distance(game.getMyCastle())/(portal.distance(game.getEnemyCastle())+0.1)<=1.3)
  1751. {
  1752. isThereClosePortal=true;
  1753. }
  1754. }
  1755. return isThereClosePortal;
  1756. }
  1757. public static Portal closestEnemyPortalToOurCastle(Game game)
  1758. {
  1759. if(game.getEnemyPortals().length==0)
  1760. return null;
  1761. Portal mPortal=game.getEnemyPortals()[0];
  1762. for(Portal portal :game.getEnemyPortals())
  1763. {
  1764. if(portal.distance(game.getMyCastle())<=mPortal.distance(game.getMyCastle()))
  1765. {
  1766. mPortal=portal;
  1767. }
  1768. }
  1769. return mPortal;
  1770. }
  1771. public static int numOfEnoughClosePortalToEnemyCastle(Game game)
  1772. {
  1773. int counter=0;
  1774. for(Portal portal :game.getMyPortals())
  1775. {
  1776. if(portal.distance(game.getEnemyCastle())<=portal.distance(game.getMyCastle()))
  1777. {
  1778. counter++;
  1779. }
  1780. }
  1781. return counter;
  1782. }
  1783. public static boolean isSafeLocForSike(Location loc, Location preLoc,Game game)
  1784. {
  1785. int enemyCount=0;
  1786. for(Elf enemy: enemyLivingElves)//enemy saftey
  1787. {
  1788. double enemySpeed= enemy.maxSpeed;
  1789. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1790. // //////System.out.println(enemySpeed+" id "+enemy.id);
  1791. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1792. {
  1793. int allycount=0;
  1794. for(Elf elf : game.getMyLivingElves())
  1795. {
  1796. if(elf.distance(loc)<=elf.attackRange*1.1)
  1797. allycount++;
  1798. }
  1799. enemyCount++;
  1800. //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1801. return false;
  1802. }
  1803. }
  1804. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1805. {
  1806. double enemySpeed= enemy.maxSpeed;
  1807. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1808. // //////System.out.println(enemySpeed+" id "+enemy.id);
  1809. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1810. {
  1811. enemyCount++;
  1812. if(enemyCount>=1)
  1813. return false;
  1814. }
  1815. }
  1816. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1817. return false;
  1818. return true;
  1819. }
  1820. public static boolean isSafeLoc(Location loc,Game game)
  1821. {
  1822. int enemyCount=0;
  1823. /*for(Mothership mother: game.getAllMotherships())
  1824. {
  1825. if(mother.distance(loc)<game.pirateMaxSpeed)
  1826. return true;
  1827. }//WTF*/
  1828. for(Elf enemy: enemyLivingElves)//enemy saftey
  1829. {
  1830. double enemySpeed= enemy.maxSpeed;
  1831. Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
  1832. // //////System.out.println(enemySpeed+" id "+enemy.id);
  1833. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
  1834. {
  1835. int allycount=0;
  1836. for(Elf elf : game.getMyLivingElves())
  1837. {
  1838. if(elf.distance(loc)<=elf.attackRange*1.1+enemy.maxSpeed)
  1839. allycount++;
  1840. }
  1841. enemyCount++;
  1842. if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1843. return false;
  1844. }
  1845. }
  1846. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1847. {
  1848. double enemySpeed= enemy.maxSpeed;
  1849. Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
  1850. // //////System.out.println(enemySpeed+" id "+enemy.id);
  1851. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
  1852. {
  1853. enemyCount++;
  1854. if(enemyCount>=1)
  1855. return false;
  1856. }
  1857. }
  1858. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1859. return false;
  1860. //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)
  1861. //{
  1862. // return false;
  1863. //}
  1864.  
  1865. //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)
  1866. // return false;
  1867. return true;
  1868. }
  1869. public static Location trigoLocation(int nearLocX, int nearLocY,Location diractionLoc,double radius,double angle){
  1870. double zavit=(nearLocY-diractionLoc.col+0.0)/(nearLocX+0.0-diractionLoc.row);
  1871. if(nearLocX+0.0-diractionLoc.row==0)
  1872. {
  1873. if(nearLocY>=diractionLoc.col)
  1874. zavit=90;
  1875. else
  1876. zavit=-90;
  1877. }
  1878. double zavitEnemyBase=(Math.atan(zavit)+(Math.PI/180.0)*angle+0.0);
  1879. if(nearLocX<diractionLoc.row)
  1880. {
  1881. zavitEnemyBase=Math.PI+zavitEnemyBase;
  1882. }
  1883. int xRet=0;
  1884. xRet= (int)(nearLocX-radius*Math.cos(zavitEnemyBase));
  1885. int yRet=0;
  1886. yRet= (int)(nearLocY-radius*Math.sin(zavitEnemyBase));
  1887.  
  1888. Location defRow1= new Location(xRet,yRet);
  1889. return defRow1;
  1890. }
  1891. public static double safteyRate(Location loc,Location preLoc,Game game)
  1892. {
  1893. double enemyCount=0;
  1894. for(Elf enemy: enemyLivingElves)//enemy saftey
  1895. {
  1896. double enemySpeed= enemy.maxSpeed;
  1897. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1898. // ////System.out.println(enemySpeed+" id "+enemy.id);
  1899. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1900. {
  1901. enemyCount+=enemyNextLoc.distance(loc);
  1902. //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1903. }
  1904. }
  1905. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1906. {
  1907. double enemySpeed= enemy.maxSpeed;
  1908. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1909. // ////System.out.println(enemySpeed+" id "+enemy.id);
  1910. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1911. {
  1912. enemyCount+=enemyNextLoc.distance(loc);
  1913. }
  1914. }
  1915. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1916. return 0;//game.getEnemyIceTrolls().length+enemyLivingElves.length;
  1917. return enemyCount;
  1918. }
  1919. public static Location safteyLoc(Location elf,Location diractionLoc,int radius2,Game game)
  1920. {
  1921. //if(elf.distance(diractionLoc)<radius2)
  1922. // radius2=elf.distance(diractionLoc);
  1923. if(diractionLoc.row<=0||diractionLoc.col<=0||diractionLoc.col>=game.cols||diractionLoc.row>=game.rows)
  1924. return diractionLoc;
  1925. //radius=game.pirateMaxSpeed;
  1926. int[] degrees=new int[360];
  1927. degrees[0]=0;
  1928. int degreesCounter=1;
  1929. //radius=game.elfMaxSpeed;
  1930. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1931. {
  1932. degrees[i]=degreesCounter;
  1933. degrees[i+1]=degreesCounter*-1;
  1934. degreesCounter++;
  1935. }
  1936. Location loc=trigoLocation(elf.row,elf.col,diractionLoc,radius2,degrees[0]);
  1937. double safteyRateInt=safteyRate(diractionLoc,elf,game);
  1938. for(int radius=radius2;radius<=radius2*2;radius+=50)
  1939. {
  1940. for(int i=0;i<degrees.length;i++)
  1941. {
  1942. Location locToCheck=trigoLocation(elf.row,elf.col,diractionLoc,radius,degrees[i]);
  1943. if(isSafeLocForSike(locToCheck,elf,game))
  1944. {
  1945. //if(isSafeLocForSike(trigoLocation(elf.row,elf.col,diractionLoc,radius+50,degrees[i]),elf,game))
  1946. //{
  1947. //////System.out.println(degrees[i]);
  1948. if(closestAllyPortalToLoc(elf,game)!=null&&Math.abs(degrees[i])>90&&elf.distance(closestAllyPortalToLoc(elf,game))>=game.elfMaxSpeed*1.5)
  1949. {
  1950. if(diractionLoc==closestAllyPortalToLoc(elf,game))
  1951. return locToCheck;
  1952. else
  1953. return safteyLoc(elf,closestAllyPortalToLoc(elf,game),game.elfMaxSpeed,game);
  1954. }
  1955. return locToCheck;
  1956. //}
  1957. }
  1958. double safteyRateCurrent=safteyRate(locToCheck,elf,game);
  1959. if(safteyRateInt<safteyRateCurrent)
  1960. {
  1961. loc=locToCheck;
  1962. safteyRateInt=safteyRateCurrent;
  1963. }
  1964. }
  1965. }
  1966. System.out.println(elf+" choses most safe loc");
  1967. return loc;
  1968. }
  1969. public static Location closestAllyPortalToLoc(Location loc,Game game)
  1970. {
  1971. if(game.getMyPortals().length==0)
  1972. return null;
  1973. Portal maxPort=game.getMyPortals()[0];
  1974. for(Portal portal:game.getMyPortals())
  1975. {
  1976. if(portal.distance(loc)<maxPort.distance(loc))
  1977. maxPort=portal;
  1978. }
  1979. return maxPort.getLocation();
  1980. }
  1981. public static boolean isItSmartToSummon(Portal portal,Game game)
  1982. {
  1983. int counter=0;
  1984. for(Elf elf:enemyLivingElves)
  1985. {
  1986. if(elf.inAttackRange(portal))
  1987. counter+=elf.attackMultiplier;
  1988. }
  1989. for(Tornado torn:game.getEnemyTornadoes())
  1990. {
  1991. if(torn.distance(portal)<=torn.attackRange+portal.size)
  1992. counter+=torn.attackMultiplier;
  1993. }
  1994. if(counter*game.iceTrollSummoningDuration+1>=portal.currentHealth)
  1995. return false;
  1996. return true;
  1997. }
  1998. ///////////////////////////////////////////////////////////////////////////////////////////
  1999. public static void IceOrFire(Portal portal, Game game){
  2000. //System.out.println();
  2001. //System.out.println("Should Summon Attack Tornado: "+ ShouldSummonAttackTornado(portal, game)+" portal: "+ portal);
  2002. //////System.out.println();
  2003. //////System.out.println(portal);
  2004. //////System.out.println();
  2005. boolean acted=false;
  2006. if(!isItSmartToSummon(portal,game))
  2007. return;
  2008. if(game.getEnemyCastle().currentHealth<10){
  2009. //////System.out.println("Less than 10 life");
  2010. if(portal==closestPortalToEnemyCastle(game)&&portal.canSummonLavaGiant()){
  2011. portal.summonLavaGiant();
  2012. acted=true;
  2013. }
  2014. }
  2015. ////**1**\\\\
  2016. //if There is Enemy Elf Near Portal: summon Ice Troll
  2017. ////////////////////////////////PLAY! MAYBE ANOTHE FUNCTION WITH HARSHER CONDITIONS///////////////////////////////////
  2018. double numberOfIceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game);
  2019.  
  2020. //if(IcePortal(game,portal))
  2021. //{
  2022. ////System.out.println("number of ice to go: "+numberOfIceToGo);
  2023. //}
  2024. ////////System.out.println("number of ice needed for defense: "+NumberOfIceNeededForDefense(game));
  2025. if(!acted&&isThereEnemyElfMovingToMana(portal,game)&&allyIceAroundLoc(portal.getLocation(),game)<=2)
  2026. {
  2027. System.out.println(portal+" def from enemy rushing to mana");
  2028. if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost)
  2029. {
  2030. if(portal.canSummonIceTroll()){
  2031. ////System.out.println("I summon ice close enemy elf: "+portal);
  2032. portal.summonIceTroll();
  2033. acted=true;
  2034. }
  2035. }
  2036. }
  2037. else if(!acted&&isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)&&numberOfIceToGo<3) {
  2038. acted=true;
  2039. if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost)
  2040. {
  2041. if(portal.canSummonIceTroll()){
  2042. ////System.out.println("I summon ice close enemy elf: "+portal);
  2043. portal.summonIceTroll();
  2044. acted=true;
  2045. }
  2046. }
  2047. }
  2048. ////**3**\\\\
  2049. else if(!acted&&IcePortal(game,portal)&&(numberOfIceToGo>1.5||game.getMyMana()>=game.manaFountainCost+game.portalCost+game.iceTrollCost&&numberOfIceToGo>1)) {
  2050. if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost||(numberOfIceToGo>=3&&game.getMyMana()>=game.iceTrollCost+10)){//||(isEnemyElfAttackingMyCastle(game) && game.getMyMana() >= game.iceTrollCost)) {
  2051. if(portal.canSummonIceTroll()){
  2052. ////System.out.println("I summon ice enemy attacking my castle: "+portal);
  2053. portal.summonIceTroll();
  2054. acted=true;
  2055. }
  2056. }
  2057. }
  2058. else if(!acted&&shouldSummonDefTornado(portal,game)){
  2059. if(game.getMyMana()>=+game.tornadoCost&&portal.canSummonTornado()){
  2060. //System.out.println(portal+" Portal chose to summon defend tornado");
  2061. numberOfTornadoes++;
  2062. portal.summonTornado();
  2063. acted=true;
  2064. }
  2065. }
  2066. else if(!acted&&ShouldSummonAttackTornado(portal,game)>1&&portal==AttackTornadoPortal(game)){
  2067. if(game.getMyMana()>=handleMana(portal,game)+game.tornadoCost||ShouldSummonAttackTornado(portal,game)>2&&game.getMyMana()>=game.tornadoCost){
  2068. if(portal.canSummonTornado()) {
  2069. //System.out.println(portal+" Portal chose to summon attack tornado");
  2070. numberOfTornadoes++;
  2071. portal.summonTornado();
  2072. acted=true;
  2073. }
  2074. }
  2075. }
  2076. else if(!acted&&isPortalNeededToHelpAllyElf(portal,game)&&allyIceAroundLoc(enemyAttackingLoc,game)>NumberOfIceNeededForDefense(game))
  2077. {
  2078. if(game.getMyMana()>=handleMana(portal,game)+game.lavaGiantCost)
  2079. if(portal.canSummonLavaGiant())
  2080. {
  2081. //System.out.println("Sumonned lava to help elf: " +portal);
  2082. portal.summonLavaGiant();
  2083. acted=true;
  2084. }
  2085. }
  2086. ///**2**\\\\
  2087. else if(!acted&&FirePortal(game, portal)&&numberOfIceToGo<1.5)
  2088. {
  2089. if(game.getMyMana() >= handleMana(portal,game)+game.lavaGiantCost)
  2090. {
  2091. if(portal.canSummonLavaGiant()){
  2092. //System.out.println("I summon fire to attack: "+ portal);
  2093. portal.summonLavaGiant();
  2094. acted=true;
  2095. }
  2096. }
  2097. }
  2098. }
  2099.  
  2100. public static Portal DefTornadoPortal(Game game){
  2101. Portal OriginalPortal=game.getMyPortals()[0];
  2102. boolean flag=true;
  2103. for(Portal portal:game.getMyPortals())
  2104. {
  2105. flag=true;
  2106. for(Tornado tornado:game.getMyTornadoes())
  2107. {
  2108. if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(portal,game))
  2109. {
  2110. flag=false;
  2111. }
  2112. }
  2113. if(portal.distance(closestEnemyBuildingToObject(portal,game))<OriginalPortal.distance(closestEnemyBuildingToObject(OriginalPortal,game))&&flag)
  2114. {
  2115. OriginalPortal=portal;
  2116. }
  2117. }
  2118. flag=true;
  2119. for(Tornado tornado:game.getMyTornadoes())
  2120. {
  2121. if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(OriginalPortal,game))
  2122. {
  2123. flag=false;
  2124. }
  2125. }
  2126. if(flag)
  2127. {
  2128. //System.out.println("Defend tornado Portal: "+OriginalPortal);
  2129. return OriginalPortal;
  2130. }
  2131. return null;
  2132. }
  2133.  
  2134. public static Portal AttackTornadoPortal(Game game){
  2135. Portal cmpPortal=game.getMyPortals()[0];
  2136. boolean flag=true;
  2137. for(Portal portal:game.getMyPortals()){
  2138. flag=true;
  2139. for(Tornado tornado:game.getMyTornadoes())
  2140. {
  2141. if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(portal,game))
  2142. {
  2143. flag=false;
  2144. }
  2145. }
  2146. if(ShouldSummonAttackTornado(portal,game)>ShouldSummonAttackTornado(cmpPortal,game)&&flag)
  2147. {
  2148. cmpPortal=portal;
  2149. }
  2150. else if(ShouldSummonAttackTornado(portal,game)==ShouldSummonAttackTornado(cmpPortal,game)&&flag)
  2151. {
  2152. if(portal.distance(closestEnemyBuildingToMapObject(portal,game))<cmpPortal.distance(closestEnemyBuildingToMapObject(cmpPortal,game)))
  2153. {
  2154. cmpPortal=portal;
  2155. }
  2156. }
  2157. }
  2158. for(Tornado tornado:game.getMyTornadoes()){
  2159. if(closestEnemyBuildingToMapObject(tornado,game)==closestEnemyBuildingToMapObject(cmpPortal,game))
  2160. {
  2161. flag=false;
  2162. }
  2163. }
  2164. if(ShouldSummonAttackTornado(cmpPortal,game)>0&&flag)
  2165. {
  2166. //System.out.println("Attack Tornado portal: "+cmpPortal);
  2167. return cmpPortal;
  2168. }
  2169. return null;
  2170. }
  2171.  
  2172. public static boolean isObjectGoingForwardMe(Elf enemy, GameObject obj,Game game)
  2173. {
  2174. if(!enemy.isAlive())
  2175. return false;
  2176. boolean returned=false;
  2177. for(int i=0;i<game.getAllEnemyElves().length;i++)
  2178. {
  2179. if(game.getAllEnemyElves()[i]==enemy)
  2180. {
  2181. if(enemyElvesLocations[i].getLocation().row==enemy.getLocation().row)
  2182. {
  2183. //System.out.println(" THIS IS IT THIS IS IT THIS IS IT "+enemy+" "+obj);
  2184. returned= true;
  2185. }
  2186. else if(enemyElvesLocations[i].distance(obj)>=enemy.distance(obj)+10)
  2187. returned= true;
  2188. else
  2189. returned= false;
  2190. }
  2191. }
  2192. return returned;
  2193. }
  2194. public static boolean isItWorthSummoningIceToDefPortal(Portal portal,Elf enemy,Game game)
  2195. {
  2196. boolean hasSpeed=(enemy.maxSpeed>game.elfMaxSpeed);
  2197. int counter=0;
  2198. for(IceTroll ice:game.getMyIceTrolls())//elf is fucked
  2199. {
  2200. if(ice.distance(enemy)<=ice.attackRange)
  2201. {
  2202. //////System.out.println(portal+" it is not worth summoning ice to def this portal");
  2203. counter++;
  2204. }
  2205. }
  2206. if(counter>1)
  2207. return false;
  2208. if(enemy.distance(portal)-portal.size-enemy.attackRange<=enemy.maxSpeed*2)
  2209. return true;
  2210. for(IceTroll ice:game.getMyIceTrolls())
  2211. {
  2212. if(ice.distance(portal)<portal.distance(enemy)&&ice.distance(enemy)<portal.distance(enemy)&&ice.currentHealth>game.iceTrollSummoningDuration+2)
  2213. {
  2214. System.out.println(portal+" it is not worth summoning ice to def this portal");
  2215. return false;
  2216. }
  2217. }
  2218. return true;
  2219. }
  2220. public static ManaFountain manaEnemyGoingTo(Elf enemy,Game game)
  2221. {
  2222. if(game.getMyManaFountains().length==0)
  2223. return null;
  2224. int index=0;
  2225. for(index=0;index<game.getAllEnemyElves().length;index++)
  2226. {
  2227. if(game.getAllEnemyElves()[index]==enemy)
  2228. break;
  2229. }
  2230. ManaFountain minMana=game.getMyManaFountains()[0];
  2231. double minDis=ObjectdistanceFromALine(minMana.getLocation(),enemyElvesLocations[index].getLocation(),enemy.getLocation());
  2232. for(ManaFountain mana:game.getMyManaFountains())
  2233. {
  2234. double dis=ObjectdistanceFromALine(mana.getLocation(),enemyElvesLocations[index],enemy.getLocation());
  2235. if(dis<minDis)
  2236. {
  2237. minDis=dis;
  2238. minMana=mana;
  2239. }
  2240. }
  2241. if(minDis<=enemy.maxSpeed*3)
  2242. return minMana;
  2243. return null;
  2244. }
  2245. public static boolean isThereEnemyElfMovingToMana(Portal portal,Game game)
  2246. {
  2247. if(game.getMyManaFountains().length==0)
  2248. return false;
  2249. for(Elf enemyElf: enemyLivingElves) {
  2250. double distance=portal.distance(enemyElf)-portal.size-enemyElf.attackRange;
  2251. double timeForEnemyElfToGetToPortal= distance/enemyElf.maxSpeed;
  2252. double distancePortFromSpot=portal.size+enemyElf.attackRange-game.iceTrollAttackRange;
  2253. int mana=game.getMyself().manaPerTurn;
  2254. if(mana==0)
  2255. mana=5;
  2256. double turnsToStartSummoningIce=(-game.getMyMana()+(game.iceTrollCost))/mana;
  2257. if(game.getMyMana()>=game.iceTrollCost)
  2258. turnsToStartSummoningIce=0;
  2259. double timeForIceToGetToPortal=turnsToStartSummoningIce+game.iceTrollSummoningDuration+distancePortFromSpot/game.iceTrollMaxSpeed;
  2260. ManaFountain manaF=manaEnemyGoingTo(enemyElf,game);
  2261. if(timeForIceToGetToPortal+2>=timeForEnemyElfToGetToPortal&&(isObjectGoingForwardMe(enemyElf,portal,game)||enemyElf.maxSpeed>game.elfMaxSpeed)&&(isItWorthSummoningIceToDefPortal(portal,enemyElf,game)||enemyElf.maxSpeed>game.elfMaxSpeed||manaF!=null))
  2262. {
  2263. if(manaF!=null)
  2264. {
  2265. Location enemyAttackingLoc=manaF.getLocation().towards(enemyElf.getLocation(),enemyElf.attackRange+manaF.size);
  2266. double minDist=portal.distance(enemyAttackingLoc);
  2267. boolean flag=true;
  2268. for(Portal port:game.getMyPortals())
  2269. {
  2270. if(port!=portal&&port.distance(enemyAttackingLoc)<minDist)
  2271. flag=false;
  2272. }
  2273. if(flag==true)
  2274. {
  2275. System.out.println("ELF MOVING TO MANA "+enemyElf+portal);
  2276. return true;
  2277. }
  2278. }
  2279. }
  2280. }
  2281. return false;
  2282. }
  2283. public static boolean isThereEnemyElfNearPortal(Portal portal, Game game) {
  2284. for(Elf enemyElf: enemyLivingElves) {
  2285. double distance=portal.distance(enemyElf)-portal.size-enemyElf.attackRange;
  2286. double timeForEnemyElfToGetToPortal= distance/enemyElf.maxSpeed;
  2287. double distancePortFromSpot=portal.size+enemyElf.attackRange-game.iceTrollAttackRange;
  2288. int mana=game.getMyself().manaPerTurn;
  2289. if(mana==0)
  2290. mana=5;
  2291. double turnsToStartSummoningIce=(-game.getMyMana()+(game.iceTrollCost))/mana;
  2292. if(game.getMyMana()>=game.iceTrollCost)
  2293. turnsToStartSummoningIce=0;
  2294. double timeForIceToGetToPortal=turnsToStartSummoningIce+game.iceTrollSummoningDuration+distancePortFromSpot/game.iceTrollMaxSpeed;
  2295. if(timeForIceToGetToPortal+2>=timeForEnemyElfToGetToPortal&&isObjectGoingForwardMe(enemyElf,portal,game)&&isItWorthSummoningIceToDefPortal(portal,enemyElf,game))
  2296. {
  2297. double minDist=portal.distance(enemyElf);
  2298. boolean flag=true;
  2299. for(Portal port:game.getMyPortals())
  2300. {
  2301. if(port!=portal&&port.distance(enemyElf)<minDist&&isObjectGoingForwardMe(enemyElf,port,game))
  2302. flag=false;
  2303. }
  2304. if(flag==true)
  2305. {
  2306. System.out.println("CLOSE ELF "+enemyElf+portal);
  2307. return true;
  2308. }
  2309. }
  2310. }
  2311. return false;
  2312. }
  2313.  
  2314. public static int getIndexByPortal(Portal portal, Game game) {
  2315. int index=0;
  2316. for(int i=0;i<game.getMyPortals().length;i++) {
  2317. if(game.getMyPortals()[i]==portal)
  2318. index=i;
  2319. }
  2320. return index;
  2321. }
  2322.  
  2323. public static int allyIceAroundLoc(Location location, Game game) {
  2324. if(EnemyThreatningPortal(game)==null){
  2325. return game.getMyIceTrolls().length;
  2326. }
  2327. int allyIceAroundLoc=0;
  2328. for(IceTroll Ice:game.getMyIceTrolls()) {
  2329. ////////System.out.println("ObjectdistanceFromALine: "+ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle()));
  2330. if(Ice.distance(location)<Ice.maxSpeed*4/*&&ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle())<game.iceTrollMaxSpeed*4*/&&Ice.currentHealth>3) {
  2331. allyIceAroundLoc++;
  2332. }
  2333. }
  2334. return allyIceAroundLoc;
  2335. }
  2336. public static double ObjectdistanceFromALine(Location object, Location objectOnLine1 , Location objectOnLine2){
  2337. //y=mx+b
  2338. if(object==null||objectOnLine1==null||objectOnLine2==null)
  2339. return 0;
  2340. double m=getSlopeByTwoMapObjects(objectOnLine1,objectOnLine2);
  2341. double b=objectOnLine1.col-m*objectOnLine1.row;
  2342.  
  2343. double distance=Math.abs(-m*object.row+object.col-b)/Math.sqrt(m*m+1);
  2344.  
  2345. return distance;
  2346.  
  2347. }
  2348. public static double ObjectdistanceFromALine(GameObject object, GameObject objectOnLine1 , GameObject objectOnLine2){
  2349. //y=mx+b
  2350. if(object==null||objectOnLine1==null||objectOnLine2==null)
  2351. return 0;
  2352. double m=getSlopeByTwoMapObjects(objectOnLine1,objectOnLine2);
  2353. double b=objectOnLine1.getLocation().col-m*objectOnLine1.getLocation().row;
  2354.  
  2355. double distance=Math.abs(-m*object.getLocation().row+object.getLocation().col-b)/Math.sqrt(m*m+1);
  2356.  
  2357. return distance;
  2358.  
  2359. }
  2360. public static boolean isEnemyElfAttackingMyCastle(Game game){
  2361. for(Elf elf : enemyLivingElves)
  2362. {
  2363. if(elf.distance(game.getMyCastle())<=elf.attackRange+elf.maxSpeed*5+game.getMyCastle().size)
  2364. {
  2365. //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  2366. //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  2367. //////System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  2368. return true;
  2369. }
  2370. }
  2371.  
  2372. return false;
  2373. }
  2374.  
  2375. public static int EnemyLavaAttackingMyCastle(Game game) {
  2376.  
  2377. int lavaCounter=0;
  2378. if(EnemyThreatningPortal(game)!=null)
  2379. {
  2380. enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
  2381.  
  2382. for(LavaGiant lava:game.getEnemyLavaGiants())
  2383. {
  2384. if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
  2385. lavaCounter++;
  2386. }
  2387. }
  2388. else
  2389. {
  2390. for(LavaGiant lava:game.getEnemyLavaGiants())
  2391. {
  2392. if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
  2393. lavaCounter++;
  2394. }
  2395. }
  2396. //////System.out.println("Lava Counter: "+lavaCounter);
  2397. if(lavaCounter>2) {
  2398. ////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  2399. //////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  2400. //////System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  2401. }
  2402. return lavaCounter;
  2403. }
  2404.  
  2405. //Fire\\
  2406. public static boolean FirePortal(Game game, Portal portal) {
  2407. 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)) {
  2408. if(portal.canSummonLavaGiant()) {
  2409. return true;
  2410. }
  2411. }
  2412. return false;
  2413. }
  2414. public static Portal closestPortalToEnemyCastle(Game game){
  2415. if(game.getMyPortals().length==0)
  2416. return null;
  2417. Portal mPortal=game.getMyPortals()[0];
  2418. for(Portal portal :game.getMyPortals())
  2419. {
  2420. if(portal.distance(game.getEnemyCastle())<=mPortal.distance(game.getEnemyCastle())&&portal.canSummonLavaGiant())
  2421. {
  2422. mPortal=portal;
  2423. }
  2424. }
  2425. return mPortal;
  2426. }
  2427.  
  2428.  
  2429. public static double isItWorthSummoningFire(Portal portal, Game game){
  2430. return game.lavaGiantMaxHealth-(portal.distance(game.getEnemyCastle())-game.lavaGiantAttackRange-game.getEnemyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn-numOfEnemyIceDefenders(portal,game);
  2431. }
  2432.  
  2433. public static int numOfEnemyIceDefenders(Portal portal, Game game){
  2434. Location OurAttackingLoc=game.getEnemyCastle().getLocation().towards(portal,game.lavaGiantAttackRange+game.getEnemyCastle().size);
  2435. int counter=0;
  2436. for(IceTroll ice: game.getEnemyIceTrolls())
  2437. {
  2438. if(ice.distance(OurAttackingLoc)<game.iceTrollMaxSpeed*5+game.iceTrollAttackRange)
  2439. counter++;
  2440. }
  2441. return counter;
  2442. }
  2443.  
  2444. public static Building[] getEnemyBuildings(Game game){
  2445. int length=game.getEnemyPortals().length+game.getEnemyManaFountains().length;
  2446. Building[] enemyBuildings=new Building[length];
  2447. for(int i=0;i<game.getEnemyPortals().length;i++)
  2448. {
  2449. enemyBuildings[i]=game.getEnemyPortals()[i];
  2450. }
  2451. for(int j=0;j<game.getEnemyManaFountains().length;j++)
  2452. {
  2453. enemyBuildings[j+game.getEnemyPortals().length]=game.getEnemyManaFountains()[j];
  2454. }
  2455. return enemyBuildings;
  2456. }
  2457.  
  2458. public static Building closestEnemyBuildingToObjectForTornado(GameObject object,Building[] CheckedBuildings, Game game){
  2459. if(getEnemyBuildings(game).length<2||CheckedBuildings.length==getEnemyBuildings(game).length){
  2460. return null;
  2461. }
  2462. boolean flag=true;
  2463. Building OriginalEnemyBuilding;
  2464. if(object!=getEnemyBuildings(game)[0])
  2465. OriginalEnemyBuilding=getEnemyBuildings(game)[0];
  2466. else
  2467. OriginalEnemyBuilding=getEnemyBuildings(game)[1];
  2468. for(Building enemyBuilding:getEnemyBuildings(game)){
  2469. ////System.out.println("enemyBuilding: "+enemyBuilding);
  2470. flag=true;
  2471. for(int i=0;i<CheckedBuildings.length;i++){
  2472. if(enemyBuilding==CheckedBuildings[i])
  2473. flag=false;
  2474. }
  2475. if(object.distance(enemyBuilding)<object.distance(OriginalEnemyBuilding)&&flag==true){
  2476. ////System.out.println("Original: "+OriginalEnemyBuilding+" enemy: "+enemyBuilding);
  2477. OriginalEnemyBuilding=enemyBuilding;
  2478. }
  2479. }
  2480. flag=true;
  2481. for(int i=0;i<CheckedBuildings.length;i++){
  2482. if(OriginalEnemyBuilding==CheckedBuildings[i])
  2483. flag=false;
  2484. }
  2485. if(flag==true)
  2486. return OriginalEnemyBuilding;
  2487. else
  2488. return null;
  2489. }
  2490.  
  2491. public static int numberOfBuildingsTornadoCanDestroy(Building nextBuilding, int numberOfBuildings,int distanceWent,int turnsToDestroy,int numberOfIce ,Building[] CheckedBuildings, int i, Game game){
  2492. if(nextBuilding==null)
  2493. return numberOfBuildings;
  2494. Building currentBuilding=nextBuilding;
  2495. ////System.out.println("current Building: " + currentBuilding);
  2496. ////System.out.println("CheckedBuildings[i]: "+CheckedBuildings[i]);
  2497. CheckedBuildings[i]=currentBuilding;
  2498. ////System.out.println("CheckedBuildings[i]: "+CheckedBuildings[i]);
  2499. nextBuilding=closestEnemyBuildingToObjectForTornado(nextBuilding,CheckedBuildings,game);
  2500. ////System.out.println("next Building: "+ nextBuilding);
  2501. if(nextBuilding==currentBuilding||nextBuilding==null)
  2502. return numberOfBuildings;
  2503. for(IceTroll ice:game.getEnemyIceTrolls())
  2504. {
  2505. 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)
  2506. {
  2507. numberOfIce++;
  2508. }
  2509. }
  2510. boolean flag=true;
  2511. for(Elf elf:game.getMyLivingElves()){
  2512. if(elf.inAttackRange(nextBuilding))
  2513. flag=false;
  2514. }
  2515. ////System.out.println("Number of ice: "+numberOfIce);
  2516. numberOfIce=numberOfIce*game.iceTrollAttackMultiplier*2;
  2517. turnsToDestroy+=(nextBuilding.currentHealth/game.tornadoAttackMultiplier);
  2518. distanceWent+=currentBuilding.distance(nextBuilding);
  2519. if(distanceWent<game.tornadoMaxSpeed*(game.tornadoMaxHealth-turnsToDestroy-numberOfIce)+game.tornadoAttackRange+nextBuilding.size&&flag)
  2520. {
  2521. ////System.out.println("numberOfBuildings++");
  2522. numberOfBuildings++;
  2523. }
  2524. return numberOfBuildingsTornadoCanDestroy(nextBuilding,numberOfBuildings,distanceWent,turnsToDestroy,0,CheckedBuildings,i+1,game);
  2525. }
  2526.  
  2527. public static int ShouldSummonAttackTornado(Portal portal, Game game){
  2528. Building[] CheckedBuildings=new Building[getEnemyBuildings(game).length+1];
  2529. int numberOfBuildings=numberOfBuildingsTornadoCanDestroy(portal,0,0,0,0,CheckedBuildings,0,game);
  2530. return numberOfBuildings;
  2531. }
  2532. //Ice\\
  2533. public static boolean IcePortal(Game game, Portal portal) {
  2534. if(getEnemyAttackingElf(10,game)!=null) {
  2535. if(portal==ClosestPortalToEnemyElf(game,ClosestEnemyElfToOurCastle(game))&&isObjectGoingForwardMe(ClosestEnemyElfToOurCastle(game),game.getMyCastle(),game)) {
  2536. if(portal.canSummonIceTroll()){
  2537. //////System.out.println("This portal is ice for enemy attacking elf: "+portal);
  2538. return true;
  2539. }
  2540. }
  2541. }
  2542. if(allyIceAroundLoc(enemyAttackingLoc,game)<NumberOfIceNeededForDefense(game)){
  2543. if(portal==PortalToDefenseFire(game))
  2544. if(portal.canSummonIceTroll()){
  2545. //////System.out.println("This portal is ice for enemy attacking Lava: "+portal);
  2546. return true;
  2547. }
  2548. }
  2549. return false;
  2550. }
  2551. public static Portal[] getMyDefensePortals(Game game) {
  2552. int DefensePortals=0;
  2553. for(Portal portal : game.getMyPortals()) {
  2554. if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle())) {
  2555. DefensePortals++;
  2556. }
  2557. }
  2558. Portal[] DefensePortals1=new Portal[DefensePortals];
  2559. for(int i=0;i<DefensePortals1.length;i++) {
  2560. for(Portal portal : game.getMyPortals()) {
  2561. if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle()))
  2562. DefensePortals1[i]=portal;
  2563. }
  2564. }
  2565. return DefensePortals1;
  2566. }
  2567. public static Elf getEnemyAttackingElf(int turns, Game game) {
  2568. if(enemyLivingElves.length==0)
  2569. return null;
  2570. Elf elf=enemyLivingElves[0];
  2571. for(Elf EnemyElf: enemyLivingElves) {
  2572. if(EnemyElf.maxSpeed*turns>EnemyElf.distance(game.getMyCastle())) {
  2573. if(EnemyElf.distance(game.getMyCastle())<elf.distance(game.getMyCastle())) {
  2574. elf=EnemyElf;
  2575. }
  2576. }
  2577. }
  2578. if(elf.maxSpeed*turns>elf.distance(game.getMyCastle()))
  2579. return elf;
  2580. return null;
  2581. }
  2582. public static Portal PortalToDefenseFire(Game game) {
  2583. if(game.getMyPortals().length==0||game.getEnemyPortals().length==0)
  2584. return null;
  2585. Portal SPortal=game.getMyPortals()[0];
  2586. for(Portal portal : game.getMyPortals()) {
  2587. 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)))
  2588. SPortal=portal;
  2589. }
  2590. // //////System.out.println("This is the portal to defense from: "+EnemyThreatningPortal(game));
  2591. // //////System.out.println("This is the Portal to defense Fire: "+SPortal);
  2592. return SPortal;
  2593. }
  2594. public static Elf ClosestEnemyElfToOurCastle(Game game) {
  2595. if(enemyLivingElves.length==0)
  2596. return null;
  2597. Elf OriginalEnemyElf=enemyLivingElves[0];
  2598. for(Elf EnemyElf:enemyLivingElves) {
  2599. if(EnemyElf.distance(game.getMyCastle())<=OriginalEnemyElf.distance(game.getMyCastle())) {
  2600. OriginalEnemyElf=EnemyElf;
  2601. }
  2602. }
  2603. return OriginalEnemyElf;
  2604. }
  2605. public static Portal ClosestPortalToEnemyElf(Game game, Elf elf) {
  2606. if(game.getMyPortals().length==0)
  2607. return null;
  2608. Portal OriginalPortal=game.getMyPortals()[0];
  2609. for(Portal portal:game.getMyPortals()) {
  2610. if(portal.distance(elf)<=OriginalPortal.distance(elf))
  2611. OriginalPortal=portal;
  2612. }
  2613. return OriginalPortal;
  2614. }
  2615.  
  2616. public static boolean isThereCloseEnemyPortalToCastle(Game game){
  2617. boolean isThereClosePortal=false;
  2618. for(Portal portal :game.getEnemyPortals())
  2619. {
  2620. if(game.lavaGiantMaxHealth-(portal.distance(game.getMyCastle())/game.lavaGiantMaxSpeed)/game.lavaGiantSuffocationPerTurn-allyIceAroundLoc(enemyAttackingLoc,game)>=3)
  2621. {
  2622. isThereClosePortal=true;
  2623. }
  2624. }
  2625. return isThereClosePortal;
  2626. }
  2627.  
  2628. public static Portal EnemyThreatningPortal(Game game) {
  2629. if(closestEnemyPortalToOurCastle(game)==null)
  2630. return null;
  2631. Portal OEnemyPortal=game.getEnemyPortals()[0];
  2632. for(Portal EnemyPortal : game.getEnemyPortals()){
  2633. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
  2634. OEnemyPortal=EnemyPortal;
  2635. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)){
  2636.  
  2637. }
  2638. }
  2639. }
  2640.  
  2641. if(getEnemyLavaGiantsByPortal(game,OEnemyPortal)==0)
  2642. return closestEnemyPortalToOurCastle(game);
  2643. ////////System.out.println("EnemyThreatningPortal: "+OEnemyPortal+" Lava for this portal: "+getEnemyLavaGiantsByPortal(game,OEnemyPortal));
  2644. return OEnemyPortal;
  2645. }
  2646.  
  2647. public static Building closestEnemyBuildingToObject(GameObject object, Game game){
  2648. if(getEnemyBuildings(game).length==0){
  2649. return null;
  2650. }
  2651. Building OriginalEnemyBuilding=getEnemyBuildings(game)[0];
  2652. for(Building enemyBuilding:getEnemyBuildings(game)){
  2653. if(object.distance(enemyBuilding)<object.distance(OriginalEnemyBuilding))
  2654. OriginalEnemyBuilding=enemyBuilding;
  2655. }
  2656. return OriginalEnemyBuilding;
  2657. }
  2658.  
  2659. public static boolean shouldSummonDefTornado(Portal originPortal, Game game) {
  2660. int enIceAroundPortal = 0;
  2661. int allyIceAroundPortal = 0;
  2662. int enElvesAroundPortal = 0;
  2663. int allyElvesAroundPortal = 0;
  2664. boolean flag=true;
  2665. if(originPortal == null)
  2666. return false;
  2667.  
  2668. Building closestBuilding = closestEnemyBuildingToObject(originPortal, game);
  2669.  
  2670. if(closestBuilding != null){
  2671. if(closestBuilding instanceof Portal) {
  2672. 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)) {
  2673. enIceAroundPortal = 0;
  2674. allyIceAroundPortal = 0;
  2675. enElvesAroundPortal = 0;
  2676. allyElvesAroundPortal = 0;
  2677.  
  2678. for(IceTroll ice : game.getEnemyIceTrolls()) {
  2679. if(ice.distance(closestBuilding) <= game.iceTrollMaxSpeed * 4) {
  2680. enIceAroundPortal++;
  2681. }
  2682. }
  2683. for(IceTroll ice : game.getMyIceTrolls()) {
  2684. if(ice.distance(closestBuilding) <= game.iceTrollMaxSpeed * 4) {
  2685. enIceAroundPortal++;
  2686. }
  2687. }
  2688.  
  2689. for(Elf elf : game.getEnemyLivingElves()) {
  2690. if(elf.distance(closestBuilding) <= elf.maxSpeed * 4) {
  2691. enElvesAroundPortal++;
  2692. }
  2693. }
  2694. for(Elf elf : game.getMyLivingElves()) {
  2695. if(elf.distance(closestBuilding) <= elf.maxSpeed * 4) {
  2696. allyElvesAroundPortal++;
  2697. }
  2698. }
  2699. for(Tornado tornado:game.getMyTornadoes())
  2700. {
  2701. if(closestEnemyBuildingToMapObject(tornado,game)==closestBuilding)
  2702. {
  2703. flag=false;
  2704. }
  2705. }
  2706. if(flag&&(allyElvesAroundPortal == 0 || enIceAroundPortal - allyIceAroundPortal > 2 || enElvesAroundPortal > allyElvesAroundPortal || game.getEnemyMana() > game.iceTrollCost * 2.3)) {
  2707. return true;
  2708. }
  2709. }
  2710. }
  2711. }
  2712. return false;
  2713. }
  2714.  
  2715. public static double getSlopeByTwoMapObjects(MapObject FistObject,MapObject SecondObject) {
  2716. double Slope;
  2717. double DeltaY;
  2718. double DeltaX;
  2719. DeltaY=FistObject.getLocation().col-SecondObject.getLocation().col;
  2720. DeltaX=FistObject.getLocation().row-SecondObject.getLocation().row;
  2721. Slope=DeltaY/DeltaX;
  2722. return Slope;
  2723. }
  2724. public static double getSlopeByTwoMapObjects(Location FistObject,Location SecondObject) {
  2725. double Slope;
  2726. double DeltaY;
  2727. double DeltaX;
  2728. DeltaY=FistObject.col-SecondObject.col;
  2729. DeltaX=FistObject.row-SecondObject.row;
  2730. Slope=DeltaY/DeltaX;
  2731. return Slope;
  2732. }
  2733.  
  2734. public static int getEnemyLavaGiantsByPortal(Game game, Portal portal) {
  2735. if(portal==null)
  2736. return 0;
  2737. double slopePC=getSlopeByTwoMapObjects(portal,game.getMyCastle());
  2738. int lavaGiantsOnSlope=0;
  2739. for(LavaGiant LavaGiant : game.getEnemyLavaGiants()) {
  2740. if(Math.abs(getSlopeByTwoMapObjects(LavaGiant,game.getMyCastle())-slopePC)<0.5) {
  2741. lavaGiantsOnSlope++;
  2742. }
  2743. }
  2744. return lavaGiantsOnSlope;
  2745. }
  2746.  
  2747. public static boolean isElfInNeedOfFire(Elf elf,Portal portal,Game game)
  2748. {
  2749. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  2750. {
  2751. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*2&&enemyIce.currentHealth/enemyIce.suffocationPerTurn>=portal.distance(elf)/game.lavaGiantMaxSpeed)
  2752. {
  2753. return true;
  2754. }
  2755. }
  2756. return false;
  2757. }
  2758.  
  2759. public static boolean isPortalNeededToHelpAllyElf(Portal portal,Game game)
  2760. {
  2761. if(game.getMyLivingElves().length==0)
  2762. return false;
  2763. Elf elfToHelp=null;
  2764. for(Elf elf:game.getMyLivingElves())
  2765. {
  2766. if(isElfInNeedOfFire(elf,portal,game)==true)
  2767. {
  2768. elfToHelp=elf;
  2769. }
  2770. }
  2771. if(elfToHelp==null)
  2772. {
  2773. return false;
  2774. }
  2775.  
  2776. Portal bestP=null;
  2777. double minDist=game.rows*game.rows;//biggy
  2778. for(Portal myPort:game.getMyPortals())
  2779. {
  2780. if(myPort.distance(game.getEnemyCastle())>elfToHelp.distance(game.getEnemyCastle()))
  2781. {
  2782. //double elfDistLineFromIce=ObjectdistanceFromALine(elfToHelp,iceToDef,);
  2783. double mMyPortCastle=getSlopeByTwoMapObjects(myPort,game.getEnemyCastle());
  2784. double mElfCastle=getSlopeByTwoMapObjects(myPort,elfToHelp);
  2785. mMyPortCastle=Math.atan(mMyPortCastle);
  2786. mElfCastle=Math.atan(mMyPortCastle);
  2787. if(Math.abs(mMyPortCastle-mElfCastle)<=Math.PI/6 && myPort.distance(elfToHelp)<minDist && game.lavaGiantSummoningDuration<elfToHelp.currentHealth)
  2788. {
  2789. minDist=myPort.distance(elfToHelp);
  2790. bestP=myPort;
  2791. }
  2792. }
  2793. }
  2794. ////System.out.println("THIS ELF NEEDS HELP "+elfToHelp+" best portal "+bestP);
  2795. if(bestP!=null&&bestP==portal)
  2796. {
  2797. ////System.out.println(bestP+" "+elfToHelp+" "+" this portal SHOULD SUMMON FIRE TO HELP ELF");
  2798. return true;
  2799. }
  2800. return false;
  2801. }
  2802.  
  2803. public static boolean AttackingPortalsSummonigFire(Portal port,Game game){
  2804. int countSuperAttackingPortals=0;
  2805. boolean flag=false;
  2806. for(Portal portal:game.getMyPortals()){
  2807. if(port.distance(portal)<=portal.size*4&&portal!=port)
  2808. {
  2809. if(portal.distance(game.getEnemyCastle())<portal.distance(game.getMyCastle()));
  2810. flag=true;
  2811. }
  2812. if(!flag&&portal.distance(game.getEnemyCastle())<=game.lavaGiantMaxSpeed*8+game.lavaGiantAttackRange+game.getEnemyCastle().size)
  2813. {
  2814. countSuperAttackingPortals++;
  2815. }
  2816. }
  2817. if(countSuperAttackingPortals>1)//&&flag==true)
  2818. return true;
  2819. else
  2820. return false;
  2821. }
  2822.  
  2823. public static double NumberOfIceNeededForDefense(Game game){
  2824. if(EnemyThreatningPortal(game)!=null){
  2825. double FinalLavaLife;
  2826. double NumberOfIceNeededForDefense=0;
  2827. if(game.getEnemyLavaGiants().length==0)
  2828. return 0;
  2829. LavaGiant maxLava=game.getEnemyLavaGiants()[0];
  2830. for(LavaGiant lava:game.getEnemyLavaGiants())
  2831. {
  2832. if(lava.distance(game.getMyCastle())>maxLava.distance(game.getMyCastle()))
  2833. maxLava=lava;
  2834. }
  2835. FinalLavaLife=maxLava.currentHealth-(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange)))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn;
  2836. //////System.out.println(FinalLavaLife+" "+maxLava+" life that maxLava will have when close to my castle "+maxLava.currentHealth);
  2837. NumberOfIceNeededForDefense=(FinalLavaLife*0.8)/3;
  2838. //if(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>game.iceTrollSummoningDuration*NumberOfIceNeededForDefense)
  2839. // return 0;
  2840. //if(game.getEnemyLavaGiants().length<3)
  2841. // return EnemyLavaAttackingMyCastle(game);
  2842. return NumberOfIceNeededForDefense;
  2843. }
  2844. return 0;
  2845. }
  2846.  
  2847. public static int getPortalImportance(Portal portal, Game game) {
  2848. int importance;
  2849. if(isThereEnemyElfMovingToMana(portal,game)) {
  2850. importance=6;
  2851. }
  2852. if(isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)) {
  2853. importance=5;
  2854. }
  2855. else if(IcePortal(game,portal)) {
  2856. importance=4;
  2857. }
  2858. else if(ShouldSummonAttackTornado(portal,game)>1){
  2859. importance=3;
  2860. }
  2861. else if(isPortalNeededToHelpAllyElf(portal,game)) {
  2862. importance=2;
  2863. }
  2864. else if(FirePortal(game, portal)) {
  2865. importance=1;
  2866. }
  2867. else
  2868. importance=0;
  2869. return importance;
  2870. }
  2871.  
  2872. public static boolean ShouldSaveManaForManaFountain(Game game){
  2873. Location locationToBuildManaFountain=whereToBuildManaFountain(game);
  2874. if(game.getMyLivingElves().length==0||locationToBuildManaFountain==null||!game.canBuildManaFountainAt(locationToBuildManaFountain)||game.getMyManaFountains().length>game.getMyPortals().length){
  2875. return false;
  2876. }
  2877. if(game.getMyself().manaPerTurn<=game.getEnemy().manaPerTurn||game.getMyself().manaPerTurn==0){
  2878. if(game.getEnemyLivingElves().length==0)
  2879. return true;
  2880. }
  2881. return false;
  2882. }
  2883.  
  2884. public static int handleMana(Portal portal, Game game) {
  2885. boolean EnemyElfNearPortal=false;
  2886. for(Portal APortal: game.getMyPortals()){
  2887. if(isThereEnemyElfNearPortal(APortal,game)&&!AttackingPortalsSummonigFire(APortal,game))
  2888. EnemyElfNearPortal=true;
  2889. }
  2890. double iceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc,game);
  2891. int ManaSave = 0;
  2892. int PortalImportance=getPortalImportance(portal,game);
  2893. for(int i=getIndexByPortal(portal,game);i<game.getMyPortals().length;i++) {
  2894. ////System.out.println("Portal: "+portal+" Portal importance: "+PortalImportance);
  2895. int portImportance=getPortalImportance(game.getMyPortals()[i],game);
  2896. if(PortalImportance<portImportance) {
  2897. if(portImportance==6){
  2898. //////System.out.println("Saving for Close elf");
  2899. ManaSave+=game.iceTrollCost;
  2900. }
  2901. else if(portImportance==5){
  2902. //////System.out.println("Saving for Close elf");
  2903. ManaSave+=game.iceTrollCost;
  2904. }
  2905. else if(portImportance==4){
  2906. //////System.out.println("Saving for defense");
  2907. ManaSave+=game.iceTrollCost;
  2908. }
  2909. else if(portImportance==3){
  2910. ManaSave+=game.tornadoCost;
  2911. }
  2912. else if(portImportance==2){
  2913. //////System.out.println("Saving for Lava help");
  2914. ManaSave+=game.lavaGiantCost;
  2915. }
  2916. else if(portImportance==1){
  2917. //////System.out.println("Saving mana to attack");
  2918. ManaSave+=game.lavaGiantCost;
  2919. }
  2920. else
  2921. ManaSave+=0;
  2922. }
  2923. }
  2924. if(ShouldSaveManaForManaFountain(game)){//&&!EnemyElfNearPortal&&(iceToGo<1.5||game.getMyself().manaPerTurn==0)){
  2925. ////System.out.println("Saving for mana fountain");
  2926. ManaSave+=game.manaFountainCost;
  2927. }
  2928. if(EnemyElfNearPortal&&ManaSave<game.iceTrollCost&&PortalImportance<3){
  2929. //////System.out.println("Saving for near elf");
  2930. ManaSave+=game.iceTrollCost;
  2931. }
  2932. if(!isThereDefPortals(game)&&isElfTryingToBuildPortal){
  2933. //////System.out.println("Saving for defense portal");
  2934. ManaSave+=game.portalCost;
  2935. }
  2936. if(!EnemyElfNearPortal&&isElfTryingToBuildPortal&&iceToGo<2.1) {
  2937. //////System.out.println("Saving for attack portal");
  2938. ManaSave+=game.portalCost-game.getMyself().manaPerTurn;
  2939. }
  2940. //System.out.println("ManaSave= "+ManaSave+" Portal: "+portal);
  2941. return ManaSave;
  2942. }
  2943.  
  2944. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement