Page 1 of 1

Changing words?

Posted: Tue Mar 11, 2014 1:24 am
by icesteruk
Unsure an easy way to explain this but I'll try my best.. in the game I play, You deal certain afflictions such as..

"rightlegmodbruised", "rightarmbroken", "rightarmbruised", "rightarmcritbruised",

My question is, How can I take away the 'bruised, broken, modbruised, critbruised' from the above and seperaet rightarm to be 'right arm' but keep record of both?

This also works for torso, rightleg, leftleg, head, leftarm.. The way im using is a BIG script and wondering if I could make it smaller.. it all runs of a list such as

combat.normal = {"rightlegmodbruised", "leftlegbroken", "leftarmbruised", "rightlegcritbruised",}

at present I'm using 3 different 'local' and gsubs to seperate them out
Code: [show] | [select all] lua
local afflimb = nextSmash[1]:gsub("rightleg", ""):gsub("rightarm", ""):gsub("leftleg", ""):gsub("leftarm", ""):gsub("head", ""):gsub("torso", "")
local hitlimb = nextSmash[1]:gsub("rightarm", "right arm"):gsub("leftarm", "left arm"):gsub("rightleg", "right leg"):gsub("leftleg", "left leg")
local teraaff = hitlimb:gsub("modbruised", ""):gsub("bruised", ""):gsub("crit", ""):gsub("broken", "")
the nextSmash[1] calls from the combat.normal on the one it can deliver..

Re: Changing words?

Posted: Tue Mar 11, 2014 11:50 am
by Jor'Mox
You could use table lookups... like this:
Code: [show] | [select all] lua
local afflictions = {
	rightlegbruised = {"right leg", "bruised"},
	rightlegmodbruised = {"right leg", "modbruised"},
	rightlegcritbruised = {"right leg", "critbruised"},
	rightlegbroken = {"right leg", "broken"},
	
	leftlegbruised = {"left leg", "bruised"},
	leftlegmodbruised = {"left leg", "modbruised"},
	leftlegcritbruised = {"left leg", "critbruised"},
	leftlegbroken = {"left leg", "broken"},
	
	rightarmbruised = {"right arm", "bruised"},
	rightarmmodbruised = {"right arm", "modbruised"},
	rightarmcritbruised = {"right arm", "critbruised"},
	rightarmbroken = {"right arm", "broken"},
	
	leftarmbruised = {"left arm", "bruised"},
	leftarmmodbruised = {"left arm", "modbruised"},
	leftarmcritbruised = {"left arm", "critbruised"},
	leftarmbroken = {"left arm", "broken"},
	
	headbruised = {"head", "bruised"},
	headmodbruised = {"head", "modbruised"},
	headcritbruised = {"head", "critbruised"},
	headbroken = {"head", "broken"},
	
	torsobruised = {"torso", "bruised"},
	torsomodbruised = {"torso", "modbruised"},
	torsocritbruised = {"torso", "critbruised"},
	torsobroken = {"torso", "broken"},
}
local part, condition, result

result = afflictions[nextSmash[1]]
part, condition = result[1], result[2]
It is a bit larger in a sense, but it will run very quickly, is easy to debug and to modify as needed. Also, if you are dealing with this same stuff in multiple places, it could potentially save space code wise. Obviously, for the purposes of making it easy to read, I made the table declaration rather spread out, but it could be substantially condensed. Like this:
Code: [show] | [select all] lua
local afflictions = {}
for k,v in ipairs({"leftleg","leftarm","rightarm","rightleg","head","torso"}) do
	for k2, v2 in ipairs({"bruised","modbruised","critbruised","broken"}) do
		afflictions[v..v2] = {v:gsub("right","right "):gsub("left","left "), v2}
	end
end

Re: Changing words?

Posted: Tue Mar 11, 2014 11:56 am
by keneanung
I'd do something like this:
Code: [show] | [select all] lua
local pattern = rex.new("^(left|right)?(arm|leg|torso|head)(modbruised|bruised|crit|broken)$")
local side, limb, aff = pattern:match(nextSmash[1])
local hitlimb = (side and (side .. " ") or "") .. limb

--[[
Now the variables contain:
aff - the affliction (modbruised, bruised...)
side - The side (left, right) or false, if no side is given (for torso and head)
limb - the limb (head, torso, arm, leg)
hitlimb - side and limb seperated by whitespace. (Use a similar thing for side and limb glued together without whitespace)
Mind you, this is untested code!

The idea behind this is to ge the three seperate parts into a variable each and recombine them as necessary.

There are probably other ways. And possibly other ways to represent what you want, but I can't suggest better representations without deeper knowledge in your system.


...And ninja'd by Jor'Mox

Re: Changing words?

Posted: Tue Mar 11, 2014 12:28 pm
by Jor'Mox
Code: [show] | [select all] lua
local pattern = rex.new("^(left|right)?(arm|leg|torso|head)((?:mod|crit)?bruised|broken)$")
local side,limb, aff = pattern:match(nextSmash[1])
local hitlimb = (side and (side .. " ") or "") .. limb

--[[
Now the variables contain:
aff - the affliction (modbruised, bruised...)
side - The side (left, right) or false, if no side is given (for torso and head)
limb - the limb (head, torso, arm, leg)
hitlimb - side and limb seperated by whitespace. (Use a similar thing for side and limb glued together without whitespace)
Fixed the pattern for you, it wasn't catching critbruised. Otherwise it works fine.

Re: Changing words?

Posted: Tue Mar 11, 2014 12:33 pm
by keneanung
Ah. I missed that crit was actually a modifier... Yay for reading comprehension :P

Re: Changing words?

Posted: Tue Mar 11, 2014 12:40 pm
by icesteruk
Thanks guys :)

Re: Changing words?

Posted: Tue Mar 11, 2014 1:10 pm
by Jor'Mox
Because I'm bored, here is another way to do it:
Code: [show] | [select all] lua
local result = rex.gsub(nextSmash[1],"((right|left)(arm|leg|head|torso)((?:crit|mod)bruised|broken","%1 %2 %3")
result = string.split(result," ")
local side, limb, aff = result[1], result[2], result[3]