Rule Chaining

I’m putting some thought into refactoring the Rules handling a little bit. I have two goals: 1) making the process of modifying/replacing action output more elegant, and 2) supporting rule chaining (allowing multiple Rules to modify the same output).

Right now a Rule returns text, along with a constant which specifies whether the Rule cancels the action, replaces the action, does nothing, or appends to the action’s output. However, I’ve already done the groundwork for Rules that happen before or after the action is handled. The idea is that I can have rules like:

  • Before Rule: Instead of Walking North from Dim Clearing while Actor is stuck in Bear Trap, say “You can’t move. You are stuck in a bear trap.”
  • After Rule: After Walking North from Dim Clearing, say “Something clangs loudly behind you.”

The first Rule cancels the regular action processing, whereas the second merely adds to the output. This approach renders the “append” rule handling moot. Instead, I’d like to create some kind of Action object, which is created before the first Rules are processed. It would look something like:

Action Object


1
2
3
4
5
var action = {
'actor':player,
'action':input,
'output':''
};

This object would be passed to each rule in sequence, where it might be modified (primarily the output field). It can be prepended, appended, replaced entirely, etc. The resulting object is passed to the next rule, and so on until all rules have been processed or a rule has cancelled the action.

The full flow would look like:

  1. Run ‘Before’ rules
  2. If action is not canceled, run Verb processing and append to Action output
  3. If action is not canceled, run ‘After’ rules
  4. Return Action object

Note that the After rules cannot cancel the action. It’s too late. They can still prepend, append, or change the output.

Lastly, this makes me think certain rules will need broken into two rules (Before and After), which I’d like to streamline. Could expand the understand() rule builder to have before() and after() methods (effectively generating two rules in one overall block), but I don’t know if that’s really worthwhile (vs just generating two rules one after the other). Probably not.

Open questions:

  • Is there a need for a Rule to not cancel the action entirely, but cancel any remaining rules in the current rule group?
  • Is there a need for a Rule to cancel Verb processing but not cancel After rules?