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

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/Dynamix_AI/Group/PurchaseGroup.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.Group;
 
16
 
 
17
import games.strategy.engine.data.GameData;
 
18
import games.strategy.engine.data.PlayerID;
 
19
import games.strategy.engine.data.ProductionRule;
 
20
import games.strategy.engine.data.Unit;
 
21
import games.strategy.engine.data.UnitType;
 
22
import games.strategy.triplea.Constants;
 
23
import games.strategy.triplea.Dynamix_AI.DUtils;
 
24
import games.strategy.triplea.Dynamix_AI.Dynamix_AI;
 
25
import games.strategy.triplea.delegate.remote.IPurchaseDelegate;
 
26
import games.strategy.util.IntegerMap;
 
27
import java.util.ArrayList;
 
28
import java.util.Collection;
 
29
import java.util.Collections;
 
30
import java.util.List;
 
31
import java.util.logging.Level;
 
32
 
 
33
/**
 
34
 *
 
35
 * @author Stephen
 
36
 */
 
37
public class PurchaseGroup
 
38
{
 
39
    private Collection<Unit> m_units = new ArrayList<Unit>();
 
40
    private IPurchaseDelegate m_purchaser = null;
 
41
    private GameData m_data = null;
 
42
    private PlayerID m_player = null;
 
43
    private IntegerMap<ProductionRule> m_generatedRules = new IntegerMap<ProductionRule>();
 
44
    private List<Unit> m_generatedSampleUnits = new ArrayList<Unit>();
 
45
    private int m_totalPurchasePrice = 0;
 
46
    public PurchaseGroup(Unit unit, IPurchaseDelegate purchaser, GameData data, PlayerID player)
 
47
    {
 
48
        m_units = Collections.singleton(unit);
 
49
        m_purchaser = purchaser;
 
50
        m_data = data;
 
51
        m_player = player;
 
52
        GenerateProductionRulesAndSampleUnits();
 
53
    }
 
54
    public PurchaseGroup(Collection<Unit> units, IPurchaseDelegate purchaser, GameData data, PlayerID player)
 
55
    {
 
56
        m_units = units;
 
57
        m_purchaser = purchaser;
 
58
        m_data = data;
 
59
        m_player = player;
 
60
        GenerateProductionRulesAndSampleUnits();
 
61
    }
 
62
    private void GenerateProductionRulesAndSampleUnits()
 
63
    {
 
64
        List<ProductionRule> rules = m_player.getProductionFrontier().getRules();
 
65
        int index = 0;
 
66
        m_generatedRules.clear();
 
67
        m_generatedSampleUnits.clear();
 
68
        m_totalPurchasePrice = 0;
 
69
 
 
70
        int totalUnitRulesCosts = 0;
 
71
        for (Unit unit : m_units)
 
72
        {
 
73
            for (ProductionRule rule : rules)
 
74
            {
 
75
                if (rule != null && rule.getResults() != null && rule.getResults().keySet() != null && rule.getResults().keySet().toArray() != null && rule.getResults().keySet().toArray().length > 0 && rule.getResults().keySet().toArray()[0] != null && rule.getResults().keySet().toArray()[0] instanceof UnitType && unit != null && unit.getUnitType() != null && ((UnitType) rule.getResults().keySet().toArray()[0]) == unit.getUnitType())
 
76
                {
 
77
                    int cost = rule.getCosts().getInt(m_data.getResourceList().getResource(Constants.PUS));
 
78
                    totalUnitRulesCosts += cost;
 
79
                }
 
80
            }
 
81
        }
 
82
        int timesUnitsCanBeBought = (m_maxPurchaseCost / totalUnitRulesCosts);
 
83
        int timesEachUnitShouldBeBought = Math.min(timesUnitsCanBeBought, m_maxPurchaseCount / m_units.size());
 
84
        timesEachUnitShouldBeBought = Math.max(timesEachUnitShouldBeBought, 1); //Buy at least one
 
85
        for (Unit unit : m_units)
 
86
        {
 
87
            for (ProductionRule rule : rules)
 
88
            {
 
89
                if (rule != null && rule.getResults() != null && rule.getResults().keySet() != null && rule.getResults().keySet().toArray() != null && rule.getResults().keySet().toArray().length > 0 && rule.getResults().keySet().toArray()[0] != null && rule.getResults().keySet().toArray()[0] instanceof UnitType && unit != null && unit.getUnitType() != null && ((UnitType) rule.getResults().keySet().toArray()[0]) == unit.getUnitType())
 
90
                {
 
91
                    int cost = rule.getCosts().getInt(m_data.getResourceList().getResource(Constants.PUS));
 
92
                    m_generatedRules.add(rule, timesEachUnitShouldBeBought);
 
93
                    m_totalPurchasePrice += cost * timesEachUnitShouldBeBought;
 
94
                    for (int i = 0; i < timesEachUnitShouldBeBought; i++)
 
95
                    {
 
96
                        m_generatedSampleUnits.add(unit.getType().create(m_player));
 
97
                    }
 
98
                    index++;
 
99
                    break;
 
100
                }
 
101
            }
 
102
        }
 
103
    }
 
104
    public int GetCost()
 
105
    {
 
106
        return m_totalPurchasePrice;
 
107
    }
 
108
    private int m_maxPurchaseCost = Integer.MAX_VALUE;
 
109
    private int m_maxPurchaseCount = 1;
 
110
    public void ApplyMaxValues(int maxPurchaseCost, int maxPurchaseCount)
 
111
    {
 
112
        if (maxPurchaseCost != m_maxPurchaseCost || maxPurchaseCount != m_maxPurchaseCount)
 
113
        {
 
114
            m_maxPurchaseCost = maxPurchaseCost;
 
115
            m_maxPurchaseCount = maxPurchaseCount;
 
116
            GenerateProductionRulesAndSampleUnits();
 
117
        }
 
118
    }
 
119
    public int Purchase()
 
120
    {
 
121
        Dynamix_AI.Pause();
 
122
        m_purchaser.purchase(m_generatedRules);
 
123
        DUtils.Log(Level.FINER, "      Purchase made. Units: {0}", DUtils.UnitList_ToString(GetSampleUnits()));
 
124
        return m_totalPurchasePrice;
 
125
    }
 
126
    public List<Unit> GetSampleUnits()
 
127
    {
 
128
        return m_generatedSampleUnits;
 
129
    }
 
130
}