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
});
}

View File

@@ -4,8 +4,6 @@
#include "../input.h"
#include "../buildings.h"
#include "../pathfinding.h"
#include "../unit_ai.h"
#include "../unit_actions.h"
#include <rlImGui.h>
#include <raymath.h>
@@ -92,12 +90,22 @@ void inputUnitAction(Game *game, InputState *input) {
for (i32 i = 0; i < it.count; i++) {
const ecs_entity_t entity = it.entities[i];
const Position target = *ecs_get(ECS, taskEntity, Position);
setAIBehaviour(entity, game->BTs.workerHarvest, &(AIBlackboard) {
.as.worker = {
.harvestType = RES_WOOD,
.harvestTarget = taskEntity,
.harvestPos = target,
},
.proximity = 6.0f,
});
/*
setUnitAI(entity, game, &(const UnitAI) {
.type = AI_WORKER_HARVEST,
.as.workerHarvest.resource = RES_WOOD,
.as.workerHarvest.target = taskEntity,
.as.workerHarvest.targetPosition = target
});
*/
//addAction(entity, game, &(const Action) {
// .type = ACTION_MOVE_TO,
// .as.moveTo.target = target,
@@ -128,12 +136,18 @@ void inputUnitAction(Game *game, InputState *input) {
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
const ecs_entity_t entity = it.entities[i];
setAIBehaviour(entity, game->BTs.moveTo, &(AIBlackboard) {
.moveToPos = target,
.proximity = 6.0f,
});
/*
clearActions(entity, game);
addAction(entity, game, &(const Action) {
.type = ACTION_MOVE_TO,
.as.moveTo.target = target,
.as.moveTo.proximityThreshold = 6.0f,
});
*/
}
}
ecs_defer_end(ECS);

View File

@@ -114,10 +114,7 @@ void setupSystems() {
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, entityUpdateArms, EcsOnUpdate, Position, Velocity, Rotation, Orientation, Arms);
ECS_SYSTEM(ECS, handleUnitActionsSystem, EcsOnUpdate, UnitAction);
ECS_SYSTEM(ECS, updateUnitAISystem, EcsOnUpdate, UnitAI, UnitAction);
// Needs to be called after AI update, since it removes finished actions
ECS_SYSTEM(ECS, updateUnitActionsSystem, EcsOnUpdate, UnitAction);
ECS_SYSTEM(ECS, updateAISystem, EcsOnUpdate, BzBTState);
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateAnimation, EcsOnUpdate, Animation, TextureRegion);

View File

@@ -4,6 +4,7 @@
#include <flecs.h>
#include "../components.h"
#include "../ai_actions.h"
typedef struct Game Game;
@@ -19,22 +20,12 @@ bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game);
/*
* 0: Game (singleton)
* 1: UnitAction
* 1: BzBTState
*/
void handleUnitActionsSystem(ecs_iter_t *it);
void updateAISystem(ecs_iter_t *it);
/*
* 0: Game (singleton)
* 1: UnitAI
* 2: UnitAction
*/
void updateUnitAISystem(ecs_iter_t *it);
/*
* 0: Game (singleton)
* 1: UnitAction
*/
void updateUnitActionsSystem(ecs_iter_t *it);
void setAIBehaviour(ecs_entity_t entity, const BzBTNode *root,
const AIBlackboard *blackboard);
/**********************************