102 lines
2.4 KiB
Python
102 lines
2.4 KiB
Python
from config import GRID_WIDTH, GRID_HEIGHT
|
|
from DataModels.Road import Road
|
|
import json, os, platform
|
|
|
|
def movement(environment, x ,y):
|
|
movement = {
|
|
"right": (x + 1, y) if x + 1 < GRID_WIDTH and type(environment[x + 1][y]) == Road else (x, y),
|
|
"left": (x - 1, y) if x - 1 >= 0 and type(environment[x - 1][y]) == Road else (x, y),
|
|
"down": (x, y + 1) if y + 1 < GRID_HEIGHT and type(environment[x][y + 1]) == Road else (x, y),
|
|
"up": (x, y - 1) if y - 1 >= 0 and type(environment[x][y - 1]) == Road else (x, y)
|
|
}
|
|
|
|
forbidden_movement = {
|
|
"right": "left",
|
|
"left": "right",
|
|
"up": "down",
|
|
"down": "up"
|
|
}
|
|
|
|
return (movement, forbidden_movement)
|
|
|
|
def check_moves(environment, x,y,direction=None):
|
|
if direction == None:
|
|
return ([dir for dir in movement(environment, x, y)[0] if movement(environment, x,y)[0][dir] != (x,y)])
|
|
return ([dir for dir in movement(environment, x, y)[0] if movement(environment, x,y)[0][dir] != (x,y) and dir != movement(environment,x,y)[1][direction]])
|
|
|
|
def define_dir(coord1, coord2):
|
|
if( coord1[0]-coord2[0] > 0):
|
|
return 4
|
|
elif( coord1[0]-coord2[0] < 0 ):
|
|
return 2
|
|
elif( coord1[0]-coord2[0] == 0):
|
|
if( coord1[1]-coord2[1] > 0):
|
|
return 1
|
|
elif( coord1[1]-coord2[1] < 0):
|
|
return 3
|
|
else:
|
|
return -1
|
|
|
|
def parse_coords(coords):
|
|
|
|
#left = 4 right = 2 up = 1 down = 3 pick_garbage = 99
|
|
crd = list(coords)
|
|
output = []
|
|
|
|
#for i in range(0,len(crd)-1):
|
|
# while( crd[i+1] == "pick_garbage" ):
|
|
# crd.pop(i+1)
|
|
# output.append(99)
|
|
# output.append(define_dir(crd[i],crd[i+1]))
|
|
|
|
current = crd[0]
|
|
for i in range (1, len(crd) - 1):
|
|
if crd[i] == "pick_garbage":
|
|
output.append(99)
|
|
continue
|
|
else:
|
|
output.append(define_dir(current, crd[i]))
|
|
current = crd[i]
|
|
|
|
|
|
return output
|
|
|
|
def save_moveset(moveset, maps):
|
|
|
|
if platform.system() == 'Windows':
|
|
path = '\moveset_data.json'
|
|
else:
|
|
path = '/moveset_data.json'
|
|
output_file = os.path.normpath(os.getcwd()) + path
|
|
|
|
results = {}
|
|
try:
|
|
f = open(output_file, 'r+')
|
|
except:
|
|
open(output_file, 'a').close()
|
|
finally:
|
|
f = open(output_file, 'r+')
|
|
try:
|
|
results = json.load(f)
|
|
except:
|
|
pass
|
|
finally:
|
|
if "moveset" not in results:
|
|
results = { "moveset": [] }
|
|
|
|
dirs = parse_coords(moveset)
|
|
|
|
while(True):
|
|
try:
|
|
ind = dirs.index(-1)
|
|
dirs.pop(ind)
|
|
maps.pop(ind)
|
|
except:
|
|
break
|
|
|
|
results["moveset"].append({'maps': maps[:-2], 'moves': dirs})
|
|
f.seek(0)
|
|
json.dump(results, f, indent=1)
|
|
f.close()
|
|
|