Files
lakebane/src/engine/wpak/EffectsParser.java
T

279 lines
10 KiB
Java
Raw Normal View History

2024-07-23 20:47:39 -05:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2024
// www.magicbane.com
2024-08-13 11:58:41 -04:00
package engine.wpak;
2024-07-24 12:56:24 -04:00
2024-07-23 20:47:39 -05:00
import engine.gameManager.ConfigManager;
2024-08-10 17:54:29 -04:00
import engine.mbEnums;
2024-08-21 12:35:01 -04:00
import engine.wpak.data.ConditionEntry;
2024-08-22 16:21:13 -04:00
import engine.wpak.data.Effect;
2024-08-22 16:34:05 -04:00
import engine.wpak.data.ModifierEntry;
2024-08-10 17:54:29 -04:00
import org.pmw.tinylog.Logger;
2024-07-23 21:15:37 -05:00
2024-07-23 20:47:39 -05:00
import java.io.IOException;
2024-07-24 12:56:24 -04:00
import java.nio.file.Files;
import java.nio.file.Paths;
2024-08-21 12:35:01 -04:00
import java.util.*;
2024-08-06 16:52:06 -04:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2024-07-23 20:47:39 -05:00
public class EffectsParser {
2024-07-24 12:56:24 -04:00
2024-08-13 12:20:27 -04:00
public static String effectsPath = ConfigManager.DEFAULT_DATA_DIR + "wpak/Effects.cfg";
2024-08-08 11:56:31 -04:00
private static final Pattern EFFECT_REGEX = Pattern.compile("(?<=EFFECTBEGIN)(.+?)(?=EFFECTEND)", Pattern.DOTALL);
private static final Pattern SOURCE_REGEX = Pattern.compile("(?<=SOURCEBEGIN)(.+?)(?=SOURCEEND)", Pattern.DOTALL);
2024-08-10 18:02:22 -04:00
private static final Pattern MODS_REGEX = Pattern.compile("(?<=MODSBEGIN)(.+?)(?=MODSEND)", Pattern.DOTALL);
2024-08-13 11:29:37 -04:00
private static final Pattern CONDITIONS_REGEX = Pattern.compile("(?<=CONDITIONBEGIN)(.+?)(?=CONDITIONEND)", Pattern.DOTALL);
2024-08-14 16:55:11 -04:00
private static final Pattern STRSPLIT_REGEX = Pattern.compile("([^\"]\\S*|\"[^\"]*\")\\s*"); // Regex ignores spaces within quotes
2024-08-12 08:14:52 -04:00
2024-08-19 20:38:09 -04:00
public static void parseWpakFile() {
2024-07-24 12:56:24 -04:00
2024-08-13 11:28:07 -04:00
// Read .wpak file from disk
2024-08-20 19:37:09 -04:00
byte[] fileData;
2024-08-19 20:38:09 -04:00
try {
fileData = Files.readAllBytes(Paths.get(effectsPath));
} catch (IOException e) {
throw new RuntimeException(e);
}
2024-08-20 12:06:35 -04:00
2024-08-06 16:52:06 -04:00
String fileContents = new String(fileData);
2024-07-24 12:56:24 -04:00
2024-08-13 11:28:34 -04:00
// Iterate over effect entries from .wpak data
2024-07-24 12:56:24 -04:00
2024-08-13 11:28:07 -04:00
Matcher matcher = EFFECT_REGEX.matcher(fileContents);
2024-08-06 16:52:06 -04:00
while (matcher.find()) {
2024-08-22 16:21:13 -04:00
Effect effect = parseEffectEntry(matcher.group());
2024-09-07 19:57:20 -05:00
WpakPowerManager.effect_data.put(effect.effect_id, effect);
2024-08-06 17:07:07 -04:00
}
2024-07-23 20:47:39 -05:00
}
2024-08-22 16:21:13 -04:00
private static Effect parseEffectEntry(String effectData) {
2024-08-20 19:30:33 -04:00
2024-08-22 16:21:13 -04:00
Effect effect = new Effect();
2024-08-06 17:07:07 -04:00
2024-08-21 11:45:11 -04:00
// Parse fields that lie outside the other tags
2024-08-22 16:21:13 -04:00
effect.isItemEffect = effectData.contains("IsItemEffect");
effect.isSpireEffect = effectData.contains("IsSpireEffect");
effect.ignoreNoMod = effectData.contains("IgnoreNoMod");
effect.dontSave = effectData.contains("DontSave");
2024-08-20 19:14:50 -05:00
2024-08-21 11:45:11 -04:00
// Remove all lines that contain a # and leading/trailing blank lines
2024-08-06 17:07:07 -04:00
2024-08-19 07:45:26 -04:00
effectData = effectData.replaceAll("(?m)^(\\s*#.*|\\s*)\r?\n?", "");
2024-08-08 11:38:09 -04:00
effectData = effectData.trim();
2024-08-06 16:56:41 -04:00
2024-08-12 16:05:36 -04:00
// Parse effect entry header
2024-08-06 17:17:57 -04:00
2024-08-08 12:54:58 -04:00
String firstLine;
2024-08-12 16:05:36 -04:00
ArrayList<String> effectHeader = new ArrayList<>();
2024-08-08 12:28:35 -04:00
2024-08-08 12:57:03 -04:00
// Some effects exist without sources/mods or conditions
// (ACID "MOB" 0)
2024-08-08 13:14:21 -04:00
2024-08-08 12:54:58 -04:00
if (effectData.indexOf('\n') > 0)
2024-08-08 12:28:35 -04:00
firstLine = effectData.substring(0, effectData.indexOf('\n'));
2024-08-08 12:54:58 -04:00
else
firstLine = effectData;
2024-08-08 12:28:35 -04:00
2024-08-08 12:19:53 -04:00
Matcher matcher = STRSPLIT_REGEX.matcher(firstLine);
while (matcher.find())
2024-08-14 16:46:02 -04:00
effectHeader.add(matcher.group().trim());
2024-08-06 17:17:57 -04:00
2024-08-22 16:21:13 -04:00
effect.effect_id = effectHeader.get(0);
effect.effect_name = effectHeader.get(1);
effect.effect_name = effect.effect_name.replaceAll("\"", "");
2024-08-06 17:17:57 -04:00
2024-08-08 13:14:07 -04:00
// Some effect mods have no icon
// (SEEINVIS-SHADE "See Invis")
2024-08-12 16:05:36 -04:00
if (effectHeader.size() == 3)
2024-08-22 16:21:13 -04:00
effect.icon = Integer.parseInt(effectHeader.get(2));
2024-08-08 13:14:07 -04:00
else
2024-08-22 16:21:13 -04:00
effect.icon = 0;
2024-08-08 13:14:07 -04:00
2024-08-06 17:17:57 -04:00
// Parse source entries
2024-08-08 12:19:53 -04:00
matcher = SOURCE_REGEX.matcher(effectData);
2024-08-06 17:17:57 -04:00
while (matcher.find())
2024-08-22 16:21:13 -04:00
effect.sources.add(matcher.group().trim());
2024-08-06 17:17:57 -04:00
2024-08-06 17:18:46 -04:00
// Parse modifier entries
2024-08-06 17:17:57 -04:00
2024-08-10 16:29:55 -04:00
matcher = MODS_REGEX.matcher(effectData);
// Iterate effect entries from .wpak config data
while (matcher.find()) {
2024-08-22 16:34:05 -04:00
ModifierEntry modifierEntry = parseModEntry(matcher.group());
effect.mods.add(modifierEntry);
2024-08-10 16:29:55 -04:00
}
2024-08-12 11:01:27 -04:00
// Parse Conditions
matcher = CONDITIONS_REGEX.matcher(effectData);
while (matcher.find()) {
2024-08-21 13:06:35 -04:00
String[] conditions = matcher.group().trim().split("\n");
2024-08-12 11:10:52 -04:00
for (String condition : conditions) {
2024-08-21 12:39:45 -04:00
List<String> parameters = Arrays.asList(condition.trim().split("\\s+"));
2024-08-21 12:35:01 -04:00
Iterator<String> iterator = parameters.iterator();
ConditionEntry conditionEntry = new ConditionEntry();
conditionEntry.condition = iterator.next();
conditionEntry.arg = Integer.parseInt(iterator.next());
if (iterator.hasNext())
conditionEntry.curveType = mbEnums.CompoundCurveType.valueOf(iterator.next());
while (iterator.hasNext())
2024-08-21 12:43:14 -04:00
conditionEntry.damageTypes.add(mbEnums.DamageType.valueOf(iterator.next().toUpperCase()));
2024-08-21 12:35:01 -04:00
2024-08-22 16:21:13 -04:00
effect.conditions.add(conditionEntry);
2024-08-12 11:10:52 -04:00
}
2024-08-12 11:01:27 -04:00
}
2024-08-22 16:21:13 -04:00
return effect;
2024-08-06 16:56:41 -04:00
}
2024-08-10 16:29:55 -04:00
2024-08-22 16:34:05 -04:00
private static ModifierEntry parseModEntry(String modData) {
2024-08-10 16:29:55 -04:00
2024-08-22 16:34:05 -04:00
ModifierEntry modifierEntry = new ModifierEntry();
2024-08-10 16:29:55 -04:00
2024-08-10 18:07:54 -04:00
String[] modEntries = modData.trim().split("\n");
2024-08-10 17:54:29 -04:00
2024-08-10 18:13:15 -04:00
for (String modEntry : modEntries) {
2024-08-10 17:54:29 -04:00
2024-08-10 18:13:15 -04:00
ArrayList<String> modValues = new ArrayList<>();
2024-08-10 18:45:23 -04:00
Matcher matcher = STRSPLIT_REGEX.matcher(modEntry.trim());
2024-08-10 18:13:15 -04:00
while (matcher.find())
2024-08-14 16:46:02 -04:00
modValues.add(matcher.group().trim());
2024-08-10 18:13:15 -04:00
2024-08-22 16:34:05 -04:00
modifierEntry.type = mbEnums.ModType.valueOf(modValues.get(0).trim());
2024-08-10 18:13:15 -04:00
2024-08-22 16:34:05 -04:00
switch (modifierEntry.type) {
2024-08-10 18:13:15 -04:00
case AnimOverride:
2024-08-22 16:34:05 -04:00
modifierEntry.min = Float.parseFloat(modValues.get(1).trim());
modifierEntry.max = Float.parseFloat(modValues.get(2).trim());
2024-08-10 18:13:15 -04:00
break;
2024-08-12 09:00:59 -04:00
case Health:
2024-08-12 09:09:44 -04:00
case Mana:
case Stamina:
2024-08-22 16:34:05 -04:00
modifierEntry.min = Float.parseFloat(modValues.get(1).trim());
modifierEntry.max = Float.parseFloat(modValues.get(2).trim());
modifierEntry.value = Float.parseFloat(modValues.get(3).trim());
2024-08-12 09:01:49 -04:00
// Parameter 4 is always 0.
2024-08-22 16:34:05 -04:00
modifierEntry.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(5).trim());
modifierEntry.arg1 = modValues.get(6).trim();
2024-08-12 09:39:01 -04:00
break;
2024-08-12 08:45:42 -04:00
case Attr:
2024-08-12 08:52:24 -04:00
case Resistance:
case Skill:
2024-08-12 09:25:17 -04:00
case HealthRecoverRate:
case ManaRecoverRate:
case StaminaRecoverRate:
2024-08-12 09:32:58 -04:00
case DamageShield:
case HealthFull:
2024-08-12 09:57:19 -04:00
case ManaFull:
case StaminaFull:
2024-08-12 10:28:37 -04:00
case Slay:
2024-08-12 10:38:23 -04:00
case Fade:
2024-08-12 10:41:11 -04:00
case Durability:
2024-08-22 16:34:05 -04:00
modifierEntry.min = Float.parseFloat(modValues.get(1).trim());
modifierEntry.max = Float.parseFloat(modValues.get(2).trim());
modifierEntry.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(3).trim());
2024-08-12 10:36:55 -04:00
if (modValues.size() > 4)
2024-08-22 16:34:05 -04:00
modifierEntry.arg1 = modValues.get(4).trim(); // Some HeathFull entries do not have an argument
2024-08-12 08:47:57 -04:00
break;
2024-08-10 18:23:40 -04:00
case MeleeDamageModifier:
2024-08-10 18:27:17 -04:00
case OCV:
case DCV:
2024-08-11 12:44:54 -04:00
case AttackDelay:
case AdjustAboveDmgCap:
case DamageCap:
case ArmorPiercing:
2024-08-12 09:13:29 -04:00
case Speed:
2024-08-12 09:25:17 -04:00
case PowerDamageModifier:
2024-08-12 09:40:43 -04:00
case DR:
2024-08-12 09:45:26 -04:00
case PassiveDefense:
2024-08-12 09:59:17 -04:00
case MaxDamage:
case Value:
2024-08-12 10:02:24 -04:00
case WeaponSpeed:
2024-08-12 10:12:28 -04:00
case MinDamage:
2024-08-12 10:15:04 -04:00
case PowerCost:
2024-08-12 10:20:48 -04:00
case Block:
2024-08-12 10:23:56 -04:00
case Parry:
case Dodge:
2024-08-22 15:52:28 -04:00
case WeaponRange:
2024-08-12 10:21:37 -04:00
case ScanRange:
2024-08-12 10:43:45 -04:00
case ScaleHeight:
2024-08-12 10:45:19 -04:00
case ScaleWidth:
2024-08-22 16:34:05 -04:00
modifierEntry.min = Float.parseFloat(modValues.get(1).trim());
modifierEntry.max = Float.parseFloat(modValues.get(2).trim());
modifierEntry.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(3).trim());
2024-08-10 18:50:28 -04:00
break;
2024-08-11 12:48:56 -04:00
case ItemName:
2024-08-12 09:16:17 -04:00
case BlockedPowerType:
2024-08-12 09:32:58 -04:00
case ImmuneTo:
case BlackMantle:
2024-08-22 16:34:05 -04:00
modifierEntry.arg1 = modValues.get(1).trim();
2024-08-12 10:09:54 -04:00
// Some BlockedPowerType entries have only one argument
if (modValues.size() > 2)
2024-08-22 16:34:05 -04:00
modifierEntry.arg2 = modValues.get(2).trim();
2024-08-11 12:48:56 -04:00
break;
2024-08-12 09:16:17 -04:00
case NoMod:
2024-08-12 09:32:58 -04:00
case ConstrainedAmbidexterity:
2024-08-12 10:05:49 -04:00
case ProtectionFrom:
2024-08-12 10:19:20 -04:00
case ExclusiveDamageCap:
2024-08-12 10:28:37 -04:00
case IgnoreDamageCap:
2024-08-22 16:34:05 -04:00
modifierEntry.arg1 = modValues.get(1).trim();
2024-08-12 09:16:17 -04:00
break;
2024-08-12 10:02:24 -04:00
case WeaponProc:
2024-08-22 16:34:05 -04:00
modifierEntry.min = Float.parseFloat(modValues.get(1).trim());
modifierEntry.arg1 = modValues.get(2).trim();
modifierEntry.max = Float.parseFloat(modValues.get(3).trim());
2024-08-12 10:02:24 -04:00
break;
2024-08-22 16:06:56 -04:00
case BladeTrails: // These tags have no parms or are not parsed
case ImmuneToAttack:
case ImmuneToPowers:
case Ambidexterity:
case Silenced:
case IgnorePassiveDefense:
case Stunned:
case PowerCostHealth:
case Charmed:
case Fly:
case CannotMove:
case CannotTrack:
case CannotAttack:
case CannotCast:
case SpireBlock:
case Invisible:
case SeeInvisible:
break;
2024-08-10 18:13:15 -04:00
default:
2024-08-22 16:34:05 -04:00
Logger.error("Unhandled type: " + modifierEntry.type);
2024-08-10 18:13:15 -04:00
break;
}
2024-08-10 17:54:29 -04:00
}
2024-08-10 18:13:15 -04:00
2024-08-22 16:34:05 -04:00
return modifierEntry;
2024-08-10 16:29:55 -04:00
}
2024-08-06 16:52:06 -04:00
}