Modify farm and fishtrap code slightly
This commit is contained in:
@@ -155,6 +155,16 @@ class Gather():
|
|||||||
Tasks.FISH_SHIP_TRAP,
|
Tasks.FISH_SHIP_TRAP,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
FARM_LIKE_TASKS = [
|
||||||
|
Tasks.FARM,
|
||||||
|
Tasks.FISH_SHIP_TRAP,
|
||||||
|
]
|
||||||
|
|
||||||
|
FARM_LIKE_BUILDINGS = [
|
||||||
|
Buildings.FARM,
|
||||||
|
Buildings.FISHTRAP,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def check_gather(self, world, task, count=1):
|
def check_gather(self, world, task, count=1):
|
||||||
if task in [
|
if task in [
|
||||||
@@ -175,7 +185,7 @@ class Gather():
|
|||||||
if not match:
|
if not match:
|
||||||
raise GatherBuildingNotAvailableException
|
raise GatherBuildingNotAvailableException
|
||||||
|
|
||||||
if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if task in Gather.FARM_LIKE_TASKS:
|
||||||
self._check_consumable_gather_building_available(world,
|
self._check_consumable_gather_building_available(world,
|
||||||
task,
|
task,
|
||||||
count)
|
count)
|
||||||
@@ -212,8 +222,8 @@ class Gather():
|
|||||||
rate = self.gather_rates[task]
|
rate = self.gather_rates[task]
|
||||||
decay = self.decay_rates[task]
|
decay = self.decay_rates[task]
|
||||||
|
|
||||||
if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if task in Gather.FARM_LIKE_TASKS:
|
||||||
_, __, used, ___ = self._get_consumable_info(task)
|
used = self.consumable_buildings[task]['used']
|
||||||
try:
|
try:
|
||||||
available = used[0]
|
available = used[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@@ -234,7 +244,7 @@ class Gather():
|
|||||||
|
|
||||||
|
|
||||||
def improve_gather_rate_by_building(self, building, tasks):
|
def improve_gather_rate_by_building(self, building, tasks):
|
||||||
if building in [Buildings.FARM, Buildings.FISHTRAP]:
|
if building in Gather.FARM_LIKE_TASKS:
|
||||||
raise ValueError('Do not call this routine with a farm')
|
raise ValueError('Do not call this routine with a farm')
|
||||||
|
|
||||||
if building not in self.buildings_gather_increase.keys():
|
if building not in self.buildings_gather_increase.keys():
|
||||||
@@ -257,7 +267,8 @@ class Gather():
|
|||||||
|
|
||||||
|
|
||||||
def permanently_modify_consumable_capacity(self, building, increase):
|
def permanently_modify_consumable_capacity(self, building, increase):
|
||||||
_, __, used, unused = self._get_consumable_info(building)
|
task = self._get_task_from_consumable_building(building)
|
||||||
|
used, unused = self.consumable_buildings[task].values()
|
||||||
|
|
||||||
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
|
||||||
@@ -270,7 +281,7 @@ class Gather():
|
|||||||
|
|
||||||
|
|
||||||
def stop_using_consumable_buildings(self, task, count):
|
def stop_using_consumable_buildings(self, task, count):
|
||||||
_, __, used, unused = self._get_consumable_info(task)
|
used, unused = self.consumable_buildings[task].values()
|
||||||
|
|
||||||
# remove villagers from farms with lowest remaining capacity
|
# remove villagers from farms with lowest remaining capacity
|
||||||
try:
|
try:
|
||||||
@@ -283,71 +294,46 @@ class Gather():
|
|||||||
|
|
||||||
|
|
||||||
def start_using_consumable_buildings(self, task, count):
|
def start_using_consumable_buildings(self, task, count):
|
||||||
_, __, used, unused = self._get_consumable_info(task)
|
used, unused = self.consumable_buildings[task].values()
|
||||||
|
|
||||||
# add villagers to farms with highest remaining capacity
|
# add villagers to farms with highest remaining capacity
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
heapq.heappush(used, heapq.heappop_max(unused))
|
heapq.heappush(used, heapq.heappop_max(unused))
|
||||||
|
|
||||||
|
|
||||||
def _get_consumable_info(self, task_or_building):
|
def _get_task_from_consumable_building(self, building):
|
||||||
if task_or_building in Buildings:
|
|
||||||
return self._get_consumable_info_from_building(task_or_building)
|
|
||||||
elif task_or_building in Tasks:
|
|
||||||
return self._get_consumable_info_from_task(task_or_building)
|
|
||||||
else:
|
|
||||||
raise ValueError
|
|
||||||
|
|
||||||
|
|
||||||
def _get_consumable_info_from_task(self, task):
|
|
||||||
match task:
|
|
||||||
case Tasks.FARM:
|
|
||||||
building = Buildings.FARM
|
|
||||||
case Tasks.FISH_SHIP_TRAP:
|
|
||||||
building = Buildings.FISHTRAP
|
|
||||||
case _:
|
|
||||||
raise NotFarmEquivalentException
|
|
||||||
|
|
||||||
_, __, used, unused = self._get_consumable_info_from_building(building)
|
|
||||||
return task, building, used, unused
|
|
||||||
|
|
||||||
|
|
||||||
def _get_consumable_info_from_building(self, building):
|
|
||||||
match building:
|
match building:
|
||||||
case Buildings.FARM:
|
case Buildings.FARM:
|
||||||
task = Tasks.FARM
|
task = Tasks.FARM
|
||||||
used = self.farms
|
|
||||||
unused = self.unused_farms
|
|
||||||
case Buildings.FISHTRAP:
|
case Buildings.FISHTRAP:
|
||||||
task = Tasks.FISH_SHIP_TRAP
|
task = Tasks.FISH_SHIP_TRAP
|
||||||
used = self.fishtraps
|
|
||||||
unused = self.unused_fishtraps
|
|
||||||
case _:
|
case _:
|
||||||
raise NotFarmEquivalentException
|
raise NotFarmEquivalentException
|
||||||
|
|
||||||
return task, building, used, unused
|
return task
|
||||||
|
|
||||||
|
|
||||||
def add_consumable_building(self, building):
|
def add_consumable_building(self, building):
|
||||||
task, _, __, unused = self._get_consumable_info(building)
|
task = self._get_task_from_consumable_building(building)
|
||||||
|
unused = self.consumable_buildings[task]['unused']
|
||||||
new_value = self.buildings_gather_increase[building][task]
|
new_value = self.buildings_gather_increase[building][task]
|
||||||
heapq.heappush(unused, new_value)
|
heapq.heappush(unused, new_value)
|
||||||
|
|
||||||
|
|
||||||
def remove_consumable_building(self, building):
|
def remove_consumable_building(self, building):
|
||||||
_, __, used, ___ = self._get_consumable_info(building)
|
task = self._get_task_from_consumable_building(building)
|
||||||
|
used = self.consumable_buildings[task]['used']
|
||||||
heapq.heappop(used)
|
heapq.heappop(used)
|
||||||
|
|
||||||
|
|
||||||
def _check_consumable_gather_building_available(self, world, task, count):
|
def _check_consumable_gather_building_available(self, world, task, count):
|
||||||
_, __, ___, unused = self._get_consumable_info(task)
|
unused = self.consumable_buildings[task]['unused']
|
||||||
if world.villager_tasks[task] + count > len(unused):
|
if world.villager_tasks[task] + count > len(unused):
|
||||||
raise GatherBuildingNotAvailableException
|
raise GatherBuildingNotAvailableException
|
||||||
|
|
||||||
|
|
||||||
def get_num_working_consumable_buildings(self, task):
|
def get_num_working_consumable_buildings(self, task):
|
||||||
_, __, used, ___ = self._get_consumable_info(task)
|
working = len(self.consumable_buildings[task]['used'])
|
||||||
working = len(used)
|
|
||||||
return working
|
return working
|
||||||
|
|
||||||
|
|
||||||
@@ -371,8 +357,8 @@ class Gather():
|
|||||||
|
|
||||||
rate = self.gather_rates[task]
|
rate = self.gather_rates[task]
|
||||||
|
|
||||||
if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if task in Gather.FARM_LIKE_TASKS:
|
||||||
_, __, used, unused = self._get_consumable_info(task)
|
used, unused = self.consumable_buildings[task].values()
|
||||||
for i in range(len(used)):
|
for i in range(len(used)):
|
||||||
used[i] -= rate*duration
|
used[i] -= rate*duration
|
||||||
else:
|
else:
|
||||||
@@ -391,10 +377,8 @@ class Gather():
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.resources = [200, 200, 100, 200]
|
self.resources = [200, 200, 100, 200]
|
||||||
|
|
||||||
self.farms = []
|
self.consumable_buildings = {t: {'used': [], 'unused': []}
|
||||||
self.unused_farms = []
|
for t in Gather.FARM_LIKE_TASKS}
|
||||||
self.fishtraps = []
|
|
||||||
self.unused_fishtraps = []
|
|
||||||
|
|
||||||
self.gather_rates = {
|
self.gather_rates = {
|
||||||
Tasks.BERRIES: 18.6/60,
|
Tasks.BERRIES: 18.6/60,
|
||||||
|
|||||||
10
aoe/world.py
10
aoe/world.py
@@ -43,8 +43,6 @@ class World():
|
|||||||
# to farms with the highest capacity.
|
# to farms with the highest capacity.
|
||||||
# We keep track of only the closest upcoming farm deletion.
|
# We keep track of only the closest upcoming farm deletion.
|
||||||
|
|
||||||
# TODO make used* and unused* into single dict w/ task as key
|
|
||||||
# TODO gather.consumable_building_tasks
|
|
||||||
# TODO require market to use market buy/sell
|
# TODO require market to use market buy/sell
|
||||||
# TODO what happens if you take away a builder while building?
|
# TODO what happens if you take away a builder while building?
|
||||||
# TODO allow automation:
|
# TODO allow automation:
|
||||||
@@ -122,7 +120,7 @@ class World():
|
|||||||
|
|
||||||
if next_decrease is not None:
|
if next_decrease is not None:
|
||||||
next_decrease += self.current_time
|
next_decrease += self.current_time
|
||||||
if task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if task in Gather.FARM_LIKE_TASKS:
|
||||||
self.events[next_decrease].append((self._remove_consumable_building,
|
self.events[next_decrease].append((self._remove_consumable_building,
|
||||||
(task,)))
|
(task,)))
|
||||||
else:
|
else:
|
||||||
@@ -194,10 +192,10 @@ class World():
|
|||||||
|
|
||||||
self.villager_tasks[previous_task] -= count
|
self.villager_tasks[previous_task] -= count
|
||||||
|
|
||||||
if previous_task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if previous_task in Gather.FARM_LIKE_TASKS:
|
||||||
self.gather.stop_using_consumable_buildings(previous_task, count)
|
self.gather.stop_using_consumable_buildings(previous_task, count)
|
||||||
|
|
||||||
if new_task in [Tasks.FARM, Tasks.FISH_SHIP_TRAP]:
|
if new_task in Gather.FARM_LIKE_TASKS:
|
||||||
self.gather.start_using_consumable_buildings(new_task, count)
|
self.gather.start_using_consumable_buildings(new_task, count)
|
||||||
|
|
||||||
self._schedule_next_gather_rate_decrease(previous_task)
|
self._schedule_next_gather_rate_decrease(previous_task)
|
||||||
@@ -318,7 +316,7 @@ class World():
|
|||||||
if building == Buildings.HOUSE:
|
if building == Buildings.HOUSE:
|
||||||
self._free_housing()
|
self._free_housing()
|
||||||
|
|
||||||
if building in [Buildings.FARM, Buildings.FISHTRAP]:
|
if building in Gather.FARM_LIKE_BUILDINGS:
|
||||||
self.gather.add_consumable_building(building)
|
self.gather.add_consumable_building(building)
|
||||||
else:
|
else:
|
||||||
self._improve_gather_rate_by_building(building, tasks_improved)
|
self._improve_gather_rate_by_building(building, tasks_improved)
|
||||||
|
|||||||
@@ -512,8 +512,10 @@ class TestWorld(unittest.TestCase):
|
|||||||
new_food = 500 + amount_per_farm
|
new_food = 500 + amount_per_farm
|
||||||
|
|
||||||
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
||||||
self.assertEqual(0, len(game.world.gather.farms))
|
|
||||||
self.assertEqual(0, len(game.world.gather.unused_farms))
|
farms = game.world.gather.consumable_buildings[Tasks.FARM]
|
||||||
|
self.assertEqual(0, len(farms['used']))
|
||||||
|
self.assertEqual(0, len(farms['unused']))
|
||||||
|
|
||||||
|
|
||||||
def test_farm_too_many_farmers(self):
|
def test_farm_too_many_farmers(self):
|
||||||
@@ -637,8 +639,10 @@ class TestWorld(unittest.TestCase):
|
|||||||
new_food = 500 + amount_per_fishtrap
|
new_food = 500 + amount_per_fishtrap
|
||||||
|
|
||||||
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
||||||
self.assertEqual(0, len(game.world.gather.fishtraps))
|
|
||||||
self.assertEqual(0, len(game.world.gather.unused_fishtraps))
|
traps = game.world.gather.consumable_buildings[Tasks.FISH_SHIP_TRAP]
|
||||||
|
self.assertEqual(0, len(traps['used']))
|
||||||
|
self.assertEqual(0, len(traps['unused']))
|
||||||
|
|
||||||
# fishing ships go back to fishing after trap is depleted
|
# fishing ships go back to fishing after trap is depleted
|
||||||
deep_rate_per_60 = 29.4
|
deep_rate_per_60 = 29.4
|
||||||
|
|||||||
Reference in New Issue
Block a user