// Store the logics
ArrayList<Logic> logics = new ArrayList<Logic>();
// Create a logic factory to handle the creation of in-game logics
LogicFactory factory = new LogicFactory();
int l = s.length;
// Skip to the start of the logics
n += 7;
// If we have a scrolling background we need to skip some more
if (MonolithResource.readInteger(s, n) == 1313818964) n += 5;
// Read logics
try {
while (n + 284 < l) {
// Data of the logic
int dataOffset = n;
// String lengths
int nameLength = MonolithResource.readInteger(s, n + 4);
int logicLength = MonolithResource.readInteger(s, n + 8);
int graphicLength = MonolithResource.readInteger(s, n + 12);
int aniLength = MonolithResource.readInteger(s, n + 16);
if (logicLength == 0) break;
// Skip to name / image / animation of the logic
n += 284;
// Get the logic, graphic file and animation/sound file if there is one
n += nameLength;
String logicRef = new String(Arrays.copyOfRange(s, n, n + logicLength));
n += logicLength;
String graphicRef = new String(Arrays.copyOfRange(s, n, n + graphicLength)).replace('_', '/');
n += graphicLength;
String aniRef = new String(Arrays.copyOfRange(s, n, n + aniLength)).replace('_', '/');
n += aniLength;
if (!logicRef.matches("[A-Za-z]+")) break;
// Map to correct world if a level reference
if (graphicRef.startsWith("LEVEL")) graphicRef = "AREA" + fWorld + "/IMAGEZ" + graphicRef.substring(5);
else if (graphicRef.length() > 0) graphicRef = "GAME/IMAGEZ" + graphicRef.substring(4);
if (aniRef.startsWith("LEVEL")) aniRef = "AREA" + fWorld + "/ANIZ" + aniRef.substring(5);
else if (aniRef.length() > 0) aniRef = "GAME/ANIZ" + aniRef.substring(4);
// For some reason, ambient files are sometimes incorrectly called "AREAXLOOP" rather than "AMBIENTXLOOP"
if (GruntzGame.resourceManager().rez().file(aniRef, "ani") == null) aniRef = aniRef.replace("AREA", "AMBIENT");
// Check that the string exists
Logic log = factory.createFromWWD(logicRef, graphicRef, aniRef, Arrays.copyOfRange(s, dataOffset, dataOffset + 284));
if (log != null) logics.add(log);
else if (!logicRef.equals("GiantRock")) System.err.println("Failed to find controller for logic " + logicRef);
}
} catch (Exception e) {
// Finish parsing errors if an exception occurs