Limit how many workers can harvest the same resource

This commit is contained in:
2024-02-05 09:19:59 +01:00
parent ab0fef8ebf
commit d55ed29f97
11 changed files with 87 additions and 19 deletions

View File

@@ -53,10 +53,14 @@ BzBTStatus aiEvadeTarget(AIBlackboard *data, f32 dt) {
BzBTStatus aiFindNextHarvestable(AIBlackboard *data, f32 dt) {
ecs_entity_t harvestTarget = data->as.worker.harvestTarget;
if (ecs_is_alive(ECS, harvestTarget)) {
BZ_ASSERT(ecs_has_id(ECS, harvestTarget, Harvestable));
// Target still alive, no need to find next harvestable
data->moveToPos = data->as.worker.harvestPos;
return BZ_BT_SUCCESS;
BZ_ASSERT(ecs_has_id(ECS, harvestTarget, ecs_id(Harvestable)));
Harvestable harvestable = *ecs_get(ECS, harvestTarget, Harvestable);
if (harvestable.harvestCount < harvestable.harvestLimit) {
// Target still alive, no need to find next harvestable
data->moveToPos = data->as.worker.harvestPos;
return BZ_BT_SUCCESS;
}
}
BZ_ASSERT(ecs_has(ECS, data->entity, Worker));
@@ -82,10 +86,13 @@ BzBTStatus aiFindNextHarvestable(AIBlackboard *data, f32 dt) {
while (bzSpatialGridQueryNext(&it)) {
ecs_entity_t entity = *(ecs_entity_t *) it.data;
if (!ecs_is_alive(ECS, entity)) continue;
if (!ecs_has_id(ECS, entity, Harvestable) ||
if (!ecs_has_id(ECS, entity, ecs_id(Harvestable)) ||
!ecs_has(ECS, entity, Resource) ||
!ecs_has(ECS, entity, Position))
continue;
Harvestable harvestable = *ecs_get(ECS, entity, Harvestable);
if (harvestable.harvestCount >= harvestable.harvestLimit)
continue;
Resource resource = *ecs_get(ECS, entity, Resource);
Position resPos = *ecs_get(ECS, entity, Position);
if (resource.type != harvestType) continue;
@@ -151,7 +158,11 @@ BzBTStatus aiHarvestRes(AIBlackboard *data, f32 dt) {
if (!ecs_is_alive(ECS, harvestTarget))
return BZ_BT_FAIL;
BZ_ASSERT(ecs_has_id(ECS, harvestTarget, Harvestable));
BZ_ASSERT(ecs_has_id(ECS, harvestTarget, ecs_id(Harvestable)));
Harvestable *harvestable = ecs_get_mut(ECS, harvestTarget, Harvestable);
if (harvestable->harvestCount >= harvestable->harvestLimit)
return BZ_BT_FAIL;
harvestable->harvestCount++;
if (data->elapsed < worker->collectSpeed) {
data->elapsed += dt;
return BZ_BT_RUNNING;