random first population - herbs random spawn works

This commit is contained in:
Mateusz 2020-05-28 23:57:27 +02:00
parent cf0a4be82f
commit 14850cd230
6 changed files with 41 additions and 169 deletions

View File

@ -459,155 +459,5 @@
"y": 15
},
"isPickupable" : false
},
{
"name" : "herb1",
"position" : {
"x": 12,
"y": 2
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb2",
"position" : {
"x": 16,
"y": 2
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb3",
"position" : {
"x": 17,
"y": 5
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb4",
"position" : {
"x": 14,
"y": 7
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb5",
"position" : {
"x": 17,
"y": 17
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb6",
"position" : {
"x": 13,
"y": 17
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb7",
"position" : {
"x": 5,
"y": 15
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb8",
"position" : {
"x": 2,
"y": 9
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb9",
"position" : {
"x": 8,
"y": 5
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
},
{
"name" : "herb10",
"position" : {
"x": 11,
"y": 10
},
"isPickupable": true,
"effect" : {
"hp" : 0,
"stamina" : 0,
"thirst" : 0,
"hunger" : 0
},
"type" : "herb"
}
]

View File

@ -5,8 +5,6 @@ from src.AI.GaTravelingForHerbs.GeneticAlgorithm import GeneticAlgorithm
START_COORD = [(6, 2)]
END_COORD = [(10, 7)]
COORDS = [(12, 2), (16, 2), (17, 5), (14, 7), (17, 17), (13, 17), (5, 15), (2, 9), (8, 5), (11, 10)]
class Traveling:
def __init__(self, coords):

View File

@ -251,5 +251,9 @@ class Entity(pygame.sprite.Sprite):
# If A* has ben called, move the entity
self.updateEntityCoords()
def __str__(self):
return "Entity - ID:{}, pos:({}x, {}y)".format(self.id, self.x, self.y)
# def __str__(self):
# return "Entity - ID:{}, pos:({}x, {}y)".format(self.id, self.x, self.y)
def __repr__(self):
coords = (self.x, self.y)
return str(coords)

View File

@ -1,8 +1,10 @@
import json
import random
from os import path
from pathlib import Path
import os
from random import sample
from random import shuffle
import pygame
@ -13,7 +15,7 @@ from src.AI.DecisionTrees.TestDecisionTree import testDecisionTree
from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import SurvivalClassification
from src.AI.GA import geneticAlgorithm
from src.AI.GaTravelingForHerbs.GeneticAlgorithm import GeneticAlgorithm
from src.AI.GaTravelingForHerbs.Traveling import Traveling, START_COORD, COORDS, END_COORD
from src.AI.GaTravelingForHerbs.Traveling import Traveling, START_COORD, END_COORD
from src.entities.Player import Player
from src.game.EventManager import EventManager
from src.game.Map import Map
@ -410,6 +412,7 @@ class Game:
self.screen = Screen(self, self.config["window"])
print("OK")
# Initialize map
self.initializeMap(filesPath)
# Initialize the player
@ -417,25 +420,23 @@ class Game:
self.map.addEntity(self.player, DONTADD=True)
self.eventManager = EventManager(self, self.player)
firstGeneration = [Traveling(START_COORD + sample(COORDS, len(COORDS)) + END_COORD) for _ in range(100)]
mutationProbability = float(0.1)
self.travelCoords = random.sample(self.map.movableList(), 10)
import ast
self.travelCoords = ast.literal_eval(str(self.travelCoords))
self.map.insertHerbs(self.travelCoords)
# Initialize genetic algorithm
firstGeneration = [Traveling(START_COORD + sample(self.travelCoords, len(self.travelCoords)) + END_COORD) for _
in range(100)]
mutationProbability = float(0.1)
ga = GeneticAlgorithm(firstGeneration, mutationProbability)
movementList = ga.listOfTravel()
self.movementList = ga.listOfTravel()
self.entityToVisitList = []
for i in movementList:
for i in self.movementList:
self.entityToVisitList.append(self.map.getEntityOnCoord(i))
self.entityToVisitList.remove(self.entityToVisitList[0])
# for i in entityToVisitList:
# self.player.gotoToTarget(i, self.map)
# print("ile")
# Start game loop
self.mainLoop()

View File

@ -22,9 +22,10 @@ class Map:
"""
# TODO: Should map be self-aware of its own loacation?
self.screen = screen
# tekstowa macierz terenów
self.terrain = []
# tereny bez kolizji
# tereny nie kolizyjne, potrzebne też do metody movableList, która zwraca tereny na których nie ma żadnych entity
self.terrainTilesList = []
# grupa objektów kolizyjnych (tereny kolizyjne i entities)
self.collidables = pygame.sprite.Group()
@ -242,6 +243,20 @@ class Map:
return True
return False
def insertHerbs(self, coordsList):
nr = 1
for i in range(10):
entity = Pickupable("herb" + str(nr) + ".png", self.tileSize, coordsList[i], Statistics(0, 0, 0, 0), "herb")
self.entitiesRawData.append(entity)
self.addEntity(entity)
nr += 1
def movableList(self):
terrainList = self.terrainTilesList
for i in self.entitiesRawData:
terrainList.remove(self.getTileOnCoord((i.x, i.y)))
return terrainList
def __del__(self):
for entity in self.entities.sprites():
entity.kill()

View File

@ -24,3 +24,7 @@ class TerrainTile(pygame.sprite.Sprite):
self.rect.x = x * tileSize
self.rect.y = y * tileSize
self.cost = cost
def __repr__(self):
coords = (self.x, self.y)
return str(coords)