def double_left_click_actor(self, act): mods = pygame.key.get_mods() actors_to_select = [] scr_rect = (-self.scroll_x + self.draw_area[0], -self.scroll_y + self.draw_area[1], -self.scroll_x + self.draw_area[2], -self.scroll_y + self.draw_area[3]) for a in self.actors: if a.actor_type == act.actor_type: if actor_lib.is_inside(a, scr_rect): actors_to_select.append(a) if KMOD_SHIFT & mods: # Add to current selection for a in actors_to_select: self.select_actor(a) else: self.selected_actors = [] # Set as current selection for a in actors_to_select: self.select_actor(a) self._selection_has_changed = True
def handle_mousedragup(self, event, drag_rect): if drag_rect == None: return mods = pygame.key.get_mods() self.drag_rect = None # Correct for margins and scroll drag_rect = ( drag_rect[0] - self.draw_margin[0] + self.scroll_x, drag_rect[1] - self.draw_margin[1] + self.scroll_y, drag_rect[2] - self.draw_margin[0] + self.scroll_x, drag_rect[3] - self.draw_margin[1] + self.scroll_y, ) contains_friendly = False short_list = [] if event.button == 1: if not KMOD_SHIFT & mods: self.unselect_all_actors() # First see if there are friendlies there # if the selection contains friendlies then we # should only select the friendlies for a in self.actors: if actor_lib.is_inside(a, drag_rect): if a.team == self.player_team: contains_friendly = True short_list.append(a) # contains_friendly for a in short_list: if contains_friendly: if a.team == self.player_team: self.select_actor(a) else: self.select_actor(a)
def inspect_bases(self): # First find out which buildings we are missing for base_name, base_data in self.bases.items(): base_area = ( base_data['location'][0] - base_data['size'][0], base_data['location'][1] - base_data['size'][1], base_data['location'][0] + base_data['size'][0], base_data['location'][1] + base_data['size'][1], ) buildings_needed = set(base_data['buildings']) # Reset this now base_data['current_buildings'] = [] # Get all buildings within this base found_buildings = {} total_needed = {} for b in buildings_needed: found_buildings[b] = 0 total_needed[b] = 0 for b in base_data['buildings']: total_needed[b] += 1 # Loop through all actors and see what we've got for i, a in self.own_actors.items(): if actor_lib.is_inside(a, base_area): if a.actor_type in buildings_needed: found_buildings[a.actor_type] += 1 if a.completion >= 100: base_data['current_buildings'].append(a.oid) # Now also loop through all the buildings_in_progress # Use a range loop so we can update the list as we go for i in range( len(base_data['buildings_in_progress']) - 1, -1, -1): b, ttl = base_data['buildings_in_progress'][i] if ttl <= 0: del (base_data['buildings_in_progress'][i]) continue base_data['buildings_in_progress'][i][1] -= 1 if b in found_buildings: found_buildings[b] += 1 # Now find out what we are missing missing = {} for b in buildings_needed: m = total_needed[b] - found_buildings[b] if m > 0: missing[b] = m # None missing? We can stop looking around now if missing == {}: continue # Now find out which builders we can use # Narrow down our list of builders so we don't later iterate over # non-builders more than once builders = [] builders_used = [] for aid, a in self.own_actors.items(): if self.actor_types[a.actor_type]['can_construct']: if a.current_order[0] == "stop": builders.append(a) # Now we work out which buildings we can build and who is closest to it for building_type, amount in missing.items(): for i in range(amount): target_pos = base_data['location'] closest_builder = None, 9999999 for b in builders: if b in builders_used: continue if actor_lib.can_build( builder_type=self.actor_types[b.actor_type], item_type=self.actor_types[building_type], build_lists=self.build_lists, ): # If they are closest we want them to go build it dist = vectors.distance(target_pos, b.pos) if dist < closest_builder[1]: closest_builder = b, dist # Now remove them from the pool and issue the build order if closest_builder[0] != None: builders_used.append(closest_builder[0]) self.issue_orders(closest_builder[0].oid, cmd="build", pos=target_pos, target=building_type) base_data['buildings_in_progress'].append( [building_type, buildings_in_progress_ttl])