Implement warehouse and granary
This commit is contained in:
@@ -117,21 +117,27 @@ BzBTStatus aiFindNextHarvestable(AIBlackboard *data, f32 dt) {
|
||||
return BZ_BT_FAIL;
|
||||
}
|
||||
BzBTStatus aiFindNearestStorage(AIBlackboard *data, f32 dt) {
|
||||
if (!ecs_has(ECS, data->entity, Worker))
|
||||
return BZ_BT_FAIL;
|
||||
ecs_filter_t *storageFilter = ecs_filter(ECS, {
|
||||
.terms = {{ecs_id(Position)}, {ecs_id(Storage)}},
|
||||
});
|
||||
ecs_iter_t it = ecs_filter_iter(ECS, storageFilter);
|
||||
|
||||
const Vector2 pos = *ecs_get(ECS, data->entity, Position);
|
||||
ResourceType resType = data->as.worker.harvestType;
|
||||
|
||||
ecs_entity_t closest = 0;
|
||||
f32 closestDst = INFINITY;
|
||||
Position closestPos = {INFINITY, INFINITY};
|
||||
while (ecs_filter_next(&it)) {
|
||||
Position *storagePos = ecs_field(&it, Position, 1);
|
||||
Storage *storage = ecs_field(&it, Storage, 2);
|
||||
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
f32 dst = Vector2Distance(pos, closestPos);
|
||||
if (resType < 0 || resType >= RES_COUNT) continue;
|
||||
if (!storage[i].stores[resType]) continue;
|
||||
f32 dst = Vector2Distance(pos, storagePos[i]);
|
||||
if (closestDst == INFINITY || dst < closestDst) {
|
||||
closest = it.entities[i];
|
||||
closestDst = dst;
|
||||
|
||||
Reference in New Issue
Block a user