2022-04-10 20:28:50 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
import pygame
|
|
|
|
|
2022-05-08 19:26:09 +02:00
|
|
|
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD
|
2022-04-10 20:28:50 +02:00
|
|
|
from common.constants import *
|
2022-05-08 20:36:33 +02:00
|
|
|
from learning.decision_tree import DecisionTree
|
2022-04-11 00:01:57 +02:00
|
|
|
from logic.knights_queue import KnightsQueue
|
2022-04-10 20:28:50 +02:00
|
|
|
from logic.spawner import Spawner
|
|
|
|
from models.castle import Castle
|
|
|
|
from models.knight import Knight
|
|
|
|
from models.monster import Monster
|
|
|
|
from models.tile import Tile
|
|
|
|
|
|
|
|
|
|
|
|
class Level:
|
2022-04-12 20:44:27 +02:00
|
|
|
def __init__(self, screen, logs):
|
2022-04-10 20:28:50 +02:00
|
|
|
self.screen = screen
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs = logs
|
2022-05-08 20:36:33 +02:00
|
|
|
self.decision_tree = DecisionTree()
|
2022-04-10 20:28:50 +02:00
|
|
|
# sprite group setup
|
2022-04-28 10:18:17 +02:00
|
|
|
self.sprites = pygame.sprite.LayeredUpdates()
|
2022-04-10 20:28:50 +02:00
|
|
|
|
2022-04-13 18:35:02 +02:00
|
|
|
self.map = [['g' for _ in range(COLUMNS)] for y in range(ROWS)]
|
2022-04-10 20:28:50 +02:00
|
|
|
|
2022-04-11 00:01:57 +02:00
|
|
|
self.list_knights_blue = []
|
|
|
|
self.list_knights_red = []
|
|
|
|
self.list_monsters = []
|
|
|
|
self.list_castles = []
|
|
|
|
|
|
|
|
self.knights_queue = None
|
2022-04-10 20:28:50 +02:00
|
|
|
|
2022-04-11 00:01:57 +02:00
|
|
|
def create_map(self):
|
|
|
|
self.generate_map()
|
|
|
|
self.setup_base_tiles()
|
|
|
|
self.setup_objects()
|
|
|
|
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
|
|
|
|
|
|
|
|
def generate_map(self):
|
|
|
|
spawner = Spawner(self.map)
|
2022-04-13 18:35:02 +02:00
|
|
|
spawner.spawn_where_possible(['w' for _ in range(NBR_OF_WATER)])
|
|
|
|
spawner.spawn_where_possible(['t' for _ in range(NBR_OF_TREES)])
|
|
|
|
spawner.spawn_where_possible(['s' for _ in range(NBR_OF_SANDS)])
|
2022-04-10 20:28:50 +02:00
|
|
|
|
2022-04-13 18:35:02 +02:00
|
|
|
spawner.spawn_in_area(['k_b' for _ in range(4)], LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL,
|
2022-04-10 20:28:50 +02:00
|
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
2022-04-13 18:35:02 +02:00
|
|
|
spawner.spawn_in_area(['k_r' for _ in range(4)], RIGHT_KNIGHTS_SPAWN_FIRST_ROW, RIGHT_KNIGHTS_SPAWN_FIRST_COL,
|
2022-04-10 20:28:50 +02:00
|
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
|
|
|
|
|
|
spawner.spawn_in_area(['c'], CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL, CASTLE_SPAWN_WIDTH,
|
|
|
|
CASTLE_SPAWN_HEIGHT, 2)
|
|
|
|
|
2022-04-13 18:35:02 +02:00
|
|
|
spawner.spawn_where_possible(['m' for _ in range(NBR_OF_MONSTERS)])
|
2022-04-10 20:28:50 +02:00
|
|
|
|
2022-04-11 00:01:57 +02:00
|
|
|
def setup_base_tiles(self):
|
2022-04-10 20:28:50 +02:00
|
|
|
textures = []
|
|
|
|
for texture_path in TILES:
|
|
|
|
converted_texture = pygame.image.load('resources/textures/' + texture_path).convert_alpha()
|
|
|
|
converted_texture = pygame.transform.scale(converted_texture, (40, 40))
|
|
|
|
textures.append((texture_path, converted_texture))
|
|
|
|
|
2022-04-11 00:01:57 +02:00
|
|
|
for row_index, row in enumerate(self.map):
|
2022-04-10 20:28:50 +02:00
|
|
|
for col_index, col in enumerate(row):
|
|
|
|
|
|
|
|
# add base tiles, e.g. water, tree, grass
|
|
|
|
if col == "w":
|
|
|
|
texture_index = 5
|
|
|
|
texture_surface = textures[texture_index][1]
|
2022-04-11 12:00:15 +02:00
|
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
2022-04-10 20:28:50 +02:00
|
|
|
elif col == "t":
|
|
|
|
texture_index = 6
|
|
|
|
texture_surface = textures[texture_index][1]
|
2022-04-11 12:00:15 +02:00
|
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
2022-04-13 18:35:02 +02:00
|
|
|
elif col == "s":
|
|
|
|
texture_index = 4
|
|
|
|
texture_surface = textures[texture_index][1]
|
|
|
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
2022-04-10 20:28:50 +02:00
|
|
|
else:
|
2022-04-13 18:35:02 +02:00
|
|
|
texture_index = random.randint(0, 3)
|
2022-04-10 20:28:50 +02:00
|
|
|
texture_surface = textures[texture_index][1]
|
2022-04-11 12:00:15 +02:00
|
|
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
2022-04-11 00:01:57 +02:00
|
|
|
|
|
|
|
def setup_objects(self):
|
|
|
|
castle_count = 0 # TODO: find some smarter method to print castle
|
|
|
|
|
|
|
|
for row_index, row in enumerate(self.map):
|
|
|
|
print(row)
|
|
|
|
for col_index, col in enumerate(row):
|
2022-04-10 20:28:50 +02:00
|
|
|
|
|
|
|
# add objects, e.g. knights, monsters, castle
|
2022-04-11 00:01:57 +02:00
|
|
|
if col == "k_b":
|
2022-04-28 09:50:24 +02:00
|
|
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
|
2022-04-11 00:01:57 +02:00
|
|
|
self.map[row_index][col_index] = knight
|
|
|
|
self.list_knights_blue.append(knight)
|
|
|
|
elif col == "k_r":
|
2022-04-28 09:50:24 +02:00
|
|
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
|
2022-04-11 00:01:57 +02:00
|
|
|
self.map[row_index][col_index] = knight
|
|
|
|
self.list_knights_red.append(knight)
|
2022-04-10 20:28:50 +02:00
|
|
|
elif col == "m":
|
2022-04-28 14:13:59 +02:00
|
|
|
monster = Monster(self.screen, (col_index, row_index), self.sprites)
|
2022-04-11 00:01:57 +02:00
|
|
|
self.map[row_index][col_index] = monster
|
|
|
|
self.list_monsters.append(monster)
|
2022-04-10 20:28:50 +02:00
|
|
|
elif col == "c":
|
|
|
|
castle_count += 1
|
|
|
|
if castle_count == 4:
|
2022-04-28 14:13:59 +02:00
|
|
|
castle = Castle(self.screen, (col_index, row_index), self.sprites)
|
2022-04-11 00:01:57 +02:00
|
|
|
self.map[row_index][col_index] = castle
|
|
|
|
self.list_castles.append(castle)
|
|
|
|
|
|
|
|
def handle_turn(self):
|
|
|
|
print("next turn")
|
|
|
|
current_knight = self.knights_queue.dequeue_knight()
|
|
|
|
knight_pos_x = current_knight.position[0]
|
|
|
|
knight_pos_y = current_knight.position[1]
|
2022-04-11 12:56:22 +02:00
|
|
|
|
2022-05-09 18:22:01 +02:00
|
|
|
goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight,
|
2022-05-08 20:36:33 +02:00
|
|
|
monsters=self.list_monsters,
|
|
|
|
opponents=self.list_knights_red
|
|
|
|
if current_knight.team_alias == 'k_r' else self.list_knights_blue,
|
|
|
|
castle=self.list_castles[0])
|
2022-04-12 00:35:01 +02:00
|
|
|
|
2022-05-09 18:22:01 +02:00
|
|
|
if len(goal_list) == 0:
|
|
|
|
return
|
2022-04-12 00:35:01 +02:00
|
|
|
|
|
|
|
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
|
|
|
action_list = a_star(state, self.map, goal_list)
|
2022-04-11 00:01:57 +02:00
|
|
|
print(action_list)
|
2022-05-08 19:26:09 +02:00
|
|
|
print(goal_list)
|
2022-04-11 00:01:57 +02:00
|
|
|
|
|
|
|
if len(action_list) == 0:
|
|
|
|
return
|
|
|
|
|
2022-04-11 12:00:15 +02:00
|
|
|
next_action = action_list.pop(0)
|
2022-04-12 19:12:41 +02:00
|
|
|
if next_action == TURN_LEFT:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
2022-04-11 00:01:57 +02:00
|
|
|
current_knight.rotate_left()
|
2022-04-12 19:12:41 +02:00
|
|
|
elif next_action == TURN_RIGHT:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w prawo.')
|
2022-04-11 00:01:57 +02:00
|
|
|
current_knight.rotate_right()
|
2022-04-12 19:12:41 +02:00
|
|
|
elif next_action == FORWARD:
|
2022-04-11 00:01:57 +02:00
|
|
|
current_knight.step_forward()
|
2022-04-13 18:35:02 +02:00
|
|
|
self.map[knight_pos_y][knight_pos_x] = 'g'
|
2022-04-11 00:01:57 +02:00
|
|
|
|
2022-04-11 12:00:15 +02:00
|
|
|
# update knight on map
|
2022-04-12 19:12:41 +02:00
|
|
|
if current_knight.direction.name == UP:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
|
2022-04-12 20:21:29 +02:00
|
|
|
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
|
2022-04-12 19:12:41 +02:00
|
|
|
elif current_knight.direction.name == RIGHT:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w prawo.')
|
2022-04-12 20:21:29 +02:00
|
|
|
self.map[knight_pos_y][knight_pos_x + 1] = current_knight.team_alias()
|
2022-04-12 19:12:41 +02:00
|
|
|
elif current_knight.direction.name == DOWN:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w dół.')
|
2022-04-12 20:21:29 +02:00
|
|
|
self.map[knight_pos_y + 1][knight_pos_x] = current_knight.team_alias()
|
2022-04-12 19:12:41 +02:00
|
|
|
elif current_knight.direction.name == LEFT:
|
2022-04-12 20:44:27 +02:00
|
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
|
2022-04-12 20:21:29 +02:00
|
|
|
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
|
2022-04-11 00:01:57 +02:00
|
|
|
|
|
|
|
def update(self):
|
2022-04-10 20:28:50 +02:00
|
|
|
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
|
|
|
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
|
|
|
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(5, 5, bg_width, bg_height), 0, BORDER_RADIUS)
|
|
|
|
|
|
|
|
# update and draw the game
|
|
|
|
self.sprites.draw(self.screen)
|
2022-05-13 10:37:00 +02:00
|
|
|
self.sprites.update()
|