private void checkMainBase(FakeMessage msg) {
   EntityKnowledge mainBase = getMainBase();
   if ((mainBase != null) && (msg.getSender() == mainBase.getID())) {
     x = (int) -(Math.cos(Math.toRadians(msg.getAngle())) * msg.getDistance());
     y = (int) -(Math.sin(Math.toRadians(msg.getAngle())) * msg.getDistance());
   }
 }
 /**
  * Renvoie la direction vers une entité.
  *
  * @param id L'id de l'entité vers laquelle on se dirige.
  * @return
  */
 public int getDistance(int id) {
   int dst = 0;
   EntityKnowledge ke = getEntity(id);
   if (ke != null) {
     dst = (int) Math.sqrt((ke.getY() - y) * (ke.getY() - y) + (ke.getX() - x) * (ke.getX() - x));
     return dst;
   }
   return 0;
 }
 /**
  * Renvoie la direction vers une entité.
  *
  * @param id L'id de l'entité vers laquelle on se dirige.
  * @return
  */
 public int getHeading(int id) {
   int angle = 0;
   EntityKnowledge ke = getEntity(id);
   if (ke != null) {
     double radian = Math.atan2((ke.getY() - y), (ke.getX() - x));
     angle = (int) Math.toDegrees(radian);
     return angle;
   }
   return 0;
 }
 public List<EntityKnowledge> getTeam(int id) {
   List<EntityKnowledge> lek = new LinkedList<EntityKnowledge>();
   for (SortedMap<Integer, EntityKnowledge> map : allies.values()) {
     for (EntityKnowledge ke : map.values()) {
       if (ke.getTeamNumber() == id) {
         lek.add(ke);
       }
     }
   }
   return lek;
 }
 /**
  * Renvoie le nombre de base dans l'équipe.
  *
  * @return
  */
 public int getAlliedBaseCount() {
   SortedMap<Integer, EntityKnowledge> a = allies.get(Names.BASE);
   if (a == null) {
     return 0;
   }
   int count = 0;
   for (EntityKnowledge ek : a.values()) {
     if (ek.getLastUpdateDuration(tick) < 2) {
       count++;
     }
   }
   return count;
 }
  public EntityKnowledge getNearestEnnemyBase() {
    EntityKnowledge res = null;

    SortedMap<Integer, EntityKnowledge> map = ennemies.get(Names.BASE);
    if (map != null) {
      for (EntityKnowledge entity : map.values()) {
        if ((res == null) || (getDistance(res.getID()) > getDistance(entity.getID()))) {
          res = entity;
        }
      }
    }

    return res;
  }
 public EntityKnowledge getNearestEnnemy() {
   EntityKnowledge nearest = null;
   int minDistance = Integer.MAX_VALUE;
   for (Map<Integer, EntityKnowledge> map : ennemies.values()) {
     for (EntityKnowledge ek : map.values()) {
       if (ek.getLastUpdateDuration(tick) < 2) {
         int distance = ek.getDistance(getX(), getY());
         if (distance < minDistance) {
           minDistance = distance;
           nearest = ek;
         }
       }
     }
   }
   return nearest;
 }
 public EntityKnowledge getEntity(int id) {
   for (SortedMap<Integer, EntityKnowledge> map : allies.values()) {
     for (EntityKnowledge ke : map.values()) {
       if (ke.getID() == id) {
         return ke;
       }
     }
   }
   for (SortedMap<Integer, EntityKnowledge> map : ennemies.values()) {
     for (EntityKnowledge ke : map.values()) {
       if (ke.getID() == id) {
         return ke;
       }
     }
   }
   return null;
 }
  public void processMessage(FakeMessage msg) {
    if (msg.getMessage().equals("alive")) {
      SortedMap<Integer, EntityKnowledge> a = allies.get(msg.getSenderType());
      if (a == null) {
        a = new TreeMap<Integer, EntityKnowledge>();
        allies.put(msg.getSenderType(), a);
      }
      EntityKnowledge ent = a.get(msg.getSender());
      if (ent == null) {
        ent = new EntityKnowledge(msg, getTick());
        a.put(msg.getSender(), ent);
      } else {
        ent.update(msg, getTick());
      }
      if (msg.getSenderType().equals(Names.BASE)) {
        checkMainBase(msg);
      }
    } else if (msg.getMessage().equals("ennemy")) {
      String[] content = msg.getContent();
      int id = Integer.parseInt(content[0]);
      String type = content[2];

      SortedMap<Integer, EntityKnowledge> a = ennemies.get(type);
      if (a == null) {
        a = new TreeMap<Integer, EntityKnowledge>();
        ennemies.put(type, a);
      }
      EntityKnowledge ent = a.get(id);
      if (ent == null) {
        ent = new EntityKnowledge(msg, getTick());
        a.put(id, ent);
      } else {
        ent.update(msg, getTick());
      }
    } else if (msg.getMessage().equals("beginContract")) {
      String[] content = msg.getContent();
      setActiveContract(content[0], Integer.parseInt(content[1]));
    } else if (msg.getMessage().equals("endContract")) {
      String[] content = msg.getContent();
      setActiveContract(content[0], -1);
    }
  }
  public double getBestShootAngle(EntityKnowledge target) {
    /*double nbTick = 6.0; // Le nombre de tick d'avance que l'on calcule

    double speed = 0.0;
    if (target.getType().equals(Names.EXPLORER)) {
    	speed = WarExplorer.SPEED;
    } else if (target.getType().equals(Names.ROCKET_LAUNCHER)) {
    	speed = WarRocketLauncher.SPEED;
    }

    int dx = (int) (Math.cos(Math.toRadians(target.getHeading())) * speed * nbTick);
    int dy = (int) (Math.sin(Math.toRadians(target.getHeading())) * speed * nbTick);
    System.out.println(dx + " " + dy + " " + speed);*/

    int dx = 0;
    int dy = 0;

    Point p = new Point(target.getX() + dx, target.getY() + dy);
    Point here = new Point(x, y);

    return here.heading(p);
  }
  public EntityKnowledge getBestTarget() {
    int radius = (int) (WarRocket.AUTONOMY * WarRocket.SPEED);
    EntityKnowledge target = null;

    for (Map<Integer, EntityKnowledge> map : ennemies.values()) {
      for (EntityKnowledge ek : map.values()) {
        if ((ek.getLastUpdateDuration(tick) < 5) && (ek.getDistance(x, y) < radius)) {
          if ((target == null) || (ek.getEnergy() < target.getEnergy())) {
            target = ek;
          }
        }
      }
    }
    return target;
  }
 public boolean isInformationReliable(EntityKnowledge e) {
   return !(e.getTick() + 4 < tick);
 }