Exemple #1
0
    def execute(self):
        (result, building) = super(LumberjackEvaluator, self).execute()
        if result != BUILD_RESULT.OK:
            return (result, None)

        production_builder = self.area_builder
        coastline = production_builder.land_manager.coastline
        island_ground_map = production_builder.island.ground_map
        forest_coords_list = []
        for coords in building.position.get_radius_coordinates(
                Entities.buildings[BUILDINGS.LUMBERJACK].radius):
            if coords in production_builder.plan and production_builder.plan[
                    coords][
                        0] == BUILDING_PURPOSE.NONE and coords not in coastline:
                ok = False
                if island_ground_map[
                        coords].object is not None and island_ground_map[
                            coords].object.id == BUILDINGS.TREE:
                    ok = True
                else:
                    builder = BasicBuilder(BUILDINGS.TREE, coords, 0)
                    if not builder.have_resources(
                            production_builder.land_manager):
                        break
                    if builder:
                        assert builder.execute(production_builder.land_manager)
                        ok = True
                if ok:
                    forest_coords_list.append(coords)

        production_builder.register_change_list(forest_coords_list,
                                                BUILDING_PURPOSE.TREE, None)

        return (BUILD_RESULT.OK, building)
Exemple #2
0
    def execute(self):
        # TODO Add a check that figures out if all trees that should be planted are in range of the settlement.
        # If not, return range missing result
        (result, building) = super(LumberjackEvaluator, self).execute()
        if result != BUILD_RESULT.OK:
            return (result, None)

        production_builder = self.area_builder
        coastline = production_builder.land_manager.coastline
        island_ground_map = production_builder.island.ground_map
        forest_coords_list = []
        for coords in building.position.get_radius_coordinates(
                Entities.buildings[BUILDINGS.LUMBERJACK].radius):
            if coords in production_builder.plan and production_builder.plan[
                    coords][
                        0] == BUILDING_PURPOSE.NONE and coords not in coastline:
                if island_ground_map[
                        coords].object is not None and island_ground_map[
                            coords].object.id == BUILDINGS.TREE:
                    forest_coords_list.append(coords)
                elif island_ground_map[
                        coords].settlement is not None and island_ground_map[
                            coords].settlement.owner is self.area_builder.owner:
                    builder = BasicBuilder(BUILDINGS.TREE, coords, 0)
                    if not builder.have_resources(
                            production_builder.land_manager):
                        break
                    if builder:
                        assert builder.execute(production_builder.land_manager)
                        forest_coords_list.append(coords)

        production_builder.register_change_list(forest_coords_list,
                                                BUILDING_PURPOSE.TREE, None)

        return (BUILD_RESULT.OK, building)
Exemple #3
0
    def build_tent(self, coords=None):
        """Build the next tent (or the specified one if coords is not None)."""
        if not self.tent_queue:
            return BUILD_RESULT.IMPOSSIBLE

        # can_trigger_next_section is used to start building the next section when the old one is done
        # if a tent is built just to extend the area then that can't trigger the next section
        # TODO: handle extension tents nicer than just letting them die
        can_trigger_next_section = False
        if coords is None:
            coords = self.tent_queue[0]
            can_trigger_next_section = True

        ok = True
        x, y = coords
        owned_by_other = False
        size = Entities.buildings[BUILDINGS.RESIDENTIAL].size
        for dx in xrange(size[0]):
            for dy in xrange(size[1]):
                coords2 = (x + dx, y + dy)
                if coords2 not in self.settlement.ground_map:
                    ok = False
                    if self.island.ground_map[coords2].settlement is not None:
                        owned_by_other = True

        if ok and not owned_by_other:
            if not self.have_resources(BUILDINGS.RESIDENTIAL):
                return BUILD_RESULT.NEED_RESOURCES
            assert BasicBuilder(BUILDINGS.RESIDENTIAL, (x, y),
                                0).execute(self.land_manager)

        if ok or owned_by_other:
            if self.tent_queue[0] == coords:
                self.tent_queue.popleft()
            else:
                for i in xrange(len(self.tent_queue)):
                    if self.tent_queue[i] == coords:
                        del self.tent_queue[i]
                        break
            if owned_by_other:
                self.log.debug(
                    '%s tent position owned by other player at (%d, %d)', self,
                    x, y)
                return BUILD_RESULT.IMPOSSIBLE

        if not ok:
            # need to extends the area, it is not owned by another player
            self.log.debug(
                '%s tent position not owned by the player at (%d, %d), extending settlement area instead',
                self, x, y)
            return self.extend_settlement(
                Rect.init_from_topleft_and_size(x, y, size[0], size[1]))

        if not self.roads_built:
            self.build_roads()
        if can_trigger_next_section and self.plan[coords][1][
                0] > self.current_section:
            self.current_section = self.plan[coords][1][0]
        return BUILD_RESULT.OK
 def build_road(self, path):
     """Build the road given a valid path or None. Return True if it worked, False if the path was None."""
     if path is not None:
         for x, y in path:
             self.register_change_list([(x, y)], BUILDING_PURPOSE.ROAD,
                                       None)
             building = self.island.ground_map[(x, y)].object
             if building is not None and building.id == BUILDINGS.TRAIL:
                 continue
             assert BasicBuilder(BUILDINGS.TRAIL, (x, y),
                                 0).execute(self.land_manager)
     return path is not None
	def _reached_destination_area(self):
		self.log.info('%s reached BO area', self)

		builder = BasicBuilder(BUILDINGS.WAREHOUSE, self.coords, 0)
		self.warehouse = builder.execute(self.land_manager, ship=self.ship)
		if not self.warehouse:
			self.report_failure('Unable to build the warehouse')
			return

		self.land_manager.settlement = self.warehouse.settlement
		self.log.info('%s built the warehouse', self)

		self._unload_all_resources(self.land_manager.settlement)
		self.report_success('Built the warehouse, transferred resources')
