| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | public class TreeNodePosition {
|
| 8 | private final AdvancementNode node;
|
| 9 | private final @Nullable TreeNodePosition parent;
|
| 10 | private final @Nullable TreeNodePosition previousSibling;
|
| 11 | private final int childIndex;
|
| 12 | private final List<TreeNodePosition> children = Lists.newArrayList();
|
| 13 | private TreeNodePosition ancestor;
|
| 14 | private @Nullable TreeNodePosition thread;
|
| 15 | private int x;
|
| 16 | private float y;
|
| 17 | private float mod;
|
| 18 | private float change;
|
| 19 | private float shift;
|
| 20 |
|
| 21 | public TreeNodePosition(
|
| 22 | final AdvancementNode node, final @Nullable TreeNodePosition parent, final @Nullable TreeNodePosition previousSibling, final int childIndex, final int depth
|
| 23 | ) {
|
| 24 | if (node.advancement().display().isEmpty()) {
|
| 25 | throw new IllegalArgumentException("Can't position an invisible advancement!");
|
| 26 | }
|
| 27 |
|
| 28 | this.node = node;
|
| 29 | this.parent = parent;
|
| 30 | this.previousSibling = previousSibling;
|
| 31 | this.childIndex = childIndex;
|
| 32 | this.ancestor = this;
|
| 33 | this.x = depth;
|
| 34 | this.y = -1.0F;
|
| 35 | TreeNodePosition previous = null;
|
| 36 |
|
| 37 | for (AdvancementNode child : node.children()) {
|
| 38 | previous = this.addChild(child, previous);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private @Nullable TreeNodePosition addChild(final AdvancementNode node, @Nullable TreeNodePosition previous) {
|
| 43 | if (node.advancement().display().isPresent()) {
|
| 44 | previous = new TreeNodePosition(node, this, previous, this.children.size() + 1, this.x + 1);
|
| 45 | this.children.add(previous);
|
| 46 | } else {
|
| 47 | for (AdvancementNode grandchild : node.children()) {
|
| 48 | previous = this.addChild(grandchild, previous);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | return previous;
|
| 53 | }
|
| 54 |
|
| 55 | private void firstWalk() {
|
| 56 | if (this.children.isEmpty()) {
|
| 57 | if (this.previousSibling != null) {
|
| 58 | this.y = this.previousSibling.y + 1.0F;
|
| 59 | } else {
|
| 60 | this.y = 0.0F;
|
| 61 | }
|
| 62 | } else {
|
| 63 | TreeNodePosition defaultAncestor = null;
|
| 64 |
|
| 65 | for (TreeNodePosition child : this.children) {
|
| 66 | child.firstWalk();
|
| 67 | defaultAncestor = child.apportion(defaultAncestor == null ? child : defaultAncestor);
|
| 68 | }
|
| 69 |
|
| 70 | this.executeShifts();
|
| 71 | float midpoint = (this.children.get(0).y + this.children.get(this.children.size() - 1).y) / 2.0F;
|
| 72 | if (this.previousSibling != null) {
|
| 73 | this.y = this.previousSibling.y + 1.0F;
|
| 74 | this.mod = this.y - midpoint;
|
| 75 | } else {
|
| 76 | this.y = midpoint;
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private float secondWalk(final float modSum, final int depth, float min) {
|
| 82 | this.y += modSum;
|
| 83 | this.x = depth;
|
| 84 | if (this.y < min) {
|
| 85 | min = this.y;
|
| 86 | }
|
| 87 |
|
| 88 | for (TreeNodePosition child : this.children) {
|
| 89 | min = child.secondWalk(modSum + this.mod, depth + 1, min);
|
| 90 | }
|
| 91 |
|
| 92 | return min;
|
| 93 | }
|
| 94 |
|
| 95 | private void thirdWalk(final float offset) {
|
| 96 | this.y += offset;
|
| 97 |
|
| 98 | for (TreeNodePosition child : this.children) {
|
| 99 | child.thirdWalk(offset);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | private void executeShifts() {
|
| 104 | float shift = 0.0F;
|
| 105 | float change = 0.0F;
|
| 106 |
|
| 107 | for (int i = this.children.size() - 1; i >= 0; i--) {
|
| 108 | TreeNodePosition child = this.children.get(i);
|
| 109 | child.y += shift;
|
| 110 | child.mod += shift;
|
| 111 | change += child.change;
|
| 112 | shift += child.shift + change;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private @Nullable TreeNodePosition previousOrThread() {
|
| 117 | if (this.thread != null) {
|
| 118 | return this.thread;
|
| 119 | } else {
|
| 120 | return !this.children.isEmpty() ? this.children.get(0) : null;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private @Nullable TreeNodePosition nextOrThread() {
|
| 125 | if (this.thread != null) {
|
| 126 | return this.thread;
|
| 127 | } else {
|
| 128 | return !this.children.isEmpty() ? this.children.get(this.children.size() - 1) : null;
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | private TreeNodePosition apportion(TreeNodePosition defaultAncestor) {
|
| 133 | if (this.previousSibling == null) {
|
| 134 | return defaultAncestor;
|
| 135 | }
|
| 136 |
|
| 137 | TreeNodePosition vir = this;
|
| 138 | TreeNodePosition vor = this;
|
| 139 | TreeNodePosition vil = this.previousSibling;
|
| 140 | TreeNodePosition vol = this.parent.children.get(0);
|
| 141 | float sir = this.mod;
|
| 142 | float sor = this.mod;
|
| 143 | float sil = vil.mod;
|
| 144 | float sol = vol.mod;
|
| 145 |
|
| 146 | while (vil.nextOrThread() != null && vir.previousOrThread() != null) {
|
| 147 | vil = vil.nextOrThread();
|
| 148 | vir = vir.previousOrThread();
|
| 149 | vol = vol.previousOrThread();
|
| 150 | vor = vor.nextOrThread();
|
| 151 | vor.ancestor = this;
|
| 152 | float shift = vil.y + sil - (vir.y + sir) + 1.0F;
|
| 153 | if (shift > 0.0F) {
|
| 154 | vil.getAncestor(this, defaultAncestor).moveSubtree(this, shift);
|
| 155 | sir += shift;
|
| 156 | sor += shift;
|
| 157 | }
|
| 158 |
|
| 159 | sil += vil.mod;
|
| 160 | sir += vir.mod;
|
| 161 | sol += vol.mod;
|
| 162 | sor += vor.mod;
|
| 163 | }
|
| 164 |
|
| 165 | if (vil.nextOrThread() != null && vor.nextOrThread() == null) {
|
| 166 | vor.thread = vil.nextOrThread();
|
| 167 | vor.mod += sil - sor;
|
| 168 | } else {
|
| 169 | if (vir.previousOrThread() != null && vol.previousOrThread() == null) {
|
| 170 | vol.thread = vir.previousOrThread();
|
| 171 | vol.mod += sir - sol;
|
| 172 | }
|
| 173 |
|
| 174 | defaultAncestor = this;
|
| 175 | }
|
| 176 |
|
| 177 | return defaultAncestor;
|
| 178 | }
|
| 179 |
|
| 180 | private void moveSubtree(final TreeNodePosition right, final float shift) {
|
| 181 | float subtrees = right.childIndex - this.childIndex;
|
| 182 | if (subtrees != 0.0F) {
|
| 183 | right.change -= shift / subtrees;
|
| 184 | this.change += shift / subtrees;
|
| 185 | }
|
| 186 |
|
| 187 | right.shift += shift;
|
| 188 | right.y += shift;
|
| 189 | right.mod += shift;
|
| 190 | }
|
| 191 |
|
| 192 | private TreeNodePosition getAncestor(final TreeNodePosition other, final TreeNodePosition defaultAncestor) {
|
| 193 | return this.ancestor != null && other.parent.children.contains(this.ancestor) ? this.ancestor : defaultAncestor;
|
| 194 | }
|
| 195 |
|
| 196 | private void finalizePosition() {
|
| 197 | this.node.setLocation(this.x, this.y);
|
| 198 | if (!this.children.isEmpty()) {
|
| 199 | for (TreeNodePosition child : this.children) {
|
| 200 | child.finalizePosition();
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | public static void run(final AdvancementNode node) {
|
| 206 | if (node.advancement().display().isEmpty()) {
|
| 207 | throw new IllegalArgumentException("Can't position children of an invisible root!");
|
| 208 | }
|
| 209 |
|
| 210 | TreeNodePosition root = new TreeNodePosition(node, null, null, 1, 0);
|
| 211 | root.firstWalk();
|
| 212 | float min = root.secondWalk(0.0F, 0, root.y);
|
| 213 | if (min < 0.0F) {
|
| 214 | root.thirdWalk(-min);
|
| 215 | }
|
| 216 |
|
| 217 | root.finalizePosition();
|
| 218 | }
|
| 219 | }
|
| 220 |
|