Allow user to choose whether getting housed is an error

Previously, the simulation would automatically end whenever it
determined you were housed.  That is still the default, but the user can
now choose to have the simulation continue instead.  Also, this likely
fixes a bug whereby an error could have been thrown for an unavailable
production building when searching the production queue.
This commit is contained in:
Nate Bowman
2026-11-12 00:51:32 -05:00
parent 20d4f5bd6c
commit 8865950678
5 changed files with 81 additions and 5 deletions

View File

@@ -102,6 +102,12 @@ class Gameplay():
(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,
action_time,
previous_task,

View File

@@ -17,6 +17,15 @@ class Housing():
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):
self.space = defaultdict(int, {
Buildings.HOUSE: 5,

View File

@@ -14,6 +14,15 @@ class Production():
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
def __init__(self):
self.building = defaultdict(None, {

View File

@@ -248,6 +248,14 @@ class World():
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):
if task in Gather.FISHING_TASKS:
return Tasks.FISH_SHIP_BUILD
@@ -382,6 +390,10 @@ class World():
self._handle_events()
def set_housed_is_error(self, is_error=True):
self.housed_is_error = is_error
def get_population_space(self):
space = sum([self.housing.space[building] for building in self.buildings])
return min(space, World.POPULATION_CAP)
@@ -394,13 +406,15 @@ class World():
def _free_housing(self):
pop_space = self.get_population_space()
house_check = self._get_house_check_function()
i = 0
while (self.get_effective_population() < pop_space and
i < len(self.production_queue)):
thing, task = self.production_queue[i]
if self.housing.check_housing(self, thing) and \
self.production.check_production(self, thing):
if house_check(self, thing) and \
self.production.check_production_safe(self, thing):
self._start_producing(thing, task)
del self.production_queue[i]
else:
@@ -421,11 +435,13 @@ class World():
# production queue as a shared pool, and I don't really care.
self.production_available[building] += 1
house_check = self._get_house_check_function()
i = 0
while i < len(self.production_queue):
thing, task = self.production_queue[i]
if self.production.building[thing] == building and \
self.housing.check_housing(self, thing):
house_check(self, thing):
self._start_producing(thing, task)
del self.production_queue[i]
break
@@ -487,8 +503,14 @@ class World():
def _start_producing(self, thing, initial_task):
try:
self._use_housing_and_production(thing)
except ProductionBuildingNotAvailableException:
self.production_queue.append((thing, initial_task))
except Exception as e:
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:
update_time = self.current_time + self.build_times.build_times[thing]
self.events[update_time].append((self._add_thing, (thing, initial_task,)))
@@ -594,4 +616,6 @@ class World():
self.production = Production()
self.gather = Gather()
self.housed_is_error = True
self._deal_with_civ()

View File

@@ -202,6 +202,34 @@ class TestWorld(unittest.TestCase):
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):
game = Gameplay()
game.assign_villager(0, Tasks.IDLE, Tasks.BOAR, 1)