Allow hunting and building as initial villager tasks

This commit is contained in:
Nate Bowman
2026-11-15 16:29:13 -05:00
parent e4aaba0320
commit 8a8effe3d1
3 changed files with 134 additions and 41 deletions

View File

@@ -34,78 +34,99 @@ class Gameplay():
self.actions[action_time].append((self.world.build, self.actions[action_time].append((self.world.build,
(building, (building,
villagers_from, villagers_from,
villagers_to,))) villagers_to,),
{}))
def market_buy(self, action_time, resource, amount): def market_buy(self, action_time, resource, amount):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.market_buy, self.actions[action_time].append((self.world.market_buy,
(resource, amount))) (resource, amount),
{}))
def market_sell(self, action_time, resource, amount): def market_sell(self, action_time, resource, amount):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.market_sell, self.actions[action_time].append((self.world.market_sell,
(resource, amount))) (resource, amount),
{}))
def queue_research(self, action_time, research): def queue_research(self, action_time, research):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_research, self.actions[action_time].append((self.world.queue_research,
(research,))) (research,),
{}))
def queue_villagers_over_time(self, from_time, until_time, initial_task): def queue_villagers_over_time(self,
from_time,
until_time,
initial_task,
**kwargs):
train_time = self.world.build_times.build_times[Units.VILLAGER] train_time = self.world.build_times.build_times[Units.VILLAGER]
duration = max(0, until_time - from_time) duration = max(0, until_time - from_time)
for i in range(duration//train_time + 1): for i in range(duration//train_time + 1):
self.queue_villager(from_time + i*train_time, initial_task) self.queue_villager(from_time + i*train_time,
initial_task,
**kwargs)
def queue_villager(self, action_time, initial_task): def queue_villager(self, action_time, initial_task, **kwargs):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_villager, self.actions[action_time].append((self.world.queue_villager,
(initial_task,))) (initial_task,),
kwargs))
def queue_fishing_ship(self, action_time, initial_task=Tasks.FISH_SHIP_DEEP): def queue_fishing_ship(self, action_time, initial_task=Tasks.FISH_SHIP_DEEP):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_fishing_ship, self.actions[action_time].append((self.world.queue_fishing_ship,
(initial_task,))) (initial_task,),
{}))
def queue_trade_cog(self, action_time): def queue_trade_cog(self, action_time):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_trade_cog, ())) self.actions[action_time].append((self.world.queue_trade_cog,
(),
{}))
def queue_trade_cart(self, action_time): def queue_trade_cart(self, action_time):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_trade_cart, ())) self.actions[action_time].append((self.world.queue_trade_cart,
(),
{}))
def queue_unit(self, action_time, unit): def queue_unit(self, action_time, unit):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_unit, (unit,))) self.actions[action_time].append((self.world.queue_unit,
(unit,),
{}))
def assign_villager(self, action_time, old_task, new_task, count=1): def assign_villager(self, action_time, old_task, new_task, count=1):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.assign_villager, self.actions[action_time].append((self.world.assign_villager,
(old_task, new_task, count,))) (old_task, new_task, count,),
{}))
def get_relic(self, action_time, walking_time=60): def get_relic(self, action_time, walking_time=60):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.get_relic, self.actions[action_time].append((self.world.get_relic,
(walking_time,))) (walking_time,),
{}))
def set_housed_is_error(self, action_time, is_error=True): def set_housed_is_error(self, action_time, is_error=True):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.set_housed_is_error, self.actions[action_time].append((self.world.set_housed_is_error,
(is_error,))) (is_error,),
{}))
def lure_huntable_with_villager(self, def lure_huntable_with_villager(self,
@@ -114,12 +135,15 @@ class Gameplay():
walking_time=30): walking_time=30):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.lure_huntable_with_villager, self.actions[action_time].append((self.world.lure_huntable_with_villager,
(previous_task, walking_time))) (previous_task, walking_time),
{}))
def increase_age(self, action_time): def increase_age(self, action_time):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.increase_age, ())) self.actions[action_time].append((self.world.increase_age,
(),
{}))
def _check_time(self, action_time): def _check_time(self, action_time):
@@ -150,12 +174,12 @@ class Gameplay():
def _handle_actions(self): def _handle_actions(self):
for func, args in self.actions[self.world.current_time]: for func, args, kwargs in self.actions[self.world.current_time]:
try: try:
func(*args) func(*args, **kwargs)
except Exception as e: except Exception as e:
raise type(e)( raise type(e)(
f'Error in {func.__name__}{args}' f'Error in {func.__name__}{args}{kwargs}'
f' at time {self.world.current_time}.') from e f' at time {self.world.current_time}.') from e
del self.actions[self.world.current_time] del self.actions[self.world.current_time]

