Skip to content
Snippets Groups Projects
Commit d8c05a65 authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

If a player is not created with a specified location, a random emtpy tile is chosen.

parent 28e18c05
No related branches found
No related tags found
1 merge request!1340-visualisierungsregeln
Pipeline #42057 failed
___________ __________________
_CCUCTCLCC_ _CCUCTCLCC________
_C_______C_ _C_______C________
_C_______C_ _C_______C________
_W_________ _W________________
_C_________ _C________________
_P_________ _P________________
_C_______C_ _C_______C________
_C_______X_ _C_______X________
_CCBBCCCCC_ _CCBBCCCCC________
___________ __________________
import sys import sys
import numpy as np
import pygame import pygame
from overcooked_simulator import ROOT_DIR from overcooked_simulator import ROOT_DIR
...@@ -13,8 +12,12 @@ def main(): ...@@ -13,8 +12,12 @@ def main():
simulator = Simulator(ROOT_DIR / "game_content" / "layouts" / "basic.layout", 600) simulator = Simulator(ROOT_DIR / "game_content" / "layouts" / "basic.layout", 600)
player_one_name = "p1" player_one_name = "p1"
player_two_name = "p2" player_two_name = "p2"
simulator.register_player(Player(player_one_name, np.array([350.0, 200.0]))) # simulator.register_player(Player(player_one_name, np.array([350.0, 200.0])))
simulator.register_player(Player(player_two_name, np.array([100.0, 200.0]))) # simulator.register_player(Player(player_two_name, np.array([100.0, 200.0])))
for i in range(90):
simulator.register_player(Player(f"p{i+3}"))
simulator.register_player(Player(player_one_name))
simulator.register_player(Player(player_two_name))
# TODO maybe read the player names and keyboard keys from config file? # TODO maybe read the player names and keyboard keys from config file?
keys1 = [ keys1 = [
......
...@@ -2,6 +2,7 @@ from __future__ import annotations ...@@ -2,6 +2,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import numpy.typing as npt
import yaml import yaml
from overcooked_simulator.game_items import ItemInfo from overcooked_simulator.game_items import ItemInfo
...@@ -81,7 +82,7 @@ class Environment: ...@@ -81,7 +82,7 @@ class Environment:
), # Stove with pot: U because it looks like a pot ), # Stove with pot: U because it looks like a pot
} }
self.counters: list[Counter] = self.create_counters(self.layout_path) self.counters, self.free_positions = self.create_counters(self.layout_path)
self.score: int = 0 self.score: int = 0
self.world_width: int = 800 self.world_width: int = 800
self.world_height: int = 600 self.world_height: int = 600
...@@ -107,8 +108,9 @@ class Environment: ...@@ -107,8 +108,9 @@ class Environment:
Args: Args:
layout_file: Path to the layout file. layout_file: Path to the layout file.
""" """
current_y = self.counter_side_length / 2 current_y: float = self.counter_side_length / 2
counters = [] counters: list[Counter] = []
free_positions: list[npt.NDArray] = []
with open(layout_file, "r") as layout_file: with open(layout_file, "r") as layout_file:
lines = layout_file.readlines() lines = layout_file.readlines()
...@@ -122,9 +124,13 @@ class Environment: ...@@ -122,9 +124,13 @@ class Environment:
if counter_class is not None: if counter_class is not None:
counter = counter_class(pos) counter = counter_class(pos)
counters.append(counter) counters.append(counter)
else:
free_positions.append(np.array([current_x, current_y]))
current_x += self.counter_side_length current_x += self.counter_side_length
current_y += self.counter_side_length current_y += self.counter_side_length
return counters
return counters, free_positions
def perform_action(self, action: Action): def perform_action(self, action: Action):
"""Performs an action of a player in the environment. Maps different types of action inputs to the """Performs an action of a player in the environment. Maps different types of action inputs to the
......
...@@ -14,9 +14,13 @@ class Player: ...@@ -14,9 +14,13 @@ class Player:
""" """
def __init__(self, name: str, pos: npt.NDArray[float]): def __init__(self, name: str, pos: Optional[npt.NDArray[float]] = None):
self.name: str = name self.name: str = name
self.pos: npt.NDArray[float] = np.array(pos, dtype=float) if pos is not None:
self.pos: npt.NDArray[float] = np.array(pos, dtype=float)
else:
self.pos = None
self.holding: Optional[Item] = None self.holding: Optional[Item] = None
self.radius: int = 18 self.radius: int = 18
......
import random
import time import time
from threading import Thread from threading import Thread
...@@ -73,6 +74,13 @@ class Simulator(Thread): ...@@ -73,6 +74,13 @@ class Simulator(Thread):
# print(f"Added player {player.name} to the game.") # print(f"Added player {player.name} to the game.")
self.env.players[player.name] = player self.env.players[player.name] = player
if player.pos is None:
if len(self.env.free_positions) > 0:
free_idx = random.randint(0, len(self.env.free_positions) - 1)
player.move_abs(self.env.free_positions[free_idx])
del self.env.free_positions[free_idx]
else:
print("No free positions left in kitchens.")
def register_players(self, players: list[Player]): def register_players(self, players: list[Player]):
"""Registers multiple players from a list """Registers multiple players from a list
...@@ -86,6 +94,13 @@ class Simulator(Thread): ...@@ -86,6 +94,13 @@ class Simulator(Thread):
def run(self): def run(self):
"""Starts the simulator thread. Runs in a loop until stopped.""" """Starts the simulator thread. Runs in a loop until stopped."""
for p in self.env.players.values():
print(p.pos)
assert all(
p.pos is not None for p in self.env.players.values()
), "At least one player position not initialized"
overslept_in_ns = 0 overslept_in_ns = 0
while not self.finished: while not self.finished:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment