From e25abd87ab34f2aaf7d8cc54b972c3c11a921915 Mon Sep 17 00:00:00 2001 From: Nate Bowman Date: Thu, 30 Jul 2026 19:33:05 -0400 Subject: [PATCH] Move more code into separate classes It might make logical sense. Honestly, I'm just tired of the code for World being so long. --- aoe/gather.py | 18 +++++++++++++++++ aoe/housing.py | 10 ++++++++++ aoe/production.py | 7 +++++++ aoe/world.py | 49 +++++++++-------------------------------------- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/aoe/gather.py b/aoe/gather.py index f9f39a0..0556977 100644 --- a/aoe/gather.py +++ b/aoe/gather.py @@ -1,6 +1,7 @@ import copy from aoe.types import * +from aoe.exceptions import * @@ -136,6 +137,23 @@ class Gather(): BIG_NUMBER = 1e6 + def check_gather(self, world, task, count=1): + if task in [Tasks.IDLE, Tasks.BUILD]: + return + + required = self.required_gather_buildings[task] + have = set(world.buildings) + + match = any(map(have.issuperset, required)) + + if not match: + raise GatherBuildingNotAvailableException + + if task == task.FARM: + if world.villager_tasks[task] + count > len(world.unused_farms): + raise GatherBuildingNotAvailableException + + def __init__(self): self.gather_rates = { Tasks.BERRIES: 18.6/60, diff --git a/aoe/housing.py b/aoe/housing.py index 4dc5ffb..0ece7da 100644 --- a/aoe/housing.py +++ b/aoe/housing.py @@ -1,12 +1,22 @@ from collections import defaultdict from aoe.types import * +from aoe.exceptions import * class Housing(): + def check_housing(self, world, thing): + required = self.required[thing] + + if required > 0: + pop_space = world.get_population_space() + if world.get_effective_population() > pop_space + required: + raise YallGotHousedException + + def __init__(self): self.space = defaultdict(int, { Buildings.HOUSE: 5, diff --git a/aoe/production.py b/aoe/production.py index e4334d5..4ca00d1 100644 --- a/aoe/production.py +++ b/aoe/production.py @@ -1,12 +1,19 @@ from collections import defaultdict from aoe.types import * +from aoe.exceptions import * class Production(): + def check_production(self, world, thing): + building = self.building[thing] + if world.production_available[building] < 1: + raise ProductionBuildingNotAvailableException + + # assumes for now each unit can be built in only one type of building def __init__(self): self.building = defaultdict(None, { diff --git a/aoe/world.py b/aoe/world.py index 7ab9a9f..d0ee4ca 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -166,7 +166,7 @@ class World(): raise IllegalVillagerMoveException if new_task: - self._check_gather(new_task, count) + self.gather.check_gather(self, new_task, count) self.villager_tasks[new_task] += count else: self.villagers -= 1 @@ -291,24 +291,25 @@ class World(): self._handle_events() - def _get_population_space(self): + def get_population_space(self): space = sum([self.housing.space[building] for building in self.buildings]) return min(space, 200) - def _get_effective_population(self): + def get_effective_population(self): return self.population + self.units_being_built def _free_housing(self): - pop_space = self._get_population_space() + pop_space = self.get_population_space() i = 0 - while (self._get_effective_population() < pop_space and + while (self.get_effective_population() < pop_space and i < len(self.production_queue)): thing = self.production_queue[i] - if self._check_housing(thing) and self._check_production(thing): + if self.housing.check_housing(self, thing) and \ + self.production.check_production(self, thing): self._start_producing(thing) del self.production_queue[i] else: @@ -338,41 +339,9 @@ class World(): self._start_producing(thing) - def _check_gather(self, task, count=1): - if task in [Tasks.IDLE, Tasks.BUILD]: - return - - required = self.gather.required_gather_buildings[task] - have = set(self.buildings) - - match = any(map(have.issuperset, required)) - - if not match: - raise GatherBuildingNotAvailableException - - if task == task.FARM: - if self.villager_tasks[task] + count > len(self.unused_farms): - raise GatherBuildingNotAvailableException - - - def _check_housing(self, thing): - required = self.housing.required[thing] - - if required > 0: - pop_space = self._get_population_space() - if self._get_effective_population() > pop_space + required: - raise YallGotHousedException - - - def _check_production(self, unit): - building = self.production.building[unit] - if self.production_available[building] < 1: - raise ProductionBuildingNotAvailableException - - def _use_housing_and_production(self, thing): - self._check_housing(thing) - self._check_production(thing) + self.housing.check_housing(self, thing) + self.production.check_production(self, thing) building = self.production.building[thing] if building is not None: