diplomacy.engine.map¶
Map - Contains the map object which represents a map where the game can be played
-
class
diplomacy.engine.map.
Map
(name='standard', use_cache=True)[source]¶ Bases:
object
Map Class
Properties:
- abbrev: Contains the power abbreviation, otherwise defaults to first letter of PowerName e.g. {‘ENGLISH’: ‘E’}
- abuts_cache: Contains a cache of abuts for [‘A,’F’] between all locations for orders [‘S’, ‘C’, ‘-‘] e.g. {(A, PAR, -, MAR): 1, …}
- aliases: Contains a dict of all the aliases (e.g. full province name to 3 char) e.g. {‘EAST’: ‘EAS’, ‘STP ( /SC )’: ‘STP/SC’, ‘FRENCH’: ‘FRANCE’, ‘BUDAPEST’: ‘BUD’, ‘NOR’: ‘NWY’, … }
- centers: Contains a dict of owned supply centers for each player at the beginning of the 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 e.g. [‘’DUPLICATE MAP ALIAS OR POWER: JAPAN’]
- files: Contains a list of files that were loaded (e.g. USES keyword) e.g. [‘standard.map’, ‘standard.politics’, ‘standard.geography’, ‘standard.military’]
- first_year: Indicates the year where the game is starting. e.g. 1901
- flow: List that contains the seasons with the phases e.g. [‘SPRING:MOVEMENT,RETREATS’, ‘FALL:MOVEMENT,RETREATS’, ‘WINTER:ADJUSTMENTS’]
- flow_sign: Indicate the direction of flow (1 is positive, -1 is negative) e.g. 1
- homes: Contains the list of supply centers where units can be built (i.e. assigned at the beginning) e.g. {‘RUSSIA’: [‘MOS’, ‘SEV’, ‘STP’, ‘WAR’], ‘FRANCE’: [‘BRE’, ‘MAR’, ‘PAR’], … }
- inhabits: List that indicates which power have a INHABITS, HOME, or HOMES line e.g. [‘FRANCE’]
- keywords: Contains a dict of keywords to parse status files and orders e.g. {‘BUILDS’: ‘B’, ‘>’: ‘’, ‘SC’: ‘/SC’, ‘REMOVING’: ‘D’, ‘WAIVED’: ‘V’, ‘ATTACK’: ‘’, … }
- loc_abut: Contains a adjacency list for each province e.g. {‘LVP’: [‘CLY’, ‘edi’, ‘IRI’, ‘NAO’, ‘WAL’, ‘yor’], …}
- loc_coasts: Contains a mapping of all coasts for every location e.g. {‘PAR’: [‘PAR’], ‘BUL’: [‘BUL’, ‘BUL/EC’, ‘BUL/SC’], … }
- loc_name: Dict that indicates the 3 letter name of each location e.g. {‘GULF OF LYON’: ‘LYO’, ‘BREST’: ‘BRE’, ‘BUDAPEST’: ‘BUD’, ‘RUHR’: ‘RUH’, … }
- loc_type: Dict that indicates if each location is ‘WATER’, ‘COAST’, ‘LAND’, or ‘PORT’ e.g. {‘MAO’: ‘WATER’, ‘SER’: ‘LAND’, ‘SYR’: ‘COAST’, ‘MOS’: ‘LAND’, ‘VEN’: ‘COAST’, … }
- locs: List of 3 letter locations (With coasts) e.g. [‘ADR’, ‘AEG’, ‘ALB’, ‘ANK’, ‘APU’, ‘ARM’, ‘BAL’, ‘BAR’, ‘BEL’, ‘BER’, … ]
- name: Name of the map (or full path to a custom map file) e.g. ‘standard’ or ‘/some/path/to/file.map’
- own_word: Dict to indicate the word used to refer to people living in each power’s country e.g. {‘RUSSIA’: ‘RUSSIAN’, ‘FRANCE’: ‘FRENCH’, ‘UNOWNED’: ‘UNOWNED’, ‘TURKEY’: ‘TURKISH’, … }
- owns: List that indicates which power have a OWNS or CENTERS line e.g. [‘FRANCE’]
- phase: String to indicate the beginning phase of the map e.g. ‘SPRING 1901 MOVEMENT’
- phase_abbrev: Dict to indicate the 1 letter abbreviation for each phase e.g. {‘A’: ‘ADJUSTMENTS’, ‘M’: ‘MOVEMENT’, ‘R’: ‘RETREATS’}
- pow_name: Dict to indicate the power’s name e.g. {‘RUSSIA’: ‘RUSSIA’, ‘FRANCE’: ‘FRANCE’, ‘TURKEY’: ‘TURKEY’, ‘GERMANY’: ‘GERMANY’, … }
- powers: Contains the list of powers (players) in the game e.g. [‘AUSTRIA’, ‘ENGLAND’, ‘FRANCE’, ‘GERMANY’, ‘ITALY’, ‘RUSSIA’, ‘TURKEY’]
- root_map: Contains the name of the original map file loaded (before the USES keyword are applied) A map that is called with MAP is the root_map. e.g. ‘standard’
- rules: Contains a list of rules used by all variants (for display only) e.g. [‘RULE_1’]
- scs: Contains a list of all the supply centers in the game e.g. [‘MOS’, ‘SEV’, ‘STP’, ‘WAR’, ‘BRE’, ‘MAR’, ‘PAR’, ‘BEL’, ‘BUL’, ‘DEN’, ‘GRE’, ‘HOL’, ‘NWY’, … ]
- seq: [] Contains the sequence of seasons in format ‘SEASON_NAME SEASON_TYPE’ e.g. [‘NEWYEAR’, ‘SPRING MOVEMENT’, ‘SPRING RETREATS’, ‘FALL MOVEMENT’, ‘FALL RETREATS’, ‘WINTER ADJUSTMENTS’]
- unclear: Contains the alias for ambiguous places e.g. {‘EAST’: ‘EAS’}
- unit_names: {} Contains a dict of the unit names e.g. {‘F’: ‘FLEET’, ‘A’: ‘ARMY’}
- units: Dict that contains the current position of each unit by power e.g. {‘FRANCE’: [‘F BRE’, ‘A MAR’, ‘A PAR’], ‘RUSSIA’: [‘A WAR’, ‘A MOS’, ‘F SEV’, ‘F STP/SC’], … }
- validated: Boolean to indicate if the map file has been validated e.g. 1
- victory: Indicates the number of supply centers to win the game (>50% required if None) e.g. 18
-
__init__
(name='standard', use_cache=True)[source]¶ Constructor function
Parameters: - name – Name of the map to load (or full path to a custom map file)
- use_cache – Boolean flag to indicate we want a blank object that doesn’t use cache
-
svg_path
¶ Return path to the SVG file of this map (or None if it does not exist)
-
validate
(force=0)[source]¶ Validate that the configuration from a map file is correct
Parameters: force – Indicate that we want to force a validation, even if the map is already validated Returns: Nothing
-
load
(file_name=None)[source]¶ Loads a map file from disk
Parameters: file_name – Optional. A string representing the file to open. Otherwise, defaults to the map name Returns: Nothing
-
add_homes
(power, homes, reinit)[source]¶ Add new homes (and deletes previous homes if reinit)
Parameters: - power – Name of power (e.g. ITALY)
- homes – List of homes e.g.
['BUR', '-POR', '*ITA', ... ]
- reinit – Indicates that we want to strip the list of homes before adding
Returns: Nothing
-
norm_power
(power)[source]¶ Normalise the name of a power (removes spaces)
Parameters: power – Name of power to normalise Returns: Normalised power name
-
norm
(phrase)[source]¶ Normalise a sentence (add spaces before /, replace -+, with ‘ ‘, remove .:
Parameters: phrase – Phrase to normalise Returns: Normalised sentences
-
compact
(phrase)[source]¶ Compacts a full sentence into a list of short words
Parameters: phrase – The full sentence to compact (e.g. ‘England: Fleet Western Mediterranean -> Tyrrhenian Sea. (bounce)’) Returns: The compacted phrase in an array (e.g. [‘ENGLAND’, ‘F’, ‘WES’, ‘TYS’, ‘|’])
-
alias
(word)[source]¶ This function is used to replace multi-words with their acronyms
Parameters: word – The current list of words to try to shorten Returns: alias, ix - alias is the shorten list of word, ix is the ix of the next non-processed word
-
vet
(word, strict=0)[source]¶ Determines the type of every word in a compacted order phrase
0 - Undetermined, 1 - Power, 2 - Unit, 3 - Location, 4 - Coastal location 5 - Order, 6 - Move Operator
(-=_^)
, 7 - Non-move separator(|?~)
or result(*!?~+)
Parameters: - word – The list of words to vet (e.g.
['A', 'POR', 'S', 'SPA/NC']
) - strict – Boolean to indicate that we want to verify that the words actually exist. Numbers become negative if they don’t exist
Returns: A list of tuple (e.g.
[('A', 2), ('POR', 3), ('S', 5), ('SPA/NC', 4)]
)- word – The list of words to vet (e.g.
-
rearrange
(word)[source]¶ This function is used to parse commands
Parameters: word – The list of words to vet (e.g. [‘ENGLAND’, ‘F’, ‘WES’, ‘TYS’, ‘|’]) Returns: The list of words in the correct order to be processed (e.g. [‘ENGLAND’, ‘F’, ‘WES’, ‘-‘, ‘TYS’])
-
area_type
(loc)[source]¶ Returns ‘WATER’, ‘COAST’, ‘PORT’, ‘LAND’, ‘SHUT’
Parameters: loc – The name of the location to query Returns: Type of the location (‘WATER’, ‘COAST’, ‘PORT’, ‘LAND’, ‘SHUT’)
-
default_coast
(word)[source]¶ Returns the coast for a fleet move order that can only be to a single coast (e.g. F GRE-BUL returns F GRE-BUL/SC)
Parameters: word – A list of tokens (e.g. [‘F’, ‘GRE’, ‘-‘, ‘BUL’]) Returns: The updated list of tokens (e.g. [‘F’, ‘GRE’, ‘-‘, ‘BUL/SC’])
-
find_coasts
(loc)[source]¶ Finds all coasts for a given location
Parameters: loc – The name of a location (e.g. ‘BUL’) Returns: Returns the list of all coasts, including the location (e.g. [‘BUL’, ‘BUL/EC’, ‘BUL/SC’]
-
abuts
(unit_type, unit_loc, order_type, other_loc)[source]¶ Determines if a order for unit_type from unit_loc to other_loc is adjacent.
Note: This method uses the precomputed cache
Parameters: - unit_type – The type of unit (‘A’ or ‘F’)
- unit_loc – The location of the unit (‘BUR’, ‘BUL/EC’)
- order_type – The type of order (‘S’ for Support, ‘C’ for Convoy’, ‘-‘ for move)
- other_loc – The location of the other unit
Returns: 1 if the locations are adjacent for the move, 0 otherwise
-
is_valid_unit
(unit, no_coast_ok=0, shut_ok=0)[source]¶ Determines if a unit and location combination is valid (e.g. ‘A BUR’) is valid
Parameters: - unit – The name of the unit with its location (e.g. F SPA/SC)
- no_coast_ok – Indicates if a coastal location with no coast (e.g. SPA vs SPA/SC) is acceptable
- shut_ok – Indicates if a impassable country (e.g. Switzerland) is OK
Returns: A boolean to indicate if the unit/location combination is valid
-
abut_list
(site, incl_no_coast=False)[source]¶ Returns the adjacency list for the site
Parameters: - site – The province we want the adjacency list for
- incl_no_coast – Boolean flag that indicates to also include province without coast if it has coasts e.g. will return [‘BUL/SC’, ‘BUL/EC’] if False, and [‘bul’, ‘BUL/SC’, ‘BUL/EC’] if True
Returns: A list of adjacent provinces
Note: abuts are returned in mixed cases
- An adjacency that is lowercase (e.g. ‘bur’) can only be used by an army
- An adjacency that starts with a capital letter (e.g. ‘Bal’) can only be used by a fleet
- An adjacency that is uppercase can be used by both an army and a fleet
-
find_next_phase
(phase, phase_type=None, skip=0)[source]¶ Returns the long name of the phase coming immediately after the phase
Parameters: - phase – The long name of the current phase (e.g. SPRING 1905 RETREATS)
- phase_type – The type of phase we are looking for (e.g. ‘M’ for Movement, ‘R’ for Retreats, ‘A’ for Adjust.)
- skip – The number of match to skip (e.g. 1 to find not the next phase, but the one after)
Returns: The long name of the next phase (e.g. FALL 1905 MOVEMENT)
-
find_previous_phase
(phase, phase_type=None, skip=0)[source]¶ Returns the long name of the phase coming immediately prior the phase
Parameters: - phase – The long name of the current phase (e.g. SPRING 1905 RETREATS)
- phase_type – The type of phase we are looking for (e.g. ‘M’ for Movement, ‘R’ for Retreats, ‘A’ for Adjust.)
- skip – The number of match to skip (e.g. 1 to find not the next phase, but the one after)
Returns: The long name of the previous phase (e.g. SPRING 1905 MOVEMENT)
-
compare_phases
(phase1, phase2)[source]¶ Compare 2 phases (Strings) and return 1, -1, or 0 to indicate which phase is larger
Parameters: - phase1 – The first phase (e.g. S1901M, FORMING, COMPLETED)
- phase2 – The second phase (e.g. S1901M, FORMING, COMPLETED)
Returns: 1 if phase1 > phase2, -1 if phase2 > phase1 otherwise 0 if they are equal
-
static
phase_abbr
(phase, default='?????')[source]¶ Constructs a 5 character representation (S1901M) from a phase (SPRING 1901 MOVEMENT)
Parameters: - phase – The full phase (e.g. SPRING 1901 MOVEMENT)
- default – The default value to return in case conversion fails
Returns: A 5 character representation of the phase
-
phase_long
(phase_abbr, default='?????')[source]¶ Constructs a full sentence of a phase from a 5 character abbreviation
Parameters: - phase_abbr – 5 character abbrev. (e.g. S1901M)
- default – The default value to return in case conversion fails
Returns: A full phase description (e.g. SPRING 1901 MOVEMENT)