Skip to content

Deduplicate food-bias sort logic in choose_task #230

Description

@itsmiso-ai

Problem

choose_task() in scripts/main.gd (line 1446) has two separate food-sorting branches that do virtually the same thing:

# Branch 1: "gather" kind with low-food bias (lines ~1455-1465)
if String(kind) == "gather" and should_bias_to_food_gathering():
    tasks.sort_custom(func(a, b) -> bool:
        var a_is_food := String(a.get("resource", "")) == "food"
        var b_is_food := String(b.get("resource", "")) == "food"
        if a_is_food and not b_is_food: return true
        if not a_is_food and b_is_food: return false
        return task_distance(worker, a) < task_distance(worker, b)
    )

# Branch 2: "gather_food" kind (lines ~1467-1477)
elif String(kind) == "gather_food":
    tasks.sort_custom(func(a, b) -> bool:
        var a_is_food := ColonyStance.is_food_gather_task(a)
        var b_is_food := ColonyStance.is_food_gather_task(b)
        if a_is_food and not b_is_food: return true
        if not a_is_food and b_is_food: return false
        return task_distance(worker, a) < task_distance(worker, b)
    )

Both sort food tasks first, then by distance. Branch 1 checks resource == "food" directly. Branch 2 uses ColonyStance.is_food_gather_task() which does the same check via the stance module. The duplication means a change to food prioritization logic must be applied in two places.

Fix

Unify into a single sort comparator. Either:

  • Extract a sort_tasks_by_food_priority(tasks, worker) helper that both branches call
  • Or collapse gather and gather_food into one kind that always checks ColonyStance.is_food_gather_task(), eliminating the need for separate branches

The second option is cleaner — tasks_for_kind() already maps both "gather" and "gather_food" to gather_gather_tasks() (line 1497), so the separate kinds only exist for the sort comparator.

Acceptance criteria

  • Single food-prioritization sort path, not two
  • Food gathering still prioritized when should_bias_to_food_gathering() returns true
  • Food stance (gather_food priority) still works
  • tests/test_colony_stance.gd passes
  • Worker behavior unchanged in manual testing

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions