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

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/ui/StatPanel.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
 * StatPanel.java
 
17
 *
 
18
 * Created on January 6, 2002, 4:07 PM
 
19
 */
 
20
 
 
21
package games.strategy.triplea.ui;
 
22
 
 
23
import games.strategy.engine.data.Change;
 
24
import games.strategy.engine.data.GameData;
 
25
import games.strategy.engine.data.GameStep;
 
26
import games.strategy.engine.data.PlayerID;
 
27
import games.strategy.engine.data.Resource;
 
28
import games.strategy.engine.data.Territory;
 
29
import games.strategy.engine.data.Unit;
 
30
import games.strategy.engine.data.UnitType;
 
31
import games.strategy.engine.data.events.GameDataChangeListener;
 
32
import games.strategy.engine.stats.AbstractStat;
 
33
import games.strategy.engine.stats.IStat;
 
34
import games.strategy.triplea.Constants;
 
35
import games.strategy.triplea.Properties;
 
36
import games.strategy.triplea.attatchments.PlayerAttachment;
 
37
import games.strategy.triplea.attatchments.TerritoryAttachment;
 
38
import games.strategy.triplea.delegate.BattleCalculator;
 
39
import games.strategy.triplea.delegate.Matches;
 
40
import games.strategy.triplea.delegate.OriginalOwnerTracker;
 
41
import games.strategy.triplea.delegate.TechAdvance;
 
42
import games.strategy.triplea.delegate.TechTracker;
 
43
import games.strategy.util.IntegerMap;
 
44
import games.strategy.util.Match;
 
45
 
 
46
import java.awt.Graphics;
 
47
import java.awt.GridLayout;
 
48
import java.awt.Image;
 
49
import java.util.ArrayList;
 
50
import java.util.Arrays;
 
51
import java.util.Collection;
 
52
import java.util.Collections;
 
53
import java.util.Comparator;
 
54
import java.util.HashMap;
 
55
import java.util.Iterator;
 
56
import java.util.List;
 
57
import java.util.Map;
 
58
import java.util.TreeSet;
 
59
 
 
60
import javax.swing.JPanel;
 
61
import javax.swing.JScrollPane;
 
62
import javax.swing.JTable;
 
63
import javax.swing.SwingUtilities;
 
64
import javax.swing.table.AbstractTableModel;
 
65
import javax.swing.table.TableColumn;
 
66
import javax.swing.table.TableModel;
 
67
 
 
68
/**
 
69
 * 
 
70
 * @author Sean Bridges
 
71
 */
 
72
public class StatPanel extends JPanel
 
