diff --git a/aoe/gameplay.py b/aoe/gameplay.py index 9f9f840..21c5d9d 100644 --- a/aoe/gameplay.py +++ b/aoe/gameplay.py @@ -64,8 +64,16 @@ class Gameplay(): def queue_villager(self, action_time, initial_task, **kwargs): + ''' + initial_task can be instance of one of the following: + Tasks -- assign this task along with any keyword arguments + (Tasks, dict) -- assume the dict contains the keyword arguments + List of (Tasks, dict) -- several tasks to complete in order, each with + its own keyword arguments + ''' self._check_time(action_time) - initial_task = (initial_task, kwargs) + if isinstance(initial_task, Tasks): + initial_task = (initial_task, kwargs) self.actions[action_time].append((self.world.queue_villager, (initial_task,))) @@ -97,6 +105,12 @@ class Gameplay(): (old_task, new_task, count,))) + def add_task_to_work_queue(self, action_time, task): + self._check_time(action_time) + self.actions[action_time].append((self.world.add_task_to_work_queue, + (task,))) + + def get_relic(self, action_time, walking_time=60): self._check_time(action_time) self.actions[action_time].append((self.world.get_relic, diff --git a/aoe/world.py b/aoe/world.py index 8cca4ab..24f1e8d 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -180,6 +180,33 @@ class World(): self.assign_villager(Tasks.LURE, Tasks.BOAR) + def add_task_to_work_queue(self, task, **kwargs): + self.villager_work_queue.append((task, kwargs)) + self._assign_to_villagers_from_work_queue() + + + def _assign_idle_villager_possibly_compound_task(self, task, **kwargs): + if task == Tasks.LURE: + self.lure_huntable_with_villager(Tasks.IDLE, **kwargs) + elif task == Tasks.BUILD: + if 'building_task' not in kwargs.keys(): + kwargs['building_task'] = None + self.build(kwargs['building'], + Tasks.IDLE, + kwargs['villagers_to'], + kwargs['building_task']) + else: + self.assign_villager(Tasks.IDLE, task) + + + def _assign_to_villagers_from_work_queue(self): + while (self.villager_tasks[Tasks.IDLE] > 0 and + len(self.villager_work_queue) > 0): + original_task = self.villager_work_queue.pop(0) + task, kwargs = self._parse_task(original_task) + self._assign_idle_villager_possibly_compound_task(task, **kwargs) + + def assign_villager(self, previous_task, new_task, count=1): # new_task of None means kill the villager @@ -208,6 +235,8 @@ class World(): if new_task: self._schedule_next_gather_rate_decrease(new_task) + self._assign_to_villagers_from_work_queue() + def _check_valid_builders(self, villagers_from, building): # If building a fishtrap, at least one builder needs to be a fishing @@ -472,6 +501,11 @@ class World(): def _add_thing(self, thing, initial_task): + if (initial_task and + thing != Units.VILLAGER and + not isinstance(initial_task, Tasks)): + raise ValueError('Only villagers can accept compound tasks') + self.units_being_built -= self.housing.required[thing] self.population += self.housing.required[thing] @@ -484,20 +518,8 @@ class World(): if thing == Units.VILLAGER: self.villagers += 1 self.villager_tasks[Tasks.IDLE] += 1 - - initial_task, kwargs = self._parse_initial_task(initial_task) - - if initial_task == Tasks.LURE: - self.lure_huntable_with_villager(Tasks.IDLE, **kwargs) - elif initial_task == Tasks.BUILD: - if 'building_task' not in kwargs.keys(): - kwargs['building_task'] = None - self.build(kwargs['building'], - Tasks.IDLE, - kwargs['villagers_to'], - kwargs['building_task']) - else: - self.assign_villager(Tasks.IDLE, initial_task) + task, kwargs = self._parse_task(initial_task) + self._assign_idle_villager_possibly_compound_task(task, **kwargs) elif thing == Units.MONK: self.villager_tasks[Tasks.MONK_IDLE] += 1 self._get_next_waiting_relic() @@ -559,7 +581,19 @@ class World(): self._queue_thing(Units.TRADECART) - def _parse_initial_task(self, initial_task): + def _parse_task(self, initial_task): + ''' + A single task is either a Task or a tuple of (Tasks, dict) where the + dictionary contains keyword arguments. + + If a list is passed, all tasks beyond the first are appended to the + villager work queue. + ''' + if isinstance(initial_task, list): + remaining_tasks = initial_task[1:] + self.villager_work_queue.extend(remaining_tasks) + initial_task = initial_task[0] + kwargs = {} if isinstance(initial_task, tuple): @@ -621,6 +655,7 @@ class World(): self.villager_tasks = {t: 0 for t in Tasks} self.villager_tasks[Tasks.IDLE] = self.villagers + self.villager_work_queue = [] self.rate_decrease_event_times = {t: None for t in Tasks} diff --git a/tests/test.py b/tests/test.py index 4683292..e83464b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -815,3 +815,87 @@ class TestWorld(unittest.TestCase): gathered = sheep_rate_per_60 self.assertResourcesMatch([150 + gathered, 175, 100, 200], game.world) + + + def test_work_queue_adding_villager(self): + game = Gameplay() + + build_house = (Tasks.BUILD, + {'building': Buildings.HOUSE, + 'villagers_to': Tasks.IDLE}) + build_lumber = (Tasks.BUILD, + {'building': Buildings.LUMBERCAMP, + 'villagers_to': Tasks.IDLE}) + build_house_to_wood = (Tasks.BUILD, + {'building': Buildings.HOUSE, + 'villagers_to': Tasks.WOOD}) + + task_list = [ + build_house, + build_house, + build_lumber, + build_house_to_wood, + ] + + game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP) + game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP) + game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP) + game.queue_villager(0, task_list) + + game.play_until(10) + self.assertEqual(1, len(game.world.buildings)) + self.assertEqual(4, game.world.population) + + game.play_until(30) + self.assertEqual(1, len(game.world.buildings)) + self.assertEqual(5, game.world.population) + + train_and_build_time = 25 + 25*2 + 35 + 25 + duration = train_and_build_time + 60 + game.play_until(duration) + self.assertEqual(5, len(game.world.buildings)) + self.assertEqual(5, game.world.population) + + sheep_rate_per_60 = 19.8 + gathered_food = sheep_rate_per_60*(duration/60)*3 + + wood_rate_per_60 = 23.4 + gathered_wood = wood_rate_per_60 + + self.assertResourcesMatch([150 + gathered_food, + 25 + gathered_wood, + 100, + 200], + game.world) + + + def test_add_tasks_to_work_queue(self): + game = Gameplay() + + task_list = [ + Tasks.WOOD, + Tasks.WOOD, + Tasks.SHEEP, + ] + + game.world._add_building(Buildings.LUMBERCAMP, {}, {}) + + game.queue_villager(0, Tasks.WOOD) + for t in task_list: + game.add_task_to_work_queue(0, t) + + duration = 25 + 60 + game.play_until(duration) + + sheep_rate_per_60 = 19.8 + gathered_food = sheep_rate_per_60*(duration/60) + + wood_rate_per_60 = 23.4 + gathered_wood = wood_rate_per_60*(duration/60)*2 + gathered_wood += wood_rate_per_60 + + self.assertResourcesMatch([150 + gathered_food, + 200 + gathered_wood, + 100, + 200], + game.world)