<commit_before>package jsettlers.logic.buildings.military;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import jsettlers.common.CommonConstants;
import jsettlers.common.buildings.EBuildingType;
import jsettlers.common.buildings.IBuilding;
import jsettlers.common.buildings.IBuildingOccupyer;
import jsettlers.common.buildings.OccupyerPlace;
import jsettlers.common.buildings.OccupyerPlace.ESoldierType;
import jsettlers.common.map.shapes.MapCircle;
import jsettlers.common.mapobject.EMapObjectType;
import jsettlers.common.material.ESearchType;
import jsettlers.common.movable.EMovableType;
import jsettlers.common.movable.IMovable;
import jsettlers.common.position.ISPosition2D;
import jsettlers.common.position.RelativePoint;
import jsettlers.logic.algorithms.path.IPathCalculateable;
import jsettlers.logic.algorithms.path.Path;
import jsettlers.logic.algorithms.path.dijkstra.DijkstraAlgorithm.DijkstraContinuableRequest;
import jsettlers.logic.buildings.Building;
import jsettlers.logic.constants.Constants;
import jsettlers.logic.map.newGrid.movable.IHexMovable;
import random.RandomSingleton;

/**
 * Tower building.
 * 
 * <p>
 * 
 * @author <NAME>
 * 
 */
public class OccupyingBuilding extends Building implements IBuilding.IOccupyed, IPathCalculateable, IOccupyableBuilding {
	private static final long serialVersionUID = 5267249978497095473L;

	private static LinkedList<OccupyingBuilding> allOccupyingBuildings = new LinkedList<OccupyingBuilding>();

	private final LinkedList<TowerOccupyer> occupiers;
	private final LinkedList<ESearchType> searchedSoldiers = new LinkedList<ESearchType>();
	private final LinkedList<OccupyerPlace> emptyPlaces = new LinkedList<OccupyerPlace>();

	private DijkstraContinuableRequest request;
	private byte delayCtr;

	private boolean occupiedArea;

	public OccupyingBuilding(EBuildingType type, byte player) {
		super(type, player);

		final OccupyerPlace[] occupyerPlaces = super.getBuildingType().getOccupyerPlaces();
		occupiers = new LinkedList<TowerOccupyer>(); // for testing purposes
		if (occupyerPlaces.length > 0) {

			for (OccupyerPlace currPlace : occupyerPlaces) {
				emptyPlaces.add(currPlace);
				searchedSoldiers.add(currPlace.getType() == ESoldierType.INFANTARY ? ESearchType.SOLDIER_SWORDSMAN : ESearchType.SOLDIER_BOWMAN);
			}
		}

		allOccupyingBuildings.add(this);

		delayCtr = (byte) RandomSingleton.getInt(0, 3);
	}

	@Override
	protected final void constructionFinishedEvent() {
		for (RelativePoint curr : super.getBuildingType().getAttackers()) {
			super.getGrid().getMapObjectsManager()
					.addSimpleMapObject(curr.calculatePoint(super.getPos()), EMapObjectType.ATTACKABLE_TOWER, false, super.getPlayer());
		}
	}

	@Override
	protected final void appearedEvent() {
		occupyArea();
		searchedSoldiers.remove(ESearchType.SOLDIER_SWORDSMAN);
	}

	public final MapCircle getOccupyablePositions() {
		return new MapCircle(super.getPos(), CommonConstants.TOWERRADIUS);
	}

	@Override
	protected final EMapObjectType getFlagType() {
		return EMapObjectType.FLAG_DOOR;
	}

	@Override
	public final void stopOrStartWorking(boolean stop) {
	}

	@Override
	protected final void subTimerEvent() {
		delayCtr++;
		if (delayCtr > 5) {
			delayCtr = 0;

			if (!searchedSoldiers.isEmpty()) {
				if (request == null) {
					request = new DijkstraContinuableRequest(this, super.getPos().getX(), super.getPos().getY(), (short) 1,
							Constants.TOWER_SOLDIER_SEARCH_AREA);
				}
				request.setSearchType(searchedSoldiers.peek());

				Path path = super.getGrid().getDijkstra().find(request);
				if (path != null) {
					System.out.println("soldier found");

					IHexMovable soldier = super.getGrid().getMovable(path.getTargetPos());
					if (soldier != null && soldier.setOccupyableBuilding(this)) {
						searchedSoldiers.pop();
					}// soldier wasn't at the position
				}
			}
		}
	}

