Esempio n. 1
0
 def populate_map(self, players, alliances, factions=()):
     # add "true" (non neutral) players
     worldrandom.shuffle(self.players_starts)
     for client in players:
         start = self.players_starts.pop()
         if client.__class__.__name__ == "DummyClient":
             self._add_player(Computer, client, start, False)
         else:
             self._add_player(Human, client, start)
     # create the alliances
     if alliances:
         for p, pa in zip(self.players, alliances):
             for other, oa in zip(self.players, alliances):
                 if other is not p and oa == pa:
                     p.allied.append(other)
     else:  # computer players are allied by default
         for p in self.players:
             if isinstance(p, Computer):
                 for other in self.players:
                     if other is not p and isinstance(other, Computer):
                         p.allied.append(other)
     # set the factions for players
     if factions:
         for p, pr in zip(self.players, factions):
             if pr == "random_faction":
                 p.faction = worldrandom.choice(self.get_factions())
             else:
                 p.faction = pr
     # add "neutral" (independent) computers
     for start in self.computers_starts:
         self._add_player(Computer, worldclient.DummyClient(), start, True)
     # init all players positions
     for player in self.players:
         player.init_position()
     self.admin = players[0]  # define get_admin()?
Esempio n. 2
0
 def auto_explore(self):
     if not self.action_target:
         if self.place in self.player.places_to_explore:
             self.player.places_to_explore.remove(self.place)
         # level 1
         for e in self.place.exits:
             p = e.other_side.place
             if p not in self.player.observed_before_squares and \
                p not in self.player.places_to_explore:
                 self.player.places_to_explore.append(p)
         # level 2: useful for air units
         for e in self.place.exits:
             p = e.other_side.place
             for e2 in p.exits:
                 p2 = e2.other_side.place
                 if p2 not in self.player.observed_before_squares and \
                    p2 not in self.player.places_to_explore:
                     self.player.places_to_explore.append(p2)
         if self.player.places_to_explore:
             for place in self.player.places_to_explore[:]:
                 if place in self.player.observed_before_squares:
                     self.player.places_to_explore.remove(place)
                 else:
                     self.action_target = self.next_stage(place)
                     break
         else:
             self.player.places_to_explore = [p
                  for p in self.player.world.squares
                  if p not in self.player.observed_before_squares
                  and self.next_stage(p)]
             worldrandom.shuffle(self.player.places_to_explore)
             if not self.player.places_to_explore:
                 return True
Esempio n. 3
0
 def populate_map(self, players, alliances, races=()):
     # add "true" (non neutral) players
     worldrandom.shuffle(self.players_starts)
     for client in players:
         start = self.players_starts.pop()
         if client.__class__.__name__ == "DummyClient":
             self._add_player(Computer, client, start, False)
         else:
             self._add_player(Human, client, start)
     # create the alliances
     if alliances:
         for p, pa in zip(self.players, alliances):
             for other, oa in zip(self.players, alliances):
                 if other is not p and oa == pa:
                     p.allied.append(other)
     else: # computer players are allied by default
         for p in self.players:
             if isinstance(p, Computer):
                 for other in self.players:
                     if other is not p and isinstance(other, Computer):
                         p.allied.append(other)
     # set the races for players
     if races:
         for p, pr in zip(self.players, races):
             if pr == "random_race":
                 p.race = worldrandom.choice(self.get_races())
             else:
                 p.race = pr
     # add "neutral" (independent) computers
     for start in self.computers_starts:
         self._add_player(Computer, worldclient.DummyClient(), start, True)
     # init all players positions
     for player in self.players:
         player.init_position()
     self.admin = players[0] # define get_admin()?
Esempio n. 4
0
 def auto_explore(self):
     if not self.action_target:
         if self.place in self.player.places_to_explore:
             self.player.places_to_explore.remove(self.place)
         # level 1
         for e in self.place.exits:
             p = e.other_side.place
             if p not in self.player.observed_before_squares and \
                p not in self.player.places_to_explore:
                 self.player.places_to_explore.append(p)
         # level 2: useful for air units
         for e in self.place.exits:
             p = e.other_side.place
             for e2 in p.exits:
                 p2 = e2.other_side.place
                 if p2 not in self.player.observed_before_squares and \
                    p2 not in self.player.places_to_explore:
                     self.player.places_to_explore.append(p2)
         if self.player.places_to_explore:
             for place in self.player.places_to_explore[:]:
                 if place in self.player.observed_before_squares:
                     self.player.places_to_explore.remove(place)
                 else:
                     self.action_target = self.next_stage(place)
                     break
         else:
             self.player.places_to_explore = [p
                  for p in self.player.world.squares
                  if p not in self.player.observed_before_squares
                  and self.next_stage(p)]
             worldrandom.shuffle(self.player.places_to_explore)
             if not self.player.places_to_explore:
                 return True
