Skip to content

Commit 6487689

Browse files
committed
agents: narrow vital-need preemption to productive/social actions
1 parent f0c4006 commit 6487689

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/agents/ActionSystem.cpp

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -506,45 +506,48 @@ void ActionExecutor::update(entt::registry& registry, float deltaSeconds) {
506506
if (!isVitalNeed(criticalNeed)) {
507507
// Social "critical" should not preempt work globally; it is handled by normal planning/actions.
508508
} else {
509-
510-
const auto alreadyHasCriticalConsume = [&]() -> bool {
511-
for (const auto& t : queue.tasks) {
512-
if (t.type == ActionType::ConsumeResource && t.need == criticalNeed) {
513-
return true;
509+
const auto alreadyHasCriticalConsume = [&]() -> bool {
510+
for (const auto& t : queue.tasks) {
511+
if (t.type == ActionType::ConsumeResource && t.need == criticalNeed) {
512+
return true;
513+
}
514514
}
515-
}
516-
return false;
517-
};
518-
519-
const auto preemptForCriticalNeed = [&](bool mustAbortWorkshop) {
520-
bool inserted = false;
521-
if (!ensureCriticalConsume(criticalNeed, criticalResource, inserted)) {
522-
recordCriticalPreempt(entity, criticalNeed, registry);
523-
if (mustAbortWorkshop) {
524-
if (auto* job = registry.try_get<components::WorkshopJob>(entity)) {
525-
abortWorkshopJob(*job, criticalNeed);
526-
return;
515+
return false;
516+
};
517+
518+
const auto preemptForCriticalNeed = [&](bool mustAbortWorkshop) {
519+
bool inserted = false;
520+
if (!ensureCriticalConsume(criticalNeed, criticalResource, inserted)) {
521+
recordCriticalPreempt(entity, criticalNeed, registry);
522+
if (mustAbortWorkshop) {
523+
if (auto* job = registry.try_get<components::WorkshopJob>(entity)) {
524+
abortWorkshopJob(*job, criticalNeed);
525+
return;
526+
}
527527
}
528+
abandonAllActions();
529+
return;
528530
}
529-
abandonAllActions();
530-
return;
531-
}
532-
if (inserted) {
533-
recordCriticalPreempt(entity, criticalNeed, registry);
534-
}
535-
};
531+
if (inserted) {
532+
recordCriticalPreempt(entity, criticalNeed, registry);
533+
}
534+
};
536535

537-
if (auto* job = registry.try_get<components::WorkshopJob>(entity)) {
538-
if (job->outputType != criticalResource && !alreadyHasCriticalConsume()) {
539-
// Take a break instead of throwing away progress: eat/drink first, then resume work.
540-
releaseWorkshopSlot(job->interaction, entity);
541-
preemptForCriticalNeed(true);
536+
if (auto* job = registry.try_get<components::WorkshopJob>(entity)) {
537+
if (job->outputType != criticalResource && !alreadyHasCriticalConsume()) {
538+
// Take a break instead of throwing away progress: eat/drink first, then resume work.
539+
releaseWorkshopSlot(job->interaction, entity);
540+
preemptForCriticalNeed(true);
541+
}
542+
} else if (!queue.tasks.empty() && !alreadyHasCriticalConsume()) {
543+
const auto& front = queue.tasks.front();
544+
const bool shouldYield =
545+
(front.type == ActionType::ProduceResource && front.resource != criticalResource)
546+
|| (front.type == ActionType::SocializeWithAgent);
547+
if (shouldYield) {
548+
preemptForCriticalNeed(false);
549+
}
542550
}
543-
} else if (!alreadyHasCriticalConsume()) {
544-
// Any non-critical work (including socializing) yields to vital needs.
545-
preemptForCriticalNeed(false);
546-
}
547-
548551
}
549552
}
550553
}

0 commit comments

Comments
 (0)