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