From da3d30872b1658e39e5f65144c0d13f7773878d1 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 12 May 2022 08:56:15 -0700 Subject: [PATCH] Add logging action handling in a tree-like fashion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These logs are available in the actions.tree logger. They'll print a helpful list of actions and their results in a tree-like way. For example: ``` 2022-05-12 08:57:57 actions.tree: Processing Hero Actions 2022-05-12 08:57:57 actions.tree: |-> @[(x:4, y:6)][30/30] 2022-05-12 08:57:57 actions.tree: | |-> BumpAction toward (δx:-1, δy:1) by @[(x:4, y:6)][30/30] => success=False done=False alternate=WalkAction[@] 2022-05-12 08:57:57 actions.tree: | `-> WalkAction toward (δx:-1, δy:1) by @[(x:3, y:7)][30/30] => success=True done=True alternate=None 2022-05-12 08:57:57 actions.tree: Processing Entity Actions 2022-05-12 08:57:57 actions.tree: |-> Orc with 10/10 hp at (x:4, y:5) 2022-05-12 08:57:57 actions.tree: | |-> BumpAction toward (δx:-1, δy:1) by Orc with 10/10 hp at (x:4, y:5) => success=False done=False alternate=WalkAction[o] 2022-05-12 08:57:57 actions.tree: | `-> WalkAction toward (δx:-1, δy:1) by Orc with 10/10 hp at (x:3, y:6) => success=True done=True alternate=None 2022-05-12 08:57:57 actions.tree: |-> Orc with 10/10 hp at (x:5, y:5) 2022-05-12 08:57:57 actions.tree: | |-> BumpAction toward (δx:-1, δy:1) by Orc with 10/10 hp at (x:5, y:5) => success=False done=False alternate=WalkAction[o] 2022-05-12 08:57:57 actions.tree: | `-> WalkAction toward (δx:-1, δy:1) by Orc with 10/10 hp at (x:4, y:6) => success=True done=True alternate=None ``` --- roguebasin/events.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/roguebasin/events.py b/roguebasin/events.py index 09dfef2..46295d2 100644 --- a/roguebasin/events.py +++ b/roguebasin/events.py @@ -15,6 +15,7 @@ if TYPE_CHECKING: from .engine import Engine LOG = logging.getLogger('events') +ACTIONS_TREE_LOG = logging.getLogger('actions.tree') class EventHandler(tcod.event.EventDispatch[Action]): '''Handler of `tcod` events''' @@ -37,6 +38,9 @@ class EventHandler(tcod.event.EventDispatch[Action]): LOG.debug('Unhandled event: %s', event) return + ACTIONS_TREE_LOG.info('Processing Hero Actions') + ACTIONS_TREE_LOG.info('|-> %s', action.actor) + result = self.perform_action_until_done(action) # Player's action failed, don't proceed with turn. @@ -50,6 +54,8 @@ class EventHandler(tcod.event.EventDispatch[Action]): self.engine.entities, key=lambda e: e.position.euclidean_distance_to(hero_position)) + ACTIONS_TREE_LOG.info('Processing Entity Actions') + for i, ent in enumerate(entities): if not isinstance(ent, Actor): continue @@ -58,6 +64,9 @@ class EventHandler(tcod.event.EventDispatch[Action]): if not ent_ai: continue + if self.engine.map.visible[tuple(ent.position)]: + ACTIONS_TREE_LOG.info('%s-> %s', '|' if i < len(entities) - 1 else '`', ent) + action = ent_ai.act(self.engine) self.perform_action_until_done(action) @@ -66,14 +75,36 @@ class EventHandler(tcod.event.EventDispatch[Action]): def perform_action_until_done(self, action: Action) -> ActionResult: '''Perform the given action and any alternate follow-up actions until the action chain is done.''' result = action.perform(self.engine) - LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate) + + if ACTIONS_TREE_LOG.isEnabledFor(logging.INFO) and self.engine.map.visible[tuple(action.actor.position)]: + if result.alternate: + alternate_string = f'{result.alternate.__class__.__name__}[{result.alternate.actor.symbol}]' + else: + alternate_string = str(result.alternate) + ACTIONS_TREE_LOG.info('| %s-> %s => success=%s done=%s alternate=%s', + '|' if not result.success or not result.done else '`', + action, + result.success, + result.done, + alternate_string) while not result.done: action = result.alternate assert action is not None, f'Action {result.action} incomplete but no alternate action given' result = action.perform(self.engine) - LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate) + + if ACTIONS_TREE_LOG.isEnabledFor(logging.INFO) and self.engine.map.visible[tuple(action.actor.position)]: + if result.alternate: + alternate_string = f'{result.alternate.__class__.__name__}[{result.alternate.actor.symbol}]' + else: + alternate_string = str(result.alternate) + ACTIONS_TREE_LOG.info('| %s-> %s => success=%s done=%s alternate=%s', + '|' if not result.success or not result.done else '`', + action, + result.success, + result.done, + alternate_string) if result.success: break