Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void update(AIPlayerConquer entity) {
- //this is the hierarchy of action priorities
- //when idle, this will be called to search for what state to select next
- //shoot ogres first because they will destroy walls
- entity.findPersons();
- if (entity.shouldAttackPersons()) {
- entity.stateMachine.changeState(SHOOT_PERSONS);
- }
- //shoot if the opponent is aggressive or controls a lot of the map
- else if (entity.isOpponentAggressive() ||
- entity.shouldAttackBasedOnOpponentPercentage() ||
- entity.shouldAttackBasedOnPercentOwned()) {
- entity.stateMachine.changeState(SHOOT);
- //otherwise try to build
- } else {
- entity.stateMachine.changeState(BUILD);
- }
- }
- public enum AIPlayerConquerState implements State<AIPlayerConquer> {
- IDLE,
- SHOOT {
- @Override
- public void enter(AIPlayerConquer entity) {
- entity.findOpponentWallTiles();
- if (!entity.doesOpponentHaveWalls()) {
- entity.stateMachine.changeState(BUILD);
- } else {
- entity.angryAsEnemy();
- entity.tryToShootWalls();
- }
- entity.stateMachine.changeState(IDLE);
- }
- },
- SHOOT_PERSONS {
- @Override
- public void enter(AIPlayerConquer entity) {
- entity.findPersons();
- if (!entity.shouldAttackPersons()) {
- entity.stateMachine.changeState(BUILD);
- } else {
- entity.tryToShootPersons();
- }
- entity.stateMachine.changeState(IDLE);
- }
- },
- BUILD {
- @Override
- public void enter(AIPlayerConquer entity) {
- entity.buildingAsEnemy();
- entity.doActionForConquerGame();
- entity.stateMachine.changeState(IDLE);
- }
- },
- AGGRESIVE_SPELL {
- @Override
- public void enter(AIPlayerConquer entity) {
- entity.tryToCastOffensiveSpell();
- entity.stateMachine.changeState(IDLE);
- }
- },
- DEFENSIVE_SPELL {
- @Override
- public void enter(AIPlayerConquer entity) {
- entity.tryToCastDefensiveSpell();
- entity.stateMachine.changeState(IDLE);
- }
- };
- @Override
- public void update(AIPlayerConquer entity) {
- //this is the hierarchy of action priorities
- //when idle, this will be called to search for what state to select next
- //shoot ogres first because they will destroy walls
- entity.findPersons();
- if (entity.shouldAttackPersons()) {
- entity.stateMachine.changeState(SHOOT_PERSONS);
- }
- //shoot if the opponent is aggressive or controls a lot of the map
- else if (entity.isOpponentAggressive() ||
- entity.shouldAttackBasedOnOpponentPercentage() ||
- entity.shouldAttackBasedOnPercentOwned()) {
- if (entity.shouldCastSpell(true)) {
- entity.stateMachine.changeState(AGGRESIVE_SPELL);
- } else {
- entity.stateMachine.changeState(SHOOT);
- }
- //otherwise try to build
- } else {
- if (entity.shouldCastSpell(false)) {
- entity.stateMachine.changeState(DEFENSIVE_SPELL);
- } else {
- entity.stateMachine.changeState(BUILD);
- }
- }
- }
- @Override
- public void enter(AIPlayerConquer entity) {
- }
- @Override
- public void exit(AIPlayerConquer entity) {
- }
- @Override
- public boolean onMessage(AIPlayerConquer entity, Telegram telegram) {
- return false;
- }
- }
- public enum SpellTarget {
- MY_CRYSTALS,
- MY_WALLS,
- MY_EMPTY_SPACE,
- MY_CANNON,
- OPPONENT_WALLS,
- OPPONENT_EMPTY_SPACE,
- OPPONENT_CANNON;
- }
- public final static List<SpellType> aggresiveSpells = SpellType.getAggresiveSpells();
- public final static List<SpellType> defensiveSpells = SpellType.getDefensiveSpells();
- private static List<SpellType> getAggresiveSpells() {
- List<SpellType> types = new ArrayList<SpellType>();
- types.add(FIREBALL);
- types.add(FIRE_WALL);
- types.add(SKELETONS);
- types.add(OGRES);
- types.add(LIGHTNING);
- types.add(CANNON_CHARGE);
- types.add(ENERGIZE_CRYSTALS);
- return types;
- }
- private static List<SpellType> getDefensiveSpells() {
- List<SpellType> types = new ArrayList<SpellType>();
- types.add(SHIELD);
- types.add(STATIC_CHARGE);
- types.add(DIG);
- types.add(SHROUD);
- types.add(BONUS_WALLS);
- return types;
- }
- for (SpellType type : playerType.spells) {
- if (SpellType.aggresiveSpells.contains(type)) {
- this.aggresiveSpells.add(type);
- } else if (SpellType.defensiveSpells.contains(type)) {
- this.defensiveSpells.add(type);
- }
- }
- protected boolean shouldCastSpell(boolean offensive) {
- if (this.timeSinceSpellCast < this.difficulty.minSecondsBetweenSpells) {
- return false;
- }
- if (offensive) {
- return this.canAffordAggressiveSpell();
- } else {
- return this.canAffordDefensiveSpell();
- }
- }
- protected boolean canAffordAggressiveSpell() {
- if (this.aggresiveSpells.size() == 0) {
- return false;
- }
- this.spellsReadyToCast = this.getAffordableSpells(this.aggresiveSpells, this.spellsReadyToCast);
- return this.spellsReadyToCast.size() > 0;
- }
- protected boolean canAffordDefensiveSpell() {
- if (this.defensiveSpells.size() == 0) {
- return false;
- }
- this.spellsReadyToCast = this.getAffordableSpells(this.defensiveSpells, this.spellsReadyToCast);
- return this.spellsReadyToCast.size() > 0;
- }
- protected List<SpellType> getAffordableSpells(List<SpellType> availableSpells, List<SpellType> canAfford) {
- canAfford.clear();
- for (SpellType type : availableSpells) {
- if (this.hasEnergyForSpell(type)) {
- canAfford.add(type);
- }
- }
- return canAfford;
- }
- public void tryToCastOffensiveSpell() {
- this.spellsReadyToCast = this.getAffordableSpells(this.aggresiveSpells, this.spellsReadyToCast);
- if (this.spellsReadyToCast.size() == 0) {
- return;
- }
- this.tryToCastSpell(this.spellsReadyToCast);
- }
- public void tryToCastDefensiveSpell() {
- this.spellsReadyToCast = this.getAffordableSpells(this.defensiveSpells, this.spellsReadyToCast);
- if (this.spellsReadyToCast.size() == 0) {
- return;
- }
- this.tryToCastSpell(this.spellsReadyToCast);
- }
- public void tryToCastSpell(List<SpellType> spells) {
- this.timeSinceSpellCast = 0;
- int randomIndex = this.random.nextInt(spells.size());
- SpellType randomSpell = spells.get(randomIndex);
- switch(randomSpell.target) {
- case MY_CANNON:
- this.castSpellOnMyCannon(randomSpell);
- break;
- case MY_CRYSTALS:
- this.castSpellOnMyCrystals(randomSpell);
- break;
- case MY_EMPTY_SPACE:
- this.castSpellOnMyEmptySpace(randomSpell);
- break;
- case MY_WALLS:
- this.castSpellOnMyWalls(randomSpell);
- break;
- case OPPONENT_CANNON:
- this.castSpellOnOpponentCannon(randomSpell);
- break;
- case OPPONENT_EMPTY_SPACE:
- this.castSpellOnOpponentEmpty(randomSpell);
- break;
- case OPPONENT_WALLS:
- this.castSpellOnOpponentWalls(randomSpell);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement