Compare commits
4 Commits
e8c6e8eff8
...
24a0e9e16b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24a0e9e16b | ||
|
|
7ce4b4a150 | ||
|
|
8a8effe3d1 | ||
|
|
e4aaba0320 |
@@ -63,8 +63,9 @@ class Gameplay():
|
|||||||
self.queue_villager(from_time + i*train_time, initial_task)
|
self.queue_villager(from_time + i*train_time, initial_task)
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
initial_task = (initial_task, kwargs)
|
||||||
self.actions[action_time].append((self.world.queue_villager,
|
self.actions[action_time].append((self.world.queue_villager,
|
||||||
(initial_task,)))
|
(initial_task,)))
|
||||||
|
|
||||||
@@ -127,6 +128,15 @@ class Gameplay():
|
|||||||
raise CannotChangeThePastException
|
raise CannotChangeThePastException
|
||||||
|
|
||||||
|
|
||||||
|
def _get_last_action_time(self):
|
||||||
|
try:
|
||||||
|
last_action_time = max(self.actions.keys())
|
||||||
|
except ValueError:
|
||||||
|
last_action_time = self.world.current_time
|
||||||
|
|
||||||
|
return last_action_time
|
||||||
|
|
||||||
|
|
||||||
def _get_next_action_time(self):
|
def _get_next_action_time(self):
|
||||||
try:
|
try:
|
||||||
next_action_time = min(self.actions.keys())
|
next_action_time = min(self.actions.keys())
|
||||||
@@ -159,6 +169,11 @@ class Gameplay():
|
|||||||
self._handle_actions()
|
self._handle_actions()
|
||||||
|
|
||||||
|
|
||||||
|
def play_until_last_action(self, extra_time=0):
|
||||||
|
last_action_time = self._get_last_action_time()
|
||||||
|
self.play_until(last_action_time + extra_time)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.world = World()
|
self.world = World()
|
||||||
self.actions = defaultdict(list)
|
self.actions = defaultdict(list)
|
||||||
|
|||||||
28
aoe/world.py
28
aoe/world.py
@@ -484,8 +484,19 @@ class World():
|
|||||||
if thing == Units.VILLAGER:
|
if thing == Units.VILLAGER:
|
||||||
self.villagers += 1
|
self.villagers += 1
|
||||||
self.villager_tasks[Tasks.IDLE] += 1
|
self.villager_tasks[Tasks.IDLE] += 1
|
||||||
if not initial_task:
|
|
||||||
initial_task = Tasks.IDLE
|
initial_task, kwargs = self._parse_initial_task(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)
|
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
|
||||||
@@ -548,6 +559,19 @@ class World():
|
|||||||
self._queue_thing(Units.TRADECART)
|
self._queue_thing(Units.TRADECART)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_initial_task(self, initial_task):
|
||||||
|
kwargs = {}
|
||||||
|
|
||||||
|
if isinstance(initial_task, tuple):
|
||||||
|
kwargs = initial_task[1]
|
||||||
|
initial_task = initial_task[0]
|
||||||
|
|
||||||
|
if not initial_task:
|
||||||
|
initial_task = Tasks.IDLE
|
||||||
|
|
||||||
|
return initial_task, kwargs
|
||||||
|
|
||||||
|
|
||||||
def _handle_events(self):
|
def _handle_events(self):
|
||||||
for func, args in self.events[self.current_time]:
|
for func, args in self.events[self.current_time]:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -740,3 +766,52 @@ class TestWorld(unittest.TestCase):
|
|||||||
new_food = 500 + rate_per_60
|
new_food = 500 + rate_per_60
|
||||||
|
|
||||||
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
||||||
|
|
||||||
|
|
||||||
|
def test_play_until_last_action(self):
|
||||||
|
game = Gameplay()
|
||||||
|
|
||||||
|
game.build(0, Buildings.HOUSE, [Tasks.IDLE]*2, [Tasks.IDLE]*2)
|
||||||
|
game.build(0, Buildings.HOUSE, Tasks.IDLE, Tasks.IDLE)
|
||||||
|
game.build(100, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
|
||||||
|
|
||||||
|
game.play_until_last_action()
|
||||||
|
|
||||||
|
self.assertEqual(3, len(game.world.buildings))
|
||||||
|
|
||||||
|
game.assign_villager(200, Tasks.IDLE, Tasks.SHEEP)
|
||||||
|
game.assign_villager(200, Tasks.IDLE, Tasks.SHEEP)
|
||||||
|
|
||||||
|
game.play_until_last_action(extra_time=60)
|
||||||
|
|
||||||
|
sheep_rate_per_60 = 19.8
|
||||||
|
gathered = sheep_rate_per_60*2
|
||||||
|
|
||||||
|
self.assertEqual(4, len(game.world.buildings))
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user