Created
April 10, 2010 21:55
-
-
Save davelyon/362324 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client; | |
import java.awt.Point; | |
import java.util.Enumeration; | |
import java.util.Vector; | |
import client.BattleCharacter.CHARACTER_TYPE; | |
public class GoTeamVenture_AI extends AI{ | |
//Some variable declarations | |
private BattleMap myBattleMap; | |
private BattleCharacter[] characters; | |
private BattleCharacter[] teamVenture; | |
private BattleCharacter[] enemyTeam; | |
private BattleCharacter[] enemiesInRange; | |
private BattleCharacter targetEnemy; | |
private Vector<Point> nontrav; | |
public GoTeamVenture_AI( ){ | |
} | |
/** | |
* Initializing method for your AI, this will only be called once before | |
* the game starts. | |
* | |
*/ | |
public void init() { | |
} | |
/** | |
* This method is called whenever a character who is on your team has a turn. | |
*/ | |
public void runBattle(BattleCharacter character) { | |
myBattleMap = this.getBattleMap(); | |
characters = this.getAllBattleCharacters(); | |
teamVenture = this.getMyCharacters(); | |
enemyTeam = this.getEnemyCharacters(); | |
if(nontrav != null) | |
nontrav = this.getNonTrav(); | |
try { | |
this.changeTargetIfNeeded(); | |
} catch (Exception e){ | |
System.out.println("F'ed up target change."); | |
} | |
//System.out.println("Prioritized Target: " + targetEnemy.getID() + " t = " + targetEnemy.getTeam() + " hp: " + targetEnemy.getCurHealth()); | |
if(character.isDead()) return; | |
this.attackTargetEnemy(character); | |
// JACLYNS CODE | |
Vector<Point> moves = this.getBestMove(character); | |
if (!moves.isEmpty()) { | |
ActionError err = this.battleMove(moves.get(0)); | |
if (err.errorExists() ) { | |
err.printErrors(); | |
} | |
} | |
if(character.isDead()) return; | |
this.attackAnyEnemy(character); | |
} | |
/** | |
* This method is called once at the beginning of the game to determine what characters you will | |
* have on your team. | |
*/ | |
public CHARACTER_TYPE[] setBattleTeam() { | |
CHARACTER_TYPE[] temp = {CHARACTER_TYPE.RANGED, CHARACTER_TYPE.RANGED, CHARACTER_TYPE.SUPPORT, CHARACTER_TYPE.MELEE}; | |
return temp; | |
} | |
public void attackTargetEnemy(BattleCharacter character) { | |
ActionError err = new ActionError("TeamVenture Attacking Errors"); | |
if(isRanged(character)) { | |
if(character.ableToUseSpecial()){ | |
if(!targetEnemy.isDead()) | |
err = this.battleUseSpecial(targetEnemy.getID()); | |
} | |
if(err.errorExists()) | |
err.printErrors(); | |
else | |
return; | |
} | |
} | |
public void attackAnyEnemy(BattleCharacter character) { | |
this.attackTargetEnemy(character); | |
ActionError err2 = this.battleAttack(targetEnemy.getID()); | |
if(err2.errorExists()) { | |
err2.addError("Failed to attack priority target. Lets mess with (anyone) someone else!"); | |
err2.printErrors(); | |
for(int i=0; i < enemyTeam.length-1; i++) { | |
if(!enemyTeam[i].isDead()){ | |
ActionError err3 = this.battleAttack(enemyTeam[i].getID()); | |
if(err3.errorExists()) | |
err3.printErrors(); | |
else | |
return; | |
} | |
} | |
} | |
} | |
public void changeTargetIfNeeded() { | |
if ( targetEnemy == null || targetEnemy.isDead() ) | |
targetEnemy = getBestEnemyToAttack(); | |
} | |
public BattleCharacter getBestEnemyToAttack() { | |
BattleCharacter temp = null; | |
for(int i=0; i<enemyTeam.length; i++) { | |
if( temp == null || getValue(enemyTeam[i].getCharacterType().toString()) < getValue(temp.getCharacterType().toString())){ | |
if(!enemyTeam[i].isDead()) | |
temp = enemyTeam[i]; | |
} | |
} | |
if (temp == null) | |
return enemyTeam[0]; | |
return temp; | |
} | |
public int getValue(String s) { | |
if (s.equalsIgnoreCase("ASSASIN")) | |
return 9; | |
else if (s.equalsIgnoreCase("HEALER")) | |
return 8; | |
else if (s.equalsIgnoreCase("RANGED")) | |
return 7; | |
else if (s.equalsIgnoreCase("SUPPORT")) | |
return 5; | |
else if (s.equalsIgnoreCase("MELEE")) | |
return 3; | |
return 1; | |
} | |
public boolean isRanged(BattleCharacter c) { | |
return getValue(c.getCharacterType().toString()) == getValue("RANGED"); | |
} | |
public boolean isSupport(BattleCharacter c) { | |
return getValue(c.getCharacterType().toString()) == getValue("SUPPORT"); | |
} | |
public boolean isMelee(BattleCharacter c){ | |
return getValue(c.getCharacterType().toString()) == getValue("MELEE"); | |
} | |
public void moveToEnemy(BattleCharacter character ) { | |
Point location = targetEnemy.getGridLoc(); | |
if( this.getAllPossibleMoveLocations(character).contains( location ) ){ | |
} | |
} | |
public Vector<Point> getBadPoints(){ | |
Vector<Point> bad = new Vector<Point>(); | |
for(int i = 0; i < enemyTeam.length; i++) | |
bad.add(enemyTeam[i].getGridLoc()); | |
for(int i = 0; i < teamVenture.length; i++) | |
bad.add(teamVenture[i].getGridLoc()); | |
return bad; | |
} | |
public Vector<Point> getNonTrav(){ | |
Vector<Point> bad = new Vector<Point>(); | |
BattleMapTile[][] tiles = myBattleMap.getTiles(); | |
for(int i = 0; i < myBattleMap.getRows(); i++) | |
for(int z = 0; i < myBattleMap.getColumns(); i++) | |
if(!tiles[i][z].isTraversable()) | |
bad.add(new Point(i,z)); | |
return bad; | |
} | |
public boolean shouldIRun(BattleCharacter character){ | |
return character.getCurHealth() / character.getMaxHealth() < .35; | |
} | |
public Point safeHarbors(BattleCharacter character){ | |
Vector<Point> moves = this.getAllPossibleMoveLocations(character); | |
for(moves.elements(); moves.elements().hasMoreElements();) | |
return null; | |
return null; | |
} | |
// Jaclyns methods | |
public Vector<Point> getBestMove(BattleCharacter character) { | |
Vector<Point> possibleLocs = this.getAllPossibleMoveLocations(character); | |
Point whereAmI = character.getGridLoc(); | |
if(possibleLocs.isEmpty()) | |
return possibleLocs; | |
//Point closestItem = this.getClosestItemLoc(whereAmI, myBattleMap.getAllItemsOnMap(), possibleLocs); | |
Point closestEnemy = targetEnemy.getGridLoc();//this.getClosestEnemyLoc(whereAmI, possibleLocs); // | |
return this.getClosestMovesToEnemy(possibleLocs, character, closestEnemy, whereAmI); | |
} | |
public Vector<Point> moveTowardEnemy(Vector<Point> possibleLocs, BattleCharacter character, Point enemyLoc, Point whereAmI) { | |
Vector<Point> narrowedDown = this.findNearestTiles(character, enemyLoc, possibleLocs); | |
Point supportChar = this.getSupportChar(); | |
// Stay where you are if you are too far away from support. | |
if(!this.meleeIsDead() && !this.supportIsDead()) { | |
if(this.getDistanceMoved(supportChar, character.getGridLoc()) > 5) { | |
narrowedDown.clear(); | |
return narrowedDown; | |
} | |
} | |
// If you can't reach the enemy, get as close as possible | |
if(narrowedDown.isEmpty()) { | |
double minDist = this.getDistanceMoved(possibleLocs.get(0), enemyLoc); | |
Point maxMove = possibleLocs.get(0); | |
for(int i=0; i<possibleLocs.size(); i++) { | |
Vector<Item> items = myBattleMap.getAllItemsOnMap(); | |
if(this.canGetToItem(possibleLocs, items)) { | |
maxMove = this.getClosestItemLoc(whereAmI, items, possibleLocs); | |
} | |
else { | |
Point moveLoc = possibleLocs.get(i); | |
if(this.getDistanceMoved(moveLoc, enemyLoc) < minDist) { | |
minDist = this.getDistanceMoved(moveLoc, enemyLoc); | |
maxMove = moveLoc; | |
} | |
} | |
} | |
narrowedDown.add(maxMove); | |
} | |
return this.getFarthestTileInRange(narrowedDown, enemyLoc, character.getAttackRange(), character.getGridLoc()); | |
} | |
public Vector<Point> followMelee(Vector<Point> possibleLocs, BattleCharacter character) { | |
Point meleeChar = this.getMeleeChar(); | |
double minDist = 999; | |
Point whereTo = possibleLocs.get(0); | |
// Get points near the melee character | |
Vector<Point> narrowedDown = this.findNearestTiles(character, meleeChar, possibleLocs); | |
// If there are none, get as close as possible | |
if(narrowedDown.isEmpty()) { | |
Point maxMove = possibleLocs.get(0); | |
Vector<Item> items = myBattleMap.getAllItemsOnMap(); | |
if(this.canGetToItem(possibleLocs, items)) { | |
maxMove = this.getClosestItemLoc(character.getGridLoc(), items, possibleLocs); | |
} | |
else { | |
for(int i=0; i<possibleLocs.size(); i++) { | |
Point moveLoc = possibleLocs.get(i); | |
if(this.getDistanceMoved(moveLoc, meleeChar) < minDist) { | |
minDist = this.getDistanceMoved(moveLoc, meleeChar); | |
maxMove = moveLoc; | |
} | |
} | |
} | |
narrowedDown.add(maxMove); | |
} | |
else { | |
minDist = 999; | |
for(Enumeration e = narrowedDown.elements(); e.hasMoreElements();) { | |
Point tile = (Point) e.nextElement(); | |
if(this.getDistanceMoved(tile, meleeChar) < minDist) { | |
minDist = this.getDistanceMoved(tile, meleeChar); | |
whereTo = tile; | |
} | |
} | |
} | |
narrowedDown.add(whereTo); | |
return narrowedDown; | |
} | |
public boolean canGetToItem(Vector<Point> possibleLocs, Vector<Item> items) { | |
/* for(int i=0; i<items.size(); i++) { | |
if(items.get(i).getGridLoc() != null && possibleLocs.contains((Point) items.get(i).getGridLoc())) { | |
System.out.println(items.get(i).getGridLoc()); | |
System.out.println(items.get(i).getID()); | |
return true; | |
} | |
}*/ | |
return false; | |
} | |
public Vector<Point> getFarthestTileInRange(Vector<Point> tiles, Point enemyLoc, int range, Point characterLoc) { | |
Vector<Item> items = myBattleMap.getAllItemsOnMap(); | |
if(this.canGetToItem(tiles, items)) { | |
tiles.clear(); | |
tiles.add(this.getClosestItemLoc(characterLoc, items, tiles)); | |
} | |
else { | |
for(Enumeration e = tiles.elements(); e.hasMoreElements();) { | |
Point tile = (Point) e.nextElement(); | |
if(this.getDistanceMoved(tile, enemyLoc) == range) { | |
tiles.clear(); | |
tiles.add(tile); | |
break; | |
} | |
} | |
} | |
return tiles; | |
} | |
public Vector<Point> moveRanged(Vector<Point> possibleLocs, BattleCharacter character, Point enemyLoc, Point whereAmI) { | |
Vector<Point> narrowedDown = this.findNearestTiles(character, enemyLoc, possibleLocs); | |
int range = character.getAttackRange(); | |
if(narrowedDown.isEmpty()) { | |
return this.followMelee(possibleLocs, character); | |
} | |
return getFarthestTileInRange(narrowedDown, enemyLoc, range, character.getGridLoc()); | |
} | |
public Vector<Point> getClosestMovesToEnemy(Vector<Point> possibleLocs, BattleCharacter character, Point enemyLoc, Point whereAmI) { | |
Vector<Point> narrowedDown = (Vector<Point>) possibleLocs.clone(); // HACK | |
narrowedDown.clear(); // HACK | |
if(this.isMelee(character)) { | |
return this.moveTowardEnemy(possibleLocs, character, enemyLoc, whereAmI); | |
} | |
else if(this.isSupport(character)) { | |
if(this.meleeIsDead()) | |
return this.moveTowardEnemy(possibleLocs, character, enemyLoc, whereAmI); | |
return this.followMelee(possibleLocs, character); | |
} | |
else if(this.meleeIsDead()) { | |
return this.moveTowardEnemy(possibleLocs, character, enemyLoc, whereAmI); | |
} | |
return this.moveRanged(possibleLocs, character, enemyLoc, whereAmI); | |
} | |
public Vector<Point> getClosestMovesToItem(Vector<Point> possibleLocs, Point itemLoc, Point whereAmI) { | |
Vector<Point> narrowedDown = (Vector<Point>) possibleLocs.clone(); // HACK | |
narrowedDown.clear(); // HACK | |
if(possibleLocs.contains(itemLoc)) | |
narrowedDown.add(itemLoc); | |
if(narrowedDown.isEmpty()) { | |
double minDist = this.getDistanceMoved(possibleLocs.get(0), itemLoc); | |
Point maxMove = possibleLocs.get(0); | |
for(int i=0; i<possibleLocs.size(); i++) { | |
Point moveLoc = possibleLocs.get(i); | |
if(this.getDistanceMoved(moveLoc, itemLoc) < minDist) { | |
minDist = this.getDistanceMoved(moveLoc, itemLoc); | |
maxMove = moveLoc; | |
} | |
} | |
narrowedDown.add(maxMove); | |
} | |
return narrowedDown; | |
} | |
public Point getClosestItemLoc(Point whereAmI, Vector<Item> items, Vector<Point> possibleLocs) { | |
double minDist = this.getDistanceMoved(whereAmI, items.get(0).getGridLoc()); | |
Point closest = items.get(0).getGridLoc(); | |
for(int i=0; i<items.size(); i++) { | |
Point itemLoc = items.get(i).getGridLoc(); | |
for(int j=0; j<possibleLocs.size(); j++) { | |
if(this.getDistanceMoved(possibleLocs.get(j), itemLoc) < minDist) { | |
minDist = this.getDistanceMoved(possibleLocs.get(j), itemLoc); | |
closest = itemLoc; | |
} | |
} | |
} | |
return closest; | |
} | |
public Point getClosestEnemyLoc(Point whereAmI, Vector<Point> possibleLocs) { | |
double minDist = 999; | |
Point closest = enemyTeam[0].getGridLoc(); | |
for(int i=0; i<4; i++) { | |
Point enemy = enemyTeam[i].getGridLoc(); | |
for(int j=0; j<possibleLocs.size(); j++) { | |
if(this.getDistanceMoved(possibleLocs.get(j), enemy) < minDist) { | |
minDist = this.getDistanceMoved(possibleLocs.get(j), enemy); | |
closest = enemy; | |
} | |
} | |
} | |
return closest; | |
} | |
public Vector<Point> findNearestTiles(BattleCharacter character, Point whereIsHe, Vector<Point> possibleLocs) { | |
Point whereAmI = character.getGridLoc(); | |
Vector<Point> nearestTile = (Vector<Point>) possibleLocs.clone(); | |
nearestTile.clear(); | |
if(this.getDistanceMoved(whereAmI, whereIsHe) > character.getMoveRange()) { | |
return nearestTile; | |
} | |
int range = character.getAttackRange(); | |
double dist = 999; | |
for(int i=0; i<possibleLocs.size(); i++) { | |
Point move = possibleLocs.get(i); | |
double rangeTemp = this.getDistanceMoved(move, whereIsHe); | |
//double distTemp = this.getDistanceMoved(whereAmI, move); | |
if(rangeTemp <= range) { | |
nearestTile.add(move); | |
} | |
} | |
return nearestTile; | |
} | |
public double getDistanceMoved(Point current, Point move) { | |
if(current == null || move == null) | |
return 999; | |
return Math.abs(current.getX() - move.getX()) + Math.abs(current.getY() - move.getY()); | |
} | |
private Point getMeleeChar() { | |
for(int i=0; i<teamVenture.length; i++) { | |
if(this.isMelee(teamVenture[i])) | |
return teamVenture[i].getGridLoc(); | |
} | |
return teamVenture[0].getGridLoc(); | |
} | |
private Point getSupportChar() { | |
for(int i=0; i<teamVenture.length; i++) { | |
if(this.isSupport(teamVenture[i])) | |
return teamVenture[i].getGridLoc(); | |
} | |
return teamVenture[0].getGridLoc(); | |
} | |
public boolean meleeIsDead() { | |
for(int i=0; i<4; i++) { | |
if(this.isMelee(teamVenture[i]) && teamVenture[i].isDead()) | |
return true; | |
} | |
return false; | |
} | |
public boolean supportIsDead() { | |
for(int i=0; i<4; i++) { | |
if(this.isSupport(teamVenture[i]) && teamVenture[i].isDead()) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment