from collections import defaultdict from aoe.exceptions import * from aoe.types import * from aoe.world import * class Gameplay(): def print_status(self): print('-'*80) print(f'Current time: {self.world.current_time}') print() print(f'Civ: {self.world.civ}') print(f'Age: {self.world.age}') print(f'Population: {self.world.population}') print() print(f'Villagers: {self.world.villagers}') print(f'Tasks: {[(t, c) for t, c in self.world.villager_tasks.items() if c != 0]}') print() print(f'Resources: {[int(r) for r in self.world.resources]}') print() print(f'Buildings: {self.world.buildings}') print() print(f'Research: {self.world.research}') print('-'*80) print() def build(self, action_time, building, villagers_from, villagers_to): self._check_time(action_time) self.actions[action_time].append((self.world.build, (building, villagers_from, villagers_to,))) def queue_research(self, action_time, research): self._check_time(action_time) self.actions[action_time].append((self.world.queue_research, (research,))) def queue_villager(self, action_time, initial_task): self._check_time(action_time) self.actions[action_time].append((self.world.queue_villager, (initial_task,))) def assign_villager(self, action_time, old_task, new_task, count=1): self._check_time(action_time) self.actions[action_time].append((self.world.assign_villager, (old_task, new_task, count,))) def _check_time(self, action_time): # TODO will it work if we do something at current time? if self.world.current_time > action_time: raise CannotChangeThePastException def _get_next_action_time(self): try: next_action_time = min(self.actions.keys()) except ValueError: next_action_time = self.end_time return next_action_time def _play_without_actions(self, end_time): self.world.advance_until_time(end_time) def _handle_actions(self): for func, args in self.actions[self.world.current_time]: func(*args) del self.actions[self.world.current_time] def play_until(self, end_time): self.end_time = end_time while self.world.current_time < self.end_time: next_action_time = self._get_next_action_time() self._play_without_actions(min(next_action_time, self.end_time)) self._handle_actions() def __init__(self): self.world = World() self.actions = defaultdict(list) self.end_time = 0