ID:1036701
 
(See the best response by Kaiochao.)
Code:
mob
determine_part(mob/target, prob)
if(!(target.LeftArm.Condition || target.RightArm.Condition || target.LeftLeg.Condition || target.RightLeg.Condition))
return 0
switch(round(rand(1,100), 1))
if(1 to 25)
if(target.LeftArm.Condition)
return target.LeftArm
else
determine_part(target, prob)
if(26 to 50)
if(target.RightArm.Condition)
return target.RightArm
else
determine_part(target, prob)
if(51 to 75)
if(target.LeftLeg.Condition)
return target.LeftLeg
else
determine_part(target, prob)
if(76 to 100)
if(target.RightLeg.Condition)
return target.RightLeg
else
determine_part(target, prob)
else
return 0


Problem description:

I'm using this system to determine which Limb to use in a successful block. I however don't want to use a broken limb, hence the Condition checks.

This looks so ugly though, and I haven't tested it to determine that it works. Is there a more logical way to go about this check?

There are 4 limbs, and I'm trying to give each of them a 25% chance to be chosen.

Best response
//  This is all assuming every limb actually exists.

// make a list of potential limbs
var limbs[] = list(target.LeftArm, target.RightArm, target.LeftLeg, target.RightLeg)

// filter out bad limbs
for(var/[limb type]/limb in limbs)
if(!limb.Condition)
limbs -= limb

// if any limbs left, pick one (randomly)
if(limbs.len)
return pick(limbs)

Also:
You don't seem to be using the 'prob' argument.
rand(a, b) always returns an integer (no rounding necessary).
Wow, that just looks much better. The prob argument is something I included for another purpose, but I think I'm going to end up doing that calculation in another proc, thanks for reminding me it was there.

I'm also glad you pointed out that bit about rand(a, b), as I was curious if round() was necessary here.

Thanks a lot Kaiochao!