1 package cz.cuni.amis.pogamut.defcon.utils.quadtree;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Map.Entry;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
9 import java.util.logging.Logger;
10
11 import cz.cuni.amis.pogamut.defcon.base3d.worldview.object.DefConLocation;
12 import cz.cuni.amis.pogamut.defcon.communication.worldview.DefConWorldView;
13 import cz.cuni.amis.pogamut.defcon.utils.Pair;
14
15
16
17
18
19
20
21 public class QuadTreesManager {
22
23 private final SortedMap<Integer, SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>>> enemyQuadTrees =
24 new TreeMap<Integer, SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>>>();
25 private final SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>> ownQuadTrees =
26 new TreeMap<Integer, Pair<List<QuadTree>, List<QuadTree>>>();
27
28 public QuadTreesManager(
29 SortedMap<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>> ownTerritories,
30 SortedMap<Integer, SortedMap<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>>> enemyTerritories,
31 DefConWorldView worldview) {
32 this(ownTerritories, enemyTerritories, worldview, null);
33 }
34
35 public QuadTreesManager(
36 SortedMap<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>> ownTerritories,
37 SortedMap<Integer, SortedMap<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>>> enemyTerritories,
38 DefConWorldView worldview, Logger log) {
39
40 for (Entry<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>> territory : ownTerritories
41 .entrySet()) {
42
43 Pair<List<QuadTree>, List<QuadTree>> qTreesPair = new Pair<List<QuadTree>, List<QuadTree>>();
44
45 ownQuadTrees.put(territory.getKey(), qTreesPair);
46
47
48
49 ArrayList<QuadTree> qTreesList = new ArrayList<QuadTree>();
50 qTreesPair.first = qTreesList;
51
52 for (List<DefConLocation> sea : territory.getValue().first) {
53 QuadTree tree = new QuadTree(sea);
54
55 QuadTreeLabeler.label(tree, new
56 UnitPlacementQuadTreeLabellingMethod(worldview,
57 log));
58
59 qTreesList.add(tree);
60 }
61
62 qTreesList = new ArrayList<QuadTree>();
63 qTreesPair.second = qTreesList;
64
65 for (List<DefConLocation> land : territory.getValue().second) {
66 QuadTree tree = new QuadTree(land);
67
68 QuadTreeLabeler.label(tree, new
69 BuildingPlacementQuadTreeLabellingMethod(worldview,
70 log));
71
72 qTreesList.add(tree);
73 }
74 }
75
76
77
78
79 for (Entry<Integer, SortedMap<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>>> enemy : enemyTerritories
80 .entrySet()) {
81
82 SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>> enemy_territories =
83 new TreeMap<Integer, Pair<List<QuadTree>, List<QuadTree>>>();
84
85 enemyQuadTrees.put(enemy.getKey(), enemy_territories);
86
87 for (Entry<Integer, Pair<List<List<DefConLocation>>, List<List<DefConLocation>>>> territory : enemy
88 .getValue().entrySet()) {
89
90 Pair<List<QuadTree>, List<QuadTree>> qTreesPair = new Pair<List<QuadTree>, List<QuadTree>>();
91
92 enemy_territories.put(territory.getKey(), qTreesPair);
93
94
95
96 ArrayList<QuadTree> qTreesList = new ArrayList<QuadTree>();
97 qTreesPair.first = qTreesList;
98
99 for (List<DefConLocation> sea : territory.getValue().first) {
100 QuadTree tree = new QuadTree(sea);
101
102 qTreesList.add(tree);
103 }
104
105 qTreesList = new ArrayList<QuadTree>();
106 qTreesPair.second = qTreesList;
107
108 for (List<DefConLocation> land : territory.getValue().second) {
109 QuadTree tree = new QuadTree(land);
110
111 qTreesList.add(tree);
112 }
113 }
114 }
115 }
116
117 public final SortedMap<Integer, SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>>> getEnemyQuadTrees() {
118 return Collections.unmodifiableSortedMap(enemyQuadTrees);
119 }
120
121 public final SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>> getEnemyQuadTrees(
122 int enemyId) {
123 return Collections.unmodifiableSortedMap(enemyQuadTrees.get(enemyId));
124 }
125
126 public final SortedMap<Integer, Pair<List<QuadTree>, List<QuadTree>>> getOwnQuadTrees() {
127 return Collections.unmodifiableSortedMap(ownQuadTrees);
128 }
129 }