PDA

View Full Version : String patterns (find/replace dice string values)



pr6i6e6st
July 15th, 2020, 19:17
hey guys! i've been working on something for 5e here for the past couple of weeks that is really coming together, and i believe the last thing i need is to be able to replace some things.

so i have a window. the window has 6 windowlists (traits, actions, reactions, legendary actions, lair actions, innate spells).

i have, through trial and error, found how to find and replace the attack bonus of an action that is built up in the action windowlist when i drop an npc on the window. problem is that it does this for the damage as well.

i ultimately want to try and achieve at least 3 things here, but just need a to understand what the pattern i need is and why that pattern works as it does.
1 - replace the attack bonus
2 - replace the damage bonus
3 - replace the DC stated in the text.

this is my parseComponents() function, largely taken from the npc_skills.lua from 5e. the "actionSource" is the "Action" list windowinstance that is created and is being edited by this function. the parseComponents() function is currently called upon by an onDrop() function that creates the windowinstance of the action and then calls on the parseComponents() function to edit after.


function parseComponents(actionSource)
aComponents = {};
if DB.getValue(getDatabaseNode(), "dexbasedattacks", 0) == 0 then
AttackBonus = tostring(proficiency.getValue() + DB.getValue(getDatabaseNode(), "abilities.strength.bonus", 0));

else
AttackBonus = tostring(proficiency.getValue() + DB.getValue(getDatabaseNode(), "abilities.dexterity.bonus", 0));
end
-- Get the comma-separated strings
local aClauses, aClauseStats = StringManager.split(DB.getValue(actionSource, "desc", ""), ",;\r", true);

Debug.console("parseComponents(), aClauses, aClauseStats", aClauses, aClauseStats);
-- Check each comma-separated string for a potential skill roll or auto-complete opportunity
for i = 1, #aClauses do
local nStarts, nEnds, sLabel, sSign, sMod = string.find(aClauses[i], "([%w%s\(\)]*[%w\(\)]+:)%s*([%+%-–]?)(%d*)");
Debug.console("parseComponents(), nStarts, nEnds, sLabel, sSign, sMod", nStarts, nEnds, sLabel, sSign, sMod);
if nStarts then
-- Calculate modifier based on mod value and sign value, if any
local nAllowRoll = 0;
local nMod = 0;
if sMod ~= "" then

local ReCheck = StringManager.multireplace(DB.getValue(actionSourc e, "desc", ""), sMod, AttackBonus);
DB.setValue(actionSource, "desc", "string", ReCheck);
Debug.console("ReCheck", ReCheck);
nAllowRoll = 1;
nMod = tonumber(sMod) or 0;
if sSign == "-" or sSign == "–" then
nMod = 0 - nMod;
end
end

-- Insert the possible skill into the skill list
table.insert(aComponents, {nStart = aClauseStats[i].startpos, nLabelEnd = aClauseStats[i].startpos + nEnds, nEnd = aClauseStats[i].endpos, sLabel = sLabel, nMod = nMod, nAllowRoll = nAllowRoll });
end
end

bParsed = true;
end


thanks in advance for any explanations, elaboratons, assistance or suggestions. You're always a magnificent help.

pr6i6e6st
July 15th, 2020, 20:30
seems like i might be able to do this if for the "if sMod ~= "" then" section i replace it with


if sMod ~= "" then
if sLabel == "Melee Weapon Attack:" then
local ReCheck = StringManager.multireplace(aClauses[i], sMod, AttackBonus);
local ReCheck2 = StringManager.multireplace(DB.getValue(actionSourc e, "desc", ""), aClauses[i], ReCheck);
DB.setValue(actionSource, "desc", "string", ReCheck2);
Debug.console("ReCheck, ReCheck2, aClauses[i]", ReCheck, ReCheck2, aClauses[i]);
end
end


but the ReCheck2 is coming back unchanged. can i not use multireplace for a whole string like that? or am i doing something wrong there?

gamerhawaii
July 15th, 2020, 20:57
One thing to note is that based on your regex expression, sLabel will be equal to " Melee Weapon Attack:" (that is, with a leading space), so you might want to change to just look for "Attack:" is contained in the string

pr6i6e6st
July 15th, 2020, 21:56
One thing to note is that based on your regex expression, sLabel will be equal to " Melee Weapon Attack:" (that is, with a leading space), so you might want to change to just look for "Attack:" is contained in the string

I didn’t appear to have that issue, though it may be useful for accommodating the different types of attacks after. What is the expression I’m looking for to find a diceroll expression?

And will the miltireplace function only work on single characters? I can’t seem to get it to replace the entire component.

pr6i6e6st
July 15th, 2020, 22:46
replacing the ReCheck2 line with the following yeilds slightly better results. that's a start, but i'm worried of it interfering with other bonus's that might appear in the action text?


local ReCheck2 = StringManager.multireplace(DB.getValue(actionSourc e, "desc", ""), sSign .. sMod, sSign .. AttackBonus);

pr6i6e6st
July 16th, 2020, 07:14
so this following line is pulling out "Hit:", "9", "(1d10 + 4", "nil" from a string component that reads "Hit: 9 (1d10 + 4) bludgeoning damage"




local nDamStarts, nDamEnds, sDamLabel, sDamAverage, sDieRoll, sDamBonus = string.find(aClauses[i], "([%w%s\(\)]*[%w\(\)]+:)%s*(%d*)%s*([(%d*)]+[d][(%d*)]+%s?[%+%-–]?%s[%d*)]?)");


this is progress to me, and something i could potentially work with if i have to, but i'm wondering if someone can show me how to get the last parenthases in the sDieRoll, and how i can set up the modifier bonus to be the sDamBonus?

Moon Wizard
July 16th, 2020, 07:16
Sometimes I find that breaking down the expression matching into steps can work better than doing a monster regular expression. Of course, sometimes it's just as much work; but it helps me to work through the problem easier.

Regards,
JPG

pr6i6e6st
July 16th, 2020, 17:39
Sometimes I find that breaking down the expression matching into steps can work better than doing a monster regular expression. Of course, sometimes it's just as much work; but it helps me to work through the problem easier.

Regards,
JPG

that helped, thank you. i managed to break it all down and now have all the information i need i think. i get a return of a dice expression without the brackets, i get the bonus if there is one and the sign that comes with it, they're separated. now to replace numbers and rebuild the string. :D