-
Notifications
You must be signed in to change notification settings - Fork 2
/
Randomiser.java
441 lines (376 loc) · 17.3 KB
/
Randomiser.java
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
package com.gmeister.temp.pkcmmsrando;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.gmeister.temp.pkcmmsrando.map.data.Block;
import com.gmeister.temp.pkcmmsrando.map.data.BlockSet;
import com.gmeister.temp.pkcmmsrando.map.data.Map;
import com.gmeister.temp.pkcmmsrando.map.data.Warp;
public class Randomiser
{
private Random random;
/**
* Initialises a Randomiser with a new Random.
*/
public Randomiser()
{
this.random = new Random();
}
/**
* Initialises a Randomiser with the provided Random.
* @param random the Random to initialise with.
*/
public Randomiser(Random random)
{
this.random = random;
}
/**
* Shuffles blocks to other blocks with the same collision.
* <br><br>
* Replaces every block with a block that has the same collision values.
* This creates maps that look like an absolute mess but play exactly like the original maps.
* <br><br>
* This randomiser is race safe.
* @param blockSet the BlockSet from which new Blocks will be drawn
* @param blocks the Blocks to replace
* @return a new array of random blocks
*/
public Block[] randomiseBlocksByCollision(BlockSet blockSet, Block[] blocks)
{
Random random = new Random(this.random.nextLong());
Block[] randomisedBlocks = new Block[blocks.length];
for (int i = 0; i < blocks.length; i++) if (blockSet.getBlocks().indexOf(blocks[i]) != 0)
{
ArrayList<Block> blockGroup = null;
for (ArrayList<Block> group : blockSet.getCollGroups()) if (group.contains(blocks[i]))
{
blockGroup = group;
break;
}
int index = random.nextInt(blockGroup.size());
//inelegant solution, but works
while (blockSet.getBlocks().indexOf(blockGroup.get(index)) == 0) index = random.nextInt(blockGroup.size());
randomisedBlocks[i] = blockGroup.get(index);
if (randomisedBlocks[i] == null || blockSet.getBlocks().indexOf(randomisedBlocks[i]) == 0) throw new IllegalStateException();
}
return randomisedBlocks;
}
/**
* Shuffles selected lines in a script.
* @param script the script
* @param toShuffle an array of the same length of the script, denoting the lines to shuffle
* @return a shuffled script
*/
public ArrayList<String> shuffleScriptLines(ArrayList<String> script, boolean[] toShuffle)
{
Random random = new Random(this.random.nextLong());
ArrayList<String> shuffledScript = new ArrayList<>();
for (int i = 0; i < toShuffle.length && i < script.size(); i++) if (toShuffle[i]) shuffledScript.add(script.get(i));
Collections.shuffle(shuffledScript, random);
for (int i = 0; i < script.size(); i++)
{
if (i >= toShuffle.length) shuffledScript.add(script.get(i));
else if (!toShuffle[i]) shuffledScript.add(i, script.get(i));
}
return shuffledScript;
}
/**
* Shuffles the pointers of all music tracks, such that every incidence of one track is replaced with a different track.
* <br><br>
* This randomiser is safe for races.
* @param script the music script to shuffle
* @return a shuffled script
*/
public ArrayList<String> shuffleMusicPointers(ArrayList<String> script)
{
boolean[] toShuffle = new boolean[script.size()];
for (int i = 0; i < script.size(); i++) toShuffle[i] = script.get(i).startsWith("\tdba") && !script.get(i).contains("Music_Nothing");
return this.shuffleScriptLines(script, toShuffle);
}
/**
* Shuffles the pointers of all sound effects, such that every incidence of one sound effect is replaced with a different sound effect.
* <br><br>
* This randomiser is safe for races, but having quick sound-effects replaced with long ones can greatly slow down pace.
* @param script the SFX script to shuffle
* @return a shuffled version of the provided script
*/
public ArrayList<String> shuffleSFXPointers(ArrayList<String> script)
{
boolean[] toShuffle = new boolean[script.size()];
for (int i = 0; i < script.size(); i++) toShuffle[i] = script.get(i).startsWith("\tdba");
return this.shuffleScriptLines(script, toShuffle);
}
/**
* Move all trainers to a random position on the map.
* <br><br>
* This edits the map script in place and requires it to be written out.
* <br><br>
* This randomiser is not safe for races as trainers may block required paths.
* @param map the map to shuffle trainer locations on
*/
public void randomiseTrainerLocation(Map map)
{
Random random = new Random(this.random.nextLong());
Pattern commentsPattern = Pattern.compile("\\s*;.*");
Pattern mapConstPattern = Pattern.compile("\\tmap_const\\s+");
Pattern commaWhitespacePattern = Pattern.compile("\\s*,\\s*");
Pattern objectEventPattern = Pattern.compile("\\tobject_event\\s+");
Pattern numberPattern = Pattern.compile("\\d+");
for (int i = 0; i < map.getScript().size(); i++)
{
String line = map.getScript().get(i);
if (objectEventPattern.matcher(line).find())
{
String argsLine = commentsPattern.matcher(line).replaceFirst("");
argsLine = mapConstPattern.matcher(line).replaceFirst("");
String[] args = commaWhitespacePattern.split(argsLine);
if (args[9].equals("OBJECTTYPE_TRAINER"))
{
int newX = random.nextInt(map.getXCapacity());
int newY = random.nextInt(map.getYCapacity());
StringBuffer buffer = new StringBuffer();
Matcher numbers = numberPattern.matcher(line);
numbers.find();
numbers.appendReplacement(buffer, String.valueOf(newX));
numbers.find();
numbers.appendReplacement(buffer, String.valueOf(newY));
numbers.appendTail(buffer);
map.getScript().set(i, buffer.toString());
}
}
}
}
public void shuffleWarpGroups(ArrayList<ArrayList<Warp>> warpGroups, boolean allowSelfWarps, boolean twoWay)
{
//Get a Random object
Random random = new Random(this.random.nextLong());
if (warpGroups.size() % 2 != 0 && twoWay && !allowSelfWarps) throw new IllegalArgumentException("Could not avoid self warps as there are an odd number of destinations");
//Create a random list of destinations
ArrayList<ArrayList<Warp>> groups = new ArrayList<>(warpGroups);
ArrayList<ArrayList<Warp>> shuffledGroups = new ArrayList<>(warpGroups);
Collections.shuffle(shuffledGroups, random);
//Pull random destinations
ArrayList<ArrayList<Warp>> oldGroups = new ArrayList<>();
ArrayList<ArrayList<Warp>> newGroups = new ArrayList<>();
while (shuffledGroups.size() > 0)
{
ArrayList<Warp> newGroup = shuffledGroups.get(0);
boolean testedAllOldDestsForThisGroup = false;
oldGroupLoops:
while (true)
{
for (ArrayList<Warp> oldGroup : groups) if (allowSelfWarps || ((!newGroups.contains(oldGroup) || testedAllOldDestsForThisGroup) && !oldGroup.equals(newGroup)))
{
groups.remove(oldGroup);
oldGroups.add(oldGroup);
shuffledGroups.remove(newGroup);
newGroups.add(newGroup);
if (twoWay)
{
groups.remove(newGroup);
oldGroups.add(newGroup);
shuffledGroups.remove(oldGroup);
newGroups.add(oldGroup);
}
break oldGroupLoops;
}
if (!testedAllOldDestsForThisGroup)
{
testedAllOldDestsForThisGroup = true;
}
else
{
throw new IllegalStateException("Could not find an old destination for the warp that links " + newGroup.get(0).getMap().getConstName() + " to " + newGroup.get(0).getDestination().getMap().getConstName());
}
}
}
for (int i = 0; i < oldGroups.size(); i++)
{
ArrayList<Warp> oldGroup = oldGroups.get(i);
ArrayList<Warp> newGroup = newGroups.get(i);
for (int j = 0; j < oldGroup.size(); j++) oldGroup.get(j).setDestination(newGroup.get(j % newGroup.size()));
}
}
public void buildWarpGroups(ArrayList<ArrayList<Warp>> warpGroups, HashMap<ArrayList<Warp>, ArrayList<ArrayList<Warp>>> accessibleGroups, ArrayList<Warp> startingGroup)
{
/*
* Make a random
* Check that the accessible groups contains all keys from the groups
* check that the groups contains the starting group
* Pick a random group that we can currently access
* Pick a random group that we haven't selected yet
* if performing this link would mean there is at least one warp we can still access (unless it's the last one) then do it
* don't do the self warps so include similar code to above
*/
Random random = new Random(this.random.nextLong());
for (ArrayList<Warp> warpGroup : warpGroups) if (!accessibleGroups.containsKey(warpGroup)) throw new IllegalArgumentException("accessibleGroups does not contain a key for every group");
for (ArrayList<Warp> warpGroup : accessibleGroups.keySet()) if (!warpGroups.contains(warpGroup)) throw new IllegalArgumentException("accessibleGroups contains groups that are not present in warpGroups");
if (!warpGroups.contains(startingGroup)) throw new IllegalArgumentException("warpGroups does not contain startingGroup");
if (warpGroups.size() % 2 != 0) throw new IllegalArgumentException("Could not avoid self warps as there are an odd number of groups");
//Create a random list of groups
ArrayList<ArrayList<Warp>> shuffledGroups = new ArrayList<>(warpGroups);
Collections.shuffle(shuffledGroups, random);
ArrayList<ArrayList<Warp>> freeGroups = new ArrayList<>();
freeGroups.add(startingGroup);
freeGroups.addAll(accessibleGroups.get(startingGroup));
/*
* We can't enforce both not allowing self warps and making sure we're always allowed to select the old group
* Does this even solve the issue of things being added multiple times?
*/
//Pull random destinations
ArrayList<ArrayList<Warp>> oldGroups = new ArrayList<>();
ArrayList<ArrayList<Warp>> newGroups = new ArrayList<>();
while (shuffledGroups.size() > 0)
{
ArrayList<Warp> oldGroup = freeGroups.get(0);
oldGroupLoops:
while (true)
{
for (ArrayList<Warp> newGroup : shuffledGroups) if (oldGroup != newGroup || shuffledGroups.size() < 2)
{
ArrayList<ArrayList<Warp>> nextFreeGroups = new ArrayList<>(freeGroups);
for (ArrayList<Warp> freeGroup : accessibleGroups.get(newGroup)) if (shuffledGroups.contains(freeGroup)) nextFreeGroups.add(freeGroup);
nextFreeGroups.remove(oldGroup);
nextFreeGroups.remove(newGroup);
if (nextFreeGroups.size() > 0 || shuffledGroups.size() < 2)
{
oldGroups.add(oldGroup);
newGroups.add(newGroup);
oldGroups.add(newGroup);
newGroups.add(oldGroup);
shuffledGroups.remove(newGroup);
shuffledGroups.remove(oldGroup);
freeGroups = nextFreeGroups;
break oldGroupLoops;
}
}
throw new IllegalStateException("Could not find a destination for " + oldGroup.get(0));
}
}
for (int i = 0; i < oldGroups.size(); i++)
{
ArrayList<Warp> oldGroup = oldGroups.get(i);
ArrayList<Warp> newGroup = newGroups.get(i);
for (int j = 0; j < oldGroup.size(); j++) oldGroup.get(j).setDestination(newGroup.get(j % newGroup.size()));
}
}
/*
* Currently, this method performs various initial filtering steps which could ideally be moved out to a more relevant class.
* For example, one-way warps are not selected as destinations. While this acts as a good balancing feature, it is not necessary for all warp randomisers.
*
* Groups of destinations will be considered in the future. This will allow 2-tile warp carpets leading to other 2-tile warp carpets to be considered as a single destination each.
*/
/**
* Shuffles the destinations of provided warps.
* <br><br>
* Any warp which is accessible from another warp, does not move, and leads back to one of the warps that leads to it is selected as a destination for shuffling.
* All warps leading to a destination which was selected for shuffling are then redirected to a shuffled destination.
* <br><br>
* This randomiser has the potential to be safe for races with enough moderation of the warps provided, but currently this is not present in the code.
* @param warps the list of warps to shuffle the destination of
* @param allowSelfWarps whether a warp can select itself as its destination
* @param twoWay whether to force randomised destinations to lead back to where they came from
* @param preserveMapConnections whether to keep connections between Maps
*/
public void shuffleWarpDestinations(ArrayList<Warp> warps, boolean allowSelfWarps, boolean twoWay, boolean preserveMapConnections)
{
//Get a Random object
Random random = new Random(this.random.nextLong());
//Create groups of warps which all lead to the same warp
ArrayList<ArrayList<Warp>> warpSourcess = new ArrayList<>();
for (int i = 0; i < warps.size(); i++) warpSourcess.add(new ArrayList<>());
for (Warp warp : warps) if (warp.getDestination() != null) warpSourcess.get(warps.indexOf(warp.getDestination())).add(warp);
//Create a list of useable destinations
ArrayList<Warp> destinations = new ArrayList<>();
for (Warp warp : warps) if (warpSourcess.get(warps.indexOf(warp)).size() > 0 //Destinations must have another warp lead to them
&& warp.getDestination() != null //Destinations must not move (SS Aqua, Elevators, Cable Club)
&& warp.hasAccessibleDestination() //Destinations must be able to reach their own destination
&& warpSourcess.get(warps.indexOf(warp)).contains(warp.getDestination())) //Destinations lead back to one of their sources
{
//Destinations have at least one warp that can access it
for (Warp source : warpSourcess.get(warps.indexOf(warp))) if (source.hasAccessibleDestination())
{
destinations.add(warp);
break;
}
}
for (int i = 0; i < destinations.size(); i++) if (!destinations.contains(destinations.get(i).getDestination()))
{
System.out.println("A dest was not included!");
destinations.remove(i);
i--;
}
ArrayList<Map> maps = new ArrayList<>();
ArrayList<ArrayList<Map>> mapConnections = new ArrayList<>();
ArrayList<ArrayList<Map>> newConnections = new ArrayList<>();
for (Warp warp : destinations)
{
if (!maps.contains(warp.getMap()))
{
maps.add(warp.getMap());
mapConnections.add(new ArrayList<>());
newConnections.add(new ArrayList<>());
}
if (warp.getDestination() != null) mapConnections.get(maps.indexOf(warp.getMap())).add(warp.getDestination().getMap());
}
if (destinations.size() % 2 != 0 && twoWay && !allowSelfWarps) throw new IllegalArgumentException("Could not avoid self warps as there are an odd number of destinations");
//Create a random list of destinations
ArrayList<Warp> shuffledDestinations = new ArrayList<>(destinations);
Collections.shuffle(shuffledDestinations, random);
//Pull random destinations
ArrayList<Warp> oldDests = new ArrayList<>();
ArrayList<Warp> newDests = new ArrayList<>();
while (shuffledDestinations.size() > 0)
{
Warp newDest = shuffledDestinations.get(0);
boolean testedAllOldDestsForThisWarp = false;
oldDestLoops:
while (true)
{
for (Warp oldDest : destinations) if (allowSelfWarps || ((!newDests.contains(oldDest) || testedAllOldDestsForThisWarp) && !oldDest.equals(newDest)))
{
if (preserveMapConnections &&
Collections.frequency(newConnections.get(maps.indexOf(oldDest.getDestination().getMap())), newDest.getDestination().getMap())
>= Collections.frequency(mapConnections.get(maps.indexOf(oldDest.getDestination().getMap())), newDest.getDestination().getMap()))
continue;
destinations.remove(oldDest);
oldDests.add(oldDest);
shuffledDestinations.remove(newDest);
newDests.add(newDest);
newConnections.get(maps.indexOf(oldDest.getDestination().getMap())).add(newDest.getDestination().getMap());
if (twoWay)
{
destinations.remove(newDest);
oldDests.add(newDest);
shuffledDestinations.remove(oldDest);
newDests.add(oldDest);
newConnections.get(maps.indexOf(newDest.getDestination().getMap())).add(oldDest.getDestination().getMap());
}
break oldDestLoops;
}
if (!testedAllOldDestsForThisWarp)
{
testedAllOldDestsForThisWarp = true;
}
else
{
throw new IllegalStateException("Could not find an old destination for the warp that links " + newDest.getMap().getConstName() + " to " + newDest.getDestination().getMap().getConstName());
}
}
}
if (destinations.size() > 0) throw new IllegalStateException("Logic error: not all destinations were arrigned a new one");
if (oldDests.size() != newDests.size()) throw new IllegalStateException("Logic error: there are different numbers of old and new destinations");
//For each warp, record it's new destination to prevent concurrent modification errors
ArrayList<Warp> newDestinations = new ArrayList<>();
for (Warp warp : warps)
{
if (oldDests.contains(warp.getDestination())) newDestinations.add(newDests.get(oldDests.indexOf(warp.getDestination())).getDestination());
else newDestinations.add(warp.getDestination());
}
//Apply the new recorded destinations
for (int i = 0; i < warps.size(); i++) warps.get(i).setDestination(newDestinations.get(i));
}
}