Add logging action handling in a tree-like fashion
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 ```
This commit is contained in:
parent
d236b827cd
commit
da3d30872b
1 changed files with 33 additions and 2 deletions
|
@ -15,6 +15,7 @@ if TYPE_CHECKING:
|
||||||
from .engine import Engine
|
from .engine import Engine
|
||||||
|
|
||||||
LOG = logging.getLogger('events')
|
LOG = logging.getLogger('events')
|
||||||
|
ACTIONS_TREE_LOG = logging.getLogger('actions.tree')
|
||||||
|
|
||||||
class EventHandler(tcod.event.EventDispatch[Action]):
|
class EventHandler(tcod.event.EventDispatch[Action]):
|
||||||
'''Handler of `tcod` events'''
|
'''Handler of `tcod` events'''
|
||||||
|
@ -37,6 +38,9 @@ class EventHandler(tcod.event.EventDispatch[Action]):
|
||||||
LOG.debug('Unhandled event: %s', event)
|
LOG.debug('Unhandled event: %s', event)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
ACTIONS_TREE_LOG.info('Processing Hero Actions')
|
||||||
|
ACTIONS_TREE_LOG.info('|-> %s', action.actor)
|
||||||
|
|
||||||
result = self.perform_action_until_done(action)
|
result = self.perform_action_until_done(action)
|
||||||
|
|
||||||
# Player's action failed, don't proceed with turn.
|
# Player's action failed, don't proceed with turn.
|
||||||
|
@ -50,6 +54,8 @@ class EventHandler(tcod.event.EventDispatch[Action]):
|
||||||
self.engine.entities,
|
self.engine.entities,
|
||||||
key=lambda e: e.position.euclidean_distance_to(hero_position))
|
key=lambda e: e.position.euclidean_distance_to(hero_position))
|
||||||
|
|
||||||
|
ACTIONS_TREE_LOG.info('Processing Entity Actions')
|
||||||
|
|
||||||
for i, ent in enumerate(entities):
|
for i, ent in enumerate(entities):
|
||||||
if not isinstance(ent, Actor):
|
if not isinstance(ent, Actor):
|
||||||
continue
|
continue
|
||||||
|
@ -58,6 +64,9 @@ class EventHandler(tcod.event.EventDispatch[Action]):
|
||||||
if not ent_ai:
|
if not ent_ai:
|
||||||
continue
|
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)
|
action = ent_ai.act(self.engine)
|
||||||
self.perform_action_until_done(action)
|
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:
|
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.'''
|
'''Perform the given action and any alternate follow-up actions until the action chain is done.'''
|
||||||
result = action.perform(self.engine)
|
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:
|
while not result.done:
|
||||||
action = result.alternate
|
action = result.alternate
|
||||||
assert action is not None, f'Action {result.action} incomplete but no alternate action given'
|
assert action is not None, f'Action {result.action} incomplete but no alternate action given'
|
||||||
|
|
||||||
result = action.perform(self.engine)
|
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:
|
if result.success:
|
||||||
break
|
break
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue