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.
166 lines
5.5 KiB
Python
166 lines
5.5 KiB
Python
from collections import defaultdict
|
|
|
|
from aoe.exceptions import *
|
|
from aoe.types import *
|
|
from aoe.world import *
|
|
|
|
|
|
|
|
class Gameplay():
|
|
|
|
|
|
def print_status(self):
|
|
print('-'*80)
|
|
print(f'Current time: {self.world.current_time}')
|
|
print()
|
|
print(f'Civ: {self.world.civ}')
|
|
print(f'Age: {self.world.age}')
|
|
print(f'Population: {self.world.population}')
|
|
print()
|
|
print(f'Villagers: {self.world.villagers}')
|
|
print(f'Tasks: {[(t, c) for t, c in self.world.villager_tasks.items() if c != 0]}')
|
|
print()
|
|
print(f'Resources: {[int(r) for r in self.world.gather.resources]}')
|
|
print()
|
|
print(f'Buildings: {self.world.buildings}')
|
|
print()
|
|
print(f'Research: {self.world.research}')
|
|
print('-'*80)
|
|
print()
|
|
|
|
|
|
def build(self, action_time, building, villagers_from, villagers_to):
|
|
self._check_time(action_time)
|
|
self.actions[action_time].append((self.world.build,
|
|
(building,
|
|
villagers_from,
|
|
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_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):
|
|
self._check_time(action_time)
|
|
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 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):
|
|
if self.world.current_time > action_time:
|
|
raise CannotChangeThePastException
|
|
|
|
|
|
def _get_next_action_time(self):
|
|
try:
|
|
next_action_time = min(self.actions.keys())
|
|
except ValueError:
|
|
next_action_time = self.end_time
|
|
|
|
return next_action_time
|
|
|
|
|
|
def _play_without_actions(self, end_time):
|
|
self.world.advance_until_time(end_time)
|
|
|
|
|
|
def _handle_actions(self):
|
|
for func, args in self.actions[self.world.current_time]:
|
|
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]
|
|
|
|
|
|
def play_until(self, end_time):
|
|
self.end_time = end_time
|
|
while self.world.current_time < self.end_time:
|
|
next_action_time = self._get_next_action_time()
|
|
self._play_without_actions(min(next_action_time, self.end_time))
|
|
self._handle_actions()
|
|
|
|
|
|
def __init__(self):
|
|
self.world = World()
|
|
self.actions = defaultdict(list)
|
|
self.end_time = 0
|