Files
magicbane/src/engine/parsers/wpak/CompoundCurveParser.java
T

101 lines
3.5 KiB
Java
Raw Normal View History

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2026
// https://www.magicbane.com
package engine.parsers.wpak;
import engine.parsers.objects.wpak.CompoundCurveTemplate;
import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.HashMap;
// Loads CompoundCurves.cfg.
public class CompoundCurveParser {
2026-10-22 11:38:07 -04:00
private static final String fileName = "Config/CompoundCurves.cfg";
// Segments run back to back on one line, so the param count per simple curve
// is what tells where the next segment starts.
private static final HashMap<String, Integer> paramCounts = new HashMap<>();
static {
paramCounts.put("FlatLine", 0);
paramCounts.put("SlopedLine", 1);
paramCounts.put("SlopedInitValLine", 1);
}
public static HashMap<String, CompoundCurveTemplate> parseCompoundCurvesFile() {
HashMap<String, CompoundCurveTemplate> curves = new HashMap<>();
ArrayList<WpakLine> lines;
CompoundCurveTemplate curve;
int failedCount = 0;
try {
lines = WpakFileReader.readLines(fileName);
} catch (Exception e) {
Logger.error(fileName + ": " + e.getMessage());
return null;
}
for (WpakLine line : lines) {
try {
curve = parseCurveLine(line);
if (curves.containsKey(curve.name))
throw line.error("duplicate curve " + curve.name);
curves.put(curve.name, curve);
} catch (WpakFormatException e) {
Logger.error(e.getMessage());
failedCount++;
}
}
Logger.info("Loaded " + curves.size() + " of " + lines.size() + " compound curves");
if (failedCount > 0)
return null;
return curves;
}
private static CompoundCurveTemplate parseCurveLine(WpakLine line) {
CompoundCurveTemplate curve = new CompoundCurveTemplate();
CompoundCurveTemplate.Segment segment;
Integer paramCount;
int index = 1;
curve.name = line.token(0);
while (line.has(index)) {
segment = new CompoundCurveTemplate.Segment();
segment.down_per_range = line.intToken(index);
segment.simple_curve = line.token(index + 1);
paramCount = paramCounts.get(segment.simple_curve);
if (paramCount == null)
throw line.error("unknown simple curve " + segment.simple_curve);
segment.params = new float[paramCount];
for (int i = 0; i < paramCount; i++)
segment.params[i] = line.floatToken(index + 2 + i);
curve.segments.add(segment);
index += 2 + paramCount;
}
if (curve.segments.isEmpty())
throw line.error("curve " + curve.name + " has no segments");
return curve;
}
}