CoolFace
Apppublic

guyar/terra_faction_bot

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
terravisualisation.py153 linesDownload Raw Back to visualisations
1"""terravisualisation contains all the data processing & matplotlib graphing functions related to board visualisation2functions for terraBot. However for an interaction GUI, this function is contained in "terravisgui". This GUI uses3the tools built in terravisualisation.4"""5 6import numpy as np7 8 9def rotate_around_dig_wheel(gremangles, rotation):10    from math import pi11 12    # rotate13    blumangles = (gremangles + ((rotation / 7) * pi) + pi) % (2 * pi) - pi14 15    # back to numbers16    bluemap = blumangles / (pi * (2 / 7))17 18    # create the array19    rotatedmap = 4 - abs(bluemap)20 21    return rotatedmap22 23 24def faction_map(faction):25    """faction_map outputs an array representing the Terra Mystica map, where they array26    shows the amount of digs every tile is away from a faction's home tile27 28    Arguments:29    faction -- a string referring to a TM faction30 31    Returns:32    finalarr -- a (9, 13) array where each element is [4 - the amount of digs a faction is away33    from their home tile], where an element maps to a tile. Rivers have a value of 0. Rows of the34    map constituting of 12 tiles have their final element set to 0, representing a blank tile35     """36    from math import pi37 38    # for green39    greenmap = np.array([[-3, 1, 0, -1, 3, 2, -3, -2, 2, 0, -1, 2, -2],40                         [3, 4, 4, -3, -2, 4, 4, 3, -2, 4, 4, 3, 4],41                         [4, 4, -2, 4, 1, 4, 0, 4, 0, 4, 1, 4, 4],42                         [0, -1, 3, 4, 4, 2, -1, 4, 2, 4, 2, -3, 4],43                         [-2, -3, 2, -1, -2, -3, 1, 3, 4, 4, 0, -2, -1],44                         [1, 0, 4, 4, 3, 0, 4, 4, 4, -3, 1, -3, 4],45                         [4, 4, 4, 1, 4, 2, 4, 0, 4, 3, -2, -1, 3],46                         [3, -1, -3, 4, 4, 4, -1, -2, 4, 1, -3, 1, 4],47                         [2, -2, 1, -1, 2, 0, 3, -3, 1, 4, -1, 0, 2]])48 49    gremangles = greenmap * (pi * (2/7))50        51    # river & empties mask52    rivermask = np.array([[False, False, False, False, False, False, False, False, False, False, False, False, False],53                          [False, True, True, False, False, True, True, False, False, True, True, False, True],54                          [True, True, False, True, False, True, False, True, False, True, False, True, True],55                          [False, False, False, True, True, False, False, True, False, True, False, False, True],56                          [False, False, False, False, False, False, False, False, True, True, False, False, False],57                          [False, False, True, True, False, False, True,  True,  True, False, False, False, True],58                          [True, True, True, False, True, False, True, False, True, False, False, False, False],59                          [False, False, False, True, True, True, False, False, True, False, False, False, True],60                          [False, False, False, False, False, False, False, False, False, True, False, False, False]])61 62    if faction == 'witches' or faction == 'auren':63        # create the array64        arr1 = 4 - abs(greenmap)65 66    elif faction == 'swarmlings' or faction == 'mermaids':67        arr1 = rotate_around_dig_wheel(gremangles, 2)68 69    elif faction == 'darklings' or faction == 'alchemists':70        arr1 = rotate_around_dig_wheel(gremangles, 4)71 72    elif faction == 'halflings' or faction == 'cultists':73        arr1 = rotate_around_dig_wheel(gremangles, 6)74 75    elif faction == 'engineers' or faction == 'dwarves':76        arr1 = rotate_around_dig_wheel(gremangles, -2)77 78    elif faction == 'chaos magicians' or faction == 'giants':79        arr1 = rotate_around_dig_wheel(gremangles, -4)80 81    elif faction == 'fakirs' or faction == 'nomads':82        arr1 = rotate_around_dig_wheel(gremangles, -6)83 84    else:85        return86 87    # apply river spacing mask over88    # alt: arr1 = np.multiply(arr1, 1 - rivermask)89    arr1[rivermask] = 090 91    # turn back to integer from potential float92    arr1 = arr1.astype(int)93 94    # flip95    arr2 = np.flip(arr1, 0)96 97    # re-size98    arr3 = np.squeeze(np.resize(arr2, (1, 117)))99 100    # remove the added hexes101    finalarr = np.delete(arr3, [25, 51, 77, 103])102 103    return finalarr104 105 106def display_map(faction, plot=True):107    """takes the input, faction, and returns a map of the board108    where hex brightness relates to how many digs that faction109    needs to convert that hex into its home territory.110 111    Arguments:112    faction -- string or (9, 13) numpy array: where the string refers to a faction name,113    or the numpy array refers to map to be plotted in the format of faction_map114 115    Returns:116    None. Plots the map.117    """118    import matplotlib.pyplot as plt119    import numpy.matlib120 121    # make sure the faction string is all lowercase122    faction = faction.lower()123 124    # create array to display map on125    x1 = np.linspace(4.5, 16.5, 13)126    x2 = np.linspace(5, 16, 12)127    x3 = np.hstack((x1, x2))128    x4 = np.matlib.repmat(x3, 1, 4)129    x4 = np.squeeze(x4)130    x5 = np.hstack((x4, x1))131    x = np.squeeze(x5)132 133    y1 = np.linspace(6, 15, 9)134    y = np.repeat(y1, np.array([13, 12, 13, 12, 13, 12, 13, 12, 13]))135 136    if type(faction) == str:137        factionmap = faction_map(faction)138    else:  # assume it's already a map mask array139        factionmap = faction140 141    x = np.repeat(x, factionmap)142    y = np.repeat(y, factionmap)143 144    # need to define the size of the plot145    x = np.hstack((x, [1, 1, 20, 20]))146    y = np.hstack((y, [1, 20, 1, 20]))147 148    if plot:149        plt.hexbin(x, y, gridsize=(19, 9), cmap='magma')150        plt.show()151    else:152        return x, y153