Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Message
| The only problem with the above technique is, you probably have more than one attack type that maps to a single word. That means you will eventually get something messy like this:
["cleave"] = "(slash)",
["slice"] = "(slash)",
["cut"] = "(slash)",
["flaming bite"] = "(fire)",
["pummel"] = "(pound)",
Here the word "(slash)" appears multiple times. What might be nicer is to group the different types of attack together. The script below, whilst lengthier, does that.
damage_fixup_init =
{ -- start of table
slice = {
"miss", "barely scratch", "scratch", "nick", "cut", "hit", "tear",
"rip", "gash", "lacerate", "hack", "maul", "rend", "decimate",
}, -- end of slice
pound = {
"miss", "barely scuff", "scuff", "pelt", "bruise", "strike", "thrash",
"batter", "flog", "pummel", "smash", "maul", "bludgeon", "decimate",
}, -- end of pound
hit = {
"misses", "brushes", "scratches", "grazes", "nicks", "jolts", "wounds",
"injures", "hits", "jars", "thrashes", "mauls", "decimates", "_traumatizes_",
}, -- end of hit
} -- end of table
-- convert table into one where the word we are looking for is the key
damage_fixup = {}
for k, v in pairs (damage_fixup_init) do
t = {}
for _, msg in ipairs (v) do
t [msg] = true
end -- inner loop
damage_fixup [k] = t
end -- for
function mytrigger (name, line, wildcards, styles)
-- look up attack type
for k, v in pairs (damage_fixup) do
if v [styles [2].text] then
styles [2].text = styles [2].text .. " (" .. k .. ")"
break
end -- if
end -- for
for k, v in ipairs (styles) do
ColourTell ( RGBColourToName (v.textcolour),
RGBColourToName (v.backcolour),
v.text)
end -- for
Note "" -- force a new line
end -- function
The first part is a table of tables (nested tables are a great part of the Lua language!). The outer table is the different types of attack (slice, hit, pound etc.) and for each one we have an inner table which is all the words that match that attack type.
Now the only problem here is that this syntax stores the words as the value of the table, not the key.
If I use tprint to show that table you will see what I mean:
/tprint (damage_fixup_init)
slice:
1=miss
2=barely scratch
3=scratch
4=nick
5=cut
6=hit
7=tear
8=rip
9=gash
10=lacerate
11=hack
12=maul
13=rend
14=decimate
hit:
1=misses
2=brushes
3=scratches
4=grazes
5=nicks
6=jolts
7=wounds
8=injures
9=hits
10=jars
11=thrashes
12=mauls
13=decimates
14=_traumatizes_
pound:
1=miss
2=barely scuff
3=scuff
4=pelt
5=bruise
6=strike
7=thrash
8=batter
9=flog
10=pummel
11=smash
12=maul
13=bludgeon
14=decimate
What we really want is the words like "slice" to be the key. So, as a once-off action we make a second table, that rearranges the tables, so that the keyword is the key. This code is outside a function, so it is run when the script first initialises. After that is run we have this:
/tprint (damage_fixup)
slice:
tear=true
miss=true
lacerate=true
rend=true
decimate=true
cut=true
gash=true
hack=true
scratch=true
barely scratch=true
maul=true
rip=true
nick=true
hit=true
pound:
strike=true
bruise=true
bludgeon=true
decimate=true
maul=true
flog=true
smash=true
scuff=true
barely scuff=true
pummel=true
thrash=true
pelt=true
miss=true
batter=true
hit:
thrashes=true
_traumatizes_=true
decimates=true
jars=true
brushes=true
jolts=true
misses=true
injures=true
scratches=true
nicks=true
wounds=true
mauls=true
grazes=true
hits=true
Now you can see that a word like "hits" is the key of the "hit" table, with its value simply being true.
Now all we have to do is process each table, and for each one look up to see if the word we have is in it, and if so, use the key of the outer table as the action word.
This is what the loop in mytrigger does. Once it finds one it uses a break statement to drop out of the loop, and we now have our word to substitute into the message. This still should be pretty fast to execute.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|