def __init__(self, parentcontext, io, world, actionsystem, parser, stringeval, actoractivities, actor): self.parentcontext = parentcontext self.io = io self.world = world self.actor = actor self.activity = ActivityHelperObject(self) self.rule = RuleHelperObject(self) self.actionsystem = actionsystem self.parser = parser self.stringeval = stringeval self.actoractivities = actoractivities
def __init__(self, parentcontext, io, world, actionsystem, parser, stringeval, actoractivities, actor) : self.parentcontext = parentcontext self.io = io self.world = world self.actor = actor self.activity = ActivityHelperObject(self) self.rule = RuleHelperObject(self) self.actionsystem = actionsystem self.parser = parser self.stringeval = stringeval self.actoractivities = actoractivities
def __init__(self) : self.properties = PropertyTable() self.property_types = dict() # name -> Property self.inv_property_types = dict() # Property -> name self.modified_properties = dict() self.game_defined = False self.relations = dict() self.relation_handlers = [] self.name_to_relation = dict() self._activities = dict() self.activity = ActivityHelperObject(self)
class ActorContext(GameContext) : """Represents the context in which the player is assuming the role of the actor. The parser is the main parser in the parser module.""" def __init__(self, parentcontext, io, world, actionsystem, parser, stringeval, actoractivities, actor) : self.parentcontext = parentcontext self.io = io self.world = world self.actor = actor self.activity = ActivityHelperObject(self) self.rule = RuleHelperObject(self) self.actionsystem = actionsystem self.parser = parser self.stringeval = stringeval self.actoractivities = actoractivities def write(self, *stuff, **kwargs) : """Writes a line by evaluating the string using the utilities module. If there is an actor, then the text is wrapped so that the text is rendered as if the actor were doing it.""" if kwargs.has_key("actor") : stuff = [as_actor(s, kwargs["actor"]) for s in stuff] newstuff = [self.stringeval.eval_str(s, self) for s in stuff] self.io.write(*newstuff) def run(self, input=None, action=None) : if not self.world.get_property("Global", "game_started") : self.activity.start_game() self.io.set_status_var("headline", self.stringeval.eval_str(self.activity.make_current_location_headline(self.actor), self)) try : if input is None and action is None: input = self.io.get_input() if input == "dump" : self.world.dump() return (self, {}) try : if action is None : action, disambiguated = self.parser.handle_all(input, self, self.actionsystem.verify_action) else : disambiguated = True try : if disambiguated : self.actionsystem.run_action(action, self, write_action=True) else : self.actionsystem.run_action(action, self) except AbortAction as ab : if len(ab.args) > 0 : # the AbortAction may contain a message self.write(*ab.args, **ab.kwargs) if self.world.get_property("Global", "end_game_message") : self.activity.end_game_actions() if self.world.get_property("Global", "end_game_message") : self.io.set_status_var("headline", "*** Game over ***") self.io.flush() return (None, {}) for i in xrange(0, action.num_turns) : self.activity.step_turn() self.io.set_status_var("headline", self.stringeval.eval_str(self.activity.make_current_location_headline(self.actor), self)) except parser.NoSuchWord as ex : esc = escape_str(ex.word) self.write("I don't know what you mean by '%s'." % esc) except parser.NoUnderstand : self.write("Huh?") except parser.NoInput : pass except parser.Ambiguous as ex : return (DisambiguationContext(self, ex), dict()) return (self, dict()) except Exception : import traceback traceback.print_exc() self.io.write("<pre>"+traceback.format_exc()+"</pre>") return (self, dict()) def call_activity(self, name, *args, **kwargs) : kwargs["ctxt"] = self return self.actoractivities.call(name, *args, **kwargs) # def call_rule(self, name, *args, **kwargs) : # kwargs["ctxt"] = self # return actorrules.call(name, *args, **kwargs) def activity_table(self, name) : """Gets the action table of the given name.""" return actoractivities.activity_table(name) def rule_table(self, name) : """Gets the event table of the given name.""" return actorrules.rule_table(name)
def __init__(self): self._activities = dict() self.activity = ActivityHelperObject(self)
class ActorContext(GameContext): """Represents the context in which the player is assuming the role of the actor. The parser is the main parser in the parser module.""" def __init__(self, parentcontext, io, world, actionsystem, parser, stringeval, actoractivities, actor): self.parentcontext = parentcontext self.io = io self.world = world self.actor = actor self.activity = ActivityHelperObject(self) self.rule = RuleHelperObject(self) self.actionsystem = actionsystem self.parser = parser self.stringeval = stringeval self.actoractivities = actoractivities def write(self, *stuff, **kwargs): """Writes a line by evaluating the string using the utilities module. If there is an actor, then the text is wrapped so that the text is rendered as if the actor were doing it.""" if kwargs.has_key("actor"): stuff = [as_actor(s, kwargs["actor"]) for s in stuff] newstuff = [self.stringeval.eval_str(s, self) for s in stuff] self.io.write(*newstuff) def run(self, input=None, action=None): self.world.set_property("Global", "inhibit_location_description_when_moved", value=False) if not self.world.get_property("Global", "game_started"): self.activity.start_game() vis_cont = self.world.get_property( "VisibleContainer", self.world.get_property("Location", self.actor)) self.io.set_status_var("visible_container", vis_cont) self.io.set_status_var( "headline", self.stringeval.eval_str( self.activity.make_current_location_headline(self.actor), self)) try: if input is None and action is None: input = self.io.get_input() if input == "dump": self.world.dump() return (self, {}) try: if action is None: action, disambiguated = self.parser.handle_all( input, self, self.actionsystem.verify_action, allow_period_at_end=True) else: disambiguated = True try: if disambiguated: self.actionsystem.run_action(action, self, write_action=True) else: self.actionsystem.run_action(action, self) except AbortAction as ab: self.world.set_property( "Global", "inhibit_location_description_when_moved", value=True) if len(ab.args ) > 0: # the AbortAction may contain a message self.write(*ab.args, **ab.kwargs) if self.world.get_property("Global", "end_game_message"): self.activity.end_game_actions() if self.world.get_property("Global", "end_game_message"): self.io.set_status_var("headline", "*** Game over ***") self.io.flush() return (None, {}) for i in xrange(0, action.num_turns): self.activity.step_turn() vis_cont = self.world.get_property( "VisibleContainer", self.world.get_property("Location", self.actor)) self.io.set_status_var("visible_container", vis_cont) self.io.set_status_var( "headline", self.stringeval.eval_str( self.activity.make_current_location_headline( self.actor), self)) except parser.NoSuchWord as ex: esc = escape_str(ex.word) if esc == ".": self.write( "[char 91]I don't understand periods in sentences.[char 93]" ) else: self.write( "[char 91]I don't know what you mean by '%s'.[char 93]" % esc) except parser.NoUnderstand: self.write( "[char 91]I don't understand what you mean.[char 93]") except parser.NoInput: pass except parser.Ambiguous as ex: return (DisambiguationContext(self, ex), dict()) except SwitchContext as sc: return (sc.construct(self), dict()) return (self, dict()) except Exception: import traceback traceback.print_exc() self.io.write("<pre>" + traceback.format_exc() + "</pre>") return (self, dict()) def call_activity(self, name, *args, **kwargs): kwargs["ctxt"] = self return self.actoractivities.call(name, *args, **kwargs) # def call_rule(self, name, *args, **kwargs) : # kwargs["ctxt"] = self # return actorrules.call(name, *args, **kwargs) def activity_table(self, name): """Gets the action table of the given name.""" return actoractivities.activity_table(name) def rule_table(self, name): """Gets the event table of the given name.""" return actorrules.rule_table(name)