Package org.freerealm.executor

Examples of org.freerealm.executor.CommandResult


     * @param realm Realm to execute the command
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        if (storer.getResourceQuantity(resource) < amount) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        storer.setResourceQuantity(resource, storer.getResourceQuantity(resource) - amount);
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here


     */
    public CommandResult execute(Realm realm) {
        boolean tileHasAllowMovement = false;
        Move moveAbility = (Move) unit.getType().getAbility("Move");
        if (moveAbility == null) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        Tile toTile = realm.getTile(coordinate);
        Iterator<TileImprovementType> iterator = toTile.getImprovementsIterator();
        while (iterator.hasNext()) {
            TileImprovementType tileImprovement = iterator.next();
            if (tileImprovement.hasPropertyNamed("AllowMovement")) {
                tileHasAllowMovement = true;
                break;
            }
        }
        if (!moveAbility.hasTileType(toTile.getType()) && !tileHasAllowMovement) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        Vector<Coordinate> adjacentTiles = Utility.getNeighborCoordinates(realm, unit.getCoordinate());
        if (!adjacentTiles.contains(coordinate)) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        if (unit.getMovementPoints() <= 0) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        if (unit.getMovementPoints() > 0 && unit.getMovementPoints() < 1 && toTile.getMovementCost() >= 1) {
            return new CommandResult(CommandResult.RESULT_ERROR, "");
        }
        if (toTile.getNumberOfUnits() > 0) {
            Unit defendingUnit = toTile.getUnits().get(toTile.getUnits().firstKey());
            if (!unit.getPlayer().equals(defendingUnit.getPlayer())) {
                return new CommandResult(CommandResult.RESULT_ERROR, "Tile contains a unit that does not belong to player. Use AttackTileCommand.");
            }
        }

        if ((toTile.getSettlement() != null) && (!unit.getPlayer().equals((toTile.getSettlement().getPlayer())))) {
            return new CommandResult(CommandResult.RESULT_ERROR, "Tile contains a settlement that does not belong to player. Use InvadeCityCommand.");
        }

        Coordinate oldCoordinate = unit.getCoordinate();
        realm.getWorldMap().removeUnit(unit);
        unit.setCoordinate(coordinate);
        realm.getWorldMap().addUnit(unit);
        float movementCost = toTile.getMovementCost();
        float newMovementPoints = (unit.getMovementPoints() >= movementCost ? unit.getMovementPoints() - movementCost : 0);
        unit.setMovementPoints(newMovementPoints);
        manageExploration(realm);
        manageDiplomacy(realm);
        CommandResult commandResult = new CommandResult(CommandResult.RESULT_OK, "", CommandResult.UNIT_MOVEMENT_UPDATE);
        commandResult.addParameter(unit);
        commandResult.addParameter(oldCoordinate);
        commandResult.addParameter(coordinate);
        return commandResult;
    }
View Full Code Here

     * @param realm Realm to execute the command
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        if (unit.getCoordinate().equals(coordinate)) {
            return new CommandResult(CommandResult.RESULT_OK, "");
        }
        PathFinder pathFinder = realm.getPathFinder();
        if (pathFinder == null) {
            return new CommandResult(CommandResult.RESULT_ERROR, "PathFinder for realm is null.");
        }
        Path path = pathFinder.findPath(unit, coordinate, false);
        if (path == null) {
            String errorMessage = "There is not any path from unit's current location to target tile\n";
            errorMessage = errorMessage + "Units location : " + unit.getCoordinate();
            errorMessage = errorMessage + " Target coordinate :" + coordinate;
            return new CommandResult(CommandResult.RESULT_ERROR, errorMessage);
        }
        int i = 0;
        while ((unit.getMovementPoints() > 0) && (i < path.getLength())) {
            Coordinate pathCoordinate = path.getStep(i);
            MoveUnitCommand moveUnitCommand = new MoveUnitCommand(unit, pathCoordinate);
            CommandResult moveCommandResult = Executor.getInstance().execute(moveUnitCommand);
            if (moveCommandResult.getCode() == CommandResult.RESULT_ERROR) {
                return new CommandResult(CommandResult.RESULT_ERROR, "A move command returned an error. Error : " + moveCommandResult.getText());
            }
            i++;
        }
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here

        }
        playerIterator = realm.getPlayerManager().getPlayersIterator();
        while (playerIterator.hasNext()) {
            managePlayerHistory(playerIterator.next());
        }
        return new CommandResult(CommandResult.RESULT_OK, "", CommandResult.TURN_ENDED_UPDATE);
    }
