Fix and integrate BT

This commit is contained in:
2024-01-10 14:42:21 +01:00
parent a9d20cb7f9
commit 3ba5c8932b
16 changed files with 551 additions and 109 deletions

View File

@@ -1,7 +1,6 @@
#include "components.h"
#include "unit_ai.h"
#include "unit_actions.h"
#include "ai_actions.h"
ECS_TAG_DECLARE(GameEntity);
@@ -26,8 +25,8 @@ ECS_COMPONENT_DECLARE(Easing);
ECS_COMPONENT_DECLARE(Arms);
ECS_COMPONENT_DECLARE(Arm);
ECS_COMPONENT_DECLARE(UnitAI);
ECS_COMPONENT_DECLARE(UnitAction);
ECS_COMPONENT_DECLARE(BzBTState);
ECS_COMPONENT_DECLARE(AIBlackboard);
ECS_TAG_DECLARE(Selectable);
ECS_TAG_DECLARE(Selected);
@@ -65,8 +64,8 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_COMPONENT_DEFINE(ecs, Arms);
ECS_COMPONENT_DEFINE(ecs, Arm);
ECS_COMPONENT_DEFINE(ecs, UnitAI);
ECS_COMPONENT_DEFINE(ecs, UnitAction);
ECS_COMPONENT_DEFINE(ecs, BzBTState);
ECS_COMPONENT_DEFINE(ecs, AIBlackboard);
ECS_TAG_DEFINE(ecs, Selectable);
ECS_TAG_DEFINE(ecs, Selected);
@@ -191,14 +190,90 @@ void igArm(ecs_world_t *ecs,
}
void igUnitAction(ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
void igVisualizeBTState(const BzBTNode *node, const BzBTNodeState *state,
bool isActive, bool sameLine, i32 depth) {
const BzBTNode *child = bzBTNodeChild(node);
BzBTNodeType type = bzBTGetNodeType(node);
char extraInfo[128];
extraInfo[0] = '\0';
bool hasState = bzBTNodeMatchesState(node, state);
isActive |= hasState;
switch (type) {
case BZ_BT_DECOR_REPEAT:
if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), " (%d < %d)",
bzBTRepeatStateGetIter(state),
bzBTDecorGetRepeat(node));
} else {
snprintf(extraInfo, sizeof(extraInfo), " (%d)", bzBTDecorGetRepeat(node));
}
break;
case BZ_BT_DECOR_DELAY:
if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), " (%.2f < %.2fms)",
bzBTDelayStateGetElapsed(state),
bzBTDecorGetDelay(node));
} else {
snprintf(extraInfo, sizeof(extraInfo), " (%.2fms)", bzBTDecorGetDelay(node));
}
break;
case BZ_BT_ACTION:
snprintf(extraInfo, sizeof(extraInfo), " (%s:%p)",
bzBTNodeGetName(node) ? bzBTNodeGetName(node) : "?",
bzBTActionGetFn(node));
break;
default:
break;
}
ImVec4 color = {1.0f, 1.0f, 1.0f, 1.0f};
if (isActive)
color = (ImVec4) {1.0f, 1.0f, 0.5f, 1.0f};
bool hasSingleChild = true;
if (child && bzBTNodeNext(child)) hasSingleChild = false;
const char *suffix = hasSingleChild ? " > " : ": ";
if (sameLine) {
igTextColored(color, "%s%s%s", bzBTNodeTypeToStr(type),
extraInfo, suffix);
} else {
igTextColored(color, "%*s%s %s",
depth * 2, "",
bzBTNodeTypeToStr(type), extraInfo, suffix);
depth++;
}
bool isComposite = type == BZ_BT_COMP_SELECTOR ||
type == BZ_BT_COMP_PARALLEL_SELECTOR ||
type == BZ_BT_COMP_SEQUENCE ||
type == BZ_BT_COMP_PARALLEL_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);
child = bzBTNodeNext(child);
}
}
void igBzBTState(ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
}
void igUnitAI(ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
void igAIBlackboard(ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
}
void igWorker(ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {