diff --git a/aoe/gameplay.py b/aoe/gameplay.py index ed23a29..176686e 100644 --- a/aoe/gameplay.py +++ b/aoe/gameplay.py @@ -20,7 +20,7 @@ class Gameplay(): 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(f'Resources: {[int(r) for r in self.world.gather.resources]}') print() print(f'Buildings: {self.world.buildings}') print() diff --git a/aoe/gather.py b/aoe/gather.py index a5dde06..778b33e 100644 --- a/aoe/gather.py +++ b/aoe/gather.py @@ -1,4 +1,7 @@ +from collections import defaultdict import copy +import heapq +import math from aoe.types import * from aoe.exceptions import * @@ -150,10 +153,19 @@ class Gather(): raise GatherBuildingNotAvailableException if task == task.FARM: - if world.villager_tasks[task] + count > len(world.unused_farms): + if world.villager_tasks[task] + count > len(self.unused_farms): raise GatherBuildingNotAvailableException + def spend_resources(self, cost): + for i in range(len(self.resources)): + if self.resources[i] < cost[i]: + raise NotEnoughResourcesException + + for i in range(len(self.resources)): + self.resources[i] -= cost[i] + + def permanently_modify_gather_rate(self, task, rate_multiplier): self.gather_rates[task] *= rate_multiplier self.original_gather_rates[task] *= rate_multiplier @@ -167,6 +179,27 @@ class Gather(): return new_rate + def compute_next_gather_rate_decrease(self, task, villagers): + rate = self.gather_rates[task] + + if task == Tasks.FARM: + available = self.farms[0] + villagers = min(villagers, 1) + else: + available = self.gatherable_amounts[task] + + try: + next_decrease = available/(villagers*rate) + # rounding error will give 1 extra second of gathering + next_decrease = math.ceil(next_decrease) + except ZeroDivisionError: + # either villagers or rate was 0, so no next decrease should + # be scheduled + next_decrease = None + + return next_decrease + + def improve_gather_rate_by_building(self, building, tasks): if building == Buildings.FARM: raise ValueError('Do not call this routine with a farm') @@ -190,24 +223,74 @@ class Gather(): return tasks - def permanently_modify_consumable_capacity(self, building, task, increase): + def permanently_modify_consumable_capacity(self, building, increase): current_initial_capacity = self.buildings_gather_increase[building] self.buildings_gather_increase[building] += increase - return increase/current_initial_farm + modifier = increase/current_initial_capacity + + if building == Buildings.FARM: + used = self.farms + unused = self.unused_farms + + for i in range(len(used)): + used[i] += used[i]*multiplier + for i in range(len(unused)): + unused[i] += unused[i]*multiplier - def compute_gathered_resources_for_duration(self, villager_tasks, duration): - all_gathered = [0, 0, 0, 0] + def stop_using_farms(self, count): + # remove villagers from farms with lowest remaining capacity + for i in range(count): + heapq.heappush_max(self.unused_farms, + heapq.heappop(self.farms)) + + + def start_using_farms(self, count): + # add villagers to farms with highest remaining capacity + for i in range(count): + heapq.heappush(self.farms, + heapq.heappop_max(self.unused_farms)) + + + def add_farm(self): + new_farm_value = self.buildings_gather_increase[Buildings.FARM][Tasks.FARM] + heapq.heappush(self.unused_farms, new_farm_value) + + + def remove_farm(self): + heapq.heappop(self.farms) + + + def get_num_working_farms(self): + return len(self.farms) + + + def gather_resources_for_duration(self, villager_tasks, duration): + # assumes there are sufficient resources in gatherable_amounts for task, count in villager_tasks.items(): + if task in [Tasks.BUILD, Tasks.IDLE]: + continue + rate = self.gather_rates[task] - gathered = self.gathered_resources[task] - for resource, mult in gathered.items(): - all_gathered[resource] += count*rate*mult*duration - return all_gathered + + if task == Tasks.FARM: + for i in range(len(self.farms)): + self.farms[i] -= rate*duration + else: + self.gatherable_amounts[task] -= count*rate*duration + + for resource, mult in self.gathered_resources[task].items(): + amount_gathered = count*rate*mult*duration + self.resources[resource] += amount_gathered def __init__(self): + self.resources = [200, 200, 100, 200] + + self.farms = [] + self.unused_farms = [] + self.gather_rates = { Tasks.BERRIES: 18.6/60, Tasks.SHEEP: 19.8/60, diff --git a/aoe/world.py b/aoe/world.py index 059cb36..491cfbc 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -1,6 +1,4 @@ from collections import defaultdict, Counter -import heapq -import math from aoe.exceptions import * from aoe.costs import * @@ -59,22 +57,12 @@ class World(): MAX_WORLD_TIME = 3*60*60 # 3 hours - - - def add_resources(self, gathered): - for i in range(len(self.resources)): - self.resources[i] += gathered[i] + POPULATION_CAP = 200 def pay_cost(self, thing): - spent = self.costs.compute_cost(thing) - - for i in range(len(self.resources)): - if self.resources[i] < spent[i]: - raise NotEnoughResourcesException - - for i in range(len(self.resources)): - self.resources[i] -= spent[i] + cost = self.costs.compute_cost(thing) + self.gather.spend_resources(cost) def _remove_event_at_time(self, removed_event, scheduled_time): @@ -95,33 +83,19 @@ class World(): self._remove_event_at_time(self._decrease_gather_rate_by_overuse, self.rate_decrease_event_times[task]) - rate = self.gather.gather_rates[task] - - if task == Tasks.FARM: - available = self.farms[0] - villagers = min(self.villager_tasks[task], 1) - else: - available = self.gather.gatherable_amounts[task] - villagers = self.villager_tasks[task] - - try: - next_decrease = self.current_time + available/(villagers*rate) - except ZeroDivisionError: - # either villagers or rate was 0, so no next decrease should - # be scheduled - self.rate_decrease_event_times[task] = None - else: - # rounding error will give 1 extra second of gathering - next_decrease = math.ceil(self.current_time + next_decrease) - - self.rate_decrease_event_times[task] = next_decrease + next_decrease = self.gather.compute_next_gather_rate_decrease(task, + self.villager_tasks[task]) + if next_decrease: + next_decrease += self.current_time if task == Tasks.FARM: self.events[next_decrease].append((self._remove_farm, ())) else: self.events[next_decrease].append((self._decrease_gather_rate_by_overuse, (task,))) + self.rate_decrease_event_times[task] = next_decrease + def _decrease_gather_rate_by_overuse(self, task): new_rate = self.gather.decrease_gather_rate_by_overuse(task) @@ -151,19 +125,7 @@ class World(): def _permanently_modify_consumable_capacity(self, building, task, increase): - multiplier = self.gather.permanently_modify_consumable_capacity(building, - task, - increase) - - if building == Buildings.FARM: - used = self.farms - unused = self.unused_farms - - for i in range(len(used)): - used[i] += used[i]*multiplier - for i in range(len(unused)): - unused[i] += unused[i]*multiplier - + self.gather.permanently_modify_consumable_capacity(building, increase) self._schedule_next_gather_rate_decrease(task) @@ -184,16 +146,10 @@ class World(): self.villager_tasks[previous_task] -= count if previous_task == Tasks.FARM: - # remove villagers from farms with lowest remaining capacity - for i in range(count): - heapq.heappush_max(self.unused_farms, - heapq.heappop(self.farms)) + self.gather.stop_using_farms(count) if new_task == Tasks.FARM: - # add villagers to farms with highest remaining capacity - for i in range(count): - heapq.heappush(self.farms, - heapq.heappop_max(self.unused_farms)) + self.gather.start_using_farms(count) self._schedule_next_gather_rate_decrease(previous_task) if new_task: @@ -259,20 +215,19 @@ class World(): def _add_farm(self): self.buildings.append(Buildings.FARM) - new_farm_value = self.gather.buildings_gather_increase[Buildings.FARM][Tasks.FARM] - heapq.heappush(self.unused_farms, new_farm_value) + self.gather.add_farm() def _remove_farm(self): self.buildings.remove(Buildings.FARM) - heapq.heappop(self.farms) + self.gather.remove_farm() - if self.villager_tasks[Tasks.FARM] > len(self.farms): + if self.villager_tasks[Tasks.FARM] > gather.get_num_working_farms(): self.assign_villager(Tasks.FARM, Tasks.IDLE) def advance_by_time(self, duration): - self._run_clock_until(self.time + duration) + self._run_clock_until_time(self.time + duration) def advance_until_time(self, end_time): @@ -290,11 +245,8 @@ class World(): def _run_clock_without_events(self, end_time): duration = end_time - self.current_time - - gathered = self.gather.compute_gathered_resources_for_duration(self.villager_tasks, - duration) - self.add_resources(gathered) - + gathered = self.gather.gather_resources_for_duration(self.villager_tasks, + duration) self.current_time = end_time @@ -307,7 +259,7 @@ class World(): def get_population_space(self): space = sum([self.housing.space[building] for building in self.buildings]) - return min(space, 200) + return min(space, World.POPULATION_CAP) def get_effective_population(self): @@ -370,7 +322,7 @@ class World(): if unit == Units.VILLAGER and not task: raise IllegalVillagerMoveException - self.population -= self.housing.required(unit) + self.population -= self.housing.required[unit] if unit == Units.VILLAGER: self.assign_villager(task, None) @@ -457,11 +409,8 @@ class World(): self.age = Ages.DARK self.villagers = 3 self.population = 4 - self.resources = [200, 200, 100, 200] self.research = [] self.buildings = [Buildings.TOWNCENTER] - self.farms = [] - self.unused_farms = [] self.villager_tasks = {t: 0 for t in Tasks} self.villager_tasks[Tasks.IDLE] = self.villagers