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
15
package games.strategy.triplea.Dynamix_AI;
17
import games.strategy.engine.data.GameData;
18
import games.strategy.engine.data.Resource;
19
import games.strategy.engine.data.Route;
20
import games.strategy.engine.data.Territory;
21
import games.strategy.engine.data.Unit;
22
import games.strategy.triplea.Dynamix_AI.CommandCenter.CachedCalculationCenter;
23
import games.strategy.util.Match;
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.Collections;
27
import java.util.Comparator;
28
import java.util.HashMap;
29
import java.util.List;
37
//Think of the compare method like this: the integer returned tells java the position of the first object in relation to the second...
38
//If -1 is return, java puts the first object before the second, 0 means they're equal(not sure which would come first), 1 tells java to put the second object before the first
40
///////////////////////////////////////////////List Sorting///////////////////////////////////////////////
41
public static List SortListByX(Collection list, Comparator comparator)
43
List result = new ArrayList(list);
44
Collections.sort(result, comparator);
47
public static List SortListByScores_HashMap_A(Collection list, final HashMap<?, ? extends Number> scores)
49
final List result = new ArrayList(list);
50
Collections.sort(result, new Comparator()
52
public int compare(Object o1, Object o2)
54
double v1 = safeGet(scores, o1);
55
double v2 = safeGet(scores, o2);
64
private double safeGet(final HashMap<?, ? extends Number> map, Object key)
66
if (!map.containsKey(key))
67
return DConstants.Integer_HalfMin; //Put ones without scores at the bottom of the list
68
return map.get(key).doubleValue();
73
public static List SortListByScores_HashMap_D(Collection list, final HashMap<?, ? extends Number> scores)
75
return DUtils.InvertList(SortListByScores_HashMap_A(list, scores));
77
public static List SortListByScores_List_A(Collection list, Collection scoreList)
79
final HashMap scores = DUtils.ToHashMap(list, scoreList);
80
return SortListByScores_HashMap_A(list, scores);
82
public static List SortListByScores_List_D(Collection list, Collection scoreList)
84
return DUtils.InvertList(SortListByScores_List_A(list, scoreList));
86
///////////////////////////////////////////////End List Sorting///////////////////////////////////////////////
88
///////////////////////////////////////////////Territory Sorting///////////////////////////////////////////////
89
public static List<Territory> SortTerritoriesByDistance_A(final List<Territory> ters, final GameData data, final Territory target, final Match<Territory> routeMatch)
91
return SortListByX(ters, new Comparator<Territory>()
93
public int compare(Territory t1, Territory t2)
95
Route route1 = data.getMap().getRoute(t1, target, routeMatch);
96
Route route2 = data.getMap().getRoute(t2, target, routeMatch);
101
distance1 = DConstants.Integer_HalfMax;
103
distance1 = route1.getLength();
105
distance2 = DConstants.Integer_HalfMax;
107
distance2 = route2.getLength();
109
return ((Integer)distance1).compareTo(distance2);
113
public static List<Territory> SortTerritoriesByDistance_D(final List<Territory> ters, final GameData data, final Territory target, final Match<Territory> routeMatch)
115
return DUtils.InvertList(SortTerritoriesByDistance_A(ters, data, target, routeMatch));
117
public static List<Territory> SortTerritoriesByLandDistance_A(final List<Territory> ters, final GameData data, final Territory target)
119
return SortListByX(ters, new Comparator<Territory>()
121
public int compare(Territory t1, Territory t2)
123
Route route1 = CachedCalculationCenter.GetLandRoute(data, t1, target);
124
Route route2 = CachedCalculationCenter.GetLandRoute(data, t2, target);
129
distance1 = DConstants.Integer_HalfMax;
131
distance1 = route1.getLength();
133
distance2 = DConstants.Integer_HalfMax;
135
distance2 = route2.getLength();
137
return ((Integer)distance1).compareTo(distance2);
141
public static List<Territory> SortTerritoriesByLandDistance_D(final List<Territory> ters, final GameData data, final Territory target)
143
return DUtils.InvertList(SortTerritoriesByLandDistance_A(ters, data, target));
145
public static List<Territory> SortTerritoriesBySeaDistance_A(final List<Territory> ters, final GameData data, final Territory target)
147
return SortListByX(ters, new Comparator<Territory>()
149
public int compare(Territory t1, Territory t2)
151
Route route1 = CachedCalculationCenter.GetSeaRoute(data, t1, target);
152
Route route2 = CachedCalculationCenter.GetSeaRoute(data, t2, target);
157
distance1 = DConstants.Integer_HalfMax;
159
distance1 = route1.getLength();
161
distance2 = DConstants.Integer_HalfMax;
163
distance2 = route2.getLength();
165
return ((Integer)distance1).compareTo(distance2);
169
public static List<Territory> SortTerritoriesBySeaDistance_D(final List<Territory> ters, final GameData data, final Territory target)
171
return DUtils.InvertList(SortTerritoriesBySeaDistance_A(ters, data, target));
173
public static List<Territory> SortTerritoriesByNoCondDistance_A(final List<Territory> ters, final GameData data, final Territory target)
175
return SortListByX(ters, new Comparator<Territory>()
177
public int compare(Territory t1, Territory t2)
179
Route route1 = CachedCalculationCenter.GetRoute(data, t1, target);
180
Route route2 = CachedCalculationCenter.GetRoute(data, t2, target);
185
distance1 = DConstants.Integer_HalfMax;
187
distance1 = route1.getLength();
189
distance2 = DConstants.Integer_HalfMax;
191
distance2 = route2.getLength();
193
return ((Integer)distance1).compareTo(distance2);
197
public static List<Territory> SortTerritoriesByNoCondDistance_D(final List<Territory> ters, final GameData data, final Territory target)
199
return DUtils.InvertList(SortTerritoriesByNoCondDistance_A(ters, data, target));
201
public static List<Territory> SortTerritoriesByLandThenNoCondDistance_A(final List<Territory> ters, final GameData data, final Territory target)
203
return SortListByX(ters, new Comparator<Territory>()
205
public int compare(Territory t1, Territory t2)
207
Route route1 = CachedCalculationCenter.GetLandRoute(data, t1, target);
208
Route route2 = CachedCalculationCenter.GetLandRoute(data, t2, target);
209
Route route1_nc = CachedCalculationCenter.GetRoute(data, t1, target);
210
Route route2_nc = CachedCalculationCenter.GetRoute(data, t2, target);
212
if(route1_nc == null && route2_nc == null)
213
return 0; //We can't compare these, so say they're equal
214
if(route1_nc == null)
216
if(route2_nc == null)
219
int distance1 = route1_nc.getLength() * 100;
220
int distance2 = route2_nc.getLength() * 100;
222
distance1 = route1.getLength();
224
distance2 = route2.getLength();
226
return ((Integer)distance1).compareTo(distance2);
230
public static List<Territory> SortTerritoriesByLandThenNoCondDistance_D(final List<Territory> ters, final GameData data, final Territory target)
232
return DUtils.InvertList(SortTerritoriesByLandThenNoCondDistance_A(ters, data, target));
234
///////////////////////////////////////////////End Territory Sorting///////////////////////////////////////////////
236
///////////////////////////////////////////////Unit Sorting///////////////////////////////////////////////
237
public static List<Unit> SortUnitsByCost_A(List<Unit> units, final Resource resource)
239
return SortListByX(units, new Comparator<Unit>()
241
public int compare(Unit o1, Unit o2)
243
int cost1 = DUtils.GetTUVOfUnits(Collections.singletonList(o1), resource);
244
int cost2 = DUtils.GetTUVOfUnits(Collections.singletonList(o2), resource);
246
return ((Integer)cost1).compareTo(cost2);
250
///////////////////////////////////////////////End Unit Sorting///////////////////////////////////////////////