aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/GriffinJavaUtils.java
blob: 688923f65299c66ca5dbfd02c59964ae6d1449a8 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package at.hannibal2.skyhanni.test;

import at.hannibal2.skyhanni.utils.LorenzVec;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.text.DecimalFormat;
import java.util.List;
import java.util.*;
import java.util.function.Function;

//TODO delte this class after next diana mayor
public class GriffinJavaUtils {
    public static <T> void permute(ArrayList<ArrayList<T>> result, T[] a, int k) {
        if (k == a.length) {
            ArrayList<T> subResult = new ArrayList<>();
            result.add(subResult);
            Collections.addAll(subResult, a);
        } else {
            for (int i = k; i < a.length; i++) {
                T temp = a[k];
                a[k] = a[i];
                a[i] = temp;

                permute(result, a, k + 1);

                temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }
        }
    }

//    public static <T> ArrayList<T> sortLocationListC(LorenzVec start, Map<T, LorenzVec> map, boolean brokenMath) {
//        Map<T, Double> fastestWithout = new HashMap<>();
//        for (T without : map.keySet()) {
//
//            ArrayList<ArrayList<T>> variants = new ArrayList<>();
//            ArrayList<T> values = new ArrayList<>(map.keySet());
//
//            values.remove(without);
//            T[] array = (T[]) values.toArray();
//
//            permute(variants, array, 0);
//
//            LinkedHashMap<ArrayList<T>, Double> distances = new LinkedHashMap<>();
//
//            for (ArrayList<T> list : variants) {
//
//                double distance = 0;
//                LorenzVec last = start;
//
//                for (T t : list) {
//                    LorenzVec location = map.get(t);
//                    distance += last.distanceSq(location);
//                    last = location;
//                }
//
//                distances.put(list, distance);
//            }
//
//            Map<ArrayList<T>, Double> sort;
//            if (brokenMath) {
//                sort = sortByValue(distances);
//            } else {
//                sort = sortByValueAsc(distances);
//            }
//
//            double fastestDistance = sort.values().iterator().next();
//            fastestWithout.put(without, fastestDistance);
//        }
//
//        T skip = sortByValueAsc(fastestWithout).keySet().iterator().next();
//
//
//        map.remove(skip);
//        ArrayList<T> result = sortLocationListB(start, map, brokenMath, false, T -> false, 0);
//        result.add(skip);
//
//        return result;
//    }

    public static <T> ArrayList<LorenzVec> sortLocationListB(LorenzVec start, Map<T, LorenzVec> map, boolean brokenMath,
                                                             boolean skipWorst, Function<T, Boolean> shouldAddToHostile, int addToHostileLastValue) {

//        if (skipWorst) {
//            return sortLocationListC(start, map, brokenMath);
//        }
        ArrayList<ArrayList<T>> variants = new ArrayList<>();
        Set<T> values = map.keySet();
        T[] array = (T[]) values.toArray();

        permute(variants, array, 0);

        LinkedHashMap<ArrayList<T>, Double> distances = new LinkedHashMap<>();

        int with = 0;
        int without = 0;

        for (ArrayList<T> list : variants) {

            double distance = 0;
            LorenzVec last = start;
            T lastT = null;

            for (T t : list) {
                LorenzVec location = map.get(t);
                distance += last.distanceSq(location);
                last = location;
                lastT = t;
            }
            if (shouldAddToHostile.apply(lastT)) {
                distance += addToHostileLastValue;
                with++;
            } else {
                without++;
            }

            distances.put(list, distance);
        }
//        LorenzUtils.Companion.chat("with: " + with);
//        LorenzUtils.Companion.chat("without: " + without);

        Map<ArrayList<T>, Double> sort;
        if (brokenMath) {
            sort = sortByValue(distances);
        } else {
            sort = sortByValueAsc(distances);
        }
        ArrayList<T> result = sort.keySet().iterator().next();
        ArrayList<LorenzVec> resultList = new ArrayList<>();
        for (T t : result) {
            resultList.add(map.get(t));
        }

        return resultList;
    }