	@Override
	protected void positionedEvent(ISPosition2D pos) {
	}

	@Override
	protected final void killedEvent() {
		setSelected(false);

		if (occupiedArea) {
			MapCircle occupied = getOccupyablePositions();
			super.getGrid().freeOccupiedArea(occupied, super.getPos());

			int idx = 0;
			for (TowerOccupyer curr : occupiers) {
				curr.soldier.leaveOccupyableBuilding(super.getBuildingArea().get(idx));
				idx++;
			}
			occupiers.clear();
		}

		allOccupyingBuildings.remove(this);
	}

	@Override
	public final List<? extends IBuildingOccupyer> getOccupyers() {
		return occupiers;
	}

	@Override
	public final boolean needsPlayersGround() { // soldiers don't need players ground.
		return false;
	}

	@Override
	public final void setSoldier(IBuildingOccupyableMovable soldier) {
		for (OccupyerPlace curr : emptyPlaces) {
			if (curr.getType() == soldier.getSoldierType()) {
				emptyPlaces.remove(curr);
				occupiers.add(new TowerOccupyer(curr, soldier));
				break;
			}
		}

		occupyArea();
	}

	private final void occupyArea() {
		if (!occupiedArea) {
			MapCircle occupying = getOccupyablePositions();
			super.getGrid().occupyArea(occupying, super.getPos(), super.getPlayer());
			occupiedArea = true;
		}
	}

	@Override
	public final void requestFailed(EMovableType movableType) {
		ESearchType searchType = getSearchType(movableType);
		if (searchType != null)
			searchedSoldiers.add(searchType);
	}

