Keeping variables through multiple lines

Post Reply
OGslinkyT
Posts: 15
Joined: Thu Sep 06, 2018 5:26 am

Keeping variables through multiple lines

Post by OGslinkyT »

This is the code

Code: Select all

* R HP:Wounded MV:Full > 
where
Players in your Zone
--------------------
Jimmy                - Path Past a Small Village
Charles              - A Small Path
Michael              - A Small Path
Ben                  - A Small Path
Jill                 - Plains of Maredo

* R HP:Wounded MV:Full >
this is what I want to do

Code: Select all

* R HP:Wounded MV:Full > 
where
Players in your Zone
--------------------
Jimmy                - Path Past a Small Village 1
Charles              - A Small Path 2
Michael              - A Small Path 3
Ben                  - A Small Path 4
Jill                 - Plains of Maredo 5

* R HP:Wounded MV:Full >
I want to be able to access the variables in each line by using an alias associated with the line number. There is an unknown number of players in the zone so the trigger needs to keep going until it the next line reaches the "HP:" the status bar (first thing in bar that is constant).

Starting is easy enough just use the first two lines "Players in your Zone" and "--------------------" make a multiline/and trigger set it to two lines and click the fire length box and change it to one. Then make a child trigger to the first trigger and use "(.+) - (.+)".

Now I have captured the Line "Jimmy - Path Past a Small Village" with Jimmy being matches[2] and Path Past a Small Village being matches [3]
how do I get to the next line and keep it from overwriting my first set of variables. Also how do I stop the trigger because when I am fighting my status bar looks like this "* R HP:Healthy MV:Strong - a rat: Beaten > " and this would set off the trigger

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Keeping variables through multiple lines

Post by SoulSpirit »

I'd use 3 triggers to handle this:
1st. the initiator: could be the multiline trigger you said to capture the beginning of the "table". It should set a global flag (like "playersTrigger=true")
2nd. matches evey line of your table (something like ^(\S)\s+[-] (.+)$ ) and contains the logic you want. Your logic should be encapsulated in "if playersTrigger then" that checks that the first trigger has fired
3rd. the terminator: matches the end of the table (the blank line seems a good candidate: ^$ ) and resets playersTrigger to false

OGslinkyT
Posts: 15
Joined: Thu Sep 06, 2018 5:26 am

Re: Keeping variables through multiple lines

Post by OGslinkyT »

I ended up talking jor'mox in discord and the key part I wasn't understanding was how tables worked. I had already found this code in the mapper script i use and thought I could use its method

Code: Select all

---if current MUD line contains roomname, let trigger chain fire another line
if line == wotmudmapper.roomname or string.find(line, "HP:") then
  setTriggerStayOpen("Catch Name", 1)
else
  ---if not exit line, add line to room desc, let trigger chain fire another line
  if not string.find(line, "obvious exits:") then
    if wotmudmapper.brief then
      wotmudmapper.brief = false
    end
    wotmudmapper.roomdesc = wotmudmapper.roomdesc .. "\n" .. line
    setTriggerStayOpen("Catch Name", 1)
		 end
end
After jor'mox explaining to me how tables worked I used this as my parent trigger

Code: Select all

Players in your Zone
-------------------- 
--(these are the trigger lines and they are both set to substring)
--(checked mutltline/and trigger box and set value to 2)
--(checked fire length box and set value to 1)
zonewhere {}
--(this creates an empty table and resets the table value to empty every time the parent trigger condition is met)
Now I used (.+) - (.+) as my trigger for my child trigger changed to pearl regx and left all boxes unchecked and used this code based off of the mapper script code (naming the trigger Zone Where of course)

Code: Select all

local name = matches[2]
local room = matches[3]
table.insert (zonewhere,{name,room})
if string.find (line, "Players in your Zone") or string.find (line, "--------------------")  then
	setTriggerStayOpen("Zone Where", 1) else
			if not string.find ("HP:") then
	 setTriggerStayOpen("Zone Where", 1)
	end
end
cecho (" <green>"..table.size(zonewhere))
I used table.size to get my number in after looking at different options from the wiki. My original choice was table.index_of but for some reason I couldn't get this to echo or print. After failing with table.index_of I just used table.size to make sure that the original trigger did in fact clear my table. I realized of course since it did clear the table the table size grows by 1 each additional line and got me the result I wanted. :lol:

Part of the mapper script i use has a find room alias that looks like this

Code: Select all

local roomname = matches[2]
local foundrooms = searchRoom(roomname, true, true)
if table.is_empty(foundrooms) then
  cecho("<red>No matches found!\n")
elseif table.size(foundrooms) == 1 then
  cecho("<green>One matching room found! Map has been centered on this room.\n")
  for k, v in pairs(foundrooms) do
    centerview(k)
  end
else
  cecho("<red>Multiple matches found!\n")
end


All I had to do was change "local roomname = matches[2]" to "local roomname = (zonewhere[2][2])" {this example finds the second room}
I made the proper aliases (i.e. fr1, fr2, fr3, .......) and it worked beautifully.

OGslinkyT
Posts: 15
Joined: Thu Sep 06, 2018 5:26 am

Re: Keeping variables through multiple lines

Post by OGslinkyT »

SoulSpirit wrote:
Fri Sep 07, 2018 7:07 am
I'd use 3 triggers to handle this:
1st. the initiator: could be the multiline trigger you said to capture the beginning of the "table". It should set a global flag (like "playersTrigger=true")
2nd. matches evey line of your table (something like ^(\S)\s+[-] (.+)$ ) and contains the logic you want. Your logic should be encapsulated in "if playersTrigger then" that checks that the first trigger has fired
3rd. the terminator: matches the end of the table (the blank line seems a good candidate: ^$ ) and resets playersTrigger to false
What is the difference between (\S) and (.+)?
Does your method get rid of the extra spaces?
What does \s+ do after the (\S)?

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Keeping variables through multiple lines

Post by Jor'Mox »

There are several predefined character groups in regex, such as \w, \d, and \s (they are all letters, numbers and _, all numbers, and all white space, respectively). But if you instead use the upper case version of the letter, you get everything NOT in that group. So \W is everything that isn't a letter, number or underscore, \D is everything that isn't a number, and \S is everything that isn't a whitespace character.

Post Reply