Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. @Override
  2. public void update(AIPlayerConquer entity) {
  3. //this is the hierarchy of action priorities
  4. //when idle, this will be called to search for what state to select next
  5.  
  6. //shoot ogres first because they will destroy walls
  7. entity.findPersons();
  8. if (entity.shouldAttackPersons()) {
  9. entity.stateMachine.changeState(SHOOT_PERSONS);
  10. }
  11.  
  12. //shoot if the opponent is aggressive or controls a lot of the map
  13. else if (entity.isOpponentAggressive() ||
  14. entity.shouldAttackBasedOnOpponentPercentage() ||
  15. entity.shouldAttackBasedOnPercentOwned()) {
  16.  
  17. entity.stateMachine.changeState(SHOOT);
  18.  
  19. //otherwise try to build
  20. } else {
  21. entity.stateMachine.changeState(BUILD);
  22. }
  23. }
  24.  
  25. public enum AIPlayerConquerState implements State<AIPlayerConquer> {
  26. IDLE,
  27.  
  28. SHOOT {
  29. @Override
  30. public void enter(AIPlayerConquer entity) {
  31.  
  32. entity.findOpponentWallTiles();
  33.  
  34. if (!entity.doesOpponentHaveWalls()) {
  35. entity.stateMachine.changeState(BUILD);
  36. } else {
  37. entity.angryAsEnemy();
  38. entity.tryToShootWalls();
  39. }
  40.  
  41. entity.stateMachine.changeState(IDLE);
  42. }
  43. },
  44.  
  45. SHOOT_PERSONS {
  46. @Override
  47. public void enter(AIPlayerConquer entity) {
  48.  
  49. entity.findPersons();
  50.  
  51. if (!entity.shouldAttackPersons()) {
  52. entity.stateMachine.changeState(BUILD);
  53. } else {
  54. entity.tryToShootPersons();
  55. }
  56.  
  57. entity.stateMachine.changeState(IDLE);
  58. }
  59. },
  60.  
  61. BUILD {
  62. @Override
  63. public void enter(AIPlayerConquer entity) {
  64. entity.buildingAsEnemy();
  65. entity.doActionForConquerGame();
  66.  
  67. entity.stateMachine.changeState(IDLE);
  68. }
  69. },
  70.  
  71. AGGRESIVE_SPELL {
  72. @Override
  73. public void enter(AIPlayerConquer entity) {
  74. entity.tryToCastOffensiveSpell();
  75.  
  76. entity.stateMachine.changeState(IDLE);
  77. }
  78. },
  79.  
  80. DEFENSIVE_SPELL {
  81. @Override
  82. public void enter(AIPlayerConquer entity) {
  83. entity.tryToCastDefensiveSpell();
  84.  
  85. entity.stateMachine.changeState(IDLE);
  86. }
  87. };
  88.  
  89. @Override
  90. public void update(AIPlayerConquer entity) {
  91. //this is the hierarchy of action priorities
  92. //when idle, this will be called to search for what state to select next
  93.  
  94. //shoot ogres first because they will destroy walls
  95. entity.findPersons();
  96. if (entity.shouldAttackPersons()) {
  97. entity.stateMachine.changeState(SHOOT_PERSONS);
  98. }
  99.  
  100. //shoot if the opponent is aggressive or controls a lot of the map
  101. else if (entity.isOpponentAggressive() ||
  102. entity.shouldAttackBasedOnOpponentPercentage() ||
  103. entity.shouldAttackBasedOnPercentOwned()) {
  104.  
  105. if (entity.shouldCastSpell(true)) {
  106. entity.stateMachine.changeState(AGGRESIVE_SPELL);
  107. } else {
  108. entity.stateMachine.changeState(SHOOT);
  109. }
  110.  
  111. //otherwise try to build
  112. } else {
  113. if (entity.shouldCastSpell(false)) {
  114. entity.stateMachine.changeState(DEFENSIVE_SPELL);
  115. } else {
  116. entity.stateMachine.changeState(BUILD);
  117. }
  118. }
  119. }
  120.  
  121. @Override
  122. public void enter(AIPlayerConquer entity) {
  123. }
  124. @Override
  125. public void exit(AIPlayerConquer entity) {
  126. }
  127. @Override
  128. public boolean onMessage(AIPlayerConquer entity, Telegram telegram) {
  129. return false;
  130. }
  131. }
  132.  
  133. public enum SpellTarget {
  134.  
  135. MY_CRYSTALS,
  136. MY_WALLS,
  137. MY_EMPTY_SPACE,
  138. MY_CANNON,
  139. OPPONENT_WALLS,
  140. OPPONENT_EMPTY_SPACE,
  141. OPPONENT_CANNON;
  142.  
  143. }
  144.  
  145. public final static List<SpellType> aggresiveSpells = SpellType.getAggresiveSpells();
  146. public final static List<SpellType> defensiveSpells = SpellType.getDefensiveSpells();
  147.  
  148. private static List<SpellType> getAggresiveSpells() {
  149. List<SpellType> types = new ArrayList<SpellType>();
  150. types.add(FIREBALL);
  151. types.add(FIRE_WALL);
  152. types.add(SKELETONS);
  153. types.add(OGRES);
  154. types.add(LIGHTNING);
  155. types.add(CANNON_CHARGE);
  156. types.add(ENERGIZE_CRYSTALS);
  157. return types;
  158. }
  159.  
  160. private static List<SpellType> getDefensiveSpells() {
  161. List<SpellType> types = new ArrayList<SpellType>();
  162. types.add(SHIELD);
  163. types.add(STATIC_CHARGE);
  164. types.add(DIG);
  165. types.add(SHROUD);
  166. types.add(BONUS_WALLS);
  167. return types;
  168. }
  169.  
  170. for (SpellType type : playerType.spells) {
  171. if (SpellType.aggresiveSpells.contains(type)) {
  172. this.aggresiveSpells.add(type);
  173. } else if (SpellType.defensiveSpells.contains(type)) {
  174. this.defensiveSpells.add(type);
  175. }
  176. }
  177.  
  178. protected boolean shouldCastSpell(boolean offensive) {
  179. if (this.timeSinceSpellCast < this.difficulty.minSecondsBetweenSpells) {
  180. return false;
  181. }
  182.  
  183. if (offensive) {
  184. return this.canAffordAggressiveSpell();
  185. } else {
  186. return this.canAffordDefensiveSpell();
  187. }
  188. }
  189.  
  190. protected boolean canAffordAggressiveSpell() {
  191. if (this.aggresiveSpells.size() == 0) {
  192. return false;
  193. }
  194.  
  195. this.spellsReadyToCast = this.getAffordableSpells(this.aggresiveSpells, this.spellsReadyToCast);
  196. return this.spellsReadyToCast.size() > 0;
  197. }
  198.  
  199. protected boolean canAffordDefensiveSpell() {
  200. if (this.defensiveSpells.size() == 0) {
  201. return false;
  202. }
  203.  
  204. this.spellsReadyToCast = this.getAffordableSpells(this.defensiveSpells, this.spellsReadyToCast);
  205. return this.spellsReadyToCast.size() > 0;
  206. }
  207.  
  208. protected List<SpellType> getAffordableSpells(List<SpellType> availableSpells, List<SpellType> canAfford) {
  209. canAfford.clear();
  210. for (SpellType type : availableSpells) {
  211. if (this.hasEnergyForSpell(type)) {
  212. canAfford.add(type);
  213. }
  214. }
  215. return canAfford;
  216. }
  217.  
  218. public void tryToCastOffensiveSpell() {
  219. this.spellsReadyToCast = this.getAffordableSpells(this.aggresiveSpells, this.spellsReadyToCast);
  220. if (this.spellsReadyToCast.size() == 0) {
  221. return;
  222. }
  223. this.tryToCastSpell(this.spellsReadyToCast);
  224. }
  225.  
  226. public void tryToCastDefensiveSpell() {
  227. this.spellsReadyToCast = this.getAffordableSpells(this.defensiveSpells, this.spellsReadyToCast);
  228. if (this.spellsReadyToCast.size() == 0) {
  229. return;
  230. }
  231. this.tryToCastSpell(this.spellsReadyToCast);
  232. }
  233.  
  234. public void tryToCastSpell(List<SpellType> spells) {
  235. this.timeSinceSpellCast = 0;
  236.  
  237. int randomIndex = this.random.nextInt(spells.size());
  238. SpellType randomSpell = spells.get(randomIndex);
  239.  
  240. switch(randomSpell.target) {
  241. case MY_CANNON:
  242. this.castSpellOnMyCannon(randomSpell);
  243. break;
  244. case MY_CRYSTALS:
  245. this.castSpellOnMyCrystals(randomSpell);
  246. break;
  247. case MY_EMPTY_SPACE:
  248. this.castSpellOnMyEmptySpace(randomSpell);
  249. break;
  250. case MY_WALLS:
  251. this.castSpellOnMyWalls(randomSpell);
  252. break;
  253. case OPPONENT_CANNON:
  254. this.castSpellOnOpponentCannon(randomSpell);
  255. break;
  256. case OPPONENT_EMPTY_SPACE:
  257. this.castSpellOnOpponentEmpty(randomSpell);
  258. break;
  259. case OPPONENT_WALLS:
  260. this.castSpellOnOpponentWalls(randomSpell);
  261. break;
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement