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