From e03911b78af0c021abc9cecfb379a9e3fe8c60e7 Mon Sep 17 00:00:00 2001 From: Nate Bowman Date: Sun, 8 Nov 2026 20:51:38 -0500 Subject: [PATCH] Modify farm and fishtrap code slightly --- aoe/gather.py | 76 ++++++++++++++++++++------------------------------- aoe/world.py | 10 +++---- tests/test.py | 12 +++++--- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/aoe/gather.py b/aoe/gather.py index 7bc67b3..216cad3 100644 --- a/aoe/gather.py +++ b/aoe/gather.py @@ -155,6 +155,16 @@ class Gather(): Tasks.FISH_SHIP_TRAP, ] + FARM_LIKE_TASKS = [ + Tasks.FARM, + Tasks.FISH_SHIP_TRAP, + ] + + FARM_LIKE_BUILDINGS = [ + Buildings.FARM, + Buildings.FISHTRAP, + ] + def check_gather(self, world, task, count=1): if task in [ @@ -175,7 +185,7 @@ class Gather(): if not match: raise GatherBuildingNotAvailableException - if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: + if task in Gather.FARM_LIKE_TASKS: self._check_consumable_gather_building_available(world, task, count) @@ -212,8 +222,8 @@ class Gather(): rate = self.gather_rates[task] decay = self.decay_rates[task] - if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: - _, __, used, ___ = self._get_consumable_info(task) + if task in Gather.FARM_LIKE_TASKS: + used = self.consumable_buildings[task]['used'] try: available = used[0] except IndexError: @@ -234,7 +244,7 @@ class Gather(): def improve_gather_rate_by_building(self, building, tasks): - if building in [Buildings.FARM, Buildings.FISHTRAP]: + if building in Gather.FARM_LIKE_TASKS: raise ValueError('Do not call this routine with a farm') if building not in self.buildings_gather_increase.keys(): @@ -257,7 +267,8 @@ class Gather(): def permanently_modify_consumable_capacity(self, building, increase): - _, __, used, unused = self._get_consumable_info(building) + task = self._get_task_from_consumable_building(building) + used, unused = self.consumable_buildings[task].values() current_initial_capacity = self.buildings_gather_increase[building] self.buildings_gather_increase[building] += increase @@ -270,7 +281,7 @@ class Gather(): def stop_using_consumable_buildings(self, task, count): - _, __, used, unused = self._get_consumable_info(task) + used, unused = self.consumable_buildings[task].values() # remove villagers from farms with lowest remaining capacity try: @@ -283,71 +294,46 @@ class Gather(): def start_using_consumable_buildings(self, task, count): - _, __, used, unused = self._get_consumable_info(task) + used, unused = self.consumable_buildings[task].values() # add villagers to farms with highest remaining capacity for i in range(count): heapq.heappush(used, heapq.heappop_max(unused)) - def _get_consumable_info(self, task_or_building): - if task_or_building in Buildings: - return self._get_consumable_info_from_building(task_or_building) - elif task_or_building in Tasks: - return self._get_consumable_info_from_task(task_or_building) - else: - raise ValueError - - - def _get_consumable_info_from_task(self, task): - match task: - case Tasks.FARM: - building = Buildings.FARM - case Tasks.FISH_SHIP_TRAP: - building = Buildings.FISHTRAP - case _: - raise NotFarmEquivalentException - - _, __, used, unused = self._get_consumable_info_from_building(building) - return task, building, used, unused - - - def _get_consumable_info_from_building(self, building): + def _get_task_from_consumable_building(self, building): match building: case Buildings.FARM: task = Tasks.FARM - used = self.farms - unused = self.unused_farms case Buildings.FISHTRAP: task = Tasks.FISH_SHIP_TRAP - used = self.fishtraps - unused = self.unused_fishtraps case _: raise NotFarmEquivalentException - return task, building, used, unused + return task def add_consumable_building(self, building): - task, _, __, unused = self._get_consumable_info(building) + task = self._get_task_from_consumable_building(building) + unused = self.consumable_buildings[task]['unused'] new_value = self.buildings_gather_increase[building][task] heapq.heappush(unused, new_value) def remove_consumable_building(self, building): - _, __, used, ___ = self._get_consumable_info(building) + task = self._get_task_from_consumable_building(building) + used = self.consumable_buildings[task]['used'] heapq.heappop(used) def _check_consumable_gather_building_available(self, world, task, count): - _, __, ___, unused = self._get_consumable_info(task) + unused = self.consumable_buildings[task]['unused'] if world.villager_tasks[task] + count > len(unused): raise GatherBuildingNotAvailableException def get_num_working_consumable_buildings(self, task): - _, __, used, ___ = self._get_consumable_info(task) - working = len(used) + working = len(self.consumable_buildings[task]['used']) return working @@ -371,8 +357,8 @@ class Gather(): rate = self.gather_rates[task] - if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: - _, __, used, unused = self._get_consumable_info(task) + if task in Gather.FARM_LIKE_TASKS: + used, unused = self.consumable_buildings[task].values() for i in range(len(used)): used[i] -= rate*duration else: @@ -391,10 +377,8 @@ class Gather(): def __init__(self): self.resources = [200, 200, 100, 200] - self.farms = [] - self.unused_farms = [] - self.fishtraps = [] - self.unused_fishtraps = [] + self.consumable_buildings = {t: {'used': [], 'unused': []} + for t in Gather.FARM_LIKE_TASKS} self.gather_rates = { Tasks.BERRIES: 18.6/60, diff --git a/aoe/world.py b/aoe/world.py index 59cc99d..431ab75 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -43,8 +43,6 @@ class World(): # to farms with the highest capacity. # We keep track of only the closest upcoming farm deletion. - # TODO make used* and unused* into single dict w/ task as key - # TODO gather.consumable_building_tasks # TODO require market to use market buy/sell # TODO what happens if you take away a builder while building? # TODO allow automation: @@ -122,7 +120,7 @@ class World(): if next_decrease is not None: next_decrease += self.current_time - if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: + if task in Gather.FARM_LIKE_TASKS: self.events[next_decrease].append((self._remove_consumable_building, (task,))) else: @@ -194,10 +192,10 @@ class World(): self.villager_tasks[previous_task] -= count - if previous_task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: + if previous_task in Gather.FARM_LIKE_TASKS: self.gather.stop_using_consumable_buildings(previous_task, count) - if new_task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]: + if new_task in Gather.FARM_LIKE_TASKS: self.gather.start_using_consumable_buildings(new_task, count) self._schedule_next_gather_rate_decrease(previous_task) @@ -318,7 +316,7 @@ class World(): if building == Buildings.HOUSE: self._free_housing() - if building in [Buildings.FARM, Buildings.FISHTRAP]: + if building in Gather.FARM_LIKE_BUILDINGS: self.gather.add_consumable_building(building) else: self._improve_gather_rate_by_building(building, tasks_improved) diff --git a/tests/test.py b/tests/test.py index cd4b7b9..6d0a8a3 100644 --- a/tests/test.py +++ b/tests/test.py @@ -512,8 +512,10 @@ class TestWorld(unittest.TestCase): new_food = 500 + amount_per_farm self.assertResourcesMatch([new_food, 500, 500, 500], game.world) - self.assertEqual(0, len(game.world.gather.farms)) - self.assertEqual(0, len(game.world.gather.unused_farms)) + + farms = game.world.gather.consumable_buildings[Tasks.FARM] + self.assertEqual(0, len(farms['used'])) + self.assertEqual(0, len(farms['unused'])) def test_farm_too_many_farmers(self): @@ -637,8 +639,10 @@ class TestWorld(unittest.TestCase): new_food = 500 + amount_per_fishtrap self.assertResourcesMatch([new_food, 500, 500, 500], game.world) - self.assertEqual(0, len(game.world.gather.fishtraps)) - self.assertEqual(0, len(game.world.gather.unused_fishtraps)) + + traps = game.world.gather.consumable_buildings[Tasks.FISH_SHIP_TRAP] + self.assertEqual(0, len(traps['used'])) + self.assertEqual(0, len(traps['unused'])) # fishing ships go back to fishing after trap is depleted deep_rate_per_60 = 29.4