WMICraft/logic/level.py

270 lines
13 KiB
Python

import random
import pygame
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD
from algorithms.genetic.const import MAP_ALIASES
from algorithms.genetic.map_importer_exporter import import_random_map
from common.constants import *
from learning.decision_tree import DecisionTree
from logic.knights_queue import KnightsQueue
from models.castle import Castle
from models.knight import Knight
from models.monster import Monster
from models.tile import Tile
class Level:
def __init__(self, screen, logs):
self.screen = screen
self.logs = logs
self.decision_tree = DecisionTree()
# sprite group setup
self.sprites = pygame.sprite.LayeredUpdates()
self.map = []
self.list_knights_blue = []
self.list_knights_red = []
self.list_monsters = []
self.list_castles = []
self.knights_queue = None
def create_map(self):
self.map = import_random_map()
self.setup_base_tiles()
self.setup_objects()
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
def setup_base_tiles(self):
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))
for row_index, row in enumerate(self.map):
for col_index, col in enumerate(row):
# add base tiles, e.g. water, tree, grass
if col == MAP_ALIASES.get('WATER'):
texture_index = 5
texture_surface = textures[texture_index][1]
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
elif col == MAP_ALIASES.get('TREE'):
texture_index = 6
texture_surface = textures[texture_index][1]
Tile((col_index, row_index), texture_surface, self.sprites, 't')
elif col == MAP_ALIASES.get('SAND'):
texture_index = 4
texture_surface = textures[texture_index][1]
Tile((col_index, row_index), texture_surface, self.sprites)
else:
texture_index = random.randint(0, 3)
texture_surface = textures[texture_index][1]
Tile((col_index, row_index), texture_surface, self.sprites)
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):
# add objects, e.g. knights, monsters, castle
if col == MAP_ALIASES.get('KNIGHT_BLUE'):
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
self.map[row_index][col_index] = knight
self.list_knights_blue.append(knight)
elif col == MAP_ALIASES.get('KNIGHT_RED'):
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
self.map[row_index][col_index] = knight
self.list_knights_red.append(knight)
elif col == MAP_ALIASES.get('MONSTER'):
monster = Monster(self.screen, (col_index, row_index), self.sprites)
self.map[row_index][col_index] = monster
self.list_monsters.append(monster)
elif col == MAP_ALIASES.get('CASTLE'):
castle_count += 1
if castle_count == 4:
castle = Castle(self.screen, (col_index, row_index), self.sprites)
self.map[row_index][col_index] = castle
self.list_castles.append(castle)
#def attack_knight(self, knights_list, positions, current_knight):
# op_pos_1 = current_knight.position[0] - 1, current_knight.position[1]
# positions.append(op_pos_1)
# op_pos_2 = current_knight.position[0], current_knight.position[1] - 1
# positions.append(op_pos_2)
# op_pos_3 = current_knight.position[0] + 1, current_knight.position[1]
# positions.append(op_pos_3)
# op_pos_4 = current_knight.position[0], current_knight.position[1] + 1
# positions.append(op_pos_4)
# for some_knight in knights_list:
# for some_position in positions:
# if (some_knight.position == some_position and some_knight.team != current_knight.team):
# some_knight.health_bar.take_dmg(current_knight.attack)
# if some_knight.health_bar.current_hp == 0:
# some_knight.kill()
# positions.clear()
def attack_knight_left(self, knights_list, current_knight):
position_left = current_knight.position[0] - 1, current_knight.position[1]
for some_knight in knights_list:
if (some_knight.position == position_left and some_knight.team != current_knight.team):
some_knight.health_bar.take_dmg(current_knight.attack)
if some_knight.health_bar.current_hp <= 0:
some_knight.kill()
for monster in self.list_monsters:
if monster.position == position_left:
monster.health_bar.take_dmg(current_knight.attack)
if monster.health_bar.current_hp <= 0:
monster.kill()
else:
current_knight.health_bar.take_dmg(monster.attack)
if current_knight.health_bar.current_hp <= 0:
current_knight.kill()
for castle in self.list_castles:
if castle.position == position_left:
castle.health_bar.take_dmg(current_knight.attack)
def attack_knight_right(self, knights_list, current_knight):
position_right = current_knight.position[0] + 1, current_knight.position[1]
for some_knight in knights_list:
if (some_knight.position == position_right and some_knight.team != current_knight.team):
some_knight.health_bar.take_dmg(current_knight.attack)
if some_knight.health_bar.current_hp == 0:
some_knight.kill()
for monster in self.list_monsters:
if monster.position == position_right:
monster.health_bar.take_dmg(current_knight.attack)
if monster.health_bar.current_hp <= 0:
monster.kill()
else:
current_knight.health_bar.take_dmg(monster.attack)
if current_knight.health_bar.current_hp <= 0:
current_knight.kill()
for castle in self.list_castles:
if castle.position == position_right:
castle.health_bar.take_dmg(current_knight.attack)
def attack_knight_up(self, knights_list, current_knight):
position_up = current_knight.position[0], current_knight.position[1] - 1
for some_knight in knights_list:
if (some_knight.position == position_up and some_knight.team != current_knight.team):
some_knight.health_bar.take_dmg(current_knight.attack)
if some_knight.health_bar.current_hp == 0:
some_knight.kill()
for monster in self.list_monsters:
if monster.position == position_up:
monster.health_bar.take_dmg(current_knight.attack)
if monster.health_bar.current_hp <= 0:
monster.kill()
else:
current_knight.health_bar.take_dmg(monster.attack)
if current_knight.health_bar.current_hp <= 0:
current_knight.kill()
for castle in self.list_castles:
if castle.position == position_up:
castle.health_bar.take_dmg(current_knight.attack)
def attack_knight_down(self, knights_list, current_knight):
position_down = current_knight.position[0], current_knight.position[1] + 1
for some_knight in knights_list:
if (some_knight.position == position_down and some_knight.team != current_knight.team):
some_knight.health_bar.take_dmg(current_knight.attack)
if some_knight.health_bar.current_hp == 0:
some_knight.kill()
for monster in self.list_monsters:
if monster.position == position_down:
monster.health_bar.take_dmg(current_knight.attack)
if monster.health_bar.current_hp <= 0:
monster.kill()
else:
current_knight.health_bar.take_dmg(monster.attack)
if current_knight.health_bar.current_hp <= 0:
current_knight.kill()
for castle in self.list_castles:
if castle.position == position_down:
castle.health_bar.take_dmg(current_knight.attack)
def handle_turn(self):
current_knight = self.knights_queue.dequeue_knight()
knights_list = self.list_knights_red + self.list_knights_blue
print("next turn " + current_knight.team)
knight_pos_x = current_knight.position[0]
knight_pos_y = current_knight.position[1]
positions = []
goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight,
monsters=self.list_monsters,
opponents=self.list_knights_blue
if current_knight.team_alias() == 'k_r' else self.list_knights_red,
castle=self.list_castles[0])
if (len(self.list_knights_blue) == 0 or len(self.list_knights_red) == 0):
pygame.quit()
if len(goal_list) == 0:
return
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
action_list = a_star(state, self.map, goal_list)
print(action_list)
print(goal_list)
if len(action_list) == 0:
return
next_action = action_list.pop(0)
#if current_knight.health_bar.current_hp != 0:
#self.attack_knight(knights_list, positions, current_knight)
if current_knight.direction.name == UP:
self.attack_knight_up(knights_list, current_knight)
elif current_knight.direction.name == DOWN:
self.attack_knight_down(knights_list, current_knight)
elif current_knight.direction.name == RIGHT:
self.attack_knight_right(knights_list, current_knight)
elif current_knight.direction.name == LEFT:
self.attack_knight_left(knights_list, current_knight)
if next_action == TURN_LEFT:
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
current_knight.rotate_left()
elif next_action == TURN_RIGHT:
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w prawo.')
current_knight.rotate_right()
elif next_action == FORWARD:
current_knight.step_forward()
self.map[knight_pos_y][knight_pos_x] = MAP_ALIASES.get("GRASS")
# update knight on map
if current_knight.direction.name == UP:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
elif current_knight.direction.name == RIGHT:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w prawo.')
self.map[knight_pos_y][knight_pos_x + 1] = current_knight.team_alias()
elif current_knight.direction.name == DOWN:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w dół.')
self.map[knight_pos_y + 1][knight_pos_x] = current_knight.team_alias()
elif current_knight.direction.name == LEFT:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
def update(self):
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()