Merge pull request 'updated main, repaired astar, added method for get actions from coords, expected to be used in GA' (#31) from astar_fix into master
Reviewed-on: #31
This commit is contained in:
commit
4309525ed4
@ -12,7 +12,7 @@ def getPrediction(img_path, network_name):
|
|||||||
img = Image.open(img_path)
|
img = Image.open(img_path)
|
||||||
transform_tensor = transforms.ToTensor()(img).unsqueeze_(0)
|
transform_tensor = transforms.ToTensor()(img).unsqueeze_(0)
|
||||||
classes = ['glass', 'metal', 'paper', 'plastic']
|
classes = ['glass', 'metal', 'paper', 'plastic']
|
||||||
neural_net.load_state_dict(torch.load(PATH + network_name))
|
neural_net.load_state_dict(torch.load(PATH + network_name, map_location='cpu'))
|
||||||
neural_net.eval()
|
neural_net.eval()
|
||||||
outputs = neural_net(transform_tensor)
|
outputs = neural_net(transform_tensor)
|
||||||
|
|
||||||
|
0
data_structures/__init__.py
Normal file
0
data_structures/__init__.py
Normal file
@ -4,40 +4,18 @@
|
|||||||
| | | |--- class: 0
|
| | | |--- class: 0
|
||||||
| | |--- feature_0 > 1.50
|
| | |--- feature_0 > 1.50
|
||||||
| | | |--- feature_3 <= 3.50
|
| | | |--- feature_3 <= 3.50
|
||||||
| | | | |--- feature_2 <= 2.50
|
| | | | |--- feature_4 <= 2.50
|
||||||
| | | | | |--- class: 1
|
| | | | | |--- class: 1
|
||||||
| | | | |--- feature_2 > 2.50
|
| | | | |--- feature_4 > 2.50
|
||||||
| | | | | |--- feature_4 <= 2.50
|
| | | | | |--- feature_2 <= 2.50
|
||||||
| | | | | | |--- class: 1
|
| | | | | | |--- class: 1
|
||||||
| | | | | |--- feature_4 > 2.50
|
| | | | | |--- feature_2 > 2.50
|
||||||
| | | | | | |--- class: 0
|
| | | | | | |--- class: 0
|
||||||
| | | |--- feature_3 > 3.50
|
| | | |--- feature_3 > 3.50
|
||||||
| | | | |--- feature_3 <= 4.50
|
| | | | |--- feature_3 <= 4.50
|
||||||
| | | | | |--- feature_1 <= 2.50
|
| | | | | |--- feature_1 <= 2.50
|
||||||
| | | | | | |--- feature_0 <= 2.50
|
| | | | | | |--- feature_0 <= 2.50
|
||||||
| | | | | | | |--- feature_1 <= 1.50
|
| | | | | | | |--- feature_1 <= 1.50
|
||||||
| | | | | | | | |--- feature_2 <= 2.50
|
|
||||||
| | | | | | | | | |--- class: 1
|
|
||||||
| | | | | | | | |--- feature_2 > 2.50
|
|
||||||
| | | | | | | | | |--- feature_4 <= 2.00
|
|
||||||
| | | | | | | | | | |--- class: 1
|
|
||||||
| | | | | | | | | |--- feature_4 > 2.00
|
|
||||||
| | | | | | | | | | |--- class: 0
|
|
||||||
| | | | | | | |--- feature_1 > 1.50
|
|
||||||
| | | | | | | | |--- class: 0
|
|
||||||
| | | | | | |--- feature_0 > 2.50
|
|
||||||
| | | | | | | |--- feature_4 <= 2.50
|
|
||||||
| | | | | | | | |--- class: 1
|
|
||||||
| | | | | | | |--- feature_4 > 2.50
|
|
||||||
| | | | | | | | |--- feature_2 <= 2.50
|
|
||||||
| | | | | | | | | |--- class: 1
|
|
||||||
| | | | | | | | |--- feature_2 > 2.50
|
|
||||||
| | | | | | | | | |--- class: 0
|
|
||||||
| | | | | |--- feature_1 > 2.50
|
|
||||||
| | | | | | |--- feature_0 <= 3.50
|
|
||||||
| | | | | | | |--- class: 0
|
|
||||||
| | | | | | |--- feature_0 > 3.50
|
|
||||||
| | | | | | | |--- feature_1 <= 3.50
|
|
||||||
| | | | | | | | |--- feature_4 <= 2.50
|
| | | | | | | | |--- feature_4 <= 2.50
|
||||||
| | | | | | | | | |--- class: 1
|
| | | | | | | | | |--- class: 1
|
||||||
| | | | | | | | |--- feature_4 > 2.50
|
| | | | | | | | |--- feature_4 > 2.50
|
||||||
@ -45,8 +23,30 @@
|
|||||||
| | | | | | | | | | |--- class: 1
|
| | | | | | | | | | |--- class: 1
|
||||||
| | | | | | | | | |--- feature_2 > 2.00
|
| | | | | | | | | |--- feature_2 > 2.00
|
||||||
| | | | | | | | | | |--- class: 0
|
| | | | | | | | | | |--- class: 0
|
||||||
| | | | | | | |--- feature_1 > 3.50
|
| | | | | | | |--- feature_1 > 1.50
|
||||||
| | | | | | | | |--- class: 0
|
| | | | | | | | |--- class: 0
|
||||||
|
| | | | | | |--- feature_0 > 2.50
|
||||||
|
| | | | | | | |--- feature_2 <= 2.50
|
||||||
|
| | | | | | | | |--- class: 1
|
||||||
|
| | | | | | | |--- feature_2 > 2.50
|
||||||
|
| | | | | | | | |--- feature_4 <= 2.50
|
||||||
|
| | | | | | | | | |--- class: 1
|
||||||
|
| | | | | | | | |--- feature_4 > 2.50
|
||||||
|
| | | | | | | | | |--- class: 0
|
||||||
|
| | | | | |--- feature_1 > 2.50
|
||||||
|
| | | | | | |--- feature_1 <= 3.50
|
||||||
|
| | | | | | | |--- feature_0 <= 3.50
|
||||||
|
| | | | | | | | |--- class: 0
|
||||||
|
| | | | | | | |--- feature_0 > 3.50
|
||||||
|
| | | | | | | | |--- feature_4 <= 2.50
|
||||||
|
| | | | | | | | | |--- class: 1
|
||||||
|
| | | | | | | | |--- feature_4 > 2.50
|
||||||
|
| | | | | | | | | |--- feature_2 <= 2.00
|
||||||
|
| | | | | | | | | | |--- class: 1
|
||||||
|
| | | | | | | | | |--- feature_2 > 2.00
|
||||||
|
| | | | | | | | | | |--- class: 0
|
||||||
|
| | | | | | |--- feature_1 > 3.50
|
||||||
|
| | | | | | | |--- class: 0
|
||||||
| | | | |--- feature_3 > 4.50
|
| | | | |--- feature_3 > 4.50
|
||||||
| | | | | |--- class: 0
|
| | | | | |--- class: 0
|
||||||
| |--- feature_4 > 3.50
|
| |--- feature_4 > 3.50
|
||||||
@ -73,8 +73,8 @@
|
|||||||
| | |--- feature_2 > 1.50
|
| | |--- feature_2 > 1.50
|
||||||
| | | |--- class: 0
|
| | | |--- class: 0
|
||||||
|--- feature_2 > 3.50
|
|--- feature_2 > 3.50
|
||||||
| |--- feature_4 <= 1.50
|
| |--- feature_1 <= 1.50
|
||||||
| | |--- feature_1 <= 1.50
|
| | |--- feature_4 <= 1.50
|
||||||
| | | |--- feature_2 <= 4.50
|
| | | |--- feature_2 <= 4.50
|
||||||
| | | | |--- feature_0 <= 1.50
|
| | | | |--- feature_0 <= 1.50
|
||||||
| | | | | |--- class: 0
|
| | | | | |--- class: 0
|
||||||
@ -85,7 +85,7 @@
|
|||||||
| | | | | | |--- class: 0
|
| | | | | | |--- class: 0
|
||||||
| | | |--- feature_2 > 4.50
|
| | | |--- feature_2 > 4.50
|
||||||
| | | | |--- class: 0
|
| | | | |--- class: 0
|
||||||
| | |--- feature_1 > 1.50
|
| | |--- feature_4 > 1.50
|
||||||
| | | |--- class: 0
|
| | | |--- class: 0
|
||||||
| |--- feature_4 > 1.50
|
| |--- feature_1 > 1.50
|
||||||
| | |--- class: 0
|
| | |--- class: 0
|
||||||
|
Binary file not shown.
BIN
last_map.nparr
Normal file
BIN
last_map.nparr
Normal file
Binary file not shown.
92
main.py
92
main.py
@ -1,25 +1,24 @@
|
|||||||
from asyncio import sleep
|
|
||||||
from calendar import c
|
|
||||||
from random import randint
|
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
from game_objects.player import Player
|
|
||||||
import pygame as pg
|
|
||||||
import sys
|
import sys
|
||||||
from os import path
|
from random import randint
|
||||||
import math
|
import math
|
||||||
from map import *
|
|
||||||
from settings import *
|
import pygame as pg
|
||||||
from map import map
|
import numpy
|
||||||
from map import map_utils
|
|
||||||
from path_search_algorthms import bfs
|
from game_objects.player import Player
|
||||||
from path_search_algorthms import a_star, a_star_utils
|
from game_objects.aiPlayer import aiPlayer
|
||||||
from decision_tree import decisionTree
|
|
||||||
from NeuralNetwork import prediction
|
|
||||||
from game_objects.trash import Trash
|
from game_objects.trash import Trash
|
||||||
|
|
||||||
from game_objects import aiPlayer
|
from map import map
|
||||||
import itertools
|
from map import map_utils
|
||||||
|
|
||||||
|
from path_search_algorthms import bfs
|
||||||
|
from path_search_algorthms import a_star_controller
|
||||||
|
from decision_tree import decisionTree
|
||||||
|
from NeuralNetwork import prediction
|
||||||
|
|
||||||
|
from settings import *
|
||||||
|
|
||||||
|
|
||||||
def getTree():
|
def getTree():
|
||||||
@ -51,17 +50,7 @@ class Game():
|
|||||||
# because dont work without data.txt
|
# because dont work without data.txt
|
||||||
# self.init_bfs()
|
# self.init_bfs()
|
||||||
# self.init_a_star()
|
# self.init_a_star()
|
||||||
self.t = aiPlayer.aiPlayer(self.player, game=self)
|
self.t = aiPlayer(self.player, game=self)
|
||||||
|
|
||||||
|
|
||||||
def get_actions_by_coords(self,x,y):
|
|
||||||
pos = (x,y)
|
|
||||||
offset_x, offset_y = self.camera.offset()
|
|
||||||
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
|
||||||
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
|
||||||
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
||||||
clicked_coords[0], clicked_coords[1], self.mapArray)
|
|
||||||
return actions
|
|
||||||
|
|
||||||
def init_game(self):
|
def init_game(self):
|
||||||
# initialize all variables and do all the setup for a new game
|
# initialize all variables and do all the setup for a new game
|
||||||
@ -70,6 +59,12 @@ class Game():
|
|||||||
|
|
||||||
# sprite groups and map array for calculations
|
# sprite groups and map array for calculations
|
||||||
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
|
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
|
||||||
|
|
||||||
|
# save current map
|
||||||
|
file = open('last_map.nparr', 'wb')
|
||||||
|
numpy.save(file, self.mapArray, allow_pickle=True)
|
||||||
|
file.close
|
||||||
|
|
||||||
self.trashDisplay = pg.sprite.Group()
|
self.trashDisplay = pg.sprite.Group()
|
||||||
self.agentSprites = pg.sprite.Group()
|
self.agentSprites = pg.sprite.Group()
|
||||||
# player obj
|
# player obj
|
||||||
@ -80,7 +75,6 @@ class Game():
|
|||||||
# other
|
# other
|
||||||
self.debug_mode = False
|
self.debug_mode = False
|
||||||
|
|
||||||
|
|
||||||
def init_bfs(self):
|
def init_bfs(self):
|
||||||
start_node = (0, 0)
|
start_node = (0, 0)
|
||||||
target_node = (18, 18)
|
target_node = (18, 18)
|
||||||
@ -96,17 +90,8 @@ class Game():
|
|||||||
nextNode = node[1]
|
nextNode = node[1]
|
||||||
print(realPath)
|
print(realPath)
|
||||||
|
|
||||||
def init_a_star(self):
|
|
||||||
# szukanie sciezki na sztywno i wyprintowanie wyniku (tablica stringow)
|
|
||||||
start_x = 0
|
|
||||||
start_y = 0
|
|
||||||
target_x = 6
|
|
||||||
target_y = 2
|
|
||||||
path = a_star.search_path(start_x, start_y, target_x, target_y, self.mapArray)
|
|
||||||
print(path)
|
|
||||||
|
|
||||||
def init_decision_tree(self):
|
def init_decision_tree(self):
|
||||||
# logika pracy z drzewem
|
# logika pracy z drzewem
|
||||||
self.positive_decision = []
|
self.positive_decision = []
|
||||||
self.negative_decision = []
|
self.negative_decision = []
|
||||||
|
|
||||||
@ -129,8 +114,10 @@ class Game():
|
|||||||
print(len(self.positive_decision))
|
print(len(self.positive_decision))
|
||||||
for i in self.positive_decision:
|
for i in self.positive_decision:
|
||||||
# print(i.get_coords())
|
# print(i.get_coords())
|
||||||
|
print('action')
|
||||||
trash_x, trash_y = i.get_coords()
|
trash_x, trash_y = i.get_coords()
|
||||||
action = self.get_actions_by_coords(trash_x, trash_y)
|
action = a_star_controller.get_actions_for_target_coords(trash_x, trash_y, self)
|
||||||
|
print(action)
|
||||||
self.t.startAiController(action)
|
self.t.startAiController(action)
|
||||||
|
|
||||||
print('')
|
print('')
|
||||||
@ -140,15 +127,15 @@ class Game():
|
|||||||
for i in range(0, 10):
|
for i in range(0, 10):
|
||||||
random = randint(0, 48)
|
random = randint(0, 48)
|
||||||
file = files[random]
|
file = files[random]
|
||||||
result = prediction.getPrediction(dir + '/' +file, 'trained_nn_20.pth')
|
result = prediction.getPrediction(dir + '/' + file, 'trained_nn_20.pth')
|
||||||
img = pg.image.load(dir + '/' +file).convert_alpha()
|
img = pg.image.load(dir + '/' + file).convert_alpha()
|
||||||
img = pg.transform.scale(img, (128, 128))
|
img = pg.transform.scale(img, (128, 128))
|
||||||
trash = Trash(img, 0, 0, 128, 128)
|
trash = Trash(img, 0, 0, 128, 128)
|
||||||
self.trashDisplay.add(trash)
|
self.trashDisplay.add(trash)
|
||||||
self.text_display = result
|
self.text_display = result
|
||||||
self.draw()
|
self.draw()
|
||||||
print(result + ' ' + file)
|
# print(result + ' ' + file)
|
||||||
pg.time.wait(1000)
|
pg.time.wait(100)
|
||||||
self.text_display = ''
|
self.text_display = ''
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
@ -156,12 +143,11 @@ class Game():
|
|||||||
|
|
||||||
# self.t.startAiController(self.positive_actions[0])
|
# self.t.startAiController(self.positive_actions[0])
|
||||||
|
|
||||||
|
|
||||||
def load_data(self):
|
def load_data(self):
|
||||||
game_folder = path.dirname(__file__)
|
game_folder = os.path.dirname(__file__)
|
||||||
img_folder = path.join(game_folder, 'resources/textures')
|
img_folder = os.path.join(game_folder, 'resources/textures')
|
||||||
|
|
||||||
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
self.player_img = pg.image.load(os.path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
||||||
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -195,8 +181,8 @@ class Game():
|
|||||||
map.render_tiles(self.trashDisplay, self.screen, self.camera)
|
map.render_tiles(self.trashDisplay, self.screen, self.camera)
|
||||||
|
|
||||||
# draw text
|
# draw text
|
||||||
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (0,0,0))
|
text_surface = pg.font.SysFont('Comic Sans MS', 30).render(self.text_display, False, (0, 0, 0))
|
||||||
self.screen.blit(text_surface, (0,128))
|
self.screen.blit(text_surface, (0, 128))
|
||||||
|
|
||||||
# rerender additional sprites
|
# rerender additional sprites
|
||||||
for sprite in self.agentSprites:
|
for sprite in self.agentSprites:
|
||||||
@ -221,16 +207,12 @@ class Game():
|
|||||||
pos = pg.mouse.get_pos()
|
pos = pg.mouse.get_pos()
|
||||||
offset_x, offset_y = self.camera.offset()
|
offset_x, offset_y = self.camera.offset()
|
||||||
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
||||||
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
actions = a_star_controller.get_actions_by_coords(clicked_coords[0], clicked_coords[1], self)
|
||||||
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
||||||
clicked_coords[0], clicked_coords[1], self.mapArray)
|
|
||||||
# print(actions)
|
|
||||||
|
|
||||||
if (actions != None):
|
if (actions != None):
|
||||||
self.t.startAiController(actions)
|
self.t.startAiController(actions)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# create the game object
|
# create the game object
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
10
main_test.py
Normal file
10
main_test.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from map import map_utils
|
||||||
|
from path_search_algorthms import a_star, a_star_utils
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
file = open('last_map.nparr', 'rb')
|
||||||
|
array = numpy.load(file)
|
||||||
|
file.close
|
||||||
|
|
||||||
|
actions = a_star.search_path(12, 0, a_star_utils.Rotation.UP, 1, 5, array)
|
||||||
|
print(actions)
|
@ -1,8 +1,13 @@
|
|||||||
from data_structures.heap import Heap
|
from data_structures.heap import Heap
|
||||||
from path_search_algorthms import a_star_utils as utils
|
from path_search_algorthms import a_star_utils as utils
|
||||||
|
|
||||||
def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array):
|
|
||||||
|
|
||||||
|
def get_cost(start_x: int, start_y: int, target_x: int, target_y: int, array):
|
||||||
|
actions = search_path(start_x, start_y, utils.Rotation.NONE, target_x, target_y, array)
|
||||||
|
return len(actions)
|
||||||
|
|
||||||
|
|
||||||
|
def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array):
|
||||||
start_node = utils.Node(start_x, start_y, agent_rotation)
|
start_node = utils.Node(start_x, start_y, agent_rotation)
|
||||||
target_node = utils.Node(target_x, target_y, utils.Rotation.NONE)
|
target_node = utils.Node(target_x, target_y, utils.Rotation.NONE)
|
||||||
|
|
||||||
@ -39,50 +44,9 @@ def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, targ
|
|||||||
neighbour.parent = node
|
neighbour.parent = node
|
||||||
|
|
||||||
# add to search
|
# add to search
|
||||||
if(not search_list.contains(neighbour)):
|
if (not search_list.contains(neighbour)):
|
||||||
search_list.append(neighbour, neighbour.f_cost())
|
search_list.append(neighbour, neighbour.f_cost())
|
||||||
|
|
||||||
# array version
|
|
||||||
|
|
||||||
# nodes for check
|
|
||||||
# search_list = [start_node]
|
|
||||||
|
|
||||||
# checked nodes
|
|
||||||
# searched_list: list[(int, int)] = []
|
|
||||||
|
|
||||||
# while (len(search_list) > 0):
|
|
||||||
# node = search_list[0]
|
|
||||||
|
|
||||||
# # find cheapest node in search_list
|
|
||||||
# for i in range(1, len(search_list)):
|
|
||||||
# if (search_list[i].f_cost() <= node.f_cost()):
|
|
||||||
# if(search_list[i].h_cost < node.h_cost):
|
|
||||||
# node = search_list[i]
|
|
||||||
|
|
||||||
# search_list.remove(node)
|
|
||||||
# searched_list.append((node.x, node.y))
|
|
||||||
|
|
||||||
# # check for target node
|
|
||||||
# if ((node.x, node.y) == (target_x, target_y)):
|
|
||||||
# return trace_path(node)
|
|
||||||
|
|
||||||
# # neightbours processing
|
|
||||||
# neighbours = utils.get_neighbours(node, searched_list, array)
|
|
||||||
# for neighbour in neighbours:
|
|
||||||
|
|
||||||
# # calculate new g cost for neightbour (start -> node -> neightbour)
|
|
||||||
# new_neighbour_cost = node.g_cost + utils.get_neighbour_cost(node, neighbour)
|
|
||||||
|
|
||||||
# if (new_neighbour_cost < neighbour.g_cost or neighbour not in search_list):
|
|
||||||
|
|
||||||
# # replace cost and set parent node
|
|
||||||
# neighbour.g_cost = new_neighbour_cost
|
|
||||||
# neighbour.h_cost = utils.get_h_cost(neighbour, target_node)
|
|
||||||
# neighbour.parent = node
|
|
||||||
|
|
||||||
# # add to search
|
|
||||||
# if(neighbour not in search_list):
|
|
||||||
# search_list.append(neighbour)
|
|
||||||
|
|
||||||
def trace_path(end_node: utils.Node):
|
def trace_path(end_node: utils.Node):
|
||||||
path = []
|
path = []
|
||||||
@ -91,9 +55,11 @@ def trace_path(end_node: utils.Node):
|
|||||||
# set final rotation of end_node because we don't do it before
|
# set final rotation of end_node because we don't do it before
|
||||||
node.rotation = utils.get_needed_rotation(node.parent, node)
|
node.rotation = utils.get_needed_rotation(node.parent, node)
|
||||||
|
|
||||||
while (node.parent != 0):
|
while (node.parent != False):
|
||||||
move = utils.get_move(node.parent, node)
|
if (node.parent == utils.Rotation.NONE):
|
||||||
path += move
|
path += "forward"
|
||||||
|
else:
|
||||||
|
path += utils.get_move(node.parent, node)
|
||||||
node = node.parent
|
node = node.parent
|
||||||
|
|
||||||
# delete move on initial tile
|
# delete move on initial tile
|
||||||
@ -107,6 +73,3 @@ def trace_path(end_node: utils.Node):
|
|||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
26
path_search_algorthms/a_star_controller.py
Normal file
26
path_search_algorthms/a_star_controller.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import math
|
||||||
|
from path_search_algorthms import a_star
|
||||||
|
from settings import *
|
||||||
|
|
||||||
|
def get_actions_by_coords(x, y, game):
|
||||||
|
# print('get_actions_by_coords')
|
||||||
|
# print(x, y, x/TILESIZE, y/TILESIZE)
|
||||||
|
offset_x, offset_y = game.camera.offset()
|
||||||
|
# print('offset ' + str(self.camera.offset()))
|
||||||
|
clicked_coords = [math.floor(x / TILESIZE) - offset_x, math.floor(y / TILESIZE) - offset_y]
|
||||||
|
# print(self.player.pos[0], self.player.pos[1], clicked_coords)
|
||||||
|
actions = a_star.search_path(math.floor(game.player.pos[0] / TILESIZE),
|
||||||
|
math.floor(game.player.pos[1] / TILESIZE), game.player.rotation(),
|
||||||
|
clicked_coords[0], clicked_coords[1], game.mapArray)
|
||||||
|
return actions
|
||||||
|
|
||||||
|
def get_actions_for_target_coords(x, y, game):
|
||||||
|
actions = a_star.search_path(
|
||||||
|
math.floor(game.player.pos[0] / TILESIZE),
|
||||||
|
math.floor(game.player.pos[1] / TILESIZE),
|
||||||
|
game.player.rotation(),
|
||||||
|
x / TILESIZE,
|
||||||
|
y / TILESIZE,
|
||||||
|
game.mapArray
|
||||||
|
)
|
||||||
|
return actions
|
@ -3,6 +3,7 @@ from enum import Enum
|
|||||||
from map import map_utils
|
from map import map_utils
|
||||||
from settings import *
|
from settings import *
|
||||||
|
|
||||||
|
|
||||||
class Rotation(Enum):
|
class Rotation(Enum):
|
||||||
UP = 0
|
UP = 0
|
||||||
RIGHT = 1
|
RIGHT = 1
|
||||||
@ -13,34 +14,37 @@ class Rotation(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, x: int, y: int, rotation: Rotation):
|
def __init__(self, x: int, y: int, rotation: Rotation):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.g_cost = 0
|
self.g_cost = 0
|
||||||
self.h_cost = 0
|
self.h_cost = 0
|
||||||
self.parent = 0
|
self.parent: Node | bool = False
|
||||||
self.rotation = rotation
|
self.rotation: Rotation = rotation
|
||||||
|
|
||||||
def f_cost(self):
|
def f_cost(self):
|
||||||
return self.g_cost + self.h_cost
|
return self.g_cost + self.h_cost
|
||||||
|
|
||||||
|
|
||||||
def get_neighbours(node, searched_list, array):
|
def get_neighbours(node, searched_list, array):
|
||||||
neighbours = []
|
neighbours = []
|
||||||
for offset_x in range (-1, 2):
|
for offset_x in range(-1, 2):
|
||||||
for offset_y in range (-1, 2):
|
for offset_y in range(-1, 2):
|
||||||
# don't look for cross neighbours
|
# don't look for cross neighbours
|
||||||
if(abs(offset_x) + abs(offset_y) == 1):
|
if (abs(offset_x) + abs(offset_y) == 1):
|
||||||
x = node.x + offset_x
|
x = node.x + offset_x
|
||||||
y = node.y + offset_y
|
y = node.y + offset_y
|
||||||
# prevent out of map coords
|
# prevent out of map coords
|
||||||
if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT):
|
if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT):
|
||||||
if(map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list):
|
if (map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list):
|
||||||
neighbour = Node(x, y, Rotation.NONE)
|
neighbour = Node(x, y, Rotation.NONE)
|
||||||
neighbour.rotation = get_needed_rotation(node, neighbour)
|
neighbour.rotation = get_needed_rotation(node, neighbour)
|
||||||
neighbours.append(neighbour)
|
neighbours.append(neighbour)
|
||||||
return neighbours
|
return neighbours
|
||||||
|
|
||||||
|
|
||||||
# move cost schema:
|
# move cost schema:
|
||||||
# - move from tile to tile: 10
|
# - move from tile to tile: 10
|
||||||
# - add extra 10 (1 rotation) if it exists
|
# - add extra 10 (1 rotation) if it exists
|
||||||
@ -49,11 +53,12 @@ def get_h_cost(start_node: Node, target_node: Node):
|
|||||||
distance_y = abs(start_node.y - target_node.y)
|
distance_y = abs(start_node.y - target_node.y)
|
||||||
cost = (distance_x + distance_y) * 10
|
cost = (distance_x + distance_y) * 10
|
||||||
|
|
||||||
if(distance_x > 0 and distance_y > 0):
|
if (distance_x > 0 and distance_y > 0):
|
||||||
cost += 10
|
cost += 10
|
||||||
|
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
|
|
||||||
# move cost schema:
|
# move cost schema:
|
||||||
# - move from tile to tile: 10
|
# - move from tile to tile: 10
|
||||||
# - every rotation 90*: 10
|
# - every rotation 90*: 10
|
||||||
@ -67,6 +72,7 @@ def get_neighbour_cost(start_node: Node, target_node: Node) -> int:
|
|||||||
else:
|
else:
|
||||||
return 30
|
return 30
|
||||||
|
|
||||||
|
|
||||||
# translate rotation change to move
|
# translate rotation change to move
|
||||||
def get_move(start_node: Node, target_node: Node):
|
def get_move(start_node: Node, target_node: Node):
|
||||||
rotate_change = get_rotate_change(start_node.rotation, target_node.rotation)
|
rotate_change = get_rotate_change(start_node.rotation, target_node.rotation)
|
||||||
@ -79,10 +85,12 @@ def get_move(start_node: Node, target_node: Node):
|
|||||||
else:
|
else:
|
||||||
return ["left", "forward"]
|
return ["left", "forward"]
|
||||||
|
|
||||||
|
|
||||||
# simple calc func
|
# simple calc func
|
||||||
def get_rotate_change(rotationA: Rotation, rotationB: Rotation) -> int:
|
def get_rotate_change(rotationA: Rotation, rotationB: Rotation) -> int:
|
||||||
return int(rotationA) - int(rotationB)
|
return int(rotationA) - int(rotationB)
|
||||||
|
|
||||||
|
|
||||||
# get new rotation for target_node as neighbour of start_node
|
# get new rotation for target_node as neighbour of start_node
|
||||||
def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation:
|
def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation:
|
||||||
if (start_node.x - target_node.x > 0):
|
if (start_node.x - target_node.x > 0):
|
||||||
@ -98,4 +106,3 @@ def get_needed_rotation(start_node: Node, target_node: Node) -> Rotation:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user