AirSeaRescueJason/src/java/searchDrone/get_direction.java

53 lines
2.1 KiB
Java

package searchDrone;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Atom;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.Term;
import jason.environment.grid.Location;
import operator.WorldEnvironment;
import operator.WorldModel;
public class get_direction extends DefaultInternalAction {
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] terms) throws Exception {
try {
String sAction = "skip";
int id = WorldEnvironment.getAgIdBasedOnName(ts.getAgArch().getAgName());
WorldModel model = WorldModel.get();
Location agent_loc =model.getAgPos(id);
int agent_x = agent_loc.x;
int agent_y = agent_loc.y;
int to_x = (int)((NumberTerm)terms[2]).solve();
int to_y = (int)((NumberTerm)terms[3]).solve();
if (model.inGrid(to_x,to_y)) {
Location agnet_location = new Location(agent_x, agent_y);
while (!model.isFree(to_x,to_y) && to_x > 0) to_x--;
while (!model.isFree(to_x,to_y) && to_x < model.getWidth()) to_x++;
while (!model.isFree(to_x,to_y) && to_y > 0) to_y--;
while (!model.isFree(to_x,to_y) && to_y < model.getHeight()) to_y++;
if(to_x < agent_x && model.isFree(agent_x-1,agent_y)){
sAction = "left";
}else if (to_x > agent_x && model.isFree(agent_x+1,agent_y)){
sAction = "right";
}
if (to_y > agent_y && model.isFree(agent_x,agent_y+1) || sAction.equals("skip") && model.isFree(agent_x,agent_y+1) && to_y >= agent_y) {
sAction = "down";
} else if (to_y < agent_y && model.isFree(agent_x,agent_y-1) || sAction.equals("skip") && model.isFree(agent_x,agent_y-1) && to_y <= agent_y){
sAction = "up";
}
}
return un.unifies(terms[4], new Atom(sAction));
} catch (Throwable e) {
e.printStackTrace();
return false;
}
}
}