Foundation.java 7.98 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
5
6
7
8
9
import de._82grfl1bif.KPI_Visualizer.helpers.Layout;
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
19
20

    private ArrayList<Structure> children = new ArrayList<>();
    private Layout layout;

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
50
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
    public Structure generateChild(boolean isFoundation, Klasse klasse, String name) {
        Structure result;
        if (isFoundation) {
            result = new Foundation(this.depth + 1, nextMaterial(), name);
        } else {
            result = new Building(Material.LIME_CONCRETE, klasse, name);
        }
        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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    @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;
    }

    public Structure findByName(String findName) {
        if (this.name.equals(findName)) {
            return this;
        } else {
            for (Structure s : this.children) {
                if (s.getClass() == Foundation.class) {
                    if ((((Foundation) s).findByName(findName)) != null) {
                        return s;
                    }
                }
            }
        }
        return null;
    }

    private static class DepthComparator implements Comparator<Foundation> {
Florian Grabowski's avatar
Florian Grabowski committed
162
163
164
165
166
167
        @Override
        public int compare(Foundation o1, Foundation o2) {
            return Integer.compare(o1.getDepth(), o2.getDepth());
        }
    }

168
    private static class WidthComparator implements Comparator<Structure> {
Florian Grabowski's avatar
Florian Grabowski committed
169
170
171

        @Override
        public int compare(Structure o1, Structure o2) {
172
            return Integer.compare(o1.getWidth().x, o2.getWidth().x);
Florian Grabowski's avatar
Florian Grabowski committed
173
174
175
176
177
        }
    }

    private boolean baseFoundation = true;

178
179
    public void organizeFoundation() {
        if (this.depth == 0 && baseFoundation) { //rootElement prepwork and recursion call
Florian Grabowski's avatar
Florian Grabowski committed
180
181
182
183
            ArrayList<Foundation> allFoundations = collectAllFoundations();
            baseFoundation = false;
            allFoundations.sort(new DepthComparator());
            Collections.reverse(allFoundations); //higher Depths first
184
            for (Foundation f : allFoundations) {
Florian Grabowski's avatar
Florian Grabowski committed
185
186
187
                //if(!this.equals(f))
                f.organizeFoundation();
            }
188
        } else { //normal Call and recursion
Florian Grabowski's avatar
Florian Grabowski committed
189
190
191
192
            this.children.sort(new WidthComparator());
            Collections.reverse(this.children); //Widest Building or Foundation first
            this.layout = new Layout(this.children);
            this.layout.createLayout();
193
            this.width.x = layout.getSize();
Florian Grabowski's avatar
Florian Grabowski committed
194
195
196
        }
    }

197
    private ArrayList<Foundation> collectAllFoundations() { //called on the base Foundation
Florian Grabowski's avatar
Florian Grabowski committed
198
199
200
201
        ArrayList<Foundation> fCollection = new ArrayList<>();
        fCollection.add(this);
        int currentDepth = this.depth;
        int lastDepthAdded = -1;
202
203
204
        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
205
206
207
208
209
210
211
212
213
                    fCollection.addAll(fCollection.get(i).getNextDepth());
                    lastDepthAdded = fCollection.get(i).getNextDepth().size();
                }
            }
            currentDepth += 1;
        }
        return fCollection;
    }

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

229
230
    public void correctAllLocations(Location location) {
        for (Structure s : this.children) {
Florian Grabowski's avatar
Florian Grabowski committed
231
232
            try {
                Point temp = this.layout.getCoordinateOf(s);
233
234
235
                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
236
237
238
239
240
241
242
243
                    f.correctAllLocations(f.getLocation());
                }
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}