package edu.lehigh.insyte.ctp2_liet; import java.util.Random; import java.util.Vector; import java.util.ArrayList; import utility.InvalidArgumentException; import mil.navy.nrl.liet.ActionException; import mil.navy.nrl.liet.ActionInitiator; import mil.navy.nrl.liet.Agent; import mil.navy.nrl.liet.AgentBuilder; import mil.navy.nrl.liet.AgentException; import mil.navy.nrl.liet.Environment; import mil.navy.nrl.liet.EnvironmentBuilder; import mil.navy.nrl.liet.ExternalEnvironmentException; import mil.navy.nrl.liet.GlobalExceptionHandlerAccess; import mil.navy.nrl.liet.UnexpectedEnvironmentException; import mil.navy.nrl.liet.data.Action; import mil.navy.nrl.liet.data.LIETObj; import mil.navy.nrl.liet.data.LIETObjFactory; import mil.navy.nrl.liet.data.Observation; import mil.navy.nrl.liet.data.Range; import mil.navy.nrl.liet.data.Symbol; import tielt.execution.ExecutionEnvironment; import tielt.data.file.GameModel; import java.util.LinkedHashMap; /* First we define classes for each type of object in the API * Instances of these classes should only be used in this file * because they are used as a representation of the game state * in CTP2. */ /* Class for a player */ class API_Player { public int index; //integer index referring to one player /* Constructors */ public API_Player() { index = 0; } public API_Player(int value) { index = value; } } /* Class for an army unit */ class API_Army { public API_Player p_Player; //player object public int armyID; //ID of this army unit public int xPosition; //army's x-position on the map public int yPosition; //army's y-position on the map public Boolean garrisoning; //whether it is garrisoning /* Constructor. Note that garrisoning is set to false * automatically because a unit is not garrisoning when it * has been newly created. */ public API_Army(int playerID, int army, int x_pos, int y_pos) { p_Player = new API_Player(playerID); armyID = army; xPosition = x_pos; yPosition = y_pos; garrisoning = false; } } /* Class for a city */ class API_City { public API_Player p_Player; //player object public int cityID; //ID of this city public int xPosition; //city's x-position on the map public int yPosition; //city's y-position on the map /* Constructor */ public API_City(int playerID, int city, int x_pos, int y_pos) { p_Player = new API_Player(playerID); cityID = city; xPosition = x_pos; yPosition = y_pos; } } /* Class for a location on the map */ class API_Location { public int x_coord; //x-position on the map public int y_coord; //y-position on the map /* Constructor */ public API_Location (int x, int y) { x_coord = x; y_coord = y; } } public class CTP2Agent extends Agent { /** * This is the public identifier for this type of agent. Each created agent * should have a similar identifier, which is returned by Agent.getType and * AgentBuilder.getType, and indexes it in the registry. */ public static final Symbol CTP2_AGENT_TYPE = Symbol.makeSymbol("CTP2 agent"); /** * This constant identifies the success metric, which measures whether the * agent was successful in the match. It is a legal input to the measure * method. */ public static final Symbol SUCCESS_METRIC = Symbol.makeSymbol("Success"); /** * This constant is an action to attempt to settle a city by querying if it is possible */ public static final Symbol MAKE_CITY = Symbol.makeSymbol("QueryCityBuildable"); /** * This constant is an action attempt to load a game. Not currently in API, just * for testing messages now. */ public static final Symbol LOAD_GAME = Symbol.makeSymbol("Load"); /*********** Gane State Variables **********/ /* Set true once we have a city */ private Boolean haveCity = false; /* ArrayList for all armies */ ArrayList allMyArmies = new ArrayList(); /* ArrayList for all cities you control */ ArrayList allMyCities = new ArrayList(); /* ArrayList for all locations of enemy cities */ ArrayList enemyCitiesFound = new ArrayList(); /* Variable to count how many turns have passed */ private int turn_count = 0; /* Variable for player index */ public static Integer player_index; /* Variable for number of enemy players in the game */ private int num_enemy_players; /* Variable for the number of enemy cities found */ private int num_enemy_cities = 0; /* Variable for the number of cities you control */ private int num_my_cities = 0; /* Variable for the number of armies you control */ private int num_my_armies = 0; /************ End of State Variables *************/ private static int COUNT; private ActionInitiator ai; private static final Range[] METRICS = new Range[1]; static { METRICS[0] = Range.createEnumeratedIntegerRange( SUCCESS_METRIC, -1, 1, 1, 0); } private Symbol sName; private int iSuccess = 0; private String sRole; public ExecutionEnvironment env; /** * Constructor. * Creates a new tic-tac-toe environment object. */ public CTP2Agent() { super(); sName = Symbol.makeSymbol(CTP2_AGENT_TYPE + "-" + COUNT); COUNT ++; } @Override public Symbol getName() { return sName; } @Override public void setup(Environment env, ActionInitiator initiator) throws UnexpectedEnvironmentException { ai = initiator; } @Override public void receiveObservation(Observation o) throws AgentException { System.out.println("I received: " + o.toString()); /* Change state according to the messages received */ /* Also want to check for failure messages */ /* Ideally, this should mimic what TIELT has, and use ArrayLists for each type of important thing */ //Handle CityAttackable observation model if (o.getType().toString().equalsIgnoreCase("CityAttackable")) { Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int city_x = Integer.valueOf(o.getParams().elementAt(2).toString()); int city_y = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == false) { System.out.println("Army with ID: " + armyID + " cannot attack city at " + city_x + ", " + city_y); } else { System.out.println("Army with ID: " + armyID + " is attacking city at " + city_x + ", " + city_y); } } // Handle CityBuildable observation model if (o.getType().toString().equalsIgnoreCase("CityBuildable")) { Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int city_x = Integer.valueOf(o.getParams().elementAt(2).toString()); int city_y = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == false) { System.out.println("Army with ID: " + armyID + " cannot build a city at " + city_x + ", " + city_y); } else { System.out.println("Army with ID: " + armyID + " is settling a city at " + city_x + ", " + city_y); } } // Handle CitySettled observation model if (o.getType().toString().equalsIgnoreCase("CitySettled")) { int cityID = Integer.valueOf(o.getParams().elementAt(0).toString()); int city_x = Integer.valueOf(o.getParams().elementAt(1).toString()); int city_y = Integer.valueOf(o.getParams().elementAt(2).toString()); // Make an API_City object and put this into the arraylist API_City city = new API_City(player_index, cityID, city_x, city_y); allMyCities.add(city); // Increase counter of number of our cities num_my_cities++; //Set boolean that we have built a city haveCity = true; } //Handle EnemyCityDestroyed observation model if (o.getType().toString().equalsIgnoreCase("EnemyCityDestroyed")) { int armyID = Integer.valueOf(o.getParams().elementAt(0).toString()); int city_x = Integer.valueOf(o.getParams().elementAt(1).toString()); int city_y = Integer.valueOf(o.getParams().elementAt(2).toString()); // Find out where this city is in our list API_Location location = new API_Location(city_x, city_y); int index_to_remove = enemyCitiesFound.indexOf(location); //Remove this element from the list of enemy cities if (index_to_remove != -1) { enemyCitiesFound.remove(index_to_remove); //Decrement count of enemy cities num_enemy_cities--; } else { System.out.println("Enemy city at " + city_x + ", " + city_y + "not in the list"); } } //Handle EnemyUnitDestroyed observation model if (o.getType().toString().equalsIgnoreCase("EnemyUnitDestroyed")) { int armyID = Integer.valueOf(o.getParams().elementAt(0).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); System.out.println("Army " + armyID + " killed the opposing unit"); } // Handle GameOver observation model if (o.getType().toString().equalsIgnoreCase("GameOver")) { System.out.println("Game has ended. Agent should be stopped."); } //Handle GameStarted observation model if (o.getType().toString().equalsIgnoreCase("GameStarted")) { System.out.println("New game has begun."); } // Handle Garrisoning observation model if (o.getType().toString().equalsIgnoreCase("Garrisoning")) { System.out.println("Garrisoning parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString() + ", " + o.getParams().elementAt(3).toString()); // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == true) { // Make an API_Army object with the old position to retrieve it from the arraylist API_Army tempArmy = new API_Army(player_index, armyID, x_pos, y_pos); // Find the index of the element in the arraylist that has this unitID int index_to_change = allMyArmies.indexOf(tempArmy); if (index_to_change == -1) { System.out.println("Error: Can't find army " + armyID + " at position " + x_pos + "," + y_pos); } else { // Change tempArmy to have garrisoning set to true tempArmy.garrisoning = true; allMyArmies.set(index_to_change, tempArmy); } } else { System.out.println("Error. Army " + armyID + " cannot garrison at " + x_pos + ", " + y_pos); } } // Handle GameLoaded observation model if (o.getType().toString().equalsIgnoreCase("GameLoaded")) { Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); if (success == true) { System.out.println("Game successfully loaded."); } else { System.out.println("Game failed to load."); } } // Handle ImprovementBuildable observation model if (o.getType().toString().equalsIgnoreCase("ImprovementBuildable")) { Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int cityID = Integer.valueOf(o.getParams().elementAt(1).toString()); int type = Integer.valueOf(o.getParams().elementAt(2).toString()); if (success == true) { System.out.println("City " + cityID + " has queued improvement #" + type); } else { System.out.println("City " + cityID + " could not build improvement #" + type); } } // Handle Lose observation model if (o.getType().toString().equalsIgnoreCase("Lose")) { System.out.println("YOU LOSE!!! ...Loser"); } // Handle MyCityDestroyed observation model if (o.getType().toString().equalsIgnoreCase("MyCityDestroyed")) { System.out.println("MyCityDestroyed parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString()); // Extract the arguments int cityID = Integer.valueOf(o.getParams().elementAt(0).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); // Make an API_City object to retrieve it from the arraylist API_City tempCity = new API_City(player_index, cityID, x_pos, y_pos); // Find the index of the element in the arraylist that has this cityID int index_to_remove = allMyCities.indexOf(tempCity); if (index_to_remove == -1) { System.out.println("Error: Can't find city " + cityID + " at position " + x_pos + "," + y_pos); } else { // Remove this city from the arraylist allMyCities.remove(index_to_remove); num_my_cities--; } } // Handle MyID observation model if (o.getType().toString().equalsIgnoreCase("MyID")) { player_index = Integer.valueOf(o.getParams().elementAt(0).toString()); } //Handle MyUnitDestroyed observation model if (o.getType().toString().equalsIgnoreCase("MyUnitDestroyed")) { System.out.println("MyUnitDestroyed parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString()); // Extract the arguments int armyID = Integer.valueOf(o.getParams().elementAt(0).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); // Make an API_City object to retrieve it from the arraylist API_Army tempArmy = new API_Army(player_index, armyID, x_pos, y_pos); // Find the index of the element in the arraylist that has this unitID int index_to_remove = allMyCities.indexOf(tempArmy); if (index_to_remove == -1) { System.out.println("Error: Can't find army " + armyID + " at position " + x_pos + "," + y_pos); } else { // Remove this city from the arraylist allMyArmies.remove(index_to_remove); num_my_armies--; } } // Handle NearEnemyCity observation model if (o.getType().toString().equalsIgnoreCase("NearEnemyCity")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); if (success == true) { // Make an API_City object to retrieve it from the arraylist API_Location tempCity = new API_Location(x_pos, y_pos); // Add this city to the list of enemy cities enemyCitiesFound.add(tempCity); // Increment number of enemy cities found num_enemy_cities++; } else { System.out.println("No enemy cities nearby"); } } // Handle NearEnemyUnit observation model // Note: there is currently no datastructure used to store the enemy units found if (o.getType().toString().equalsIgnoreCase("NearEnemyUnit")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); if (success == true) { System.out.println("Enemy Unit detected at " + x_pos + ", " + y_pos); } else { System.out.println("No enemy units found."); } } // Handle NewUnitComplete observation model if (o.getType().toString().equalsIgnoreCase("NewUnitComplete")) { System.out.println("NewUnitComplete parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString()); // Extract the arguments int unitID = Integer.valueOf(o.getParams().elementAt(0).toString()); int x_position = Integer.valueOf(o.getParams().elementAt(1).toString()); int y_position = Integer.valueOf(o.getParams().elementAt(2).toString()); // Make an API_Army object and add it to the ArrayList API_Army tempArmy = new API_Army(player_index, unitID, x_position, y_position); allMyArmies.add(tempArmy); // Increment the number of armies we control num_my_armies++; } // Handle NumPlayers observation model if (o.getType().toString().equalsIgnoreCase("NumPlayers")) { int num_players = Integer.valueOf(o.getParams().elementAt(0).toString()); num_enemy_players = num_players - 1; } // Handle StoppedGarrisoning observation model if (o.getType().toString().equalsIgnoreCase("StoppedGarrisoning")) { System.out.println("StoppedGarrisoning parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString() + ", " + o.getParams().elementAt(3).toString()); // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == true) { // Make an API_Army object with the old position to retrieve it from the arraylist API_Army tempArmy = new API_Army(player_index, armyID, x_pos, y_pos); // Find the index of the element in the arraylist that has this unitID int index_to_change = allMyArmies.indexOf(tempArmy); if (index_to_change == -1) { System.out.println("Error: Can't find army " + armyID + " at position " + x_pos + "," + y_pos); } else { // Change tempArmy to have garrisoning set to false tempArmy.garrisoning = false; allMyArmies.set(index_to_change, tempArmy); } } else { System.out.println("Error. Army " + armyID + " could not ungarrison at " + x_pos + ", " + y_pos); } } // Handle UnexploredTile observation model if (o.getType().toString().equalsIgnoreCase("UnexploredTile")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == true) { System.out.println("Unexplored tile found at " + x_pos + ", " + y_pos); } else { System.out.println("No unexplored tiles around unit with ID " + armyID); } } // Handle UnitAttackable observation model if (o.getType().toString().equalsIgnoreCase("UnitAttackable")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == true) { System.out.println("Army " + armyID + " can attack position " + x_pos + ", " + y_pos); } else { System.out.println("Army " + armyID + " cannot attack position " + x_pos + ", " + y_pos); } } // Handle UnitBuildable observation model if (o.getType().toString().equalsIgnoreCase("UnitBuildable")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int cityID = Integer.valueOf(o.getParams().elementAt(1).toString()); int type = Integer.valueOf(o.getParams().elementAt(2).toString()); if (success == true) { System.out.println("City " + cityID + " can build improvement " + type); } else { System.out.println("City " + cityID + " cannot build improvement " + type); } } // Handle UnitMoveable observation model if (o.getType().toString().equalsIgnoreCase("UnitMoveable")) { // Extract the arguments Boolean success = Boolean.valueOf(o.getParams().elementAt(0).toString()); int armyID = Integer.valueOf(o.getParams().elementAt(1).toString()); int x_pos = Integer.valueOf(o.getParams().elementAt(2).toString()); int y_pos = Integer.valueOf(o.getParams().elementAt(3).toString()); if (success == true) { System.out.println("Army " + armyID + " can move to " + x_pos + ", " + y_pos); } else { System.out.println("Army " + armyID + " cannot move to " + x_pos + ", " + y_pos); } } // Assign variables for UpdatePosition message if (o.getType().toString().equalsIgnoreCase("UpdatePosition")) { System.out.println("UpdatePosition parameters:" + o.getParams().elementAt(0).toString() + ", " + o.getParams().elementAt(1).toString() + ", " + o.getParams().elementAt(2).toString()); // Should find this ID from the arraylist, and change its x-y position // Extract the arguments int unitID = Integer.valueOf(o.getParams().elementAt(0).toString()); int old_x = Integer.valueOf(o.getParams().elementAt(1).toString()); int old_y = Integer.valueOf(o.getParams().elementAt(2).toString()); int new_x = Integer.valueOf(o.getParams().elementAt(3).toString()); int new_y = Integer.valueOf(o.getParams().elementAt(4).toString()); // Make an API_Army object with the old position to retrieve it from the arraylist API_Army tempArmy = new API_Army(player_index, unitID, old_x, old_y); // Find the index of the element in the arraylist that has this unitID int index_to_change = allMyArmies.indexOf(tempArmy); /***NEED debugging here, print the whole arraylist***/ if (index_to_change == -1) { //System.out.println("Error: Can't find unitID " + unitID + " at position " + old_x + "," + old_y); } else { // Change tempArmy to have the updated x-y coordinates, then change the // element in the arraylist tempArmy.xPosition = new_x; tempArmy.yPosition = new_y; allMyArmies.set(index_to_change, tempArmy); } } } public void notifyOfEpisodeStart() { //This method intentionally left blank. } public void notifyOfTick() { //This method intentionally left blank. } public void notifyOfEpisodeEnd() { //This method intentionally left blank. } public void notifyOfPause() { //This method intentionally left blank. } public void notifyOfUnpause() { //This method intentionally left blank. } private static Random r = new Random(); /* Call all the desired actions for the turn here */ public void notifyOfTurnEnd() { /* Arbitrary delay just to make sure we've received all the state update messages */ if (turn_count == 2) { /*** Test QueryUngarrison -use this with scenario 'ungarrison test' ***/ LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(5)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(17)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(21)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(true)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); System.out.println("About to send QueryUngarrison()"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryUngarrison"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } /*** Test QueryGarrison -use this with scenario 'garrison test' ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(5)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(17)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(21)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); System.out.println("About to send QueryGarrison()"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryGarrison"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryAttackUnit -use this with scenario 'test3' ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(0)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(24)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(10)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up API_Location object mProps.clear(); mProps.put(Symbol.makeSymbol("m_iXCoord"), LIETObjFactory.makeInt(24)); mProps.put(Symbol.makeSymbol("m_iYCoord"), LIETObjFactory.makeInt(11)); LIETObj Location = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Location"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); v.add(Location); System.out.println("About to send QueryAttackUnit()"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryAttackUnit"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryAttackCity -use this with scenario 'test3' ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(0)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(24)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(10)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up API_Location object mProps.clear(); mProps.put(Symbol.makeSymbol("m_iXCoord"), LIETObjFactory.makeInt(24)); mProps.put(Symbol.makeSymbol("m_iYCoord"), LIETObjFactory.makeInt(11)); LIETObj Location = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Location"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); v.add(Location); System.out.println("About to send QueryAttackCity()"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryAttackCity"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryEnemyCity ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(allMyArmies.get(0).armyID)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).xPosition)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).yPosition)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); v.add(LIETObjFactory.makeInt(2)); //vision range of the unit System.out.println("About to send QueryEnemyCity(" + player_index + ", " + allMyArmies.get(0).armyID + ", " + allMyArmies.get(0).xPosition + ", " + allMyArmies.get(0).yPosition +", 2)"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryEnemyCity"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryEnemyUnit ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(allMyArmies.get(0).armyID)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).xPosition)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).yPosition)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); v.add(LIETObjFactory.makeInt(2)); //vision range of the unit System.out.println("About to send QueryEnemyUnit(" + player_index + ", " + allMyArmies.get(0).armyID + ", " + allMyArmies.get(0).xPosition + ", " + allMyArmies.get(0).yPosition +", 2)"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryEnemyUnit"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryUnexploredMap ***/ /* LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); // Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); // Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(allMyArmies.get(0).armyID)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).xPosition)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).yPosition)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); // Set up arguments for the action model Vector v = new Vector(); v.add(Army); System.out.println("About to send QueryUnexploredMap(" + player_index + ", " + allMyArmies.get(0).armyID + ", " + allMyArmies.get(0).xPosition + ", " + allMyArmies.get(0).yPosition +")"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryUnexploredMap"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test Load ***/ /* Vector v = new Vector(); v.add(LIETObjFactory.makeString("C:\\devel\\~myProjs\\CTP2\\ctp2_code\\ctp\\save\\games\\Julius\\test1")); //name of the game to load System.out.println("About to send Load"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("Load"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ /*** Test QueryCityBuildable and QueryMoveable ***/ /* Prepare hashmap to convert the TIELT object LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); Set up API_Player object to be used inside of API_Army mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); Set up API_Player object mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iArmyId"), LIETObjFactory.makeInt(allMyArmies.get(0).armyID)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).xPosition)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(allMyArmies.get(0).yPosition)); //from message mProps.put(Symbol.makeSymbol("garrisoning"), LIETObjFactory.makeBoolean(false)); LIETObj Army = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Army"), mProps); Set up API_Location object for destination (x and y location + 1) mProps.clear(); mProps.put(Symbol.makeSymbol("m_iXCoord"), LIETObjFactory.makeInt(x_position + 1)); mProps.put(Symbol.makeSymbol("m_iYCoord"), LIETObjFactory.makeInt(y_position)); LIETObj Location = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Location"), mProps); Set up arguments for the action model Vector v = new Vector(); v.add(Army); //v.add(Location); System.out.println("About to send QueryCityBuildable(" + player_index + ", " + allMyArmies.get(0).armyID + ", " + allMyArmies.get(0).xPosition + ", " + allMyArmies.get(0).yPosition +")"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryCityBuildable"), v)); } System.out.println("About to send QueryMoveable"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryMoveable"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } */ } //end if if (haveCity == true) { /*** Test QueryUnitBuildable ***/ /* Prepare hashmap to convert the TIELT object */ LinkedHashMap mProps = new LinkedHashMap(); mProps.clear(); /* Set up API_Player object to be used inside of API_Army */ mProps.put(Symbol.makeSymbol("m_iIndex"), LIETObjFactory.makeInt(player_index)); //note, make a variable for this LIETObj Player = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_Player"), mProps); /* Set up API_City object */ mProps.clear(); mProps.put(Symbol.makeSymbol("m_pPlayer"), Player); mProps.put(Symbol.makeSymbol("m_iCityId"), LIETObjFactory.makeInt(allMyCities.get(0).cityID)); //from message mProps.put(Symbol.makeSymbol("x_pos"), LIETObjFactory.makeInt(allMyCities.get(0).xPosition)); //from message mProps.put(Symbol.makeSymbol("y_pos"), LIETObjFactory.makeInt(allMyCities.get(0).yPosition)); //from message LIETObj City = LIETObjFactory.makeInstance(Symbol.makeSymbol("API_City"), mProps); /* Set up arguments for the action model */ Vector v = new Vector(); v.add(LIETObjFactory.makeInt(70)); //add ID of Warrior to the message v.add(City); System.out.println("About to send QueryUnitBuildable"); try { ai.initiateAction(Action.createAction(Symbol.makeSymbol("QueryUnitBuildable"), v)); } catch (ExternalEnvironmentException e) { System.out.println("External environment exception."); } catch (ActionException e) { System.out.println("Action exception."); } } //end if turn_count++; return; } public void notifyOfSetup() { //This method intentionally left blank. } public void notifyOfCleanUp() { //This method intentionally left blank. } public Range[] getMetrics() { return METRICS; } public Object measure(Symbol metric) { if (metric.equals(SUCCESS_METRIC)) { return this.iSuccess; } else { throw new InvalidArgumentException(); } } /** * Returns a factory object that builds CTP2Agent objects. * @return a factory object that builds CTP2Agent objects. */ public static final AgentBuilder getBuilder() { return new AgentBuilder() { public Agent build() { return new CTP2Agent(); } public Range[] getMetrics() { return METRICS; } public void setInitializationParameter(Symbol paramName, Object value) { throw new InvalidArgumentException(); } public Object getInitializationParameterValue(Symbol paramName) { throw new InvalidArgumentException(); } public Range[] getInitializationParameters() { return Range.EMPTY_ARRAY; } public Symbol getType() { return CTP2_AGENT_TYPE; } public boolean canHandleEnvironment(EnvironmentBuilder eb) { return eb.getType() == Main.TYPE_CTP2_GAME; } }; } @Override public Symbol getType() { return CTP2_AGENT_TYPE; } }