Exemplo n.º 1
0
  private boolean executeMovementAgent(Agent a, int wantsToMove) {

    int currentLocation = a.getLocation();
    int xCurrent = this.calcXFromCoor(currentLocation);
    int yCurrent = this.calcYFromCoor(currentLocation);
    int xWants = this.calcXFromCoor(wantsToMove);
    int yWants = this.calcYFromCoor(wantsToMove);

    boolean isRed = a.isRed();

    // check if this is a valid movement request, otherwhise crash the game
    // check if wantsToMove is in bounds

    if (wantsToMove < 0 || wantsToMove >= this.gridIdentifier.length) {
      System.err.println("REQUESTED MOVE OUT OF BOUNDS. EXITING.");
      System.exit(-1);
    }

    // we can either move up, down, left or right, just check the four cases
    // crash the game if the move is not one of these choices
    if (!((xCurrent == xWants - 1 && yCurrent == yWants)
        || (xCurrent == xWants + 1 && yCurrent == yWants)
        || (yCurrent == yWants + 1 && xCurrent == xWants)
        || (yCurrent == yWants - 1 && xCurrent == xWants))) {

      System.err.println(xCurrent);
      System.err.println(yCurrent);
      System.err.println(xWants);
      System.err.println(yWants);
      System.err.println("REQUESTED MOVE NOT UP,DOWN,RIGHT OR LEFT.");
      System.exit(-1);
    }

    // move is not legal because there is a rock
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.ROCK_TILE)) {
      System.err.println("REQUESTED TO MOVE ON ROCK SQUARE.");
      System.exit(-1);
    }

    // move is also illegal if you're tyring to move into the enemies camp

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE) && isRed)
        || (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE) && !isRed)) {
      System.err.println("REQUESTED TO MOVE ON ENEMIES CAMP.");
      System.exit(-1);
    }

    // I guess if we get here, we can actually move!
    // Then there are three options, either the goto tile is a coin or else
    // its a grass field or its own camp

    // if its a camp, the sprite shouldnt move, but if is carrying a coin it
    // should drop of the coin and the movement list should go empty
    if (isRed && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE))) {

      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }
      a.clearMovementList();
      return false;
    } else if (!isRed
        && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE))) {
      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }

      a.clearMovementList();
      return false;
    }

    // if its a coin, two things can happen, either the actor is already
    // carrying a coin, in which case the coin being held is dropped at the
    // current location of the actor, then the actor moves to destination
    // and removed the coin there and puts it in inventory
    // else if he is not carrying anything, he removes the coin and moves to
    // the target square
    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.COIN_TILE))) {
      int carrying = a.getCarrying();

      // if he is carrying something, drop the coint
      if (carrying != -1) {
        // current location becomes a coin
        gridIdentifier[currentLocation] = mapper.get(Tile.COIN_TILE);
        // update coint location of coin dropped
        this.coinLocations.put(currentLocation, carrying);
      } else {
        gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      }
      // targetlocation becomes sprite (red or blue)
      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      // pick up the coin
      int pickup = this.coinLocations.get(wantsToMove);
      // give it to sprite
      a.setCarrying(pickup);
      // remove it from coint map
      this.coinLocations.remove(wantsToMove);
      // remove the action
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.GRASS_TILE))) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();

      a.setLocation(wantsToMove);

      return true;
    }

    // this is still fatal if there is a head on collision, it is easier to
    // just allow for passing movements
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_SPRITE_TILE)
        || gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_SPRITE_TILE)) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    throw new RuntimeErrorException(new Error("Ran out of options."));
  }