Skip to content

Commit

Permalink
Función que actualiza el mapa global a partir de las percepciones rec…
Browse files Browse the repository at this point in the history
…ibidas de un agente vehiculo.

Otras funciones varias útiles.
  • Loading branch information
jorge committed Dec 29, 2017
1 parent b7fb95a commit 3c716d0
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 12 deletions.
176 changes: 167 additions & 9 deletions DBAP3/src/gugelcar/AgenteMapa.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import es.upv.dsic.gti_ia.core.ACLMessage;
import es.upv.dsic.gti_ia.core.AgentID;
import es.upv.dsic.gti_ia.core.SingleAgent;
import gugelcar.exceptions.ExceptionBadParam;
import gugelcar.exceptions.ExceptionNonInitialized;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -34,6 +36,7 @@ public class AgenteMapa extends SingleAgent{
private final String nameMap;
private boolean objetivo_encontrado;
private int iteracion;
private int bateriaGlobal;

//Comunicacion
private final JSON jsonobj;
Expand All @@ -47,16 +50,17 @@ public class AgenteMapa extends SingleAgent{
* @param aid3 el id del vehiculo 3
* @param aid4 el id del vehiculo 4
* @brief Constructor
* @author Javier Bejar Mendez, Emilien Giard, Dani
* @author Javier Bejar Mendez, Emilien Giard, Dani, Jorge
*/
public AgenteMapa(AgentID aid, String nameMap, AgentID controlador_id, AgentID aid1, AgentID aid2, AgentID aid3, AgentID aid4) throws Exception{
public AgenteMapa(AgentID aid, String nameMap, AgentID controlador_id, AgentID aid1, AgentID aid2, AgentID aid3, AgentID aid4, int bateriaGlobal) throws Exception{
super(aid);
this.aid1 = aid1;
this.aid2 = aid2;
this.aid3 = aid3;
this.aid4 = aid4;
this.nameMap = nameMap;
this.controlador_id = controlador_id;
this.bateriaGlobal = bateriaGlobal;
jsonobj = new JSON();

objetivo_encontrado = false; // TODO: Cargar de disco si hemos encontrado el
Expand Down Expand Up @@ -112,16 +116,169 @@ private void enviarConversationID() {
/**
* @param percepciones de un vehiculo
* @brief Metodo que actualiza la mapa en funcion de los percepciones de un vehiculo
* @author Emilien Giard
* @author Emilien Giard, Jorge
*/
private void updateMap(Integer[][] percepciones) {
private void updateMap(Integer[][] percepciones, Posicion posicion, AgentType a) throws ExceptionNonInitialized, ExceptionBadParam {
String agente = a.toString();
Posicion aux = posicion;

switch (agente){
case "Car":

aux.setX(posicion.getX() - 1);
aux.setY(posicion.getY() - 1);
for(int i = 0; i < 3; i++){
this.map.set(aux, percepciones[0][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX());
aux.setY(posicion.getY()-1);
for(int i = 0; i < 3; i++){
this.map.set(aux, percepciones[1][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 1);
aux.setY(posicion.getY() - 1);
for(int i = 0; i < 3; i++){
this.map.set(aux, percepciones[2][i]);
aux.setY(aux.getY()+1);
}

break;
case "Drone":

aux.setX(posicion.getX() - 2);
aux.setY(posicion.getY() - 2);
for(int i = 0; i < 5; i++){
this.map.set(aux, percepciones[0][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX()-1);
aux.setY(posicion.getY()-2);
for(int i = 0; i < 5; i++){
this.map.set(aux, percepciones[1][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX());
aux.setY(posicion.getY() - 2);
for(int i = 0; i < 5; i++){
this.map.set(aux, percepciones[2][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 1);
aux.setY(posicion.getY() - 2);
for(int i = 0; i < 5; i++){
this.map.set(aux, percepciones[3][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 2);
aux.setY(posicion.getY() - 2);
for(int i = 0; i < 5; i++){
this.map.set(aux, percepciones[4][i]);
aux.setY(aux.getY()+1);
}
break;
case "Truck":

aux.setX(posicion.getX() - 5);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[0][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX()-4);
aux.setY(posicion.getY()-5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[1][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() - 3);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[2][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() - 2);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[3][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() - 1);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[4][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX());
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[5][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() +1);
aux.setY(posicion.getY() -5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[6][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 2);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[7][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 3);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[8][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 4);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[9][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 5);
aux.setY(posicion.getY() - 5);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[10][i]);
aux.setY(aux.getY()+1);
}

aux.setX(posicion.getX() + 2);
aux.setY(posicion.getY() - 2);
for(int i = 0; i < 11; i++){
this.map.set(aux, percepciones[11][i]);
aux.setY(aux.getY()+1);
}
break;
}
}

/**
* @brief Metodo que envia la mapa global a un vehiculo
* @author Emilien Giard
* @brief Metodo que envia el mapa global a un vehiculo
* @author Emilien Giard, Jorge
*/
private void enviarMapa() {
private Mapa enviarMapa() {
return map;
}

/**
Expand Down Expand Up @@ -173,7 +330,7 @@ private ACLMessage crearMensaje(AgentID sender, AgentID receiver, int performati

/**
* Metodo que crea los vehiculos y recibe los messajes de los vehiculos
* @author Emilien Giard, Javier Bejar Mendez, Dani
* @author Emilien Giard, Javier Bejar Mendez, Dani, Jorge
*/
@Override
public void execute(){
Expand Down Expand Up @@ -210,6 +367,7 @@ public void execute(){
// this.enviarMapa();
break;
case "export-map":
this.exportarMapa();
break;
default:
ACLMessage outbox = new ACLMessage();
Expand Down Expand Up @@ -251,7 +409,7 @@ private JSONObject importarMapa(){
* Exporta el mapa en un archivo llamado "mapa.json"
* @author Jorge
*/
private void actualizaDatosMapa(){
private void actualizaDatosMapaImportado(){
JSONObject obj = this.importarMapa();
this.map.setTam((int)obj.get("tamaño"));
this.map.setMapa((Integer[][])obj.get("mapa"));
Expand Down
12 changes: 12 additions & 0 deletions DBAP3/src/gugelcar/AgenteVehiculo.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,16 @@ public Posicion getServerPos(){
public boolean getEstoyEnObjetivo(){
return this.estoy_en_objetivo;
}

/**
* @brief Devuelve true si tiene batería para moverse, false en caso contrario
* @author Jorge
*/
public boolean puedoMoverme(){
if(this.bateria > this.fuelrate)
return true;
else return false;
}


}
9 changes: 6 additions & 3 deletions DBAP3/src/gugelcar/Mapa.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
public class Mapa {
private Integer[][] mapa;
private Integer TAM;

//Constructor

/**
* @brief Constructor, inicializa cada casilla del mapa a -1
* @author Javier Bejar Mendez
*/
public Mapa(Integer tam) throws ExceptionBadParam{
if(tam < 0){
throw new ExceptionBadParam("Error al construir mapa, parametro menores que 0");
Expand All @@ -23,7 +26,7 @@ public Mapa(Integer tam) throws ExceptionBadParam{
this.mapa = new Integer[TAM][TAM];
for(int i = 0; i < tam; ++i){
for(int j = 0; j < tam; ++j){
this.mapa[i][j] = 0;
this.mapa[i][j] = -1;
}
}

Expand Down
1 change: 1 addition & 0 deletions DBAP3/src/gugelcar/main.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void main(String[] args) {
vehiculo3_id = new AgentID(vehiculo3_name);
vehiculo4_id = new AgentID(vehiculo4_name);

//Da error ya que hay que añadir la batería global.
//Creacion de los Agentes
AgenteMapa agente_mapa = new AgenteMapa(agente_mapa_id, mapName, controlador_id,
vehiculo1_id, vehiculo2_id, vehiculo3_id, vehiculo4_id);
Expand Down

0 comments on commit 3c716d0

Please sign in to comment.