Skip to content

Commit

Permalink
#608 Changes AI military estimation to work correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomount committed Jul 20, 2023
1 parent 4e55dbe commit b1ec510
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,8 @@ private static EspionageMission scoreEspionageMission(final PlayerInfo info,
break;
}
}
int difference = starMap.getMilitaryDifference(infoIndex, target);
int difference = starMap.getMilitaryDifference(infoIndex, target,
info.getDiplomacy().isWar(target));
if (difference > 100) {
scores[i] = scores[i] + 30;
} else if (difference > 80) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,9 @@ public void searchPlanetsForMissions() {
break;
}
}
if (game.getStarMap().getMilitaryDifference(
index1, index2) > neededDifference && attacks < LIMIT_ATTACKS) {
boolean isWar = info.getDiplomacy().isWar(index2);
if (game.getStarMap().getMilitaryDifference(index1, index2,
isWar) > neededDifference && attacks < LIMIT_ATTACKS) {
addAttackMission(planet, info);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2305,17 +2305,18 @@ public int getOfferDifferenceForBoth() {
break;
}
}
int militaryDifference = starMap.getMilitaryDifference(first, second);
if (militaryDifference > 0) {
int militaryDifference = starMap.getMilitaryDifference(second, first,
isWar);
if (militaryDifference < 0) {
if (isWar) {
if (firstOffer.isPeaceInOffer()) {
difference = difference - militaryDifference / divider;
difference = difference + militaryDifference / divider;
}
} else {
difference = difference - militaryDifference / divider;
difference = difference + militaryDifference / divider;
}
} else {
difference = difference - militaryDifference / ownDivider;
difference = difference + militaryDifference / ownDivider;
}
}
if (!militaryThreat && secondValueZero && difference <= 0) {
Expand Down
35 changes: 29 additions & 6 deletions src/main/java/org/openRealmOfStars/starMap/StarMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4049,29 +4049,51 @@ protected int getMilitaryEstimation(final int first, final int second) {
* @param first Player index who is estimating
* @param second Second player who is being estimated
* and who belong to defensive pact
* @param isWar between first and second.
* @return Military value
*/
protected int getMilitaryEstimationForDefensivePact(final int first,
final int second) {
final int second, final boolean isWar) {
int result = getMilitaryEstimation(first, second);
PlayerInfo secondInfo = players.getPlayerInfoByIndex(second);
PlayerInfo firstInfo = players.getPlayerInfoByIndex(first);
int maxPlayer = players.getCurrentMaxRealms();
for (int i = 0; i < maxPlayer; i++) {
if (i != first && i != second
&& secondInfo.getDiplomacy().isDefensivePact(i)) {
result = result + getMilitaryEstimation(first, i);
if (!isWar) {
if (i != first && i != second
&& secondInfo.getDiplomacy().isDefensivePact(i)) {
result = result + getMilitaryEstimation(first, i);
}
} else {
if (i != first && i != second
&& secondInfo.getDiplomacy().isDefensivePact(i)
&& firstInfo.getDiplomacy().isWar(i)) {
result = result + getMilitaryEstimation(first, i);
}
}
}
return result;
}
/**
* Get latest military difference between two players, using espionage
* information and news corp.
* information and news corp. First one is making the comparison.
* @param first First player index. This is where second one is compared.
* @param second Second player index.
* @return Positive number if first player has bigger military
*/
public int getMilitaryDifference(final int first, final int second) {
return getMilitaryDifference(first, second, false);
}
/**
* Get latest military difference between two players, using espionage
* information and news corp. First one is making the comparison.
* @param first First player index. This is where second one is compared.
* @param second Second player index.
* @param isWar There is war between first and second realm.
* @return Positive number if first player has bigger military
*/
public int getMilitaryDifference(final int first, final int second,
final boolean isWar) {
int result = 0;
PlayerInfo firstInfo = players.getPlayerInfoByIndex(first);
PlayerInfo secondInfo = players.getPlayerInfoByIndex(second);
Expand Down Expand Up @@ -4102,7 +4124,8 @@ public int getMilitaryDifference(final int first, final int second) {
}
} else {
int actualMilitaryFirst = NewsCorpData.calculateMilitaryValue(firstInfo);
int estimate = getMilitaryEstimationForDefensivePact(first, second);
int estimate = getMilitaryEstimationForDefensivePact(first, second,
isWar);
result = actualMilitaryFirst - estimate;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public void testCustomTradeGeneration() {
Mockito.when(map.getPlayerByIndex(1)).thenReturn(player2);
Mockito.when(map.getNewsCorpData()).thenReturn(newsCorp);
Mockito.when(map.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt())).thenReturn(-100);
Mockito.when(map.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt(),
Mockito.anyBoolean())).thenReturn(100);
DiplomaticTrade trade = new DiplomaticTrade(map, 0, 1);
NegotiationList offer = new NegotiationList();
offer.add(new NegotiationOffer(NegotiationType.CREDIT, 80));
Expand All @@ -343,7 +345,9 @@ public void testCustomTradeGeneration() {
assertEquals(false, trade.isOfferGoodForBoth());

Mockito.when(newsCorp.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt())).thenReturn(50);
Mockito.when(map.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt())).thenReturn(50);
Mockito.when(map.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt())).thenReturn(-50);
Mockito.when(map.getMilitaryDifference(Mockito.anyInt(), Mockito.anyInt(),
Mockito.anyBoolean())).thenReturn(-50);
trade = new DiplomaticTrade(map, 0, 1);
offer = new NegotiationList();
offer.add(new NegotiationOffer(NegotiationType.CREDIT, 15));
Expand Down
199 changes: 193 additions & 6 deletions src/test/java/org/openRealmOfStars/starMap/StarMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1468,27 +1468,214 @@ public void testDefensivePactEstimation() {
Mockito.when(players.getCurrentMaxRealms()).thenReturn(4);
StarMap map = new StarMap(config, players);
map.getNewsCorpData().calculateMilitary(players, false);
assertEquals(144, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(144, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-120, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
assertEquals(131, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(131, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-107, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(94, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(94, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-70, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(98, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(98, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-74, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(89, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(89, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-65, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(96, map.getMilitaryEstimationForDefensivePact(0, 2));
assertEquals(96, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-72, map.getMilitaryDifference(0, 2));
}

@Test
@Category(org.openRealmOfStars.UnitTest.class)
public void testDefensivePactEstimation2() {
GalaxyConfig config = Mockito.mock(GalaxyConfig.class);
Mockito.when(config.getSizeX()).thenReturn(50);
Mockito.when(config.getSizeY()).thenReturn(50);
Mockito.when(config.getMaxPlayers()).thenReturn(4);
Mockito.when(config.getStartingPosition()).thenReturn(
GalaxyConfig.START_POSITION_BORDER);

PlayerInfo info = Mockito.mock(PlayerInfo.class);
Mockito.when(info.getRace()).thenReturn(SpaceRace.HUMAN);
Mockito.when(info.getEmpireName()).thenReturn("Empire of Human");
Mockito.when(info.getGovernment()).thenReturn(GovernmentType.EMPIRE);
Leader ruler = Mockito.mock(Leader.class);
Mockito.when(info.getRuler()).thenReturn(ruler);
Mockito.when(ruler.getName()).thenReturn("Max Power");
MessageList msgList = Mockito.mock(MessageList.class);
Mockito.when(info.getMsgList()).thenReturn(msgList);
ShipStat[] stats = new ShipStat[0];
Mockito.when(info.getShipStatList()).thenReturn(stats);
Espionage espionage = new Espionage(4);
espionage.getByIndex(2).setEspionageLevel1Estimate(40);
espionage.getByIndex(2).setEspionageLevel3Estimate(-30);
espionage.getByIndex(2).setEspionageLevel5Estimate(20);
espionage.getByIndex(2).setEspionageLevel7Estimate(-10);
espionage.getByIndex(3).setEspionageLevel1Estimate(35);
espionage.getByIndex(3).setEspionageLevel3Estimate(25);
espionage.getByIndex(3).setEspionageLevel5Estimate(-15);
espionage.getByIndex(3).setEspionageLevel7Estimate(-8);
Diplomacy diplomacy = Mockito.mock(Diplomacy.class);
Mockito.when(diplomacy.isDefensivePact(3)).thenReturn(true);
Mockito.when(diplomacy.hasDefensivePact()).thenReturn(true);
Fleet fleet = Mockito.mock(Fleet.class);
Mockito.when(fleet.getMilitaryValue()).thenReturn(24);
FleetList fleetList = Mockito.mock(FleetList.class);
Mockito.when(fleetList.getNumberOfFleets()).thenReturn(1);
Mockito.when(fleetList.getByIndex(Mockito.anyInt())).thenReturn(fleet);
Mockito.when(info.getFleets()).thenReturn(fleetList);
Mockito.when(info.getFakeMilitarySize()).thenReturn(100);
Mockito.when(info.getEspionage()).thenReturn(espionage);
Mockito.when(info.getDiplomacy()).thenReturn(diplomacy);

PlayerInfo info2 = Mockito.mock(PlayerInfo.class);
Mockito.when(info2.getRace()).thenReturn(SpaceRace.CENTAURS);
Mockito.when(info2.getEmpireName()).thenReturn("Empire of Centaurs");
Mockito.when(info2.getGovernment()).thenReturn(GovernmentType.EMPIRE);
Leader ruler2 = Mockito.mock(Leader.class);
Mockito.when(info2.getRuler()).thenReturn(ruler2);
Mockito.when(ruler2.getName()).thenReturn("Low Power");
Mockito.when(info2.getMsgList()).thenReturn(msgList);
Mockito.when(info2.getShipStatList()).thenReturn(stats);
FleetList fleetList2 = Mockito.mock(FleetList.class);
Mockito.when(fleetList2.getNumberOfFleets()).thenReturn(2);
Mockito.when(fleetList2.getByIndex(Mockito.anyInt())).thenReturn(fleet);
Mockito.when(info2.getFleets()).thenReturn(fleetList2);
Mockito.when(info2.getFakeMilitarySize()).thenReturn(150);
Mockito.when(info2.getEspionage()).thenReturn(espionage);
Mockito.when(info2.getDiplomacy()).thenReturn(diplomacy);

PlayerList players = Mockito.mock(PlayerList.class);
Mockito.when(players.getPlayerInfoByIndex(0)).thenReturn(info);
Mockito.when(players.getPlayerInfoByIndex(1)).thenReturn(info);
Mockito.when(players.getPlayerInfoByIndex(2)).thenReturn(info2);
Mockito.when(players.getPlayerInfoByIndex(3)).thenReturn(info2);
Mockito.when(players.getCurrentMaxPlayers()).thenReturn(5);
Mockito.when(players.getCurrentMaxRealms()).thenReturn(4);
StarMap map = new StarMap(config, players);
map.getNewsCorpData().calculateMilitary(players, false);
assertEquals(72, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-120, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
assertEquals(67, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-107, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(34, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-70, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(57, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-74, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(44, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-65, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(48, map.getMilitaryEstimationForDefensivePact(0, 2, true));
assertEquals(-72, map.getMilitaryDifference(0, 2));
}

@Test
@Category(org.openRealmOfStars.UnitTest.class)
public void testDefensivePactEstimation3() {
GalaxyConfig config = Mockito.mock(GalaxyConfig.class);
Mockito.when(config.getSizeX()).thenReturn(50);
Mockito.when(config.getSizeY()).thenReturn(50);
Mockito.when(config.getMaxPlayers()).thenReturn(4);
Mockito.when(config.getStartingPosition()).thenReturn(
GalaxyConfig.START_POSITION_BORDER);

PlayerInfo info = Mockito.mock(PlayerInfo.class);
Mockito.when(info.getRace()).thenReturn(SpaceRace.HUMAN);
Mockito.when(info.getEmpireName()).thenReturn("Empire of Human");
Mockito.when(info.getGovernment()).thenReturn(GovernmentType.EMPIRE);
Leader ruler = Mockito.mock(Leader.class);
Mockito.when(info.getRuler()).thenReturn(ruler);
Mockito.when(ruler.getName()).thenReturn("Max Power");
MessageList msgList = Mockito.mock(MessageList.class);
Mockito.when(info.getMsgList()).thenReturn(msgList);
ShipStat[] stats = new ShipStat[0];
Mockito.when(info.getShipStatList()).thenReturn(stats);
Espionage espionage = new Espionage(4);
espionage.getByIndex(2).setEspionageLevel1Estimate(40);
espionage.getByIndex(2).setEspionageLevel3Estimate(-30);
espionage.getByIndex(2).setEspionageLevel5Estimate(20);
espionage.getByIndex(2).setEspionageLevel7Estimate(-10);
espionage.getByIndex(3).setEspionageLevel1Estimate(35);
espionage.getByIndex(3).setEspionageLevel3Estimate(25);
espionage.getByIndex(3).setEspionageLevel5Estimate(-15);
espionage.getByIndex(3).setEspionageLevel7Estimate(-8);
Diplomacy diplomacy = Mockito.mock(Diplomacy.class);
Mockito.when(diplomacy.isDefensivePact(3)).thenReturn(true);
Mockito.when(diplomacy.isWar(3)).thenReturn(true);
Mockito.when(diplomacy.hasDefensivePact()).thenReturn(true);
Fleet fleet = Mockito.mock(Fleet.class);
Mockito.when(fleet.getMilitaryValue()).thenReturn(24);
FleetList fleetList = Mockito.mock(FleetList.class);
Mockito.when(fleetList.getNumberOfFleets()).thenReturn(1);
Mockito.when(fleetList.getByIndex(Mockito.anyInt())).thenReturn(fleet);
Mockito.when(info.getFleets()).thenReturn(fleetList);
Mockito.when(info.getFakeMilitarySize()).thenReturn(100);
Mockito.when(info.getEspionage()).thenReturn(espionage);
Mockito.when(info.getDiplomacy()).thenReturn(diplomacy);

PlayerInfo info2 = Mockito.mock(PlayerInfo.class);
Mockito.when(info2.getRace()).thenReturn(SpaceRace.CENTAURS);
Mockito.when(info2.getEmpireName()).thenReturn("Empire of Centaurs");
Mockito.when(info2.getGovernment()).thenReturn(GovernmentType.EMPIRE);
Leader ruler2 = Mockito.mock(Leader.class);
Mockito.when(info2.getRuler()).thenReturn(ruler2);
Mockito.when(ruler2.getName()).thenReturn("Low Power");
Mockito.when(info2.getMsgList()).thenReturn(msgList);
Mockito.when(info2.getShipStatList()).thenReturn(stats);
FleetList fleetList2 = Mockito.mock(FleetList.class);
Mockito.when(fleetList2.getNumberOfFleets()).thenReturn(2);
Mockito.when(fleetList2.getByIndex(Mockito.anyInt())).thenReturn(fleet);
Mockito.when(info2.getFleets()).thenReturn(fleetList2);
Mockito.when(info2.getFakeMilitarySize()).thenReturn(150);
Mockito.when(info2.getEspionage()).thenReturn(espionage);
Mockito.when(info2.getDiplomacy()).thenReturn(diplomacy);

PlayerList players = Mockito.mock(PlayerList.class);
Mockito.when(players.getPlayerInfoByIndex(0)).thenReturn(info);
Mockito.when(players.getPlayerInfoByIndex(1)).thenReturn(info);
Mockito.when(players.getPlayerInfoByIndex(2)).thenReturn(info2);
Mockito.when(players.getPlayerInfoByIndex(3)).thenReturn(info2);
Mockito.when(players.getCurrentMaxPlayers()).thenReturn(5);
Mockito.when(players.getCurrentMaxRealms()).thenReturn(4);
StarMap map = new StarMap(config, players);
map.getNewsCorpData().calculateMilitary(players, false);
assertEquals(144, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-120, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 1, "Test");
assertEquals(131, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-107, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(94, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-70, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(98, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-74, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(89, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-65, map.getMilitaryDifference(0, 2));
espionage.getByIndex(2).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
espionage.getByIndex(3).addEspionageBonus(EspionageBonusType.SPY_FLEET, 2, "Test");
assertEquals(96, map.getMilitaryEstimationForDefensivePact(0, 2, false));
assertEquals(-72, map.getMilitaryDifference(0, 2));
}

Expand Down

0 comments on commit b1ec510

Please sign in to comment.