Foundation.java 8.51 KB
Newer Older
Florian Grabowski's avatar
Florian Grabowski committed
1
2
package de._82grfl1bif.KPI_Visualizer.structures;

3
import de._82grfl1bif.KPI_Visualizer.data.Klasse;
Florian Grabowski's avatar
Florian Grabowski committed
4
import de._82grfl1bif.KPI_Visualizer.layouts.SimpleSquare.SimpleSquareLayout;
Florian Grabowski's avatar
Florian Grabowski committed
5
6
7
8
9
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.util.Vector;

import javax.management.AttributeNotFoundException;
10
import java.awt.*;
Florian Grabowski's avatar
Florian Grabowski committed
11
12
13
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
14
import java.util.Random;
Florian Grabowski's avatar
Florian Grabowski committed
15

16
public class Foundation extends Structure {
Florian Grabowski's avatar
Florian Grabowski committed
17
18

    private ArrayList<Structure> children = new ArrayList<>();
Florian Grabowski's avatar
Florian Grabowski committed
19
    private SimpleSquareLayout simpleSquareLayout;
Florian Grabowski's avatar
Florian Grabowski committed
20

21
    public Foundation(int depth, Material material) { //Foundations are uniform Height
22
        this.width.x = 0;
Florian Grabowski's avatar
Florian Grabowski committed
23
24
25
26
        this.depth = depth;
        this.material = material;
    }

27
28
29
30
31
32
33
    public Foundation(int depth, Material material, String name) { //Foundations are uniform Height
        this.width.x = 0;
        this.name = name;
        this.depth = depth;
        this.material = material;
    }

Florian Grabowski's avatar
Florian Grabowski committed
34
35
36
37
38
    public ArrayList<Structure> getChildren() {
        return children;
    }

    public void addChildren(ArrayList<Structure> children) {
39
        for (Structure s : children) {
Florian Grabowski's avatar
Florian Grabowski committed
40
41
42
43
44
            s.setDepth(this.depth);
        }
        this.children.addAll(children);
    }

45
46
47
48
49
    public Structure generateChild(boolean isFoundation, Klasse klasse, String name) {
        Structure result;
        if (isFoundation) {
            result = new Foundation(this.depth + 1, nextMaterial(), name);
        } else {
Florian Grabowski's avatar
Florian Grabowski committed
50
            result = new Building(nextMaterial(), klasse, name);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        }
        this.children.add(result);
        return result;
    }

    public Material nextMaterial() {
        Material result = null;
        Random rand = new Random();
        while ((result == this.material) || (result == null)) {
            int r = rand.nextInt(15); //there are 16 kins of concrete
            switch (r) {
                case 0:
                    result = Material.BLACK_CONCRETE;
                    break;
                case 1:
                    result = Material.BROWN_CONCRETE;
                    break;
                case 2:
                    result = Material.GRAY_CONCRETE;
                    break;
                case 3:
                    result = Material.LIME_CONCRETE;
                    break;
                case 4:
                    result = Material.YELLOW_CONCRETE;
                    break;
                case 5:
                    result = Material.LIGHT_GRAY_CONCRETE;
                    break;
                case 6:
                    result = Material.RED_CONCRETE;
                    break;
                case 7:
                    result = Material.BLUE_CONCRETE;
                    break;
                case 8:
                    result = Material.CYAN_CONCRETE;
                    break;
                case 9:
                    result = Material.GREEN_CONCRETE;
                    break;
                case 10:
                    result = Material.LIGHT_BLUE_CONCRETE;
                    break;
                case 11:
                    result = Material.MAGENTA_CONCRETE;
                    break;
                case 12:
                    result = Material.ORANGE_CONCRETE;
                    break;
                case 13:
                    result = Material.PINK_CONCRETE;
                    break;
                case 14:
                    result = Material.PURPLE_CONCRETE;
                    break;
                case 15:
                    result = Material.WHITE_CONCRETE;
                    break;
            }
        }
        return result;
    }

    public void removeChildren() {
Florian Grabowski's avatar
Florian Grabowski committed
116
117
118
        this.children = new ArrayList<>();
    }

119
    public void optimizeLayout() {
Florian Grabowski's avatar
Florian Grabowski committed
120
121
        int cWith = 0;
        for (Structure s : children) {
122
            s.setLocation(location.clone().add(new Vector(1, depth, ((cWith) + 1))));
123
            cWith += (s.getWidth().x + 2);
Florian Grabowski's avatar
Florian Grabowski committed
124
        }
125
        this.width.x = cWith;
Florian Grabowski's avatar
Florian Grabowski committed
126
127
    }

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    @Override
    public Structure getFromTree(String checkName) {
        if (this.name != null) {
            if (this.name.equals(checkName)) {
                return this;
            } else {
                for (Structure s : this.children) {
                    if (s.getFromTree(checkName) != null) return s.getFromTree(checkName);
                }
            }
        } else {
            for (Structure s : this.children) {
                if (s.getFromTree(checkName) != null) return s.getFromTree(checkName);
            }
        }
        return null;
    }

Florian Grabowski's avatar
Florian Grabowski committed
146
147
148
149
    @Override
    public double getArea(){
        if(area == 0)setAllAreas();
        return area;
150
151
152
    }