73
{
 
74
    private final StatTableModel m_dataModel;
 
75
    private final TechTableModel m_techModel;
 
76
    private IStat[] m_stats = new IStat[] {new PUStat(), new ProductionStat(), new UnitsStat(), new TUVStat()};
 
77
    private GameData m_data;
 
78
    private JTable m_statsTable;
 
79
    private Image m_statsImage;
 
80
    
 
81
    //sort based on first step
 
82
    private final Comparator<PlayerID> m_playerOrderComparator = new Comparator<PlayerID>()
 
83
    {
 
84
        public int compare(PlayerID p1, PlayerID p2)
 
85
        {
 
86
            
 
87
            Iterator iter = m_data.getSequence().iterator();
 
88
            
 
89
            while(iter.hasNext())
 
90
            {
 
91
                GameStep s = (GameStep) iter.next();
 
92
                
 
93
                if(s.getPlayerID() == null)
 
94
                    continue;
 
95
                
 
96
                if(s.getPlayerID().equals(p1))
 
97
                    return -1;
 
98
                else if(s.getPlayerID().equals(p2))
 
99
                    return 1;
 
100
            }
 
101
            return 0;
 
102
        }
 
103
        
 
104
    };
 
105
 
 
106
    /** Creates a new instance of InfoPanel */
 
107
    public StatPanel(GameData data)
 
108
    {
 
109
        m_data = data;
 
110
        //only add the vc stat if we have some victory cities
 
111
        if(Match.someMatch(data.getMap().getTerritories(), Matches.TerritoryIsVictoryCity))
 
112
        {
 
113
            List<IStat> stats = new ArrayList<IStat>(Arrays.asList(m_stats));
 
114
            stats.add(new VictoryCityStat());
 
115
            m_stats = stats.toArray(new IStat[stats.size()]);
 
116
        }
 
117
        //only add the vps in pacific
 
118
        if(data.getProperties().get(Constants.PACIFIC_THEATER, false))
 
119
        {
 
120
            List<IStat> stats = new ArrayList<IStat>(Arrays.asList(m_stats));
 
121
            stats.add(new VPStat());
 
122
            m_stats = stats.toArray(new IStat[stats.size()]);
 
123
        }
 
124
        
 
125
        setLayout(new GridLayout(2, 1));
 
126
 
 
127
        m_dataModel = new StatTableModel();
 
128
        m_techModel = new TechTableModel();
 
129
        m_statsImage = null;
 
130
        m_statsTable = new JTable(m_dataModel) {
 
131
 
 
132
            public void print(Graphics g)
 
133
            {
 
134
                if(m_statsImage != null)
 
135
                    g.drawImage(m_statsImage, 0, 0, null, null);
 
136
                super.print(g);
 
137
            }
 
138
        };
 
139
        JTable table = m_statsTable;
 
140
        // Strangely, this is enabled by default
 
141
        table.getTableHeader().setReorderingAllowed(false);
 
142
 
 
143
        // Set width of country column
 
144
        TableColumn column = table.getColumnModel().getColumn(0);
 
145
        column.setPreferredWidth(175);
 
146
 
 
147
        JScrollPane scroll = new JScrollPane(table);
 
148
        // add(scroll, BorderLayout.NORTH);
 
149
        add(scroll);
 
150
 
 
151
        table = new JTable(m_techModel);
 
152
        // Strangely, this is enabled by default
 
153
        table.getTableHeader().setReorderingAllowed(false);
 
154
 
 
155
        // Make the technology column big. Value chosen by trial and error
 
156
        // The right way to do this is probably to get a FontMetrics object
 
157
        // and measure the pixel width of the longest technology name in the
 
158
        // current font.
 
159
        column = table.getColumnModel().getColumn(0);
 
160
        column.setPreferredWidth(500);
 
161
 
 
162
        scroll = new JScrollPane(table);
 
163
        // add(scroll, BorderLayout.SOUTH);
 
164
        add(scroll);
 
165
 
 
166
    }    
 
167
    
 
168
    
 
169
    public void setGameData(GameData data)
 
170
    {
 
171
        m_data = data;
 
172
        m_dataModel.setGameData(data);
 
173
        m_techModel.setGameData(data);
 
174
        m_dataModel.gameDataChanged(null);
 
175
        m_techModel.gameDataChanged(null);
 
176
 
 
177
    }
 
178
 
 
179
    public void setStatsBgImage(Image image)
 
180
    {
 
181
        m_statsImage = image;
 
182
    }
 
183
 
 
184
    public StatTableModel getStatsModel()
 
185
    {
 
186
        return m_dataModel;
 
187
    }
 
188
 
 
189
    public JTable getStatsTable()
 
190
    {
 
191
        return m_statsTable;
 
192
    }
 
193
 
 
194
    public TableModel getTechModel()
 
195
    {
 
196
        return m_techModel;
 
197
    }
 
198
    
 
199
    /**
 
200
     * 
 
201
     * @return all the alliances with more than one player.
 
202
     */
 
203
    public Collection<String> getAlliances()
 
204
    {
 
205
        Iterator allAlliances = m_data.getAllianceTracker().getAlliances().iterator();
 
206
        //order the alliances use a Tree Set
 
207
        Collection<String> rVal = new TreeSet<String>();
 
208
 
 
209
        while (allAlliances.hasNext())
 
210
        {
 
211
            String alliance = (String) allAlliances.next();
 
212
            if (m_data.getAllianceTracker().getPlayersInAlliance(alliance).size() > 1)
 
213
            {
 
214
                rVal.add(alliance);
 
215
            }
 
216
        }
 
217
        return rVal;
 
218
    }
 
219
 
 
220
    
 
221
    public List<PlayerID> getPlayers()
 
222
    {
 
223
        List<PlayerID> players = new ArrayList<PlayerID>( m_data.getPlayerList().getPlayers());
 
224
        Collections.sort(players,m_playerOrderComparator);
 
225
        return players;
 
226
        
 
227
    }
 
228
 
 
229
    public IStat[] getStats()
 
230
    {
 
231
        return m_stats;
 
232
    }
 
233
    
 
234
 
 
235
    
 
236
    /*
 
237
     * Custom table model.
 
238
     * 
 
239
     * This model is thread safe.
 
240
     */
 
241
    class StatTableModel extends AbstractTableModel implements GameDataChangeListener
 
242
    {
 
243
        /* Flag to indicate whether data needs to be recalculated */
 
244
        private boolean m_isDirty = true;
 
245
        /* Column Header Names */
 
246
 
 
247
        
 
248
        /* Underlying data for the table */
 
249
        private String[][] m_collectedData;
 
250
        
 
251
        
 
252
        public StatTableModel()
 
253
        {
 
254
            m_data.addDataChangeListener(this);
 
255
            m_isDirty = true;
 
256
            
 
257
        }
 
258
 
 
259
        private synchronized void loadData()
 
260
        {
 
261
            m_data.acquireReadLock();
 
262
            try
 
263
            {
 
264
                List players = getPlayers();
 
265
                Collection<String> alliances = getAlliances();
 
266
                    
 
267
                    m_collectedData = new String[players.size() + alliances.size()][m_stats.length + 1];
 
268
                    
 
269
                    int row = 0;
 
270
                    Iterator playerIter = players.iterator();
 
271
                    while (playerIter.hasNext())
 
272
                    {
 
273
                        PlayerID player = (PlayerID) playerIter.next();
 
274
                        
 
275
                        m_collectedData[row][0] = player.getName();
 
276
                        for(int i = 0; i < m_stats.length; i++)
 
277
                        {
 
278
                            m_collectedData[row][i+1] = m_stats[i].getFormatter().format(m_stats[i].getValue(player, m_data));
 
279
                        }
 
280
                        row++;
 
281
                    }
 
282
                    Iterator<String> allianceIterator = alliances.iterator();
 
283
                    while (allianceIterator.hasNext())
 
284
                    {
 
285
                        String alliance = allianceIterator.next();
 
286
                        
 
287
                        m_collectedData[row][0] = alliance;
 
288
                        for(int i = 0; i < m_stats.length; i++)
 
289
                        {
 
290
                            m_collectedData[row][i+1] = m_stats[i].getFormatter().format(m_stats[i].getValue(alliance, m_data));
 
291
                        }
 
292
                        row++;
 
293
                    }
 
294
            }
 
295
            finally
 
296
            {
 
297
                m_data.releaseReadLock();
 
298
            }
 
299
            
 
300
        }
 
301
 
 
302
 
 
303
 
 
304
        public void gameDataChanged(Change aChange)
 
305
        {
 
306
            synchronized(this)
 
307
            {
 
308
                m_isDirty = true;
 
309
            }
 
310
            
 
311
            SwingUtilities.invokeLater(new Runnable()
 
312
            {
 
313
                public void run()
 
314
                {
 
315
                    repaint();
 
316
                }
 
317
            
 
318
            });
 
319
            
 
320
        }
 
321
 
 
322
        /*
 
323
         * Recalcs the underlying data in a lazy manner Limitation: This is not
 
324
         * a threadsafe implementation
 
325
         */
 
326
        public synchronized Object getValueAt(int row, int col)
 
327
        {
 
328
            if (m_isDirty)
 
329
            {
 
330
                loadData();
 
331
                m_isDirty = false;
 
332
            }
 
333
 
 
334
            return m_collectedData[row][col];
 
335
        }
 
336
 
 
337
        // Trivial implementations of required methods
 
338
        public String getColumnName(int col)
 
339
        {
 
340
            if(col == 0)
 
341
                return "Player";
 
342
            return 
 
343
                m_stats[col -1].getName();
 
344
        }
 
345
 
 
346
        public int getColumnCount()
 
347
        {
 
348
            return m_stats.length + 1;
 
349
        }
 
350
 
 
351
        public synchronized int getRowCount()
 
352
        {
 
353
            if(!m_isDirty)
 
354
                return m_collectedData.length;
 
355
            else
 
356
            {
 
357
                //no need to recalculate all the stats just to get the row count
 
358
                //getting the row count is a fairly frequent operation, and will
 
359
                //happen even if we are not displayed!
 
360
                m_data.acquireReadLock();
 
361
                try
 
362
                {
 
363
                    return m_data.getPlayerList().size() + getAlliances().size();
 
364
 
 
365
                }
 
366
                finally
 
367
                {
 
368
                  m_data.releaseReadLock();  
 
369
                }
 
370
            }
 
371
        }
 
372
 
 
373
        public synchronized void setGameData(GameData data)
 
374
        {
 
375
            synchronized(this)
 
376
            {
 
377
                    m_data.removeDataChangeListener(this);
 
378
                    m_data = data;
 
379
                    m_data.addDataChangeListener(this);
 
380
                    m_isDirty = true;
 
381
            }
 
382
            repaint();
 
383
        }
 
384
 
 
385
    }
 
386
 
 
387
    class TechTableModel extends AbstractTableModel implements GameDataChangeListener
 
388
    {
 
389
        /* Flag to indicate whether data needs to be recalculated */
 
390
        private boolean isDirty = true;
 
391
        /* Column Header Names */
 
392
 
 
393
        /* Row Header Names */
 
394
        private String[] colList;
 
395
        /* Underlying data for the table */
 
396
        private String[][] data;
 
397
        /* Convenience mapping of country names -> col */
 
398
        private Map<String, Integer> colMap = null;
 
399
        /* Convenience mapping of technology names -> row */
 
400
        private Map<String, Integer> rowMap = null;
 
401
 
 
402
        public TechTableModel()
 
403
        {
 
404
            m_data.addDataChangeListener(this);
 
405
 
 
406
            initColList();
 
407
 
 
408
            /* Load the country -> col mapping */
 
409
            colMap = new HashMap<String, Integer>();
 
410
            for (int i = 0; i < colList.length; i++)
 
411
            {
 
412
                colMap.put(colList[i], new Integer(i + 1));
 
413
            }
 
414
 
 
415
            /*
 
416
             * .size()+1 added to stop index out of bounds errors when using an
 
417
             * Italian player.
 
418
             */
 
419
            boolean useTech = false;
 
420
            if(m_data.getResourceList().getResource(Constants.TECH_TOKENS) != null)
 
421
                {
 
422
                useTech = true;
 
423
                data = new String[TechAdvance.getTechAdvances(m_data,null).size()+1][colList.length + 2];
 
424
                }
 
425
            else
 
426
            {
 
427
                data = new String[TechAdvance.getTechAdvances(m_data,null).size()][colList.length + 1]; 
 
428
            }
 
429
           
 
430
            /* Load the technology -> row mapping */
 
431
            rowMap = new HashMap<String, Integer>();
 
432
            Iterator iter = TechAdvance.getTechAdvances(m_data,null).iterator();
 
433
            int row = 0;
 
434
 
 
435
            if (useTech)
 
436
            {
 
437
                rowMap.put("Tokens", new Integer(row));
 
438
                data[row][0] = "Tokens";
 
439
                row++;
 
440
            }
 
441
 
 
442
            while (iter.hasNext())
 
443
            {
 
444
                TechAdvance tech = (TechAdvance) iter.next();
 
445
                rowMap.put((tech).getName(), new Integer(row));
 
446
                data[row][0] = tech.getName();
 
447
                row++;
 
448
            }
 
449
 
 
450
            clearAdvances();
 
451
        }
 
452
 
 
453
        private void clearAdvances()
 
454
        {
 
455
 
 
456
            /* Initialize the table with the tech names */
 
457
            for (int i = 0; i < data.length; i++)
 
458
            {
 
459
                for (int j = 1; j <= colList.length; j++)
 
460
                {
 
461
                    data[i][j] = "";
 
462
                }
 
463
            }
 
464
 
 
465
        }
 
466
 
 
467
        private void initColList()
 
468
        {
 
469
            java.util.List<PlayerID> players = new ArrayList<PlayerID>(m_data.getPlayerList().getPlayers());
 
470
 
 
471
            colList = new String[players.size()];
 
472
 
 
473
            for (int i = 0; i < players.size(); i++)
 
474
            {
 
475
                colList[i] = players.get(i).getName();
 
476
            }
 
477
 
 
478
            Arrays.sort(colList, 0, players.size());
 
479
        }
 
480
 
 
481
        public void update()
 
482
        {
 
483
            clearAdvances();
 
484
            //copy so aquire/release read lock are on the same object!
 
485
            final GameData gameData = m_data;
 
486
            
 
487
            gameData.acquireReadLock();
 
488
            try
 
489
            {
 
490
                Iterator playerIter = gameData.getPlayerList().getPlayers().iterator();
 
491
                while (playerIter.hasNext())
 
492
                {
 
493
                    PlayerID pid = (PlayerID) playerIter.next();
 
494
                    if (colMap.get(pid.getName()) == null)
 
495
                        throw new IllegalStateException("Unexpected player in GameData.getPlayerList()" + pid.getName());
 
496
    
 
497
                    int col = colMap.get(pid.getName()).intValue();
 
498
 
 
499
                    int row = 0;
 
500
                    boolean useTokens = false;
 
501
                        if(m_data.getResourceList().getResource(Constants.TECH_TOKENS) != null)
 
502
                        {
 
503
                                useTokens = true;
 
504
                                Integer tokens = pid.getResources().getQuantity(Constants.TECH_TOKENS);
 
505
                                data[row][col] = tokens.toString();
 
506
                        }
 
507
 
 
508
                    Iterator advances = TechTracker.getTechAdvances(pid,m_data).iterator();
 
509
    
 
510
                    while (advances.hasNext())
 
511
                    {
 
512
                        
 
513
                        TechAdvance advance = (TechAdvance) advances.next();
 
514
                        row = rowMap.get(advance.getName()).intValue();
 
515
                        // System.err.println("(" + row + ", " + col + ")");
 
516
                        data[row][col] = "X";
 
517
                        // data[row][col] = colList[col].substring(0, 1);
 
518
                    }
 
519
                    advances = TechAdvance.getTechAdvances(m_data,null).iterator();
 
520
                    List<TechAdvance> has = TechAdvance.getTechAdvances(m_data,pid);
 
521
                    while (advances.hasNext())
 
522
                    {
 
523
                        TechAdvance advance = (TechAdvance) advances.next();
 
524
                        //if(!pid.getTechnologyFrontierList().getAdvances().contains(advance)){
 
525
                        if(!has.contains(advance)) {
 
526
                                row = rowMap.get(advance.getName()).intValue();     
 
527
                                data[row][col] = "-";
 
528
                        }
 
529
                    }
 
530
                }
 
531
            }
 
532
            finally
 
533
            {
 
534
                gameData.releaseReadLock();
 
535
            }
 
536
        }
 
537
 
 
538
        public String getColumnName(int col)
 
539
        {
 
540
            if (col == 0)
 
541
                return "Technology";
 
542
            return colList[col - 1].substring(0, 1);
 
543
        }
 
544
 
 
545
        /*
 
546
         * Recalcs the underlying data in a lazy manner Limitation: This is not
 
547
         * a threadsafe implementation
 
548
         */
 
549
        public Object getValueAt(int row, int col)
 
550
        {
 
551
            if (isDirty)
 
552
            {
 
553
                update();
 
554
                isDirty = false;
 
555
            }
 
556
 
 
557
            return data[row][col];
 
558
        }
 
559
 
 
560
        // Trivial implementations of required methods
 
561
        public int getColumnCount()
 
562
        {
 
563
            return colList.length + 1;
 
564
        }
 
565
 
 
566
        public int getRowCount()
 
567
        {
 
568
            return data.length;
 
569
        }
 
570
 
 
571
        public void gameDataChanged(Change aChange)
 
572
        {
 
573
            isDirty = true;
 
574
            
 
575
            SwingUtilities.invokeLater(new Runnable()
 
576
            {
 
577
            
 
578
                public void run()
 
579
                {
 
580
                    repaint();
 
581
                }
 
582
            
 
583
            });
 
584
        }
 
585
 
 
586
        public void setGameData(GameData data)
 
587
        {
 
588
            m_data.removeDataChangeListener(this);
 
589
            m_data = data;
 
590
            m_data.addDataChangeListener(this);
 
591
            isDirty = true;
 
592
        }
 
593
    }
 
594
}
 
595
 
 
596
 
 
597
 
 
598
class ProductionStat extends AbstractStat
 
599
{
 
600
 
 
601
    public String getName()
 
602
    {
 
603
        return "Production";
 
604
    }
 
605
 
 
606
    public double getValue(PlayerID player, GameData data)
 
607
    {
 
608
        int rVal = 0; 
 
609
        Iterator iter = data.getMap().getTerritories().iterator();
 
610
        while (iter.hasNext())
 
611
        {
 
612
            boolean isOwnedConvoyOrLand = false; 
 
613
            Territory place = (Territory) iter.next();
 
614
            OriginalOwnerTracker origOwnerTracker = new OriginalOwnerTracker();
 
615
            TerritoryAttachment ta = TerritoryAttachment.get(place);
 
616
 
 
617
            /* Check if terr is a Land Convoy Route and check ownership of neighboring Sea Zone*/
 
618
            if(ta != null)
 
619
            {   
 
620
                //if it's water, it is a Convoy Center
 
621
                if (place.isWater())
 
622
                {
 
623
                        //Preset the original owner
 
624
                        PlayerID origOwner = ta.getOccupiedTerrOf();
 
625
                        if(origOwner == null)
 
626
                                origOwner = origOwnerTracker.getOriginalOwner(place);
 
627
                        
 
628
                        //Can't get PUs for capturing a CC, only original owner can get them.
 
629
                        if (origOwner != PlayerID.NULL_PLAYERID && origOwner == player)
 
630
                                isOwnedConvoyOrLand = true;
 
631
 
 
632
                        if(origOwner == null)
 
633
                                isOwnedConvoyOrLand = true;
 
634
                }
 
635
                else
 
636
                {
 
637
                        //if it's a convoy route
 
638
                        if (TerritoryAttachment.get(place).isConvoyRoute())
 
639
                        {
 
640
                                //Determine if both parts of the convoy route are owned by the attacker or allies
 
641
                                boolean ownedConvoyRoute =  data.getMap().getNeighbors(place, Matches.territoryHasConvoyOwnedBy(player, data, place)).size() > 0;
 
642
                                if(ownedConvoyRoute)
 
643
                                        isOwnedConvoyOrLand = true;                        
 
644
                        }
 
645
                        //it's a regular land territory
 
646
                        else
 
647
                        {
 
648
                                isOwnedConvoyOrLand = true;
 
649
                        }       
 
650
                }
 
651
 
 
652
                /*add 'em all up*/
 
653
                if(place.getOwner().equals(player) && isOwnedConvoyOrLand)
 
654
                {
 
655
                        rVal += ta.getProduction();
 
656
                }
 
657
            }            
 
658
        }
 
659
        rVal*= Properties.getPU_Multiplier(data);
 
660
        return rVal;
 
661
    }
 
662
    
 
663
}
 
664
 
 
665
class PUStat extends AbstractStat
 
666
{
 
667
 
 
668
    public String getName()
 
669
    {
 
670
        return "PUs";
 
671
    }
 
672
 
 
673
    public double getValue(PlayerID player, GameData data)
 
674
    {
 
675
        return player.getResources().getQuantity(Constants.PUS);
 
676
    }
 
677
}
 
678
 
 
679
 
 
680
class UnitsStat extends AbstractStat
 
681
{
 
682
 
 
683
    public String getName()
 
684
    {
 
685
        return "Units";
 
686
    }
 
687
 
 
688
    public double getValue(PlayerID player, GameData data)
 
689
    {
 
690
        int rVal = 0; 
 
691
        Match<Unit> ownedBy = Matches.unitIsOwnedBy(player);
 
692
        Iterator iter = data.getMap().getTerritories().iterator();
 
693
        while (iter.hasNext())
 
694
        {
 
695
            Territory place = (Territory) iter.next();
 
696
            rVal += place.getUnits().countMatches(ownedBy);
 
697
            
 
698
        }
 
699
        return rVal;
 
700
    }
 
701
}
 
702
 
 
703
class TUVStat extends AbstractStat
 
704
{
 
705
 
 
706
    public String getName()
 
707
    {
 
708
        return "TUV";
 
709
    }
 
710
 
 
711
    public double getValue(PlayerID player, GameData data)
 
712
    {
 
713
        IntegerMap<UnitType> costs = BattleCalculator.getCosts(player, data);
 
714
        
 
715
        Match<Unit> unitIsOwnedBy = Matches.unitIsOwnedBy(player);
 
716
        
 
717
        int rVal = 0; 
 
718
        Iterator iter = data.getMap().getTerritories().iterator();
 
719
        while (iter.hasNext())
 
720
        {
 
721
            Territory place = (Territory) iter.next();
 
722
            Collection<Unit> owned = place.getUnits().getMatches(unitIsOwnedBy);
 
723
            rVal += BattleCalculator.getTUV(owned, costs);
 
724
        }
 
725
        return rVal;
 
726
    }
 
727
}
 
