2023-03-26 19:44:34 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from random import randint
|
|
|
|
|
2023-03-27 01:00:35 +02:00
|
|
|
import pygame as pg
|
|
|
|
from agent import Agent
|
|
|
|
|
2023-03-26 19:44:34 +02:00
|
|
|
ROAD_SPRITE = pg.Vector2(0, 0)
|
|
|
|
|
|
|
|
HOUSE_WITHOUT_TRASH_SPRITE = pg.Vector2(1, 0)
|
|
|
|
HOUSE_WITH_TRASH_SPRITE = pg.Vector2(1, 1)
|
|
|
|
HOUSE_SPRITES = {0: HOUSE_WITHOUT_TRASH_SPRITE,
|
|
|
|
1: HOUSE_WITH_TRASH_SPRITE}
|
|
|
|
|
|
|
|
TRUCK_SPRITE = pg.Vector2(2, 0)
|
|
|
|
|
|
|
|
PAPER_DUMP_SPRITE = pg.Vector2(3, 0)
|
|
|
|
PLASTIC_DUMP_SPRITE = pg.Vector2(3, 1)
|
|
|
|
GLASS_DUMP_SPRITE = pg.Vector2(3, 2)
|
|
|
|
MIXED_DUMP_SPRITE = pg.Vector2(3, 3)
|
|
|
|
DUMP_SPRITES = {'paper': PAPER_DUMP_SPRITE,
|
|
|
|
'glass': GLASS_DUMP_SPRITE,
|
|
|
|
'plastic': PLASTIC_DUMP_SPRITE,
|
|
|
|
'mixed': MIXED_DUMP_SPRITE}
|
|
|
|
|
2023-03-27 01:00:35 +02:00
|
|
|
TRASH_TYPES = ['paper', 'plastic', 'glass', 'mixed']
|
|
|
|
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
class Entity:
|
|
|
|
def __init__(self, state, position):
|
|
|
|
self.state = state
|
|
|
|
self.position = position
|
|
|
|
|
|
|
|
|
|
|
|
class TruckEntity(Entity):
|
|
|
|
def __init__(self, state, position):
|
|
|
|
super().__init__(state, position)
|
|
|
|
self.tile = TRUCK_SPRITE
|
2023-03-27 01:00:35 +02:00
|
|
|
self.entity_type = 'truck'
|
|
|
|
self.fullness = {'paper': 0,
|
|
|
|
'glass': 0,
|
|
|
|
'plastic': 0,
|
|
|
|
'mixed': 0}
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
def move(self, move_vector):
|
|
|
|
proposed_pos = self.position + move_vector
|
|
|
|
move_valid = True
|
|
|
|
|
2023-03-26 20:11:29 +02:00
|
|
|
if proposed_pos not in self.state.roads_pos \
|
|
|
|
and proposed_pos not in self.state.houses_pos \
|
|
|
|
and proposed_pos not in self.state.dumps_pos:
|
2023-03-26 19:44:34 +02:00
|
|
|
move_valid = False
|
|
|
|
if proposed_pos.x < 0 or proposed_pos.x >= self.state.world_limits.x:
|
|
|
|
move_valid = False
|
|
|
|
if proposed_pos.y < 0 or proposed_pos.y >= self.state.world_limits.y:
|
|
|
|
move_valid = False
|
|
|
|
|
|
|
|
if move_valid:
|
|
|
|
self.position = proposed_pos
|
|
|
|
|
|
|
|
|
2023-03-26 20:57:59 +02:00
|
|
|
class Bin:
|
|
|
|
def __init__(self, size, trash_type):
|
|
|
|
self.size = size
|
|
|
|
self.trash_type = trash_type
|
2023-03-27 01:00:35 +02:00
|
|
|
self.fullness = randint(0, 100)
|
2023-03-26 20:57:59 +02:00
|
|
|
|
2023-03-27 11:15:11 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.trash_type + " bin"
|
|
|
|
|
2023-03-26 20:57:59 +02:00
|
|
|
|
2023-03-26 19:44:34 +02:00
|
|
|
class HouseEntity(Entity):
|
|
|
|
def __init__(self, state, position):
|
|
|
|
super().__init__(state, position)
|
|
|
|
self.tile = HOUSE_SPRITES[0]
|
2023-03-27 01:00:35 +02:00
|
|
|
self.entity_type = 'house'
|
|
|
|
self.bins = [Bin(randint(1, 2), trash_type) for trash_type in TRASH_TYPES]
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DumpEntity(Entity):
|
|
|
|
def __init__(self, state, position, trash_type):
|
|
|
|
super().__init__(state, position)
|
|
|
|
self.tile = DUMP_SPRITES[trash_type]
|
2023-03-27 01:00:35 +02:00
|
|
|
self.entity_type = 'dump'
|
|
|
|
self.trash_type = trash_type
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SimulationState:
|
|
|
|
def __init__(self):
|
|
|
|
self.roads_pos = []
|
|
|
|
self.houses_pos = []
|
2023-03-26 20:11:29 +02:00
|
|
|
self.dumps_pos = []
|
2023-03-26 19:44:34 +02:00
|
|
|
self.entities = []
|
|
|
|
|
2023-03-26 20:57:59 +02:00
|
|
|
# stworzenie mapy i jednostek na podstawie pliku txt
|
2023-03-26 19:44:34 +02:00
|
|
|
map_path = Path("../res/map.txt")
|
|
|
|
with open(map_path, "r") as map_file:
|
|
|
|
map_data = map_file.readlines()
|
|
|
|
max_x = len(map_data[0].replace('\n', ''))
|
|
|
|
max_y = len(map_data)
|
|
|
|
for y in range(0, max_y):
|
|
|
|
for x in range(0, max_x):
|
|
|
|
tile = map_data[y].replace('\n', '')[x]
|
|
|
|
|
|
|
|
if tile == "O":
|
|
|
|
self.truck_origin = pg.Vector2(x, y)
|
|
|
|
self.roads_pos.append(pg.Vector2(x, y))
|
|
|
|
if tile == "R":
|
|
|
|
self.roads_pos.append(pg.Vector2(x, y))
|
|
|
|
if tile == "H":
|
|
|
|
self.houses_pos.append(pg.Vector2(x, y))
|
|
|
|
self.entities.append(HouseEntity(self, pg.Vector2(x, y)))
|
|
|
|
if tile == "M":
|
2023-03-26 20:11:29 +02:00
|
|
|
self.dumps_pos.append(pg.Vector2(x, y))
|
2023-03-26 19:44:34 +02:00
|
|
|
self.paper_dump_pos = pg.Vector2(x, y)
|
|
|
|
self.entities.append(DumpEntity(self, pg.Vector2(x, y), 'paper'))
|
|
|
|
if tile == "P":
|
2023-03-26 20:11:29 +02:00
|
|
|
self.dumps_pos.append(pg.Vector2(x, y))
|
2023-03-26 19:44:34 +02:00
|
|
|
self.plastic_dump_pos = pg.Vector2(x, y)
|
|
|
|
self.entities.append(DumpEntity(self, pg.Vector2(x, y), 'plastic'))
|
|
|
|
if tile == "S":
|
2023-03-26 20:11:29 +02:00
|
|
|
self.dumps_pos.append(pg.Vector2(x, y))
|
2023-03-26 19:44:34 +02:00
|
|
|
self.glass_dump_pos = pg.Vector2(x, y)
|
|
|
|
self.entities.append(DumpEntity(self, pg.Vector2(x, y), 'glass'))
|
|
|
|
if tile == "Z":
|
2023-03-26 20:11:29 +02:00
|
|
|
self.dumps_pos.append(pg.Vector2(x, y))
|
2023-03-26 19:44:34 +02:00
|
|
|
self.mixed_dump_pos = pg.Vector2(x, y)
|
|
|
|
self.entities.append(DumpEntity(self, pg.Vector2(x, y), 'mixed'))
|
2023-03-26 20:08:11 +02:00
|
|
|
self.entities.append(TruckEntity(self, self.truck_origin))
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
self.world_limits = pg.Vector2(max_x, max_y)
|
|
|
|
|
|
|
|
def update(self, move_agent):
|
|
|
|
for entity in self.entities:
|
2023-03-27 11:15:11 +02:00
|
|
|
if entity.entity_type == 'truck':
|
2023-03-26 19:44:34 +02:00
|
|
|
entity.move(move_agent)
|
|
|
|
|
|
|
|
|
|
|
|
class Layer:
|
|
|
|
def __init__(self, sim, texture_file):
|
|
|
|
self.sim = sim
|
|
|
|
self.texture_atlas = pg.image.load(texture_file)
|
|
|
|
|
|
|
|
def renderTile(self, surface, position, tile):
|
|
|
|
# pozycja na ekranie
|
|
|
|
sprite_pos = position.elementwise() * self.sim.cell_size
|
|
|
|
|
|
|
|
# tekstura
|
|
|
|
pos_in_atlas = tile.elementwise() * self.sim.cell_size
|
|
|
|
texture = pg.Rect(int(pos_in_atlas.x),
|
|
|
|
int(pos_in_atlas.y),
|
|
|
|
self.sim.cell_size.x,
|
|
|
|
self.sim.cell_size.y)
|
|
|
|
|
|
|
|
# render
|
|
|
|
surface.blit(self.texture_atlas, sprite_pos, texture)
|
|
|
|
|
|
|
|
def render(self, surface):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class EntityLayer(Layer):
|
|
|
|
def __init__(self, sim, texture_file, simulation_state, entities):
|
|
|
|
super().__init__(sim, texture_file)
|
|
|
|
self.simulation_state = simulation_state
|
|
|
|
self.entities = entities
|
|
|
|
|
|
|
|
def render(self, surface):
|
|
|
|
for entity in self.entities:
|
|
|
|
self.renderTile(surface, entity.position, entity.tile)
|
|
|
|
|
|
|
|
|
|
|
|
class StructureLayer(Layer):
|
|
|
|
def __init__(self, sim, texture_file, simulation_state, structures_pos, tile):
|
|
|
|
super().__init__(sim, texture_file)
|
|
|
|
self.simulation_state = simulation_state
|
|
|
|
self.structures_pos = structures_pos
|
|
|
|
self.tile = tile
|
|
|
|
|
|
|
|
def render(self, surface):
|
|
|
|
for position in self.structures_pos:
|
|
|
|
self.renderTile(surface, position, self.tile)
|
|
|
|
|
|
|
|
|
|
|
|
class Interface:
|
2023-03-27 01:00:35 +02:00
|
|
|
def __init__(self):
|
2023-03-26 19:44:34 +02:00
|
|
|
pg.init()
|
|
|
|
|
|
|
|
# stan symulacji
|
2023-03-27 01:00:35 +02:00
|
|
|
self.state = SimulationState()
|
|
|
|
self.move_truck = pg.Vector2(0, 0)
|
|
|
|
|
|
|
|
# autonomiczny agent
|
|
|
|
self.agent = Agent(self)
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
# rendering
|
|
|
|
self.cell_size = pg.Vector2(64, 64)
|
|
|
|
texture_file = Path("../res/tiles.png")
|
2023-03-27 01:00:35 +02:00
|
|
|
self.layers = [StructureLayer(self, texture_file, self.state, self.state.roads_pos, ROAD_SPRITE),
|
|
|
|
EntityLayer(self, texture_file, self.state, self.state.entities)]
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
# okno
|
|
|
|
pg.display.set_caption("Inteligentna śmieciarka")
|
2023-03-27 01:00:35 +02:00
|
|
|
window_size = self.state.world_limits.elementwise() * self.cell_size
|
2023-03-26 19:44:34 +02:00
|
|
|
self.window = pg.display.set_mode((int(window_size.x), int(window_size.y)))
|
2023-03-27 01:00:35 +02:00
|
|
|
self.state.world_limits.elementwise() * self.cell_size
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
# dla pętli
|
|
|
|
self.clock = pg.time.Clock()
|
|
|
|
self.run_simulation = True
|
|
|
|
|
|
|
|
self.debug_mode = False
|
|
|
|
|
|
|
|
def processUserInput(self):
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck = pg.Vector2(0, 0)
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
for event in pg.event.get():
|
|
|
|
if event.type == pg.QUIT:
|
|
|
|
self.run_simulation = False
|
|
|
|
break
|
|
|
|
elif event.type == pg.KEYDOWN:
|
|
|
|
if event.key == pg.K_ESCAPE:
|
|
|
|
self.run_simulation = False
|
|
|
|
break
|
|
|
|
if event.key == pg.K_BACKQUOTE:
|
|
|
|
self.debug_mode = not self.debug_mode
|
|
|
|
|
|
|
|
if self.debug_mode:
|
|
|
|
if event.key == pg.K_RIGHT:
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck.x = 1
|
2023-03-26 19:44:34 +02:00
|
|
|
if event.key == pg.K_LEFT:
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck.x = -1
|
2023-03-26 19:44:34 +02:00
|
|
|
if event.key == pg.K_DOWN:
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck.y = 1
|
2023-03-26 19:44:34 +02:00
|
|
|
if event.key == pg.K_UP:
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck.y = -1
|
2023-03-27 11:15:11 +02:00
|
|
|
if event.key == pg.K_d:
|
|
|
|
self.agent.discover()
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
def processAgentInput(self):
|
2023-03-27 01:00:35 +02:00
|
|
|
self.move_truck = self.agent.decide_move()
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
def update(self):
|
2023-03-27 01:00:35 +02:00
|
|
|
self.state.update(self.move_truck)
|
|
|
|
self.agent.update()
|
2023-03-26 19:44:34 +02:00
|
|
|
|
|
|
|
def render(self):
|
|
|
|
if not self.debug_mode:
|
|
|
|
self.window.fill((89, 183, 53))
|
|
|
|
else:
|
|
|
|
self.window.fill((180, 32, 42))
|
|
|
|
|
|
|
|
for layer in self.layers:
|
|
|
|
layer.render(self.window)
|
|
|
|
|
|
|
|
pg.display.update()
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
while self.run_simulation:
|
|
|
|
self.processUserInput()
|
|
|
|
if not self.debug_mode:
|
|
|
|
self.processAgentInput()
|
|
|
|
self.update()
|
|
|
|
self.render()
|
|
|
|
self.clock.tick(24)
|
|
|
|
pg.quit()
|