View Full Code Here

     * Executes command and removes given unit.
     * @param realm Realm to execute the command
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        CommandResult commandResult = Executor.getInstance().execute(new RemoveUnitFromPlayerCommand(player, unit));
        if (commandResult.getCode() != CommandResult.RESULT_OK) {
            return commandResult;
        }
        if (unit.getStatus() == Unit.UNIT_ACTIVE) {
            Executor.getInstance().execute(new RemoveUnitFromWorldMapCommand(player, unit));
        }
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here

     * @param realm Realm to execute the command
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        if ((cityName == null) || (cityName.trim().equals(""))) {
            return new CommandResult(CommandResult.RESULT_ERROR, "City name cannot be empty", CommandResult.NO_UPDATE);
        }
        if (getBuildCityProperty(unit) == null) {
            return new CommandResult(CommandResult.RESULT_ERROR, "No BuildCity ability", CommandResult.NO_UPDATE);
        }
        if (realm.getTile(unit.getCoordinate()).getSettlement() != null) {
            return new CommandResult(CommandResult.RESULT_ERROR, "Tile alredy contains a settlement", CommandResult.NO_UPDATE);
        }
        Settlement settlement = new Settlement(realm);
        settlement.setCoordinate(unit.getCoordinate());
        settlement.setPlayer(unit.getPlayer());
        settlement.setName(cityName);
        int cityFoundingPopulation = Integer.parseInt(realm.getProperty("city_founding_population"));
        settlement.setPopulation(cityFoundingPopulation);
        unit.getPlayer().addSettlement(settlement);
        Executor.getInstance().execute(new RemoveUnitCommand(unit.getPlayer(), unit));
        realm.getWorldMap().addSettlement(settlement);
        CommandResult commandResult = new CommandResult(CommandResult.RESULT_OK, "", CommandResult.NEW_SETTLEMENT_UPDATE);
        commandResult.addParameter(settlement);
        return commandResult;
    }
View Full Code Here

    public CommandResult execute(Realm realm) {
        int availableWorkers = settlement.getProductionWorkforce() + settlementImprovement.getNumberOfWorkers();
        int workersToAssign = (availableWorkers > workers ? workers : availableWorkers);
        workersToAssign = (settlementImprovement.getType().getMaximumWorkers() > workersToAssign ? workersToAssign : settlementImprovement.getType().getMaximumWorkers());
        settlementImprovement.setNumberOfWorkers(workersToAssign);
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here

     * @param realm
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        (new RealmXMLWrapper(realm)).initializeFromNode(realm, node);
        return new CommandResult(CommandResult.RESULT_OK, "", CommandResult.REALM_INITIALIZE_UPDATE);
    }
View Full Code Here

     * @param realm Realm to execute the command
     * @return CommandResult
     */
    public CommandResult execute(Realm realm) {
        player.setStatus(status);
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here

        unit.getPlayer().addExploredCoordinate(unit.getCoordinate());
        for (int i = 1; i < unit.getType().getExplorationRadius() + 1; i++) {
            unit.getPlayer().addExploredCoordinates(realm.normalizeCoordinates(org.freerealm.Utility.getCircleCoordinates(coordinate, realm, i)));
        }
        realm.getWorldMap().addUnit(unit);
        return new CommandResult(CommandResult.RESULT_OK, "");
    }
View Full Code Here

TOP

Related Classes of org.freerealm.executor.CommandResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.