• Please note: In an effort to ensure that all of our users feel welcome on our forums, we’ve updated our forum rules. You can review the updated rules here: http://forums.wolflair.com/showthread.php?t=5528.

    If a fellow Community member is not following the forum rules, please report the post by clicking the Report button (the red yield sign on the left) located on every post. This will notify the moderators directly. If you have any questions about these new rules, please contact support@wolflair.com.

    - The Lone Wolf Development Team

Parsing a collection of things

frumple

Well-known member
I have a template that hides all of the base creature's natural attacks and replaces it with a single new attack. If the original attacks had extra damage, the new attack gets all of that extra damage as well.

How can I make sure I do not add an string of extra damage to the new attack that it already has (so I don't get something like ' plus burn plus burn plus stun plus 1d6 fire').

Since I cannot make an array to contain all the extra damages, I am making a long string of all the damages with a separator (and it strips ' plus ' or 'plus ').

So in the above example my master string is: burn%burn%stun%1d6 fire

I want to collapse this to just: burn%stun%1d6 fire
and then parse the list in standard form -> burn, stun and 1d6 fire

Ideas?
 
Carve off the first section of the string (until the first divider) into a temporary variable, then compare that temporary variable to the remaining text in the original string.

If there is a match, delete the contents of the temporary variable, and carve off the next section.

If there is no match, then place the contents of the temporary variable in a final string variable, delete its contents and carve off the next section.
 
Here is the code I developed to parse the list.

Code:
~ extra damage for swarm attack based on removed attacks
~ in another script we add a custom tag to all attacks we removed
~ first we put all the extra damages into one long string with % as separator
~ then we grab the first piece of the string and compare

~ run this at Validation/90000 to make sure all extra damages have been applied

~
~ BUILD MASTER STRING
~
var master as string
var cur as string

foreach pick in hero from BaseNatWep where "Custom.TagAttk"
  cur = eachpick.field[wDamExtra].text
  if (empty(cur) = 0) then
    master &= cur & "%"
  endif
 
nexteach

~ now strip potential leading characters for each extra damage
master = replace(master," plus ","",0)
master = replace(master,"plus ","",0)
master = replace(master," +","",0)
master = replace(master," -","",0)
master = replace(master," and ","",0)

~ we now have our master string in the variable master
~ debug master

~
~ STRIP DUPLICATES
~

~ set up while loop for stripping duplicates
var cur_pos as number
var srch_str as string
var cur_str as string
var build_str as string

~ move master -> cur_str  for debugging purposes
~ debug master
cur_str = master
cur_pos = pos(master,"%")

while (cur_pos <> -1)
  ~ strip any leading separators
  while (cur_pos = 0)
    cur_str = mid(cur_str,1,length(cur_str))
    cur_pos = pos(cur_str,"%")
  loop

  ~ cur_pos now has the position of the separator
  ~ since pos is 0 based, cur_pos is the length of  
  ~ the search string we want
  ~ build up new string from srch_str
  srch_str = mid(cur_str,0,cur_pos)
  cur_str = replace(cur_str,srch_str,"",0)
  build_str &= srch_str & "%"

  ~ find position of out next separator for next time around the loop
  ~ when we cannot find any separators any more we exit the loop
   cur_pos = pos(cur_str,"%")
loop

~ remove trailing separators
var sep_pos as number
var len as number
sep_pos = lastpos(build_str,"%")
len = length(build_str) - 1

while (sep_pos = len)

if (sep_pos >= 0) then
  build_str = mid(build_str,0,sep_pos - 1)
endif

 len = length(build_str)
 sep_pos = lastpos(build_str,"%")
 
loop

~ get out if we have an empty string
doneif (empty(build_str) <> 0)

~
~ MAKE NEW EXTRA DAMAGE STRING
~

~ now build up new extra damage string
~ dam_str will hold the final result
var dam_str as string
dam_str = build_str

~ we determine if we insert a comma or 'and' by using
~ lastpos and pos. If lastpos = pos we put in an 'and'
~ otherwise we put in a comma

var lst as number
var fst as number
var tst as number

lst = lastpos(dam_str,"%")
fst = pos(dam_str,"%")


while (fst > 0)
  tst = lst - fst

  if (tst <> 0) then
    dam_str = replace(dam_str,"%",", ",1)
  else
    dam_str = replace(dam_str,"%"," and ",1)
  endif
  
  lst = lastpos(dam_str,"%")
  fst = pos(dam_str,"%")
  
loop

~ add leading plus for extra damage string
dam_str = " plus " & dam_str
 
Last edited:
Yep Aaron. I tested it and it works.

I am not sure if it can handle all cases since any text can be in wDamExtra, but I think it can handle most cases.

I also wanted to do a sort on the various pieces, but without having access to an array to put the various pieces in that is very hard to do. I know that array and matrix fields are hard coded into the game system, but I wish we could define an array and/or matrix variable. It would make text manipulation so much easier as well as giving us the ability to make look-up tables for the more complex templates, monsters, etc.
 
Back
Top