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

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/Dynamix_AI/CommandCenter/GlobalCenter.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.CommandCenter;
 
16
 
 
17
import games.strategy.engine.data.GameData;
 
18
import games.strategy.engine.data.NamedAttachable;
 
19
import games.strategy.engine.data.PlayerID;
 
20
import games.strategy.engine.data.ProductionFrontier;
 
21
import games.strategy.engine.data.ProductionRule;
 
22
import games.strategy.engine.data.Resource;
 
23
import games.strategy.engine.data.UnitType;
 
24
import games.strategy.triplea.Constants;
 
25
import games.strategy.triplea.Dynamix_AI.DUtils;
 
26
import games.strategy.triplea.Dynamix_AI.Others.PhaseType;
 
27
import games.strategy.triplea.attatchments.UnitAttachment;
 
28
import games.strategy.util.IntegerMap;
 
29
import java.util.ArrayList;
 
30
import java.util.HashMap;
 
31
import java.util.List;
 
32
 
 
33
/**
 
34
 *
 
35
 * @author Stephen
 
36
 */
 
37
public class GlobalCenter
 
38
{
 
39
    public static boolean IsPaused = false;
 
40
    public static final Object IsPaused_Object = new Object();
 
41
 
 
42
    public static PhaseType FirstDynamixPhase;
 
43
    public static PlayerID FirstDynamixPlayer;
 
44
    public static int GameRound;
 
45
    private static Resource PUResource;
 
46
    public static Resource GetPUResource()
 
47
    {
 
48
        return PUResource;
 
49
    }
 
50
    public static void Initialize(GameData data)
 
51
    {
 
52
        //This part just resets all the static variables to their default value so that other code will be able to fill in the real info
 
53
        //(For example, the Dynamix_AI class sets the FirstDynamixPhase variable)
 
54
        FirstDynamixPhase = PhaseType.Unknown;
 
55
        FirstDynamixPlayer = null;
 
56
        GameRound = 0;
 
57
        PUResource = null;
 
58
        CurrentPlayer = null;
 
59
        MapTerCount = 0;
 
60
        MapTerCountScale = 1.0F;
 
61
        CurrentPhaseType = PhaseType.Unknown;
 
62
        IsFFAGame = false;
 
63
        FastestUnitMovement = 0;
 
64
        FastestLandUnitMovement = 0;
 
65
        HighestTerProduction = -1;
 
66
        PUsAtEndOfLastTurn = 0;
 
67
        MergedAndAveragedProductionFronter = null;
 
68
        AllMapUnitTypes = null;
 
69
        
 
70
        //Now we 'initialize' by filling in some of the values (the rest are filled in somewhere else)
 
71
        PUResource = data.getResourceList().getResource(Constants.PUS);
 
72
        MapTerCount = data.getMap().getTerritories().size();
 
73
        //75 is considered the 'base' map ter count (For comparison, Great Lakes War has 90)
 
74
        MapTerCountScale = ((float)data.getMap().getTerritories().size() / 75.0F);
 
75
 
 
76
        IsFFAGame = true;
 
77
        for(String alliance : data.getAllianceTracker().getAlliances())
 
78
        {
 
79
            List<PlayerID> playersInAlliance = DUtils.ToList(data.getAllianceTracker().getPlayersInAlliance(alliance));
 
80
            if(playersInAlliance.size() > 1)
 
81
            {
 
82
                IsFFAGame = false;
 
83
                break;
 
84
            }
 
85
        }
 
86
        HighestTerProduction = DUtils.GetHighestTerProduction(data);
 
87
 
 
88
        GenerateMergedAndAveragedProductionFrontier(data);
 
89
    }
 
90
    public static PlayerID CurrentPlayer;
 
91
    public static int MapTerCount;
 
92
    /**Please use this for all hard-coded values. (Multiply the hard-coded value by this float, and the hard-coded value will scale up or down with the maps*/
 
93
    public static float MapTerCountScale;
 
94
    public static PhaseType CurrentPhaseType;
 
95
    public static boolean IsFFAGame;
 
96
    public static int FastestUnitMovement;
 
97
    public static int FastestLandUnitMovement;
 
98
    public static int HighestTerProduction;
 
99
    public static int PUsAtEndOfLastTurn;
 
100
 
 
101
    private static ProductionFrontier MergedAndAveragedProductionFronter;
 
102
    public static List<UnitType> AllMapUnitTypes;
 
103
    /**
 
104
     * Generates a merged and averaged production frontier that can be used to determine TUV of units even when player is neutral or unknown.
 
105
     * This method also sets the global FastestUnitMovement value and the AllMapUnitTypes list.
 
106
     */
 
107
    private static void GenerateMergedAndAveragedProductionFrontier(GameData data)
 
108
    {
 
109
        MergedAndAveragedProductionFronter = new ProductionFrontier("Merged and averaged global production frontier", data);
 
110
        AllMapUnitTypes = new ArrayList<UnitType>();
 
111
 
 
112
        HashMap<UnitType, Integer> purchaseCountsForUnit = new HashMap<UnitType, Integer>();
 
113
        HashMap<UnitType, List<Integer>> differentCosts = new HashMap<UnitType, List<Integer>>();
 
114
        for(PlayerID player : data.getPlayerList().getPlayers())
 
115
        {
 
116
            if(player.getProductionFrontier() == null)
 
117
                continue;
 
118
            for(ProductionRule rule : player.getProductionFrontier())
 
119
            {
 
120
                UnitType ut = (UnitType)rule.getResults().keySet().iterator().next();
 
121
                UnitAttachment ua = UnitAttachment.get(ut);
 
122
                DUtils.AddObjToListValueForKeyInMap(differentCosts, ut, rule.getCosts().getInt(PUResource));
 
123
                purchaseCountsForUnit.put(ut, rule.getResults().keySet().size());
 
124
 
 
125
                int movement = ua.getMovement(player);
 
126
                if(movement > FastestUnitMovement)
 
127
                    FastestUnitMovement = movement;
 
128
                if(movement > FastestLandUnitMovement && !ua.isSea() && !ua.isAir())
 
129
                    FastestLandUnitMovement = movement;
 
130
 
 
131
                AllMapUnitTypes.add(ut);
 
132
            }
 
133
        }
 
134
 
 
135
        for(UnitType unitType : differentCosts.keySet())
 
136
        {
 
137
            int totalCosts = 0;
 
138
            List<Integer> costs = differentCosts.get(unitType);
 
139
            for(int cost : costs)
 
140
            {
 
141
                totalCosts += cost;
 
142
            }
 
143
            int averagedCost = (int)((float)totalCosts / (float)costs.size());
 
144
 
 
145
            IntegerMap<NamedAttachable> results = new IntegerMap<NamedAttachable>();
 
146
            results.put(unitType, purchaseCountsForUnit.get(unitType));
 
147
            IntegerMap<Resource> cost = new IntegerMap<Resource>();
 
148
            cost.put(PUResource, averagedCost);
 
149
            ProductionRule rule = new ProductionRule("Averaged production rule for unit " + unitType.getName(), data, results, cost);
 
150
            MergedAndAveragedProductionFronter.addRule(rule);
 
151
        }
 
152
    }
 
153
    public static ProductionFrontier GetMergedAndAveragedProductionFrontier()
 
154
    {
 
155
        return MergedAndAveragedProductionFronter;
 
156
    }
 
157
}