skillDefinitions.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export default function skillDefinitions(code){
  2. let firstLine = code.split("\n")[0];
  3. let twCode = firstLine.replace("#","::")+`\n<<run console.warn("Use of skillDefinitions-passages is deprecated (${firstLine})")>>`;
  4. let tsCode = "setup.skills ??= {};\n";
  5. const attributeRegex = /\$att_name\[(\d+)\] = '(\w+)'/gd;
  6. const attributes = [];
  7. let result;
  8. while(result = attributeRegex.exec(code)) {
  9. attributes[Number(result[1])] = result[2];
  10. }
  11. const skilToAttributeEffectRegex = /(\w+)\[1\]\s*=\s*(\d)(?:\s*&\s*\1\[\d+\]\s*=\s*(\d+))*/gd;
  12. while(result = skilToAttributeEffectRegex.exec(code)) {
  13. const skillId = result[1];
  14. const arrayIndizes = result[0].split("&").map((singleCommand)=>attributes[Number(singleCommand.split("=")[1].trim())]);
  15. tsCode += `setup.skills['${skillId}'] = {attributes:${JSON.stringify(arrayIndizes)}}\n`;
  16. }
  17. tsCode += "setup.proficiencies ??= {};\n"
  18. //const proficiencyRegex = /pcs_(\w+)\s*=\s*([^&]*)/gd;
  19. const proficiencyRegex = /^\s*pcs_(\w+)\s*=\s*([^&^\n]*)([^\n]*)\s*$/gm;
  20. const proficiencyInnerRegex = /(?:pcs_(\w+))|(?:(\w+)_lvl)|([a-z]\w*)/gd;
  21. while(result = proficiencyRegex.exec(code)) {
  22. const proficiencyId = result[1];
  23. if(attributes.indexOf(proficiencyId) != -1)
  24. continue;
  25. const proficiencyCodeRaw = result[2];
  26. let proficiencyCode = proficiencyCodeRaw;
  27. let innerResult;
  28. while(innerResult = proficiencyInnerRegex.exec(proficiencyCodeRaw)) {
  29. const attributeOrSkillId = innerResult[1] ?? innerResult[2];
  30. if(attributes.indexOf(attributeOrSkillId) != -1)
  31. proficiencyCode = proficiencyCode.replace(innerResult[0],`attributes['${attributeOrSkillId}'].level`);
  32. else if(innerResult[3])
  33. proficiencyCode = proficiencyCode.replace(innerResult[3],"0");
  34. else
  35. proficiencyCode = proficiencyCode.replace(innerResult[0],`skills['${attributeOrSkillId}'].level`);
  36. }
  37. tsCode += `setup.proficiencies['${proficiencyId}'] = {calc:(attributes,skills)=>${proficiencyCode}}\n`;
  38. }
  39. return [twCode, tsCode];
  40. }