diff options
author | Philip Paquette <pcpaquette@gmail.com> | 2019-03-05 16:01:53 -0500 |
---|---|---|
committer | Philip Paquette <pcpaquette@gmail.com> | 2019-04-18 11:25:05 -0400 |
commit | ab82a735cd57dd20236135447b774643faf7e1c1 (patch) | |
tree | 4ce08d7ff3f8f22861dbcfc701b424cfbf2bc649 /diplomacy/engine/map.py | |
parent | 88a5715f8c53852a6e231315d4aca361df2e6493 (diff) |
Refactored get_all_possible_orders for 2.5x speed improvement
- Returning all locations at once
- Removed 'loc' argument from method
- Map has pre-computed attribute dest_with_coats
Diffstat (limited to 'diplomacy/engine/map.py')
-rw-r--r-- | diplomacy/engine/map.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/diplomacy/engine/map.py b/diplomacy/engine/map.py index acbc089..2730168 100644 --- a/diplomacy/engine/map.py +++ b/diplomacy/engine/map.py @@ -44,6 +44,8 @@ class Map(): e.g. {'RUSSIA': ['MOS', 'SEV', 'STP', 'WAR'], 'FRANCE': ['BRE', 'MAR', 'PAR'], ... } - convoy_paths: Contains a list of all possible convoys paths bucketed by number of fleets format: {nb of fleets: [(START_LOC, {FLEET LOC}, {DEST LOCS})]} + - dest_with_coasts: Contains a dictionary of locs with all destinations (incl coasts) that can be reached + e.g. {'PAR': ['BRE', 'PIC', 'BUR', ...], ...} - dummies: Indicates the list of powers that are dummies e.g. ['FRANCE', 'ITALY'] - error: Contains a list of errors that the map generated @@ -112,7 +114,8 @@ class Map(): __slots__ = ['name', 'first_year', 'victory', 'phase', 'validated', 'flow_sign', 'root_map', 'abuts_cache', 'homes', 'loc_name', 'loc_type', 'loc_abut', 'loc_coasts', 'own_word', 'abbrev', 'centers', 'units', 'pow_name', 'rules', 'files', 'powers', 'scs', 'owns', 'inhabits', 'flow', 'dummies', 'locs', 'error', - 'seq', 'phase_abbrev', 'unclear', 'unit_names', 'keywords', 'aliases', 'convoy_paths'] + 'seq', 'phase_abbrev', 'unclear', 'unit_names', 'keywords', 'aliases', 'convoy_paths', + 'dest_with_coasts'] def __new__(cls, name='standard', use_cache=True): """ New function - Retrieving object from cache if possible @@ -140,7 +143,7 @@ class Map(): self.rules, self.files, self.powers, self.scs, self.owns, self.inhabits = [], [], [], [], [], [] self.flow, self.dummies, self.locs = [], [], [] self.error, self.seq = [], [] - self.phase_abbrev, self.unclear = {}, {} + self.phase_abbrev, self.unclear, self.dest_with_coasts = {}, {}, {} self.unit_names = {'A': 'ARMY', 'F': 'FLEET'} self.keywords, self.aliases = KEYWORDS.copy(), ALIASES.copy() self.load() @@ -682,6 +685,13 @@ class Map(): query_tuple = (unit_type, unit_loc, order_type, other_loc) self.abuts_cache[query_tuple] = self._abuts(*query_tuple) + # Building dest_with_coasts + for loc in self.locs: + loc = loc.upper() + dest_1_hops = [l.upper() for l in self.abut_list(loc, incl_no_coast=True)] + dest_with_coasts = [self.find_coasts(dest) for dest in dest_1_hops] + self.dest_with_coasts[loc] = list({val for sublist in dest_with_coasts for val in sublist}) + def add_homes(self, power, homes, reinit): """ Add new homes (and deletes previous homes if reinit) :param power: Name of power (e.g. ITALY) |