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:
Nate Bowman
2026-07-30 19:33:05 -04:00
parent d7dcc0d873
commit e25abd87ab
4 changed files with 44 additions and 40 deletions

View File

@@ -166,7 +166,7 @@ class World():
raise IllegalVillagerMoveException
if new_task:
self._check_gather(new_task, count)
self.gather.check_gather(self, new_task, count)
self.villager_tasks[new_task] += count
else:
self.villagers -= 1
@@ -291,24 +291,25 @@ class World():
self._handle_events()
def _get_population_space(self):
def get_population_space(self):
space = sum([self.housing.space[building] for building in self.buildings])
return min(space, 200)
def _get_effective_population(self):
def get_effective_population(self):
return self.population + self.units_being_built
def _free_housing(self):
pop_space = self._get_population_space()
pop_space = self.get_population_space()
i = 0
while (self._get_effective_population() < pop_space and
while (self.get_effective_population() < pop_space and
i < len(self.production_queue)):
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)
del self.production_queue[i]
else:
@@ -338,41 +339,9 @@ class World():
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):
self._check_housing(thing)
self._check_production(thing)
self.housing.check_housing(self, thing)
self.production.check_production(self, thing)
building = self.production.building[thing]
if building is not None: