Combine unit and research functions
This commit is contained in:
@@ -12,3 +12,5 @@ class Housing():
|
|||||||
Buildings.HOUSE: 5,
|
Buildings.HOUSE: 5,
|
||||||
Buildings.TOWNCENTER: 5,
|
Buildings.TOWNCENTER: 5,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
self.required = defaultdict(int, {u: 1 for u in Units})
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from aoe.types import *
|
from aoe.types import *
|
||||||
|
|
||||||
|
|
||||||
@@ -7,7 +9,7 @@ class Production():
|
|||||||
|
|
||||||
# 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 = {
|
self.building = defaultdict(None, {
|
||||||
Units.VILLAGER: Buildings.TOWNCENTER,
|
Units.VILLAGER: Buildings.TOWNCENTER,
|
||||||
Units.TRADECART: Buildings.MARKET,
|
Units.TRADECART: Buildings.MARKET,
|
||||||
Units.SPEARMAN: Buildings.BARRACKS,
|
Units.SPEARMAN: Buildings.BARRACKS,
|
||||||
@@ -93,4 +95,4 @@ class Production():
|
|||||||
Research.LOOM: Buildings.TOWNCENTER,
|
Research.LOOM: Buildings.TOWNCENTER,
|
||||||
Research.SAPPERS: Buildings.CASTLE,
|
Research.SAPPERS: Buildings.CASTLE,
|
||||||
Research.BOMBARDTOWER: Buildings.UNIVERSITY,
|
Research.BOMBARDTOWER: Buildings.UNIVERSITY,
|
||||||
}
|
})
|
||||||
|
|||||||
180
aoe/world.py
180
aoe/world.py
@@ -54,6 +54,7 @@ class World():
|
|||||||
# possibly other things
|
# possibly other things
|
||||||
# TODO gathering boars and hunting
|
# TODO gathering boars and hunting
|
||||||
# spend villager time in exchange for adding gatherable resource
|
# spend villager time in exchange for adding gatherable resource
|
||||||
|
# TODO food spoilage
|
||||||
# TODO use market for buy/sell
|
# TODO use market for buy/sell
|
||||||
|
|
||||||
|
|
||||||
@@ -68,19 +69,22 @@ class World():
|
|||||||
self.resources[i] -= self.costs.costs[purchase][i]
|
self.resources[i] -= self.costs.costs[purchase][i]
|
||||||
|
|
||||||
|
|
||||||
def _check_research_dependencies(self, research):
|
def _check_dependencies(self, thing):
|
||||||
self._check_dependencies(self.research_deps.dependencies[research])
|
thing_type = type(thing)
|
||||||
|
|
||||||
|
if thing_type == Ages:
|
||||||
|
self._check_age_dependencies(thing)
|
||||||
|
else:
|
||||||
|
if thing_type == Buildings:
|
||||||
|
dependencies = self.building_deps.dependencies[thing]
|
||||||
|
elif thing_type == Units:
|
||||||
|
dependencies = self.unit_deps.dependencies[thing]
|
||||||
|
elif thing_type == Research:
|
||||||
|
dependencies = self.research_deps.dependencies[thing]
|
||||||
|
self._check_non_age_dependencies(dependencies)
|
||||||
|
|
||||||
|
|
||||||
def _check_building_dependencies(self, building):
|
def _check_non_age_dependencies(self, dependencies):
|
||||||
self._check_dependencies(self.building_deps.dependencies[building])
|
|
||||||
|
|
||||||
|
|
||||||
def _check_unit_dependencies(self, unit):
|
|
||||||
self._check_dependencies(self.unit_deps.dependencies[unit])
|
|
||||||
|
|
||||||
|
|
||||||
def _check_dependencies(self, dependencies):
|
|
||||||
for dep in dependencies:
|
for dep in dependencies:
|
||||||
if dep not in self.buildings and \
|
if dep not in self.buildings and \
|
||||||
dep not in self.research and \
|
dep not in self.research and \
|
||||||
@@ -103,13 +107,6 @@ class World():
|
|||||||
raise MissingDependencyException
|
raise MissingDependencyException
|
||||||
|
|
||||||
|
|
||||||
def queue_research(self, research):
|
|
||||||
self._check_research_dependencies(research)
|
|
||||||
self._pay_cost(research)
|
|
||||||
|
|
||||||
self._start_research(research)
|
|
||||||
|
|
||||||
|
|
||||||
def _schedule_next_gather_rate_decrease(self, task):
|
def _schedule_next_gather_rate_decrease(self, task):
|
||||||
if task in [Tasks.IDLE, Tasks.BUILD]:
|
if task in [Tasks.IDLE, Tasks.BUILD]:
|
||||||
return
|
return
|
||||||
@@ -254,7 +251,8 @@ class World():
|
|||||||
villagers_from_count = Counter(villagers_from)
|
villagers_from_count = Counter(villagers_from)
|
||||||
|
|
||||||
self._check_villager_move(villagers_from_count)
|
self._check_villager_move(villagers_from_count)
|
||||||
self._check_building_dependencies(building)
|
|
||||||
|
self._check_dependencies(building)
|
||||||
|
|
||||||
self._pay_cost(building)
|
self._pay_cost(building)
|
||||||
self._move_villagers_to_build(villagers_from_count)
|
self._move_villagers_to_build(villagers_from_count)
|
||||||
@@ -342,60 +340,40 @@ class World():
|
|||||||
|
|
||||||
def _free_housing(self):
|
def _free_housing(self):
|
||||||
pop_space = self._get_population_space()
|
pop_space = self._get_population_space()
|
||||||
original_queue_len = len(self.production_queue)
|
|
||||||
|
|
||||||
# Assumes each unit costs 1 pop
|
|
||||||
new_builds = []
|
|
||||||
i = 0
|
i = 0
|
||||||
while (self._get_effective_population() < pop_space and
|
while (self._get_effective_population() < pop_space and
|
||||||
i < original_queue_len):
|
i < len(self.production_queue)):
|
||||||
|
|
||||||
unit = self.production_queue[i]
|
thing = self.production_queue[i]
|
||||||
if self._check_production(unit):
|
if self._check_housing(thing) and self._check_production(thing):
|
||||||
if unit in Units:
|
self._start_producing(thing)
|
||||||
self._build_unit(unit)
|
del self.production_queue[i]
|
||||||
elif unit in Research:
|
else:
|
||||||
self._start_research(unit)
|
i += 1
|
||||||
new_builds.append(i)
|
|
||||||
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
self.production_queue = [self.production_queue[i]
|
|
||||||
for i in range(original_queue_len)
|
|
||||||
if i not in new_builds]
|
|
||||||
|
|
||||||
|
|
||||||
def _free_production_building_of_unit(self, unit):
|
def _free_production_building_of_thing(self, thing):
|
||||||
building = self.production.building[unit]
|
building = self.production.building[thing]
|
||||||
self._free_production_building(building)
|
self._free_production_building(building)
|
||||||
|
|
||||||
|
|
||||||
def _free_production_building(self, building):
|
def _free_production_building(self, building):
|
||||||
self.production_available[building] += 1
|
self.production_available[building] += 1
|
||||||
|
|
||||||
# Start the next queued unit if one exists
|
# Start the next queued thing if one exists
|
||||||
ready_index = None
|
ready_index = None
|
||||||
ready_unit = None
|
ready_thing = None
|
||||||
for i in range(len(self.production_queue)):
|
for i in range(len(self.production_queue)):
|
||||||
unit = self.production_queue[i]
|
thing = self.production_queue[i]
|
||||||
if self.production.building[unit] == building:
|
if self.production.building[thing] == building:
|
||||||
ready_index = i
|
ready_index = i
|
||||||
ready_unit = unit
|
ready_thing = thing
|
||||||
break
|
break
|
||||||
|
|
||||||
if ready_index:
|
if ready_index:
|
||||||
self.production_queue.remove(i)
|
self.production_queue.remove(i)
|
||||||
if unit in Units:
|
self._start_producing(thing)
|
||||||
self._build_unit(unit)
|
|
||||||
elif unit in Research:
|
|
||||||
self._start_research(unit)
|
|
||||||
|
|
||||||
|
|
||||||
def _check_housing(self, unit):
|
|
||||||
# assumes for now that all units cost 1 pop
|
|
||||||
pop_space = sum([self.housing.space[building] for building in self.buildings])
|
|
||||||
if self._get_effective_population() >= pop_space:
|
|
||||||
raise YallGotHousedException
|
|
||||||
|
|
||||||
|
|
||||||
def _check_gather(self, task, count=1):
|
def _check_gather(self, task, count=1):
|
||||||
@@ -415,51 +393,37 @@ class World():
|
|||||||
raise GatherBuildingNotAvailableException
|
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):
|
def _check_production(self, unit):
|
||||||
building = self.production.building[unit]
|
building = self.production.building[unit]
|
||||||
if self.production_available[building] < 1:
|
if self.production_available[building] < 1:
|
||||||
raise ProductionBuildingNotAvailableException
|
raise ProductionBuildingNotAvailableException
|
||||||
|
|
||||||
|
|
||||||
def _use_production_for_research(self, research):
|
def _use_housing_and_production(self, thing):
|
||||||
self._check_production(research)
|
self._check_housing(thing)
|
||||||
|
self._check_production(thing)
|
||||||
|
|
||||||
building = self.production.building[research]
|
building = self.production.building[thing]
|
||||||
self.production_available[building] -= 1
|
if building is not None:
|
||||||
|
self.production_available[building] -= 1
|
||||||
|
|
||||||
|
self.units_being_built += self.housing.required[thing]
|
||||||
def _add_research(self, research):
|
|
||||||
self.research.append(research)
|
|
||||||
ResearchEffects.complete_research(self, research)
|
|
||||||
self._free_production_building_of_unit(research)
|
|
||||||
|
|
||||||
|
|
||||||
def _start_research(self, research):
|
|
||||||
try:
|
|
||||||
self._use_production_for_research(research)
|
|
||||||
except ProductionBuildingNotAvailableException:
|
|
||||||
self.production_queue.append(research)
|
|
||||||
else:
|
|
||||||
update_time = self.current_time + self.build_times.build_times[research]
|
|
||||||
self.events[update_time].append((self._add_research, (research,)))
|
|
||||||
|
|
||||||
|
|
||||||
def _use_housing_and_production(self, unit):
|
|
||||||
self._check_housing(unit)
|
|
||||||
self._check_production(unit)
|
|
||||||
|
|
||||||
building = self.production.building[unit]
|
|
||||||
|
|
||||||
self.production_available[building] -= 1
|
|
||||||
self.units_being_built += 1
|
|
||||||
|
|
||||||
|
|
||||||
def _kill_unit(self, unit, task=None):
|
def _kill_unit(self, unit, task=None):
|
||||||
if unit == Units.VILLAGER and not task:
|
if unit == Units.VILLAGER and not task:
|
||||||
raise IllegalVillagerMoveException
|
raise IllegalVillagerMoveException
|
||||||
|
|
||||||
# assumes population of 1 for every unit
|
self.population -= self.housing.required(unit)
|
||||||
self.population -= 1
|
|
||||||
|
|
||||||
if unit == Units.VILLAGER:
|
if unit == Units.VILLAGER:
|
||||||
self.assign_villager(task, None)
|
self.assign_villager(task, None)
|
||||||
@@ -467,39 +431,49 @@ class World():
|
|||||||
self._free_housing()
|
self._free_housing()
|
||||||
|
|
||||||
|
|
||||||
def _add_unit(self, unit, initial_task):
|
def _add_thing(self, thing, initial_task):
|
||||||
# assumes population of 1 for every unit
|
self.units_being_built -= self.housing.required[thing]
|
||||||
self.units_being_built -= 1
|
self.population += self.housing.required[thing]
|
||||||
self.population += 1
|
|
||||||
self._free_production_building_of_unit(unit)
|
|
||||||
|
|
||||||
if unit == Units.VILLAGER:
|
if thing in Research:
|
||||||
|
self.research.append(thing)
|
||||||
|
ResearchEffects.complete_research(self, thing)
|
||||||
|
|
||||||
|
self._free_production_building_of_thing(thing)
|
||||||
|
|
||||||
|
if thing == Units.VILLAGER:
|
||||||
self.villagers += 1
|
self.villagers += 1
|
||||||
self.villager_tasks[Tasks.IDLE] += 1
|
self.villager_tasks[Tasks.IDLE] += 1
|
||||||
self.assign_villager(Tasks.IDLE, initial_task)
|
self.assign_villager(Tasks.IDLE, initial_task)
|
||||||
|
|
||||||
|
|
||||||
def _build_unit(self, unit, initial_task):
|
def _start_producing(self, thing, initial_task):
|
||||||
try:
|
try:
|
||||||
self._use_housing_and_production(unit)
|
self._use_housing_and_production(thing)
|
||||||
except ProductionBuildingNotAvailableException:
|
except ProductionBuildingNotAvailableException:
|
||||||
self.production_queue.append(unit)
|
self.production_queue.append(thing)
|
||||||
except YallGotHousedException:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
update_time = self.current_time + self.build_times.build_times[unit]
|
update_time = self.current_time + self.build_times.build_times[thing]
|
||||||
self.events[update_time].append((self._add_unit, (unit, initial_task,)))
|
self.events[update_time].append((self._add_thing, (thing, initial_task,)))
|
||||||
|
|
||||||
|
|
||||||
def queue_unit(self, unit, initial_task=None):
|
def _queue_thing(self, thing, initial_task=None):
|
||||||
self._check_unit_dependencies(unit)
|
self._check_dependencies(thing)
|
||||||
self._pay_cost(unit)
|
self._pay_cost(thing)
|
||||||
|
|
||||||
self._build_unit(unit, initial_task)
|
self._start_producing(thing, initial_task)
|
||||||
|
|
||||||
|
|
||||||
|
def queue_research(self, research):
|
||||||
|
self._queue_thing(research)
|
||||||
|
|
||||||
|
|
||||||
|
def queue_unit(self, unit):
|
||||||
|
self._queue_thing(unit)
|
||||||
|
|
||||||
|
|
||||||
def queue_villager(self, initial_task):
|
def queue_villager(self, initial_task):
|
||||||
self.queue_unit(Units.VILLAGER, initial_task)
|
self._queue_thing(Units.VILLAGER, initial_task)
|
||||||
|
|
||||||
|
|
||||||
def _gather_resources_for_duration(self, duration):
|
def _gather_resources_for_duration(self, duration):
|
||||||
|
|||||||
Reference in New Issue
Block a user