Analyzing landmasses

In the previous article we took a more detailed look at how we want the scout AI to work. Today we’ll look at the methods that are used to analyze the land masses. The first thing we do is to update the land masses. The main purpose of the update method is to check which settlements and armies are located on which land mass. An army or settlement is located on the land mass if a walkable land route can be found between its own location and the closest location that we’ve previously decided is located on the land mass. If no route can be found to any land mass a new land mass is created. This happens, for example, when an army has disembarked on a new island or continent.

public static List<Landmass> updateLandmasses(World w, Empire e, List<Landmass> landmasses, Map<String, CompanyData> companyData) {	
	settlementLoop: for ( Settlement s:e.getSettlements() ) {
		boolean added = false;
		// Check if settlement is connected to any of the land masses
		for ( Landmass l:landmasses ) {
			// Check if the land mass is on the same level as the settlement
			if ( l.getSectors().containsKey(s.getLevel()) ) {
				// Find the sector on the land mass that is closest to the settlement
				Sector nearestSector = null;
				for ( Sector sector:l.getLevelSectors(s.getLevel()) ) {
					if ( nearestSector == null ) {
						nearestSector = sector;
					}
					else if ( MapMethods.calculateRange(sector, s.getSector()) < MapMethods.calculateRange(nearestSector, s.getSector()) ) {
						nearestSector = sector;
					}
				}
				// if a path can be found between settlement and nearestSector (or they are on the same sector) we add settlement to land mass
				String path = PathfinderArmy.findPathSequence(e, PathfinderArmy.testArmy(w, companyData, s.getSector(), s.getLevel()), s.getSector(), nearestSector, w.getMaps().get(s.getLevel()), e.getMemory().getMemoryMaps().get(s.getLevel()));
				if ( s.getSector().equals(nearestSector) ) {
					l.addSettlement(s);
					added = true;
					continue settlementLoop;
				}
				else if ( path != null && !path.isEmpty() ) {
					l.addSettlement(s);
					added = true;
					continue settlementLoop;
				}
			}
		}
		// If it couldn't be added to an existing land mass create new one
		if ( !added ) {
			Landmass newLandmass = new Landmass(s.getSector(), s.getLevel());
			newLandmass.addSettlement(s);
			landmasses.add(newLandmass);
		}
	}
	
	armyLoop: for ( Army a:e.getArmies() ) {
		boolean added = false;
		// Check if settlement is connected to any of the landmasses
		for ( Landmass l:landmasses ) {
			// Check if the land mass is on the same level as the army
			if ( l.getSectors().containsKey(a.getLevel()) ) {
				// Find the sector on the land mass that is closest to the army
				Sector nearestSector = null;
				for ( Sector sector:l.getLevelSectors(a.getLevel()) ) {
					if ( nearestSector == null ) {
						nearestSector = sector;
					}
					else if ( MapMethods.calculateRange(sector, a.getSector()) < MapMethods.calculateRange(nearestSector, a.getSector()) ) {
						nearestSector = sector;
					}
				}
				// if a path can be found between army and nearestSector (or they are on the same sector) we add army to landmass
				String path = PathfinderArmy.findPathSequence(e, PathfinderArmy.testArmy(w, companyData, a.getSector(), a.getLevel()), a.getSector(), nearestSector, w.getMaps().get(a.getLevel()), e.getMemory().getMemoryMaps().get(a.getLevel()));
				if ( a.getSector().equals(nearestSector) ) {
					l.addArmy(a);
					added = true;
					continue armyLoop;
				}
				else if ( path != null && !path.isEmpty() ) {
					l.addArmy(a);
					added = true;
					continue armyLoop;
				}
			}
		}
		// If it couldn't be added to an existing landmass create new one
		if ( !added ) {
			Landmass newLandmass = new Landmass(a.getSector(), a.getLevel());
			newLandmass.addArmy(a);
			landmasses.add(newLandmass);
		}
	}
	
	return landmasses;
}

As you can see I use my pathfinder when searching for connections between different locations. When an army is added to a land mass we also check if it’s a scout army:

public void addArmy(Army a) {
	armies.add(a.getId());
	if ( ArmyMethods.isScout(a) ) {
		scoutArmies.add(a.getId());
	}
	if ( !sectors.containsKey(a.getLevel()) ) {
		sectors.put(a.getLevel(), new ArrayList<Sector>());
	}
	sectors.get(a.getLevel()).add(a.getSector());
}

Leave a Reply