728
 
 
729
class VictoryCityStat extends AbstractStat
 
730
{
 
731
    public String getName()
 
732
    {
 
733
        return "VC";
 
734
    }
 
735
 
 
736
    public double getValue(PlayerID player, GameData data)
 
737
    {
 
738
        int rVal = 0; 
 
739
        Iterator iter = data.getMap().getTerritories().iterator();
 
740
        while (iter.hasNext())
 
741
        {
 
742
            Territory place = (Territory) iter.next();
 
743
            if(!place.getOwner().equals(player))
 
744
                continue;
 
745
            
 
746
            TerritoryAttachment ta =  TerritoryAttachment.get(place);
 
747
            if(ta == null)
 
748
                continue;
 
749
            
 
750
            if(ta.isVictoryCity())
 
751
                rVal ++;
 
752
        }
 
753
        return rVal;
 
754
    }
 
755
}
 
756
 
 
757
class VPStat extends AbstractStat
 
758
{
 
759
    public String getName()
 
760
    {
 
761
        return "VPs";
 
762
    }
 
763
 
 
764
    public double getValue(PlayerID player, GameData data)
 
765
    {
 
766
        PlayerAttachment pa = PlayerAttachment.get(player);
 
767
        if(pa != null)
 
768
            return Double.parseDouble(pa.getVps());
 
769
        return 0; 
 
770
    }
 
771
}