SI_InteligentnyWozekWidlowy/GameModel.py
2022-04-28 21:22:19 +02:00

99 lines
3.1 KiB
Python

from typing import List
from mesa import Model
from mesa.space import MultiGrid
from mesa.time import RandomActivation
from AgentBase import AgentBase
from ForkliftAgent import ForkliftAgent
from InitialStateFactory import InitialStateFactory
from PatchAgent import PatchAgent
from PatchType import PatchType
from data.GameConstants import GameConstants
from decision.ActionType import ActionType
from pathfinding.PathfinderOnStates import PathFinderOnStates, PathFinderState
from util.PathDefinitions import GridLocation, GridWithWeights
class GameModel(Model):
def __init__(self, width, height, graph: GridWithWeights, graph2: GridWithWeights):
# self.num_agents = 5
self.running = True
self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self)
self.agents = [AgentBase]
self.forklift_agent = ForkliftAgent(self)
self.schedule.add(self.forklift_agent)
self.agents.append(self.forklift_agent)
initial_state_factory = InitialStateFactory()
self.game_constants = GameConstants(
width,
height,
graph.walls,
graph2.puddles
)
# Add the agent to a random grid cell
x = 5
y = 5
self.grid.place_agent(self.forklift_agent, (x, y))
self.forklift_agent.current_position = (x, y)
start, goal = (x, y), (1, 3)
pathFinder = PathFinderOnStates(
self.game_constants,
goal,
PathFinderState(self.forklift_agent.current_position, self.forklift_agent.current_rotation, 0,
ActionType.NONE, [])
)
self.place_patch_agents()
self.place_walls_agents(graph.walls)
self.place_puddles(graph2.puddles)
actions = pathFinder.get_action_list()
print("PATHFINDING")
print(actions)
self.forklift_agent.queue_movement_actions(actions)
def place_patch_agents(self):
agent = PatchAgent(self, PatchType.pickUp)
self.schedule.add(agent)
self.grid.place_agent(agent, (self.grid.width - 1, self.grid.height - 1))
self.agents.append(agent)
agent = PatchAgent(self, PatchType.dropOff)
# self.schedule.add(agent)
self.grid.place_agent(agent, (0, self.grid.height - 1))
self.agents.append(agent)
for i in range(3):
a = PatchAgent(self, PatchType.item)
self.agents.append(a)
self.grid.place_agent(a, (i, 0))
def place_walls_agents(self, walls: List[GridLocation]):
for w in walls:
agent = PatchAgent(self, PatchType.wall)
self.agents.append(agent)
self.grid.place_agent(agent, w)
def place_puddles(self, puddles: List[GridLocation]):
for p in puddles:
agent = PatchAgent(self, PatchType.diffTerrain)
self.agents.append(agent)
self.grid.place_agent(agent, p)
def step(self):
self.schedule.step()
print("update multiGrid")
self.grid.remove_agent(self.forklift_agent)
self.grid.place_agent(self.forklift_agent, self.forklift_agent.current_position)