2022-04-12 14:47:30 +02:00
|
|
|
import math
|
2022-05-10 20:06:15 +02:00
|
|
|
import random
|
2022-04-28 09:21:32 +02:00
|
|
|
import time
|
2022-05-10 20:06:15 +02:00
|
|
|
import pygame
|
|
|
|
import agent
|
2022-04-28 21:32:34 +02:00
|
|
|
import astar
|
2022-05-10 20:06:15 +02:00
|
|
|
import common
|
|
|
|
import field
|
|
|
|
import settings
|
2022-03-11 03:02:29 +01:00
|
|
|
|
2022-03-11 03:34:20 +01:00
|
|
|
possibleFields = {
|
|
|
|
'dirt': field.Dirt(),
|
|
|
|
'grass': field.Grass(),
|
|
|
|
'cobble': field.Cobble(),
|
|
|
|
'sand': field.Sand(),
|
2022-04-12 14:47:30 +02:00
|
|
|
'station': field.Station(),
|
2022-05-10 20:06:15 +02:00
|
|
|
'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()
|
2022-03-11 03:34:20 +01:00
|
|
|
}
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-05-10 20:06:15 +02:00
|
|
|
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'
|
|
|
|
]
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
def randomize_map():
|
2022-03-11 03:34:20 +01:00
|
|
|
fields_array = []
|
|
|
|
for i in possibleFields:
|
|
|
|
fields_array.append(possibleFields[i].tile.object)
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_big = []
|
|
|
|
field_array_small = []
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_big_2 = []
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_big_3 = []
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_small_2 = []
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_small_3 = []
|
2022-04-12 14:47:30 +02:00
|
|
|
width = settings.Field.horizontal_count()
|
|
|
|
height = settings.Field.vertical_count()
|
|
|
|
for i in range(width):
|
|
|
|
for j in range(height):
|
|
|
|
# k = random.choice(list(possibleFields.keys()))
|
|
|
|
x = random.uniform(0, 100)
|
2022-05-10 20:06:15 +02:00
|
|
|
if x <= 80:
|
|
|
|
plant = random.choice(possibleFieldsWithPlants)
|
|
|
|
field_array_small.append(possibleFields[plant].tile.object)
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small_2.append('dirt')
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_small_3.append(plant)
|
|
|
|
elif 80 < x <= 90:
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small.append(possibleFields['sand'].tile.object)
|
|
|
|
field_array_small_2.append('sand')
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_small_3.append('sand')
|
|
|
|
elif 90 < x <= 100:
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_small.append(possibleFields['grass'].tile.object)
|
|
|
|
field_array_small_2.append('grass')
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_small_3.append('grass')
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_big.append(field_array_small)
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_big_2.append(field_array_small_2)
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_big_3.append(field_array_small_3)
|
2022-03-09 20:55:12 +01:00
|
|
|
field_array_small = []
|
2022-04-05 14:05:38 +02:00
|
|
|
field_array_small_2 = []
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_small_3 = []
|
2022-04-12 14:47:30 +02:00
|
|
|
|
|
|
|
for i in range(height):
|
|
|
|
field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object
|
|
|
|
field_array_big_2[math.floor(width / 2)][i] = 'cobble'
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_big_3[math.floor(width / 2)][i] = 'cobble'
|
2022-04-12 14:47:30 +02:00
|
|
|
for i in range(width):
|
|
|
|
field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object
|
|
|
|
field_array_big_2[i][math.floor(height / 2)] = 'cobble'
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_big_3[i][math.floor(height / 2)] = 'cobble'
|
2022-04-12 14:47:30 +02:00
|
|
|
field_array_big[0][0] = possibleFields['station'].tile.object
|
|
|
|
field_array_big_2[0][0] = 'station'
|
2022-05-10 20:06:15 +02:00
|
|
|
field_array_big_3[0][0] = 'station'
|
|
|
|
return field_array_big, field_array_big_2, field_array_big_3
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
def read_img(agent, fields):
|
2022-03-11 03:02:29 +01:00
|
|
|
window = common.get('window')
|
2022-03-11 03:34:20 +01:00
|
|
|
current_field = fields[agent.x()][agent.y()]
|
|
|
|
if current_field == possibleFields['grass'].tile.object:
|
|
|
|
window.blit(possibleFields['grass'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['dirt'].tile.object:
|
|
|
|
window.blit(possibleFields['dirt'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['sand'].tile.object:
|
|
|
|
window.blit(possibleFields['sand'].block.object, (0, 0))
|
|
|
|
elif current_field == possibleFields['cobble'].tile.object:
|
|
|
|
window.blit(possibleFields['cobble'].block.object, (0, 0))
|
2022-04-12 14:47:30 +02:00
|
|
|
elif current_field == possibleFields['station'].tile.object:
|
|
|
|
window.blit(possibleFields['station'].block.object, (0, 0))
|
2022-03-25 06:52:06 +01:00
|
|
|
|
2022-03-09 20:55:12 +01:00
|
|
|
pygame.display.update()
|
|
|
|
pygame.time.delay(2000)
|
2022-03-11 03:34:20 +01:00
|
|
|
common.set('state_imgShown', False)
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-11 03:34:20 +01:00
|
|
|
def agent_action(action: str):
|
|
|
|
if action == 'open_window':
|
|
|
|
common.set('state_imgShown', True)
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-11 03:02:29 +01:00
|
|
|
def draw_window(agent, fields):
|
|
|
|
window = common.get('window')
|
|
|
|
rect = agent.rect
|
2022-03-11 03:34:20 +01:00
|
|
|
for i in range(settings.Field.horizontal_count()):
|
|
|
|
for j in range(settings.Field.vertical_count()):
|
2022-03-11 03:02:29 +01:00
|
|
|
window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size()))
|
2022-04-28 09:21:32 +02:00
|
|
|
if agent.direction == 2:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_east.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 4:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_west.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 1:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_north.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-04-28 09:21:32 +02:00
|
|
|
elif agent.direction == 3:
|
2022-04-12 14:47:30 +02:00
|
|
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_south.png')
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
|
|
|
window.blit(AGENT, (rect.x, rect.y))
|
2022-03-11 03:02:29 +01:00
|
|
|
pygame.display.update()
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-05-10 20:06:15 +02:00
|
|
|
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()
|
2022-05-12 20:14:06 +02:00
|
|
|
fuel = agent.get_tank_capacity()
|
|
|
|
water = agent.get_water()
|
|
|
|
feritizer = agent.get_feritizer()
|
|
|
|
seeds = agent.get_seeds()
|
|
|
|
carrots = agent.get_carrots()
|
|
|
|
potatoes = agent.get_potatoes()
|
|
|
|
wheat = agent.get_wheat()
|
|
|
|
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
|
|
|
|
"potatoes:", potatoes, "wheat:", wheat)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
wheat += 1
|
|
|
|
agent.set_wheat(wheat)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
carrots += 1
|
|
|
|
agent.set_carrots(carrots)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
potatoes += 1
|
|
|
|
agent.set_potatoes(potatoes)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
water -= 1
|
|
|
|
agent.set_water(water)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
water -= 1
|
|
|
|
agent.set_water(water)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
water -= 1
|
|
|
|
agent.set_water(water)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
feritizer -= 1
|
|
|
|
agent.set_feritizer(feritizer)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
feritizer -= 1
|
|
|
|
agent.set_feritizer(feritizer)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
feritizer -= 1
|
|
|
|
agent.set_feritizer(feritizer)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
seeds -= 1
|
|
|
|
agent.set_seeds(seeds)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
seeds -= 1
|
|
|
|
agent.set_seeds(seeds)
|
2022-05-10 20:06:15 +02:00
|
|
|
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)
|
2022-05-12 20:14:06 +02:00
|
|
|
seeds -= 1
|
|
|
|
agent.set_seeds(seeds)
|
2022-05-10 20:06:15 +02:00
|
|
|
if len(fields_to_sow) == 0:
|
|
|
|
k = 0
|
2022-05-12 20:14:06 +02:00
|
|
|
elif decision:
|
|
|
|
print("Going back to base")
|
|
|
|
state = astar.State(dir, x, y)
|
|
|
|
move_list = (
|
|
|
|
astar.graphsearch([], astar.f, [], [0, 0], state, fields_for_astar,
|
|
|
|
astar.succ))
|
|
|
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
|
|
|
draw_window(agent, fields_for_movement)
|
|
|
|
agent.set_tank_capacity(1500)
|
|
|
|
agent.set_water(50)
|
|
|
|
agent.set_feritizer(50)
|
|
|
|
agent.set_seeds(50)
|
|
|
|
agent.set_carrots(0)
|
|
|
|
agent.set_potatoes(0)
|
|
|
|
agent.set_wheat(0)
|
|
|
|
print("I'm in base")
|
|
|
|
time.sleep(10)
|
2022-05-10 20:06:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
2022-05-12 20:14:06 +02:00
|
|
|
cords = agent.coordinates()
|
|
|
|
x = cords['x']
|
|
|
|
y = cords['y']
|
|
|
|
fuel = agent.get_tank_capacity()
|
|
|
|
if fields_2[x][y] == 'dirt':
|
|
|
|
fuel -= 5
|
|
|
|
elif fields_2[x][y] == 'cobble':
|
|
|
|
fuel -= 1
|
|
|
|
elif fields_2[x][y] == 'grass':
|
|
|
|
fuel -= 3
|
|
|
|
elif fields_2[x][y] == 'sand':
|
|
|
|
fuel -= 10
|
|
|
|
agent.set_tank_capacity(fuel)
|
2022-05-10 20:06:15 +02:00
|
|
|
draw_window(agent, fields)
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
2022-03-11 03:02:29 +01:00
|
|
|
common = common.Instance()
|
2022-05-10 20:06:15 +02:00
|
|
|
agent = agent.Instance(1500, 2, 50, 50, 50, 0, 0, 0)
|
2022-03-08 20:27:24 +01:00
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
def main():
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set('game_running', True)
|
2022-03-11 03:34:20 +01:00
|
|
|
common.set('state_imgShown', False)
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set(
|
|
|
|
'window',
|
|
|
|
pygame.display.set_mode((
|
|
|
|
settings.Pygame.width(),
|
|
|
|
settings.Pygame.height())))
|
|
|
|
|
|
|
|
pygame.display.set_caption(settings.Pygame.display_name())
|
2022-05-10 20:06:15 +02:00
|
|
|
fields, fields_2, fields_3 = randomize_map()
|
2022-04-28 09:21:32 +02:00
|
|
|
x = True
|
2022-03-11 03:02:29 +01:00
|
|
|
while common.get('game_running'):
|
|
|
|
pygame.time.Clock().tick(settings.Pygame.fps())
|
2022-03-08 20:27:24 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2022-03-11 03:02:29 +01:00
|
|
|
common.set('game_running', False)
|
2022-03-11 03:34:20 +01:00
|
|
|
if common.get('state_imgShown'):
|
2022-03-11 03:02:29 +01:00
|
|
|
read_img(agent, fields)
|
|
|
|
else:
|
|
|
|
draw_window(agent, fields)
|
2022-04-28 09:21:32 +02:00
|
|
|
if x:
|
2022-05-10 20:06:15 +02:00
|
|
|
generate_movement(fields_2, fields_3, fields, agent)
|
2022-04-28 09:21:32 +02:00
|
|
|
x = False
|
2022-05-10 20:06:15 +02:00
|
|
|
|
2022-04-28 09:21:32 +02:00
|
|
|
agent_action(agent.rotate(None))
|
|
|
|
agent_action(agent.move(None))
|
2022-03-09 20:55:12 +01:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
pygame.quit()
|
|
|
|
|
2022-04-12 14:47:30 +02:00
|
|
|
|
2022-03-08 20:27:24 +01:00
|
|
|
if __name__ == "__main__":
|
2022-03-11 03:02:29 +01:00
|
|
|
main()
|