Rename BzBTExecStatus to BzBTExecReturn

This commit is contained in:
2024-01-11 06:24:46 +01:00
parent e82fde83c9
commit 086ee5b348

View File

@@ -258,10 +258,10 @@ typedef struct BzBTExecState {
BzBTNodeState *last; BzBTNodeState *last;
void *userData; void *userData;
} BzBTExecState; } BzBTExecState;
typedef struct BzBTExecStatus { typedef struct BzBTExecReturn {
BzBTStatus status; BzBTStatus status;
BzBTExecState state; BzBTExecState state;
} BzBTExecStatus; } BzBTExecReturn;
BzBTNodeState *execStatePopFront(BzBTExecState *state) { BzBTNodeState *execStatePopFront(BzBTExecState *state) {
BZ_ASSERT(state->first); BZ_ASSERT(state->first);
@@ -360,9 +360,9 @@ static void releaseNodeState(BzObjectPool *pool, BzBTNodeState *nodeState) {
bzObjectPoolRelease(pool, nodeState); bzObjectPoolRelease(pool, nodeState);
} }
static inline BzBTExecStatus bzBTExecuteNode(const BzBTNode *node, f32 dt, static inline BzBTExecReturn bzBTExecuteNode(const BzBTNode *node, f32 dt,
BzBTExecState *nodeState, BzObjectPool *statePool); BzBTExecState *nodeState, BzObjectPool *statePool);
static inline BzBTExecStatus bzBTExecuteComposite(const BzBTNode *node, f32 dt, static inline BzBTExecReturn bzBTExecuteComposite(const BzBTNode *node, f32 dt,
BzBTExecState *state, BzObjectPool *statePool) { BzBTExecState *state, BzObjectPool *statePool) {
BzBTNodeState *nodeState = getNodeState(node, state); BzBTNodeState *nodeState = getNodeState(node, state);
@@ -377,16 +377,16 @@ static inline BzBTExecStatus bzBTExecuteComposite(const BzBTNode *node, f32 dt,
i32 numSuccessful = 0; i32 numSuccessful = 0;
i32 numFailed = 0; i32 numFailed = 0;
i32 numChildren = 0; i32 numChildren = 0;
BzBTExecStatus status = { .status = BZ_BT_ERROR }; BzBTExecReturn execReturn = { .status = BZ_BT_ERROR };
BzBTNode *child = start; BzBTNode *child = start;
for (;child; child = child->next) { for (;child; child = child->next) {
BzBTExecStatus childStatus = bzBTExecuteNode(child, dt, state, statePool); BzBTExecReturn childReturn = bzBTExecuteNode(child, dt, state, statePool);
if (childStatus.status == BZ_BT_RUNNING) { if (childReturn.status == BZ_BT_RUNNING) {
execStateMerge(&status.state, &childStatus.state); execStateMerge(&execReturn.state, &childReturn.state);
} }
BZ_ASSERT(childStatus.state.first == NULL); BZ_ASSERT(childReturn.state.first == NULL);
numChildren++; numChildren++;
switch (childStatus.status) { switch (childReturn.status) {
case BZ_BT_RUNNING: case BZ_BT_RUNNING:
numRunning++; numRunning++;
break; break;
@@ -402,21 +402,21 @@ static inline BzBTExecStatus bzBTExecuteComposite(const BzBTNode *node, f32 dt,
switch (node->type) { switch (node->type) {
case BZ_BT_COMP_SELECTOR: case BZ_BT_COMP_SELECTOR:
case BZ_BT_COMP_PARALLEL_SELECTOR: case BZ_BT_COMP_PARALLEL_SELECTOR:
if (childStatus.status == BZ_BT_SUCCESS) if (childReturn.status == BZ_BT_SUCCESS)
status.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
break; break;
case BZ_BT_COMP_SEQUENCE: case BZ_BT_COMP_SEQUENCE:
case BZ_BT_COMP_PARALLEL_SEQUENCE: case BZ_BT_COMP_PARALLEL_SEQUENCE:
if (childStatus.status == BZ_BT_FAIL) if (childReturn.status == BZ_BT_FAIL)
status.status = BZ_BT_FAIL; execReturn.status = BZ_BT_FAIL;
break; break;
default: default:
break; break;
} }
if (status.status == BZ_BT_FAIL || status.status == BZ_BT_SUCCESS) if (execReturn.status == BZ_BT_FAIL || execReturn.status == BZ_BT_SUCCESS)
break; break;
if (numRunning > 0 && !isParallel) { if (numRunning > 0 && !isParallel) {
status.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
break; break;
} }
} }
@@ -424,61 +424,61 @@ static inline BzBTExecStatus bzBTExecuteComposite(const BzBTNode *node, f32 dt,
case BZ_BT_COMP_SELECTOR: case BZ_BT_COMP_SELECTOR:
case BZ_BT_COMP_PARALLEL_SELECTOR: case BZ_BT_COMP_PARALLEL_SELECTOR:
if (numFailed == numChildren) if (numFailed == numChildren)
status.status = BZ_BT_FAIL; execReturn.status = BZ_BT_FAIL;
break; break;
case BZ_BT_COMP_SEQUENCE: case BZ_BT_COMP_SEQUENCE:
case BZ_BT_COMP_PARALLEL_SEQUENCE: case BZ_BT_COMP_PARALLEL_SEQUENCE:
if (numSuccessful == numChildren) if (numSuccessful == numChildren)
status.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
break; break;
default: default:
break; break;
} }
// Propagate ERROR up // Propagate ERROR up
if (status.status == BZ_BT_ERROR) { if (execReturn.status == BZ_BT_ERROR) {
execStateClear(&status.state, statePool); execStateClear(&execReturn.state, statePool);
status.status = BZ_BT_ERROR; execReturn.status = BZ_BT_ERROR;
return status; return execReturn;
} }
bool finished = status.status == BZ_BT_SUCCESS || bool finished = execReturn.status == BZ_BT_SUCCESS ||
status.status == BZ_BT_FAIL; execReturn.status == BZ_BT_FAIL;
if (finished) { if (finished) {
BZ_ASSERT(status.state.first == NULL); BZ_ASSERT(execReturn.state.first == NULL);
if (nodeState) releaseNodeState(statePool, nodeState); if (nodeState) releaseNodeState(statePool, nodeState);
return status; return execReturn;
} }
BZ_ASSERT(status.status == BZ_BT_RUNNING); BZ_ASSERT(execReturn.status == BZ_BT_RUNNING);
nodeState = ensureValidNodeState(node, statePool, nodeState); nodeState = ensureValidNodeState(node, statePool, nodeState);
nodeState->as.composite.running = child; nodeState->as.composite.running = child;
execStatePushFront(&status.state, nodeState); execStatePushFront(&execReturn.state, nodeState);
return status; return execReturn;
} }
static inline BzBTExecStatus bzBTExecuteDecorator(const BzBTNode *node, f32 dt, static inline BzBTExecReturn bzBTExecuteDecorator(const BzBTNode *node, f32 dt,
BzBTExecState *state, BzObjectPool *statePool) { BzBTExecState *state, BzObjectPool *statePool) {
// Ensure decorator has only one child, if any // Ensure decorator has only one child, if any
BZ_ASSERT(!node->first || node->first == node->last); BZ_ASSERT(!node->first || node->first == node->last);
BzBTNodeState *nodeState = getNodeState(node, state); BzBTNodeState *nodeState = getNodeState(node, state);
BzBTExecStatus retStatus = { .status = BZ_BT_ERROR }; BzBTExecReturn execReturn = { .status = BZ_BT_ERROR };
switch (node->type) { switch (node->type) {
case BZ_BT_DECOR_REPEAT: case BZ_BT_DECOR_REPEAT:
nodeState = ensureValidNodeState(node, statePool, nodeState); nodeState = ensureValidNodeState(node, statePool, nodeState);
execStatePushBack(&retStatus.state, nodeState); execStatePushBack(&execReturn.state, nodeState);
break; break;
case BZ_BT_DECOR_DELAY: case BZ_BT_DECOR_DELAY:
nodeState = ensureValidNodeState(node, statePool, nodeState); nodeState = ensureValidNodeState(node, statePool, nodeState);
nodeState->as.delay.elapsed += dt; nodeState->as.delay.elapsed += dt;
execStatePushBack(&retStatus.state, nodeState); execStatePushBack(&execReturn.state, nodeState);
if (nodeState->as.delay.elapsed < node->as.delay.ms) { if (nodeState->as.delay.elapsed < node->as.delay.ms) {
retStatus.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
return retStatus; return execReturn;
} }
BZ_ASSERT(nodeState == execStatePopFront(&retStatus.state)); BZ_ASSERT(nodeState == execStatePopFront(&execReturn.state));
releaseNodeState(statePool, nodeState); releaseNodeState(statePool, nodeState);
break; break;
default: default:
@@ -486,80 +486,80 @@ static inline BzBTExecStatus bzBTExecuteDecorator(const BzBTNode *node, f32 dt,
} }
// Implicit success, if no children are present // Implicit success, if no children are present
BzBTExecStatus inStatus = { .status = BZ_BT_SUCCESS }; BzBTExecReturn childReturn = { .status = BZ_BT_SUCCESS };
if (node->first) if (node->first)
inStatus = bzBTExecuteNode(node->first, dt, state, statePool); childReturn = bzBTExecuteNode(node->first, dt, state, statePool);
// Propagate ERROR, RUNNING up // Propagate ERROR, RUNNING up
if (inStatus.status == BZ_BT_ERROR) { if (childReturn.status == BZ_BT_ERROR) {
execStateClear(&inStatus.state, statePool); execStateClear(&childReturn.state, statePool);
execStateClear(&retStatus.state, statePool); execStateClear(&execReturn.state, statePool);
retStatus.status = BZ_BT_ERROR; execReturn.status = BZ_BT_ERROR;
return retStatus; return execReturn;
} }
if (inStatus.status == BZ_BT_RUNNING) { if (childReturn.status == BZ_BT_RUNNING) {
execStateMerge(&retStatus.state, &inStatus.state); execStateMerge(&execReturn.state, &childReturn.state);
retStatus.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
return retStatus; return execReturn;
} }
BZ_ASSERT(inStatus.state.first == NULL); BZ_ASSERT(childReturn.state.first == NULL);
switch (node->type) { switch (node->type) {
case BZ_BT_DECOR_DUMMY: case BZ_BT_DECOR_DUMMY:
case BZ_BT_DECOR_DELAY: // Delay already handled case BZ_BT_DECOR_DELAY: // Delay already handled
retStatus.status = inStatus.status; execReturn.status = childReturn.status;
break; break;
case BZ_BT_DECOR_SUCCESS: case BZ_BT_DECOR_SUCCESS:
retStatus.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
break; break;
case BZ_BT_DECOR_FAIL: case BZ_BT_DECOR_FAIL:
retStatus.status = BZ_BT_FAIL; execReturn.status = BZ_BT_FAIL;
break; break;
case BZ_BT_DECOR_INVERT: case BZ_BT_DECOR_INVERT:
if (inStatus.status == BZ_BT_FAIL) if (childReturn.status == BZ_BT_FAIL)
retStatus.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
if (inStatus.status == BZ_BT_SUCCESS) if (childReturn.status == BZ_BT_SUCCESS)
retStatus.status = BZ_BT_FAIL; execReturn.status = BZ_BT_FAIL;
break; break;
case BZ_BT_DECOR_UNTIL_SUCCESS: case BZ_BT_DECOR_UNTIL_SUCCESS:
if (inStatus.status == BZ_BT_SUCCESS) if (childReturn.status == BZ_BT_SUCCESS)
retStatus.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
else else
retStatus.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
break; break;
case BZ_BT_DECOR_UNTIL_FAIL: case BZ_BT_DECOR_UNTIL_FAIL:
if (inStatus.status == BZ_BT_FAIL) if (childReturn.status == BZ_BT_FAIL)
retStatus.status = BZ_BT_SUCCESS; execReturn.status = BZ_BT_SUCCESS;
else else
retStatus.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
break; break;
case BZ_BT_DECOR_REPEAT: case BZ_BT_DECOR_REPEAT:
BZ_ASSERT(nodeMatchesState(node, &retStatus.state)); BZ_ASSERT(nodeMatchesState(node, &execReturn.state));
BZ_ASSERT(nodeState); BZ_ASSERT(nodeState);
nodeState->as.repeat.iter++; nodeState->as.repeat.iter++;
if (nodeState->as.repeat.iter >= node->as.repeat.n) { if (nodeState->as.repeat.iter >= node->as.repeat.n) {
BZ_ASSERT(nodeState == execStatePopFront(&retStatus.state)); BZ_ASSERT(nodeState == execStatePopFront(&execReturn.state));
releaseNodeState(statePool, nodeState); releaseNodeState(statePool, nodeState);
retStatus.status = inStatus.status; execReturn.status = childReturn.status;
break; break;
} }
retStatus.status = BZ_BT_RUNNING; execReturn.status = BZ_BT_RUNNING;
break; break;
default: default:
break; break;
} }
return retStatus; return execReturn;
} }
static inline BzBTExecStatus bzBTExecuteNode(const BzBTNode *node, f32 dt, static inline BzBTExecReturn bzBTExecuteNode(const BzBTNode *node, f32 dt,
BzBTExecState *nodeState, BzObjectPool *statePool) { BzBTExecState *nodeState, BzObjectPool *statePool) {
BzBTExecStatus status = { .status = BZ_BT_ERROR }; BzBTExecReturn execReturn = { .status = BZ_BT_ERROR };
switch (node->type) { switch (node->type) {
case BZ_BT_COMP_SELECTOR: case BZ_BT_COMP_SELECTOR:
case BZ_BT_COMP_SEQUENCE: case BZ_BT_COMP_SEQUENCE:
case BZ_BT_COMP_PARALLEL_SELECTOR: case BZ_BT_COMP_PARALLEL_SELECTOR:
case BZ_BT_COMP_PARALLEL_SEQUENCE: case BZ_BT_COMP_PARALLEL_SEQUENCE:
status = bzBTExecuteComposite(node, dt, nodeState, statePool); execReturn = bzBTExecuteComposite(node, dt, nodeState, statePool);
break; break;
case BZ_BT_DECOR_DUMMY: case BZ_BT_DECOR_DUMMY:
case BZ_BT_DECOR_SUCCESS: case BZ_BT_DECOR_SUCCESS:
@@ -569,14 +569,14 @@ static inline BzBTExecStatus bzBTExecuteNode(const BzBTNode *node, f32 dt,
case BZ_BT_DECOR_UNTIL_FAIL: case BZ_BT_DECOR_UNTIL_FAIL:
case BZ_BT_DECOR_REPEAT: case BZ_BT_DECOR_REPEAT:
case BZ_BT_DECOR_DELAY: case BZ_BT_DECOR_DELAY:
status = bzBTExecuteDecorator(node, dt, nodeState, statePool); execReturn = bzBTExecuteDecorator(node, dt, nodeState, statePool);
break; break;
case BZ_BT_ACTION: case BZ_BT_ACTION:
BZ_ASSERT(node->as.action.fn); BZ_ASSERT(node->as.action.fn);
status.status = node->as.action.fn(nodeState->userData, dt); execReturn.status = node->as.action.fn(nodeState->userData, dt);
break; break;
} }
return status; return execReturn;
} }
BzBTStatus bzBTExecute(BzBTState *state, f32 dt) { BzBTStatus bzBTExecute(BzBTState *state, f32 dt) {
@@ -594,15 +594,15 @@ BzBTStatus bzBTExecute(BzBTState *state, f32 dt) {
.last = state->_last, .last = state->_last,
.userData = state->userData .userData = state->userData
}; };
BzBTExecStatus status = bzBTExecuteNode(firstNode, dt, &execState, state->nodeStatePool); BzBTExecReturn execReturn = bzBTExecuteNode(firstNode, dt, &execState, state->nodeStatePool);
if (status.status == BZ_BT_ERROR) if (execReturn.status == BZ_BT_ERROR)
execStateClear(&status.state, state->nodeStatePool); execStateClear(&execReturn.state, state->nodeStatePool);
BZ_ASSERT(execState.first == NULL && execState.last == NULL); BZ_ASSERT(execState.first == NULL && execState.last == NULL);
state->_first = status.state.first; state->_first = execReturn.state.first;
state->_last = status.state.last; state->_last = execReturn.state.last;
switch (status.status) { switch (execReturn.status) {
case BZ_BT_SUCCESS: case BZ_BT_SUCCESS:
state->onSuccess && state->onSuccess(state->userData, dt); state->onSuccess && state->onSuccess(state->userData, dt);
break; break;
@@ -616,5 +616,5 @@ BzBTStatus bzBTExecute(BzBTState *state, f32 dt) {
break; break;
} }
return status.status; return execReturn.status;
} }