Esempio n. 1
0
 def on_queued(self):
     if self.type.effect_target == ["ask"]:
         if self.args:
             self.target = self.player.get_object_by_id(self.args[0])
         else:
             self.target = None
         if self.target is None:
             self.mark_as_impossible()
             return
         if self._target_type == "square":
             # make sure that the target is a square
             if not hasattr(self.target, "x"):
                 self.mark_as_impossible()
                 return
     elif self.type.effect_target == ["worldrandom"]:
         self.target = worldrandom.choice(self.player.world.squares)
     elif self.type.effect_target == ["self"]:
         self.target = self.unit
     # check cost
     if self.unit.mana < self.type.mana_cost:
         if self._group_has_enough_mana(self.type.mana_cost):
             self.mark_as_complete() # ignore silently
         else:
             self.mark_as_impossible("not_enough_mana")
         return
     self.unit.notify("order_ok")
Esempio n. 2
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. 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 on_queued(self):
     if self.type.effect_target == ["ask"]:
         if self.args:
             self.target = self.player.get_object_by_id(self.args[0])
         else:
             self.target = None
         if self.target is None:
             self.mark_as_impossible()
             return
         if self._target_type == "square":
             # make sure that the target is a square
             if not hasattr(self.target, "x"):
                 self.mark_as_impossible()
                 return
     elif self.type.effect_target == ["random"]:
         self.target = worldrandom.choice(self.player.world.squares)
     elif self.type.effect_target == ["self"]:
         self.target = self.unit
     # check cost
     if self.unit.mana < self.type.mana_cost:
         if self._group_has_enough_mana(self.type.mana_cost):
             self.mark_as_complete()  # ignore silently
         else:
             self.mark_as_impossible("not_enough_mana")
         return
     self.unit.notify("order_ok")
Esempio n. 5
0
 def next_stage_enemy(self):
     for e in self.place.exits:
         if e.other_side.place.contains_enemy(self.player):
             return e
     if not self.player.attack_squares:
         self.player.attack_squares.append(
             worldrandom.choice([x for x in self.player.world.squares
                                 if x.exits and x != self.place]))
     return self.next_stage(self.player.attack_squares[0])
Esempio n. 6
0
 def next_stage_enemy(self):
     for e in self.place.exits:
         if e.other_side.place.contains_enemy(self.player):
             return e
     if not self.player.attack_squares:
         self.player.attack_squares.append(
             worldrandom.choice([x for x in self.player.world.squares
                                 if x.exits and x != self.place]))
     return self.next_stage(self.player.attack_squares[0])
Esempio n. 7
0
 def _play(self):
     if not self._plan: return
     if self.watchdog and self.world.time > \
        self._previous_linechange + self.watchdog * 1000:
         self._line_nb += 1
     self._line_nb %= len(self._plan)
     line = self._plan[self._line_nb]
     cmd = line.split()
     if cmd:
         if cmd[0] == "goto":
             if re.match("^[+-][0-9]+$", cmd[1]):
                 self._line_nb += int(cmd[1])
             elif "label " + cmd[1] in self._plan:
                 self._line_nb = self._plan.index("label " + cmd[1])
             elif re.match("^[0-9]+$", cmd[1]):
                 self._line_nb = int(cmd[1])
             else:
                 warning("goto: wrong destination: %s", cmd[1])
                 self._line_nb += 1
         elif cmd[0] == "label":
             self._line_nb += 1
             info(cmd[1])
         elif cmd[0] == "goto_random":
             dest = worldrandom.choice(cmd[1:])
             if "label " + dest in self._plan:
                 self._line_nb = self._plan.index("label " + dest)
             else:
                 warning("goto_random: label not found: %s", dest)
                 self._line_nb += 1
         elif cmd[0] == "attack":
             self.attack()
             self._line_nb += 1
         elif cmd[0] in ("retaliate", "watchdog", "constant_attacks",
                         "research", "teleportation", "send_soldiers_to_base", "raise_dead"):
             setattr(self, cmd[0], int(cmd[1]))
             self._line_nb += 1
         elif cmd[0] == "get":
             n = 1
             done = True
             for w in cmd[1:]:
                 if re.match("^[0-9]+$", w):
                     n = int(w)
                 elif w in rules.classnames():
                     if not self.get(n, self.equivalent(w)):
                         done = False
                         break
                     n = 1
                 else:
                     warning("get: unknown unit: '%s' (in ai.txt)", w)
                     n = 1
             if done:
                 self._line_nb += 1
         else:
             warning("unknown command: '%s' (in ai.txt)", cmd[0])
             self._line_nb += 1
Esempio n. 8
0
 def random_choice_repl(matchobj):
     return worldrandom.choice(
         matchobj.group(1).split("\n#end_choice\n"))
Esempio n. 9
0
 def random_choice_repl(matchobj):
     return worldrandom.choice(matchobj.group(1).split("\n#end_choice\n"))