Move functionality into gather file
Also implement initial version of decay rate for huntables
This commit is contained in:
@@ -20,7 +20,7 @@ class Gameplay():
|
|||||||
print(f'Villagers: {self.world.villagers}')
|
print(f'Villagers: {self.world.villagers}')
|
||||||
print(f'Tasks: {[(t, c) for t, c in self.world.villager_tasks.items() if c != 0]}')
|
print(f'Tasks: {[(t, c) for t, c in self.world.villager_tasks.items() if c != 0]}')
|
||||||
print()
|
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()
|
||||||
print(f'Buildings: {self.world.buildings}')
|
print(f'Buildings: {self.world.buildings}')
|
||||||
print()
|
print()
|
||||||
|
|||||||
108
aoe/gather.py
108
aoe/gather.py
@@ -1,4 +1,6 @@
|
|||||||
|
from collections import defaultdict
|
||||||
import copy
|
import copy
|
||||||
|
import heapq
|
||||||
|
|
||||||
from aoe.types import *
|
from aoe.types import *
|
||||||
from aoe.exceptions import *
|
from aoe.exceptions import *
|
||||||
@@ -150,10 +152,19 @@ class Gather():
|
|||||||
raise GatherBuildingNotAvailableException
|
raise GatherBuildingNotAvailableException
|
||||||
|
|
||||||
if task == task.FARM:
|
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
|
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):
|
def permanently_modify_gather_rate(self, task, rate_multiplier):
|
||||||
self.gather_rates[task] *= rate_multiplier
|
self.gather_rates[task] *= rate_multiplier
|
||||||
self.original_gather_rates[task] *= rate_multiplier
|
self.original_gather_rates[task] *= rate_multiplier
|
||||||
@@ -167,6 +178,28 @@ class Gather():
|
|||||||
return new_rate
|
return new_rate
|
||||||
|
|
||||||
|
|
||||||
|
def compute_next_gather_rate_decrease(self, task, villagers):
|
||||||
|
rate = self.gather_rates[task]
|
||||||
|
decay = self.decay_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 + decay)
|
||||||
|
# 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):
|
def improve_gather_rate_by_building(self, building, tasks):
|
||||||
if building == Buildings.FARM:
|
if building == Buildings.FARM:
|
||||||
raise ValueError('Do not call this routine with a farm')
|
raise ValueError('Do not call this routine with a farm')
|
||||||
@@ -190,24 +223,75 @@ class Gather():
|
|||||||
return tasks
|
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]
|
current_initial_capacity = self.buildings_gather_increase[building]
|
||||||
self.buildings_gather_increase[building] += increase
|
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):
|
def stop_using_farms(self, count):
|
||||||
all_gathered = [0, 0, 0, 0]
|
# 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():
|
for task, count in villager_tasks.items():
|
||||||
rate = self.gather_rates[task]
|
rate = self.gather_rates[task]
|
||||||
gathered = self.gathered_resources[task]
|
|
||||||
for resource, mult in gathered.items():
|
if task == Tasks.FARM:
|
||||||
all_gathered[resource] += count*rate*mult*duration
|
for i in range(len(self.farms)):
|
||||||
return all_gathered
|
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
|
||||||
|
|
||||||
|
for task in self.decay_rates.keys():
|
||||||
|
if self.gatherable_amounts[task] > 0:
|
||||||
|
self.gatherable_amounts[task] -= self.decay_rates[task]*duration
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.resources = [200, 200, 100, 200]
|
||||||
|
|
||||||
|
self.farms = []
|
||||||
|
self.unused_farms = []
|
||||||
|
|
||||||
self.gather_rates = {
|
self.gather_rates = {
|
||||||
Tasks.BERRIES: 18.6/60,
|
Tasks.BERRIES: 18.6/60,
|
||||||
Tasks.SHEEP: 19.8/60,
|
Tasks.SHEEP: 19.8/60,
|
||||||
@@ -230,6 +314,12 @@ class Gather():
|
|||||||
|
|
||||||
self.original_gather_rates = copy.deepcopy(self.gather_rates)
|
self.original_gather_rates = copy.deepcopy(self.gather_rates)
|
||||||
|
|
||||||
|
self.decay_rates = defaultdict(int, {
|
||||||
|
Tasks.SHEEP: 0.25,
|
||||||
|
Tasks.BOAR: 0.25,
|
||||||
|
Tasks.HUNT: 0.25,
|
||||||
|
}
|
||||||
|
|
||||||
self.gathered_resources = {
|
self.gathered_resources = {
|
||||||
Tasks.BERRIES: {Resources.FOOD: 1},
|
Tasks.BERRIES: {Resources.FOOD: 1},
|
||||||
Tasks.SHEEP: {Resources.FOOD: 1},
|
Tasks.SHEEP: {Resources.FOOD: 1},
|
||||||
|
|||||||
89
aoe/world.py
89
aoe/world.py
@@ -1,5 +1,4 @@
|
|||||||
from collections import defaultdict, Counter
|
from collections import defaultdict, Counter
|
||||||
import heapq
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from aoe.exceptions import *
|
from aoe.exceptions import *
|
||||||
@@ -59,22 +58,12 @@ class World():
|
|||||||
|
|
||||||
|
|
||||||
MAX_WORLD_TIME = 3*60*60 # 3 hours
|
MAX_WORLD_TIME = 3*60*60 # 3 hours
|
||||||
|
POPULATION_CAP = 200
|
||||||
|
|
||||||
def add_resources(self, gathered):
|
|
||||||
for i in range(len(self.resources)):
|
|
||||||
self.resources[i] += gathered[i]
|
|
||||||
|
|
||||||
|
|
||||||
def pay_cost(self, thing):
|
def pay_cost(self, thing):
|
||||||
spent = self.costs.compute_cost(thing)
|
cost = self.costs.compute_cost(thing)
|
||||||
|
self.gather.spend_resources(cost)
|
||||||
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]
|
|
||||||
|
|
||||||
|
|
||||||
def _remove_event_at_time(self, removed_event, scheduled_time):
|
def _remove_event_at_time(self, removed_event, scheduled_time):
|
||||||
@@ -95,33 +84,20 @@ class World():
|
|||||||
self._remove_event_at_time(self._decrease_gather_rate_by_overuse,
|
self._remove_event_at_time(self._decrease_gather_rate_by_overuse,
|
||||||
self.rate_decrease_event_times[task])
|
self.rate_decrease_event_times[task])
|
||||||
|
|
||||||
rate = self.gather.gather_rates[task]
|
next_decrease = self.gather.compute_next_gather_rate_decrease(self,
|
||||||
|
task,
|
||||||
if task == Tasks.FARM:
|
self.villager_tasks[task])
|
||||||
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
|
|
||||||
|
|
||||||
|
if next_decrease:
|
||||||
|
next_decrease += self.current_time
|
||||||
if task == Tasks.FARM:
|
if task == Tasks.FARM:
|
||||||
self.events[next_decrease].append((self._remove_farm, ()))
|
self.events[next_decrease].append((self._remove_farm, ()))
|
||||||
else:
|
else:
|
||||||
self.events[next_decrease].append((self._decrease_gather_rate_by_overuse,
|
self.events[next_decrease].append((self._decrease_gather_rate_by_overuse,
|
||||||
(task,)))
|
(task,)))
|
||||||
|
|
||||||
|
self.rate_decrease_event_times[task] = next_decrease
|
||||||
|
|
||||||
|
|
||||||
def _decrease_gather_rate_by_overuse(self, task):
|
def _decrease_gather_rate_by_overuse(self, task):
|
||||||
new_rate = self.gather.decrease_gather_rate_by_overuse(task)
|
new_rate = self.gather.decrease_gather_rate_by_overuse(task)
|
||||||
@@ -151,19 +127,7 @@ class World():
|
|||||||
|
|
||||||
|
|
||||||
def _permanently_modify_consumable_capacity(self, building, task, increase):
|
def _permanently_modify_consumable_capacity(self, building, task, increase):
|
||||||
multiplier = self.gather.permanently_modify_consumable_capacity(building,
|
self.gather.permanently_modify_consumable_capacity(building, increase)
|
||||||
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._schedule_next_gather_rate_decrease(task)
|
self._schedule_next_gather_rate_decrease(task)
|
||||||
|
|
||||||
|
|
||||||
@@ -184,16 +148,10 @@ class World():
|
|||||||
self.villager_tasks[previous_task] -= count
|
self.villager_tasks[previous_task] -= count
|
||||||
|
|
||||||
if previous_task == Tasks.FARM:
|
if previous_task == Tasks.FARM:
|
||||||
# remove villagers from farms with lowest remaining capacity
|
self.gather.stop_using_farms(count)
|
||||||
for i in range(count):
|
|
||||||
heapq.heappush_max(self.unused_farms,
|
|
||||||
heapq.heappop(self.farms))
|
|
||||||
|
|
||||||
if new_task == Tasks.FARM:
|
if new_task == Tasks.FARM:
|
||||||
# add villagers to farms with highest remaining capacity
|
self.gather.start_using_farms(count)
|
||||||
for i in range(count):
|
|
||||||
heapq.heappush(self.farms,
|
|
||||||
heapq.heappop_max(self.unused_farms))
|
|
||||||
|
|
||||||
self._schedule_next_gather_rate_decrease(previous_task)
|
self._schedule_next_gather_rate_decrease(previous_task)
|
||||||
if new_task:
|
if new_task:
|
||||||
@@ -259,20 +217,19 @@ class World():
|
|||||||
|
|
||||||
def _add_farm(self):
|
def _add_farm(self):
|
||||||
self.buildings.append(Buildings.FARM)
|
self.buildings.append(Buildings.FARM)
|
||||||
new_farm_value = self.gather.buildings_gather_increase[Buildings.FARM][Tasks.FARM]
|
self.gather.add_farm()
|
||||||
heapq.heappush(self.unused_farms, new_farm_value)
|
|
||||||
|
|
||||||
|
|
||||||
def _remove_farm(self):
|
def _remove_farm(self):
|
||||||
self.buildings.remove(Buildings.FARM)
|
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)
|
self.assign_villager(Tasks.FARM, Tasks.IDLE)
|
||||||
|
|
||||||
|
|
||||||
def advance_by_time(self, duration):
|
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):
|
def advance_until_time(self, end_time):
|
||||||
@@ -290,11 +247,8 @@ class World():
|
|||||||
|
|
||||||
def _run_clock_without_events(self, end_time):
|
def _run_clock_without_events(self, end_time):
|
||||||
duration = end_time - self.current_time
|
duration = end_time - self.current_time
|
||||||
|
gathered = self.gather.gather_resources_for_duration(self.villager_tasks,
|
||||||
gathered = self.gather.compute_gathered_resources_for_duration(self.villager_tasks,
|
|
||||||
duration)
|
duration)
|
||||||
self.add_resources(gathered)
|
|
||||||
|
|
||||||
self.current_time = end_time
|
self.current_time = end_time
|
||||||
|
|
||||||
|
|
||||||
@@ -307,7 +261,7 @@ class World():
|
|||||||
|
|
||||||
def get_population_space(self):
|
def get_population_space(self):
|
||||||
space = sum([self.housing.space[building] for building in self.buildings])
|
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):
|
def get_effective_population(self):
|
||||||
@@ -370,7 +324,7 @@ class World():
|
|||||||
if unit == Units.VILLAGER and not task:
|
if unit == Units.VILLAGER and not task:
|
||||||
raise IllegalVillagerMoveException
|
raise IllegalVillagerMoveException
|
||||||
|
|
||||||
self.population -= self.housing.required(unit)
|
self.population -= self.housing.required[unit]
|
||||||
|
|
||||||
if unit == Units.VILLAGER:
|
if unit == Units.VILLAGER:
|
||||||
self.assign_villager(task, None)
|
self.assign_villager(task, None)
|
||||||
@@ -457,11 +411,8 @@ class World():
|
|||||||
self.age = Ages.DARK
|
self.age = Ages.DARK
|
||||||
self.villagers = 3
|
self.villagers = 3
|
||||||
self.population = 4
|
self.population = 4
|
||||||
self.resources = [200, 200, 100, 200]
|
|
||||||
self.research = []
|
self.research = []
|
||||||
self.buildings = [Buildings.TOWNCENTER]
|
self.buildings = [Buildings.TOWNCENTER]
|
||||||
self.farms = []
|
|
||||||
self.unused_farms = []
|
|
||||||
|
|
||||||
self.villager_tasks = {t: 0 for t in Tasks}
|
self.villager_tasks = {t: 0 for t in Tasks}
|
||||||
self.villager_tasks[Tasks.IDLE] = self.villagers
|
self.villager_tasks[Tasks.IDLE] = self.villagers
|
||||||
|
|||||||
Reference in New Issue
Block a user