modified map + moving agent #19

Merged
s459307 merged 1 commits from s464921 into master 2022-05-10 20:27:15 +02:00
32 changed files with 411 additions and 31 deletions

View File

@ -3,12 +3,18 @@ import settings
class Instance: class Instance:
def __init__(self, tank_capacity, direction): def __init__(self, tank_capacity, direction, seeds, feritizer, water, carrots, potatoes, wheat):
size = settings.Field.size size = settings.Field.size
self.rect = pygame.Rect(0, 0, size(), size()) self.rect = pygame.Rect(0, 0, size(), size())
self.action = '' self.action = ''
self.tank_capacity = tank_capacity self.tank_capacity = tank_capacity
self.direction = direction self.direction = direction
self.seeds = seeds
self.feritizer = feritizer
self.water = water
self.carrots = carrots
self.potatoes = potatoes
self.wheat = wheat
def coordinates(self): def coordinates(self):
return { return {
@ -28,6 +34,48 @@ class Instance:
def set_tank_capacity(self, fuel_units): def set_tank_capacity(self, fuel_units):
self.tank_capacity = fuel_units self.tank_capacity = fuel_units
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_seeds(self):
return self.seeds
def set_seeds(self, seeds):
self.seeds = seeds
def get_feritizer(self):
return self.feritizer
def set_feritizer(self, feritizer):
self.feritizer = feritizer
def get_water(self):
return self.water
def set_water(self, water):
self.water = water
def get_carrots(self):
return self.carrots
def set_carrots(self, carrots):
self.carrots = carrots
def get_potatoes(self):
return self.potatoes
def set_potatoes(self, potatoes):
self.potatoes = potatoes
def get_wheat(self):
return self.wheat
def set_wheat(self, wheat):
self.wheat = wheat
def move(self, action): def move(self, action):
key_pressed = pygame.key.get_pressed() key_pressed = pygame.key.get_pressed()
height = settings.Pygame.height() height = settings.Pygame.height()

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
assets/plants/carrot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

BIN
assets/plants/potato.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
assets/plants/wheat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -143,11 +143,11 @@ def print_moves(elem):
return moves_list return moves_list
def is_move_allowed(node): def is_move_allowed(node):
if node.get_direction() == 2 and node.get_x() + 1 < settings.Pygame.width(): if node.get_direction() == 2 and node.get_x() + 1 < settings.Field.horizontal_count():
return "x + 1" return "x + 1"
elif node.get_direction() == 1 and node.get_y() - 1 >= 0: elif node.get_direction() == 1 and node.get_y() - 1 >= 0:
return "y - 1" return "y - 1"
elif node.get_direction() == 3 and node.get_y() + 1 < settings.Pygame.height(): elif node.get_direction() == 3 and node.get_y() + 1 < settings.Field.vertical_count():
return "y + 1" return "y + 1"
elif node.get_direction() == 4 and node.get_x() - 1 >= 0: elif node.get_direction() == 4 and node.get_x() - 1 >= 0:
return "x - 1" return "x - 1"

View File

@ -39,7 +39,6 @@ class Object:
def create(self): def create(self):
field_string = Type.get_string(self.type) field_string = Type.get_string(self.type)
img = pygame.image.load("./assets/fields/" + field_string + "/" + self.name + ".jpg") img = pygame.image.load("./assets/fields/" + field_string + "/" + self.name + ".jpg")
return pygame.transform.scale(img, (self.width, self.height)) return pygame.transform.scale(img, (self.width, self.height))
@ -83,6 +82,103 @@ class Dirt(Field):
def __init__(self): def __init__(self):
super(Dirt, self).__init__() super(Dirt, self).__init__()
class Carrot_empty(Field):
type = Type.PLANT
name = 'carrot_empty'
cost = 5
def __init__(self):
super(Carrot_empty, self).__init__()
class Carrot_sow(Field):
type = Type.PLANT
name = 'carrot_sow'
cost = 5
def __init__(self):
super(Carrot_sow, self).__init__()
class Carrot_watered(Field):
type = Type.PLANT
name = 'carrot_watered'
cost = 5
def __init__(self):
super(Carrot_watered, self).__init__()
class Carrot_feritized(Field):
type = Type.PLANT
name = 'carrot_feritized'
cost = 5
def __init__(self):
super(Carrot_feritized, self).__init__()
class Potato_empty(Field):
type = Type.PLANT
name = 'potato_empty'
cost = 5
def __init__(self):
super(Potato_empty, self).__init__()
class Potato_sow(Field):
type = Type.PLANT
name = 'potato_sow'
cost = 5
def __init__(self):
super(Potato_sow, self).__init__()
class Potato_watered(Field):
type = Type.PLANT
name = 'potato_watered'
cost = 5
def __init__(self):
super(Potato_watered, self).__init__()
class Potato_feritized(Field):
type = Type.PLANT
name = 'potato_feritized'
cost = 5
def __init__(self):
super(Potato_feritized, self).__init__()
class Wheat_empty(Field):
type = Type.PLANT
name = 'wheat_empty'
cost = 5
def __init__(self):
super(Wheat_empty, self).__init__()
class Wheat_sow(Field):
type = Type.PLANT
name = 'wheat_sow'
cost = 5
def __init__(self):
super(Wheat_sow, self).__init__()
class Wheat_watered(Field):
type = Type.PLANT
name = 'wheat_watered'
cost = 5
def __init__(self):
super(Wheat_watered, self).__init__()
class Wheat_feritized(Field):
type = Type.PLANT
name = 'wheat_feritized'
cost = 5
def __init__(self):
super(Wheat_feritized, self).__init__()
class Cobble(Field): class Cobble(Field):
type = Type.SPECIAL type = Type.SPECIAL

286
main.py
View File

@ -1,13 +1,12 @@
import pygame import math
import random import random
import graph import time
import pygame
import agent
import astar
import common
import field import field
import settings import settings
import common
import agent
import math
import time
import astar
possibleFields = { possibleFields = {
'dirt': field.Dirt(), 'dirt': field.Dirt(),
@ -15,8 +14,35 @@ possibleFields = {
'cobble': field.Cobble(), 'cobble': field.Cobble(),
'sand': field.Sand(), 'sand': field.Sand(),
'station': field.Station(), 'station': field.Station(),
'carrot_empty': field.Carrot_empty(),
'carrot_sow': field.Carrot_sow(),
'carrot_watered': field.Carrot_watered(),
'carrot_feritized': field.Carrot_feritized(),
'potato_empty': field.Potato_empty(),
'potato_sow': field.Potato_sow(),
'potato_watered': field.Potato_watered(),
'potato_feritized': field.Potato_feritized(),
'wheat_empty': field.Wheat_empty(),
'wheat_sow': field.Wheat_sow(),
'wheat_watered': field.Wheat_watered(),
'wheat_feritized': field.Wheat_feritized()
} }
possibleFieldsWithPlants = [
'carrot_empty',
'carrot_sow',
'carrot_watered',
'carrot_feritized',
'potato_empty',
'potato_sow',
'potato_watered',
'potato_feritized',
'wheat_empty',
'wheat_sow',
'wheat_watered',
'wheat_feritized'
]
def randomize_map(): def randomize_map():
fields_array = [] fields_array = []
@ -25,37 +51,47 @@ def randomize_map():
field_array_big = [] field_array_big = []
field_array_small = [] field_array_small = []
field_array_big_2 = [] field_array_big_2 = []
field_array_big_3 = []
field_array_small_2 = [] field_array_small_2 = []
field_array_small_3 = []
width = settings.Field.horizontal_count() width = settings.Field.horizontal_count()
height = settings.Field.vertical_count() height = settings.Field.vertical_count()
for i in range(width): for i in range(width):
for j in range(height): for j in range(height):
# k = random.choice(list(possibleFields.keys())) # k = random.choice(list(possibleFields.keys()))
x = random.uniform(0, 100) x = random.uniform(0, 100)
if x < 4: if x <= 80:
field_array_small.append(possibleFields['dirt'].tile.object) plant = random.choice(possibleFieldsWithPlants)
field_array_small.append(possibleFields[plant].tile.object)
field_array_small_2.append('dirt') field_array_small_2.append('dirt')
elif 4 < x < 55: field_array_small_3.append(plant)
elif 80 < x <= 90:
field_array_small.append(possibleFields['sand'].tile.object) field_array_small.append(possibleFields['sand'].tile.object)
field_array_small_2.append('sand') field_array_small_2.append('sand')
elif 55 < x < 100: field_array_small_3.append('sand')
elif 90 < x <= 100:
field_array_small.append(possibleFields['grass'].tile.object) field_array_small.append(possibleFields['grass'].tile.object)
field_array_small_2.append('grass') field_array_small_2.append('grass')
field_array_small_3.append('grass')
field_array_big.append(field_array_small) field_array_big.append(field_array_small)
field_array_big_2.append(field_array_small_2) field_array_big_2.append(field_array_small_2)
field_array_big_3.append(field_array_small_3)
field_array_small = [] field_array_small = []
field_array_small_2 = [] field_array_small_2 = []
field_array_small_3 = []
for i in range(height): for i in range(height):
field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object
field_array_big_2[math.floor(width / 2)][i] = 'cobble' field_array_big_2[math.floor(width / 2)][i] = 'cobble'
field_array_big_3[math.floor(width / 2)][i] = 'cobble'
for i in range(width): for i in range(width):
field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object
field_array_big_2[i][math.floor(height / 2)] = 'cobble' field_array_big_2[i][math.floor(height / 2)] = 'cobble'
field_array_big_3[i][math.floor(height / 2)] = 'cobble'
field_array_big[0][0] = possibleFields['station'].tile.object field_array_big[0][0] = possibleFields['station'].tile.object
field_array_big_2[0][0] = 'station' field_array_big_2[0][0] = 'station'
field_array_big_3[0][0] = 'station'
return field_array_big, field_array_big_2 return field_array_big, field_array_big_2, field_array_big_3
def read_img(agent, fields): def read_img(agent, fields):
@ -107,8 +143,216 @@ def draw_window(agent, fields):
pygame.display.update() pygame.display.update()
def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent):
fields_to_sow = []
fields_to_water = []
fields_to_feritize = []
fields_to_harvest = []
width = settings.Field.horizontal_count()
height = settings.Field.vertical_count()
k = 0
for i in range(width):
for j in range(height):
if fields_with_plants[i][j] == 'potato_empty' or fields_with_plants[i][j] == 'carrot_empty' or \
fields_with_plants[i][j] == 'wheat_empty':
tab = [i, j]
fields_to_sow.append(tab)
elif fields_with_plants[i][j] == 'potato_sow' or fields_with_plants[i][j] == 'carrot_sow' or \
fields_with_plants[i][j] == 'wheat_sow':
tab = [i, j]
fields_to_water.append(tab)
elif fields_with_plants[i][j] == 'potato_watered' or fields_with_plants[i][j] == 'carrot_watered' or \
fields_with_plants[i][j] == 'wheat_watered':
tab = [i, j]
fields_to_feritize.append(tab)
elif fields_with_plants[i][j] == 'potato_feritized' or fields_with_plants[i][j] == 'carrot_feritized' or \
fields_with_plants[i][j] == 'wheat_feritized':
tab = [i, j]
fields_to_harvest.append(tab)
while True:
decision = False # tu będzie wywołanie drzewa decyzyjnego (powrot do bazy)
cords = agent.coordinates()
x = cords['x']
y = cords['y']
dir = agent.get_direction()
if k == 0 and len(fields_to_harvest) > 0 and decision == False: # harvest
field_to_visit, l = get_closest_field(fields_to_harvest, x, y, dir, fields_for_astar)
x1, y1 = field_to_visit[0], field_to_visit[1]
del fields_to_harvest[l]
if fields_with_plants[x1][y1] == 'wheat_feritized':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_empty'
fields_for_movement[x1][y1] = possibleFields['wheat_empty'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_feritized':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_empty'
fields_for_movement[x1][y1] = possibleFields['carrot_empty'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_feritized':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_empty'
fields_for_movement[x1][y1] = possibleFields['potato_empty'].tile.object
draw_window(agent, fields_for_movement)
if len(fields_to_harvest) == 0:
k += 1
elif k == 1 and len(fields_to_water) > 0 and decision == False: # water
field_to_visit, l = get_closest_field(fields_to_water, x, y, dir, fields_for_astar)
del fields_to_water[l]
x1, y1 = field_to_visit[0], field_to_visit[1]
if fields_with_plants[x1][y1] == 'wheat_sow':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_watered'
fields_for_movement[x1][y1] = possibleFields['wheat_watered'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_sow':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_watered'
fields_for_movement[x1][y1] = possibleFields['carrot_watered'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_sow':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_watered'
fields_for_movement[x1][y1] = possibleFields['potato_watered'].tile.object
draw_window(agent, fields_for_movement)
if len(fields_to_water) == 0:
k += 1
elif k == 2 and len(fields_to_feritize) > 0 and decision == False: # feritize
field_to_visit, l = get_closest_field(fields_to_feritize, x, y, dir, fields_for_astar)
del fields_to_feritize[l]
x1, y1 = field_to_visit[0], field_to_visit[1]
if fields_with_plants[x1][y1] == 'wheat_watered':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_feritized'
fields_for_movement[x1][y1] = possibleFields['wheat_feritized'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_watered':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_feritized'
fields_for_movement[x1][y1] = possibleFields['carrot_feritized'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_watered':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_feritized'
fields_for_movement[x1][y1] = possibleFields['potato_feritized'].tile.object
draw_window(agent, fields_for_movement)
if len(fields_to_feritize) == 0:
k += 1
elif k == 3 and len(fields_to_sow) > 0 and decision == False: # sow
field_to_visit, l = get_closest_field(fields_to_sow, x, y, dir, fields_for_astar)
del fields_to_sow[l]
x1, y1 = field_to_visit[0], field_to_visit[1]
if fields_with_plants[x1][y1] == 'wheat_empty':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_sow'
fields_for_movement[x1][y1] = possibleFields['wheat_sow'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_empty':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_sow'
fields_for_movement[x1][y1] = possibleFields['carrot_sow'].tile.object
draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_empty':
state = astar.State(dir, x, y)
move_list = (
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
astar.succ))
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_sow'
fields_for_movement[x1][y1] = possibleFields['potato_sow'].tile.object
draw_window(agent, fields_for_movement)
if len(fields_to_sow) == 0:
k = 0
def get_closest_field(fields_to_visit, x, y, dir, fields_for_astar):
j = 0
closest_field = fields_to_visit[0]
state = astar.State(dir, x, y)
move_list_prev = (astar.graphsearch([], astar.f, [], [closest_field[0], closest_field[1]], state, fields_for_astar,
astar.succ))
for i in range(1, len(fields_to_visit)):
curr_field = fields_to_visit[i]
state = astar.State(dir, x, y)
move_list = (astar.graphsearch([], astar.f, [], [curr_field[0], curr_field[1]], state, fields_for_astar,
astar.succ))
if len(move_list) < len(move_list_prev):
closest_field = curr_field
move_list_prev = move_list
j = i
return closest_field, j
def agent_movement(move_list, agent, fields, fields_2):
for action in move_list:
if action == 'l' or action == 'r':
agent_action(agent.rotate(action))
elif action == 'f':
agent_action(agent.move(action))
draw_window(agent, fields)
time.sleep(0.5)
common = common.Instance() common = common.Instance()
agent = agent.Instance(1000, 2) agent = agent.Instance(1500, 2, 50, 50, 50, 0, 0, 0)
def main(): def main():
@ -121,10 +365,7 @@ def main():
settings.Pygame.height()))) settings.Pygame.height())))
pygame.display.set_caption(settings.Pygame.display_name()) pygame.display.set_caption(settings.Pygame.display_name())
fields, fields_2 = randomize_map() fields, fields_2, fields_3 = randomize_map()
state = astar.State(2, 0, 0)
move_list = (astar.graphsearch([], astar.f,[], [10,10] , state, fields_2 ,astar.succ))
print(move_list)
x = True x = True
while common.get('game_running'): while common.get('game_running'):
pygame.time.Clock().tick(settings.Pygame.fps()) pygame.time.Clock().tick(settings.Pygame.fps())
@ -136,14 +377,9 @@ def main():
else: else:
draw_window(agent, fields) draw_window(agent, fields)
if x: if x:
for action in move_list: generate_movement(fields_2, fields_3, fields, agent)
if action == 'l' or action == 'r':
agent_action(agent.rotate(action))
elif action == 'f':
agent_action(agent.move(action))
draw_window(agent, fields)
time.sleep(0.5)
x = False x = False
agent_action(agent.rotate(None)) agent_action(agent.rotate(None))
agent_action(agent.move(None)) agent_action(agent.move(None))

View File

@ -5,11 +5,11 @@ class Field:
@staticmethod @staticmethod
def horizontal_count(): def horizontal_count():
return 24 return 11
@staticmethod @staticmethod
def vertical_count(): def vertical_count():
return 16 return 11
class Pygame: class Pygame: