Refactor behaviour_tree (more consistent naming), fix BT visualization

This commit is contained in:
2024-01-11 07:35:58 +01:00
parent 086ee5b348
commit 2f2974fb77
4 changed files with 135 additions and 97 deletions

View File

@@ -214,6 +214,19 @@ void igArm(ecs_world_t *ecs,
}
static const BzBTNodeState *findNodeState(const BzBTNode *node, const BzBTNodeState *state) {
const BzBTNodeState *pState = state;
// Although it's painfully slow, it serves as a debug tool,
// so speed is not a critical concern.
while (pState) {
const BzBTNodeState *next = bzBTNodeStateNext(pState);
if (bzBTNodeMatchesState(node, pState))
return pState;
pState = next;
}
return NULL;
}
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
bool isActive, bool sameLine, i32 depth) {
const BzBTNode *child = bzBTNodeChild(node);
@@ -221,14 +234,15 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
char extraInfo[128];
extraInfo[0] = '\0';
bool hasState = bzBTNodeMatchesState(node, state);
const BzBTNodeState *nodeState = findNodeState(node, state);
bool hasState = nodeState != NULL;
isActive |= hasState;
switch (type) {
case BZ_BT_DECOR_REPEAT:
if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), " (%d < %d)",
bzBTRepeatStateGetIter(state),
bzBTRepeatStateGetIter(nodeState),
bzBTDecorGetRepeat(node));
} else {
snprintf(extraInfo, sizeof(extraInfo), " (%d)", bzBTDecorGetRepeat(node));
@@ -237,7 +251,7 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
case BZ_BT_DECOR_DELAY:
if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), " (%.2f < %.2fms)",
bzBTDelayStateGetElapsed(state),
bzBTDelayStateGetElapsed(nodeState),
bzBTDecorGetDelay(node));
} else {
snprintf(extraInfo, sizeof(extraInfo), " (%.2fms)", bzBTDecorGetDelay(node));
@@ -273,19 +287,16 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
}
bool isComposite = type == BZ_BT_COMP_SELECTOR ||
type == BZ_BT_COMP_PARALLEL_SELECTOR ||
type == BZ_BT_COMP_SEQUENCE ||
type == BZ_BT_COMP_PARALLEL_SEQUENCE;
type == BZ_BT_COMP_P_SELECTOR ||
type == BZ_BT_COMP_SEQUENCE ||
type == BZ_BT_COMP_P_SEQUENCE;
while (child) {
if (hasSingleChild) igSameLine(0, 0);
bool childActive = isActive && hasSingleChild;
if (hasState && isComposite && !childActive)
childActive = bzBTCompStateGetRunningChild(state) == child;
const BzBTNodeState *childState = state;
if (hasState)
childState = bzBTNodeStateNext(state);
igVisualizeBTState(child, childState, childActive, hasSingleChild, depth);
igVisualizeBTState(child, state, childActive, hasSingleChild, depth);
child = bzBTNodeNext(child);
}
}