~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/delegate/NonFightingBattle.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software
 
12
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
/*
 
16
 * NonFightingBattle.java
 
17
 *
 
18
 * Created on November 23, 2001, 11:53 AM
 
19
 */
 
20
 
 
21
package games.strategy.triplea.delegate;
 
22
 
 
23
import games.strategy.engine.data.*;
 
24
import games.strategy.engine.delegate.IDelegateBridge;
 
25
import games.strategy.triplea.formatter.MyFormatter;
 
26
import games.strategy.util.*;
 
27
 
 
28
import java.util.*;
 
29
 
 
30
/**
 
31
 * Battle in which no fighting occurs.  <b>
 
32
 * Example is a naval invasion into an empty country,
 
33
 * but the battle cannot be fought until a naval battle
 
34
 * occurs.
 
35
 *
 
36
 * @author  Sean Bridges
 
37
 * @version 1.0
 
38
 */
 
39
public class NonFightingBattle implements Battle
 
40
{
 
41
 
 
42
    private Territory m_battleSite;
 
43
    private PlayerID m_attacker;
 
44
    private BattleTracker m_battleTracker;
 
45
    private GameData m_data;
 
46
    private boolean m_isOver = false; 
 
47
 
 
48
    //dependent units
 
49
    //maps unit -> Collection of units
 
50
    //if unit is lost in a battle we are dependent on
 
51
    //then we lose the corresponding collection of units
 
52
    private Map<Unit, Collection<Unit>> m_dependentUnits = new HashMap<Unit, Collection<Unit>>();
 
53
 
 
54
    public NonFightingBattle(Territory battleSite, PlayerID attacker, BattleTracker battleTracker, boolean neutral, GameData data)
 
55
    {
 
56
        m_battleTracker = battleTracker;
 
57
        m_attacker = attacker;
 
58
        m_battleSite = battleSite;
 
59
        m_data = data;
 
60
    }
 
61
 
 
62
    public void fight(IDelegateBridge bridge)
 
63
    {
 
64
        if(!m_battleTracker.getDependentOn(this).isEmpty())
 
65
            throw new IllegalStateException("Must fight battles that this battle depends on first");
 
66
 
 
67
        //if any attacking non air units then win
 
68
        boolean someAttacking = hasAttackingUnits();
 
69
        if( someAttacking)
 
70
        {
 
71
            m_battleTracker.takeOver(m_battleSite, m_attacker, bridge, m_data, null, null);
 
72
            m_battleTracker.addToConquered(m_battleSite);
 
73
        }
 
74
        end();
 
75
    }
 
76
 
 
77
    private void end()
 
78
    {
 
79
        m_battleTracker.removeBattle(this);
 
80
        m_isOver = true;
 
81
    }
 
82
 
 
83
    public boolean isOver()
 
84
    {
 
85
        return m_isOver;
 
86
    }
 
87
 
 
88
    private boolean hasAttackingUnits()
 
89
    {
 
90
        CompositeMatch<Unit> attackingLand = new CompositeMatchAnd<Unit>();
 
91
              attackingLand.add(Matches.alliedUnit(m_attacker, m_data));
 
92
              attackingLand.add(Matches.UnitIsLand);
 
93
              boolean someAttacking = m_battleSite.getUnits().someMatch(attackingLand);
 
94
        return someAttacking;
 
95
    }
 
96
 
 
97
    public boolean isBombingRun()
 
98
    {
 
99
        return false;
 
100
    }
 
101
 
 
102
    public void removeAttack(Route route, Collection<Unit> units)
 
103
    {
 
104
        Iterator<Unit> dependents = m_dependentUnits.keySet().iterator();
 
105
        while(dependents.hasNext())
 
106
        {
 
107
            Unit dependence = dependents.next();
 
108
            Collection<Unit> dependent = m_dependentUnits.get(dependence);
 
109
            dependent.removeAll(units);
 
110
        }
 
111
    }
 
112
 
 
113
    public boolean isEmpty()
 
114
    {
 
115
        return !hasAttackingUnits();
 
116
    }
 
117
 
 
118
    public Change addAttackChange(Route route, Collection<Unit> units)
 
119
    {
 
120
        Map<Unit, Collection<Unit>> addedTransporting = new TransportTracker().transporting(units);
 
121
        Iterator<Unit> iter = addedTransporting.keySet().iterator();
 
122
        while(iter.hasNext())
 
123
        {
 
124
            Unit unit = iter.next();
 
125
            if(m_dependentUnits.get(unit) != null)
 
126
                m_dependentUnits.get(unit).addAll( addedTransporting.get(unit));
 
127
            else
 
128
                m_dependentUnits.put(unit, addedTransporting.get(unit));
 
129
        }
 
130
        return ChangeFactory.EMPTY_CHANGE;
 
131
    }
 
132
 
 
133
    public Change addCombatChange(Route route, Collection<Unit> units, PlayerID player)
 
134
    {
 
135
        Map<Unit, Collection<Unit>> addedTransporting = new TransportTracker().transporting(units);
 
136
        Iterator<Unit> iter = addedTransporting.keySet().iterator();
 
137
        while(iter.hasNext())
 
138
        {
 
139
            Unit unit = iter.next();
 
140
            if(m_dependentUnits.get(unit) != null)
 
141
                m_dependentUnits.get(unit).addAll( addedTransporting.get(unit));
 
142
            else
 
143
                m_dependentUnits.put(unit, addedTransporting.get(unit));
 
144
        }
 
145
        return ChangeFactory.EMPTY_CHANGE;
 
146
    }
 
147
 
 
148
    public Territory getTerritory()
 
149
    {
 
150
        return m_battleSite;
 
151
    }
 
152
 
 
153
    public void unitsLostInPrecedingBattle(Battle battle, Collection<Unit> units, IDelegateBridge bridge)
 
154
    {
 
155
        Collection<Unit> lost = getDependentUnits(units);
 
156
        lost = Match.getMatches(lost, Matches.unitIsInTerritory(m_battleSite));
 
157
        if(lost.size() != 0)
 
158
        {
 
159
            Change change = ChangeFactory.removeUnits(m_battleSite, lost);
 
160
            bridge.addChange(change);
 
161
 
 
162
            String transcriptText = MyFormatter.unitsToText(lost) + " lost in " + m_battleSite.getName();
 
163
            bridge.getHistoryWriter().startEvent(transcriptText);
 
164
        }
 
165
    }
 
166
 
 
167
    public Collection<Unit> getDependentUnits(Collection units)
 
168
    {
 
169
        Collection<Unit> rVal = new ArrayList<Unit>();
 
170
        
 
171
        Iterator iter = units.iterator();
 
172
        while(iter.hasNext())
 
173
        {
 
174
            Unit unit = (Unit) iter.next();
 
175
            Collection<Unit> dependent = m_dependentUnits.get(unit);
 
176
            if(dependent != null)
 
177
                rVal.addAll(dependent);
 
178
        }
 
179
        return rVal;
 
180
    }
 
181
 
 
182
    public int hashCode()
 
183
    {
 
184
      return m_battleSite.hashCode();
 
185
    }
 
186
 
 
187
    public boolean equals(Object o)
 
188
    {
 
189
      //2 battles are equal if they are both the same type (boming or not)
 
190
      //and occur on the same territory
 
191
      //equals in the sense that they should never occupy the same Set
 
192
      //if these conditions are met
 
193
      if (o == null || ! (o instanceof Battle))
 
194
        return false;
 
195
 
 
196
      Battle other = (Battle) o;
 
197
      return other.getTerritory().equals(this.m_battleSite) &&
 
198
          other.isBombingRun() == this.isBombingRun();
 
199
    }
 
200
 
 
201
    /**
 
202
     * Add bombarding unit. Doesn't make sense here so just do
 
203
     * nothing.
 
204
     */
 
205
    public void addBombardingUnit(Unit unit) {
 
206
        // nothing
 
207
    }
 
208
 
 
209
    /**
 
210
     * Return whether battle is amphibious.
 
211
     */
 
212
    public boolean isAmphibious() {
 
213
        return false;
 
214
    }
 
215
 
 
216
    public Collection<Unit> getAmphibiousLandAttackers()
 
217
    {
 
218
        return new ArrayList<Unit>();
 
219
    }
 
220
 
 
221
    public Collection<Unit> getBombardingUnits()
 
222
    {
 
223
        return new ArrayList<Unit>();
 
224
    }
 
225
    
 
226
    public int getBattleRound()
 
227
    {
 
228
        return 0;
 
229
    }
 
230
 
 
231
    
 
232
}