Skip to content

Commit

Permalink
Merge pull request #113 from 2AA4-W24/salma
Browse files Browse the repository at this point in the history
Tried to get it to find a creek
  • Loading branch information
hydrowoxy authored Mar 18, 2024
2 parents 0a32505 + 1e7046b commit 860a097
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 15 deletions.
97 changes: 89 additions & 8 deletions src/main/java/ca/mcmaster/se2aa4/island/team101/AirDecision.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ public class AirDecision extends Decision {
private Command command;
private GenericResponse response;
private Compass compass;
private int counter = 0, distanceToEdge = 0, eta = 0, stage = 0;
private int counter = 0, distanceToEdge = 0, eta = 0, stage = 0, counter2=0;;
private int edge=0;
private int distanceToLand;
Boolean facingLand=false, atLand=false, scanComplete=false;
String newDirection;
AreaMap map;

public AirDecision(Drone drone) {
super(drone);
this.compass = drone.getCompass();
this.map = drone.getMap();
//this.response = response;
}

Expand Down Expand Up @@ -68,11 +70,6 @@ else if (eta == distanceToLand){ // if we are at the island, scan
}

if (distanceToEdge < edge){
// just for the mvp, it checks for land and returns home immediately
// ideally, this is put into a method, but since its just temporary, it'll just be done in the if statement
// need to implement a drone.goHomeCost() or something to figure out when to return, its being simulated by a simple counter for now
// decision.put("action", "stop");

if (facingLand && eta < distanceToLand){ // if facing the land and not yet at land, then fly forward
command.fly();
eta++;
Expand All @@ -86,8 +83,9 @@ else if (eta == distanceToLand){ // if we are at the island, scan
return command.toString();
}
if (scanComplete){ // STOP IF SCANNED
command.stop();
return command.toString();
// command.stop();
//return command.toString();
return creekSearch();
}

// FOUR STAGES WHEN SEARCHING
Expand Down Expand Up @@ -145,6 +143,89 @@ else if (stage % 4 == 2){
return command.toString();
}

private String creekSearch(){
logger.info("************************IN CREEKSEARCH");
command = new Command();

// if the current tile is not null then stop
if(!(map.getTile(compass.getPosition()).isEmpty())){
logger.info("***CURRENT TILE IS NOT EMPTY SO STOPPING***");
logger.info("**CURRENT TILE CREEK ID: " + map.getTile(compass.getPosition()).getCreekID());
logger.info("**CURRENT TILE SITE ID: " + map.getTile(compass.getPosition()).getSiteID());
command.stop();
return command.toString();
}

logger.info("***CURRENT TILE IS EMPTY CONTINUE***");

// logger.info(compass.getDirection());
// logger.info(counter);

// SATE 0:
// ECHO TO SEE IF YOU CAN FLY FORWARDS
if (counter2==0){
logger.info("************************* STATE 0");
logger.info("**COUNTER2 = " + counter2);
// should add something to scan if ur current tile is over land idk
// so you dont miss the first tile after land finding phase bc rn itll just immediately echo then
// go fwd if it wont be sending itself into the ocean

// echo forwards
command.echo(compass.getDirection());
counter2=1;
return command.toString();
}
// STATE 1:
// FLY FORWARDS, IF YOU CAN
// OTHERWISE ECHO LEFT
else if (counter2==1){
logger.info("************************* STATE 1");
logger.info("**COUNTER2 = " + counter2);
if (((EchoResponse)response).getFound().equals("GROUND")){
// if u see ground fly forwards
command.fly();
counter2=3; // straight to scanning state
}else if (((EchoResponse)response).getFound().equals("OCEAN")){
// otherwise check left
command.echo(compass.getLeft());
counter2=2;
}

return command.toString();
}
// STATE 2:
// YOU JUST ECHOED LEFT
else if (counter2==2){
logger.info("************************* STATE 2");
logger.info("**COUNTER2 = " + counter2);
if (((EchoResponse)response).getFound().equals("GROUND")){
// If you saw ground turn left
command.heading(compass.getLeft());
// bc of the weird turns idk if this will send it off land or not but it should be minimal error not a big issue rn
}else if (((EchoResponse)response).getFound().equals("OCEAN")){
// if you didn't see land you need to go right it's the only other option
command.heading(compass.getRight());
}
counter2=3;
return command.toString();
}
// STATE 3:
// SCANNING
else if (counter2==3){
logger.info("************************* STATE 3");
logger.info("**COUNTER2 = " + counter2);
command.scan();
// map should auto update bc of the code in drone update for whenever it recieves a scan response
counter2=0; // back to the state of looking in front of you
return command.toString();
}

// need a way to stop it maybe a state 4 once all the tiles are scanned but idk how to tell if they are all scanned

// it just yells at me if i don't have this ugh idk
return command.toString();
}

public String decideLand() {
command.scan();
return command.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class AreaMap {
List<String> rows = new ArrayList<String>();
HashMap<Point, Tile> map;

public AreaMap(){
map = new HashMap<>();
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/ca/mcmaster/se2aa4/island/team101/Drone.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Drone extends Traveler {
private final Logger logger = LogManager.getLogger(Drone.class);
private Integer charge;
private Decision nextMove;
private Compass compass;
private Compass compass; // this has the position
private AreaMap map;
private String lastCommand;
private Response<?> lastResponse;
Expand All @@ -23,7 +23,7 @@ public Drone(JSONInitialization initializer) {
}

@Override
public void setNextMove(String command) {
public void setPrevMove(String command) {
lastCommand = command;
}

Expand All @@ -34,18 +34,24 @@ public String getLastMove() {

@Override
public void update(Response<?> response) {

logger.info("IN DRONE UPDATE");
lastResponse = response;

// Handle the response based on the command type
GenericResponse typedResponse = response.handleResponse();
setPrevMove(typedResponse.getType());
nextMove.updateResponse(typedResponse);

// Set the charge based on the response cost
setCharge(typedResponse.getCost());
logger.info("COST OF ACTION: " + typedResponse.getCost());

logger.info("LAST COMMAND WAS: " + lastCommand);

// Update the map only if it was a scan response
if (lastCommand.equals("scan")) {
logger.info("*******************LAST COMMAND WAS A SCAN**************************");
ScanResponse scanResponse = (ScanResponse) typedResponse;
map.updateMap(compass.getPosition(), scanResponse);
}
Expand Down Expand Up @@ -73,6 +79,10 @@ public Compass getCompass(){
return compass;
}

public AreaMap getMap(){
return map;
}

private void setCharge(Integer cost) {
logger.info("BATTERY WAS: " + charge);
charge -= cost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
public class EchoResponse extends GenericResponse {
private final int range;
private final String found;
private String type;

public EchoResponse(int cost, String status, int range, String found) {
super(cost, status);
this.range = range;
this.found = found;
this.type = "echo";
}

public int getRange() {
Expand All @@ -17,4 +19,8 @@ public int getRange() {
public String getFound() {
return found;
}

public String getType(){
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
public class GenericResponse {
protected final int cost;
protected final String status;
private String type;

public GenericResponse(int cost, String status) {
this.cost = cost;
this.status = status;
this.type = "fly";// THIS NEEDS TO CHANGE BC GENERIC ISNT ALWAYS FLY JUST HARD CODING FOR NOW
}

public int getCost() {
Expand All @@ -16,4 +18,8 @@ public int getCost() {
public String getStatus() {
return status;
}

public String getType(){
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ public class ScanResponse extends GenericResponse {
private final JSONArray biomes;
private final JSONArray creeks;
private final JSONArray sites;
private final String type;

public ScanResponse(int cost, String status, JSONArray biomes, JSONArray creeks, JSONArray sites) {
super(cost, status);
this.biomes = biomes;
this.creeks = creeks;
this.sites = sites;
this.type = "scan";
}

public JSONArray getBiomes() {
Expand All @@ -25,4 +27,8 @@ public JSONArray getCreeks() {
public JSONArray getSites() {
return sites;
}

public String getType(){
return type;
}
}
10 changes: 7 additions & 3 deletions src/main/java/ca/mcmaster/se2aa4/island/team101/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Tile {
private String siteID;

public Tile(){
this.creekID = "";
this.siteID = "";
this.creekID = null;
this.siteID = null;
}

public String getCreekID(){
Expand Down Expand Up @@ -47,4 +47,8 @@ public void fillTile(ScanResponse scanResponse) {
this.siteID = sites.getString(0);
}
}
}

public boolean isEmpty(){
return siteID == null && creekID == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public String getLastMove(){
return nextMoveStr;
}

public abstract void setNextMove(String command);
public abstract void setPrevMove(String command);

public abstract void update(Response<?> response);

Expand Down

0 comments on commit 860a097

Please sign in to comment.