Move functionality into gather file

This commit is contained in:
Nate Bowman
2026-11-05 21:30:14 -05:00
parent 00615c7c35
commit d51980290b
3 changed files with 113 additions and 81 deletions

View File

@@ -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,