Exemple #6
0
	def build(self, settlement_manager, resource_id):
		production_builder = settlement_manager.production_builder
		purpose = self.get_purpose(resource_id)
		if not production_builder.unused_fields[purpose]:
			return (BUILD_RESULT.NEED_PARENT_FIRST, None)
		if not self.have_resources(settlement_manager):
			return (BUILD_RESULT.NEED_RESOURCES, None)

		assert production_builder.unused_fields[purpose], 'expected field spot to be available'
		coords = production_builder.unused_fields[purpose][0]
		building = BasicBuilder(self.id, coords, 0).execute(settlement_manager.land_manager)
		assert building

		production_builder.unused_fields[purpose].popleft()
		production_builder.register_change_list([coords], purpose, None)
		return (BUILD_RESULT.OK, building)
	def _reached_destination_area(self):
		self.log.info('%s reached BO area', self)

		builder = BasicBuilder(BUILDINGS.WAREHOUSE, self.coords, 0)
		if not builder.have_resources(self.land_manager, ship=self.ship):
			self.report_failure('Not enough resources for a warehouse at {}'.format(str(self.coords)))
			return

		self.warehouse = builder.execute(self.land_manager, ship=self.ship)
		assert self.warehouse

		self.land_manager.settlement = self.warehouse.settlement
		self.log.info('%s built the warehouse', self)

		self._unload_all_resources(self.land_manager.settlement)
		self.report_success('Built the warehouse, transferred resources')
Exemple #8
0
	def build_roads(self):
		"""Try to build all roads in the village area, record the result in the field roads_built."""
		all_built = True
		for coords, (purpose, (section, _)) in sorted(self.plan.items()):
			if section > self.current_section or coords not in self.settlement.ground_map:
				all_built = False
				continue
			if purpose != BUILDING_PURPOSE.ROAD:
				continue
			object = self.settlement.ground_map[coords].object
			if object is not None and not object.buildable_upon:
				continue
			if not self.have_resources(BUILDINGS.TRAIL):
				all_built = False
				break
			assert BasicBuilder(BUILDINGS.TRAIL, coords, 0).execute(self.land_manager)
		self.roads_built = all_built
    def build(self, settlement_manager, resource_id):
        village_builder = settlement_manager.village_builder
        building_purpose = self.get_purpose(resource_id)
        building_id = BUILDING_PURPOSE.get_building(building_purpose)
        building_class = Entities.buildings[building_id]

        for coords, (purpose, (section,
                               _)) in village_builder.plan.iteritems():
            if section > village_builder.current_section or purpose != building_purpose:
                continue

            object = village_builder.land_manager.island.ground_map[
                coords].object
            if object is not None and object.id == self.id:
                continue

            if building_purpose != BUILDING_PURPOSE.MAIN_SQUARE:
                if not self._need_producer(settlement_manager, coords,
                                           resource_id):
                    continue

            if not village_builder.have_resources(building_id):
                return (BUILD_RESULT.NEED_RESOURCES, None)
            if coords not in village_builder.settlement.buildability_cache.cache[
                    building_class.size]:
                position = Rect.init_from_topleft_and_size_tuples(
                    coords, building_class.size)
                return (BUILD_RESULT.OUT_OF_SETTLEMENT, position)

            building = BasicBuilder(building_id, coords,
                                    0).execute(settlement_manager.land_manager)
            assert building
            if self.get_purpose(
                    resource_id
            ) == BUILDING_PURPOSE.MAIN_SQUARE and not village_builder.roads_built:
                village_builder.build_roads()
            return (BUILD_RESULT.OK, building)
        return (BUILD_RESULT.SKIP, None)