Compare commits
39 Commits
d51980290b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80ebc655de | ||
|
|
3f183cdbd5 | ||
|
|
dba4974e9f | ||
|
|
0780fec1fb | ||
|
|
1b9266ff09 | ||
|
|
3b07e6b3c5 | ||
|
|
2fc6b76db4 | ||
|
|
24a0e9e16b | ||
|
|
7ce4b4a150 | ||
|
|
8a8effe3d1 | ||
|
|
e4aaba0320 | ||
|
|
e8c6e8eff8 | ||
|
|
8865950678 | ||
|
|
20d4f5bd6c | ||
|
|
5a5c6e1d3a | ||
|
|
6680433770 | ||
|
|
9ccf25f0ab | ||
|
|
e94af31828 | ||
|
|
ec8b33ac00 | ||
|
|
c02aa3f736 | ||
|
|
e03911b78a | ||
|
|
c6254e6d1e | ||
|
|
5f1999caac | ||
|
|
4d9da78842 | ||
|
|
0d40fe5dc0 | ||
|
|
85e441927b | ||
|
|
61f98819bc | ||
|
|
dd6526fb09 | ||
|
|
d5c0d928b6 | ||
|
|
5d2178cd9c | ||
|
|
09d05e950b | ||
|
|
54050235f0 | ||
|
|
622bdc38fb | ||
|
|
e67085aab7 | ||
|
|
c6550fb007 | ||
|
|
9e45d182b0 | ||
|
|
1f19740355 | ||
|
|
0fa68c6e32 | ||
|
|
c74fe5e451 |
31
LICENSE.txt
Normal file
31
LICENSE.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
MIT NON-AI License
|
||||
|
||||
Copyright (c) 2026, Nathaniel Bowman
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of the software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions.
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
In addition, the following restrictions apply:
|
||||
|
||||
1. The Software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms,
|
||||
including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives,
|
||||
modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
|
||||
|
||||
2. The Software may not be included in any dataset used for training or improving machine learning algorithms,
|
||||
including but not limited to artificial intelligence, natural language processing, or data mining.
|
||||
|
||||
3. Any person or organization found to be in violation of these restrictions will be subject to legal action and may be held liable
|
||||
for any damages resulting from such use.
|
||||
|
||||
4. This software repository contains open-source software licenses that can be modified and used to derive other software licenses.
|
||||
However, no derivative, modification or update is allowed to remove the licensing conditions that forbid the use of that derivative
|
||||
for the purpose of training, or improving machine learning algorithms, including but not limited to artificial intelligence,
|
||||
natural language processing, or data mining.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
28
README.md
Normal file
28
README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
This is a simulator that produces a timeline of villager production and
|
||||
resource gathering for Age of Empires 2.
|
||||
|
||||
You can see the `tests/test.py` file for examples of how to use it.
|
||||
|
||||
The sofware has been lightly verified (i.e., I ran some tests), but it has not
|
||||
been validated (i.e., I didn't check that it actually matches AoE2 results).
|
||||
|
||||
The code tries to keep you from shooting yourself in the foot. It checks
|
||||
whether you are housed, do not have enough resource to build what you are
|
||||
trying to build, etc. There are various known limitations, such as the fact
|
||||
that farming produces food constantly in the simulator, and there are
|
||||
undoubtably more checks that I forgot to include.
|
||||
|
||||
Why would you need this? Well, say you were trying to develop a new build
|
||||
order. You could play variations out in the game for maximum accuracy. You
|
||||
could do simple math by hand or in a spreadsheet to quickly get approximate
|
||||
answers. Or, you could use a Python library you found online that is probably
|
||||
clunkier and less accurate than both other options! What's not to love?
|
||||
|
||||
With a nice GUI, the software might be somewhat usable, but I doubt I will get
|
||||
around to that.
|
||||
|
||||
Enjoy!
|
||||
|
||||
# Data source
|
||||
|
||||
My thanks to https://ageofempires.fandom.com/wiki
|
||||
33
aoe/costs.py
33
aoe/costs.py
@@ -9,6 +9,26 @@ class Costs():
|
||||
return self.costs[purchase]
|
||||
|
||||
|
||||
def compute_market_price(self, resource, buy=True):
|
||||
rate = self.market_rates[resource]
|
||||
if buy:
|
||||
cost = rate*(1 + self.market_fee)
|
||||
else:
|
||||
cost = rate*(1 - self.market_fee)
|
||||
return cost
|
||||
|
||||
|
||||
def adjust_market_price(self, resource, buy=True):
|
||||
rate = self.market_rates[resource]
|
||||
if buy:
|
||||
new_rate = min(self.maximum_market_rate,
|
||||
rate + self.market_increment_amount)
|
||||
else:
|
||||
new_rate = max(self.minimum_market_rate,
|
||||
rate - self.market_increment_amount)
|
||||
self.market_rates[resource] = new_rate
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.costs = {
|
||||
Ages.DARK: (0, 0, 0, 0),
|
||||
@@ -138,4 +158,17 @@ class Costs():
|
||||
Units.KNIGHT: (60, 0, 75, 0),
|
||||
Units.SKIRMISHER: (25, 35, 0, 0),
|
||||
Units.ARCHER: (0, 25, 45, 0),
|
||||
Units.MONK: (0, 0, 100, 0),
|
||||
Units.FISHINGSHIP: (0, 75, 0, 0),
|
||||
Units.TRADECOG: (0, 100, 50, 0),
|
||||
}
|
||||
self.market_rates = {
|
||||
Resources.FOOD: 115,
|
||||
Resources.WOOD: 100,
|
||||
Resources.STONE: 130,
|
||||
}
|
||||
self.market_fee = 0.3
|
||||
self.minimum_market_rate = 20
|
||||
self.maximum_market_rate = 9999
|
||||
self.market_increment_amount = 2
|
||||
self.market_transaction_amount = 100
|
||||
|
||||
@@ -269,4 +269,7 @@ class Dependencies():
|
||||
Units.KNIGHT: [Ages.CASTLE, Buildings.STABLE],
|
||||
Units.SKIRMISHER: [Ages.FEUDAL, Buildings.ARCHERYRANGE],
|
||||
Units.ARCHER: [Ages.FEUDAL, Buildings.ARCHERYRANGE],
|
||||
Units.MONK: [Ages.CASTLE, Buildings.MONASTERY],
|
||||
Units.FISHINGSHIP: [Ages.DARK, Buildings.DOCK],
|
||||
Units.TRADECOG: [Ages.FEUDAL, Buildings.DOCK],
|
||||
}
|
||||
|
||||
@@ -18,6 +18,11 @@ class IllegalVillagerMoveException(Exception):
|
||||
|
||||
|
||||
|
||||
class InvalidMarketAmountException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class MissingDependencyException(Exception):
|
||||
pass
|
||||
|
||||
@@ -28,6 +33,11 @@ class NotEnoughResourcesException(Exception):
|
||||
|
||||
|
||||
|
||||
class NotFarmEquivalentException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class ProductionBuildingNotAvailableException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
117
aoe/gameplay.py
117
aoe/gameplay.py
@@ -37,30 +37,131 @@ class Gameplay():
|
||||
villagers_to,)))
|
||||
|
||||
|
||||
def market_buy(self, action_time, resource, amount):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.market_buy,
|
||||
(resource, amount)))
|
||||
|
||||
|
||||
def market_sell(self, action_time, resource, amount):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.market_sell,
|
||||
(resource, amount)))
|
||||
|
||||
|
||||
def queue_research(self, action_time, research):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.queue_research,
|
||||
(research,)))
|
||||
|
||||
|
||||
def queue_villager(self, action_time, initial_task):
|
||||
def queue_villagers_by_task_list(self, from_time, initial_tasks):
|
||||
train_time = self.world.build_times.build_times[Units.VILLAGER]
|
||||
|
||||
if not isinstance(initial_tasks, list):
|
||||
initial_tasks = [initial_tasks]
|
||||
|
||||
for i in range(len(initial_tasks)):
|
||||
self.queue_villager(from_time + i*train_time, initial_tasks[i])
|
||||
|
||||
|
||||
def queue_villagers_over_time(self, from_time, until_time, initial_task):
|
||||
train_time = self.world.build_times.build_times[Units.VILLAGER]
|
||||
duration = max(0, until_time - from_time)
|
||||
|
||||
for i in range(duration//train_time + 1):
|
||||
self.queue_villager(from_time + i*train_time, initial_task)
|
||||
|
||||
|
||||
def queue_villager(self, action_time, initial_task, **kwargs):
|
||||
'''
|
||||
initial_task can be instance of one of the following:
|
||||
Tasks -- assign this task along with any keyword arguments
|
||||
(Tasks, dict) -- assume the dict contains the keyword arguments
|
||||
List of (Tasks, dict) -- several tasks to complete in order, each with
|
||||
its own keyword arguments
|
||||
'''
|
||||
self._check_time(action_time)
|
||||
if isinstance(initial_task, Tasks):
|
||||
initial_task = (initial_task, kwargs)
|
||||
self.actions[action_time].append((self.world.queue_villager,
|
||||
(initial_task,)))
|
||||
|
||||
|
||||
def queue_fishing_ship(self, action_time, initial_task=Tasks.FISH_SHIP_DEEP):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.queue_fishing_ship,
|
||||
(initial_task,)))
|
||||
|
||||
|
||||
def queue_trade_cog(self, action_time):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.queue_trade_cog, ()))
|
||||
|
||||
|
||||
def queue_trade_cart(self, action_time):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.queue_trade_cart, ()))
|
||||
|
||||
|
||||
def queue_unit(self, action_time, unit):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.queue_unit, (unit,)))
|
||||
|
||||
|
||||
def assign_villager(self, action_time, old_task, new_task, count=1):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.assign_villager,
|
||||
(old_task, new_task, count,)))
|
||||
|
||||
|
||||
def add_task_to_work_queue(self, action_time, task, **kwargs):
|
||||
self._check_time(action_time)
|
||||
task = (task, kwargs)
|
||||
self.actions[action_time].append((self.world.add_task_to_work_queue,
|
||||
(task,)))
|
||||
|
||||
|
||||
def get_relic(self, action_time, walking_time=60):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.get_relic,
|
||||
(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,
|
||||
walking_time=30):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.lure_huntable_with_villager,
|
||||
(previous_task, walking_time)))
|
||||
|
||||
|
||||
def increase_age(self, action_time):
|
||||
self._check_time(action_time)
|
||||
self.actions[action_time].append((self.world.increase_age, ()))
|
||||
|
||||
|
||||
def _check_time(self, action_time):
|
||||
# TODO will it work if we do something at current time?
|
||||
if self.world.current_time > action_time:
|
||||
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):
|
||||
try:
|
||||
next_action_time = min(self.actions.keys())
|
||||
@@ -76,7 +177,12 @@ class Gameplay():
|
||||
|
||||
def _handle_actions(self):
|
||||
for func, args in self.actions[self.world.current_time]:
|
||||
func(*args)
|
||||
try:
|
||||
func(*args)
|
||||
except Exception as e:
|
||||
raise type(e)(
|
||||
f'Error in {func.__name__}{args}'
|
||||
f' at time {self.world.current_time}.') from e
|
||||
del self.actions[self.world.current_time]
|
||||
|
||||
|
||||
@@ -88,6 +194,11 @@ class Gameplay():
|
||||
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):
|
||||
self.world = World()
|
||||
self.actions = defaultdict(list)
|
||||
|
||||
205
aoe/gather.py
205
aoe/gather.py
@@ -139,9 +139,42 @@ class Gather():
|
||||
|
||||
BIG_NUMBER = 1e6
|
||||
|
||||
NON_ASSIGNABLE_TASKS = [
|
||||
Tasks.MONK_GET_RELIC,
|
||||
Tasks.MONK_IDLE,
|
||||
Tasks.RELIC,
|
||||
Tasks.TRADECART,
|
||||
Tasks.TRADECOG,
|
||||
]
|
||||
|
||||
FISHING_TASKS = [
|
||||
Tasks.FISH_SHIP_IDLE,
|
||||
Tasks.FISH_SHIP_SHORE,
|
||||
Tasks.FISH_SHIP_DEEP,
|
||||
Tasks.FISH_SHIP_BUILD,
|
||||
Tasks.FISH_SHIP_TRAP,
|
||||
]
|
||||
|
||||
FARM_LIKE_TASKS = [
|
||||
Tasks.FARM,
|
||||
Tasks.FISH_SHIP_TRAP,
|
||||
]
|
||||
|
||||
FARM_LIKE_BUILDINGS = [
|
||||
Buildings.FARM,
|
||||
Buildings.FISHTRAP,
|
||||
]
|
||||
|
||||
|
||||
def check_gather(self, world, task, count=1):
|
||||
if task in [Tasks.IDLE, Tasks.BUILD]:
|
||||
if task in [
|
||||
Tasks.IDLE,
|
||||
Tasks.BUILD,
|
||||
Tasks.MONK_IDLE,
|
||||
Tasks.LURE,
|
||||
Tasks.FISH_SHIP_IDLE,
|
||||
Tasks.FISH_SHIP_BUILD,
|
||||
]:
|
||||
return
|
||||
|
||||
required = self.required_gather_buildings[task]
|
||||
@@ -152,9 +185,10 @@ class Gather():
|
||||
if not match:
|
||||
raise GatherBuildingNotAvailableException
|
||||
|
||||
if task == task.FARM:
|
||||
if world.villager_tasks[task] + count > len(self.unused_farms):
|
||||
raise GatherBuildingNotAvailableException
|
||||
if task in Gather.FARM_LIKE_TASKS:
|
||||
self._check_consumable_gather_building_available(world,
|
||||
task,
|
||||
count)
|
||||
|
||||
|
||||
def spend_resources(self, cost):
|
||||
@@ -166,6 +200,11 @@ class Gather():
|
||||
self.resources[i] -= cost[i]
|
||||
|
||||
|
||||
def acquire_resources_without_gather(self, gained):
|
||||
for i in range(len(self.resources)):
|
||||
self.resources[i] += gained[i]
|
||||
|
||||
|
||||
def permanently_modify_gather_rate(self, task, rate_multiplier):
|
||||
self.gather_rates[task] *= rate_multiplier
|
||||
self.original_gather_rates[task] *= rate_multiplier
|
||||
@@ -181,27 +220,32 @@ class Gather():
|
||||
|
||||
def compute_next_gather_rate_decrease(self, task, villagers):
|
||||
rate = self.gather_rates[task]
|
||||
decay = self.decay_rates[task]
|
||||
|
||||
if task == Tasks.FARM:
|
||||
available = self.farms[0]
|
||||
if task in Gather.FARM_LIKE_TASKS:
|
||||
used = self.consumable_buildings[task]['used']
|
||||
try:
|
||||
available = used[0]
|
||||
except IndexError:
|
||||
# this routine may be called even when there are no active farms
|
||||
available = 0
|
||||
villagers = min(villagers, 1)
|
||||
else:
|
||||
available = self.gatherable_amounts[task]
|
||||
|
||||
try:
|
||||
next_decrease = available/(villagers*rate)
|
||||
SMALL_FLOAT = 1e-8
|
||||
if rate < SMALL_FLOAT or (villagers == 0 and decay == 0):
|
||||
next_decrease = None
|
||||
else:
|
||||
next_decrease = available/(villagers*rate + decay)
|
||||
# rounding error will give 1 extra second of gathering
|
||||
next_decrease = math.ceil(next_decrease)
|
||||
except ZeroDivisionError:
|
||||
# either villagers or rate was 0, so no next decrease should
|
||||
# be scheduled
|
||||
next_decrease = None
|
||||
|
||||
return next_decrease
|
||||
|
||||
|
||||
def improve_gather_rate_by_building(self, building, tasks):
|
||||
if building == Buildings.FARM:
|
||||
if building in Gather.FARM_LIKE_TASKS:
|
||||
raise ValueError('Do not call this routine with a farm')
|
||||
|
||||
if building not in self.buildings_gather_increase.keys():
|
||||
@@ -224,59 +268,100 @@ class Gather():
|
||||
|
||||
|
||||
def permanently_modify_consumable_capacity(self, building, increase):
|
||||
task = self._get_task_from_consumable_building(building)
|
||||
used, unused = self.consumable_buildings[task].values()
|
||||
|
||||
current_initial_capacity = self.buildings_gather_increase[building]
|
||||
self.buildings_gather_increase[building] += increase
|
||||
|
||||
modifier = increase/current_initial_capacity
|
||||
|
||||
if building == Buildings.FARM:
|
||||
used = self.farms
|
||||
unused = self.unused_farms
|
||||
|
||||
for i in range(len(used)):
|
||||
used[i] += used[i]*multiplier
|
||||
for i in range(len(unused)):
|
||||
unused[i] += unused[i]*multiplier
|
||||
for consumable_list in [used, unused]:
|
||||
for i in range(len(consumable_list)):
|
||||
conusmable_list[i] += conusmable_list[i]*multiplier
|
||||
|
||||
|
||||
def stop_using_farms(self, count):
|
||||
def stop_using_consumable_buildings(self, task, count):
|
||||
used, unused = self.consumable_buildings[task].values()
|
||||
|
||||
# remove villagers from farms with lowest remaining capacity
|
||||
for i in range(count):
|
||||
heapq.heappush_max(self.unused_farms,
|
||||
heapq.heappop(self.farms))
|
||||
try:
|
||||
for i in range(count):
|
||||
heapq.heappush_max(unused, heapq.heappop(used))
|
||||
except IndexError:
|
||||
# if we are stopping because a building has been used up,
|
||||
# the building will no longer be in `used`, which is correct
|
||||
pass
|
||||
|
||||
|
||||
def start_using_farms(self, count):
|
||||
def start_using_consumable_buildings(self, task, count):
|
||||
used, unused = self.consumable_buildings[task].values()
|
||||
|
||||
# add villagers to farms with highest remaining capacity
|
||||
for i in range(count):
|
||||
heapq.heappush(self.farms,
|
||||
heapq.heappop_max(self.unused_farms))
|
||||
heapq.heappush(used, heapq.heappop_max(unused))
|
||||
|
||||
|
||||
def add_farm(self):
|
||||
new_farm_value = self.buildings_gather_increase[Buildings.FARM][Tasks.FARM]
|
||||
heapq.heappush(self.unused_farms, new_farm_value)
|
||||
def _get_task_from_consumable_building(self, building):
|
||||
match building:
|
||||
case Buildings.FARM:
|
||||
task = Tasks.FARM
|
||||
case Buildings.FISHTRAP:
|
||||
task = Tasks.FISH_SHIP_TRAP
|
||||
case _:
|
||||
raise NotFarmEquivalentException
|
||||
|
||||
return task
|
||||
|
||||
|
||||
def remove_farm(self):
|
||||
heapq.heappop(self.farms)
|
||||
def add_consumable_building(self, building):
|
||||
task = self._get_task_from_consumable_building(building)
|
||||
unused = self.consumable_buildings[task]['unused']
|
||||
new_value = self.buildings_gather_increase[building][task]
|
||||
heapq.heappush(unused, new_value)
|
||||
|
||||
|
||||
def get_num_working_farms(self):
|
||||
return len(self.farms)
|
||||
def remove_consumable_building(self, building):
|
||||
task = self._get_task_from_consumable_building(building)
|
||||
used = self.consumable_buildings[task]['used']
|
||||
heapq.heappop(used)
|
||||
|
||||
|
||||
def _check_consumable_gather_building_available(self, world, task, count):
|
||||
unused = self.consumable_buildings[task]['unused']
|
||||
if world.villager_tasks[task] + count > len(unused):
|
||||
raise GatherBuildingNotAvailableException
|
||||
|
||||
|
||||
def get_num_working_consumable_buildings(self, task):
|
||||
working = len(self.consumable_buildings[task]['used'])
|
||||
return working
|
||||
|
||||
|
||||
def add_huntable(self):
|
||||
self.improve_gather_rate_by_building(Tasks.LURE, None)
|
||||
|
||||
|
||||
def gather_resources_for_duration(self, villager_tasks, duration):
|
||||
# assumes there are sufficient resources in gatherable_amounts
|
||||
for task, count in villager_tasks.items():
|
||||
if task in [Tasks.BUILD, Tasks.IDLE]:
|
||||
if task in [
|
||||
Tasks.BUILD,
|
||||
Tasks.LURE,
|
||||
Tasks.IDLE,
|
||||
Tasks.MONK_IDLE,
|
||||
Tasks.MONK_GET_RELIC,
|
||||
Tasks.FISH_SHIP_IDLE,
|
||||
Tasks.FISH_SHIP_BUILD,
|
||||
]:
|
||||
continue
|
||||
|
||||
rate = self.gather_rates[task]
|
||||
|
||||
if task == Tasks.FARM:
|
||||
for i in range(len(self.farms)):
|
||||
self.farms[i] -= rate*duration
|
||||
if task in Gather.FARM_LIKE_TASKS:
|
||||
used, unused = self.consumable_buildings[task].values()
|
||||
for i in range(len(used)):
|
||||
used[i] -= rate*duration
|
||||
else:
|
||||
self.gatherable_amounts[task] -= count*rate*duration
|
||||
|
||||
@@ -284,12 +369,17 @@ class Gather():
|
||||
amount_gathered = count*rate*mult*duration
|
||||
self.resources[resource] += amount_gathered
|
||||
|
||||
for task in self.decay_rates.keys():
|
||||
self.gatherable_amounts[task] -= self.decay_rates[task]*duration
|
||||
if self.gatherable_amounts[task] < 0:
|
||||
self.gatherable_amounts[task] = 0
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.resources = [200, 200, 100, 200]
|
||||
|
||||
self.farms = []
|
||||
self.unused_farms = []
|
||||
self.consumable_buildings = {t: {'used': [], 'unused': []}
|
||||
for t in Gather.FARM_LIKE_TASKS}
|
||||
|
||||
self.gather_rates = {
|
||||
Tasks.BERRIES: 18.6/60,
|
||||
@@ -300,7 +390,8 @@ class Gather():
|
||||
Tasks.FISH_VILLAGER: 25.8/60,
|
||||
Tasks.FISH_SHIP_SHORE: 16.8/60,
|
||||
Tasks.FISH_SHIP_DEEP: 29.4/60,
|
||||
Tasks.FISHTRAP: 21/60,
|
||||
Tasks.FISH_SHIP_BUILD: 0,
|
||||
Tasks.FISH_SHIP_TRAP: 21/60,
|
||||
Tasks.WOOD: 23.4/60,
|
||||
Tasks.GOLD: 22.8/60,
|
||||
Tasks.TRADECART: 25.9/60,
|
||||
@@ -313,6 +404,12 @@ class Gather():
|
||||
|
||||
self.original_gather_rates = copy.deepcopy(self.gather_rates)
|
||||
|
||||
self.decay_rates = defaultdict(int, {
|
||||
Tasks.SHEEP: 0.25,
|
||||
Tasks.BOAR: 0.4,
|
||||
Tasks.HUNT: 0.25,
|
||||
})
|
||||
|
||||
self.gathered_resources = {
|
||||
Tasks.BERRIES: {Resources.FOOD: 1},
|
||||
Tasks.SHEEP: {Resources.FOOD: 1},
|
||||
@@ -322,7 +419,7 @@ class Gather():
|
||||
Tasks.FISH_VILLAGER: {Resources.FOOD: 1},
|
||||
Tasks.FISH_SHIP_SHORE: {Resources.FOOD: 1},
|
||||
Tasks.FISH_SHIP_DEEP: {Resources.FOOD: 1},
|
||||
Tasks.FISHTRAP: {Resources.FOOD: 1},
|
||||
Tasks.FISH_SHIP_TRAP: {Resources.FOOD: 1},
|
||||
Tasks.WOOD: {Resources.WOOD: 1},
|
||||
Tasks.GOLD: {Resources.GOLD: 1},
|
||||
Tasks.TRADECART: {Resources.GOLD: 1},
|
||||
@@ -349,7 +446,7 @@ class Gather():
|
||||
set([Buildings.TOWNCENTER])],
|
||||
Tasks.FISH_SHIP_SHORE: [set([Buildings.DOCK])],
|
||||
Tasks.FISH_SHIP_DEEP: [set([Buildings.DOCK])],
|
||||
Tasks.FISHTRAP: [set([Buildings.DOCK, Buildings.FISHTRAP])],
|
||||
Tasks.FISH_SHIP_TRAP: [set([Buildings.DOCK, Buildings.FISHTRAP])],
|
||||
Tasks.WOOD: [set([Buildings.LUMBERCAMP]),
|
||||
set([Buildings.TOWNCENTER])],
|
||||
Tasks.GOLD: [set([Buildings.MININGCAMP]),
|
||||
@@ -372,14 +469,14 @@ class Gather():
|
||||
# 4 deer
|
||||
Tasks.BERRIES: 6*125,
|
||||
Tasks.SHEEP: 8*100,
|
||||
Tasks.BOAR: 2*340,
|
||||
Tasks.BOAR: 0,
|
||||
Tasks.HUNT: 4*140,
|
||||
Tasks.FARM: 0,
|
||||
# TODO next two should be shared
|
||||
Tasks.FISH_VILLAGER: 0,
|
||||
Tasks.FISH_SHIP_SHORE: 0,
|
||||
Tasks.FISH_SHIP_DEEP: 0,
|
||||
Tasks.FISHTRAP: 0,
|
||||
Tasks.FISH_SHIP_TRAP: 0,
|
||||
Tasks.WOOD: 0,
|
||||
Tasks.GOLD: 0,
|
||||
Tasks.TRADECART: Gather.BIG_NUMBER,
|
||||
@@ -396,13 +493,15 @@ class Gather():
|
||||
Buildings.MININGCAMP: {Tasks.GOLD: 4*800,
|
||||
Tasks.STONE: 4*350},
|
||||
Buildings.FARM: {Tasks.FARM: 175},
|
||||
#Buildings.DOCK: {Tasks.WOOD: 100},
|
||||
Buildings.DOCK: {Tasks.FISH_SHIP_SHORE: 100,
|
||||
Tasks.FISH_SHIP_DEEP: 225*2},
|
||||
Buildings.TOWNCENTER: {Tasks.WOOD: 2000,
|
||||
Tasks.GOLD: 4*800,
|
||||
Tasks.STONE: 4*350,
|
||||
Tasks.BERRIES: 6*125,
|
||||
Tasks.HUNT: 4*140}
|
||||
#Buildings.FISHTRAP: {Tasks.WOOD: 100},
|
||||
Tasks.HUNT: 4*140},
|
||||
Buildings.FISHTRAP: {Tasks.FISH_SHIP_TRAP: 715},
|
||||
Tasks.LURE: {Tasks.BOAR: 340},
|
||||
}
|
||||
|
||||
self.gather_decrease_rates = {
|
||||
@@ -412,10 +511,10 @@ class Gather():
|
||||
Tasks.BOAR: 0,
|
||||
Tasks.HUNT: 0,
|
||||
Tasks.FARM: 0,
|
||||
Tasks.FISH_VILLAGER: 0.5,
|
||||
Tasks.FISH_SHIP_SHORE: 0.5,
|
||||
Tasks.FISH_SHIP_DEEP: 0,
|
||||
Tasks.FISHTRAP: 0,
|
||||
Tasks.FISH_VILLAGER: 0,
|
||||
Tasks.FISH_SHIP_SHORE: 0,
|
||||
Tasks.FISH_SHIP_DEEP: 0.5,
|
||||
Tasks.FISH_SHIP_TRAP: 0,
|
||||
Tasks.WOOD: 0.85,
|
||||
Tasks.GOLD: 0,
|
||||
Tasks.TRADECART: 1,
|
||||
|
||||
@@ -13,9 +13,18 @@ class Housing():
|
||||
|
||||
if required > 0:
|
||||
pop_space = world.get_population_space()
|
||||
if world.get_effective_population() > pop_space + required:
|
||||
if world.get_effective_population() + required > pop_space:
|
||||
raise YallGotHousedException
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def check_housing_safe(self, world, thing):
|
||||
try:
|
||||
return self.check_housing(world, thing)
|
||||
except YallGotHousedException:
|
||||
return False
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.space = defaultdict(int, {
|
||||
|
||||
@@ -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, {
|
||||
@@ -25,6 +34,9 @@ class Production():
|
||||
Units.KNIGHT: Buildings.STABLE,
|
||||
Units.SKIRMISHER: Buildings.ARCHERYRANGE,
|
||||
Units.ARCHER: Buildings.ARCHERYRANGE,
|
||||
Units.MONK: Buildings.MONASTERY,
|
||||
Units.FISHINGSHIP: Buildings.DOCK,
|
||||
Units.TRADECOG: Buildings.DOCK,
|
||||
Research.TOWNWATCH: Buildings.TOWNCENTER,
|
||||
Research.TOWNPATROL: Buildings.TOWNCENTER,
|
||||
Research.TREADMILLCRANE: Buildings.UNIVERSITY,
|
||||
|
||||
@@ -135,4 +135,7 @@ class Times():
|
||||
Units.KNIGHT: 30,
|
||||
Units.SKIRMISHER: 22,
|
||||
Units.ARCHER: 35,
|
||||
Units.MONK: 51,
|
||||
Units.FISHINGSHIP: 40,
|
||||
Units.TRADECOG: 36,
|
||||
}
|
||||
|
||||
10
aoe/types.py
10
aoe/types.py
@@ -53,13 +53,18 @@ class Tasks(Enum):
|
||||
SHEEP = auto()
|
||||
BOAR = auto()
|
||||
HUNT = auto()
|
||||
LURE = auto()
|
||||
FARM = auto()
|
||||
FISH_VILLAGER = auto()
|
||||
FISH_SHIP_IDLE = auto()
|
||||
FISH_SHIP_SHORE = auto()
|
||||
FISH_SHIP_DEEP = auto()
|
||||
FISHTRAP = auto()
|
||||
FISH_SHIP_BUILD = auto()
|
||||
FISH_SHIP_TRAP = auto()
|
||||
WOOD = auto()
|
||||
GOLD = auto()
|
||||
MONK_IDLE = auto()
|
||||
MONK_GET_RELIC = auto()
|
||||
RELIC = auto()
|
||||
TRADECART = auto()
|
||||
TRADECOG = auto()
|
||||
@@ -167,3 +172,6 @@ class Units(Enum):
|
||||
KNIGHT = auto()
|
||||
SKIRMISHER = auto()
|
||||
ARCHER = auto()
|
||||
MONK = auto()
|
||||
FISHINGSHIP = auto()
|
||||
TRADECOG = auto()
|
||||
|
||||
337
aoe/world.py
337
aoe/world.py
@@ -43,17 +43,15 @@ class World():
|
||||
# to farms with the highest capacity.
|
||||
# We keep track of only the closest upcoming farm deletion.
|
||||
|
||||
# TODO fishing ships
|
||||
# TODO relics
|
||||
# TODO trade carts
|
||||
# Note: If you move away a builder while building is in progress, it will
|
||||
# not slow down or stop the building. I do not plan to change this, so
|
||||
# don't manually move workers away from build tasks if you want accurate
|
||||
# results. Similarly, adding builders in the middle of building something
|
||||
# is not supported
|
||||
|
||||
# TODO allow automation:
|
||||
# reseeding of farms
|
||||
# queuing of villagers
|
||||
# possibly other things
|
||||
# TODO gathering boars and hunting
|
||||
# spend villager time in exchange for adding gatherable resource
|
||||
# TODO food spoilage
|
||||
# TODO use market for buy/sell
|
||||
|
||||
|
||||
MAX_WORLD_TIME = 3*60*60 # 3 hours
|
||||
@@ -65,6 +63,42 @@ class World():
|
||||
self.gather.spend_resources(cost)
|
||||
|
||||
|
||||
def market_buy(self, resource, amount):
|
||||
self._use_market(resource, amount, buy=True)
|
||||
|
||||
|
||||
def market_sell(self, resource, amount):
|
||||
self._use_market(resource, amount, buy=False)
|
||||
|
||||
|
||||
def _use_market(self, resource, amount, buy=True):
|
||||
if Buildings.MARKET not in self.buildings:
|
||||
raise MissingDependencyException
|
||||
|
||||
if amount % self.costs.market_transaction_amount != 0:
|
||||
raise InvalidMarketAmountException
|
||||
|
||||
for i in range(amount//self.costs.market_transaction_amount):
|
||||
cost = self.costs.compute_market_price(resource, buy=buy)
|
||||
gold_amount = [0, 0, cost, 0]
|
||||
|
||||
resource_amount = [0, 0, 0, 0]
|
||||
resource_amount[int(resource)] = self.costs.market_transaction_amount
|
||||
|
||||
if buy:
|
||||
self.gather.spend_resources(gold_amount)
|
||||
self.gather.acquire_resources_without_gather(resource_amount)
|
||||
else:
|
||||
self.gather.spend_resources(resource_amount)
|
||||
self.gather.acquire_resources_without_gather(gold_amount)
|
||||
|
||||
self.costs.adjust_market_price(resource, buy=buy)
|
||||
|
||||
|
||||
def has_building(self, building):
|
||||
return building in self.buildings
|
||||
|
||||
|
||||
def _remove_event_at_time(self, removed_event, scheduled_time):
|
||||
if not scheduled_time:
|
||||
return
|
||||
@@ -76,7 +110,11 @@ class World():
|
||||
|
||||
|
||||
def _schedule_next_gather_rate_decrease(self, task):
|
||||
if task in [Tasks.IDLE, Tasks.BUILD]:
|
||||
if task in [Tasks.IDLE,
|
||||
Tasks.BUILD,
|
||||
Tasks.LURE,
|
||||
Tasks.FISH_SHIP_IDLE,
|
||||
Tasks.FISH_SHIP_BUILD]:
|
||||
return
|
||||
|
||||
# remove old decrease
|
||||
@@ -86,10 +124,11 @@ class World():
|
||||
next_decrease = self.gather.compute_next_gather_rate_decrease(task,
|
||||
self.villager_tasks[task])
|
||||
|
||||
if next_decrease:
|
||||
if next_decrease is not None:
|
||||
next_decrease += self.current_time
|
||||
if task == Tasks.FARM:
|
||||
self.events[next_decrease].append((self._remove_farm, ()))
|
||||
if task in Gather.FARM_LIKE_TASKS:
|
||||
self.events[next_decrease].append((self._remove_consumable_building,
|
||||
(task,)))
|
||||
else:
|
||||
self.events[next_decrease].append((self._decrease_gather_rate_by_overuse,
|
||||
(task,)))
|
||||
@@ -129,6 +168,47 @@ class World():
|
||||
self._schedule_next_gather_rate_decrease(task)
|
||||
|
||||
|
||||
def lure_huntable_with_villager(self, previous_task, walking_time=30):
|
||||
update_time = self.current_time + walking_time
|
||||
self.events[update_time].append((self._add_huntable, ()))
|
||||
|
||||
self.assign_villager(previous_task, Tasks.LURE)
|
||||
|
||||
|
||||
def _add_huntable(self):
|
||||
self.gather.add_huntable()
|
||||
self.assign_villager(Tasks.LURE, Tasks.BOAR)
|
||||
|
||||
|
||||
def add_task_to_work_queue(self, task, **kwargs):
|
||||
if isinstance(task, Tasks):
|
||||
task = (task, kwargs)
|
||||
self.villager_work_queue.append(task)
|
||||
self._assign_to_villagers_from_work_queue()
|
||||
|
||||
|
||||
def _assign_idle_villager_possibly_compound_task(self, task, **kwargs):
|
||||
if task == Tasks.LURE:
|
||||
self.lure_huntable_with_villager(Tasks.IDLE, **kwargs)
|
||||
elif 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, task)
|
||||
|
||||
|
||||
def _assign_to_villagers_from_work_queue(self):
|
||||
while (self.villager_tasks[Tasks.IDLE] > 0 and
|
||||
len(self.villager_work_queue) > 0):
|
||||
original_task = self.villager_work_queue.pop(0)
|
||||
task, kwargs = self._parse_task(original_task)
|
||||
self._assign_idle_villager_possibly_compound_task(task, **kwargs)
|
||||
|
||||
|
||||
def assign_villager(self, previous_task, new_task, count=1):
|
||||
# new_task of None means kill the villager
|
||||
|
||||
@@ -137,6 +217,8 @@ class World():
|
||||
if self.villager_tasks[previous_task] < count:
|
||||
raise IllegalVillagerMoveException
|
||||
|
||||
self._check_single_villager_move(previous_task, new_task)
|
||||
|
||||
if new_task:
|
||||
self.gather.check_gather(self, new_task, count)
|
||||
self.villager_tasks[new_task] += count
|
||||
@@ -145,26 +227,97 @@ class World():
|
||||
|
||||
self.villager_tasks[previous_task] -= count
|
||||
|
||||
if previous_task == Tasks.FARM:
|
||||
self.gather.stop_using_farms(count)
|
||||
if previous_task in Gather.FARM_LIKE_TASKS:
|
||||
self.gather.stop_using_consumable_buildings(previous_task, count)
|
||||
|
||||
if new_task == Tasks.FARM:
|
||||
self.gather.start_using_farms(count)
|
||||
if new_task in Gather.FARM_LIKE_TASKS:
|
||||
self.gather.start_using_consumable_buildings(new_task, count)
|
||||
|
||||
self._schedule_next_gather_rate_decrease(previous_task)
|
||||
if new_task:
|
||||
self._schedule_next_gather_rate_decrease(new_task)
|
||||
|
||||
self._assign_to_villagers_from_work_queue()
|
||||
|
||||
def _check_villager_move(self, villagers_from_count):
|
||||
|
||||
def _check_valid_builders(self, villagers_from, building):
|
||||
# If building a fishtrap, at least one builder needs to be a fishing
|
||||
# ship. If building anything else, no builders can be fishing ships
|
||||
fish_builder = any([v in Gather.FISHING_TASKS for v in villagers_from])
|
||||
building_fishtrap = (building == Buildings.FISHTRAP)
|
||||
|
||||
if fish_builder != building_fishtrap:
|
||||
raise IllegalVillagerMoveException
|
||||
|
||||
|
||||
def _check_single_villager_move(self, villager_from, villager_to):
|
||||
for v in [villager_from, villager_to]:
|
||||
if v in Gather.NON_ASSIGNABLE_TASKS:
|
||||
raise IllegalVillagerMoveException
|
||||
|
||||
from_fishing = (villager_from in Gather.FISHING_TASKS)
|
||||
to_fishing = (villager_to in Gather.FISHING_TASKS)
|
||||
if from_fishing != to_fishing:
|
||||
raise IllegalVillagerMoveException
|
||||
|
||||
|
||||
def _check_villager_move(self,
|
||||
villagers_from_count,
|
||||
villagers_from,
|
||||
villagers_to):
|
||||
for task, count in villagers_from_count.items():
|
||||
if self.villager_tasks[task] < count:
|
||||
raise IllegalVillagerMoveException
|
||||
|
||||
for v_from, v_to in zip(villagers_from, villagers_to):
|
||||
self._check_single_villager_move(v_from, v_to)
|
||||
|
||||
|
||||
def _move_villagers_to_build(self, villagers_from_count):
|
||||
for task, count in villagers_from_count.items():
|
||||
self.assign_villager(task, Tasks.BUILD, count)
|
||||
build_task = self._get_build_task_from_task(task)
|
||||
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
|
||||
else:
|
||||
return Tasks.BUILD
|
||||
|
||||
|
||||
def get_relic(self, walking_time=60):
|
||||
if self.villager_tasks[Tasks.MONK_IDLE] < 1:
|
||||
self.relics_waiting_for_monk.append(walking_time)
|
||||
return
|
||||
|
||||
self.villager_tasks[Tasks.MONK_IDLE] -= 1
|
||||
self.villager_tasks[Tasks.MONK_GET_RELIC] += 1
|
||||
|
||||
update_time = self.current_time + walking_time
|
||||
|
||||
self.events[update_time].append((self._add_relic, ()))
|
||||
|
||||
|
||||
def _add_relic(self):
|
||||
self.villager_tasks[Tasks.MONK_IDLE] += 1
|
||||
self.villager_tasks[Tasks.MONK_GET_RELIC] -= 1
|
||||
self.villager_tasks[Tasks.RELIC] += 1
|
||||
self._get_next_waiting_relic()
|
||||
|
||||
|
||||
def _get_next_waiting_relic(self):
|
||||
if len(self.relics_waiting_for_monk) > 0:
|
||||
walking_time = self.relics_waiting_for_monk.pop()
|
||||
self.get_relic(walking_time)
|
||||
|
||||
|
||||
def build(self, building, villagers_from, villagers_to, building_tasks=None):
|
||||
@@ -178,7 +331,11 @@ class World():
|
||||
|
||||
villagers_from_count = Counter(villagers_from)
|
||||
|
||||
self._check_villager_move(villagers_from_count)
|
||||
self._check_villager_move(villagers_from_count,
|
||||
villagers_from,
|
||||
villagers_to)
|
||||
|
||||
self._check_valid_builders(villagers_from, building)
|
||||
|
||||
self.dependencies.check_dependencies(self, building)
|
||||
|
||||
@@ -204,26 +361,33 @@ class World():
|
||||
if building == Buildings.HOUSE:
|
||||
self._free_housing()
|
||||
|
||||
if building == Buildings.FARM:
|
||||
self._add_farm()
|
||||
if building in Gather.FARM_LIKE_BUILDINGS:
|
||||
self.gather.add_consumable_building(building)
|
||||
else:
|
||||
self._improve_gather_rate_by_building(building, tasks_improved)
|
||||
|
||||
for task, count in villagers_to.items():
|
||||
self.assign_villager(Tasks.BUILD, task, count)
|
||||
build_task = self._get_build_task_from_task(task)
|
||||
self.assign_villager(build_task, task, count)
|
||||
|
||||
|
||||
def _add_farm(self):
|
||||
self.buildings.append(Buildings.FARM)
|
||||
self.gather.add_farm()
|
||||
def _remove_consumable_building(self, task):
|
||||
match task:
|
||||
case Tasks.FARM:
|
||||
building = Buildings.FARM
|
||||
next_task = Tasks.IDLE
|
||||
case Tasks.FISH_SHIP_TRAP:
|
||||
building = Buildings.FISHTRAP
|
||||
next_task = Tasks.FISH_SHIP_DEEP
|
||||
case _:
|
||||
raise NotFarmEquivalentException
|
||||
|
||||
self.buildings.remove(building)
|
||||
self.gather.remove_consumable_building(building)
|
||||
|
||||
def _remove_farm(self):
|
||||
self.buildings.remove(Buildings.FARM)
|
||||
self.gather.remove_farm()
|
||||
|
||||
if self.villager_tasks[Tasks.FARM] > gather.get_num_working_farms():
|
||||
self.assign_villager(Tasks.FARM, Tasks.IDLE)
|
||||
num_working_buildings = self.gather.get_num_working_consumable_buildings(task)
|
||||
if self.villager_tasks[task] > num_working_buildings:
|
||||
self.assign_villager(task, next_task)
|
||||
|
||||
|
||||
def advance_by_time(self, duration):
|
||||
@@ -257,6 +421,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)
|
||||
@@ -269,14 +437,16 @@ 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 = self.production_queue[i]
|
||||
if self.housing.check_housing(self, thing) and \
|
||||
self.production.check_production(self, thing):
|
||||
self._start_producing(thing)
|
||||
thing, task = self.production_queue[i]
|
||||
if house_check(self, thing) and \
|
||||
self.production.check_production_safe(self, thing):
|
||||
self._start_producing(thing, task)
|
||||
del self.production_queue[i]
|
||||
else:
|
||||
i += 1
|
||||
@@ -296,20 +466,22 @@ 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 = self.production_queue[i]
|
||||
thing, task = self.production_queue[i]
|
||||
if self.production.building[thing] == building and \
|
||||
self.housing.check_housing(self, thing):
|
||||
self._start_producing(thing)
|
||||
house_check(self, thing):
|
||||
self._start_producing(thing, task)
|
||||
del self.production_queue[i]
|
||||
break
|
||||
i += 1
|
||||
|
||||
|
||||
def _use_housing_and_production(self, thing):
|
||||
self.housing.check_housing(self, thing)
|
||||
self.production.check_production(self, thing)
|
||||
self.housing.check_housing(self, thing)
|
||||
|
||||
building = self.production.building[thing]
|
||||
if building is not None:
|
||||
@@ -331,6 +503,11 @@ class World():
|
||||
|
||||
|
||||
def _add_thing(self, thing, initial_task):
|
||||
if (initial_task and
|
||||
thing != Units.VILLAGER and
|
||||
not isinstance(initial_task, Tasks)):
|
||||
raise ValueError('Only villagers can accept compound tasks')
|
||||
|
||||
self.units_being_built -= self.housing.required[thing]
|
||||
self.population += self.housing.required[thing]
|
||||
|
||||
@@ -343,14 +520,32 @@ class World():
|
||||
if thing == Units.VILLAGER:
|
||||
self.villagers += 1
|
||||
self.villager_tasks[Tasks.IDLE] += 1
|
||||
self.assign_villager(Tasks.IDLE, initial_task)
|
||||
task, kwargs = self._parse_task(initial_task)
|
||||
self._assign_idle_villager_possibly_compound_task(task, **kwargs)
|
||||
elif thing == Units.MONK:
|
||||
self.villager_tasks[Tasks.MONK_IDLE] += 1
|
||||
self._get_next_waiting_relic()
|
||||
elif thing == Units.FISHINGSHIP:
|
||||
if not initial_task:
|
||||
initial_task = Tasks.FISH_SHIP_IDLE
|
||||
self.villager_tasks[initial_task] += 1
|
||||
elif thing == Units.TRADECOG:
|
||||
self.villager_tasks[Tasks.TRADECOG] += 1
|
||||
elif thing == Units.TRADECART:
|
||||
self.villager_tasks[Tasks.TRADECART] += 1
|
||||
|
||||
|
||||
def _start_producing(self, thing, initial_task):
|
||||
try:
|
||||
self._use_housing_and_production(thing)
|
||||
except ProductionBuildingNotAvailableException:
|
||||
self.production_queue.append(thing)
|
||||
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,)))
|
||||
@@ -376,9 +571,57 @@ class World():
|
||||
self._queue_thing(Units.VILLAGER, initial_task)
|
||||
|
||||
|
||||
def queue_fishing_ship(self, initial_task=Tasks.FISH_SHIP_DEEP):
|
||||
self._queue_thing(Units.FISHINGSHIP, initial_task)
|
||||
|
||||
|
||||
def queue_trade_cog(self):
|
||||
self._queue_thing(Units.TRADECOG)
|
||||
|
||||
|
||||
def queue_trade_cart(self):
|
||||
self._queue_thing(Units.TRADECART)
|
||||
|
||||
|
||||
def _parse_task(self, initial_task):
|
||||
'''
|
||||
A single task is either a Task or a tuple of (Tasks, dict) where the
|
||||
dictionary contains keyword arguments.
|
||||
|
||||
If a list is passed, all tasks beyond the first are appended to the
|
||||
villager work queue.
|
||||
'''
|
||||
if isinstance(initial_task, list):
|
||||
remaining_tasks = initial_task[1:]
|
||||
self.villager_work_queue.extend(remaining_tasks)
|
||||
initial_task = initial_task[0]
|
||||
|
||||
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):
|
||||
for func, args in self.events[self.current_time]:
|
||||
func(*args)
|
||||
try:
|
||||
func(*args)
|
||||
except Exception as e:
|
||||
raise type(e)(
|
||||
f'Error in {func.__name__}{args}'
|
||||
f' at time {self.current_time}.'
|
||||
f' Note that the actual gameplay command that caused'
|
||||
f' the error may not have been issued at'
|
||||
f' {self.current_time} -- the action may have been'
|
||||
f' delayed until that time while'
|
||||
f' waiting in a queue') from e
|
||||
|
||||
del self.events[self.current_time]
|
||||
|
||||
|
||||
@@ -390,9 +633,9 @@ class World():
|
||||
if self.age == Ages.IMPERIAL:
|
||||
raise Exception("no.")
|
||||
|
||||
new_age = self.age + 1
|
||||
new_age = Ages(self.age + 1)
|
||||
|
||||
self.dependencies.check_dependencies(new_age)
|
||||
self.dependencies.check_dependencies(self, new_age)
|
||||
self.pay_cost(new_age)
|
||||
|
||||
update_time = self.current_time + self.build_times.build_times[new_age]
|
||||
@@ -414,6 +657,7 @@ class World():
|
||||
|
||||
self.villager_tasks = {t: 0 for t in Tasks}
|
||||
self.villager_tasks[Tasks.IDLE] = self.villagers
|
||||
self.villager_work_queue = []
|
||||
|
||||
self.rate_decrease_event_times = {t: None for t in Tasks}
|
||||
|
||||
@@ -421,6 +665,7 @@ class World():
|
||||
self.production_available = {b: 0 for b in Buildings}
|
||||
self.production_available[Buildings.TOWNCENTER] = 1
|
||||
self.production_queue = []
|
||||
self.relics_waiting_for_monk = []
|
||||
|
||||
self.current_time = 0
|
||||
self.events = defaultdict(list)
|
||||
@@ -432,4 +677,6 @@ class World():
|
||||
self.production = Production()
|
||||
self.gather = Gather()
|
||||
|
||||
self.housed_is_error = True
|
||||
|
||||
self._deal_with_civ()
|
||||
|
||||
@@ -1,18 +1,3 @@
|
||||
# TODO
|
||||
|
||||
* Get aoe info
|
||||
* API for it somewhere?
|
||||
* Write simple code
|
||||
* Write tests
|
||||
* Write GUI
|
||||
|
||||
Deal with getting housed
|
||||
|
||||
Put villagers on resource sources
|
||||
* what to do about farms?
|
||||
* one villager per
|
||||
* run out occaisionally and must be rebuilt
|
||||
|
||||
# Outline
|
||||
|
||||
Timeline of villager production
|
||||
@@ -48,30 +33,3 @@ As an aside,
|
||||
this is similar to what I've seen described as the fundamental difference
|
||||
between object-oriented (row-based) and functional (column-based) programming,
|
||||
though I'm still using classes and objects.
|
||||
|
||||
|
||||
# Scratch
|
||||
|
||||
```
|
||||
def create(self):
|
||||
self.
|
||||
|
||||
# Assume entries sorted by time
|
||||
# Not necessarily from user, but from queue_villager and move_villager
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
Class Task():
|
||||
requires = {"wood": "lumber camp"}
|
||||
# what if more villagers than farms?
|
||||
|
||||
```
|
||||
|
||||
|
||||
# Assumptions
|
||||
|
||||
* Fixed gather rate for each resource
|
||||
* what about food -- hunt vs farm?
|
||||
* At least one TC always exists
|
||||
* Using no civ bonuses for now
|
||||
|
||||
69
runner.py
69
runner.py
@@ -1,69 +0,0 @@
|
||||
from aoe.exceptions import *
|
||||
from aoe.types import *
|
||||
from aoe.gameplay import *
|
||||
#import ipdb; breakpoint()
|
||||
|
||||
|
||||
# for testing
|
||||
# TODO need to go back through old tasks when new housing is added
|
||||
# TODO wherever we test against Age, it's probably wrong
|
||||
# TODO add building to production_available when finished
|
||||
# TODO need some warning, penalty, etc. if villager is assigned to task
|
||||
# without appropriate building (e.g., mining with no camp)
|
||||
# TODO building time assumes just 1 vil is working for now
|
||||
# TODO need efficiency penalties on, e.g., "stale" lumber camp
|
||||
|
||||
|
||||
|
||||
# Set up world
|
||||
game = Gameplay()
|
||||
|
||||
# should work even before we do anything
|
||||
#game.print_status()
|
||||
|
||||
game.queue_villager(5, Tasks.WOOD) # first number is time in seconds
|
||||
|
||||
# should generate exception
|
||||
#game.assign_villager(40, Tasks.SHEEP, Tasks.WOOD)
|
||||
|
||||
game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP)
|
||||
game.assign_villager(2, Tasks.IDLE, Tasks.SHEEP, 2)
|
||||
|
||||
# should generate exception
|
||||
#game.assign_villager(0, Tasks.IDLE, Tasks.SHEEP)
|
||||
|
||||
# should generate exception
|
||||
#game.build(45, Buildings.STABLE, Tasks.WOOD, Tasks.IDLE)
|
||||
|
||||
#game.build(45, Buildings.LUMBERCAMP, Tasks.WOOD, Tasks.IDLE)
|
||||
game.build(45, Buildings.LUMBERCAMP, Tasks.WOOD, Tasks.WOOD)
|
||||
|
||||
game.queue_research(45, Research.LOOM)
|
||||
|
||||
game.queue_villager(45, Tasks.WOOD)
|
||||
game.queue_villager(45, Tasks.WOOD)
|
||||
|
||||
game.play_until(46)
|
||||
game.print_status()
|
||||
|
||||
game.build(100, Buildings.HOUSE, [Tasks.SHEEP, Tasks.WOOD], [Tasks.IDLE]*2)
|
||||
#game.assign_villager(Build.Stable(), "food", wait_until_finished=True)
|
||||
|
||||
'''
|
||||
game.queue_unit(s1, Scout)
|
||||
'''
|
||||
|
||||
game.assign_villager(130, Tasks.IDLE, Tasks.WOOD, 2)
|
||||
|
||||
# should generate exception
|
||||
#game.build(200, Buildings.FARM, [Tasks.SHEEP], [Tasks.WOOD])
|
||||
|
||||
game.build(200, Buildings.MILL, [Tasks.SHEEP], [Tasks.WOOD])
|
||||
game.build(250, Buildings.FARM, [Tasks.SHEEP, Tasks.WOOD], [Tasks.IDLE]*2)
|
||||
|
||||
# Evaluate game
|
||||
game.play_until(800)
|
||||
game.print_status()
|
||||
|
||||
# should generate exception
|
||||
#game.assign_villager(0, Tasks.SHEEP, Tasks.WOOD)
|
||||
1125
tests/test.py
Normal file
1125
tests/test.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user