Simlify and improve BT visualization
This commit is contained in:
@@ -203,6 +203,11 @@ BzBTNode *bzBTNodeNext(const BzBTNode *node) {
|
|||||||
return node->next;
|
return node->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BzBTNode *bzBTNodeStateGetNode(const BzBTNodeState *state) {
|
||||||
|
BZ_ASSERT(state);
|
||||||
|
return state->node;
|
||||||
|
}
|
||||||
|
|
||||||
const BzBTNodeState *bzBTNodeStateNext(const BzBTNodeState *state) {
|
const BzBTNodeState *bzBTNodeStateNext(const BzBTNodeState *state) {
|
||||||
BZ_ASSERT(state);
|
BZ_ASSERT(state);
|
||||||
return state->next;
|
return state->next;
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ BzBTNodeType bzBTGetNodeType(const BzBTNode *node);
|
|||||||
BzBTNode *bzBTNodeChild(const BzBTNode *node);
|
BzBTNode *bzBTNodeChild(const BzBTNode *node);
|
||||||
BzBTNode *bzBTNodeNext(const BzBTNode *node);
|
BzBTNode *bzBTNodeNext(const BzBTNode *node);
|
||||||
|
|
||||||
|
const BzBTNode *bzBTNodeStateGetNode(const BzBTNodeState *state);
|
||||||
|
|
||||||
const BzBTNodeState *bzBTNodeStateNext(const BzBTNodeState *state);
|
const BzBTNodeState *bzBTNodeStateNext(const BzBTNodeState *state);
|
||||||
bool bzBTNodeMatchesState(const BzBTNode *node, const BzBTNodeState *state);
|
bool bzBTNodeMatchesState(const BzBTNode *node, const BzBTNodeState *state);
|
||||||
|
|
||||||
|
|||||||
@@ -227,8 +227,7 @@ static const BzBTNodeState *findNodeState(const BzBTNode *node, const BzBTNodeSt
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
|
static void visualizeBTState(const BzBTNode *node, const BzBTNodeState *state, bool sameLine, bool isActive, i32 depth) {
|
||||||
bool isActive, bool sameLine, i32 depth) {
|
|
||||||
const BzBTNode *child = bzBTNodeChild(node);
|
const BzBTNode *child = bzBTNodeChild(node);
|
||||||
BzBTNodeType type = bzBTGetNodeType(node);
|
BzBTNodeType type = bzBTGetNodeType(node);
|
||||||
char extraInfo[128];
|
char extraInfo[128];
|
||||||
@@ -236,7 +235,7 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
|
|||||||
|
|
||||||
const BzBTNodeState *nodeState = findNodeState(node, state);
|
const BzBTNodeState *nodeState = findNodeState(node, state);
|
||||||
bool hasState = nodeState != NULL;
|
bool hasState = nodeState != NULL;
|
||||||
isActive |= hasState;
|
isActive |= nodeState != NULL;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case BZ_BT_DECOR_REPEAT:
|
case BZ_BT_DECOR_REPEAT:
|
||||||
@@ -249,7 +248,7 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BZ_BT_DECOR_DELAY:
|
case BZ_BT_DECOR_DELAY:
|
||||||
if (hasState) {
|
if (isActive) {
|
||||||
snprintf(extraInfo, sizeof(extraInfo), " (%.2f < %.2fms)",
|
snprintf(extraInfo, sizeof(extraInfo), " (%.2f < %.2fms)",
|
||||||
bzBTDelayStateGetElapsed(nodeState),
|
bzBTDelayStateGetElapsed(nodeState),
|
||||||
bzBTDecorGetDelay(node));
|
bzBTDecorGetDelay(node));
|
||||||
@@ -271,35 +270,43 @@ void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
|
|||||||
if (isActive)
|
if (isActive)
|
||||||
color = (ImVec4) {1.0f, 1.0f, 0.5f, 1.0f};
|
color = (ImVec4) {1.0f, 1.0f, 0.5f, 1.0f};
|
||||||
|
|
||||||
bool hasSingleChild = true;
|
bool hasSingleChild = child && bzBTNodeNext(child) == NULL;
|
||||||
if (child && bzBTNodeNext(child)) hasSingleChild = false;
|
|
||||||
|
|
||||||
const char *suffix = hasSingleChild ? " > " : ": ";
|
i32 offset = depth * 2;
|
||||||
|
if (sameLine) offset = 0;
|
||||||
if (sameLine) {
|
const char *prefix = sameLine ? " > " : "";
|
||||||
igTextColored(color, "%s%s%s", bzBTNodeTypeToStr(type),
|
const char *postfix = child != NULL && !hasSingleChild ? ":" : "";
|
||||||
extraInfo, suffix);
|
igTextColored(color, "%s%*s%s%s%s", prefix, offset, "", bzBTNodeTypeToStr(type), extraInfo, postfix);
|
||||||
} else {
|
|
||||||
igTextColored(color, "%*s%s %s",
|
|
||||||
depth * 2, "",
|
|
||||||
bzBTNodeTypeToStr(type), extraInfo, suffix);
|
|
||||||
depth++;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isComposite = type == BZ_BT_COMP_SELECTOR ||
|
bool isComposite = type == BZ_BT_COMP_SELECTOR ||
|
||||||
type == BZ_BT_COMP_P_SELECTOR ||
|
type == BZ_BT_COMP_P_SELECTOR ||
|
||||||
type == BZ_BT_COMP_SEQUENCE ||
|
type == BZ_BT_COMP_SEQUENCE ||
|
||||||
type == BZ_BT_COMP_P_SEQUENCE;
|
type == BZ_BT_COMP_P_SEQUENCE;
|
||||||
|
depth++;
|
||||||
while (child) {
|
while (child) {
|
||||||
if (hasSingleChild) igSameLine(0, 0);
|
if (hasSingleChild) igSameLine(0, 0);
|
||||||
bool childActive = isActive && hasSingleChild;
|
bool childHasState = hasState && isComposite && bzBTCompStateGetRunningChild(nodeState) == child;
|
||||||
if (hasState && isComposite && !childActive)
|
visualizeBTState(child, state, hasSingleChild, childHasState, depth);
|
||||||
childActive = bzBTCompStateGetRunningChild(state) == child;
|
|
||||||
igVisualizeBTState(child, state, childActive, hasSingleChild, depth);
|
|
||||||
child = bzBTNodeNext(child);
|
child = bzBTNodeNext(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state) {
|
||||||
|
igSeparatorText("Tree State");
|
||||||
|
visualizeBTState(node, state, false, true, 0);
|
||||||
|
|
||||||
|
igSeparatorText("State stack");
|
||||||
|
size_t stateSize = 0;
|
||||||
|
const BzBTNodeState *pState = state;
|
||||||
|
while (pState) {
|
||||||
|
const BzBTNode *pNode = bzBTNodeStateGetNode(pState);
|
||||||
|
stateSize += bzBTGetNodeStateSize();
|
||||||
|
BzBTNodeType type = bzBTGetNodeType(pNode);
|
||||||
|
igText("%s", bzBTNodeTypeToStr(type));
|
||||||
|
pState = bzBTNodeStateNext(pState);
|
||||||
|
}
|
||||||
|
igNewLine();
|
||||||
|
igText("Total stack state size: %ld bytes.", stateSize);
|
||||||
|
}
|
||||||
void igBzBTState(ecs_world_t *ecs,
|
void igBzBTState(ecs_world_t *ecs,
|
||||||
ecs_entity_t entity, ecs_entity_t comp) {
|
ecs_entity_t entity, ecs_entity_t comp) {
|
||||||
|
|
||||||
|
|||||||
@@ -235,8 +235,7 @@ void igArms(ecs_world_t *ecs,
|
|||||||
void igArm(ecs_world_t *ecs,
|
void igArm(ecs_world_t *ecs,
|
||||||
ecs_entity_t entity, ecs_entity_t comp);
|
ecs_entity_t entity, ecs_entity_t comp);
|
||||||
|
|
||||||
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
|
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state);
|
||||||
bool isActive, bool sameLine, i32 depth);
|
|
||||||
void igBzBTState(ecs_world_t *ecs,
|
void igBzBTState(ecs_world_t *ecs,
|
||||||
ecs_entity_t entity, ecs_entity_t comp);
|
ecs_entity_t entity, ecs_entity_t comp);
|
||||||
void igAIBlackboard(ecs_world_t *ecs,
|
void igAIBlackboard(ecs_world_t *ecs,
|
||||||
|
|||||||
@@ -624,7 +624,7 @@ static void renderMainMenu(Game *game, float dt) {
|
|||||||
if (uiMainMenuButton("Play")) {
|
if (uiMainMenuButton("Play")) {
|
||||||
setScreen(game, SCREEN_GAME);
|
setScreen(game, SCREEN_GAME);
|
||||||
unloadMap(game);
|
unloadMap(game);
|
||||||
loadMap(game, "assets/maps/pathing_test.tmj");
|
loadMap(game, "assets/maps/tree_test.tmj");
|
||||||
}
|
}
|
||||||
if (uiMainMenuButton("Settings")) {
|
if (uiMainMenuButton("Settings")) {
|
||||||
setScreen(game, SCREEN_SETTINGS);
|
setScreen(game, SCREEN_SETTINGS);
|
||||||
@@ -769,7 +769,7 @@ void igInspectWindow(ecs_entity_t entity, bool *open) {
|
|||||||
igCollapsingHeader_TreeNodeFlags("BehaviourTree", 0)) {
|
igCollapsingHeader_TreeNodeFlags("BehaviourTree", 0)) {
|
||||||
const BzBTState *state = ecs_get(ECS, entity, BzBTState);
|
const BzBTState *state = ecs_get(ECS, entity, BzBTState);
|
||||||
if (state->root)
|
if (state->root)
|
||||||
igVisualizeBTState(state->root, state->_first, true, false, 0);
|
igVisualizeBTState(state->root, state->_first);
|
||||||
else
|
else
|
||||||
igTextColored((ImVec4) {1, 0, 0, 1}, "NONE");
|
igTextColored((ImVec4) {1, 0, 0, 1}, "NONE");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user