示例#1
0
    def calcDistance(self, unit, square):
        ai_id = self.getID()
        unit_square = self.__world.map.getPosition(unit)
        unit_ai_id = self.__getOwner(unit)

        if self.__isVisibleObject(unit, ai_id=ai_id):
            return calcDistance(unit_square, square)
示例#2
0
文件: world.py 项目: Aureus/dmangame
    def placeRandomBuilding(self):
        b = mapobject.Building(self.wt)
        # Make sure this building is not within a distance
        # from any other buildings.
        attempts = 0
        best_min_guess = 0
        best_base = None
        while True:
            min_guess = map_settings.MAP_SIZE**2
            attempts += 1
            rand_square = self.map.getRandomSquare()
            within_range_of_other_building = False
            best_square = None

            dist_from_edge = math.sqrt(map_settings.MAP_SIZE) / 2
            if rand_square[0] < dist_from_edge:
                continue
            if rand_square[1] < dist_from_edge:
                continue
            if map_settings.MAP_SIZE - rand_square[0] < dist_from_edge:
                continue
            if map_settings.MAP_SIZE - rand_square[1] < dist_from_edge:
                continue

            for building in self.buildings:
                pos = self.map.getPosition(building)
                if building == b or not pos:
                    continue
                spawn_distance = map_settings.BUILDING_SPAWN_DISTANCE * math.log(
                    map_settings.MAP_SIZE)
                dist = calcDistance(pos, rand_square)
                if dist < spawn_distance:
                    within_range_of_other_building = True

                if dist < min_guess:
                    min_guess = dist

            if min_guess > best_min_guess:
                best_square = rand_square
                best_min_guess = min_guess

            if not within_range_of_other_building:
                # Redistribute all buildings?
                break

            if attempts >= 5 and best_square:
                log.info(
                    "Couldn't place building far enough away after five tries, taking best guess"
                )
                rand_square = best_square
                break

        self.map.placeObject(b, rand_square)
        return b
示例#3
0
    def placeRandomBuilding(self):
      b = mapobject.Building(self.wt)
      # Make sure this building is not within a distance
      # from any other buildings.
      attempts = 0
      best_min_guess = 0
      best_base = None
      while True:
        min_guess = map_settings.MAP_SIZE**2
        attempts += 1
        rand_square = self.map.getRandomSquare()
        within_range_of_other_building = False
        best_square = None

        dist_from_edge = math.sqrt(map_settings.MAP_SIZE) / 2
        if rand_square[0] < dist_from_edge:
          continue
        if rand_square[1] < dist_from_edge:
          continue
        if map_settings.MAP_SIZE - rand_square[0] < dist_from_edge:
          continue
        if map_settings.MAP_SIZE - rand_square[1] < dist_from_edge:
          continue

        for building in self.buildings:
          pos = self.map.getPosition(building)
          if building == b or not pos:
            continue
          spawn_distance = map_settings.BUILDING_SPAWN_DISTANCE * math.log(map_settings.MAP_SIZE)
          dist = calcDistance(pos, rand_square)
          if dist < spawn_distance:
            within_range_of_other_building = True

          if dist < min_guess:
            min_guess = dist

        if min_guess > best_min_guess:
          best_square = rand_square
          best_min_guess = min_guess

        if not within_range_of_other_building:
          # Redistribute all buildings?
          break

        if attempts >= 5 and best_square:
          log.info("Couldn't place building far enough away after five tries, taking best guess")
          rand_square = best_square
          break

      self.map.placeObject(b, rand_square)
      return b
示例#4
0
    def inRange(self, unit):
        # Find all visible enemy units in range of this unit's firing
        # capabilities.
        self.checkOwner(unit)
        units = []
        om = self.__world.map.objectMap
        unit_square = om[unit]
        unit_ai_id = self.__getOwner(unit)

        for vunit in self.__world.visibleunits[unit_ai_id]:
            square = om[vunit]
            if calcDistance(unit_square, square) < self.__world.bulletRange:
                units.append(vunit)
        return units