DnD-Movement-Rules / diagonal_movement.py
diagonal_movement.py
Raw
#11-10-2022 Dungeons and Dragons movement rules

from math import ceil
from random import randrange
import copy

# Grid will be bounding_box-1 X bounding_box-1
room_side = 13
bounding_box = room_side // 2
step_size = 5

# Instantiate the Lower-right quadrant:
# we'll just slice and stich copies later for the other 3
grid = [[0 for _ in range(bounding_box)] for _ in range(bounding_box)]
# first row
grid[0] = [step_size * x for x in range(bounding_box)]
# first col
for x in range(bounding_box):
    grid[x][0] = step_size * x

# Movement is Manhattan Distance MINUS the number of odd diagonals steps we can take
for x in range(1, bounding_box):
    for y in range(1, bounding_box):
        grid[x][y] = (
            (step_size * x) + (step_size * y) - (step_size * ceil(min(x, y) / 2))
        )  # Here is the math bit

# stitch reversed copies to mirror the cols
for i in range(len(grid)):
    grid[i] = grid[i][:0:-1] + grid[i][::]

# stitch reversed copies to mirror the rows
grid = grid[:0:-1] + copy.deepcopy(grid)

# format padding for output
pad_digits = 0
max_dist = grid[0][0]
while max_dist != 0:
    max_dist //= 10
    pad_digits += 1

# print distance grid from central point
# for row in grid:
#     print([str(i).rjust(pad_digits, " ") for i in row])

# Generate a new room
room = grid = [[0 for _ in range(room_side)] for _ in range(room_side)]

# Assign a random position to C Char and __ Target
char_x, char_y = room_side // 2, room_side // 2
trgt_x, trgt_y = randrange(room_side), randrange(room_side)

# Fill distance grid of room from C Char as padded strings
for x in range(room_side):
    for y in range(room_side):
        dist = str(
            (step_size * abs(char_x - x))
            + (step_size * abs(char_y - y))
            - (step_size * ceil(min(abs(char_x - x), abs(char_y - y)) / 2))
        ).rjust(3, " ")
        room[x][y] = dist if int(dist) <= 30 else " - "

        # padding inline fails in some as we dont know how much to pad by before computing distances.


# Fooling around with colors. Kinda prints, but doesnt store in the grid pretty
# room[char_x][char_y] = '\033[92m' + str("C").rjust(pad_digits, " ") + '\033[0m'
# room[trgt_x][trgt_y] = '\033[91m' + "\u0332".join("X" + room[trgt_x][trgt_y] + "")[1:] + '\033[0m'

# Place C at char position in the room, UNDERLINE target position
room[char_x][char_y] = str("C").rjust(3, " ")
room[trgt_x][trgt_y] = "\u0332".join("X" + room[trgt_x][trgt_y] + "")[1:]
distance_to_target = room[trgt_x][trgt_y]

# Report
for row in room:
    print(row)

print("Char C at: (", char_x, ", ", char_y, ")")
print("Target _ at: (", trgt_x, ", ", trgt_y, ")")
print("Distance to target: ", distance_to_target)