Polish up AI

This commit is contained in:
2024-02-13 19:01:51 +01:00
parent 417cf081d7
commit dcd4d1940d
6 changed files with 40 additions and 22 deletions

View File

@@ -15,6 +15,8 @@ float shortestArc(float a, float b) {
}
BzBTStatus aiMoveTo(AIBlackboard *data, f32 dt) {
if (!data->shouldMoveTo)
return BZ_BT_FAIL;
Game *game = ecs_singleton_get_mut(ECS, Game);
const Vector2 pos = *ecs_get(ECS, data->entity, Position);
const HitBox hb = *ecs_get(ECS, data->entity, HitBox);
@@ -23,12 +25,15 @@ BzBTStatus aiMoveTo(AIBlackboard *data, f32 dt) {
f32 dst = Vector2Distance(center, target);
if (dst < data->proximity) {
ecs_remove(ECS, data->entity, Path);
data->shouldMoveTo = false;
return BZ_BT_SUCCESS;
}
if (!ecs_has(ECS, data->entity, Path)) {
bool pathfindSuccessful = entitySetPath(data->entity, target, game);
if (!pathfindSuccessful)
if (!pathfindSuccessful) {
data->shouldMoveTo = false;
return BZ_BT_FAIL;
}
}
if (ecs_has(ECS, data->entity, Orientation)) {
Orientation *orientation = ecs_get_mut(ECS, data->entity, Orientation);
@@ -47,18 +52,21 @@ BzBTStatus aiResetElapsed(AIBlackboard *data, f32 dt) {
return BZ_BT_SUCCESS;
}
#define ENEMY_NEARBY_DST 26.0f
#define ENEMY_NEARBY_DST 30.0f
#define ENEMY_CHASE_THRESH 25.0f
#define ENEMY_EVADE_THRESH 30.0f
BzBTStatus aiIsEnemyNearby(AIBlackboard *data, f32 dt) {
if (data->seenEnemy && ecs_is_alive(ECS, data->seenEnemy)) {
Position enemyPos = *ecs_get(ECS, data->seenEnemy, Position);
Position pos = *ecs_get(ECS, data->entity, Position);
if (Vector2Distance(enemyPos, pos) > ENEMY_NEARBY_DST)
if (Vector2Distance(enemyPos, pos) < ENEMY_NEARBY_DST)
return BZ_BT_SUCCESS;
data->seenEnemy = 0;
}
const f32 range = 20.0f;
const f32 range = 30.0f;
Position pos = *ecs_get(ECS, data->entity, Position);
HitBox hb = *ecs_get(ECS, data->entity, HitBox);
Vector2 center = entityGetCenter(pos, hb);
@@ -109,6 +117,10 @@ BzBTStatus aiAttackEnemy(AIBlackboard *data, f32 dt) {
Position enemyPos = *ecs_get(ECS, data->seenEnemy, Position);
Position pos = *ecs_get(ECS, data->entity, Position);
f32 dst = Vector2Distance(pos, enemyPos);
if (dst > ENEMY_CHASE_THRESH)
return BZ_BT_FAIL;
Vector2 dif = Vector2Subtract(enemyPos, pos);
// Overload steering
Steering *steering = ecs_get_mut(ECS, data->entity, Steering);
@@ -124,6 +136,10 @@ BzBTStatus aiEvadeEnemy(AIBlackboard *data, f32 dt) {
Position enemyPos = *ecs_get(ECS, data->seenEnemy, Position);
Position pos = *ecs_get(ECS, data->entity, Position);
f32 dst = Vector2Distance(pos, enemyPos);
if (dst > ENEMY_EVADE_THRESH)
return BZ_BT_FAIL;
Vector2 dif = Vector2Subtract(pos, enemyPos);
// Overload steering
Steering *steering = ecs_get_mut(ECS, data->entity, Steering);
@@ -141,6 +157,7 @@ BzBTStatus aiFindNextHarvestable(AIBlackboard *data, f32 dt) {
if (harvestable.harvestCount < harvestable.harvestLimit) {
// Target still alive, no need to find next harvestable
data->moveToPos = data->as.worker.harvestPos;
data->shouldMoveTo = true;
return BZ_BT_SUCCESS;
}
}
@@ -191,6 +208,7 @@ BzBTStatus aiFindNextHarvestable(AIBlackboard *data, f32 dt) {
data->as.worker.harvestTarget = closest;
data->as.worker.harvestPos = closestPos;
data->moveToPos = closestPos;
data->shouldMoveTo = true;
return BZ_BT_SUCCESS;
}
@@ -233,6 +251,7 @@ BzBTStatus aiFindNearestStorage(AIBlackboard *data, f32 dt) {
data->as.worker.depositTarget = closest;
data->moveToPos = getPositionNearBuilding(closest, pos);
data->shouldMoveTo = true;
return BZ_BT_SUCCESS;
}