Advertisement
Guest User

16.2 night Angry bird fix

a guest
Feb 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 67.43 KB | None | 0 0
  1. package bots;
  2. import elf_kingdom.*;
  3. public class MyBot implements SkillzBot {
  4. /*
  5. TO DO:
  6. 1. build portals in places that they are hard to be destoyed and they are most effective in. Thats why we lose to the other bots.
  7. If we build attacking portals that are far from enemy elves and from enemy portals and from enemy's best defense portals(by slope)
  8. we start winning a lot.
  9. 2. Ice Wall
  10. 3. The elves are waiting for mana instead of attacking enemy super close portal!
  11. 4. First fight
  12. 5. Imporve Skie?
  13. */
  14. static Location[] enemyElvesLocations;
  15. static Location[] myElvesLocations;
  16. static boolean isElfTryingToBuildPortal;
  17. static Location enemyAttackingLoc;
  18.  
  19. static int ActiontryToAttackEnemyElf=0;
  20. static int ActiontryToBuildPortalCauseCan=1;
  21. static int ActionDefPortal=2;
  22. static int ActionEnemyAttackingPortal=3;
  23. static int ActionEnemyElfAttacking=4;
  24. static int ActionBuildManaFountains=5;
  25. static int ActionDestroyEnemyManaFountains=6;
  26. static int ActionBuildAttackingPortals=7;
  27. static int ActionBuildAttackPortalWhenCan=8;
  28. static int ActionDestroyEnemyBuildings=9;
  29. static int ActionAttackEnemyCastle=10;
  30.  
  31.  
  32. public void doTurn(Game game) {
  33. System.out.println(isTherePortalNearEnemyCloseSpawnToCastle(game));
  34. System.out.println(whereToBuildManaFountain(game)+" where to build mana fountain");
  35. for(int i=0;i<=9;i++)
  36. System.out.println(numberOfElvesNeededForAction(i,game)+" action number "+i);
  37. if(game.turn==1)
  38. {
  39. turnOne(game);
  40. }
  41. if(EnemyThreatningPortal(game)!=null)
  42. enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
  43. else
  44. enemyAttackingLoc=null;
  45. isElfTryingToBuildPortal=false;
  46. //System.out.println("Elves:");
  47. handleElvesNew(game);
  48. //System.out.println("");
  49. //System.out.println("Portals:");
  50. handlePortals(game);
  51. //END OF TURN RESET LOCATIONS OF ELVES
  52. for(int i=0;i<game.getAllEnemyElves().length;i++)//update enemyPirates Loc
  53. {
  54. enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
  55. }
  56. for(int i=0;i<game.getAllMyElves().length;i++)//update myPirates Loc
  57. {
  58. myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
  59. }
  60.  
  61. }
  62. public static void turnOne(Game game)
  63. {
  64. enemyElvesLocations=new Location[game.getAllEnemyElves().length];
  65. myElvesLocations=new Location[game.getAllMyElves().length];
  66.  
  67. for(int i=0;i<game.getAllEnemyElves().length;i++)
  68. {
  69. enemyElvesLocations[i]=game.getAllEnemyElves()[i].getLocation();
  70. }
  71. for(int i=0;i<game.getAllMyElves().length;i++)
  72. {
  73. myElvesLocations[i]=game.getAllMyElves()[i].getLocation();
  74. }
  75. }
  76.  
  77. private void handlePortals(Game game) {
  78. for(Portal portal: game.getMyPortals())
  79. {
  80. IceOrFire(portal,game);
  81. }
  82. }
  83. public static void swapElves(Elf elf,Elf[] elvesForActions,int counter,int actionNum,Game game)
  84. {
  85. if(counter==elvesForActions.length)
  86. {
  87. return;
  88. }
  89. else if(elvesForActions[counter]==null)
  90. {
  91. elvesForActions[counter]=elf;
  92. return;
  93. }
  94. else if(elvesForActions[counter]!=elf&&isElfBetterAtAction(elf,elvesForActions[counter],actionNum,game))
  95. {
  96. Elf keeper=elvesForActions[counter];
  97. elvesForActions[counter]=elf;
  98. swapElves(keeper,elvesForActions,0,actionNum,game);
  99. }
  100. else
  101. {
  102. swapElves(elf,elvesForActions,counter+1,actionNum,game);
  103. }
  104. }
  105. public static void elvesDoAction(Elf[] elves,int action,Game game)
  106. {
  107. for(int i=0;i<elves.length;i++)
  108. {
  109. if(elves[i]!=null)
  110. {
  111. Elf elf=elves[i];
  112. System.out.println(elf+" is it smart "+isItSmartToBuildPortal(elf,game)+" is it safe "+isItSafeToBuildPortal(elf,game));
  113. if(elf.isBuilding)
  114. System.out.println(elf+" elf is building");
  115. ///**0**\\\ ---PRIVATE
  116. //if there is no enemy close to casle: Try to attack an Elf close to you\\
  117. else if(tryToAttackElf(elf,game))
  118. {
  119. attackEnemyElf(elf,game);
  120. System.out.println(elf+" Elf Attacking");
  121. }
  122.  
  123. ////**1**\\\\---PRIVATE
  124. //if it is safe and smart and has mana build a portal
  125. else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.canBuildPortal()&&!(elf.isBuilding)&&(game.getMyMana()>=(game.portalCost+game.lavaGiantCost-game.getMyself().manaPerTurn)&&(!shouldIbuildManaFountain(game)||game.getMyMana()>game.manaFountainCost)))
  126. {
  127. System.out.println(elf+" elf chose to build portal cause he has a lot of mana");
  128. elf.buildPortal();
  129. }
  130. else if(areTheyCloseFountainsToElf(elf,game))
  131. tryToDestroyEnemyCloseFountain(elf,game);
  132. else if(game.getEnemyCastle().currentHealth==75&&game.getMyself().manaPerTurn==0&&game.getMyself().mana==0)
  133. {
  134. if(areTheyCloseFountainsToElf(elf,game))
  135. tryToDestroyEnemyCloseFountain(elf,game);
  136. else
  137. elf.attack(game.getEnemyLavaGiants()[0]);
  138. }
  139. else if(game.getMyMana()==0&&game.getMyself().manaPerTurn==0)
  140. tryToAttackEnemyCastle(elf,game);
  141. ////**2**\\\\
  142. //if there is no portal defending go build one
  143. else if(action==ActionDefPortal)
  144. {
  145. System.out.println(elf+" elf chose to build def portal");
  146. buildDefPortal(elf,game);
  147. }
  148. ////**3**\\\\
  149. //Is there is a Close Enemy Portal To Castle\\
  150. else if(action==ActionEnemyAttackingPortal)
  151. {
  152. System.out.println(elf+" elf chose to destroy enemy elves attacking portal");
  153. destroyEnemysAttackingPortal(elf,game);
  154. }
  155. ////**4**\\\\
  156. //checking if enemy elf is on his way to attack our castle
  157. else if(action==ActionEnemyElfAttacking)//ACTION THAT DOES STUFF!!!!
  158. {
  159. GoingToEnemyAttackingElf(elf,game);
  160. System.out.println(elf+" going to def cause enemy elf attacking");
  161. }
  162. ////**5**\\\\
  163. //if there is no attack portal: Go build one\\
  164. else if(action==ActionBuildAttackingPortals)
  165. {
  166. System.out.println(elf+" elf chose to build attacking portal");
  167. tryToBuildAttackingPortal(elf,game);
  168. }
  169. ////**6**\\\\
  170. //if you are close to enemys castle and you can build portal, build one
  171. else if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost)
  172. {
  173. System.out.println(elf+" elf chose to build an attacking portal cause he can");
  174. elf.buildPortal();
  175. }
  176. ////**7**\\\\
  177. //if enemy has portals, try to destroy them
  178. else if(action==ActionDestroyEnemyBuildings)
  179. {
  180. System.out.println(elf+" elf chose to destroy enemys buildings");
  181. destroyEnemysBuilding(elf,game);
  182. }
  183. else if(action==ActionBuildManaFountains)
  184. {
  185. System.out.println(elf+" trying to build a mana fountain");
  186. tryToBuildManaFountain(elf,game);
  187. }
  188. else if(action==ActionDestroyEnemyManaFountains)
  189. {
  190. System.out.println(elf+" destroying enemy mana founctain");
  191. destroyEnemysFountains(elf,game);
  192. }
  193. ////**8**\\\\
  194. else//attack enemys castle at last
  195. {
  196. //System.out.println(elf+" elf chose to attack the castle");
  197. tryToAttackEnemyCastle(elf,game);
  198. }
  199. }
  200. }
  201. }
  202. public static void handleElvesNew(Game game)
  203. {
  204. Elf[] elvesNotUsed=new Elf[game.getMyLivingElves().length];
  205. boolean hasElvesForAction=false;
  206. for(int i=0;i<elvesNotUsed.length;i++)
  207. {
  208. elvesNotUsed[i]=game.getMyLivingElves()[i];
  209. }
  210.  
  211. for(int i=0;i<=10;i++)//MAIN FOR 8 is number of actions
  212. {
  213. if(i!=ActiontryToAttackEnemyElf&&i!=ActiontryToBuildPortalCauseCan&&i!=ActionBuildAttackPortalWhenCan)//PRIVATE FUNCS
  214. {
  215. int numbOfElvesNeededForAction=numberOfElvesNeededForAction(i,game);
  216. if(numbOfElvesNeededForAction>0)
  217. {
  218. Elf[] elvesForActions=new Elf[numbOfElvesNeededForAction];
  219. for(int n=0;n<elvesNotUsed.length;n++)
  220. {
  221. if(elvesNotUsed[n]!=null)
  222. {
  223. swapElves(elvesNotUsed[n],elvesForActions,0,i,game);
  224. }
  225. }
  226. for(int k=0;k<elvesNotUsed.length;k++)
  227. {
  228. for(Elf elfUsed:elvesForActions)
  229. {
  230. if(elvesNotUsed[k]==elfUsed)
  231. {
  232. elvesNotUsed[k]=null;
  233. }
  234. }
  235. }
  236. //System.out.println("action numba "+i);
  237. //for(int k=0;k<elvesForActions.length;k++)
  238. // System.out.print(elvesForActions[k]+",");
  239. //System.out.println();
  240. //for(int k=0;k<elvesNotUsed.length;k++)
  241. // System.out.print(elvesNotUsed[k]+",");
  242. //System.out.println();
  243. elvesDoAction(elvesForActions,i,game);
  244. }
  245. }
  246. }
  247. for(int i=0;i<elvesNotUsed.length;i++)
  248. {
  249. if(elvesNotUsed[i]!=null)
  250. {
  251. tryToBuildAttackingPortal(elvesNotUsed[i],game);//deafult action
  252. }
  253. }
  254. }
  255.  
  256. public static boolean isElfBetterAtAction(Elf elf,Elf elfCompare,int action,Game game)
  257. {
  258. double difference=elf.maxSpeed*1.5;
  259. if(action==ActionDefPortal) {//2
  260. Location loc=whereElfShouldBuildDefPortal(game);
  261. if(elf.distance(loc)+difference<elfCompare.distance(loc))
  262. return true;
  263. return false;
  264. }
  265. else if(action==ActionEnemyAttackingPortal) {//3
  266. //if elf needs help ++
  267. //&& other elf is far to another action's needed positions
  268. Portal port=closestEnemyPortalToOurCastle(game);
  269. if(elf.distance(port)+difference<elfCompare.distance(port))
  270. {
  271. return true;
  272. }
  273. return false;
  274. }
  275. else if(action==ActionEnemyElfAttacking) {//4
  276. Elf enemyElf=isThereEnemyElfAttacking(game);
  277. if(elf.distance(enemyElf)+difference<elfCompare.distance(enemyElf))
  278. {
  279. return true;
  280. }
  281. return false;
  282. }
  283. else if(action==ActionBuildAttackingPortals) {//if we dont have an attacking portal 5
  284. Location whereToBuildPort1=whereElfShouldBuildAttackPortal(elf,game);
  285. Location whereToBuildPort2=whereElfShouldBuildAttackPortal(elfCompare,game);
  286. if(elf.distance(whereToBuildPort1)+difference<elfCompare.distance(whereToBuildPort2))
  287. return true;
  288. return false;
  289. }
  290. else if(action==ActionDestroyEnemyBuildings) {//7
  291. Location l1=closestEnemyBuildingToMapObject(elf,game).getLocation();
  292. Location l2=closestEnemyBuildingToMapObject(elfCompare,game).getLocation();
  293. if(elf.distance(l1)+difference<elfCompare.distance(l2))
  294. return true;
  295. return false;
  296. }
  297. else if(action==ActionBuildManaFountains) {//9
  298. Location l1=whereToBuildManaFountain(game);
  299. if(elf.distance(l1)+difference<elfCompare.distance(l1))
  300. return true;
  301. return false;
  302. }
  303. else if(action==ActionDestroyEnemyManaFountains)
  304. {
  305. Location neededToDestroyEnemyMana=getEnemyBestManaFountain(game).getLocation();
  306. if(elf.distance(neededToDestroyEnemyMana)+difference<elfCompare.distance(neededToDestroyEnemyMana))
  307. return true;
  308. return false;
  309. }
  310. return false;
  311. }
  312.  
  313. public static Building closestEnemyBuildingToMapObject(MapObject object,Game game)
  314. {
  315. //System.out.println(object+" OBJECT?!?!?!?!?!");
  316. if(game.getEnemyPortals().length==0&&game.getEnemyManaFountains().length==0)
  317. return null;
  318. Building mBuilding=null;
  319. if(game.getEnemyManaFountains().length>0)
  320. mBuilding=game.getEnemyManaFountains()[0];
  321. else
  322. mBuilding=game.getEnemyPortals()[0];
  323. for(Portal portal:game.getEnemyPortals())
  324. {
  325. if(portal.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
  326. mBuilding=portal;
  327. }
  328. for(ManaFountain Fountain:game.getEnemyManaFountains())
  329. {
  330. if(Fountain.distance(object.getLocation())<mBuilding.distance(object.getLocation()))
  331. mBuilding=Fountain;
  332. }
  333. return mBuilding;
  334. }
  335. public static boolean areTheyCloseFountainsToElf(Elf elf,Game game)
  336. {
  337. for(ManaFountain mana:game.getEnemyManaFountains())
  338. {
  339. if(elf.inAttackRange(mana))
  340. {
  341. return true;
  342. }
  343. else if(elf.distance(mana)<=elf.maxSpeed*3)
  344. {
  345. return true;
  346. }
  347. else if(mana.distance(game.getMyCastle())<=mana.size*5)
  348. {
  349. return true;
  350. }
  351. }
  352. return false;
  353. }
  354. public static void tryToDestroyEnemyCloseFountain(Elf elf,Game game)
  355. {
  356. for(ManaFountain mana:game.getEnemyManaFountains())
  357. {
  358. if(elf.inAttackRange(mana))
  359. {
  360. elf.attack(mana);
  361. return;
  362. }
  363. else if(elf.distance(mana)<=elf.maxSpeed*3)
  364. {
  365. elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed+50,game));
  366. return;
  367. }
  368. else if(mana.distance(game.getMyCastle())<=mana.size*5)
  369. {
  370. elf.moveTo(safteyLoc(elf.getLocation(),mana.getLocation(),elf.maxSpeed+50,game));
  371. return;
  372. }
  373. }
  374. }
  375.  
  376. public static void buildDefPortal(Elf elf,Game game)
  377. {
  378. isElfTryingToBuildPortal=true;
  379. Location whereToBuildPort=whereElfShouldBuildDefPortal(game);
  380. if(elf.distance(whereToBuildPort)<=elf.maxSpeed*1.5 &&elf.canBuildPortal()&&!(elf.isBuilding)&&game.getMyMana()>=(game.portalCost))
  381. {
  382. //System.out.println("building def portal");
  383. elf.buildPortal();
  384. }
  385. else
  386. {
  387. //System.out.println("going to build def portal loc");
  388. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed+50,game));
  389. }
  390. }
  391. public static boolean isTherePortalNearEnemyCloseSpawnToCastle(Game game){
  392. int count=0;
  393. Elf CloseSpawiningElf=null;
  394. for(Elf enemyElf:game.getAllEnemyElves()){
  395. if(enemyElf.initialLocation.distance(game.getMyCastle())-game.getMyCastle().size-game.elfAttackRange<game.elfMaxSpeed*15){
  396. CloseSpawiningElf=enemyElf;
  397. }
  398. }
  399. if(CloseSpawiningElf==null)
  400. return false;
  401. for(Portal portal:game.getMyPortals()){
  402. if(portal.distance(CloseSpawiningElf.initialLocation)<game.iceTrollMaxSpeed+game.iceTrollAttackRange){
  403. count++;
  404. }
  405. }
  406. if(count==0)
  407. return true;
  408. return false;
  409. }
  410. public static void buildPortalToFuckEnemyElfWhenSpawned(Elf elf, Game game){
  411. Location PortalLocation=null;
  412. for(Elf enemyElf:game.getAllEnemyElves()){
  413. if(enemyElf.initialLocation.distance(game.getMyCastle())<game.elfMaxSpeed*10){
  414. PortalLocation=enemyElf.initialLocation;
  415. }
  416. }
  417. if(PortalLocation!=null){
  418. if(elf.distance(PortalLocation)<=elf.maxSpeed*1.5&&elf.canBuildPortal()&&game.getMyMana()>=game.manaFountainCost)
  419. {
  420. System.out.println("building spawn portal");
  421. elf.buildPortal();
  422. }
  423. else
  424. {
  425. System.out.println("going spawn portaln");
  426. if(elf.distance(PortalLocation)<=elf.maxSpeed*2)
  427. elf.moveTo(PortalLocation);
  428. else
  429. elf.moveTo(safteyLoc(elf.getLocation(),PortalLocation,elf.maxSpeed+50,game));
  430. }
  431. }
  432. }
  433.  
  434.  
  435. public static void destroyEnemysAttackingPortal(Elf elf,Game game)
  436. {
  437. Portal p=closestEnemyPortalToOurCastle(game);
  438. if(ClosestEnemyElfToOurCastle(game)!=null&&ClosestEnemyElfToOurCastle(game).distance(elf)<elf.distance(p)&&isThereEnemyElfAttacking(game)!=null)
  439. {
  440. GoingToEnemyAttackingElf(elf,game);
  441. //System.out.println("going to def cause enemy elf attacking");
  442. }
  443. //if can attack: Attack!
  444. else if(elf.inAttackRange(p)&&!(elf.isBuilding)&&isItSafeToAttackEnemyBuilding(elf,p,game)){
  445. elf.attack(p);
  446. }
  447. //if can't attack: move to portal
  448. else if(!(elf.isBuilding))
  449. {
  450. if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.canBuildPortal()&&!(elf.isBuilding)&&game.getMyMana()>(game.portalCost+10)&&NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game)<2)
  451. {
  452. //System.out.println("elf chose to build portal");
  453. elf.buildPortal();
  454. }
  455. else
  456. elf.moveTo(safteyLoc(elf.getLocation(),p.getLocation(),elf.maxSpeed+50,game));
  457. }
  458. }
  459. public static void tryToBuildAttackingPortal(Elf elf,Game game)
  460. {
  461. isElfTryingToBuildPortal=true;
  462. Location whereToBuildPort;
  463. int num=numOfEnoughClosePortalToEnemyCastle(game);
  464. if(num==0)
  465. whereToBuildPort=whereElfShouldBuildAttackPortal(elf,game);
  466. else
  467. whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
  468. //System.out.println(isItSafeToBuildPortal(elf,game)+" FOR FUCKS SAKE IT IS SAFE TO BUILD A GOD DARN PORTAL");
  469.  
  470. if(isItSmartToBuildPortal(elf,game)&&isItSafeToBuildPortal(elf,game)&&elf.distance(game.getEnemyCastle())<elf.distance(game.getMyCastle()) && elf.canBuildPortal()&&game.getMyMana()>game.portalCost)
  471. {
  472. elf.buildPortal();
  473. }
  474. else if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
  475. {
  476. if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
  477. {
  478. if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
  479. {
  480. elf.buildPortal();
  481. //System.out.println("elf is building a portal");
  482. }
  483. else
  484. {
  485. //System.out.println("elf is moving safetly to the loc");
  486. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  487. elf.moveTo(whereToBuildPort);
  488. else
  489. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed+50,game));
  490. }
  491. }
  492. else
  493. {
  494. whereToBuildPort=whereElfShouldBuildAttackPortalSides(elf,game);
  495. if(elf.distance(whereToBuildPort)<=elf.maxSpeed&&!elf.isBuilding)
  496. {
  497. if(isItSafeToBuildPortal(elf,game)&&isItSmartToBuildPortal(elf,game))
  498. {
  499. if(elf.canBuildPortal()&&game.getMyMana()>=game.portalCost)
  500. {
  501. elf.buildPortal();
  502. //System.out.println("elf is building a portal");
  503. }
  504. }
  505. else
  506. {
  507. //System.out.println("elf is moving safetly to the loc");
  508. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  509. elf.moveTo(whereToBuildPort);
  510. else
  511. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed+50,game));
  512. }
  513. }
  514. else
  515. {
  516. //System.out.println("elf is moving safetly to the loc");
  517. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed+50,game));
  518. }
  519.  
  520. }
  521. }
  522. else if(!elf.isBuilding)
  523. {
  524. //System.out.println("elf is moving to the port loc");
  525. if(elf.distance(whereToBuildPort)<elf.maxSpeed*3&&isSafeLoc(whereToBuildPort,game))
  526. elf.moveTo(whereToBuildPort);
  527. else
  528. elf.moveTo(safteyLoc(elf.getLocation(),whereToBuildPort,elf.maxSpeed+50,game));
  529. }
  530. }
  531. public static void destroyEnemysBuilding(Elf elf,Game game)
  532. {
  533. Building enemyBuilding=closestEnemyBuildingToMapObject(elf,game);
  534. if(isItSafeToAttackEnemyBuilding(elf,enemyBuilding,game)&&elf.inAttackRange(enemyBuilding))
  535. {
  536. //System.out.println("ATTACKING ENEMY PORTAL");
  537. elf.attack(enemyBuilding);
  538. }
  539. //if not in Attack Range: move there
  540. else
  541. {
  542. //System.out.println("MOVING SAFTLEY TO ENEMYS CLOSEST PORTAL");
  543. elf.moveTo(safteyLoc(elf.getLocation(),enemyBuilding.getLocation(),elf.maxSpeed+50,game));
  544. }
  545. }
  546. public static void tryToAttackEnemyCastle(Elf elf,Game game)
  547. {
  548. //if elf in Attack Range: attack enemy Casle
  549. if (elf.inAttackRange(game.getEnemyCastle())&&(isSafeLoc(elf.getLocation(),game)||game.getEnemyCastle().currentHealth<=elf.attackMultiplier*elf.currentHealth))
  550. {
  551. elf.attack(game.getEnemyCastle());
  552. //.out.println("elf " + elf + " attacks " + game.getEnemyCastle());
  553. }
  554. //go back safely to My casle
  555. else {
  556. //System.out.println("moving saftley back to Base");
  557. elf.moveTo(safteyLoc(elf.getLocation(),game.getEnemyCastle().getLocation(),elf.maxSpeed+50,game));
  558. ////System.out.println("elf " + elf + " moves to " + game.getEnemyCastle());
  559. }
  560. }
  561. public static Portal portalToDestroy(Game game){
  562. if(closestEnemyPortalToOurCastle(game)==null)
  563. return null;
  564. Portal OEnemyPortal=game.getEnemyPortals()[0];
  565. for(Portal EnemyPortal : game.getEnemyPortals()){
  566. if(EnemyPortal.distance(game.getMyCastle())<EnemyPortal.distance(game.getEnemyCastle())){
  567. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
  568. OEnemyPortal=EnemyPortal;
  569. }
  570. }
  571. }
  572. System.out.println(OEnemyPortal);
  573. if(OEnemyPortal.distance(game.getMyCastle())<OEnemyPortal.distance(game.getEnemyCastle()))
  574. return OEnemyPortal;
  575. else
  576. return null;
  577. }
  578.  
  579. public static int numberOfElvesNeededForAction(int action, Game game) {
  580. /*Actions: NOT BY PRIORITY!!!
  581. *0: Attack enemy elf
  582. *1: Build on the way portal (A lot of mana)
  583. *2: Build defense portal
  584. *3: Destroy enemys attacking portal
  585. *4: Going to enemy attacking elf
  586. *5: Build attacking portal --> (first side?)
  587. *6: Building good portal
  588. *7: Destroy enemy portals (Any enemy portal)
  589. *8: Attack enemy castle
  590. *9: Build mana fountains
  591. *10: Destroy enemy mana fountains
  592. *switch 1/2/3 with 4?
  593. *
  594. */
  595. if(action==ActiontryToAttackEnemyElf||action==ActiontryToBuildPortalCauseCan||action==ActionBuildAttackPortalWhenCan)
  596. {
  597. //System.out.println("CALLING A PRIVATE ACTION ");
  598. return 0;
  599. }
  600. else if(action==ActionDefPortal) {//2
  601. // if elf needs help ++
  602. // && other elf is far to another action's needed positions
  603. if(isThereDefPortals(game))return 0;
  604. else return 1;
  605. }
  606. else if(action==ActionEnemyAttackingPortal) {//3
  607. //if elf needs help ++
  608. //&& other elf is far to another action's needed positions
  609. int count=0;
  610. Portal EnemyPortal=closestEnemyPortalToOurCastle(game);
  611. if(EnemyPortal==null||isThereClosePortalToCastle(game)==false)
  612. return 0;
  613. for(Elf EnemyElf:game.getEnemyLivingElves()) {
  614. if(EnemyElf.distance(EnemyPortal)<EnemyElf.maxSpeed*5)//&&isObjectGoingForwardMe(EnemyElf,EnemyPortal,game)) {
  615. count++;
  616. }
  617. if(portalToDestroy(game)!=null)
  618. return 1;
  619. return count;
  620. }
  621. else if(action==ActionEnemyElfAttacking) {
  622. // WHEN ICE WALL IS READY CHANGE
  623. // if elf needs help 1++ (until elf doesn't need help)
  624. // && other elf is not attacking (building attack portal);
  625. int dangerousElves = 0;
  626. for(Elf enemyElf: game.getEnemyLivingElves()) {
  627. if((enemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=15) {
  628. dangerousElves++;
  629. }
  630. }
  631. return dangerousElves;
  632. }
  633. else if(action==ActionBuildAttackingPortals) {
  634. //if we dont have an attacking portal
  635. // MAYBE DO THIS boolean enemyCastleAlmostDead = false;
  636. //*** CHEN DOES THIS but if there are left elves we add them to attack
  637. //if there are no attacking portals summon one elf
  638. if((numOfEnoughClosePortalToEnemyCastle(game) == 0))return game.getAllMyElves().length;
  639. //if there is atting portal and there is not many elfs return 0
  640. else if(numOfEnoughClosePortalToEnemyCastle(game) == 1) {
  641. return 1;
  642. }
  643. else
  644. return 0;
  645. }
  646. else if(action==ActionDestroyEnemyBuildings) {
  647. return game.getEnemyManaFountains().length+game.getEnemyPortals().length;
  648. }
  649. else if(action==ActionBuildManaFountains){
  650. if(shouldIbuildManaFountain(game)==false)
  651. return 0;
  652. else
  653. return 1;
  654. }
  655. else if(action==ActionDestroyEnemyManaFountains)
  656. {
  657. if(game.getEnemyManaFountains().length>0)
  658. return 1;
  659. else
  660. return 0;
  661. }
  662. return 0;
  663. }
  664. public static ManaFountain getEnemyBestManaFountain(Game game)
  665. {
  666. if(game.getEnemyManaFountains().length==0)
  667. return null;
  668. ManaFountain min=game.getEnemyManaFountains()[0];
  669. for(ManaFountain fount:game.getEnemyManaFountains())
  670. {
  671. if(fount.distance(game.getMyCastle())<min.distance(game.getMyCastle()))
  672. min=fount;
  673. }
  674. return min;
  675. }
  676. public static void destroyEnemysFountains(Elf elf,Game game)
  677. {
  678. ManaFountain min=getEnemyBestManaFountain(game);
  679. if(elf.inAttackRange(min)&&isItSafeToAttackEnemyBuilding(elf,min,game))
  680. elf.attack(min);
  681. else
  682. {
  683. elf.moveTo(safteyLoc(elf.getLocation(),min.getLocation(),elf.maxSpeed+50,game));
  684. }
  685. }
  686.  
  687.  
  688.  
  689. public static void tryToBuildManaFountain(Elf elf, Game game) {
  690. Location manaFountainBuildLoc=whereToBuildManaFountain(game);
  691. System.out.println(elf.canBuildManaFountain());
  692. if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*1.5&&elf.canBuildManaFountain()&&game.getMyMana()>=game.manaFountainCost)
  693. {
  694. System.out.println("building mana fountain");
  695. elf.buildManaFountain();
  696. }
  697. else
  698. {
  699. System.out.println("going to build mana fountain");
  700. if(elf.distance(manaFountainBuildLoc)<=elf.maxSpeed*2)
  701. elf.moveTo(manaFountainBuildLoc);
  702. else
  703. elf.moveTo(safteyLoc(elf.getLocation(),manaFountainBuildLoc,elf.maxSpeed+50,game));
  704. }
  705. }
  706. public static boolean isElfBuildingAt(Location loc,Game game)
  707. {
  708. for(Elf elf:game.getMyLivingElves())
  709. {
  710. if(elf.distance(loc)<=elf.maxSpeed&&elf.isBuilding)
  711. return true;
  712. }
  713. return false;
  714. }
  715. public static boolean shouldIbuildManaFountain(Game game) {
  716. Location locationToBuildManaFountain=whereToBuildManaFountain(game);
  717. if(!game.canBuildManaFountainAt(locationToBuildManaFountain)||game.getMyManaFountains().length>game.getMyPortals().length+2){
  718. return false;
  719. }
  720. if(locationToBuildManaFountain!=null) {
  721. if((game.getMyself().manaPerTurn<=game.getEnemy().manaPerTurn||game.getMyself().manaPerTurn==0)) {
  722. return true;
  723. }
  724. else if(game.getMyself().manaPerTurn>game.getEnemy().manaPerTurn) {
  725. if(game.getMyPortals().length>game.getMyManaFountains().length&&game.getMyMana()>game.manaFountainCost*1.5) { //note to change
  726. return true;
  727. }
  728. }
  729. }
  730. return false;
  731. }
  732. public static Location whereToBuildManaFountain(Game game) {
  733. Location whereToBuildManaFountain=null;
  734. for(Portal portal:game.getMyPortals()) {
  735. if(portal.distance(game.getMyCastle().getLocation())<game.manaFountainSize*5+game.getMyCastle().size) {
  736. whereToBuildManaFountain=trigoLocation(portal.getLocation().row,portal.getLocation().col,game.getEnemyCastle().getLocation(),portal.size+game.manaFountainSize+20,180);
  737. if(game.canBuildManaFountainAt(whereToBuildManaFountain)||isElfBuildingAt(whereToBuildManaFountain,game)) {
  738. return whereToBuildManaFountain;
  739. }
  740. }
  741. }
  742. //If there are no portals:
  743. int[] degrees=new int[360];
  744. degrees[0]=180;
  745. int degreesCounter=1;
  746. Castle myCastle=game.getMyCastle();
  747. int startingRad=20+myCastle.size;
  748. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  749. {
  750. degrees[i]=degreesCounter;
  751. degrees[i+1]=degreesCounter*-1;
  752. degreesCounter++;
  753. }
  754. boolean found=false;
  755. double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
  756. Location locClose= game.getEnemyCastle().getLocation();
  757. for(int radius=startingRad;radius<MaxDist;radius+=game.manaFountainSize)
  758. {
  759. for(int i=0;i<degrees.length;i++)
  760. {
  761. Location loc=trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  762. if(game.canBuildManaFountainAt(loc)||isElfBuildingAt(loc,game))
  763. {
  764. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  765. }
  766. }
  767. }
  768. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
  769. }
  770.  
  771.  
  772. public static Elf closestEnemyElfToObject(MapObject object, Game game) {
  773. if(game.getEnemyLivingElves().length==0||object==null)
  774. return null;
  775. Elf mElf=game.getEnemyLivingElves()[0];
  776. for(Elf EnemyElf :game.getEnemyLivingElves())
  777. {
  778. if(EnemyElf.distance(object)+EnemyElf.maxSpeed*1.5<=mElf.distance(object))
  779. {
  780. mElf=EnemyElf;
  781. }
  782. }
  783. return mElf;
  784. }
  785.  
  786.  
  787.  
  788. public static Location whereElfShouldBuildDefPortal(Game game)
  789. {
  790. int[] degrees=new int[360];
  791. degrees[0]=0;
  792. int degreesCounter=1;
  793. Castle myCastle=game.getMyCastle();
  794. int startingRad=game.manaFountainSize+game.portalSize+myCastle.size;
  795. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  796. {
  797. degrees[i]=degreesCounter;
  798. degrees[i+1]=degreesCounter*-1;
  799. degreesCounter++;
  800. }
  801. boolean found=false;
  802. double MaxDist=myCastle.distance(game.getEnemyCastle())/2.5;
  803. Location locClose;
  804. if(EnemyThreatningPortal(game)!=null&&EnemyThreatningPortal(game).distance(game.getMyCastle())<=myCastle.distance(game.getEnemyCastle())/2)
  805. locClose= EnemyThreatningPortal(game).getLocation();
  806. else
  807. locClose=game.getEnemyCastle().getLocation();
  808. for(int radius=startingRad;radius<MaxDist;radius+=game.portalSize)
  809. {
  810. for(int i=0;i<degrees.length;i++)
  811. {
  812. if(game.canBuildPortalAt(trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i])))
  813. {
  814. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,radius,degrees[i]);
  815. }
  816. }
  817. }
  818. return trigoLocation(myCastle.getLocation().row,myCastle.getLocation().col,locClose,startingRad,0);
  819. }
  820. public static boolean isThereDefPortals(Game game)
  821. {
  822. int counter=0;
  823. for(Portal portal:game.getMyPortals())
  824. {
  825. if(portal.distance(game.getMyCastle())-game.castleSize-game.lavaGiantAttackRange<=game.lavaGiantMaxSpeed*5)
  826. counter++;
  827. }
  828. if(counter>1||counter==1&&(isEnemyElfAttackingMyCastle(game)||EnemyLavaAttackingMyCastle(game)>2))
  829. return true;
  830. return false;
  831. }
  832. public static boolean isItSmartToBuildPortal(Elf elf, Game game)
  833. {
  834. for(Elf enemyElf: game.getEnemyLivingElves())
  835. {
  836. if((enemyElf.distance(elf)-enemyElf.attackRange)/enemyElf.maxSpeed<=game.iceTrollSummoningDuration+game.portalBuildingDuration&&enemyElf.currentHealth>game.portalMaxHealth*enemyElf.attackMultiplier&&isObjectGoingForwardMe(enemyElf,elf,game))
  837. {
  838. return false;
  839. }
  840. if(enemyElf.distance(elf)<=enemyElf.maxSpeed*2+enemyElf.attackRange)
  841. return false;
  842. }
  843. for(Portal portal:game.getMyPortals())
  844. {
  845. if(portal.distance(elf)<=portal.size*4&&elf.distance(game.getEnemyCastle())>elf.distance(game.getMyCastle()))
  846. return false;
  847. }
  848. ////System.out.println("smart building portal");
  849. return true;
  850. }
  851. public static boolean isItSafeToBuildPortal(Elf elf,Game game)
  852. {
  853. int enemyCounter=0;
  854. for(Elf enemyElf: game.getEnemyLivingElves())
  855. {
  856. if(enemyElf.distance(elf)<=enemyElf.maxSpeed*3+enemyElf.attackRange)
  857. {
  858. enemyCounter+=elf.attackMultiplier;
  859. }
  860. }
  861. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  862. {
  863. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*3)
  864. {
  865. enemyCounter+=enemyIce.attackMultiplier;
  866. }
  867. }
  868. if(enemyCounter==0)
  869. return true;
  870. if(elf.currentHealth-enemyCounter*1.3>game.portalBuildingDuration)
  871. return true;
  872. return false;
  873. }
  874. public static boolean isItSafeToAttackEnemyBuilding(Elf elf,Building building,Game game)
  875. {
  876. int enemyCounter=0;
  877. for(Elf enemyElf: game.getEnemyLivingElves())
  878. {
  879. if(enemyElf.distance(elf)<=enemyElf.maxSpeed+enemyElf.attackRange)
  880. {
  881. enemyCounter+=elf.attackMultiplier;
  882. }
  883. }
  884. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  885. {
  886. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed)
  887. {
  888. enemyCounter+=enemyIce.attackMultiplier;
  889. }
  890. }
  891. if(enemyCounter==0)
  892. return true;
  893. if(elf.currentHealth-enemyCounter>building.currentHealth+2)
  894. return true;
  895. return false;
  896. }
  897. public static Location whereElfShouldBuildAttackPortalSides(Elf elf,Game game)
  898. {
  899. int[] degrees=new int[360];
  900. degrees[0]=180;
  901. int degreesCounter=179;
  902. Castle enemyCastle=game.getEnemyCastle();
  903. int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
  904. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  905. {
  906. degrees[i]=degreesCounter;
  907. degrees[i+1]=degreesCounter*-1;
  908. degreesCounter--;
  909. }
  910. boolean found=false;
  911. int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
  912. for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
  913. {
  914. for(int i=1;i+1<degrees.length;i+=2)
  915. {
  916. Location loc1=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i]);
  917. Location loc2=trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,game.getMyCastle().getLocation(),radius,degrees[i+1]);
  918. if(game.canBuildPortalAt(loc1)&&game.canBuildPortalAt(loc2))
  919. {
  920. if(elf.distance(loc1)<elf.distance(loc2))
  921. return loc1;
  922. else
  923. return loc2;
  924. }
  925. }
  926. }
  927. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
  928. }
  929. public static Location whereElfShouldBuildAttackPortal(Elf elf,Game game)
  930. {
  931. int[] degrees=new int[360];
  932. degrees[0]=0;
  933. int degreesCounter=1;
  934. Castle enemyCastle=game.getEnemyCastle();
  935. int startingRad=(int)(game.getMyCastle().distance(enemyCastle)/2.5);
  936. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  937. {
  938. degrees[i]=degreesCounter;
  939. degrees[i+1]=degreesCounter*-1;
  940. degreesCounter++;
  941. }
  942. boolean found=false;
  943. int MaxDist=game.getMyCastle().distance(enemyCastle)/2;
  944. for(int radius=startingRad;radius<=MaxDist;radius+=game.portalSize)
  945. {
  946. for(int i=0;i<degrees.length;i++)
  947. {
  948. if(game.canBuildPortalAt(trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i])))
  949. {
  950. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),radius,degrees[i]);
  951. }
  952. }
  953. }
  954. return trigoLocation(enemyCastle.getLocation().row,enemyCastle.getLocation().col,elf.getLocation(),startingRad,0);
  955. }
  956. public static Portal getClosestEnemyPortal(Elf elf,Game game)
  957. {
  958. if(game.getEnemyPortals().length==0)
  959. return null;
  960. Portal minP=game.getEnemyPortals()[0];
  961. for(Portal p : game.getEnemyPortals())
  962. {
  963. if(elf.distance(p)<elf.distance(minP))
  964. {
  965. minP=p;
  966. }
  967. }
  968. return minP;
  969. }
  970. public static Location whereFightWillHappen(Elf elf,Game game)
  971. {
  972. int xCenterOfFight=elf.getLocation().row;
  973. int yCenterOffFight=elf.getLocation().col;
  974. for(Elf allyElf : game.getMyLivingElves())
  975. {
  976. if(allyElf!=elf&&allyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
  977. {
  978. xCenterOfFight=(xCenterOfFight+allyElf.getLocation().row)/2;
  979. yCenterOffFight=(yCenterOffFight+allyElf.getLocation().col)/2;
  980. }
  981. }
  982. for(Elf enemyElf : game.getEnemyLivingElves())
  983. {
  984. if(enemyElf.distance(elf)<elf.maxSpeed*5+elf.attackRange)
  985. {
  986. xCenterOfFight=(xCenterOfFight+enemyElf.getLocation().row)/2;
  987. yCenterOffFight=(yCenterOffFight+enemyElf.getLocation().col)/2;
  988. }
  989. }
  990. Location retLoc=new Location(xCenterOfFight,yCenterOffFight);
  991. return retLoc;
  992. }
  993.  
  994. public static boolean isWorthAttacking(Elf elf,double agroLevel, Game game)
  995. {
  996. int allyElvesAround=1;
  997. int enemyElvesAround=0;
  998. int allyLivesAround=elf.currentHealth;
  999. int enemyLivesAround=0;
  1000. int isFightUrgent = 0;
  1001. Location fightLoc=whereFightWillHappen(elf,game);
  1002.  
  1003. if(fightLoc.distance(game.getMyCastle()) - game.getMyCastle().size <= game.elfMaxSpeed * 2 + game.elfAttackRange) isFightUrgent = 1;
  1004.  
  1005. if(elf.distance(fightLoc)>elf.maxSpeed*5+elf.attackRange)
  1006. return false;
  1007. for(Elf enemyElf : game.getEnemyLivingElves())
  1008. {
  1009. if(enemyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
  1010. {
  1011. enemyElvesAround++;
  1012. enemyLivesAround+=enemyElf.currentHealth;
  1013. }
  1014. }
  1015. for(Elf allyElf : game.getMyLivingElves())
  1016. {
  1017. if(allyElf!=elf)
  1018. {
  1019. if(allyElf.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
  1020. {
  1021. allyElvesAround++;
  1022. allyLivesAround+=allyElf.currentHealth;
  1023. }
  1024. }
  1025. }
  1026. if(allyLivesAround * (1 + (isFightUrgent * 0.4))<=enemyLivesAround)
  1027. return false;
  1028. for(IceTroll allyIce : game.getMyIceTrolls())
  1029. {
  1030. if(allyIce.distance(fightLoc)<=elf.maxSpeed*5+elf.attackRange)
  1031. {
  1032. if(allyIce.currentHealth-(allyIce.distance(fightLoc)/allyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth)
  1033. {
  1034. allyElvesAround++;
  1035. allyLivesAround+=allyIce.currentHealth;
  1036. }
  1037. }
  1038. }
  1039.  
  1040. for(IceTroll enemyIce : game.getEnemyIceTrolls())
  1041. {
  1042. if(enemyIce.currentHealth-(enemyIce.distance(fightLoc)/enemyIce.maxSpeed)*game.iceTrollSuffocationPerTurn>0.5*game.iceTrollMaxHealth||enemyIce.attackRange>=enemyIce.distance(fightLoc))
  1043. {
  1044. enemyElvesAround++;
  1045. enemyLivesAround+=enemyIce.currentHealth;
  1046. }
  1047. }
  1048. if(allyElvesAround>=enemyElvesAround&&allyLivesAround*allyElvesAround*game.elfAttackMultiplier * (1 + (isFightUrgent * 0.4)) >enemyLivesAround*enemyElvesAround*agroLevel)
  1049. {
  1050. ////System.out.println("WORTH ATTACKING!");
  1051. return true;
  1052. }
  1053. ////System.out.println("NOT WORTH ATTACKING!");
  1054. return false;
  1055. }
  1056. public static Elf isThereEnemyElfAttacking(Game game)
  1057. {
  1058. Elf minElf=null;
  1059. for(Elf enemyElf:game.getEnemyLivingElves())
  1060. {
  1061. if((enemyElf.distance(game.getMyCastle())-game.getMyCastle().size-enemyElf.attackRange)/enemyElf.maxSpeed<=15)
  1062. {
  1063. if(minElf==null)
  1064. minElf=enemyElf;
  1065. else
  1066. {
  1067. if(minElf.distance(game.getMyCastle())>enemyElf.distance(game.getMyCastle()))
  1068. minElf=enemyElf;
  1069. }
  1070. }
  1071. }
  1072. return minElf;
  1073. }
  1074. public static void GoingToEnemyAttackingElf(Elf elf,Game game) {
  1075. Elf enemyElf=isThereEnemyElfAttacking(game);
  1076. if(elf.inAttackRange(enemyElf)&&isWorthAttacking(elf,1.2,game))
  1077. elf.attack(enemyElf);
  1078. else
  1079. elf.moveTo(safteyLoc(elf.getLocation(),enemyElf.getLocation(),game.elfMaxSpeed,game));
  1080. }
  1081.  
  1082. public static Elf elfToFocus(Elf elf,Game game)
  1083. {
  1084. if(game.getEnemyLivingElves().length==0)
  1085. return null;
  1086. Elf elfToAttack=null;
  1087. Location fightLocation=whereFightWillHappen(elf,game);
  1088. for(Elf Eelf:game.getEnemyLivingElves())
  1089. {
  1090. if(elfToAttack==null)
  1091. {
  1092. if(Eelf.distance(fightLocation)<=Eelf.maxSpeed*3)
  1093. elfToAttack=Eelf;
  1094. }
  1095. else if(Eelf.currentHealth<=elfToAttack.currentHealth-2&&Eelf.distance(fightLocation)<=Eelf.maxSpeed*5&&Eelf.distance(fightLocation)-Eelf.maxSpeed*3<elfToAttack.distance(fightLocation))
  1096. elfToAttack=Eelf;
  1097. }
  1098. return elfToAttack;
  1099. }
  1100. public static boolean tryToAttackElf(Elf elf,Game game)
  1101. {
  1102. if(isWorthAttacking(elf,1.3,game)==false)
  1103. return false;
  1104. boolean attacked=false;
  1105. Elf enemyElf=elfToFocus(elf,game);
  1106. if(enemyElf==null)
  1107. return false;
  1108. if(elf.inAttackRange(enemyElf))
  1109. {
  1110. attacked=true;
  1111. }
  1112. //EXPLAIN!
  1113. else if(enemyElf.distance(elf)<game.elfMaxSpeed*5+elf.attackRange&&isEnemyElfAttackingOurElf(enemyElf,game)==true || enemyElf.distance(elf)<=game.elfMaxSpeed+game.elfAttackRange||(enemyElf.distance(elf)<=game.elfMaxSpeed*2+game.elfAttackRange&&isObjectGoingForwardMe(enemyElf,elf,game)))
  1114. {
  1115. if(!(elf.distance(game.getMyCastle())>elf.distance(game.getEnemyCastle())&&isThereEnemyElfAttacking(game)!=null))
  1116. attacked=true;
  1117. }
  1118. return attacked;
  1119. }
  1120.  
  1121. public static void attackEnemyElf(Elf elf,Game game) {
  1122. Elf enemyElf=elfToFocus(elf,game);
  1123. if(elf.inAttackRange(enemyElf))
  1124. {
  1125. //System.out.println("in Attack Range so attacking");
  1126. elf.attack(enemyElf);
  1127. }
  1128. else {
  1129. //System.out.println("moving to attack");
  1130. elf.moveTo(enemyElf);
  1131. }
  1132.  
  1133. }
  1134.  
  1135. public static boolean isEnemyElfAttackingOurElf(Elf enemyElf,Game game)
  1136. {
  1137. for(Elf elf : game.getMyLivingElves())
  1138. {
  1139. if(enemyElf.distance(elf)<=elf.maxSpeed+elf.attackRange)
  1140. return true;
  1141. }
  1142. return false;
  1143. }
  1144. public static boolean isThereClosePortalToCastle(Game game)
  1145. {
  1146. boolean isThereClosePortal=false;
  1147. for(Portal portal :game.getEnemyPortals())
  1148. {
  1149. if(portal.distance(game.getMyCastle())/portal.distance(game.getEnemyCastle())<=1)
  1150. {
  1151. isThereClosePortal=true;
  1152. }
  1153. }
  1154. return isThereClosePortal;
  1155. }
  1156. public static Portal closestEnemyPortalToOurCastle(Game game)
  1157. {
  1158. if(game.getEnemyPortals().length==0)
  1159. return null;
  1160. Portal mPortal=game.getEnemyPortals()[0];
  1161. for(Portal portal :game.getEnemyPortals())
  1162. {
  1163. if(portal.distance(game.getMyCastle())<=mPortal.distance(game.getMyCastle()))
  1164. {
  1165. mPortal=portal;
  1166. }
  1167. }
  1168. return mPortal;
  1169. }
  1170. public static int numOfEnoughClosePortalToEnemyCastle(Game game)
  1171. {
  1172. int counter=0;
  1173. for(Portal portal :game.getMyPortals())
  1174. {
  1175. if(portal.distance(game.getEnemyCastle())<=portal.distance(game.getMyCastle()))
  1176. {
  1177. counter++;
  1178. }
  1179. }
  1180. return counter;
  1181. }
  1182. public static boolean isSafeLocForSike(Location loc, Location preLoc,Game game)
  1183. {
  1184. int enemyCount=0;
  1185. for(Elf enemy: game.getEnemyLivingElves())//enemy saftey
  1186. {
  1187. double enemySpeed= enemy.maxSpeed;
  1188. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1189. // //System.out.println(enemySpeed+" id "+enemy.id);
  1190. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1191. {
  1192. int allycount=0;
  1193. for(Elf elf : game.getMyLivingElves())
  1194. {
  1195. if(elf.distance(loc)<=elf.attackRange*1.1)
  1196. allycount++;
  1197. }
  1198. enemyCount++;
  1199. //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1200. return false;
  1201. }
  1202. }
  1203. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1204. {
  1205. double enemySpeed= enemy.maxSpeed;
  1206. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1207. // //System.out.println(enemySpeed+" id "+enemy.id);
  1208. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1209. {
  1210. enemyCount++;
  1211. if(enemyCount>=1)
  1212. return false;
  1213. }
  1214. }
  1215. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1216. return false;
  1217. return true;
  1218. }
  1219. public static boolean isSafeLoc(Location loc,Game game)
  1220. {
  1221. int enemyCount=0;
  1222. /*for(Mothership mother: game.getAllMotherships())
  1223. {
  1224. if(mother.distance(loc)<game.pirateMaxSpeed)
  1225. return true;
  1226. }//WTF*/
  1227. for(Elf enemy: game.getEnemyLivingElves())//enemy saftey
  1228. {
  1229. double enemySpeed= enemy.maxSpeed;
  1230. Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
  1231. // //System.out.println(enemySpeed+" id "+enemy.id);
  1232. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
  1233. {
  1234. int allycount=0;
  1235. for(Elf elf : game.getMyLivingElves())
  1236. {
  1237. if(elf.distance(loc)<=elf.attackRange*1.1+enemy.maxSpeed)
  1238. allycount++;
  1239. }
  1240. enemyCount++;
  1241. if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1242. return false;
  1243. }
  1244. }
  1245. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1246. {
  1247. double enemySpeed= enemy.maxSpeed;
  1248. Location enemyNextLoc=enemy.getLocation().towards(loc,enemy.maxSpeed);
  1249. // //System.out.println(enemySpeed+" id "+enemy.id);
  1250. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1+enemy.maxSpeed)////////play with the numbers/////////
  1251. {
  1252. enemyCount++;
  1253. if(enemyCount>=1)
  1254. return false;
  1255. }
  1256. }
  1257. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1258. return false;
  1259. //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)
  1260. //{
  1261. // return false;
  1262. //}
  1263.  
  1264. //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)
  1265. // return false;
  1266. return true;
  1267. }
  1268. public static Location trigoLocation(int nearLocX, int nearLocY,Location diractionLoc,double radius,double angle){
  1269. double zavit=(nearLocY-diractionLoc.col+0.0)/(nearLocX+0.0-diractionLoc.row);
  1270. if(nearLocX+0.0-diractionLoc.row==0)
  1271. {
  1272. if(nearLocY>=diractionLoc.col)
  1273. zavit=90;
  1274. else
  1275. zavit=-90;
  1276. }
  1277. double zavitEnemyBase=(Math.atan(zavit)+(Math.PI/180.0)*angle+0.0);
  1278. if(nearLocX<diractionLoc.row)
  1279. {
  1280. zavitEnemyBase=Math.PI+zavitEnemyBase;
  1281. }
  1282. int xRet=0;
  1283. xRet= (int)(nearLocX-radius*Math.cos(zavitEnemyBase));
  1284. int yRet=0;
  1285. yRet= (int)(nearLocY-radius*Math.sin(zavitEnemyBase));
  1286.  
  1287. Location defRow1= new Location(xRet,yRet);
  1288. return defRow1;
  1289. }
  1290. public static int safteyRate(Location loc,Location preLoc,Game game)
  1291. {
  1292. int enemyCount=0;
  1293. for(Elf enemy: game.getEnemyLivingElves())//enemy saftey
  1294. {
  1295. double enemySpeed= enemy.maxSpeed;
  1296. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1297. // //System.out.println(enemySpeed+" id "+enemy.id);
  1298. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1299. {
  1300. int allycount=0;
  1301. for(Elf elf : game.getMyLivingElves())
  1302. {
  1303. if(elf.distance(loc)<=elf.attackRange*1.1)
  1304. allycount++;
  1305. }
  1306. enemyCount++;
  1307. //if(enemyCount>=2|| enemyCount==1 && enemy.currentHealth*1.5>=allycount*game.elfMaxHealth)
  1308. }
  1309. }
  1310. for(IceTroll enemy: game.getEnemyIceTrolls())//enemy saftey
  1311. {
  1312. double enemySpeed= enemy.maxSpeed;
  1313. Location enemyNextLoc=enemy.getLocation().towards(preLoc,enemy.maxSpeed);
  1314. // //System.out.println(enemySpeed+" id "+enemy.id);
  1315. if(enemyNextLoc.distance(loc)<=enemy.attackRange*1.1)////////play with the numbers/////////
  1316. {
  1317. enemyCount++;
  1318. }
  1319. }
  1320. if(loc.row<=0||loc.col<=0||loc.col>=game.cols||loc.row>=game.rows)
  1321. return game.getEnemyIceTrolls().length+game.getEnemyLivingElves().length;
  1322. return enemyCount;
  1323. }
  1324. public static Location safteyLoc(Location elf,Location diractionLoc,double radius2,Game game)
  1325. {
  1326. if(diractionLoc.row<=0||diractionLoc.col<=0||diractionLoc.col>=game.cols||diractionLoc.row>=game.rows)
  1327. return diractionLoc;
  1328. //radius=game.pirateMaxSpeed;
  1329. int[] degrees=new int[360];
  1330. degrees[0]=0;
  1331. int degreesCounter=1;
  1332. //radius=game.elfMaxSpeed;
  1333. for(int i=1;i<degrees.length-1;i+=2)//creating the degrees array
  1334. {
  1335. degrees[i]=degreesCounter;
  1336. degrees[i+1]=degreesCounter*-1;
  1337. degreesCounter++;
  1338. }
  1339. Location loc=trigoLocation(elf.row,elf.col,diractionLoc,game.elfMaxSpeed,degrees[0]);
  1340. int safteyRateInt=safteyRate(loc,elf,game);
  1341. for(int radius=game.elfMaxSpeed;radius<=game.elfMaxSpeed*2;radius+=50)
  1342. {
  1343. for(int i=0;i<degrees.length;i++)
  1344. {
  1345. Location locToCheck=trigoLocation(elf.row,elf.col,diractionLoc,radius,degrees[i]);
  1346. if(isSafeLocForSike(locToCheck,elf,game))
  1347. {
  1348. //if(isSafeLocForSike(trigoLocation(elf.row,elf.col,diractionLoc,radius+50,degrees[i]),elf,game))
  1349. //{
  1350. ////System.out.println(degrees[i]);
  1351. if(closestAllyPortalToLoc(elf,game)!=null&&Math.abs(degrees[i])>90&&elf.distance(closestAllyPortalToLoc(elf,game))>=game.elfMaxSpeed*1.5)
  1352. {
  1353. if(diractionLoc==closestAllyPortalToLoc(elf,game))
  1354. return locToCheck;
  1355. else
  1356. return safteyLoc(elf,closestAllyPortalToLoc(elf,game),game.elfMaxSpeed+50,game);
  1357. }
  1358. return locToCheck;
  1359. //}
  1360. }
  1361. int safteyRateCurrent=safteyRate(locToCheck,elf,game);
  1362. if(safteyRateInt>safteyRateCurrent&&safteyRateCurrent>=0)
  1363. {
  1364. loc=locToCheck;
  1365. }
  1366. }
  1367. }
  1368. //System.out.println("choses most safe loc");
  1369. return loc;
  1370. }
  1371. public static Location closestAllyPortalToLoc(Location loc,Game game)
  1372. {
  1373. if(game.getMyPortals().length==0)
  1374. return null;
  1375. Portal maxPort=game.getMyPortals()[0];
  1376. for(Portal portal:game.getMyPortals())
  1377. {
  1378. if(portal.distance(loc)<maxPort.distance(loc))
  1379. maxPort=portal;
  1380. }
  1381. return maxPort.getLocation();
  1382. }
  1383.  
  1384. ///////////////////////////////////////////////////////////////////////////////////////////
  1385. public static void IceOrFire(Portal portal, Game game){
  1386. //System.out.println();
  1387. //System.out.println(portal);
  1388. //System.out.println();
  1389. if(EnemyThreatningPortal(game)!=null)
  1390. enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
  1391. else
  1392. enemyAttackingLoc=null;
  1393. if(game.getEnemyCastle().currentHealth<10){
  1394. //System.out.println("Less than 10 life");
  1395. if(portal==closestPortalToEnemyCastle(game)&&portal.canSummonLavaGiant()){
  1396. portal.summonLavaGiant();
  1397. }
  1398. }
  1399. ////**1**\\\\
  1400. //if There is Enemy Elf Near Portal: summon Ice Troll
  1401. ////////////////////////////////PLAY! MAYBE ANOTHE FUNCTION WITH HARSHER CONDITIONS///////////////////////////////////
  1402. double numberOfIceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc, game);
  1403. if(IcePortal(game,portal))
  1404. {
  1405. System.out.println("number of ice to go: "+numberOfIceToGo);
  1406. }
  1407. ////System.out.println("number of ice needed for defense: "+NumberOfIceNeededForDefense(game));
  1408. if(isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)&&numberOfIceToGo<3) {
  1409. if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost) {
  1410. if(portal.canSummonIceTroll()){
  1411. //System.out.println("I summon ice close enemy elf: "+portal);
  1412. portal.summonIceTroll();
  1413. }
  1414. }
  1415. }
  1416. ////**3**\\\\
  1417. //if portal is a defense portal, and we have enough mana: summon ice Troll
  1418. else if(IcePortal(game,portal)&&numberOfIceToGo>1.5) {
  1419. //if there aren't enough ice trolls or portal is attacked: summon ice trolls
  1420. if(game.getMyMana()>=handleMana(portal,game)+game.iceTrollCost||(numberOfIceToGo>=3&&game.getMyMana()>=game.iceTrollCost)){//||(isEnemyElfAttackingMyCastle(game) && game.getMyMana() >= game.iceTrollCost)) {
  1421. if(portal.canSummonIceTroll()){
  1422. //System.out.println("I summon ice enemy attacking my castle: "+portal);
  1423. portal.summonIceTroll();
  1424. }
  1425. }
  1426. }
  1427. /*else if(IcePortal(game,portal)&&numberOfIceToGo>=1&&game.getMyMana()>=game.iceTrollCost*3+handleMana(portal,game))
  1428. if(portal.canSummonIceTroll())
  1429. portal.summonIceTroll();
  1430. */
  1431. else if(isPortalNeededToHelpAllyElf(portal,game)&&allyIceAroundLoc(enemyAttackingLoc,game)>NumberOfIceNeededForDefense(game)&&isItWorthSummoningFire(closestPortalToEnemyCastle(game),game)<15){
  1432. if(game.getMyMana()>=handleMana(portal,game)+game.lavaGiantCost)
  1433. if(portal.canSummonLavaGiant()){
  1434. //System.out.println("Sumonned lava to help elf: " +portal);
  1435. portal.summonLavaGiant();
  1436. }
  1437. }
  1438. ///**2**\\\\
  1439. //if portal is an attack portal and we have enough mana: summon lava Giant
  1440. else if(FirePortal(game, portal)&&numberOfIceToGo<1.5)
  1441. {
  1442. // //System.out.println("Fire: "+ portal);
  1443. if(game.getMyMana() >= handleMana(portal,game)+game.lavaGiantCost)
  1444. {
  1445. if(portal.canSummonLavaGiant()){
  1446. //System.out.println("I summon fire to attack: "+ portal);
  1447. portal.summonLavaGiant();
  1448. }
  1449. }
  1450. }
  1451. }
  1452.  
  1453. public static boolean isObjectGoingForwardMe(Elf enemy, GameObject obj,Game game)
  1454. {
  1455. if(!enemy.isAlive())
  1456. return false;
  1457. boolean returned=false;
  1458. for(int i=0;i<game.getAllEnemyElves().length;i++)
  1459. {
  1460. if(game.getAllEnemyElves()[i]==enemy)
  1461. {
  1462. if(enemyElvesLocations[i].getLocation().row==enemy.getLocation().row&&enemyElvesLocations[i].getLocation().col==enemy.getLocation().col)
  1463. returned= true;
  1464. if(enemyElvesLocations[i].distance(obj)>=enemy.distance(obj))
  1465. returned= true;
  1466. else
  1467. returned= false;
  1468. }
  1469. }
  1470. return returned;
  1471. }
  1472. public static boolean isItWorthSummoningIceToDefPortal(Portal portal,Elf enemy,Game game)
  1473. {
  1474. if(enemy.distance(portal)-portal.size-enemy.attackRange<=enemy.maxSpeed*2)
  1475. return true;
  1476. for(IceTroll ice:game.getMyIceTrolls())
  1477. {
  1478. if(ice.distance(portal)<portal.distance(enemy)&&ice.distance(enemy)<portal.distance(enemy)&&ice.currentHealth>=game.iceTrollSummoningDuration)
  1479. {
  1480. //System.out.println(portal+" it is not worth summoning ice to def this portal");
  1481. return false;
  1482. }
  1483. }
  1484. return true;
  1485. }
  1486. public static boolean isThereEnemyElfNearPortal(Portal portal, Game game) {
  1487. for(Elf enemyElf: game.getEnemyLivingElves()) {
  1488. double distance=portal.distance(enemyElf)-portal.size-enemyElf.attackRange;
  1489. double timeForEnemyElfToGetToPortal= distance/enemyElf.maxSpeed;
  1490. double distancePortFromSpot=portal.size+enemyElf.attackRange-game.iceTrollAttackRange;
  1491. int mana=game.defaultManaPerTurn;
  1492. if(mana==0)
  1493. mana=5;
  1494. double turnsToStartSummoningIce=(-game.getMyMana()+(game.iceTrollCost))/mana;
  1495. if(game.getMyMana()>=game.iceTrollCost)
  1496. turnsToStartSummoningIce=0;
  1497. double timeForIceToGetToPortal=turnsToStartSummoningIce+game.iceTrollSummoningDuration+distancePortFromSpot/game.iceTrollMaxSpeed;
  1498. if(timeForIceToGetToPortal+2>=timeForEnemyElfToGetToPortal&&isObjectGoingForwardMe(enemyElf,portal,game)&&isItWorthSummoningIceToDefPortal(portal,enemyElf,game))
  1499. {
  1500. double minDist=portal.distance(enemyElf);
  1501. boolean flag=true;
  1502. for(Portal port:game.getMyPortals())
  1503. {
  1504. if(port!=portal&&port.distance(enemyElf)<minDist&&isObjectGoingForwardMe(enemyElf,port,game))
  1505. flag=false;
  1506. }
  1507. if(flag==true)
  1508. {
  1509. //System.out.println("CLOSE ELF");
  1510. return true;
  1511. }
  1512. }
  1513. }
  1514. return false;
  1515. }
  1516.  
  1517. public static int getIndexByPortal(Portal portal, Game game) {
  1518. int index=0;
  1519. for(int i=0;i<game.getMyPortals().length;i++) {
  1520. if(game.getMyPortals()[i]==portal)
  1521. index=i;
  1522. }
  1523. return index;
  1524. }
  1525.  
  1526. public static int allyIceAroundLoc(Location location, Game game) {
  1527. if(EnemyThreatningPortal(game)==null){
  1528. return game.getMyIceTrolls().length;
  1529. }
  1530. int allyIceAroundLoc=0;
  1531. for(IceTroll Ice:game.getMyIceTrolls()) {
  1532. ////System.out.println("ObjectdistanceFromALine: "+ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle()));
  1533. if(Ice.distance(location)<Ice.maxSpeed*4/*&&ObjectdistanceFromALine(Ice, EnemyThreatningPortal(game),game.getMyCastle())<game.iceTrollMaxSpeed*4*/&&Ice.currentHealth>3) {
  1534. allyIceAroundLoc++;
  1535. }
  1536. }
  1537. return allyIceAroundLoc;
  1538. }
  1539.  
  1540. public static double ObjectdistanceFromALine(GameObject object, GameObject objectOnLine1 , GameObject objectOnLine2){
  1541. //y=mx+b
  1542. if(object==null||objectOnLine1==null||objectOnLine2==null)
  1543. return 0;
  1544. double m=getSlopeByTwoMapObjects(objectOnLine1,objectOnLine2);
  1545. double b=objectOnLine1.getLocation().col-m*objectOnLine1.getLocation().row;
  1546.  
  1547. double distance=Math.abs(-m*object.getLocation().row+object.getLocation().col-b)/Math.sqrt(m*m+1);
  1548.  
  1549. return distance;
  1550.  
  1551. }
  1552. public static boolean isEnemyElfAttackingMyCastle(Game game){
  1553. for(Elf elf : game.getEnemyLivingElves())
  1554. {
  1555. if(elf.distance(game.getMyCastle())<=elf.attackRange+elf.maxSpeed*5+game.getMyCastle().size)
  1556. {
  1557. //System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  1558. //System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  1559. //System.out.println("BASE IS BEING ATTACKED-- Enemy Elf");
  1560. return true;
  1561. }
  1562. }
  1563.  
  1564. return false;
  1565. }
  1566.  
  1567. public static int EnemyLavaAttackingMyCastle(Game game) {
  1568.  
  1569. int lavaCounter=0;
  1570. if(EnemyThreatningPortal(game)!=null)
  1571. {
  1572. enemyAttackingLoc=game.getMyCastle().getLocation().towards(EnemyThreatningPortal(game),game.lavaGiantAttackRange+game.getMyCastle().size);
  1573.  
  1574. for(LavaGiant lava:game.getEnemyLavaGiants())
  1575. {
  1576. if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
  1577. lavaCounter++;
  1578. }
  1579. }
  1580. else
  1581. {
  1582. for(LavaGiant lava:game.getEnemyLavaGiants())
  1583. {
  1584. if(lava.currentHealth-(lava.distance(game.getMyCastle())-game.lavaGiantAttackRange-game.getMyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>=3)
  1585. lavaCounter++;
  1586. }
  1587. }
  1588. //System.out.println("Lava Counter: "+lavaCounter);
  1589. if(lavaCounter>2) {
  1590. System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  1591. //System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  1592. //System.out.println("BASE IS BEING ATTACKED-- Enemy Lava");
  1593. }
  1594. return lavaCounter;
  1595. }
  1596.  
  1597. //Fire\\
  1598. public static boolean FirePortal(Game game, Portal portal) {
  1599. if(portal==closestPortalToEnemyCastle(game)&&isItWorthSummoningFire(portal,game)>=10&&NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc,game)<3||isItWorthSummoningFire(portal,game)>15&&!IcePortal(game, portal)) {
  1600. if(portal.canSummonLavaGiant()) {
  1601. return true;
  1602. }
  1603. }
  1604. return false;
  1605. }
  1606. public static Portal closestPortalToEnemyCastle(Game game){
  1607. if(game.getMyPortals().length==0)
  1608. return null;
  1609. Portal mPortal=game.getMyPortals()[0];
  1610. for(Portal portal :game.getMyPortals())
  1611. {
  1612. if(portal.distance(game.getEnemyCastle())<=mPortal.distance(game.getEnemyCastle())&&portal.canSummonLavaGiant())
  1613. {
  1614. mPortal=portal;
  1615. }
  1616. }
  1617. return mPortal;
  1618. }
  1619.  
  1620.  
  1621. public static double isItWorthSummoningFire(Portal portal, Game game){
  1622. return game.lavaGiantMaxHealth-(portal.distance(game.getEnemyCastle())-game.lavaGiantAttackRange-game.getEnemyCastle().size)/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn-numOfEnemyIceDefenders(portal,game);
  1623. }
  1624.  
  1625. public static int numOfEnemyIceDefenders(Portal portal, Game game){
  1626. Location OurAttackingLoc=game.getEnemyCastle().getLocation().towards(portal,game.lavaGiantAttackRange+game.getEnemyCastle().size);
  1627. int counter=0;
  1628. for(IceTroll ice: game.getEnemyIceTrolls())
  1629. {
  1630. if(ice.distance(OurAttackingLoc)<game.iceTrollMaxSpeed*5+game.iceTrollAttackRange)
  1631. counter++;
  1632. }
  1633. return counter;
  1634. }
  1635.  
  1636.  
  1637. //Ice\\
  1638. public static boolean IcePortal(Game game, Portal portal) {
  1639. if(IsThereAnEnemyAttackingElf(10,game)!=null) {
  1640. if(portal==ClosestPortalToEnemyElf(game,ClosestEnemyElfToOurCastle(game))&&isObjectGoingForwardMe(ClosestEnemyElfToOurCastle(game),game.getMyCastle(),game)) {
  1641. if(portal.canSummonIceTroll()){
  1642. //System.out.println("This portal is ice for enemy attacking elf: "+portal);
  1643. return true;
  1644. }
  1645. }
  1646. }
  1647. else if(allyIceAroundLoc(enemyAttackingLoc,game)<NumberOfIceNeededForDefense(game)){
  1648. if(portal==PortalToDefenseFire(game))
  1649. if(portal.canSummonIceTroll()){
  1650. //System.out.println("This portal is ice for enemy attacking Lava: "+portal);
  1651. return true;
  1652. }
  1653. }
  1654. return false;
  1655. }
  1656. public static Portal[] getMyDefensePortals(Game game) {
  1657. int DefensePortals=0;
  1658. for(Portal portal : game.getMyPortals()) {
  1659. if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle())) {
  1660. DefensePortals++;
  1661. }
  1662. }
  1663. Portal[] DefensePortals1=new Portal[DefensePortals];
  1664. for(int i=0;i<DefensePortals1.length;i++) {
  1665. for(Portal portal : game.getMyPortals()) {
  1666. if(portal.distance(game.getMyCastle()) < portal.distance(game.getEnemyCastle()))
  1667. DefensePortals1[i]=portal;
  1668. }
  1669. }
  1670. return DefensePortals1;
  1671. }
  1672. public static Elf IsThereAnEnemyAttackingElf(int turns, Game game) {
  1673. if(game.getEnemyLivingElves().length==0)
  1674. return null;
  1675. Elf elf=game.getEnemyLivingElves()[0];
  1676. for(Elf EnemyElf: game.getEnemyLivingElves()) {
  1677. if(EnemyElf.maxSpeed*turns>EnemyElf.distance(game.getMyCastle())) {
  1678. if(EnemyElf.distance(game.getMyCastle())<elf.distance(game.getMyCastle())) {
  1679. elf=EnemyElf;
  1680. }
  1681. }
  1682. }
  1683. if(elf.maxSpeed*turns>elf.distance(game.getMyCastle()))
  1684. return elf;
  1685. return null;
  1686. }
  1687. public static Portal PortalToDefenseFire(Game game) {
  1688. if(game.getMyPortals().length==0||game.getEnemyPortals().length==0)
  1689. return null;
  1690. Portal SPortal=game.getMyPortals()[0];
  1691. for(Portal portal : game.getMyPortals()) {
  1692. 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)))
  1693. SPortal=portal;
  1694. }
  1695. // //System.out.println("This is the portal to defense from: "+EnemyThreatningPortal(game));
  1696. // //System.out.println("This is the Portal to defense Fire: "+SPortal);
  1697. return SPortal;
  1698. }
  1699. public static Elf ClosestEnemyElfToOurCastle(Game game) {
  1700. if(game.getEnemyLivingElves().length==0)
  1701. return null;
  1702. Elf OriginalEnemyElf=game.getEnemyLivingElves()[0];
  1703. for(Elf EnemyElf:game.getEnemyLivingElves()) {
  1704. if(EnemyElf.distance(game.getMyCastle())<=OriginalEnemyElf.distance(game.getMyCastle())) {
  1705. OriginalEnemyElf=EnemyElf;
  1706. }
  1707. }
  1708. return OriginalEnemyElf;
  1709. }
  1710. public static Portal ClosestPortalToEnemyElf(Game game, Elf elf) {
  1711. if(game.getMyPortals().length==0)
  1712. return null;
  1713. Portal OriginalPortal=game.getMyPortals()[0];
  1714. for(Portal portal:game.getMyPortals()) {
  1715. if(portal.distance(elf)<=OriginalPortal.distance(elf))
  1716. OriginalPortal=portal;
  1717. }
  1718. return OriginalPortal;
  1719. }
  1720.  
  1721. public static boolean isThereCloseEnemyPortalToCastle(Game game){
  1722. boolean isThereClosePortal=false;
  1723. for(Portal portal :game.getEnemyPortals())
  1724. {
  1725. if(game.lavaGiantMaxHealth-(portal.distance(game.getMyCastle())/game.lavaGiantMaxSpeed)/game.lavaGiantSuffocationPerTurn-allyIceAroundLoc(enemyAttackingLoc,game)>=3)
  1726. {
  1727. isThereClosePortal=true;
  1728. }
  1729. }
  1730. return isThereClosePortal;
  1731. }
  1732.  
  1733. public static Portal EnemyThreatningPortal(Game game) {
  1734. if(closestEnemyPortalToOurCastle(game)==null)
  1735. return null;
  1736. Portal OEnemyPortal=game.getEnemyPortals()[0];
  1737. for(Portal EnemyPortal : game.getEnemyPortals()){
  1738. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)) {
  1739. OEnemyPortal=EnemyPortal;
  1740. if(getEnemyLavaGiantsByPortal(game,EnemyPortal)>getEnemyLavaGiantsByPortal(game,OEnemyPortal)){
  1741.  
  1742. }
  1743. }
  1744. }
  1745.  
  1746. if(getEnemyLavaGiantsByPortal(game,OEnemyPortal)==0)
  1747. return closestEnemyPortalToOurCastle(game);
  1748. ////System.out.println("EnemyThreatningPortal: "+OEnemyPortal+" Lava for this portal: "+getEnemyLavaGiantsByPortal(game,OEnemyPortal));
  1749. return OEnemyPortal;
  1750. }
  1751.  
  1752. public static double getSlopeByTwoMapObjects(MapObject FistObject,MapObject SecondObject) {
  1753. double Slope;
  1754. double DeltaY;
  1755. double DeltaX;
  1756. DeltaY=FistObject.getLocation().col-SecondObject.getLocation().col;
  1757. DeltaX=FistObject.getLocation().row-SecondObject.getLocation().row;
  1758. Slope=DeltaY/DeltaX;
  1759. return Slope;
  1760. }
  1761.  
  1762. public static int getEnemyLavaGiantsByPortal(Game game, Portal portal) {
  1763. if(portal==null)
  1764. return 0;
  1765. double slopePC=getSlopeByTwoMapObjects(portal,game.getMyCastle());
  1766. int lavaGiantsOnSlope=0;
  1767. for(LavaGiant LavaGiant : game.getEnemyLavaGiants()) {
  1768. if(Math.abs(getSlopeByTwoMapObjects(LavaGiant,game.getMyCastle())-slopePC)<0.5) {
  1769. lavaGiantsOnSlope++;
  1770. }
  1771. }
  1772. return lavaGiantsOnSlope;
  1773. }
  1774.  
  1775. public static boolean isElfInNeedOfFire(Elf elf,Portal portal,Game game)
  1776. {
  1777. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  1778. {
  1779. if(enemyIce.distance(elf)<=enemyIce.attackRange+enemyIce.maxSpeed*2&&enemyIce.currentHealth/enemyIce.suffocationPerTurn>=portal.distance(elf)/game.lavaGiantMaxSpeed)
  1780. {
  1781. return true;
  1782. }
  1783. }
  1784. return false;
  1785. }
  1786.  
  1787. public static boolean isPortalNeededToHelpAllyElf(Portal portal,Game game)
  1788. {
  1789. if(game.getMyLivingElves().length==0)
  1790. return false;
  1791. Elf elfToHelp=null;
  1792. for(Elf elf:game.getMyLivingElves())
  1793. {
  1794. if(isElfInNeedOfFire(elf,portal,game)==true)
  1795. {
  1796. elfToHelp=elf;
  1797. }
  1798. }
  1799. if(elfToHelp==null)
  1800. {
  1801. return false;
  1802. }
  1803. IceTroll iceToDef=null;
  1804. for(IceTroll enemyIce: game.getEnemyIceTrolls())
  1805. {
  1806. if(enemyIce.distance(elfToHelp)<=enemyIce.attackRange+enemyIce.maxSpeed*2&&enemyIce.currentHealth/enemyIce.suffocationPerTurn>=portal.distance(elfToHelp)/game.lavaGiantMaxSpeed+3)
  1807. {
  1808. iceToDef=enemyIce;
  1809. }
  1810. }
  1811. if(iceToDef==null)
  1812. return false;
  1813. Portal bestP=null;
  1814. double minDist=game.rows*game.rows;//biggy
  1815. for(Portal myPort:game.getMyPortals())
  1816. {
  1817. if(myPort.distance(game.getEnemyCastle())>elfToHelp.distance(game.getEnemyCastle()))
  1818. {
  1819. //double elfDistLineFromIce=ObjectdistanceFromALine(elfToHelp,iceToDef,);
  1820. double mMyPortCastle=getSlopeByTwoMapObjects(myPort,game.getEnemyCastle());
  1821. double mElfCastle=getSlopeByTwoMapObjects(elfToHelp,game.getEnemyCastle());
  1822. mMyPortCastle=Math.atan(mMyPortCastle);
  1823. mElfCastle=Math.atan(mMyPortCastle);
  1824. if(Math.abs(mMyPortCastle-mElfCastle)<=Math.PI/6 && myPort.distance(elfToHelp)<minDist && myPort.distance(elfToHelp)/game.lavaGiantMaxSpeed+game.lavaGiantSummoningDuration<elfToHelp.currentHealth)
  1825. {
  1826. minDist=myPort.distance(elfToHelp);
  1827. bestP=myPort;
  1828. }
  1829. }
  1830. }
  1831. if(bestP!=null&&bestP==portal)
  1832. {
  1833. //System.out.println(bestP+" "+elfToHelp+" "+iceToDef+" this portal SHOULD SUMMON FIRE TO HELP ELF");
  1834. return true;
  1835. }
  1836. return false;
  1837. }
  1838.  
  1839. public static boolean AttackingPortalsSummonigFire(Portal port,Game game){
  1840. int countSuperAttackingPortals=0;
  1841. boolean flag=false;
  1842. for(Portal portal:game.getMyPortals()){
  1843. if(portal.distance(game.getEnemyCastle())<=game.lavaGiantMaxSpeed*8+game.lavaGiantAttackRange+game.getEnemyCastle().size)
  1844. {
  1845. countSuperAttackingPortals++;
  1846. if(portal==port)
  1847. flag=true;
  1848. }
  1849. }
  1850. if(countSuperAttackingPortals>1&&flag==true)
  1851. return true;
  1852. else
  1853. return false;
  1854. }
  1855.  
  1856. public static double NumberOfIceNeededForDefense(Game game){
  1857. if(EnemyThreatningPortal(game)!=null){
  1858. double FinalLavaLife;
  1859. double NumberOfIceNeededForDefense=0;
  1860. if(game.getEnemyLavaGiants().length==0)
  1861. return 0;
  1862. LavaGiant maxLava=game.getEnemyLavaGiants()[0];
  1863. for(LavaGiant lava:game.getEnemyLavaGiants())
  1864. {
  1865. if(lava.distance(game.getMyCastle())>maxLava.distance(game.getMyCastle()))
  1866. maxLava=lava;
  1867. }
  1868. FinalLavaLife=maxLava.currentHealth-(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange)))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn;
  1869. //System.out.println(FinalLavaLife+" "+maxLava+" life that maxLava will have when close to my castle "+maxLava.currentHealth);
  1870. NumberOfIceNeededForDefense=(FinalLavaLife*0.8)/3;
  1871. //if(maxLava.distance(game.getMyCastle().getLocation().towards(maxLava,game.castleSize+maxLava.attackRange))/game.lavaGiantMaxSpeed/game.lavaGiantSuffocationPerTurn>game.iceTrollSummoningDuration*NumberOfIceNeededForDefense)
  1872. // return 0;
  1873. //if(game.getEnemyLavaGiants().length<3)
  1874. // return EnemyLavaAttackingMyCastle(game);
  1875. return NumberOfIceNeededForDefense;
  1876. }
  1877. return 0;
  1878. }
  1879.  
  1880. public static int getPortalImportance(Portal portal, Game game) {
  1881. int importance;
  1882. if(isThereEnemyElfNearPortal(portal,game)&&!AttackingPortalsSummonigFire(portal,game)) {
  1883. importance=4;
  1884. }
  1885. else if(IcePortal(game,portal)) {
  1886. importance=3;
  1887. }
  1888. else if(isPortalNeededToHelpAllyElf(portal,game)) {
  1889. importance=2;
  1890. }
  1891. else if(FirePortal(game, portal)) {
  1892. importance=1;
  1893. }
  1894. else
  1895. importance=0;
  1896. return importance;
  1897. }
  1898.  
  1899. public static int handleMana(Portal portal, Game game) {
  1900. boolean EnemyElfNearPortal=false;
  1901. for(Portal APortal: game.getMyPortals()){
  1902. if(isThereEnemyElfNearPortal(APortal,game)&&!AttackingPortalsSummonigFire(APortal,game))
  1903. EnemyElfNearPortal=true;
  1904.  
  1905. }
  1906. double iceToGo=NumberOfIceNeededForDefense(game)-allyIceAroundLoc(enemyAttackingLoc,game);
  1907. int ManaSave = 0;
  1908. int PortalImportance=getPortalImportance(portal,game);
  1909. for(int i=getIndexByPortal(portal,game);i<game.getMyPortals().length;i++) {
  1910. if(PortalImportance<getPortalImportance(game.getMyPortals()[i],game)) {
  1911. if(getPortalImportance(game.getMyPortals()[i],game)==4){
  1912. //System.out.println("Saving for Close elf");
  1913. ManaSave+=game.iceTrollCost;
  1914. }
  1915. else if(getPortalImportance(game.getMyPortals()[i],game)==3){
  1916. //System.out.println("Saving for defense");
  1917. ManaSave+=game.iceTrollCost;
  1918. }
  1919. else if(getPortalImportance(game.getMyPortals()[i],game)==2){
  1920. //System.out.println("Saving for Lava help");
  1921. ManaSave+=game.lavaGiantCost;
  1922. }
  1923. else if(getPortalImportance(game.getMyPortals()[i],game)==1){
  1924. //System.out.println("Saving mana to attack");
  1925. ManaSave+=game.lavaGiantCost;
  1926. }
  1927. else
  1928. ManaSave+=0;
  1929. }
  1930. }
  1931. if(shouldIbuildManaFountain(game)&&!EnemyElfNearPortal&&(iceToGo<1.5||game.getMyself().manaPerTurn==0)){
  1932. System.out.println("Saving for mana fountain");
  1933. ManaSave+=game.manaFountainCost;
  1934. }
  1935. if(EnemyElfNearPortal&&ManaSave<game.iceTrollCost&&PortalImportance<3){
  1936. //System.out.println("Saving for near elf");
  1937. ManaSave+=game.iceTrollCost;
  1938. }
  1939. if(!isThereDefPortals(game)&&isElfTryingToBuildPortal){
  1940. //System.out.println("Saving for defense portal");
  1941. ManaSave+=game.portalCost;
  1942. }
  1943. if(isElfTryingToBuildPortal&&iceToGo<2.1) {
  1944. //System.out.println("Saving for attack portal");
  1945. ManaSave+=game.portalCost-game.getMyself().manaPerTurn;
  1946. }
  1947. System.out.println("ManaSave= "+ManaSave+" Portal: "+portal);
  1948. return ManaSave;
  1949. }
  1950.  
  1951. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement