File size: 9,918 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package net.minecraft.client.renderer.debug;

import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import java.util.Locale;
import java.util.Map;
import net.minecraft.Util;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class PathfindingRenderer implements DebugRenderer.SimpleDebugRenderer {
    private final Map<Integer, Path> pathMap = Maps.newHashMap();
    private final Map<Integer, Float> pathMaxDist = Maps.newHashMap();
    private final Map<Integer, Long> creationMap = Maps.newHashMap();
    private static final long TIMEOUT = 5000L;
    private static final float MAX_RENDER_DIST = 80.0F;
    private static final boolean SHOW_OPEN_CLOSED = true;
    private static final boolean SHOW_OPEN_CLOSED_COST_MALUS = false;
    private static final boolean SHOW_OPEN_CLOSED_NODE_TYPE_WITH_TEXT = false;
    private static final boolean SHOW_OPEN_CLOSED_NODE_TYPE_WITH_BOX = true;
    private static final boolean SHOW_GROUND_LABELS = true;
    private static final float TEXT_SCALE = 0.02F;

    public void addPath(int p_113612_, Path p_113613_, float p_113614_) {
        this.pathMap.put(p_113612_, p_113613_);
        this.creationMap.put(p_113612_, Util.getMillis());
        this.pathMaxDist.put(p_113612_, p_113614_);
    }

    @Override
    public void render(PoseStack p_113629_, MultiBufferSource p_113630_, double p_113631_, double p_113632_, double p_113633_) {
        if (!this.pathMap.isEmpty()) {
            long i = Util.getMillis();

            for (Integer integer : this.pathMap.keySet()) {
                Path path = this.pathMap.get(integer);
                float f = this.pathMaxDist.get(integer);
                renderPath(p_113629_, p_113630_, path, f, true, true, p_113631_, p_113632_, p_113633_);
            }

            for (Integer integer1 : this.creationMap.keySet().toArray(new Integer[0])) {
                if (i - this.creationMap.get(integer1) > 5000L) {
                    this.pathMap.remove(integer1);
                    this.creationMap.remove(integer1);
                }
            }
        }
    }

    public static void renderPath(
        PoseStack p_270399_,
        MultiBufferSource p_270359_,
        Path p_270189_,
        float p_270841_,
        boolean p_270481_,
        boolean p_270748_,
        double p_270187_,
        double p_270252_,
        double p_270371_
    ) {
        renderPathLine(p_270399_, p_270359_.getBuffer(RenderType.debugLineStrip(6.0)), p_270189_, p_270187_, p_270252_, p_270371_);
        BlockPos blockpos = p_270189_.getTarget();
        if (distanceToCamera(blockpos, p_270187_, p_270252_, p_270371_) <= 80.0F) {
            DebugRenderer.renderFilledBox(
                p_270399_,
                p_270359_,
                new AABB(
                        (double)((float)blockpos.getX() + 0.25F),
                        (double)((float)blockpos.getY() + 0.25F),
                        (double)blockpos.getZ() + 0.25,
                        (double)((float)blockpos.getX() + 0.75F),
                        (double)((float)blockpos.getY() + 0.75F),
                        (double)((float)blockpos.getZ() + 0.75F)
                    )
                    .move(-p_270187_, -p_270252_, -p_270371_),
                0.0F,
                1.0F,
                0.0F,
                0.5F
            );

            for (int i = 0; i < p_270189_.getNodeCount(); i++) {
                Node node = p_270189_.getNode(i);
                if (distanceToCamera(node.asBlockPos(), p_270187_, p_270252_, p_270371_) <= 80.0F) {
                    float f = i == p_270189_.getNextNodeIndex() ? 1.0F : 0.0F;
                    float f1 = i == p_270189_.getNextNodeIndex() ? 0.0F : 1.0F;
                    DebugRenderer.renderFilledBox(
                        p_270399_,
                        p_270359_,
                        new AABB(
                                (double)((float)node.x + 0.5F - p_270841_),
                                (double)((float)node.y + 0.01F * (float)i),
                                (double)((float)node.z + 0.5F - p_270841_),
                                (double)((float)node.x + 0.5F + p_270841_),
                                (double)((float)node.y + 0.25F + 0.01F * (float)i),
                                (double)((float)node.z + 0.5F + p_270841_)
                            )
                            .move(-p_270187_, -p_270252_, -p_270371_),
                        f,
                        0.0F,
                        f1,
                        0.5F
                    );
                }
            }
        }

        Path.DebugData path$debugdata = p_270189_.debugData();
        if (p_270481_ && path$debugdata != null) {
            for (Node node1 : path$debugdata.closedSet()) {
                if (distanceToCamera(node1.asBlockPos(), p_270187_, p_270252_, p_270371_) <= 80.0F) {
                    DebugRenderer.renderFilledBox(
                        p_270399_,
                        p_270359_,
                        new AABB(
                                (double)((float)node1.x + 0.5F - p_270841_ / 2.0F),
                                (double)((float)node1.y + 0.01F),
                                (double)((float)node1.z + 0.5F - p_270841_ / 2.0F),
                                (double)((float)node1.x + 0.5F + p_270841_ / 2.0F),
                                (double)node1.y + 0.1,
                                (double)((float)node1.z + 0.5F + p_270841_ / 2.0F)
                            )
                            .move(-p_270187_, -p_270252_, -p_270371_),
                        1.0F,
                        0.8F,
                        0.8F,
                        0.5F
                    );
                }
            }

            for (Node node3 : path$debugdata.openSet()) {
                if (distanceToCamera(node3.asBlockPos(), p_270187_, p_270252_, p_270371_) <= 80.0F) {
                    DebugRenderer.renderFilledBox(
                        p_270399_,
                        p_270359_,
                        new AABB(
                                (double)((float)node3.x + 0.5F - p_270841_ / 2.0F),
                                (double)((float)node3.y + 0.01F),
                                (double)((float)node3.z + 0.5F - p_270841_ / 2.0F),
                                (double)((float)node3.x + 0.5F + p_270841_ / 2.0F),
                                (double)node3.y + 0.1,
                                (double)((float)node3.z + 0.5F + p_270841_ / 2.0F)
                            )
                            .move(-p_270187_, -p_270252_, -p_270371_),
                        0.8F,
                        1.0F,
                        1.0F,
                        0.5F
                    );
                }
            }
        }

        if (p_270748_) {
            for (int j = 0; j < p_270189_.getNodeCount(); j++) {
                Node node2 = p_270189_.getNode(j);
                if (distanceToCamera(node2.asBlockPos(), p_270187_, p_270252_, p_270371_) <= 80.0F) {
                    DebugRenderer.renderFloatingText(
                        p_270399_,
                        p_270359_,
                        String.valueOf(node2.type),
                        (double)node2.x + 0.5,
                        (double)node2.y + 0.75,
                        (double)node2.z + 0.5,
                        -1,
                        0.02F,
                        true,
                        0.0F,
                        true
                    );
                    DebugRenderer.renderFloatingText(
                        p_270399_,
                        p_270359_,
                        String.format(Locale.ROOT, "%.2f", node2.costMalus),
                        (double)node2.x + 0.5,
                        (double)node2.y + 0.25,
                        (double)node2.z + 0.5,
                        -1,
                        0.02F,
                        true,
                        0.0F,
                        true
                    );
                }
            }
        }
    }

    public static void renderPathLine(PoseStack p_270666_, VertexConsumer p_270602_, Path p_270511_, double p_270524_, double p_270163_, double p_270176_) {
        for (int i = 0; i < p_270511_.getNodeCount(); i++) {
            Node node = p_270511_.getNode(i);
            if (!(distanceToCamera(node.asBlockPos(), p_270524_, p_270163_, p_270176_) > 80.0F)) {
                float f = (float)i / (float)p_270511_.getNodeCount() * 0.33F;
                int j = i == 0 ? 0 : Mth.hsvToRgb(f, 0.9F, 0.9F);
                int k = j >> 16 & 0xFF;
                int l = j >> 8 & 0xFF;
                int i1 = j & 0xFF;
                p_270602_.addVertex(
                        p_270666_.last(),
                        (float)((double)node.x - p_270524_ + 0.5),
                        (float)((double)node.y - p_270163_ + 0.5),
                        (float)((double)node.z - p_270176_ + 0.5)
                    )
                    .setColor(k, l, i1, 255);
            }
        }
    }

    private static float distanceToCamera(BlockPos p_113635_, double p_113636_, double p_113637_, double p_113638_) {
        return (float)(
            Math.abs((double)p_113635_.getX() - p_113636_)
                + Math.abs((double)p_113635_.getY() - p_113637_)
                + Math.abs((double)p_113635_.getZ() - p_113638_)
        );
    }
}