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

Florian Grabowski's avatar
Florian Grabowski committed
3
4
import de._82grfl1bif.kpiVisualizer.data.Klasse;
import de._82grfl1bif.kpiVisualizer.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;
Florian Grabowski's avatar
Florian Grabowski committed
10
import java.awt.Point;
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
    public Structure generateChild(boolean isFoundation, Klasse klasse, String name) {
        Structure result;
        if (isFoundation) {
48
            result = new Foundation(this.depth + 1, nextSediment(), name);
49
        } else {
50
            result = new Building(nextColorConcrete(), klasse, name);
51
52
53
54
55
        }
        this.children.add(result);
        return result;
    }

56
    public Material nextColorConcrete() {
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
        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;
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            }
        }
        return result;
    }

    public Material nextSediment(){
        Material result = null;
        Random rand = new Random();
        while ((result == this.material) || (result == null)) {
            int r = rand.nextInt(12); //there are 16 kins of concrete
            switch (r) {
                case 0:
                    result = Material.STONE;
                    break;
                case 1:
                    result = Material.GRASS_BLOCK;
                    break;
                case 2:
                    result = Material.ROOTED_DIRT;
                    break;
                case 3:
                    result = Material.GRANITE;
                    break;
                case 4:
                    result = Material.DIORITE;
                    break;
                case 5:
                    result = Material.ANDESITE;
                    break;
                case 6:
                    result = Material.DEEPSLATE;
                    break;
                case 7:
                    result = Material.TUFF;
                    break;
                case 8:
                    result = Material.GRAVEL;
                    break;
                case 9:
                    result = Material.SAND;
                    break;
                case 10:
                    result = Material.SANDSTONE;
                    break;
                case 11:
                    result = Material.COARSE_DIRT;
                    break;
157
158
159
160
161
162
            }
        }
        return result;
    }

    public void removeChildren() {
Florian Grabowski's avatar
Florian Grabowski committed
163
164
165
        this.children = new ArrayList<>();
    }

166
    public void optimizeLayout() {
Florian Grabowski's avatar
Florian Grabowski committed
167
168
        int cWith = 0;
        for (Structure s : children) {
169
            s.setLocation(location.clone().add(new Vector(1, depth, ((cWith) + 1))));
170
            cWith += (s.getWidth().x + 2);
Florian Grabowski's avatar
Florian Grabowski committed
171
        }
172
        this.width.x = cWith;
Florian Grabowski's avatar
Florian Grabowski committed
173
174
    }

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    @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
193
194
195
196
    @Override
    public double getArea(){
        if(area == 0)setAllAreas();
        return area;
197
198
199
    }

    private static class DepthComparator implements Comparator<Foundation> {
Florian Grabowski's avatar
Florian Grabowski committed
200
201
202
203
204
205
        @Override
        public int compare(Foundation o1, Foundation o2) {
            return Integer.compare(o1.getDepth(), o2.getDepth());
        }
    }

206
    private static class WidthComparator implements Comparator<Structure> {
Florian Grabowski's avatar
Florian Grabowski committed
207
208
209

        @Override
        public int compare(Structure o1, Structure o2) {
210
            return Integer.compare(o1.getWidth().x, o2.getWidth().x);
Florian Grabowski's avatar
Florian Grabowski committed
211
212
213
214
215
        }
    }

    private boolean baseFoundation = true;

216
    public void organizeFoundation() {
Florian Grabowski's avatar
Florian Grabowski committed
217
        if (this.depth == 0 && baseFoundation) { //rootElement prep-work and recursion call
Florian Grabowski's avatar
Florian Grabowski committed
218
219
220
221
            ArrayList<Foundation> allFoundations = collectAllFoundations();
            baseFoundation = false;
            allFoundations.sort(new DepthComparator());
            Collections.reverse(allFoundations); //higher Depths first
222
            for (Foundation f : allFoundations) {
Florian Grabowski's avatar
Florian Grabowski committed
223
224
225
                //if(!this.equals(f))
                f.organizeFoundation();
            }
226
        } else { //normal Call and recursion
Florian Grabowski's avatar
Florian Grabowski committed
227
228
            this.children.sort(new WidthComparator());
            Collections.reverse(this.children); //Widest Building or Foundation first
Florian Grabowski's avatar
Florian Grabowski committed
229
230
231
            this.simpleSquareLayout = new SimpleSquareLayout(this.children);
            this.simpleSquareLayout.createLayout();
            this.width.x = simpleSquareLayout.getSize();
Florian Grabowski's avatar
Florian Grabowski committed
232
233
234
        }
    }

Florian Grabowski's avatar
Florian Grabowski committed
235
    public ArrayList<Foundation> collectAllFoundations() { //called on the base Foundation
Florian Grabowski's avatar
Florian Grabowski committed
236
237
238
239
        ArrayList<Foundation> fCollection = new ArrayList<>();
        fCollection.add(this);
        int currentDepth = this.depth;
        int lastDepthAdded = -1;
240
241
242
        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
243
244
245
246
247
248
249
250
251
                    fCollection.addAll(fCollection.get(i).getNextDepth());
                    lastDepthAdded = fCollection.get(i).getNextDepth().size();
                }
            }
            currentDepth += 1;
        }
        return fCollection;
    }

Florian Grabowski's avatar
Florian Grabowski committed
252
    @Deprecated
Florian Grabowski's avatar
Florian Grabowski committed
253
254
255
256
257
258
259
260
261
262
263
264
    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;
    }

265
    private ArrayList<Foundation> getNextDepth() {
Florian Grabowski's avatar
Florian Grabowski committed
266
        ArrayList<Foundation> result = new ArrayList<>();
267
268
        for (Structure s : this.children) {
            if (s.getClass() == Foundation.class) {
Florian Grabowski's avatar
Florian Grabowski committed
269
                result.add((Foundation) s);
270
            } else {
Florian Grabowski's avatar
Florian Grabowski committed
271
272
273
                s.setDepth(this.depth);
            }
        }
274
275
        for (Foundation f : result) {
            f.setDepth((this.depth) + 1);
Florian Grabowski's avatar
Florian Grabowski committed
276
277
278
279
        }
        return result;
    }

280
281
    public void correctAllLocations(Location location) {
        for (Structure s : this.children) {
Florian Grabowski's avatar
Florian Grabowski committed
282
            try {
Florian Grabowski's avatar
Florian Grabowski committed
283
                Point temp = this.simpleSquareLayout.getCoordinateOf(s);
284
285
286
                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
287
288
289
290
291
292
293
                    f.correctAllLocations(f.getLocation());
                }
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
Florian Grabowski's avatar
Florian Grabowski committed
294
295
296
297
298
299
300
301
302
303
304
305
306

    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
307
}