diff --git a/aoe/gameplay.py b/aoe/gameplay.py index d5868f3..094fded 100644 --- a/aoe/gameplay.py +++ b/aoe/gameplay.py @@ -102,6 +102,12 @@ class Gameplay(): (walking_time,))) + def set_housed_is_error(self, action_time, is_error=True): + self._check_time(action_time) + self.actions[action_time].append((self.world.set_housed_is_error, + (is_error,))) + + def lure_huntable_with_villager(self, action_time, previous_task, diff --git a/aoe/housing.py b/aoe/housing.py index 1309971..3134ed4 100644 --- a/aoe/housing.py +++ b/aoe/housing.py @@ -17,6 +17,15 @@ class Housing(): raise YallGotHousedException + def check_housing_safe(self, world, thing): + try: + self.check_housing(world, thing) + except YallGotHousedException: + return False + else: + return True + + def __init__(self): self.space = defaultdict(int, { Buildings.HOUSE: 5, diff --git a/aoe/production.py b/aoe/production.py index 2e8b85b..3962efe 100644 --- a/aoe/production.py +++ b/aoe/production.py @@ -14,6 +14,15 @@ class Production(): raise ProductionBuildingNotAvailableException + def check_production_safe(self, world, thing): + try: + self.check_production(world, thing) + except ProductionBuildingNotAvailableException: + return False + else: + return True + + # 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 4d8c8a5..4f59c30 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -248,6 +248,14 @@ class World(): self.assign_villager(task, build_task, count) + def _get_house_check_function(self): + if self.housed_is_error: + house_check = self.housing.check_housing + else: + house_check = self.housing.check_housing_safe + return house_check + + def _get_build_task_from_task(self, task): if task in Gather.FISHING_TASKS: return Tasks.FISH_SHIP_BUILD @@ -382,6 +390,10 @@ class World(): self._handle_events() + def set_housed_is_error(self, is_error=True): + self.housed_is_error = is_error + + def get_population_space(self): space = sum([self.housing.space[building] for building in self.buildings]) return min(space, World.POPULATION_CAP) @@ -394,13 +406,15 @@ class World(): def _free_housing(self): pop_space = self.get_population_space() + house_check = self._get_house_check_function() + i = 0 while (self.get_effective_population() < pop_space and i < len(self.production_queue)): thing, task = self.production_queue[i] - if self.housing.check_housing(self, thing) and \ - self.production.check_production(self, thing): + if house_check(self, thing) and \ + self.production.check_production_safe(self, thing): self._start_producing(thing, task) del self.production_queue[i] else: @@ -421,11 +435,13 @@ class World(): # production queue as a shared pool, and I don't really care. self.production_available[building] += 1 + house_check = self._get_house_check_function() + i = 0 while i < len(self.production_queue): thing, task = self.production_queue[i] if self.production.building[thing] == building and \ - self.housing.check_housing(self, thing): + house_check(self, thing): self._start_producing(thing, task) del self.production_queue[i] break @@ -487,8 +503,14 @@ class World(): def _start_producing(self, thing, initial_task): try: self._use_housing_and_production(thing) - except ProductionBuildingNotAvailableException: - self.production_queue.append((thing, initial_task)) + except Exception as e: + is_prod = isinstance(e, ProductionBuildingNotAvailableException) + is_housing = isinstance(e, YallGotHousedException) + + if is_prod or (is_housing and not self.housed_is_error): + self.production_queue.append((thing, initial_task)) + else: + raise(e) else: update_time = self.current_time + self.build_times.build_times[thing] self.events[update_time].append((self._add_thing, (thing, initial_task,))) @@ -594,4 +616,6 @@ class World(): self.production = Production() self.gather = Gather() + self.housed_is_error = True + self._deal_with_civ() diff --git a/tests/test.py b/tests/test.py index 69dbee3..7d5f97c 100644 --- a/tests/test.py +++ b/tests/test.py @@ -202,6 +202,34 @@ class TestWorld(unittest.TestCase): game.play_until(25) + def test_get_housed_no_error(self): + game = Gameplay() + game.queue_villager(0, Tasks.IDLE) + game.queue_villager(0, Tasks.IDLE) + game.queue_villager(0, Tasks.IDLE) + + game.set_housed_is_error(24, False) + game.play_until(100) + self.assertEqual(5, game.world.population) + + + def test_get_housed_toggle_error(self): + game = Gameplay() + game.world.gather.resources = [1000, 1000, 1000, 1000] + + game.queue_villager(0, Tasks.IDLE) + game.queue_villager(0, Tasks.IDLE) + + game.set_housed_is_error(24, False) + game.play_until(80) + + game.set_housed_is_error(81, True) + game.queue_villager(82, Tasks.IDLE) + + with self.assertRaises(YallGotHousedException): + game.play_until(85) + + def test_gather_boar_without_lure(self): game = Gameplay() game.assign_villager(0, Tasks.IDLE, Tasks.BOAR, 1)