Example #1
0
 def can_move_to(self, place):
     #print '\tQuery: %s -> %s' % (self, place)
     if isinstance(place, Location):
         # Check whether it can move to any coastline
         return any(place.matches(prov)
             for prov in self.location.borders_out)
     else:
         # Assume a Province or province token
         return place.key in [key[1] for key in self.location.borders_out]
     return False
Example #2
0
 def convoy_note(convoyed, destination, routes, route_valid=bool):
     result = FAR
     if not convoyed.exists():
         result = NSA
     elif not convoyed.can_be_convoyed():
         # This might be an army on an inland province.
         # Shortcut to the default FAR in that case.
         if convoyed.location.unit_type != AMY:
             result = NSA
     elif not destination.exists(): result = CST
     elif destination.province.is_coastal():
         if any(route_valid(route) for route in routes): result = MBV
     #print 'convoy_note(%s, %s) => %s' % (convoyed, destination, result)
     return result
Example #3
0
 def distance(self, location, provs):
     ''' Returns the location's distance from the nearest of the provinces,
         particularly for use in determining civil disorder retreats.
     '''#'''
     # Todo: Count army and fleet movements differently?
     result = 0
     rank = seen = [location.province.key]
     while rank:
         if any(place in provs for place in rank): return result
         new_rank = []
         for here in rank:
             new_rank.extend([key
                 for key in self.spaces[here].borders_out
                 if key not in seen and key not in new_rank
             ])
         seen.extend(new_rank)
         rank = new_rank
         result += 1
     # Inaccessible island
     return Infinity
Example #4
0
    def add_option(self, section, name, option_type, default, alt_names, *help):
        # print '    %s.%s = %s' % (section, name, default)
        if any(opts.has_key(name) for opts in self.modules.values()):
            print "# Warning: Duplicate definition for option", name
        self.__super.add_option(section, name, option_type, default, alt_names, *help)

        if isinstance(alt_names, str) and len(alt_names) > 1:
            main_name = alt_names
        else:
            main_name = name

        type_name = option_type.__name__
        default_value = self.value_string(name, default)
        text = ["# %s (%s)" % (main_name, self.names.get(type_name, type_name))]
        text.extend("# " + line for line in help)
        text.append(";%s = %s" % (name, default_value))

        current_value = self.value_string(name, getattr(self, name))
        if current_value != default_value:
            text.append("%s = %s" % (name, current_value))
        self.modules[section][name] = text
Example #5
0
 def tokens(self):
     cats = defaultdict(list)
     for prov in sorted(self.borders):
         # Inland_non-SC = 0x50
         # Inland_SC = 0x51
         # Sea_non-SC = 0x52
         # Sea_SC = 0x53
         # Coastal_non-SC = 0x54
         # Coastal_SC = 0x55
         # Bicoastal_non-SC = 0x56
         # Bicoastal_SC = 0x57
         num = 0x5000
         keys = list(self.borders[prov])
         if len(keys) > 2:
             # Yes, we should probably look for coastline specs,
             # but this works for anything in the current variants.
             num += 0x0600
         elif AMY not in keys:
             num += 0x0200
         elif FLT in keys:
             num += 0x0400
         
         if any(prov in self.homes[power] for power in self.homes):
             num += 0x0100
         cats[num].append(prov)
     
     numbers = {}
     index = count()
     for cat in sorted(cats):
         for prov in cats[cat]:
             numbers[cat + index.next()] = prov
     
     for index, power in enumerate(sorted(self.homes)):
         if power != "UNO":
             numbers[0x4100 + index] = power
     
     return Representation(numbers, protocol.default_rep)
Example #6
0
 def help_requested():
     signals = "--help", "-h", "-?", "/?", "/h"
     return any(flag in argv for flag in signals)
Example #7
0
 def __contains__(self, name):
     lower = name.lower()
     return any(lower == n.lower() for n in self)