Compare commits

...

2 Commits

Author SHA1 Message Date
Nate Bowman
0fa68c6e32 Fix bug in gather rate decrease scheduling
Variable next_decrease can be 0 if the decrease should happen right now,
which caused the previous conditional to evaluate as False when it
should not have
2026-11-05 23:01:52 -05:00
Nate Bowman
c74fe5e451 Add some basic tests 2026-11-05 23:00:05 -05:00
2 changed files with 182 additions and 1 deletions

View File

@@ -86,7 +86,7 @@ class World():
next_decrease = self.gather.compute_next_gather_rate_decrease(task, next_decrease = self.gather.compute_next_gather_rate_decrease(task,
self.villager_tasks[task]) self.villager_tasks[task])
if next_decrease: if next_decrease is not None:
next_decrease += self.current_time next_decrease += self.current_time
if task == Tasks.FARM: if task == Tasks.FARM:
self.events[next_decrease].append((self._remove_farm, ())) self.events[next_decrease].append((self._remove_farm, ()))

181
tests/test.py Normal file
View File

@@ -0,0 +1,181 @@
# Run from root of repo with `python -m unittest tests/test.py`
import unittest
from aoe.exceptions import *
from aoe.types import *
from aoe.gameplay import *
class TestWorld(unittest.TestCase):
@staticmethod
def equal_to_tolerance(v1, v2, atol):
return (abs(v1 - v2) <= atol)
def assertResourcesMatch(self, expected, world):
CLOSE_ENOUGH = 1
for i in range(len(expected)):
small_diff = TestWorld.equal_to_tolerance(expected[i],
world.gather.resources[i],
CLOSE_ENOUGH)
self.assertTrue(small_diff,
f'Resource {i}: expected {expected[i]} but had '
f'{world.gather.resources[i]}')
def test_initial_world(self):
game = Gameplay()
self.assertResourcesMatch([200, 200, 100, 200], game.world)
def test_idle_start(self):
game = Gameplay()
game.play_until(60)
self.assertResourcesMatch([200, 200, 100, 200], game.world)
def test_build_villager_not_complete(self):
game = Gameplay()
game.queue_villager(5, Tasks.IDLE)
game.play_until(5 + 20)
self.assertEqual(4, game.world.population)
self.assertResourcesMatch([150, 200, 100, 200], game.world)
def test_build_villager(self):
game = Gameplay()
game.queue_villager(5, Tasks.IDLE)
game.play_until(5 + 25)
self.assertEqual(5, game.world.population)
self.assertResourcesMatch([150, 200, 100, 200], game.world)
def test_build_villager_leave_idle(self):
game = Gameplay()
game.queue_villager(5, Tasks.IDLE)
game.play_until(5 + 25 + 30)
self.assertEqual(5, game.world.population)
self.assertResourcesMatch([150, 200, 100, 200], game.world)
def test_wood_no_camp(self):
game = Gameplay()
game.queue_villager(5, Tasks.WOOD)
game.play_until(5 + 25 + 60)
wood_rate = 23.4/60
wood_rate_decrease = 0.85
slow_wood_rate_times_60 = 23.4*wood_rate_decrease
new_wood = 200 + slow_wood_rate_times_60
self.assertResourcesMatch([150, new_wood, 100, 200], game.world)
def test_good_villager_task_assign(self):
game = Gameplay()
game.assign_villager(5, Tasks.IDLE, Tasks.SHEEP)
game.play_until(5)
def test_bad_villager_task_assign(self):
game = Gameplay()
game.assign_villager(5, Tasks.SHEEP, Tasks.WOOD)
with self.assertRaises(IllegalVillagerMoveException):
game.play_until(5)
def test_assign_several_villagers(self):
num_villagers = 3
game = Gameplay()
game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP, num_villagers)
game.play_until(60)
sheep_rate_per_60 = 19.8
gathered = sheep_rate_per_60*num_villagers
self.assertResourcesMatch([200 + gathered, 200, 100, 200], game.world)
def test_change_past(self):
game = Gameplay()
game.play_until(60)
with self.assertRaises(CannotChangeThePastException):
game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP)
def test_build_lumber_camp_one_villager(self):
lc_build_time = 35
game = Gameplay()
game.build(0, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
game.play_until(20)
self.assertTrue(Buildings.LUMBERCAMP not in game.world.buildings)
game.play_until(lc_build_time)
self.assertTrue(Buildings.LUMBERCAMP in game.world.buildings)
def test_building_adds_gatherable(self):
lc_build_time = 35
game = Gameplay()
gatherable = game.world.gather.gatherable_amounts[Tasks.WOOD]
self.assertEqual(0, gatherable)
game.build(0, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
gatherable = game.world.gather.gatherable_amounts[Tasks.WOOD]
self.assertEqual(0, gatherable)
game.play_until(lc_build_time)
gatherable = game.world.gather.gatherable_amounts[Tasks.WOOD]
self.assertTrue(gatherable > 0)
def test_bad_build_villager_move(self):
game = Gameplay()
game.build(0, Buildings.LUMBERCAMP, Tasks.WOOD, Tasks.IDLE)
with self.assertRaises(IllegalVillagerMoveException):
game.play_until(1)
def test_bad_build_conservation_villagers(self):
game = Gameplay()
game.build(0, Buildings.LUMBERCAMP, Tasks.WOOD, [Tasks.IDLE]*2)
with self.assertRaises(ConservationOfVillagersException):
game.play_until(1)
def test_bad_build_resources(self):
game = Gameplay()
game.build(0, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
game.build(0, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
game.build(0, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
with self.assertRaises(NotEnoughResourcesException):
game.play_until(1)
def test_bad_build_age(self):
game = Gameplay()
game.build(0, Buildings.BLACKSMITH, Tasks.IDLE, Tasks.IDLE)
with self.assertRaises(MissingDependencyException):
game.play_until(1)
def test_bad_build_dependencies(self):
game = Gameplay()
game.build(0, Buildings.FARM, Tasks.IDLE, Tasks.IDLE)
with self.assertRaises(MissingDependencyException):
game.play_until(1)