From 348c4b7aa5d2ce5109191ce6f6f91e9382f2d339 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 20:45:34 +0100 Subject: [PATCH] Add CustomMap --- src/de/steamwar/lobby/LobbySystem.java | 4 +- src/de/steamwar/lobby/map/CustomMap.java | 189 +++++++++++++++++++++++ src/de/steamwar/lobby/map/MapColors.png | Bin 0 -> 1677 bytes 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/lobby/map/CustomMap.java create mode 100644 src/de/steamwar/lobby/map/MapColors.png diff --git a/src/de/steamwar/lobby/LobbySystem.java b/src/de/steamwar/lobby/LobbySystem.java index 9ebcd3a..3f0badd 100644 --- a/src/de/steamwar/lobby/LobbySystem.java +++ b/src/de/steamwar/lobby/LobbySystem.java @@ -19,11 +19,12 @@ package de.steamwar.lobby; -import de.steamwar.lobby.command.ModifyCommand; import de.steamwar.lobby.command.FlyCommand; import de.steamwar.lobby.command.HologramCommand; +import de.steamwar.lobby.command.ModifyCommand; import de.steamwar.lobby.command.PortalCommand; import de.steamwar.lobby.listener.*; +import de.steamwar.lobby.map.CustomMap; import de.steamwar.lobby.team.TeamPlayer; import de.steamwar.message.Message; import org.bukkit.plugin.java.JavaPlugin; @@ -67,6 +68,7 @@ public class LobbySystem extends JavaPlugin { new AlphaWall(l -> l.getZ() > 892, AlphaWall.REFLECT_Z); new AlphaWall(l -> l.getZ() < 1794, AlphaWall.REFLECT_Z); + CustomMap.init(); } @Override diff --git a/src/de/steamwar/lobby/map/CustomMap.java b/src/de/steamwar/lobby/map/CustomMap.java new file mode 100644 index 0000000..24bc598 --- /dev/null +++ b/src/de/steamwar/lobby/map/CustomMap.java @@ -0,0 +1,189 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.lobby.map; + +import org.bukkit.map.MapPalette; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class CustomMap { + + private static List colorList = new ArrayList<>(); + + private static Color c(int r, int g, int b) { + return new Color(r, g, b); + } + + static { + try { + BufferedImage bufferedImage = ImageIO.read(CustomMap.class.getResourceAsStream("MapColors.png")); + for (int x = 0; x < bufferedImage.getWidth(); x += 16) { + for (int y = 0; y < bufferedImage.getHeight(); y += 16) { + colorList.add(new Color(bufferedImage.getRGB(x, y))); + } + } + new CustomMap(ImageIO.read(CustomMap.class.getResourceAsStream("wgsbanner13.png"))); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); + } + } + + public static void init() { + } + + private List tileImages = new ArrayList<>(); + + public CustomMap(BufferedImage image) { + if (image.getWidth() % 128 != 0) { + throw new IllegalArgumentException("Image width must be a multiple of 128"); + } + if (image.getHeight() % 128 != 0) { + throw new IllegalArgumentException("Image height must be a multiple of 128"); + } + + BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); + for (int x = 0; x < image.getWidth(); x++) { + for (int y = 0; y < image.getHeight(); y++) { + Color color = new Color(image.getRGB(x, y)); + Color nearest = getNearest(color, false); + if (nearest == null) { + throw new IllegalArgumentException("Image contains invalid color: " + color); + } + bufferedImage.setRGB(x, y, nearest.getRGB()); + } + } + for (int x = 1; x < bufferedImage.getWidth() - 1; x++) { + for (int y = 1; y < bufferedImage.getHeight() - 1; y++) { + Color toMatch = new Color(image.getRGB(x, y)); + List current = new ArrayList<>(); + current.add(new Color(bufferedImage.getRGB(x, y - 1))); + current.add(new Color(bufferedImage.getRGB(x - 1, y))); + current.add(new Color(bufferedImage.getRGB(x, y))); + current.add(new Color(bufferedImage.getRGB(x + 1, y))); + current.add(new Color(bufferedImage.getRGB(x, y + 1))); + + List currentNearest = current.stream().map(c -> getNearest(c, true)).collect(Collectors.toList()); + + double min = Double.MAX_VALUE; + List minNearest = null; + for (int i = 0; i < 1 << current.size(); i++) { + List currentAverage = new ArrayList<>(); + for (int j = 0; j < current.size(); j++) { + if ((i & (1 << j)) == 0) { + currentAverage.add(current.get(j)); + } else { + currentAverage.add(currentNearest.get(j)); + } + } + Color average = getAverage(currentAverage); + double distance = colorDistance(average, toMatch); + if (distance < min) { + min = distance; + minNearest = currentAverage; + } + } + + if (minNearest == null) { + continue; + } + bufferedImage.setRGB(x, y - 1, minNearest.get(0).getRGB()); + bufferedImage.setRGB(x - 1, y, minNearest.get(1).getRGB()); + bufferedImage.setRGB(x, y, minNearest.get(2).getRGB()); + bufferedImage.setRGB(x + 1, y, minNearest.get(3).getRGB()); + bufferedImage.setRGB(x, y + 1, minNearest.get(4).getRGB()); + } + } + + for (int x = 0; x < bufferedImage.getWidth(); x += 128) { + for (int y = 0; y < bufferedImage.getHeight(); y += 128) { + BufferedImage subImage = bufferedImage.getSubimage(x, y, 128, 128); + TileImage tileImage = new TileImage(subImage, x / 128, y / 128); + tileImages.add(tileImage); + } + } + + try { + ImageIO.write(bufferedImage, "png", new File("/home/minecraft/server/NeueLobby/Lobby/map.png")); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); + } + } + + private static Color getAverage(List colors) { + int r = 0; + int g = 0; + int b = 0; + for (Color color : colors) { + r += color.getRed(); + g += color.getGreen(); + b += color.getBlue(); + } + return new Color(r / colors.size(), g / colors.size(), b / colors.size()); + } + + private static Color getNearest(Color color, boolean notSame) { + if (!notSame) { + return MapPalette.getColor(MapPalette.matchColor(color)); + } + + double min = Double.MAX_VALUE; + Color nearest = null; + for (Color c : colorList) { + double distance = colorDistance(color, c); + if (c.equals(color)) { + continue; + } + if (distance < min) { + min = distance; + nearest = c; + } + } + return nearest; + } + + private static double colorDistance(Color c1, Color c2) { + double rmean = (double)(c1.getRed() + c2.getRed()) / 2.0D; + double r = (double)(c1.getRed() - c2.getRed()); + double g = (double)(c1.getGreen() - c2.getGreen()); + int b = c1.getBlue() - c2.getBlue(); + double weightR = 2.0D + rmean / 256.0D; + double weightG = 4.0D; + double weightB = 2.0D + (255.0D - rmean) / 256.0D; + return weightR * r * r + weightG * g * g + weightB * (double)b * (double)b; + } + + private static class TileImage { + private BufferedImage image; + private int tileX; + private int tileY; + + public TileImage(BufferedImage image, int tileX, int tileY) { + this.image = image; + this.tileX = tileX; + this.tileY = tileY; + } + } +} diff --git a/src/de/steamwar/lobby/map/MapColors.png b/src/de/steamwar/lobby/map/MapColors.png new file mode 100644 index 0000000000000000000000000000000000000000..5e44d49211da7a3986781f15b55d02cb82fb79d4 GIT binary patch literal 1677 zcmcJPYfuws6ow;Mw1CB#TB33ZOhv3v)Iq@(NRTF^j1^cbmzv5tl@pa9 zk($h1bC}`^AEOrtt>kgbg1C` zg#*6fYx6?xl$C}OLW_Vt1{-vasu*}!rGZq%GNyI%kpX|)YazOo^t-Nn7tf)~orNXLnDf_V zQn;CzIjn_kp0%*zT1oshc$k0%+4QOFELH$6Fu4WWeec21e>iv;sxXIvggA zl@RhOMal(YVn$rjgKRRXw%=Zh3ywyMOKj<3CVD#SGO^Ug^27IzuYy=F=eC)|mYNQ6nXcq;~? z9_Az`f^uLXz3f@8loqMOrLNFQ-8^z#YRmsCQW?z2_lgHCvlS@vc(Fh`!a0?uh;ArO z;0}G&TWSp#HEHzJug`}dl|o-dG#~j6!X}!FsqXY%vaip8+Sl8vh~A!AlsX=(mVDkO z!+oTqVlpy1RIJ=!!(P6$tc)6O9oT3KtRRM4EQ{`GLLf1QGkkntX^vCiDCk89yQqo~ zo7=?7MqACiQE5Tutk9vf(ApdXvAh;dB<38T8mxE59#DheK+v!L3R?*}ffCOJJAT#ko z&{*AtO7~@(?dI_+qP@amm+G^6sb#k{E}!4>{uf87gS0mwOmr->f;naR3F823(I*hX z?2wq$eUT6>g*Cp%Lp38e9_T4Y55fWBT4oi|KV;(oQ=S80u?H^`3XzK;X8LF=X;Vue z;I1!jtN( zsd2je?Z4AAgdnhM8y5@