	@Override
	public final ISPosition2D getPosition(IBuildingOccupyableMovable soldier) {
		for (TowerOccupyer curr : occupiers) {
			if (curr.soldier == soldier) {
				return curr.place.getPosition().calculatePoint(super.getPos());
			}<commit_msg>- speeded up FogOfWar about 10 to 20 percent<commit_after>package jsettlers.logic.buildings.military;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import jsettlers.common.CommonConstants;
import jsettlers.common.buildings.EBuildingType;
import jsettlers.common.buildings.IBuilding;
import jsettlers.common.buildings.IBuildingOccupyer;
import jsettlers.common.buildings.OccupyerPlace;
import jsettlers.common.buildings.OccupyerPlace.ESoldierType;
import jsettlers.common.map.shapes.MapCircle;
import jsettlers.common.mapobject.EMapObjectType;
import jsettlers.common.material.ESearchType;
import jsettlers.common.movable.EMovableType;
import jsettlers.common.movable.IMovable;
import jsettlers.common.position.ISPosition2D;
import jsettlers.common.position.RelativePoint;
import jsettlers.logic.algorithms.path.IPathCalculateable;
import jsettlers.logic.algorithms.path.Path;
import jsettlers.logic.algorithms.path.dijkstra.DijkstraAlgorithm.DijkstraContinuableRequest;
import jsettlers.logic.buildings.Building;
import jsettlers.logic.constants.Constants;
import jsettlers.logic.map.newGrid.movable.IHexMovable;
import random.RandomSingleton;

/**
 * Tower building.
 * 
 * <p>
 * 
 * @author <NAME>
 * 
 */
public class OccupyingBuilding extends Building implements IBuilding.IOccupyed, IPathCalculateable, IOccupyableBuilding {
	private static final long serialVersionUID = 5267249978497095473L;

	private static LinkedList<OccupyingBuilding> allOccupyingBuildings = new LinkedList<OccupyingBuilding>();

	private final LinkedList<TowerOccupyer> occupiers;
	private final LinkedList<ESearchType> searchedSoldiers = new LinkedList<ESearchType>();
	private final LinkedList<OccupyerPlace> emptyPlaces = new LinkedList<OccupyerPlace>();

	private DijkstraContinuableRequest request;
	private byte delayCtr;

	private boolean occupiedArea;

	public OccupyingBuilding(EBuildingType type, byte player) {
		super(type, player);

		final OccupyerPlace[] occupyerPlaces = super.getBuildingType().getOccupyerPlaces();
		occupiers = new LinkedList<TowerOccupyer>(); // for testing purposes
		if (occupyerPlaces.length > 0) {

			for (OccupyerPlace currPlace : occupyerPlaces) {
				emptyPlaces.add(currPlace);
				searchedSoldiers.add(currPlace.getType() == ESoldierType.INFANTARY ? ESearchType.SOLDIER_SWORDSMAN : ESearchType.SOLDIER_BOWMAN);
			}
		}

		allOccupyingBuildings.add(this);

		delayCtr = (byte) RandomSingleton.getInt(0, 3);
	}

	@Override
	protected final void constructionFinishedEvent() {
		for (RelativePoint curr : super.getBuildingType().getAttackers()) {
			super.getGrid().getMapObjectsManager()
					.addSimpleMapObject(curr.calculatePoint(super.getPos()), EMapObjectType.ATTACKABLE_TOWER, false, super.getPlayer());
		}
	}

	@Override
	protected final void appearedEvent() {
		occupyArea();
		searchedSoldiers.remove(ESearchType.SOLDIER_SWORDSMAN);
	}

	public final MapCircle getOccupyablePositions() {
		return new MapCircle(super.getPos(), CommonConstants.TOWERRADIUS);
	}

	@Override
	protected final EMapObjectType getFlagType() {
		return EMapObjectType.FLAG_DOOR;
	}

	@Override
	public final void stopOrStartWorking(boolean stop) {
	}

	@Override
	protected final void subTimerEvent() {
		delayCtr++;
		if (delayCtr > 5) {
			delayCtr = 0;

			if (!searchedSoldiers.isEmpty()) {
				if (request == null) {
					request = new DijkstraContinuableRequest(this, super.getPos().getX(), super.getPos().getY(), (short) 1,
							Constants.TOWER_SOLDIER_SEARCH_AREA);
				}
				request.setSearchType(searchedSoldiers.peek());

				Path path = super.getGrid().getDijkstra().find(request);
				if (path != null) {
					System.out.println("soldier found");

					IHexMovable soldier = super.getGrid().getMovable(path.getTargetPos());
					if (soldier != null && soldier.setOccupyableBuilding(this)) {
						searchedSoldiers.pop();
					}// soldier wasn't at the position
				}
			}
		}
	}

	@Override
	protected void positionedEvent(ISPosition2D pos) {
	}

	@Override
	protected final void killedEvent() {
		setSelected(false);

		if (occupiedArea) {
			MapCircle occupied = getOccupyablePositions();
			super.getGrid().freeOccupiedArea(occupied, super.getPos());

			int idx = 0;
			for (TowerOccupyer curr : occupiers) {
				curr.soldier.leaveOccupyableBuilding(super.getBuildingArea().get(idx));
				idx++;
			}
			occupiers.clear();
		}

		allOccupyingBuildings.remove(this);
	}

	@Override
	public final List<? extends IBuildingOccupyer> getOccupyers() {
		return occupiers;
	}

	@Override
	public final boolean needsPlayersGround() { // soldiers don't need players ground.
		return false;
	}

	@Override
	public final void setSoldier(IBuildingOccupyableMovable soldier) {
		for (OccupyerPlace curr : emptyPlaces) {
			if (curr.getType() == soldier.getSoldierType()) {
				emptyPlaces.remove(curr);
				occupiers.add(new TowerOccupyer(curr, soldier));
				break;
			}
		}

		occupyArea();

		soldier.setSelected(super.isSelected());
	}

	private final void occupyArea() {
		if (!occupiedArea) {
			MapCircle occupying = getOccupyablePositions();
			super.getGrid().occupyArea(occupying, super.getPos(), super.getPlayer());
			occupiedArea = true;
		}
	}

	@Override
	public final void requestFailed(EMovableType movableType) {
		ESearchType searchType = getSearchType(movableType);
		if (searchType != null)
			searchedSoldiers.add(searchType);
	}

	@Override
	public final ISPosition2D getPosition(IBuildingOccupyableMovable soldier) {
		for (TowerOccupyer curr : occupiers) {
			if (curr.soldier == soldier) {
				return curr.place.getPosition().calculatePoint(super.getPos());
			}
{'fraction_non_alphanumeric': 0.10798750557786703, 'fraction_numerical': 0.005533244087460955, 'mean_word_length': 5.198008849557522, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047500', 'n_tokens_mistral': 4151, 'n_tokens_neox': 3621, 'n_words': 673}