|
| 1 | +/** |
| 2 | + * Copyright (c) 2020, 2021, 2022 Adrian Siekierka |
| 3 | + * |
| 4 | + * This file is part of zima. |
| 5 | + * |
| 6 | + * zima is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * zima is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with zima. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package pl.asie.libzzt; |
| 20 | + |
| 21 | +import com.google.common.collect.BiMap; |
| 22 | +import com.google.common.collect.HashBiMap; |
| 23 | +import lombok.AllArgsConstructor; |
| 24 | +import lombok.Getter; |
| 25 | +import pl.asie.zima.Constants; |
| 26 | + |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.InputStreamReader; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Locale; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.stream.Collectors; |
| 37 | + |
| 38 | +@Getter |
| 39 | +@AllArgsConstructor |
| 40 | +public class WeaveZZTPlatformData { |
| 41 | + private static final BiMap<String, String> INTERNAL_TO_WEAVE = HashBiMap.create(Map.ofEntries( |
| 42 | + Map.entry("BOARD_EDGE", "EDGE"), |
| 43 | + Map.entry("MESSAGE_TIMER", "DARKNESS"), |
| 44 | + Map.entry("BLINK_RAY_EW", "BLINKEW"), |
| 45 | + Map.entry("BLINK_RAY_NS", "BLINKNS"), |
| 46 | + Map.entry("UNKNOWN_46", "CUSTOMTEXT"), |
| 47 | + Map.entry("TEXT_BLUE", "BLUETEXT"), |
| 48 | + Map.entry("TEXT_GREEN", "GREENTEXT"), |
| 49 | + Map.entry("TEXT_CYAN", "CYANTEXT"), |
| 50 | + Map.entry("TEXT_RED", "REDTEXT"), |
| 51 | + Map.entry("TEXT_PURPLE", "PURPLETEXT"), |
| 52 | + Map.entry("TEXT_YELLOW", "YELLOWTEXT"), |
| 53 | + Map.entry("TEXT_WHITE", "WHITETEXT") |
| 54 | + )); |
| 55 | + |
| 56 | + private static final Map<String, Integer> COLOR_NUMBERS = Map.ofEntries( |
| 57 | + Map.entry("BLACK", 0), |
| 58 | + Map.entry("DKBLUE", 1), |
| 59 | + Map.entry("DKGREEN", 2), |
| 60 | + Map.entry("DKCYAN", 3), |
| 61 | + Map.entry("DKRED", 4), |
| 62 | + Map.entry("DKPURPLE", 5), |
| 63 | + Map.entry("BROWN", 6), |
| 64 | + Map.entry("GRAY", 7), |
| 65 | + Map.entry("GREY", 7), |
| 66 | + Map.entry("DKGRAY", 8), |
| 67 | + Map.entry("DKGREY", 8), |
| 68 | + Map.entry("BLUE", 9), |
| 69 | + Map.entry("GREEN", 10), |
| 70 | + Map.entry("CYAN", 11), |
| 71 | + Map.entry("RED", 12), |
| 72 | + Map.entry("PURPLE", 13), |
| 73 | + Map.entry("YELLOW", 14), |
| 74 | + Map.entry("WHITE", 15) |
| 75 | + ); |
| 76 | + |
| 77 | + private final int[] palette; |
| 78 | + private final int maxStatCount; |
| 79 | + private final boolean blinkingDisabled; |
| 80 | + private final ElementLibrary library; |
| 81 | + |
| 82 | + public static WeaveZZTPlatformData parse(ElementLibrary base, InputStream is) throws IOException { |
| 83 | + List<Element> elements = new ArrayList<>(base.getElements()); |
| 84 | + List<String> names = elements.stream().map(base::getInternalName).collect(Collectors.toList()); |
| 85 | + List<String> weaveNames = names.stream().map(e -> INTERNAL_TO_WEAVE.getOrDefault(e, e)).collect(Collectors.toList()); |
| 86 | + int[] palette = Arrays.copyOf(Constants.EGA_PALETTE, 16); |
| 87 | + boolean blinkingDisabled = false; |
| 88 | + boolean paletteModified = false; |
| 89 | + int maxStatCount = 150; |
| 90 | + |
| 91 | + // Weave ZZT default patches |
| 92 | + { |
| 93 | + int customTextIdx = weaveNames.indexOf("CUSTOMTEXT"); |
| 94 | + if (customTextIdx >= 0) { |
| 95 | + elements.set(customTextIdx, elements.get(customTextIdx).withTextColor(0)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // File patches |
| 100 | + if (is != null) try (InputStreamReader isr = new InputStreamReader(is); BufferedReader reader = new BufferedReader(isr)) { |
| 101 | + String line; |
| 102 | + while ((line = reader.readLine()) != null) { |
| 103 | + String s = line.strip(); |
| 104 | + if (s.length() > 1 && s.charAt(0) != '#') { |
| 105 | + String[] keyValue = s.split("=", 2); |
| 106 | + if (keyValue.length == 2) { |
| 107 | + String keyFull = keyValue[0].replaceAll("[^.A-Za-z]", ""); |
| 108 | + String value = keyValue[1].replaceAll("[^-.A-Za-z0-9]", ""); |
| 109 | + Integer valueNum = COLOR_NUMBERS.get(value.toUpperCase(Locale.ROOT)); |
| 110 | + if (valueNum == null) { |
| 111 | + try { |
| 112 | + valueNum = Integer.parseInt(value); |
| 113 | + } catch (NumberFormatException e) { |
| 114 | + // pass |
| 115 | + } |
| 116 | + } |
| 117 | + if ("other.maxstats".equalsIgnoreCase(keyFull)) { |
| 118 | + if (valueNum != null) { |
| 119 | + maxStatCount = valueNum; |
| 120 | + } |
| 121 | + continue; |
| 122 | + } else if ("theme.blinking".equalsIgnoreCase(keyFull)) { |
| 123 | + if (valueNum != null) { |
| 124 | + blinkingDisabled = valueNum != 0; |
| 125 | + } |
| 126 | + } |
| 127 | + String[] key = keyFull.split("\\."); |
| 128 | + if (key.length == 2) { |
| 129 | + if ("pal".equalsIgnoreCase(key[0])) { |
| 130 | + Integer palIdx = COLOR_NUMBERS.get(key[1].toUpperCase(Locale.ROOT)); |
| 131 | + if (palIdx == null) { |
| 132 | + try { |
| 133 | + palIdx = Integer.parseInt(key[1]); |
| 134 | + } catch (NumberFormatException e) { |
| 135 | + // pass |
| 136 | + } |
| 137 | + } |
| 138 | + String[] palValues = keyValue[1].replaceAll("[^,A-Za-z0-9]", "").split(","); |
| 139 | + if (palValues.length == 3 && palIdx != null) { |
| 140 | + try { |
| 141 | + int red = (Integer.parseInt(palValues[0]) * 255 / 63) & 0xFF; |
| 142 | + int green = (Integer.parseInt(palValues[1]) * 255 / 63) & 0xFF; |
| 143 | + int blue = (Integer.parseInt(palValues[2]) * 255 / 63) & 0xFF; |
| 144 | + System.out.println("Modifying color " + palIdx + " => " + red + ", " + green + ", " + blue); |
| 145 | + palette[palIdx] = (red << 16) | (green << 8) | blue; |
| 146 | + paletteModified = true; |
| 147 | + } catch (NumberFormatException e) { |
| 148 | + // pass |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + int elementIdx = weaveNames.indexOf(key[0].toUpperCase(Locale.ROOT)); |
| 153 | + if (elementIdx >= 0 && valueNum != null) { |
| 154 | + System.out.println("Modifying " + names.get(elementIdx) + " -> " + key[1]); |
| 155 | + Element element = elements.get(elementIdx); |
| 156 | + if ("CHAR".equalsIgnoreCase(key[1])) { |
| 157 | + element = element.withCharacter(valueNum & 0xFF); |
| 158 | + } else if ("FG".equalsIgnoreCase(key[1])) { |
| 159 | + if (element.isText()) { |
| 160 | + element = element.withTextColor((element.getTextColor() & 0xF0) | (valueNum & 0x0F)); |
| 161 | + } else { |
| 162 | + element = element.withColor((element.getColor() & 0xF0) | (valueNum & 0x0F)); |
| 163 | + } |
| 164 | + } else if ("BG".equalsIgnoreCase(key[1])) { |
| 165 | + if (element.isText()) { |
| 166 | + element = element.withTextColor((element.getTextColor() & 0x0F) | ((valueNum & 0x0F) << 4)); |
| 167 | + } else { |
| 168 | + element = element.withColor((element.getColor() & 0x0F) | ((valueNum & 0x0F) << 4)); |
| 169 | + } |
| 170 | + } else if ("DESTRUCTIBLE".equalsIgnoreCase(key[1])) { |
| 171 | + element = element.withDestructible(value.equalsIgnoreCase("TRUE")); |
| 172 | + } else if ("PUSHABLE".equalsIgnoreCase(key[1])) { |
| 173 | + element = element.withPushable(value.equalsIgnoreCase("TRUE")); |
| 174 | + } else if ("PLACEABLEONTOP".equalsIgnoreCase(key[1])) { |
| 175 | + element = element.withPlaceableOnTop(value.equalsIgnoreCase("TRUE")); |
| 176 | + } else if ("WALKABLE".equalsIgnoreCase(key[1])) { |
| 177 | + element = element.withWalkable(value.equalsIgnoreCase("TRUE")); |
| 178 | + } else if ("CYCLE".equalsIgnoreCase(key[1])) { |
| 179 | + element = element.withCycle(valueNum); |
| 180 | + } else if ("SCOREVALUE".equalsIgnoreCase(key[1])) { |
| 181 | + element = element.withScoreValue(valueNum); |
| 182 | + } |
| 183 | + // Missing: PARAM1, PARAM2, PARAM3 |
| 184 | + elements.set(elementIdx, element); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return new WeaveZZTPlatformData(paletteModified ? palette : null, maxStatCount, blinkingDisabled, new ElementLibrary(names, elements)); |
| 193 | + } |
| 194 | +} |
0 commit comments