    //descending
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
        list.sort(Map.Entry.comparingByValue());
        Collections.reverse(list);

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }

    //ascending
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAsc(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
        list.sort(Map.Entry.comparingByValue());

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }

    public static String formatInteger(int i) {
        return new DecimalFormat("#,##0").format(i).replace(',', '.');
    }

    public static List<ItemStack> getItemsInInventory() {
        return getItemsInInventory(false);
    }

    public static List<ItemStack> getItemsInInventory(boolean withCursorItem) {
        List<ItemStack> list = new ArrayList<>();

//        EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
        EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
        if (player == null) {
            System.err.println("loadCurrentInventory: player is null!");
            return list;
        }

        InventoryPlayer inventory = player.inventory;
        ItemStack[] mainInventory = inventory.mainInventory;

        ArrayList<ItemStack> helpList = new ArrayList<>();
        helpList.addAll(Arrays.asList(mainInventory));

        if (withCursorItem) {
            helpList.add(inventory.getItemStack());
        }

        for (ItemStack item : helpList) {
            if (item == null) continue;
            String name = item.getDisplayName();
            if (name.equalsIgnoreCase("air")) continue;
            if (name.equalsIgnoreCase("luft")) continue;
            list.add(item);
        }

        return list;
    }

    public static void drawWaypoint(LorenzVec pos, float partialTicks, Color color, boolean beacon) {
        drawWaypoint(pos, partialTicks, color, beacon, false);
    }

    public static void drawWaypoint(LorenzVec pos, float partialTicks, Color color, boolean beacon, boolean forceBeacon) {
        Entity viewer = Minecraft.getMinecraft().getRenderViewEntity();
        double viewerX = viewer.lastTickPosX + (viewer.posX - viewer.lastTickPosX) * partialTicks;
        double viewerY = viewer.lastTickPosY + (viewer.posY - viewer.lastTickPosY) * partialTicks;
        double viewerZ = viewer.lastTickPosZ + (viewer.posZ - viewer.lastTickPosZ) * partialTicks;

        double x = pos.getX() - viewerX;
        double y = pos.getY() - viewerY;
        double z = pos.getZ() - viewerZ;


        GlStateManager.disableDepth();
        GlStateManager.disableCull();
        GlStateManager.disableTexture2D();
        if (beacon) {
            double distSq = x * x + y * y + z * z;
            if (distSq > 5 * 5 || forceBeacon) {
                //TODO add beacon
//                GriffinUtils.renderBeaconBeam(x, y, z, color.getRGB(), 1.0f, partialTicks);
            }
        }
//        BlockPos a = pos.toBlocPos();
//        BlockPos b = pos.add(1, 1, 1).toBlocPos();
//        draw3DBox(new AxisAlignedBB(a, b), color, partialTicks);

        AxisAlignedBB aabb = new AxisAlignedBB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);

        draw3DBox(aabb, color, partialTicks);
        GlStateManager.disableLighting();
        GlStateManager.enableTexture2D();
        GlStateManager.enableDepth();

    }

    public static void draw3DLine(LorenzVec p1, LorenzVec p2, Color color, int lineWidth, boolean depth, float partialTicks) {
        GlStateManager.disableDepth();
        GlStateManager.disableCull();

//        Vec3 pos1 = new Vec3(p1.getX(), p1.getY(), p1.getZ());
//        Vec3 pos2 = new Vec3(p2.getX(), p2.getY(), p2.getZ());

        Entity render = Minecraft.getMinecraft().getRenderViewEntity();
        WorldRenderer worldRenderer = Tessellator.getInstance().getWorldRenderer();

        double realX = render.lastTickPosX + (render.posX - render.lastTickPosX) * partialTicks;
        double realY = render.lastTickPosY + (render.posY - render.lastTickPosY) * partialTicks;
        double realZ = render.lastTickPosZ + (render.posZ - render.lastTickPosZ) * partialTicks;

        GlStateManager.pushMatrix();
        GlStateManager.translate(-realX, -realY, -realZ);
        GlStateManager.disableTexture2D();
        GlStateManager.enableBlend();
        GlStateManager.disableAlpha();
        GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
        GL11.glLineWidth(lineWidth);


        if (!depth) {
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GlStateManager.depthMask(false);
        }
        GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f);
        worldRenderer.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION);

        worldRenderer.pos(p1.getX(), p1.getY(), p1.getZ()).endVertex();
        worldRenderer.pos(p2.getX(), p2.getY(), p2.getZ()).endVertex();
        Tessellator.getInstance().draw();

        GlStateManager.translate(realX, realY, realZ);
        if (!depth) {
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            GlStateManager.depthMask(true);
        }


        GlStateManager.disableBlend();
        GlStateManager.enableAlpha();
        GlStateManager.enableTexture2D();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        GlStateManager.popMatrix();


        GlStateManager.disableLighting();
        GlStateManager.enableDepth();
    }

    public static void draw3DBox(AxisAlignedBB aabb, Color colour, float partialTicks) {
        Entity render = Minecraft.getMinecraft().getRenderViewEntity();
        WorldRenderer worldRenderer = Tessellator.getInstance().getWorldRenderer();

        double realX = render.lastTickPosX + (render.posX - render.lastTickPosX) * partialTicks;
        double realY = render.lastTickPosY + (render.posY - render.lastTickPosY) * partialTicks;
        double realZ = render.lastTickPosZ + (render.posZ - render.lastTickPosZ) * partialTicks;

        GlStateManager.pushMatrix();
        GlStateManager.translate(-realX, -realY, -realZ);
        GlStateManager.disableTexture2D();
        GlStateManager.enableBlend();
        GlStateManager.disableAlpha();
        GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
        GL11.glLineWidth(2);
        GlStateManager.color(colour.getRed() / 255f, colour.getGreen() / 255f, colour.getBlue() / 255f, colour.getAlpha() / 255f);
        worldRenderer.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION);

        worldRenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        Tessellator.getInstance().draw();
        worldRenderer.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION);
        worldRenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        Tessellator.getInstance().draw();
        worldRenderer.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION);
        worldRenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
        worldRenderer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
        Tessellator.getInstance().draw();

        GlStateManager.translate(realX, realY, realZ);
        GlStateManager.disableBlend();
        GlStateManager.enableAlpha();
        GlStateManager.enableTexture2D();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        GlStateManager.popMatrix();
    }
}