Compare commits
2 Commits
20d4f5bd6c
...
e8c6e8eff8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8c6e8eff8 | ||
|
|
8865950678 |
@@ -102,6 +102,12 @@ class Gameplay():
|
|||||||
(walking_time,)))
|
(walking_time,)))
|
||||||
|
|
||||||
|
|
||||||
|
def set_housed_is_error(self, action_time, is_error=True):
|
||||||
|
self._check_time(action_time)
|
||||||
|
self.actions[action_time].append((self.world.set_housed_is_error,
|
||||||
|
(is_error,)))
|
||||||
|
|
||||||
|
|
||||||
def lure_huntable_with_villager(self,
|
def lure_huntable_with_villager(self,
|
||||||
action_time,
|
action_time,
|
||||||
previous_task,
|
previous_task,
|
||||||
|
|||||||
@@ -17,6 +17,15 @@ class Housing():
|
|||||||
raise YallGotHousedException
|
raise YallGotHousedException
|
||||||
|
|
||||||
|
|
||||||
|
def check_housing_safe(self, world, thing):
|
||||||
|
try:
|
||||||
|
self.check_housing(world, thing)
|
||||||
|
except YallGotHousedException:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.space = defaultdict(int, {
|
self.space = defaultdict(int, {
|
||||||
Buildings.HOUSE: 5,
|
Buildings.HOUSE: 5,
|
||||||
|
|||||||
@@ -14,6 +14,15 @@ class Production():
|
|||||||
raise ProductionBuildingNotAvailableException
|
raise ProductionBuildingNotAvailableException
|
||||||
|
|
||||||
|
|
||||||
|
def check_production_safe(self, world, thing):
|
||||||
|
try:
|
||||||
|
self.check_production(world, thing)
|
||||||
|
except ProductionBuildingNotAvailableException:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
# assumes for now each unit can be built in only one type of building
|
# assumes for now each unit can be built in only one type of building
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.building = defaultdict(None, {
|
self.building = defaultdict(None, {
|
||||||
|
|||||||
34
aoe/world.py
34
aoe/world.py
@@ -248,6 +248,14 @@ class World():
|
|||||||
self.assign_villager(task, build_task, count)
|
self.assign_villager(task, build_task, count)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_house_check_function(self):
|
||||||
|
if self.housed_is_error:
|
||||||
|
house_check = self.housing.check_housing
|
||||||
|
else:
|
||||||
|
house_check = self.housing.check_housing_safe
|
||||||
|
return house_check
|
||||||
|
|
||||||
|
|
||||||
def _get_build_task_from_task(self, task):
|
def _get_build_task_from_task(self, task):
|
||||||
if task in Gather.FISHING_TASKS:
|
if task in Gather.FISHING_TASKS:
|
||||||
return Tasks.FISH_SHIP_BUILD
|
return Tasks.FISH_SHIP_BUILD
|
||||||
@@ -382,6 +390,10 @@ class World():
|
|||||||
self._handle_events()
|
self._handle_events()
|
||||||
|
|
||||||
|
|
||||||
|
def set_housed_is_error(self, is_error=True):
|
||||||
|
self.housed_is_error = is_error
|
||||||
|
|
||||||
|
|
||||||
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, World.POPULATION_CAP)
|
return min(space, World.POPULATION_CAP)
|
||||||
@@ -394,13 +406,15 @@ class World():
|
|||||||
def _free_housing(self):
|
def _free_housing(self):
|
||||||
pop_space = self.get_population_space()
|
pop_space = self.get_population_space()
|
||||||
|
|
||||||
|
house_check = self._get_house_check_function()
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while (self.get_effective_population() < pop_space and
|
while (self.get_effective_population() < pop_space and
|
||||||
i < len(self.production_queue)):
|
i < len(self.production_queue)):
|
||||||
|
|
||||||
thing, task = self.production_queue[i]
|
thing, task = self.production_queue[i]
|
||||||
if self.housing.check_housing(self, thing) and \
|
if house_check(self, thing) and \
|
||||||
self.production.check_production(self, thing):
|
self.production.check_production_safe(self, thing):
|
||||||
self._start_producing(thing, task)
|
self._start_producing(thing, task)
|
||||||
del self.production_queue[i]
|
del self.production_queue[i]
|
||||||
else:
|
else:
|
||||||
@@ -421,11 +435,13 @@ class World():
|
|||||||
# production queue as a shared pool, and I don't really care.
|
# production queue as a shared pool, and I don't really care.
|
||||||
self.production_available[building] += 1
|
self.production_available[building] += 1
|
||||||
|
|
||||||
|
house_check = self._get_house_check_function()
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(self.production_queue):
|
while i < len(self.production_queue):
|
||||||
thing, task = self.production_queue[i]
|
thing, task = self.production_queue[i]
|
||||||
if self.production.building[thing] == building and \
|
if self.production.building[thing] == building and \
|
||||||
self.housing.check_housing(self, thing):
|
house_check(self, thing):
|
||||||
self._start_producing(thing, task)
|
self._start_producing(thing, task)
|
||||||
del self.production_queue[i]
|
del self.production_queue[i]
|
||||||
break
|
break
|
||||||
@@ -487,8 +503,14 @@ class World():
|
|||||||
def _start_producing(self, thing, initial_task):
|
def _start_producing(self, thing, initial_task):
|
||||||
try:
|
try:
|
||||||
self._use_housing_and_production(thing)
|
self._use_housing_and_production(thing)
|
||||||
except ProductionBuildingNotAvailableException:
|
except Exception as e:
|
||||||
self.production_queue.append((thing, initial_task))
|
is_prod = isinstance(e, ProductionBuildingNotAvailableException)
|
||||||
|
is_housing = isinstance(e, YallGotHousedException)
|
||||||
|
|
||||||
|
if is_prod or (is_housing and not self.housed_is_error):
|
||||||
|
self.production_queue.append((thing, initial_task))
|
||||||
|
else:
|
||||||
|
raise(e)
|
||||||
else:
|
else:
|
||||||
update_time = self.current_time + self.build_times.build_times[thing]
|
update_time = self.current_time + self.build_times.build_times[thing]
|
||||||
self.events[update_time].append((self._add_thing, (thing, initial_task,)))
|
self.events[update_time].append((self._add_thing, (thing, initial_task,)))
|
||||||
@@ -594,4 +616,6 @@ class World():
|
|||||||
self.production = Production()
|
self.production = Production()
|
||||||
self.gather = Gather()
|
self.gather = Gather()
|
||||||
|
|
||||||
|
self.housed_is_error = True
|
||||||
|
|
||||||
self._deal_with_civ()
|
self._deal_with_civ()
|
||||||
|
|||||||
@@ -202,6 +202,34 @@ class TestWorld(unittest.TestCase):
|
|||||||
game.play_until(25)
|
game.play_until(25)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_housed_no_error(self):
|
||||||
|
game = Gameplay()
|
||||||
|
game.queue_villager(0, Tasks.IDLE)
|
||||||
|
game.queue_villager(0, Tasks.IDLE)
|
||||||
|
game.queue_villager(0, Tasks.IDLE)
|
||||||
|
|
||||||
|
game.set_housed_is_error(24, False)
|
||||||
|
game.play_until(100)
|
||||||
|
self.assertEqual(5, game.world.population)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_housed_toggle_error(self):
|
||||||
|
game = Gameplay()
|
||||||
|
game.world.gather.resources = [1000, 1000, 1000, 1000]
|
||||||
|
|
||||||
|
game.queue_villager(0, Tasks.IDLE)
|
||||||
|
game.queue_villager(0, Tasks.IDLE)
|
||||||
|
|
||||||
|
game.set_housed_is_error(24, False)
|
||||||
|
game.play_until(80)
|
||||||
|
|
||||||
|
game.set_housed_is_error(81, True)
|
||||||
|
game.queue_villager(82, Tasks.IDLE)
|
||||||
|
|
||||||
|
with self.assertRaises(YallGotHousedException):
|
||||||
|
game.play_until(85)
|
||||||
|
|
||||||
|
|
||||||
def test_gather_boar_without_lure(self):
|
def test_gather_boar_without_lure(self):
|
||||||
game = Gameplay()
|
game = Gameplay()
|
||||||
game.assign_villager(0, Tasks.IDLE, Tasks.BOAR, 1)
|
game.assign_villager(0, Tasks.IDLE, Tasks.BOAR, 1)
|
||||||
@@ -267,7 +295,7 @@ class TestWorld(unittest.TestCase):
|
|||||||
game = Gameplay()
|
game = Gameplay()
|
||||||
|
|
||||||
game.world.age = Ages.CASTLE
|
game.world.age = Ages.CASTLE
|
||||||
game.world.resources = [1000, 1000, 1000, 1000]
|
game.world.gather.resources = [1000, 1000, 1000, 1000]
|
||||||
game.world._add_building(Buildings.MONASTERY, {}, {})
|
game.world._add_building(Buildings.MONASTERY, {}, {})
|
||||||
|
|
||||||
game.queue_unit(0, Units.MONK)
|
game.queue_unit(0, Units.MONK)
|
||||||
|
|||||||
Reference in New Issue
Block a user