a. from
http://wiki.parpg.net/Proposal:Dialogue_Engine#Response_Conditionals"Response conditionals are conditional statements that determine when and if a response is made available to the player for a particular dialogue node."
b. from
http://parpg-trac.cvsdude.com/parpg/browser/trunk/game/dialogue/drunkard.yaml:
REPLY: "Gang up? There's only one of me!"
CONDITION: "not pc.met('bart')"
c. from
http://parpg-trac.cvsdude.com/parpg/browser/trunk/game/scripts/dialogueengine.pycondition_met = condition is None or \
eval(condition, cls.game_state)
By definition a. above, we see that conditionals will allow us to execute certain dialogue based on a condition, such as the example listed in b. above. The problem, is that the condition is evaluated by the code in c, which could potentially be dangerous, assuming that same section is fed malicious code.
My proposal is outlined and explained below.
d. dialogue_actions:
known_people = {}
def met(args):
if args[-1].lower() == 'false':
expected_result = False
else:
expected_result = True
return expected_result == (args[1] in known_people[args[0]])
def meet(source, target):
try:
known_people[source].append(target)
except KeyError:
known_people[source] = [target]
e. action_parser.py
import dialogue_actions
from dialogue_actions import meet, met
def parse(condition):
args = condition.split()
function = args[1]
args.pop(1)
return getattr(dialogue_actions, function)(args)
e. modified dialogue_engine.py:
condition_met = condition is None or \
parse(condition)
f. modified drunkard.yaml:
REPLY: "Gang up? There's only one of me!"
CONDITION: "pc met bart false"
As can be seen by c. and d. above, the implementation removes the need for eval() completely, and the YAML file is more readable.
The above implementation is simply a trivial proof-of-concept and I don't plan to submit it as a patch, but I think I'm on the right track. I'd like to make the parser an object itself (ActionParser) instead of a module, and of course dialogue_actions needs to be renamed, since it clashes with dialogueactions.