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,41 +1,47 @@
#include "systems.h"
#include "../game_state.h"
#include "../ai_actions.h"
#include "../unit_ai.h"
#include "../unit_actions.h"
void handleUnitActionsSystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
UnitAction *action = ecs_field(it, UnitAction, 1);
void updateAISystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzBTState *state = ecs_field(it, BzBTState, 1);
f32 dt = GetFrameTime();
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t entity = it->entities[i];
handleAction(entity, &action[i], game);
if (state[i].root == NULL) {
// No behaviour
continue;
}
if (ecs_has(ECS, entity, AIBlackboard)) {
AIBlackboard *blackboard = ecs_get_mut(ECS, entity, AIBlackboard);
blackboard->entity = entity;
state[i].userData = blackboard;
}
bzBTExecute(&state[i], dt);
}
}
void updateUnitAISystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
UnitAI *unitAI = ecs_field(it, UnitAI, 1);
UnitAction *action = ecs_field(it, UnitAction, 2);
void setAIBehaviour(ecs_entity_t entity, const BzBTNode *root,
const AIBlackboard *blackboard) {
Game *game = ecs_singleton_get_mut(ECS, Game);
BZ_ASSERT(ecs_has(ECS, entity, BzBTState));
BZ_ASSERT(!blackboard || ecs_has(ECS, entity, AIBlackboard));
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t entity = it->entities[i];
Action *firstAction = action[i].first;
unitAI[i].action = firstAction;
updateUnitAI(entity, &unitAI[i], game);
if (blackboard) {
AIBlackboard *b = ecs_get_mut(ECS, entity, AIBlackboard);
*b = *blackboard;
if (b->proximity < 2.0f)
b->proximity = 2.0f;
}
}
void updateUnitActionsSystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
UnitAction *action = ecs_field(it, UnitAction, 1);
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t entity = it->entities[i];
updateAction(&action[i], game);
}
BzBTState *state = ecs_get_mut(ECS, entity, BzBTState);
bzBTDestroyState(state);
*state = bzBTCreateState(&(BzBTStateDesc) {
.root = root,
.pool = game->pools.btNodeState
});
}