View File

@@ -128,10 +128,12 @@ class World():
next_decrease += self.current_time next_decrease += self.current_time
if task in Gather.FARM_LIKE_TASKS: 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:
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 self.rate_decrease_event_times[task] = next_decrease
@@ -170,7 +172,7 @@ class World():
def lure_huntable_with_villager(self, previous_task, walking_time=30): def lure_huntable_with_villager(self, previous_task, walking_time=30):
update_time = self.current_time + walking_time update_time = self.current_time + walking_time
self.events[update_time].append((self._add_huntable, ())) self.events[update_time].append((self._add_huntable, (), {}))
self.assign_villager(previous_task, Tasks.LURE) self.assign_villager(previous_task, Tasks.LURE)
@@ -273,7 +275,7 @@ class World():
update_time = self.current_time + walking_time update_time = self.current_time + walking_time
self.events[update_time].append((self._add_relic, ())) self.events[update_time].append((self._add_relic, (), {}))
def _add_relic(self): def _add_relic(self):
@@ -320,7 +322,8 @@ class World():
self.events[update_time].append((self._add_building, self.events[update_time].append((self._add_building,
(building, (building,
villagers_to_count, villagers_to_count,
building_tasks,))) building_tasks,),
{}))
def _add_building(self, building, villagers_to, tasks_improved): def _add_building(self, building, villagers_to, tasks_improved):
@@ -421,12 +424,12 @@ class World():
i += 1 i += 1
def _free_production_building_of_thing(self, thing): def _free_production_building_of_thing(self, thing, **kwargs):
building = self.production.building[thing] building = self.production.building[thing]
self._free_production_building(building) self._free_production_building(building)
def _free_production_building(self, building): def _free_production_building(self, building, **kwargs):
# This will not respect the order of a queue in a particular building. # This will not respect the order of a queue in a particular building.
# For example, if there is a TC with the queue [villager, loom] and # For example, if there is a TC with the queue [villager, loom] and
# there is no available housing, the TC will research loom. In the # there is no available housing, the TC will research loom. In the
@@ -439,10 +442,10 @@ class World():
i = 0 i = 0
while i < len(self.production_queue): while i < len(self.production_queue):
thing, task = self.production_queue[i] thing, task, kwargs = self.production_queue[i]
if self.production.building[thing] == building and \ if self.production.building[thing] == building and \
house_check(self, thing): house_check(self, thing):
self._start_producing(thing, task) self._start_producing(thing, task, **kwargs)
del self.production_queue[i] del self.production_queue[i]
break break
i += 1 i += 1
@@ -471,7 +474,7 @@ class World():
self._free_housing() self._free_housing()
def _add_thing(self, thing, initial_task): def _add_thing(self, thing, initial_task, **kwargs):
self.units_being_built -= self.housing.required[thing] self.units_being_built -= self.housing.required[thing]
self.population += self.housing.required[thing] self.population += self.housing.required[thing]
@@ -486,7 +489,18 @@ class World():
self.villager_tasks[Tasks.IDLE] += 1 self.villager_tasks[Tasks.IDLE] += 1
if not initial_task: if not initial_task:
initial_task = Tasks.IDLE initial_task = Tasks.IDLE
self.assign_villager(Tasks.IDLE, initial_task)
if initial_task == Tasks.LURE:
self.lure_huntable_with_villager(Tasks.IDLE, **kwargs)
elif initial_task == Tasks.BUILD:
if 'building_task' not in kwargs.keys():
kwargs['building_task'] = None
self.build(kwargs['building'],
Tasks.IDLE,
kwargs['villagers_to'],
kwargs['building_task'])
else:
self.assign_villager(Tasks.IDLE, initial_task)
elif thing == Units.MONK: elif thing == Units.MONK:
self.villager_tasks[Tasks.MONK_IDLE] += 1 self.villager_tasks[Tasks.MONK_IDLE] += 1
self._get_next_waiting_relic() self._get_next_waiting_relic()
@@ -500,7 +514,7 @@ class World():
self.villager_tasks[Tasks.TRADECART] += 1 self.villager_tasks[Tasks.TRADECART] += 1
def _start_producing(self, thing, initial_task): def _start_producing(self, thing, initial_task, **kwargs):
try: try:
self._use_housing_and_production(thing) self._use_housing_and_production(thing)
except Exception as e: except Exception as e:
@@ -508,20 +522,22 @@ class World():
is_housing = isinstance(e, YallGotHousedException) is_housing = isinstance(e, YallGotHousedException)
if is_prod or (is_housing and not self.housed_is_error): if is_prod or (is_housing and not self.housed_is_error):
self.production_queue.append((thing, initial_task)) self.production_queue.append((thing, initial_task, kwargs))
else: else:
raise(e) 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,),
kwargs))
def _queue_thing(self, thing, initial_task=None): def _queue_thing(self, thing, initial_task=None, **kwargs):
self.dependencies.check_dependencies(self, thing) self.dependencies.check_dependencies(self, thing)
self.costs.compute_cost(thing) self.costs.compute_cost(thing)
self.pay_cost(thing) self.pay_cost(thing)
self._start_producing(thing, initial_task) self._start_producing(thing, initial_task, **kwargs)
def queue_research(self, research): def queue_research(self, research):
@@ -532,8 +548,8 @@ class World():
self._queue_thing(unit) self._queue_thing(unit)
def queue_villager(self, initial_task): def queue_villager(self, initial_task, **kwargs):
self._queue_thing(Units.VILLAGER, initial_task) self._queue_thing(Units.VILLAGER, initial_task, **kwargs)
def queue_fishing_ship(self, initial_task=Tasks.FISH_SHIP_DEEP): def queue_fishing_ship(self, initial_task=Tasks.FISH_SHIP_DEEP):
@@ -549,12 +565,13 @@ class World():
def _handle_events(self): def _handle_events(self):
for func, args in self.events[self.current_time]: for func, args, kwargs in self.events[self.current_time]:
try: try:
func(*args) func(*args, **kwargs)
except Exception as e: except Exception as e:
raise type(e)( raise type(e)(
f'Error in {func.__name__}{args}' f'Error in {func.__name__}{args}'
f' with keyword arguments {kwargs}'
f' at time {self.current_time}.' f' at time {self.current_time}.'
f' Note that the actual gameplay command that caused' f' Note that the actual gameplay command that caused'
f' the error may not have been issued at' f' the error may not have been issued at'
@@ -579,7 +596,7 @@ class World():
self.pay_cost(new_age) self.pay_cost(new_age)
update_time = self.current_time + self.build_times.build_times[new_age] update_time = self.current_time + self.build_times.build_times[new_age]
self.events[update_time].append((self._update_age, (new_age,))) self.events[update_time].append((self._update_age, (new_age,), {}))
def _deal_with_civ(self): def _deal_with_civ(self):

View File

@@ -251,6 +251,32 @@ class TestWorld(unittest.TestCase):
self.assertResourcesMatch([200 + gathered, 200, 100, 200], game.world) self.assertResourcesMatch([200 + gathered, 200, 100, 200], game.world)
def test_gather_boar_initial_lure(self):
walking = 30
game = Gameplay()
game.queue_villager(0, Tasks.LURE)
game.play_until(25 + walking + 60)
boar_rate_times_60 = 24.6
gathered = boar_rate_times_60
self.assertResourcesMatch([150 + gathered, 200, 100, 200], game.world)
def test_gather_boar_initial_lure_custom_time(self):
walking = 80
game = Gameplay()
game.queue_villager(0, Tasks.LURE, walking_time=walking)
game.play_until(25 + walking + 60)
boar_rate_times_60 = 24.6
gathered = boar_rate_times_60
self.assertResourcesMatch([150 + gathered, 200, 100, 200], game.world)
def test_gather_boar_limited(self): def test_gather_boar_limited(self):
walking = 20 walking = 20
@@ -763,3 +789,29 @@ class TestWorld(unittest.TestCase):
self.assertEqual(4, len(game.world.buildings)) self.assertEqual(4, len(game.world.buildings))
self.assertResourcesMatch([200 + gathered, 50, 100, 200], game.world) self.assertResourcesMatch([200 + gathered, 50, 100, 200], game.world)
def test_initial_build(self):
game = Gameplay()
game.queue_villager(0,
Tasks.BUILD,
building=Buildings.HOUSE,
villagers_to=Tasks.SHEEP)
game.play_until(10)
self.assertEqual(1, len(game.world.buildings))
self.assertEqual(4, game.world.population)
game.play_until(30)
self.assertEqual(1, len(game.world.buildings))
self.assertEqual(5, game.world.population)
game.play_until(25 + 25 + 60)
self.assertEqual(2, len(game.world.buildings))
self.assertEqual(5, game.world.population)
sheep_rate_per_60 = 19.8
gathered = sheep_rate_per_60
self.assertResourcesMatch([150 + gathered, 175, 100, 200], game.world)