Esempio n. 1
0
  /**
   * Executes the task. Handles exceptions. Subclasses should implement doExecute() instead.
   *
   * @param space The environment in which the task is executing.
   * @param obj The object that is executing the task.
   * @param progress The time that has passed according to the environment executor.
   */
  public void execute(
      IEnvironmentSpace space, ISpaceObject obj, long progress, IClockService clock) {
    ISpaceObject target = (ISpaceObject) getProperty(PROPERTY_TARGET);

    IVector2 loc = (IVector2) obj.getProperty(Space2D.PROPERTY_POSITION);
    IVector2 tloc = (IVector2) target.getProperty(Space2D.PROPERTY_POSITION);
    if (!loc.equals(tloc)) throw new RuntimeException("Not at location: " + obj + ", " + target);

    int ore = ((Number) target.getProperty(AnalyzeTargetTask.PROPERTY_ORE)).intValue();
    int capacity = ((Number) target.getProperty(PROPERTY_CAPACITY)).intValue();
    capacity += Math.min(ore, (time + progress) / TIME);
    ore -= Math.min(ore, (time + progress) / TIME);
    time = (time + progress) % TIME;
    target.setProperty(AnalyzeTargetTask.PROPERTY_ORE, new Integer(ore));
    target.setProperty(PROPERTY_CAPACITY, new Integer(capacity));

    if (ore == 0) {
      setFinished(space, obj, true);
    }
  }
Esempio n. 2
0
  /**
   * Performs the action.
   *
   * @param parameters parameters for the action
   * @param space the environment space
   * @return action return value
   */
  public Object perform(Map parameters, IEnvironmentSpace space) {
    //		System.out.println("move action: "+parameters);

    Grid2D grid = (Grid2D) space;
    IComponentIdentifier actor = (IComponentIdentifier) parameters.get(ISpaceAction.ACTOR_ID);
    String direction = (String) parameters.get(PARAMETER_DIRECTION);
    ISpaceObject avatar = grid.getAvatar(actor);

    if (null == space.getSpaceObject(avatar.getId())) {
      throw new RuntimeException("No such object in space: " + avatar);
    }

    IVector2 pos = (IVector2) avatar.getProperty(Space2D.PROPERTY_POSITION);
    boolean skip = false;
    if (DIRECTION_LEFT.equals(direction)) {
      pos = new Vector2Int(pos.getXAsInteger() - 1, pos.getYAsInteger());
    } else if (DIRECTION_RIGHT.equals(direction)) {
      pos = new Vector2Int(pos.getXAsInteger() + 1, pos.getYAsInteger());
    } else if (DIRECTION_UP.equals(direction)) {
      pos = new Vector2Int(pos.getXAsInteger(), pos.getYAsInteger() - 1);
    } else if (DIRECTION_DOWN.equals(direction)) {
      pos = new Vector2Int(pos.getXAsInteger(), pos.getYAsInteger() + 1);
    } else if (DIRECTION_NONE.equals(direction)) {
      skip = true;
    } else {
      throw new RuntimeException("Unknown move direction: " + direction);
    }

    if (!skip) {
      Collection obstacles = grid.getSpaceObjectsByGridPosition(pos, "obstacle");
      if (obstacles != null && !obstacles.isEmpty()) {
        throw new RuntimeException(
            "Cannot move '" + direction + "' due to obstacles: " + obstacles);
      }

      // Preys can not "tunnel" through hunters, i.e. move from the field
      // where the hunter is now to the field where the hunter was before.
      if (avatar.getType().equals("prey")) {
        Collection hunters =
            grid.getSpaceObjectsByGridPosition(
                (IVector2) avatar.getProperty(Space2D.PROPERTY_POSITION), "hunter");
        if (hunters != null) {
          pos = grid.adjustPosition(pos); // Hack!!! Position only converted in setPosition().
          for (Iterator it = hunters.iterator(); it.hasNext(); ) {
            ISpaceObject hunter = (ISpaceObject) it.next();
            if (pos.equals(hunter.getProperty(PROPERTY_LASTPOS))) {
              //							System.out.println("Cannot move '"+direction+"' due to hunter: "+hunter);
              throw new RuntimeException(
                  "Cannot move '" + direction + "' due to hunter: " + hunter);
            }
          }
        }
      }

      // Remember last position of hunter (required for detecting "tunneling").
      else if (avatar.getType().equals("hunter")) {
        avatar.setProperty(PROPERTY_LASTPOS, avatar.getProperty(Space2D.PROPERTY_POSITION));
      }

      grid.setPosition(avatar.getId(), pos);
    }

    return null;
  }