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

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/Dynamix_AI/DSorting.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
package games.strategy.triplea.Dynamix_AI;
 
16
 
 
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;
 
30
 
 
31
/**
 
32
 *
 
33
 * @author Stephen
 
34
 */
 
35
public class DSorting
 
36
{
 
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
 
39
 
 
40
    ///////////////////////////////////////////////List Sorting///////////////////////////////////////////////
 
41
    public static List SortListByX(Collection list, Comparator comparator)
 
42
    {
 
43
        List result = new ArrayList(list);
 
44
        Collections.sort(result, comparator);
 
45
        return result;
 
46
    }
 
47
    public static List SortListByScores_HashMap_A(Collection list, final HashMap<?, ? extends Number> scores)
 
48
    {
 
49
        final List result = new ArrayList(list);
 
50
        Collections.sort(result, new Comparator()
 
51
        {
 
52
            public int compare(Object o1, Object o2)
 
53
            {
 
54
                double v1 = safeGet(scores, o1);
 
55
                                double v2 = safeGet(scores, o2);
 
56
 
 
57
                if (v1 > v2)
 
58
                                        return 1;
 
59
                                else if (v1 == v2)
 
60
                                        return 0;
 
61
                                else
 
62
                                        return -1;
 
63
            }
 
64
            private double safeGet(final HashMap<?, ? extends Number> map, Object key)
 
65
                        {
 
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();
 
69
                        }
 
70
        });
 
71
        return result;
 
72
    }
 
73
    public static List SortListByScores_HashMap_D(Collection list, final HashMap<?, ? extends Number> scores)
 
74
    {
 
75
        return DUtils.InvertList(SortListByScores_HashMap_A(list, scores));
 
76
    }
 
77
    public static List SortListByScores_List_A(Collection list, Collection scoreList)
 
78
    {
 
79
        final HashMap scores = DUtils.ToHashMap(list, scoreList);
 
80
        return SortListByScores_HashMap_A(list, scores);
 
81
    }
 
82
    public static List SortListByScores_List_D(Collection list, Collection scoreList)
 
83
    {
 
84
        return DUtils.InvertList(SortListByScores_List_A(list, scoreList));
 
85
    }
 
86
    ///////////////////////////////////////////////End List Sorting///////////////////////////////////////////////
 
87
 
 
88
    ///////////////////////////////////////////////Territory Sorting///////////////////////////////////////////////
 
89
    public static List<Territory> SortTerritoriesByDistance_A(final List<Territory> ters, final GameData data, final Territory target, final Match<Territory> routeMatch)
 
90
    {
 
91
        return SortListByX(ters, new Comparator<Territory>()
 
92
        {
 
93
            public int compare(Territory t1, Territory t2)
 
94
            {
 
95
                Route route1 = data.getMap().getRoute(t1, target, routeMatch);
 
96
                Route route2 = data.getMap().getRoute(t2, target, routeMatch);
 
97
 
 
98
                int distance1 = 0;
 
99
                int distance2 = 0;
 
100
                if(route1 == null)
 
101
                    distance1 = DConstants.Integer_HalfMax;
 
102
                else
 
103
                    distance1 = route1.getLength();
 
104
                if(route2 == null)
 
105
                    distance2 = DConstants.Integer_HalfMax;
 
106
                else
 
107
                    distance2 = route2.getLength();
 
108
 
 
109
                return ((Integer)distance1).compareTo(distance2);
 
110
            }
 
111
        });
 
112
    }
 
113
    public static List<Territory> SortTerritoriesByDistance_D(final List<Territory> ters, final GameData data, final Territory target, final Match<Territory> routeMatch)
 
114
    {
 
115
        return DUtils.InvertList(SortTerritoriesByDistance_A(ters, data, target, routeMatch));
 
116
    }
 
117
    public static List<Territory> SortTerritoriesByLandDistance_A(final List<Territory> ters, final GameData data, final Territory target)
 
118
    {
 
119
        return SortListByX(ters, new Comparator<Territory>()
 
120
        {
 
121
            public int compare(Territory t1, Territory t2)
 
122
            {
 
123
                Route route1 = CachedCalculationCenter.GetLandRoute(data, t1, target);
 
124
                Route route2 = CachedCalculationCenter.GetLandRoute(data, t2, target);
 
125
 
 
126
                int distance1 = 0;
 
127
                int distance2 = 0;
 
128
                if(route1 == null)
 
129
                    distance1 = DConstants.Integer_HalfMax;
 
130
                else
 
131
                    distance1 = route1.getLength();
 
132
                if(route2 == null)
 
133
                    distance2 = DConstants.Integer_HalfMax;
 
134
                else
 
135
                    distance2 = route2.getLength();
 
136
 
 
137
                return ((Integer)distance1).compareTo(distance2);
 
138
            }
 
139
        });
 
140
    }
 
141
    public static List<Territory> SortTerritoriesByLandDistance_D(final List<Territory> ters, final GameData data, final Territory target)
 
142
    {
 
143
        return DUtils.InvertList(SortTerritoriesByLandDistance_A(ters, data, target));
 
144
    }
 
145
    public static List<Territory> SortTerritoriesBySeaDistance_A(final List<Territory> ters, final GameData data, final Territory target)
 
146
    {
 
147
        return SortListByX(ters, new Comparator<Territory>()
 
148
        {
 
149
            public int compare(Territory t1, Territory t2)
 
150
            {
 
151
                Route route1 = CachedCalculationCenter.GetSeaRoute(data, t1, target);
 
152
                Route route2 = CachedCalculationCenter.GetSeaRoute(data, t2, target);
 
153
 
 
154
                int distance1 = 0;
 
155
                int distance2 = 0;
 
156
                if(route1 == null)
 
157
                    distance1 = DConstants.Integer_HalfMax;
 
158
                else
 
159
                    distance1 = route1.getLength();
 
160
                if(route2 == null)
 
161
                    distance2 = DConstants.Integer_HalfMax;
 
162
                else
 
163
                    distance2 = route2.getLength();
 
164
 
 
165
                return ((Integer)distance1).compareTo(distance2);
 
166
            }
 
167
        });
 
168
    }
 
169
    public static List<Territory> SortTerritoriesBySeaDistance_D(final List<Territory> ters, final GameData data, final Territory target)
 
170
    {
 
171
        return DUtils.InvertList(SortTerritoriesBySeaDistance_A(ters, data, target));
 
172
    }
 
173
    public static List<Territory> SortTerritoriesByNoCondDistance_A(final List<Territory> ters, final GameData data, final Territory target)
 
174
    {
 
175
        return SortListByX(ters, new Comparator<Territory>()
 
176
        {
 
177
            public int compare(Territory t1, Territory t2)
 
178
            {
 
179
                Route route1 = CachedCalculationCenter.GetRoute(data, t1, target);
 
180
                Route route2 = CachedCalculationCenter.GetRoute(data, t2, target);
 
181
 
 
182
                int distance1 = 0;
 
183
                int distance2 = 0;
 
184
                if(route1 == null)
 
185
                    distance1 = DConstants.Integer_HalfMax;
 
186
                else
 
187
                    distance1 = route1.getLength();
 
188
                if(route2 == null)
 
189
                    distance2 = DConstants.Integer_HalfMax;
 
190
                else
 
191
                    distance2 = route2.getLength();
 
192
 
 
193
                return ((Integer)distance1).compareTo(distance2);
 
194
            }
 
195
        });
 
196
    }
 
197
    public static List<Territory> SortTerritoriesByNoCondDistance_D(final List<Territory> ters, final GameData data, final Territory target)
 
198
    {
 
199
        return DUtils.InvertList(SortTerritoriesByNoCondDistance_A(ters, data, target));
 
200
    }
 
201
    public static List<Territory> SortTerritoriesByLandThenNoCondDistance_A(final List<Territory> ters, final GameData data, final Territory target)
 
202
    {
 
203
        return SortListByX(ters, new Comparator<Territory>()
 
204
        {
 
205
            public int compare(Territory t1, Territory t2)
 
206
            {
 
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);
 
211
 
 
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)
 
215
                    return 1;
 
216
                if(route2_nc == null)
 
217
                    return -1;
 
218
 
 
219
                int distance1 = route1_nc.getLength() * 100;
 
220
                int distance2 = route2_nc.getLength() * 100;
 
221
                if(route1 != null)
 
222
                    distance1 = route1.getLength();
 
223
                if(route2 != null)
 
224
                    distance2 = route2.getLength();
 
225
 
 
226
                return ((Integer)distance1).compareTo(distance2);
 
227
            }
 
228
        });
 
229
    }
 
230
    public static List<Territory> SortTerritoriesByLandThenNoCondDistance_D(final List<Territory> ters, final GameData data, final Territory target)
 
231
    {
 
232
        return DUtils.InvertList(SortTerritoriesByLandThenNoCondDistance_A(ters, data, target));
 
233
    }
 
234
    ///////////////////////////////////////////////End Territory Sorting///////////////////////////////////////////////
 
235
 
 
236
    ///////////////////////////////////////////////Unit Sorting///////////////////////////////////////////////
 
237
    public static List<Unit> SortUnitsByCost_A(List<Unit> units, final Resource resource)
 
238
    {
 
239
        return SortListByX(units, new Comparator<Unit>()
 
240
        {
 
241
            public int compare(Unit o1, Unit o2)
 
242
            {
 
243
                int cost1 = DUtils.GetTUVOfUnits(Collections.singletonList(o1), resource);
 
244
                int cost2 = DUtils.GetTUVOfUnits(Collections.singletonList(o2), resource);
 
245
 
 
246
                return ((Integer)cost1).compareTo(cost2);
 
247
            }
 
248
        });
 
249
    }
 
250
    ///////////////////////////////////////////////End Unit Sorting///////////////////////////////////////////////
 
251
}