Move more code into separate classes
It might make logical sense. Honestly, I'm just tired of the code for World being so long.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import copy
|
import copy
|
||||||
|
|
||||||
from aoe.types import *
|
from aoe.types import *
|
||||||
|
from aoe.exceptions import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -136,6 +137,23 @@ class Gather():
|
|||||||
BIG_NUMBER = 1e6
|
BIG_NUMBER = 1e6
|
||||||
|
|
||||||
|
|
||||||
|
def check_gather(self, world, task, count=1):
|
||||||
|
if task in [Tasks.IDLE, Tasks.BUILD]:
|
||||||
|
return
|
||||||
|
|
||||||
|
required = self.required_gather_buildings[task]
|
||||||
|
have = set(world.buildings)
|
||||||
|
|
||||||
|
match = any(map(have.issuperset, required))
|
||||||
|
|
||||||
|
if not match:
|
||||||
|
raise GatherBuildingNotAvailableException
|
||||||
|
|
||||||
|
if task == task.FARM:
|
||||||
|
if world.villager_tasks[task] + count > len(world.unused_farms):
|
||||||
|
raise GatherBuildingNotAvailableException
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.gather_rates = {
|
self.gather_rates = {
|
||||||
Tasks.BERRIES: 18.6/60,
|
Tasks.BERRIES: 18.6/60,
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from aoe.types import *
|
from aoe.types import *
|
||||||
|
from aoe.exceptions import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Housing():
|
class Housing():
|
||||||
|
|
||||||
|
|
||||||
|
def check_housing(self, world, thing):
|
||||||
|
required = self.required[thing]
|
||||||
|
|
||||||
|
if required > 0:
|
||||||
|
pop_space = world.get_population_space()
|
||||||
|
if world.get_effective_population() > pop_space + required:
|
||||||
|
raise YallGotHousedException
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.space = defaultdict(int, {
|
self.space = defaultdict(int, {
|
||||||
Buildings.HOUSE: 5,
|
Buildings.HOUSE: 5,
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from aoe.types import *
|
from aoe.types import *
|
||||||
|
from aoe.exceptions import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Production():
|
class Production():
|
||||||
|
|
||||||
|
|
||||||
|
def check_production(self, world, thing):
|
||||||
|
building = self.building[thing]
|
||||||
|
if world.production_available[building] < 1:
|
||||||
|
raise ProductionBuildingNotAvailableException
|
||||||
|
|
||||||
|
|
||||||
# assumes for now each unit can be built in only one type of building
|
# assumes for now each unit can be built in only one type of building
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.building = defaultdict(None, {
|
self.building = defaultdict(None, {
|
||||||
|
|||||||
49
aoe/world.py
49
aoe/world.py
@@ -166,7 +166,7 @@ class World():
|
|||||||
raise IllegalVillagerMoveException
|
raise IllegalVillagerMoveException
|
||||||
|
|
||||||
if new_task:
|
if new_task:
|
||||||
self._check_gather(new_task, count)
|
self.gather.check_gather(self, new_task, count)
|
||||||
self.villager_tasks[new_task] += count
|
self.villager_tasks[new_task] += count
|
||||||
else:
|
else:
|
||||||
self.villagers -= 1
|
self.villagers -= 1
|
||||||
@@ -291,24 +291,25 @@ class World():
|
|||||||
self._handle_events()
|
self._handle_events()
|
||||||
|
|
||||||
|
|
||||||
def _get_population_space(self):
|
def get_population_space(self):
|
||||||
space = sum([self.housing.space[building] for building in self.buildings])
|
space = sum([self.housing.space[building] for building in self.buildings])
|
||||||
return min(space, 200)
|
return min(space, 200)
|
||||||
|
|
||||||
|
|
||||||
def _get_effective_population(self):
|
def get_effective_population(self):
|
||||||
return self.population + self.units_being_built
|
return self.population + self.units_being_built
|
||||||
|
|
||||||
|
|
||||||
def _free_housing(self):
|
def _free_housing(self):
|
||||||
pop_space = self._get_population_space()
|
pop_space = self.get_population_space()
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while (self._get_effective_population() < pop_space and
|
while (self.get_effective_population() < pop_space and
|
||||||
i < len(self.production_queue)):
|
i < len(self.production_queue)):
|
||||||
|
|
||||||
thing = self.production_queue[i]
|
thing = self.production_queue[i]
|
||||||
if self._check_housing(thing) and self._check_production(thing):
|
if self.housing.check_housing(self, thing) and \
|
||||||
|
self.production.check_production(self, thing):
|
||||||
self._start_producing(thing)
|
self._start_producing(thing)
|
||||||
del self.production_queue[i]
|
del self.production_queue[i]
|
||||||
else:
|
else:
|
||||||
@@ -338,41 +339,9 @@ class World():
|
|||||||
self._start_producing(thing)
|
self._start_producing(thing)
|
||||||
|
|
||||||
|
|
||||||
def _check_gather(self, task, count=1):
|
|
||||||
if task in [Tasks.IDLE, Tasks.BUILD]:
|
|
||||||
return
|
|
||||||
|
|
||||||
required = self.gather.required_gather_buildings[task]
|
|
||||||
have = set(self.buildings)
|
|
||||||
|
|
||||||
match = any(map(have.issuperset, required))
|
|
||||||
|
|
||||||
if not match:
|
|
||||||
raise GatherBuildingNotAvailableException
|
|
||||||
|
|
||||||
if task == task.FARM:
|
|
||||||
if self.villager_tasks[task] + count > len(self.unused_farms):
|
|
||||||
raise GatherBuildingNotAvailableException
|
|
||||||
|
|
||||||
|
|
||||||
def _check_housing(self, thing):
|
|
||||||
required = self.housing.required[thing]
|
|
||||||
|
|
||||||
if required > 0:
|
|
||||||
pop_space = self._get_population_space()
|
|
||||||
if self._get_effective_population() > pop_space + required:
|
|
||||||
raise YallGotHousedException
|
|
||||||
|
|
||||||
|
|
||||||
def _check_production(self, unit):
|
|
||||||
building = self.production.building[unit]
|
|
||||||
if self.production_available[building] < 1:
|
|
||||||
raise ProductionBuildingNotAvailableException
|
|
||||||
|
|
||||||
|
|
||||||
def _use_housing_and_production(self, thing):
|
def _use_housing_and_production(self, thing):
|
||||||
self._check_housing(thing)
|
self.housing.check_housing(self, thing)
|
||||||
self._check_production(thing)
|
self.production.check_production(self, thing)
|
||||||
|
|
||||||
building = self.production.building[thing]
|
building = self.production.building[thing]
|
||||||
if building is not None:
|
if building is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user