Josh
Josh

Reputation: 3265

Lua: String.match/string.gsub - Casing for true/false

I've been trying to figure this out for a while, but I fear I'm not seeing the entire solution quickly, and now I'm needing a fresh set of eyes to accomplish what I need.

I have a very particular script for the MUD I play to help me differentiate between MOBs and players when in a room. The script itself works, but now I want to add a new element that will check if my group mates are in the same room. This is what I have so far:

function strends(s)
  if s:match("%u%w+ is here%.") or s:match("%u%w+ is fighting .-%.") or s:match("%u%w+ is sleeping here%.") then
    return true
  else
    return false
  end
end

That's working great - it checks if an upper case name is in the room and returns information as requested.

I have a table of my group mates, though I may find it easier to do it as a string and do string.find. The problem I'm running into is casing it for each of the scenarios:

  1. Return true if there are players outside my group in the room.
  2. Return true if it's only players outside my group.
  3. Return false if there is no one in the room aside from myself.

In scenario one, it MUST return true, even if there are people in my group as well as people outside my group. But my Lua knowledge isn't expansive enough that I can work out the problem. The reason for the non-beginning string.matches is because it's possible for the particular line to have xx amount of characters before it. How should I approach this, or what should I be doing in order to accomplish my goal?

Edit: Fixed scenario 2, because there was an error. Here is what typically comes from looking in a room:

Scenario 1:

(R) NonGroupMate is sleeping here.
(W)(R)(T) Groupmate is here.

Expected result?

return true -- the function checks if there are people outside my group in the room.

Scenario 2:

(D)(W) NonGroupMate is fighting a mob!
(T)(W) NonGroupMateTwo is here.

Expected result?

return true

Scenario 3:

(T) GroupMate is here.
(W) GroupMateTwo is sleeping here.
GroupMateThree is fighting a mob!

Expected result?

return false

I hope this helps clarify things a bit. If you need more, please, let me know.

More clarification

I didn't mean for any miscommunication. I thought all the above was what was needed, but I'll attempt to break it down.

When I type "look" in the MUD, I see something similar to the following:

Room name
  Room Desc
  [ Exits: <exits> ]
  NameOne is here.
  (G) NameTwo is here.
  (R)(W) NameThree is sleeping here.
  (W) NameFour is fighting a mob.

I already have triggers to match on items directly after [ Exits: ], and the trigger fires without issue. It evaluates each line, as it's supposed to do, and runs it through the strends() function.

The issue I'm trying to solve is creating a function where it evaluates the line and determines if the player's name is in my group or not. If they ARE in my group, strends() should return false (as strends() returns whether there are players I should avoid in the room). If they are NOT in my group, then it needs to return true.

That is what I'm having a hard time understanding; how do I create the function for that? What I would really like the function to do is once it determines a player not in the group is in the room, is have it break and ignore all other outcomes. I think, for that to happen, though, I'd have to input them into a table and iterate through so I could break at any given point. What I /may/ wind up doing is setting up a conditional variable which sets to false at the start, and when set to true always remains true.

Is this clearer now? If not, I'm not sure what else I can add to it.

Update

The output from look isn't a single string. Essentially, what happens is each line gets processed individually, starting with characters in the room. For instance:

NameOne is here. -- This is processed by itself, gets passed through strends.
NameTwo is sleeping here. -- This is also processed through strends, by itself.
NameThree is here. -- Yet again it's processed individually through strends.

I could try to make it all into a table, but the code for the triggering will eventually get messy.

Upvotes: 3

Views: 696

Answers (1)

ryanpattison
ryanpattison

Reputation: 6251

In the following code I assume the output from "look" is a single string called look_out.

look_out = [[
Room name
  Room Desc
  [ Exits: <exits> ]
  NameOne is here.
  (G) NameTwo is here.
  (R)(W) NameThree is sleeping here.
  (W) NameFour is fighting a mob.
]]

group_member = {NameOne=true, NameTwo=true}

function has_non_group(look_out, group_member)
  for name in look_out:gmatch("(%u%w+) is ") do
    if not group_member[name] then 
       return true
    end
  end
  return false
end

print(has_non_group(look_out, group_member)) --  true

group_member.NameFour = true
group_member.NameThree = true
print(has_non_group(look_out, group_member)) --  false

I assume names start with a capital and are followed by "is". The easiest way to exclude group members is to create a set, group_member. If there is at least 1 non-group member then it will return true and false otherwise.

Upvotes: 1

Related Questions