package org.freerealm;
import org.freerealm.settlement.Settlement;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import org.freerealm.map.Coordinate;
import org.freerealm.map.Direction;
import java.util.Iterator;
import java.util.Properties;
import org.freerealm.tile.Tile;
import org.freerealm.player.Player;
/**
*
* @author Deniz ARIKAN
*/
public class Utility {
public static boolean isValidCoordinate(Realm realm, Coordinate coordinate) {
if ((coordinate.getOrdinate() < 0) || (coordinate.getOrdinate() >= realm.getMapHeight())) {
return false;
}
return true;
}
public static Coordinate getRelativeCoordinate(Realm realm, Coordinate coordinate, Direction direction) {
int newAbscissa = coordinate.getAbscissa();
int newOrdinate = coordinate.getOrdinate();
switch (direction.getId()) {
case 7:
newAbscissa = coordinate.getAbscissa() - 1 + (coordinate.getOrdinate() % 2);
newOrdinate = coordinate.getOrdinate() - 1;
break;
case 8:
newOrdinate = coordinate.getOrdinate() - 2;
break;
case 9:
newAbscissa = coordinate.getAbscissa() + (coordinate.getOrdinate() % 2);
newOrdinate = coordinate.getOrdinate() - 1;
break;
case 4:
newAbscissa = coordinate.getAbscissa() - 1;
break;
case 6:
newAbscissa = coordinate.getAbscissa() + 1;
break;
case 1:
newAbscissa = coordinate.getAbscissa() - 1 + (coordinate.getOrdinate() % 2);
newOrdinate = coordinate.getOrdinate() + 1;
break;
case 2:
newOrdinate = coordinate.getOrdinate() + 2;
break;
case 3:
newAbscissa = coordinate.getAbscissa() + (coordinate.getOrdinate() % 2);
newOrdinate = coordinate.getOrdinate() + 1;
break;
}
newAbscissa = (newAbscissa + realm.getMapWidth()) % realm.getMapWidth();
Coordinate relativeCoordinate = new Coordinate(newAbscissa, newOrdinate);
if (isValidCoordinate(realm, relativeCoordinate)) {
return relativeCoordinate;
} else {
return null;
}
}
public static Direction getDirection(Realm realm, Coordinate coordinate, Coordinate directionCoordinate) {
for (int i = 0; i < RealmConstants.directions.length; i++) {
Direction direction = RealmConstants.directions[i];
Coordinate tryCoordinate = Utility.getRelativeCoordinate(realm, coordinate, direction);
if (directionCoordinate.equals(tryCoordinate)) {
return direction;
}
}
return null;
}
public static Vector<Coordinate> getNeighborCoordinates(Realm realm, Coordinate coordinate) {
Vector<Coordinate> neighborCoordinates = new Vector<Coordinate>();
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.NORTHWEST));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.NORTH));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.NORTHEAST));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.EAST));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.WEST));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.SOUTHEAST));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.SOUTH));
neighborCoordinates.add(getRelativeCoordinate(realm, coordinate, RealmConstants.SOUTHWEST));
return neighborCoordinates;
}
public static Enumeration<Coordinate> getNeighborCoordinatesEnumeration(Realm realm, Coordinate coordinate) {
return getNeighborCoordinates(realm, coordinate).elements();
}
public static boolean isTileAvailable(Realm realm, Settlement settlement, Coordinate coordinate) {
boolean result = true;
Iterator<Player> playerIterator = realm.getPlayerManager().getPlayersIterator();
while (playerIterator.hasNext()) {
Player player = playerIterator.next();
Iterator<Settlement> citiesIterator = player.getSettlementsIterator();
while (citiesIterator.hasNext()) {
Settlement otherSettlement = citiesIterator.next();
if (!settlement.equals(otherSettlement)) {
if (otherSettlement.getCoordinate().equals(coordinate)) {
return false;
}
if (otherSettlement.getWorkForceManager().getAssignedWorkforceForTile(coordinate) != null) {
return false;
}
}
}
}
return result;
}
public static Properties getProperties(String filePath) {
Properties properties = null;
properties = new Properties();
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(ClassLoader.getSystemResourceAsStream(filePath));
properties.load(bufferedInputStream);
} catch (IOException exception) {
exception.printStackTrace();
}
return properties;
}
public static int calculatePercent(int value, int percent) {
float result = value;
result = (percent * value) / 100;
return (int) result;
}
public static int increaseByPercent(int value, int percent) {
float result = value;
result = ((100 + percent) * value) / 100;
return (int) result;
}
public static int getCoordinateDefenceBonus(Realm realm, Coordinate coordinate) {
int coordinateDefenceBonus = 0;
Tile tile = realm.getTile(coordinate);
coordinateDefenceBonus = tile.getType().getDefencePercentage();
if (tile.getSettlement() != null) {
coordinateDefenceBonus = coordinateDefenceBonus + tile.getSettlement().getDefenceModifier();
}
return coordinateDefenceBonus;
}
public static boolean areNeighboringCoordinates(Realm realm, Coordinate coordinate1, Coordinate coordinate2) {
return getCircleCoordinates(coordinate1, realm, 1).contains(coordinate2);
}
public static Vector<Coordinate> getCircleCoordinates(Coordinate coordinate, Realm realm, int distance) {
Vector<Coordinate> coordinates = new Vector<Coordinate>();
Coordinate relativeCoordinate = new Coordinate(coordinate.getAbscissa(), coordinate.getOrdinate() - 2 * distance);
coordinates.add(relativeCoordinate);
for (int i = 0; i < distance * 2; i++) {
relativeCoordinate = relativeCoordinate.getRelativeCoordinate(RealmConstants.SOUTHEAST);
relativeCoordinate.setAbscissa((relativeCoordinate.getAbscissa() + realm.getMapWidth()) % realm.getMapWidth());
if (isValidCoordinate(realm, relativeCoordinate)) {
coordinates.add(relativeCoordinate);
}
}
for (int i = 0; i < distance * 2; i++) {
relativeCoordinate = relativeCoordinate.getRelativeCoordinate(RealmConstants.SOUTHWEST);
relativeCoordinate.setAbscissa((relativeCoordinate.getAbscissa() + realm.getMapWidth()) % realm.getMapWidth());
if (isValidCoordinate(realm, relativeCoordinate)) {
coordinates.add(relativeCoordinate);
}
}
for (int i = 0; i < distance * 2; i++) {
relativeCoordinate = relativeCoordinate.getRelativeCoordinate(RealmConstants.NORTHWEST);
relativeCoordinate.setAbscissa((relativeCoordinate.getAbscissa() + realm.getMapWidth()) % realm.getMapWidth());
if (isValidCoordinate(realm, relativeCoordinate)) {
coordinates.add(relativeCoordinate);
}
}
for (int i = 0; i < distance * 2 - 1; i++) {
relativeCoordinate = relativeCoordinate.getRelativeCoordinate(RealmConstants.NORTHEAST);
relativeCoordinate.setAbscissa((relativeCoordinate.getAbscissa() + realm.getMapWidth()) % realm.getMapWidth());
if (isValidCoordinate(realm, relativeCoordinate)) {
coordinates.add(relativeCoordinate);
}
}
return coordinates;
}
}