Exemple #1
0
    def double_left_click_actor(self, the_actor):
        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 aid, a in self.sim.actors.items():
            if a.actor_type == the_actor.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
Exemple #2
0
    def double_left_click_actor(self, the_actor):
        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 aid, a in self.sim.actors.items():
            if a.actor_type == the_actor.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
Exemple #3
0
 def _handle_mousedragup(self, event):
     self.true_mousedrag_at = None
     self.scrolled_mousedrag_at = None
     
     if self.scrolled_mousedown_at == None:
         return self.handle_mousedragup(event, None)
     
     scrolled_mouse_pos = (
         event.pos[0] + self.scroll_x,
         event.pos[1] + self.scroll_y
     )
     
     drag_rect = (
         min(self.scrolled_mousedown_at[0], scrolled_mouse_pos[0]),
         min(self.scrolled_mousedown_at[1], scrolled_mouse_pos[1]),
         max(self.scrolled_mousedown_at[0], scrolled_mouse_pos[0]),
         max(self.scrolled_mousedown_at[1], scrolled_mouse_pos[1]),
     )
     
     contains_friendly = False
     short_list = []
     
     mods = pygame.key.get_mods()
     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 aid, a in self.sim.actors.items():
             if actor_lib.is_inside(a, drag_rect):
                 if a.team == self.sim.player_team:
                     contains_friendly = True
                 short_list.append(a)
         
         # Now to select them
         for a in short_list:
             if contains_friendly:
                 if a.team == self.sim.player_team:
                     self.select_actor(a)
             else:
                 self.select_actor(a)
     
     self.handle_mousedragup(event, drag_rect)
Exemple #4
0
    def _handle_mousedragup(self, event):
        self.true_mousedrag_at = None
        self.scrolled_mousedrag_at = None

        if self.scrolled_mousedown_at == None:
            return self.handle_mousedragup(event, None)

        scrolled_mouse_pos = (event.pos[0] + self.scroll_x,
                              event.pos[1] + self.scroll_y)

        drag_rect = (
            min(self.scrolled_mousedown_at[0], scrolled_mouse_pos[0]),
            min(self.scrolled_mousedown_at[1], scrolled_mouse_pos[1]),
            max(self.scrolled_mousedown_at[0], scrolled_mouse_pos[0]),
            max(self.scrolled_mousedown_at[1], scrolled_mouse_pos[1]),
        )

        contains_friendly = False
        short_list = []

        mods = pygame.key.get_mods()
        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 aid, a in self.sim.actors.items():
                if actor_lib.is_inside(a, drag_rect):
                    if a.team == self.sim.player_team:
                        contains_friendly = True
                    short_list.append(a)

            # Now to select them
            for a in short_list:
                if contains_friendly:
                    if a.team == self.sim.player_team:
                        self.select_actor(a)
                else:
                    self.select_actor(a)

        self.handle_mousedragup(event, drag_rect)
    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])
    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])