    private static class DepthComparator implements Comparator<Foundation> {
Florian Grabowski's avatar
Florian Grabowski committed
153
154
155
156
157
158
        @Override
        public int compare(Foundation o1, Foundation o2) {
            return Integer.compare(o1.getDepth(), o2.getDepth());
        }
    }

159
    private static class WidthComparator implements Comparator<Structure> {
Florian Grabowski's avatar
Florian Grabowski committed
160
161
162

        @Override
        public int compare(Structure o1, Structure o2) {
163
            return Integer.compare(o1.getWidth().x, o2.getWidth().x);
Florian Grabowski's avatar
Florian Grabowski committed
164
165
166
167
168
        }
    }

    private boolean baseFoundation = true;

169
170
    public void organizeFoundation() {
        if (this.depth == 0 && baseFoundation) { //rootElement prepwork and recursion call
Florian Grabowski's avatar
Florian Grabowski committed
171
172
173
174
            ArrayList<Foundation> allFoundations = collectAllFoundations();
            baseFoundation = false;
            allFoundations.sort(new DepthComparator());
            Collections.reverse(allFoundations); //higher Depths first
175
            for (Foundation f : allFoundations) {
Florian Grabowski's avatar
Florian Grabowski committed
176
177
178
                //if(!this.equals(f))
                f.organizeFoundation();
            }
179
        } else { //normal Call and recursion
Florian Grabowski's avatar
Florian Grabowski committed
180
181
            this.children.sort(new WidthComparator());
            Collections.reverse(this.children); //Widest Building or Foundation first
Florian Grabowski's avatar
Florian Grabowski committed
182
183
184
            this.simpleSquareLayout = new SimpleSquareLayout(this.children);
            this.simpleSquareLayout.createLayout();
            this.width.x = simpleSquareLayout.getSize();
Florian Grabowski's avatar
Florian Grabowski committed
185
186
187
        }
    }

Florian Grabowski's avatar
Florian Grabowski committed
188
    public ArrayList<Foundation> collectAllFoundations() { //called on the base Foundation
Florian Grabowski's avatar
Florian Grabowski committed
189
190
191
192
        ArrayList<Foundation> fCollection = new ArrayList<>();
        fCollection.add(this);
        int currentDepth = this.depth;
        int lastDepthAdded = -1;
193
194
195
        while (lastDepthAdded != 0) { //while there are new nextDepths in this Depth.
            for (int i = 0; i < fCollection.size(); i++) {
                if (!(fCollection.get(i).getDepth() < currentDepth)) { //if f is not part of a historical depth
Florian Grabowski's avatar
Florian Grabowski committed
196
197
198
199
200
201
202
203
204
                    fCollection.addAll(fCollection.get(i).getNextDepth());
                    lastDepthAdded = fCollection.get(i).getNextDepth().size();
                }
            }
            currentDepth += 1;
        }
        return fCollection;
    }

Florian Grabowski's avatar
Florian Grabowski committed
205
206
207
208
209
210
211
212
213
214
215
216
    public ArrayList<Building> collectAllBuildings() { //called on the base Foundation
        ArrayList<Building> buildings = new ArrayList<>();
        for (Structure s: children) {
            if (s.getClass() == Foundation.class){
                buildings.addAll(((Foundation) s).collectAllBuildings());
            }else{
                buildings.add((Building) s);
            }
        }
        return buildings;
    }

217
    private ArrayList<Foundation> getNextDepth() {
Florian Grabowski's avatar
Florian Grabowski committed
218
        ArrayList<Foundation> result = new ArrayList<>();
219
220
        for (Structure s : this.children) {
            if (s.getClass() == Foundation.class) {
Florian Grabowski's avatar
Florian Grabowski committed
221
                result.add((Foundation) s);
222
            } else {
Florian Grabowski's avatar
Florian Grabowski committed
223
224
225
                s.setDepth(this.depth);
            }
        }
226
227
        for (Foundation f : result) {
            f.setDepth((this.depth) + 1);
Florian Grabowski's avatar
Florian Grabowski committed
228
229
230
231
        }
        return result;
    }

232
233
    public void correctAllLocations(Location location) {
        for (Structure s : this.children) {
Florian Grabowski's avatar
Florian Grabowski committed
234
            try {
Florian Grabowski's avatar
Florian Grabowski committed
235
                Point temp = this.simpleSquareLayout.getCoordinateOf(s);
236
237
238
                s.setLocation(location.clone().add(temp.x, 0, temp.y));
                if (s.getClass() == Foundation.class) {
                    Foundation f = (Foundation) s;
Florian Grabowski's avatar
Florian Grabowski committed
239
240
241
242
243
244
245
                    f.correctAllLocations(f.getLocation());
                }
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
Florian Grabowski's avatar
Florian Grabowski committed
246
247
248
249
250
251
252
253
254
255
256
257
258

    private double setAllAreas(){
        double myArea = 0;
        for (Structure s: children) {
            if(s.getClass() == Foundation.class){
                myArea += ((Foundation) s).setAllAreas();
            }else{
                myArea += s.getArea();
            }
        }
        this.area = myArea;
        return myArea;
    }
Florian Grabowski's avatar
Florian Grabowski committed
259
}