Get the rule. @return rule @throws Exception When no entry exists for the supplied key.
()
| 207 | * @throws Exception When no entry exists for the supplied key. |
| 208 | */ |
| 209 | public Rule getRule() throws Exception |
| 210 | { |
| 211 | int rangeTop = getRange(); |
| 212 | int modifier; |
| 213 | |
| 214 | try |
| 215 | { |
| 216 | modifier = Integer.parseInt(allVars.getVal(getId() + "modifier")); |
| 217 | } |
| 218 | catch (Exception e) |
| 219 | { |
| 220 | modifier = 0; |
| 221 | } |
| 222 | |
| 223 | // Determine which entry to choose |
| 224 | Dice die = new Dice(1, rangeTop, 0); |
| 225 | int choice = die.roll(); |
| 226 | choice += modifier; |
| 227 | |
| 228 | choice = (choice < 0) ? rangeTop : choice; |
| 229 | |
| 230 | //select the detail to return |
| 231 | int aWeight = 0; |
| 232 | |
| 233 | // Iterate through the list of choices until the weights (from each DataValue) |
| 234 | // are greater the num chosen as the 'choice' |
| 235 | for (String key : this) |
| 236 | { |
| 237 | Rule chkValue = (Rule) allVars.getDataElement(key); |
| 238 | int valueWeight = chkValue.getWeight(); |
| 239 | |
| 240 | if (valueWeight > 0) |
| 241 | { |
| 242 | aWeight += valueWeight; |
| 243 | |
| 244 | if (aWeight >= choice) |
| 245 | { |
| 246 | retRule = chkValue; |
| 247 | |
| 248 | return chkValue; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return retRule; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the rule given a choice |