Move functionality into gather file
Also implement initial version of decay rate for huntables
This commit is contained in:
108
aoe/gather.py
108
aoe/gather.py
@@ -1,4 +1,6 @@
|
||||
from collections import defaultdict
|
||||
import copy
|
||||
import heapq
|
||||
|
||||
from aoe.types import *
|
||||
from aoe.exceptions import *
|
||||
@@ -150,10 +152,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 +178,28 @@ class Gather():
|
||||
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):
|
||||
if building == Buildings.FARM:
|
||||
raise ValueError('Do not call this routine with a farm')
|
||||
@@ -190,24 +223,75 @@ 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():
|
||||
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
|
||||
|
||||
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):
|
||||
self.resources = [200, 200, 100, 200]
|
||||
|
||||
self.farms = []
|
||||
self.unused_farms = []
|
||||
|
||||
self.gather_rates = {
|
||||
Tasks.BERRIES: 18.6/60,
|
||||
Tasks.SHEEP: 19.8/60,
|
||||
@@ -230,6 +314,12 @@ class Gather():
|
||||
|
||||
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 = {
|
||||
Tasks.BERRIES: {Resources.FOOD: 1},
|
||||
Tasks.SHEEP: {Resources.FOOD: 1},
|
||||
|
||||
Reference in New Issue
Block a user