Skip to content

Commit

Permalink
Update Battle Delegate to auto fight certain fights
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Jan 7, 2017
1 parent 1e8ea69 commit 437002f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 170 deletions.
70 changes: 70 additions & 0 deletions src/games/strategy/triplea/delegate/BattleDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import games.strategy.triplea.attachments.PlayerAttachment;
import games.strategy.triplea.attachments.TerritoryAttachment;
import games.strategy.triplea.attachments.UnitAttachment;
import games.strategy.triplea.delegate.IBattle;
import games.strategy.triplea.delegate.IBattle.BattleType;
import games.strategy.triplea.delegate.IBattle.WhoWon;
import games.strategy.triplea.delegate.dataObjects.BattleListing;
Expand Down Expand Up @@ -101,6 +102,75 @@ public void start() {
m_needToAddBombardmentSources = false;
}
m_battleTracker.raids(m_bridge);

Collection<IBattle> battleList = new ArrayList<>();

// Remove air raids - this adds all the bombing raids to the battle tracker
for( final Territory t : m_battleTracker.getPendingBattleSites(true) ) {
battleList.add( m_battleTracker.getPendingBattle( t, true, BattleType.BOMBING_RAID ) );
}

// Kill undefended transports. Done here to remove potentially dependent sea battles below
for( final Territory t : m_battleTracker.getPendingBattleSites(false) ) { // Loop through normal combats i.e. not bombing or air raid
final IBattle battle = m_battleTracker.getPendingBattle(t, false, BattleType.NORMAL);
if( m_battleTracker.getDependentOn(battle).isEmpty()
&& Match.allMatch( battle.getDefendingUnits(), Matches.UnitIsTransportButNotCombatTransport) ) {
battle.fight( m_bridge ); // Must be fought here to remove dependencies
}
}

// Fight all amphibious assaults with no retreat option for the attacker and no sea combat - these have no real attacker decisions
// Also remove all remaining defenseless fights by fighting them
int battleCount = 0;
int amphibCount = 0;
IBattle lastAmphib = null;
for( final Territory t : m_battleTracker.getPendingNonSBRSites() ) { // Loop through normal combats i.e. not bombing or air raid
final IBattle battle = m_battleTracker.getPendingBattle(t, false, BattleType.NORMAL);
if( battle instanceof NonFightingBattle && m_battleTracker.getDependentOn(battle).isEmpty() ) {
battleList.add( battle ); // Remove non fighting battles by fighting them automatically. Conveniently done here.
continue;
} else if (!(battle instanceof MustFightBattle) ) {
continue;
}
battleCount++;

if( battle.isAmphibious() ) {
// If there is no dependent sea battle and no retreat, fight it now
if( m_battleTracker.getDependentOn(battle).isEmpty() && !( (MustFightBattle) battle).canAnyAttackersRetreat() ) {
battleList.add( battle );
} else {
amphibCount++;
lastAmphib = battle; // Otherwise store it to see if it can be fought later i.e. if there's only one
}
}
}

// Fight amphibious assault if there is one remaining. Fight naval battles in random order if prerequisites first.
if( amphibCount == 1 ) {
// are there battles that must occur first
for( final IBattle seaBattle : m_battleTracker.getDependentOn( lastAmphib ) ) {
if( seaBattle instanceof MustFightBattle && seaBattle != null ) {
battleList.add( seaBattle );
battleCount--;
}
}
battleList.add( lastAmphib );
battleCount--;

}

battleList.forEach( battle -> battle.fight( m_bridge ) );

if( battleCount == 1 ) { // If there is only one remaining normal combat, fight it here rather than requiring the user to click it.
// This needs another loop just in case the last otherBattleCount was triggered by a sea combat already fought
for( final Territory t : m_battleTracker.getPendingBattleSites(false) ) { // Will only find one
final IBattle lastBattle = m_battleTracker.getPendingBattle( t, false, BattleType.NORMAL );
if( lastBattle instanceof MustFightBattle ) {
//battleList.add( lastBattle );
lastBattle.fight( m_bridge );
}
}
}
}

/**
Expand Down
169 changes: 0 additions & 169 deletions test/games/strategy/triplea/delegate/BattleOrderTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion test_data/ww2_g40_balanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@
<delegate name="purchase" javaClass="games.strategy.triplea.delegate.PurchaseDelegate" display="Purchase Units"/>
<delegate name="endTurn" javaClass="games.strategy.triplea.delegate.EndTurnDelegate" display="Turn Complete"/>
<delegate name="specialCombatMove" javaClass="games.strategy.triplea.delegate.SpecialMoveDelegate" display="Airborne Attack Move"/>
<delegate name="battle" javaClass="games.strategy.triplea.delegate.BattleDelegateOrdered" display="BattleDelegateOrdered"/>
<delegate name="battle" javaClass="games.strategy.triplea.delegate.BattleDelegate" display="BattleDelegate"/>
<delegate name="tech_activation" javaClass="games.strategy.triplea.delegate.TechActivationDelegate" display="Activate Technology"/>
<delegate name="endRound" javaClass="games.strategy.triplea.delegate.EndRoundDelegate" display="Round Complete"/>
<delegate name="placeNoAirCheck" javaClass="games.strategy.triplea.delegate.NoAirCheckPlaceDelegate" display="Place Units"/>
Expand Down

0 comments on commit 437002f

Please sign in to comment.