summaryrefslogtreecommitdiff
path: root/src/com/dabomstew/pkrandom/RomFunctions.java
blob: 0dc71535a0ffab5399321f7c9d4e7c7d0c3f5c89 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package com.dabomstew.pkrandom;

/*----------------------------------------------------------------------------*/
/*--  RomFunctions.java - contains functions useful throughout the program. --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Originally part of "Universal Pokemon Randomizer" by Dabomstew        --*/
/*--  Pokemon and any associated names and the like are                     --*/
/*--  trademark and (C) Nintendo 1996-2020.                                 --*/
/*--                                                                        --*/
/*--  The custom code written here is licensed under the terms of the GPL:  --*/
/*--                                                                        --*/
/*--  This program is free software: you can redistribute it and/or modify  --*/
/*--  it under the terms of the GNU 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 General Public License for more details.                          --*/
/*--                                                                        --*/
/*--  You should have received a copy of the GNU General Public License     --*/
/*--  along with this program. If not, see <http://www.gnu.org/licenses/>.  --*/
/*----------------------------------------------------------------------------*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.dabomstew.pkrandom.pokemon.Evolution;
import com.dabomstew.pkrandom.pokemon.MoveLearnt;
import com.dabomstew.pkrandom.pokemon.Pokemon;
import com.dabomstew.pkrandom.romhandlers.RomHandler;

public class RomFunctions {

    public static Set<Pokemon> getBasicPokemon(RomHandler baseRom) {
        List<Pokemon> allPokes = baseRom.getPokemonInclFormes();
        Set<Pokemon> basicPokes = new TreeSet<>();
        for (Pokemon pkmn : allPokes) {
            if (pkmn != null) {
                if (pkmn.evolutionsTo.size() < 1) {
                    basicPokes.add(pkmn);
                }
            }
        }
        return basicPokes;
    }

    public static Set<Pokemon> getSplitEvolutions(RomHandler baseRom) {
        List<Pokemon> allPokes = baseRom.getPokemonInclFormes();
        Set<Pokemon> splitEvos = new TreeSet<>();
        for (Pokemon pkmn : allPokes) {
            if (pkmn != null) {
                if (pkmn.evolutionsTo.size() > 0) {
                    Evolution onlyEvo = pkmn.evolutionsTo.get(0);
                    if (!onlyEvo.carryStats) {
                        splitEvos.add(pkmn);
                    }
                }
            }
        }
        return splitEvos;
    }

    public static Set<Pokemon> getMiddleEvolutions(RomHandler baseRom, boolean includeSplitEvos) {
        List<Pokemon> allPokes = baseRom.getPokemon();
        Set<Pokemon> middleEvolutions = new TreeSet<>();
        for (Pokemon pkmn : allPokes) {
            if (pkmn != null) {
                if (pkmn.evolutionsTo.size() == 1 && pkmn.evolutionsFrom.size() > 0) {
                    Evolution onlyEvo = pkmn.evolutionsTo.get(0);
                    if (onlyEvo.carryStats || includeSplitEvos) {
                        middleEvolutions.add(pkmn);
                    }
                }
            }
        }
        return middleEvolutions;
    }

    public static Set<Pokemon> getFinalEvolutions(RomHandler baseRom, boolean includeSplitEvos) {
        List<Pokemon> allPokes = baseRom.getPokemon();
        Set<Pokemon> finalEvolutions = new TreeSet<>();
        for (Pokemon pkmn : allPokes) {
            if (pkmn != null) {
                if (pkmn.evolutionsTo.size() == 1 && pkmn.evolutionsFrom.size() == 0) {
                    Evolution onlyEvo = pkmn.evolutionsTo.get(0);
                    if (onlyEvo.carryStats || includeSplitEvos) {
                        finalEvolutions.add(pkmn);
                    }
                }
            }
        }
        return finalEvolutions;
    }

    /**
     * Get the 4 moves known by a Pokemon at a particular level.
     * 
     * @param pkmn Pokemon index to get moves for.
     * @param movesets Map of Pokemon indices mapped to movesets.
     * @param level Level to get at.
     * @return Array with move indices.
     */
    public static int[] getMovesAtLevel(int pkmn, Map<Integer, List<MoveLearnt>> movesets, int level) {
        return getMovesAtLevel(pkmn, movesets, level, 0);
    }

    public static int[] getMovesAtLevel(int pkmn, Map<Integer, List<MoveLearnt>> movesets, int level, int emptyValue) {
        int[] curMoves = new int[4];

        if (emptyValue != 0) {
            Arrays.fill(curMoves, emptyValue);
        }

        int moveCount = 0;
        List<MoveLearnt> movepool = movesets.get(pkmn);
        for (MoveLearnt ml : movepool) {
            if (ml.level > level) {
                // we're done
                break;
            }

            boolean alreadyKnownMove = false;
            for (int i = 0; i < moveCount; i++) {
                if (curMoves[i] == ml.move) {
                    alreadyKnownMove = true;
                    break;
                }
            }

            if (!alreadyKnownMove) {
                // add this move to the moveset
                if (moveCount == 4) {
                    // shift moves up and add to last slot
                    System.arraycopy(curMoves, 1, curMoves, 0, 3);
                    curMoves[3] = ml.move;
                } else {
                    // add to next available slot
                    curMoves[moveCount++] = ml.move;
                }
            }
        }

        return curMoves;
    }

    public static String camelCase(String original) {
        char[] string = original.toLowerCase().toCharArray();
        boolean docap = true;
        for (int j = 0; j < string.length; j++) {
            char current = string[j];
            if (docap && Character.isLetter(current)) {
                string[j] = Character.toUpperCase(current);
                docap = false;
            } else {
                if (!docap && !Character.isLetter(current) && current != '\'') {
                    docap = true;
                }
            }
        }
        return new String(string);
    }

    public static int freeSpaceFinder(byte[] rom, byte freeSpace, int amount, int offset) {
        // by default align to 4 bytes to make sure things don't break
        return freeSpaceFinder(rom, freeSpace, amount, offset, true);
    }

    public static int freeSpaceFinder(byte[] rom, byte freeSpace, int amount, int offset, boolean longAligned) {
        if (!longAligned) {
            // Find 2 more than necessary and return 2 into it,
            // to preserve stuff like FF terminators for strings
            // 161: and FFFF terminators for movesets
            byte[] searchNeedle = new byte[amount + 2];
            for (int i = 0; i < amount + 2; i++) {
                searchNeedle[i] = freeSpace;
            }
            return searchForFirst(rom, offset, searchNeedle) + 2;
        } else {
            // Find 5 more than necessary and return into it as necessary for
            // 4-alignment,
            // to preserve stuff like FF terminators for strings
            // 161: and FFFF terminators for movesets
            byte[] searchNeedle = new byte[amount + 5];
            for (int i = 0; i < amount + 5; i++) {
                searchNeedle[i] = freeSpace;
            }
            return (searchForFirst(rom, offset, searchNeedle) + 5) & ~3;
        }
    }

    public static List<Integer> search(byte[] haystack, byte[] needle) {
        return search(haystack, 0, haystack.length, needle);
    }

    public static List<Integer> search(byte[] haystack, int beginOffset, byte[] needle) {
        return search(haystack, beginOffset, haystack.length, needle);
    }

    public static List<Integer> search(byte[] haystack, int beginOffset, int endOffset, byte[] needle) {
        int currentMatchStart = beginOffset;
        int currentCharacterPosition = 0;

        int needleSize = needle.length;

        int[] toFillTable = buildKMPSearchTable(needle);
        List<Integer> results = new ArrayList<>();

        while ((currentMatchStart + currentCharacterPosition) < endOffset) {

            if (needle[currentCharacterPosition] == (haystack[currentCharacterPosition + currentMatchStart])) {
                currentCharacterPosition = currentCharacterPosition + 1;

                if (currentCharacterPosition == (needleSize)) {
                    results.add(currentMatchStart);
                    currentCharacterPosition = 0;
                    currentMatchStart = currentMatchStart + needleSize;

                }

            } else {
                currentMatchStart = currentMatchStart + currentCharacterPosition
                        - toFillTable[currentCharacterPosition];

                if (toFillTable[currentCharacterPosition] > -1) {
                    currentCharacterPosition = toFillTable[currentCharacterPosition];
                }

                else {
                    currentCharacterPosition = 0;

                }

            }
        }
        return results;
    }

    private static int searchForFirst(byte[] haystack, int beginOffset, byte[] needle) {
        int currentMatchStart = beginOffset;
        int currentCharacterPosition = 0;

        int docSize = haystack.length;
        int needleSize = needle.length;

        int[] toFillTable = buildKMPSearchTable(needle);

        while ((currentMatchStart + currentCharacterPosition) < docSize) {

            if (needle[currentCharacterPosition] == (haystack[currentCharacterPosition + currentMatchStart])) {
                currentCharacterPosition = currentCharacterPosition + 1;

                if (currentCharacterPosition == (needleSize)) {
                    return currentMatchStart;
                }

            } else {
                currentMatchStart = currentMatchStart + currentCharacterPosition
                        - toFillTable[currentCharacterPosition];

                if (toFillTable[currentCharacterPosition] > -1) {
                    currentCharacterPosition = toFillTable[currentCharacterPosition];
                }

                else {
                    currentCharacterPosition = 0;

                }

            }
        }
        return -1;
    }

    private static int[] buildKMPSearchTable(byte[] needle) {
        int[] stable = new int[needle.length];
        int pos = 2;
        int j = 0;
        stable[0] = -1;
        stable[1] = 0;
        while (pos < needle.length) {
            if (needle[pos - 1] == needle[j]) {
                stable[pos] = j + 1;
                pos++;
                j++;
            } else if (j > 0) {
                j = stable[j];
            } else {
                stable[pos] = 0;
                pos++;
            }
        }
        return stable;
    }

    public static String rewriteDescriptionForNewLineSize(String moveDesc, String newline, int lineSize,
            StringSizeDeterminer ssd) {
        // We rewrite the description we're given based on some new chars per
        // line.
        moveDesc = moveDesc.replace("-" + newline, "").replace(newline, " ");
        // Keep spatk/spdef as one word on one line
        moveDesc = moveDesc.replace("Sp. Atk", "Sp__Atk");
        moveDesc = moveDesc.replace("Sp. Def", "Sp__Def");
        moveDesc = moveDesc.replace("SP. ATK", "SP__ATK");
        moveDesc = moveDesc.replace("SP. DEF", "SP__DEF");
        String[] words = moveDesc.split(" ");
        StringBuilder fullDesc = new StringBuilder();
        StringBuilder thisLine = new StringBuilder();
        int currLineWC = 0;
        int currLineCC = 0;
        int linesWritten = 0;
        for (int i = 0; i < words.length; i++) {
            // Reverse the spatk/spdef preservation from above
            words[i] = words[i].replace("SP__", "SP. ");
            words[i] = words[i].replace("Sp__", "Sp. ");
            int reqLength = ssd.lengthFor(words[i]);
            if (currLineWC > 0) {
                reqLength++;
            }
            if (currLineCC + reqLength <= lineSize) {
                // add to current line
                if (currLineWC > 0) {
                    thisLine.append(' ');
                }
                thisLine.append(words[i]);
                currLineWC++;
                currLineCC += reqLength;
            } else {
                // Save current line, if applicable
                if (currLineWC > 0) {
                    if (linesWritten > 0) {
                        fullDesc.append(newline);
                    }
                    fullDesc.append(thisLine.toString());
                    linesWritten++;
                    thisLine = new StringBuilder();
                }
                // Start the new line
                thisLine.append(words[i]);
                currLineWC = 1;
                currLineCC = ssd.lengthFor(words[i]);
            }
        }

        // If the last line has anything add it
        if (currLineWC > 0) {
            if (linesWritten > 0) {
                fullDesc.append(newline);
            }
            fullDesc.append(thisLine.toString());
        }

        return fullDesc.toString();
    }

    public static String formatTextWithReplacements(String text, Map<String, String> replacements, String newline,
            String extraline, String newpara, int maxLineLength, StringSizeDeterminer ssd) {
        // Ends with a paragraph indicator?
        boolean endsWithPara = false;
        if (text.endsWith(newpara)) {
            endsWithPara = true;
            text = text.substring(0, text.length() - newpara.length());
        }
        // Replace current line endings with spaces
        text = text.replace(newline, " ").replace(extraline, " ");
        // Replace words if replacements are set
        // do it in two stages so the rules don't conflict
        if (replacements != null) {
            int index = 0;
            for (Map.Entry<String, String> toReplace : replacements.entrySet()) {
                index++;
                text = text.replace(toReplace.getKey(), "<tmpreplace" + index + ">");
            }
            index = 0;
            for (Map.Entry<String, String> toReplace : replacements.entrySet()) {
                index++;
                text = text.replace("<tmpreplace" + index + ">", toReplace.getValue());
            }
        }
        // Split on paragraphs and deal with each one individually
        String[] oldParagraphs = text.split(newpara.replace("\\", "\\\\"));
        StringBuilder finalResult = new StringBuilder();
        int sentenceNewLineSize = Math.max(10, maxLineLength / 2);
        for (int para = 0; para < oldParagraphs.length; para++) {
            String[] words = oldParagraphs[para].split(" ");
            StringBuilder fullPara = new StringBuilder();
            StringBuilder thisLine = new StringBuilder();
            int currLineWC = 0;
            int currLineCC = 0;
            int linesWritten = 0;
            char currLineLastChar = 0;
            for (String word : words) {
                int reqLength = ssd.lengthFor(word);
                if (currLineWC > 0) {
                    reqLength++;
                }
                if ((currLineCC + reqLength > maxLineLength)
                        || (currLineCC >= sentenceNewLineSize && (currLineLastChar == '.' || currLineLastChar == '?'
                        || currLineLastChar == '!' || currLineLastChar == '…' || currLineLastChar == ','))) {
                    // new line
                    // Save current line, if applicable
                    if (currLineWC > 0) {
                        if (linesWritten > 1) {
                            fullPara.append(extraline);
                        } else if (linesWritten == 1) {
                            fullPara.append(newline);
                        }
                        fullPara.append(thisLine.toString());
                        linesWritten++;
                        thisLine = new StringBuilder();
                    }
                    // Start the new line
                    thisLine.append(word);
                    currLineWC = 1;
                    currLineCC = ssd.lengthFor(word);
                    if (word.length() == 0) {
                        currLineLastChar = 0;
                    } else {
                        currLineLastChar = word.charAt(word.length() - 1);
                    }
                } else {
                    // add to current line
                    if (currLineWC > 0) {
                        thisLine.append(' ');
                    }
                    thisLine.append(word);
                    currLineWC++;
                    currLineCC += reqLength;
                    if (word.length() == 0) {
                        currLineLastChar = 0;
                    } else {
                        currLineLastChar = word.charAt(word.length() - 1);
                    }
                }
            }

            // If the last line has anything add it
            if (currLineWC > 0) {
                if (linesWritten > 1) {
                    fullPara.append(extraline);
                } else if (linesWritten == 1) {
                    fullPara.append(newline);
                }
                fullPara.append(thisLine.toString());
            }
            if (para > 0) {
                finalResult.append(newpara);
            }
            finalResult.append(fullPara.toString());
        }
        if (endsWithPara) {
            finalResult.append(newpara);
        }
        return finalResult.toString();
    }

    public interface StringSizeDeterminer {
        int lengthFor(String encodedText);
    }

    public static class StringLengthSD implements StringSizeDeterminer {

        @Override
        public int lengthFor(String encodedText) {
            return encodedText.length();
        }

    }

}