Esempio n. 5
0
    def choose(self, c, resource_type=None, nearest_for_builders=False,
               starting_place=None, random=False): # choose nearest object
        if not self.units:
            return
        k = "%s %s %s" % (c, resource_type, starting_place)
        if k in self.previous_choose and not nearest_for_builders and not random:
            candidate = self.previous_choose[k]
            if (candidate in self.perception or candidate in self.memory) \
               and candidate.place is not None \
               and (resource_type is None or
                    self.is_ok_for_warehouse(candidate.place, resource_type)
              and self.warehouse_not_already_there(candidate.place, resource_type)
                    and self.no_enemy_in(candidate.place)):
#                warning("useful cache %s %s", c, resource_type)
                return candidate
            else:
                del self.previous_choose[k]
        if starting_place is None:
            if nearest_for_builders:
                starts = {}
                for u in self.units:
                    if isinstance(u, Worker):
                        if u.place not in starts: starts[u.place] = 1
                        else: starts[u.place] += 1
                if starts:
                    starting_place = sorted(starts.items(), key=lambda x: x[1])[-1][0]
                else:
                    starting_place = self.units[0].place
            else:
                starting_place = self.units[0].place
        candidates = [o for o in self.perception.union(self.memory) if self.check_type(o, c) and
                      o.place is not None and
                      (resource_type is None or
                       self.is_ok_for_warehouse(o.place, resource_type)
                       and self.warehouse_not_already_there(o.place, resource_type))]
        candidates = sorted(candidates, key=lambda x: x.id) # avoid synchronization errors
        if len(candidates) > 10:
            candidates = self._remove_far_candidates(candidates, starting_place, 1)
        else:
            candidates.sort(key=lambda x: starting_place.shortest_path_distance_to(x.place, self))
            while candidates and starting_place.shortest_path_distance_to(candidates[0].place, self) is None: # None < 0
                del candidates[0] # no path
        if random:
            candidates = [o for o in candidates if self.no_enemy_in(o.place)]
            if candidates:
                p = candidates[0].place
                candidates = [o for o in candidates if o.place == p]
                worldrandom.shuffle(candidates)
        for o in candidates:
            if self.no_enemy_in(o.place):
                if not nearest_for_builders and not random:
                    self.previous_choose[k] = o
                return o
        self.explore()
        self.AI_timer = 10 # don't insist for a while
Esempio n. 6
0
    def play(self):
        if self.never_played:
            self.attack_squares = [self.world.grid[name] for name in self.world.starting_squares]
            if self.units[0].place in self.attack_squares: # may not happen if additional units in other squares
                self.attack_squares.remove(self.units[0].place)
                self.my_base = self.units[0].place
            worldrandom.shuffle(self.attack_squares)
            self.never_played = False
        self.idle_peasants_gather()
        if self.constant_attacks:
            self.try_constant_attacks()
        if self.research:
            self.idle_buildings_research()
        if self.raise_dead:
            self.raise_dead_units()
        if self.send_soldiers_to_base and self.my_base is not None:
            if self.send_timer == 0:
                self.send_soldiers_to_my_base()
                self.send_timer = 40
            else:
                self.send_timer -= 1
        if self.AI_timer == 0:
            if not self.is_building_or_repairing: # XXX: not perfect (one building at a time; problems if peasants destroyed) but the AI must not be too good
                try:
                    if not self.build_a_warehouse_if_useful():
                        self._play()
                except RuntimeError: # XXX: maximum recursion (for example if no TownHall and no Peasant)
                    warning("recursion error with %s; current ai.txt line is: %s",
                            self.AI_type, self._plan[self._line_nb])
                    if VERSION[-4:] == "-dev":
                        exception("")
                    self._line_nb += 1 # go to next step; useful?
                    self.AI_timer = 100 # probably not, so make a big pause
#            else:
#                self.send_some_peasants_to_building_site() # Don't know if it will be needed,
# But sometimes AI forget the building being constructed
        else:
            self.AI_timer -= 1