WMICraft/logic/level.py

151 lines
6.7 KiB
Python
Raw Normal View History

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-05-31 01:03:00 +02:00
from algorithms.genetic.const import MAP_ALIASES
from algorithms.genetic.map_importer_exporter import import_random_map
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 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
self.sprites = pygame.sprite.LayeredUpdates()
2022-04-10 20:28:50 +02:00
2022-05-31 01:03:00 +02:00
self.map = []
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):
2022-05-31 01:03:00 +02:00
self.map = import_random_map()
2022-04-11 00:01:57 +02:00
self.setup_base_tiles()
self.setup_objects()
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
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
2022-05-31 01:03:00 +02:00
if col == MAP_ALIASES.get('WATER'):
2022-04-10 20:28:50 +02:00
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-05-31 01:03:00 +02:00
elif col == MAP_ALIASES.get('TREE'):
2022-04-10 20:28:50 +02:00
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-05-31 01:03:00 +02:00
elif col == MAP_ALIASES.get('SAND'):
2022-04-13 18:35:02 +02:00
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-05-31 01:03:00 +02:00
if col == MAP_ALIASES.get('KNIGHT_BLUE'):
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)
2022-05-31 01:03:00 +02:00
elif col == MAP_ALIASES.get('KNIGHT_RED'):
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-05-31 01:03:00 +02:00
elif col == MAP_ALIASES.get('MONSTER'):
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-05-31 01:03:00 +02:00
elif col == MAP_ALIASES.get('CASTLE'):
2022-04-10 20:28:50 +02:00
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):
current_knight = self.knights_queue.dequeue_knight()
2022-05-31 01:03:00 +02:00
print("next turn " + current_knight.team)
2022-04-11 00:01:57 +02:00
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
2022-05-31 01:03:00 +02:00
if current_knight.team_alias() == 'k_r' else self.list_knights_blue,
2022-05-08 20:36:33 +02:00
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-05-31 01:03:00 +02:00
self.map[knight_pos_y][knight_pos_x] = MAP_ALIASES.get("GRASS")
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)
self.sprites.update()