Skip to content

Commit

Permalink
commit shaders and fix/test other examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Bam4d committed Dec 24, 2023
1 parent 1dc4455 commit 83d0b69
Show file tree
Hide file tree
Showing 62 changed files with 104 additions and 115 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ tests/vendor/*
!tests/vendor/CMakeLists.txt*

# Vulkan
*.spv
*.ppm

# Idea
Expand Down
6 changes: 6 additions & 0 deletions compile_shaders.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ CALL :compile_shaders_in_dir .\tests\resources\observer\block\shaders\global_lig
CALL :compile_shaders_in_dir .\tests\resources\observer\isometric\shaders\lighting
CALL :compile_shaders_in_dir .\tests\resources\observer\sprite\shaders\health_bars

CALL :compile_shaders_in_dir .\python\examples\Custom Shaders\Global Lighting\shaders
CALL :compile_shaders_in_dir .\python\examples\Custom Shaders\Health Bars\shaders
CALL :compile_shaders_in_dir .\python\examples\Custom Shaders\Object Lighting\shaders



EXIT /B 0

:compile_shaders_in_dir
Expand Down
9 changes: 7 additions & 2 deletions compile_shaders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ cd "$(dirname "$0")"

compile_shaders_in_dir () {
echo "Compiling shaders in $1"
$GLSLC_BIN $1/triangle-textured.frag -o $1/triangle-textured.frag.spv
$GLSLC_BIN $1/triangle-textured.vert -o $1/triangle-textured.vert.spv
$GLSLC_BIN "$1/triangle-textured.frag" -o "$1/triangle-textured.frag.spv"
$GLSLC_BIN "$1/triangle-textured.vert" -o "$1/triangle-textured.vert.spv"
}

compile_shaders_in_dir ./resources/shaders/default/block
Expand All @@ -14,3 +14,8 @@ compile_shaders_in_dir ./resources/shaders/default/isometric
compile_shaders_in_dir ./tests/resources/observer/block/shaders/global_lighting
compile_shaders_in_dir ./tests/resources/observer/isometric/shaders/lighting
compile_shaders_in_dir ./tests/resources/observer/sprite/shaders/health_bars


compile_shaders_in_dir "./python/examples/Custom Shaders/Global Lighting/shaders"
compile_shaders_in_dir "./python/examples/Custom Shaders/Health Bars/shaders"
compile_shaders_in_dir "./python/examples/Custom Shaders/Object Lighting/shaders"
13 changes: 0 additions & 13 deletions docs/getting-started/action spaces/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,6 @@ Lets say our RTS game has units that have an action ``move`` and an action ``gat

.. code-block:: python
# env.step([
# [ # List of actions for player 1
# [x1, y1, action_type1, action_id1],
# [x2, y2, action_type2, action_id2],
# ...
# ],
# [ # List of actions for player 2
# [x1, y1, action_type1, action_id1],
# [x2, y2, action_type2, action_id2],
# ..
# ],
# ])
env.step([
# Player 1
[
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/observation spaces/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The observations for environments where a single avatar is being controlled are
# obs = np.array([ ... ]) # Player observation
obs, reward, done, info = env.step( ... )
obs, reward, done, truncated, info = env.step( ... )
# obs = np.array([ ... ]) # Player observation
Expand Down
17 changes: 6 additions & 11 deletions python/examples/AStar Search/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
# Uncommment to see normal actions (not rotated) being used

# Astar action space: 0: up, 1: down, 2: left, 3: right
# env = GymWrapper('astar_opponent_environment.yaml',
# player_observer_type=gd.ObserverType.VECTOR,
# global_observer_type=gd.ObserverType.SPRITE_2D,
# level=0)

# env = GymWrapper('astar_opponent_rotation_actions_environment.yaml',
# player_observer_type=gd.ObserverType.VECTOR,
# global_observer_type=gd.ObserverType.SPRITE_2D,
# level=0)

# Uncommment to see multiple spiders chasing!
env = gym(
"astar_opponent_rotation_actions_environment.yaml",
# Astar action space: 1: Rotate left, 2: Move forward, 3: Rotate right
env = GymWrapper(
"./astar_opponent_rotation_actions_environment.yaml",
player_observer_type=gd.ObserverType.VECTOR,
global_observer_type=gd.ObserverType.SPRITE_2D,
level=1,
Expand Down
5 changes: 3 additions & 2 deletions python/examples/Custom Shaders/Global Lighting/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToFile, RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"global_lighting.yaml",
player_observer_type=gd.ObserverType.SPRITE_2D,
global_observer_type=gd.ObserverType.SPRITE_2D,
Expand Down

This file was deleted.

Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions python/examples/Custom Shaders/Health Bars/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToFile, RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"health_bars.yaml",
player_observer_type=gd.ObserverType.SPRITE_2D,
global_observer_type=gd.ObserverType.SPRITE_2D,
Expand Down
Binary file modified python/examples/Custom Shaders/Health Bars/reset_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions python/examples/Custom Shaders/Object Lighting/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToFile, RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"object_lighting.yaml",
player_observer_type=gd.ObserverType.SPRITE_2D,
global_observer_type=gd.ObserverType.SPRITE_2D,
Expand Down
Binary file modified python/examples/Custom Shaders/Object Lighting/reset_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Custom Shaders/Object Lighting/reset_partial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

Binary file not shown.
Binary file not shown.
9 changes: 4 additions & 5 deletions python/examples/Custom Shaders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

In this directory are a few examples of how to make use of custom shaders to modify the standard pixel output for more complex environnments.


## Global Lighting

We use the episode step timer `_steps` to create a day/night cycle!
We use the episode step timer `_steps` to create a day/night cycle!

![Global Lighting](Global%20Lighting/global_lighting.gif)

## Health Bars

In this example we show health bars on individual units in a combat scenario.
In this example we show health bars on individual units in a combat scenario.

![Health Bars](Health%20Bars/health_bars.gif)

## Object Lighting
## Object Lighting

Add partial observability to agents by only lighting up the immediate area around the agent and the goal

![Object Lighting](Object%20Lighting/object_lighting.gif)
![Object Lighting](Object%20Lighting/object_lighting.gif)
Binary file modified python/examples/Level Design/custom_level_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/custom_level_string_player_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/custom_level_string_player_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_0_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_1_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_1_player_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_1_player_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_2_global.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_2_player_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Level Design/level_2_player_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 9 additions & 8 deletions python/examples/Level Design/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToFile
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"levels.yaml",
player_observer_type=gd.ObserverType.BLOCK_2D,
global_observer_type=gd.ObserverType.BLOCK_2D,
Expand All @@ -26,13 +27,13 @@
image_renderer.render(obs, f"level_{i}_player_2.png")

level_string = \
""". c c c c c c . . c c c c c c . . c c . c c c c c c . . c c c c c c . . c c . . . . . . c c . . . . c c .
c c . . . . . . . c c . . . c c . c c . c c . . . c c . c c . . . c c . c c . . . . . . . c c . . c c . .
c c . . . c c c . c c c c c c . . c c . c c . . . c c . c c . . . c c . c c . . . . . . . . c c c c . . .
c c . . . . c c . c c . . . c c . c c . c c . . . c c . c c . . . c c . c c . . . . . . . . . c c . . . .
. c c c c c c . . c c . . . c c . c c . c c c c c c . . c c c c c c . . c c c c c c c . . . . c c . . . .
""". c c c c c c . . c c c c c c . . c c . c1 c1 c1 c1 c1 c1 . . c2 c2 c2 c2 c2 c2 . . c c . . . . . . c c . . . . c c .
c c . . . . . . . c c . . . c c . c c . c1 c1 . . . c1 c1 . c2 c2 . . . c2 c2 . c c . . . . . . . c c . . c c . .
c c . . . c c c . c c c c c c . . c c . c1 c1 . . . c1 c1 . c2 c2 . . . c2 c2 . c c . . . . . . . . c c c c . . .
c c . . . . c c . c c . . . c c . c c . c1 c1 . . . c1 c1 . c2 c2 . . . c2 c2 . c c . . . . . . . . . c c . . . .
. c c c c c c . . c c . . . c c . c c . c1 c1 c1 c1 c1 c1 . . c2 c2 c2 c2 c2 c2 . . c c c c c c c . . . . c c . . . .
"""
env.reset(options={level_string: level_string})
env.reset(options={"level_string": level_string})
obs = global_obs_render_wrapper.render()
image_renderer.render(obs, "custom_level_global.png")
obs = player_1_obs_render_wrapper.render()
Expand Down
Binary file modified python/examples/Procedural Generation/example_maze_13x13_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Procedural Generation/example_maze_13x13_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Procedural Generation/example_maze_13x13_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Procedural Generation/example_maze_45x45_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python/examples/Procedural Generation/example_maze_45x45_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 31 additions & 20 deletions python/examples/Procedural Generation/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import gym
from typing import Any, Dict

import gymnasium as gym
import numpy as np
import numpy.typing as npt

from griddly.util.rllib.environment.level_generator import LevelGenerator
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToFile
from griddly.wrappers.render_wrapper import RenderWrapper


class LabyrinthLevelGenerator(LevelGenerator):
class LabyrinthLevelGenerator:
WALL = "w"
AGENT = "A"
GOAL = "x"
EMPTY = "."

def __init__(self, config):
def __init__(self, config: Dict[str, Any]) -> None:
"""
Initialize the LabyrinthLevelGenerator.
Expand All @@ -35,9 +40,8 @@ def __init__(self, config):
env.reset(level_string=level_string)
"""

super().__init__(config)
self._width = config.get("width", 9)
self._height = config.get("height", 9)
self._width: int = config.get("width", 9)
self._height: int = config.get("height", 9)
self._wall_density = config.get(
"wall_density", 1
) # Adjust this value to control wall density
Expand All @@ -46,7 +50,7 @@ def __init__(self, config):

self._num_goals = config.get("num_goals", 1)

def _generate_maze(self):
def _generate_maze(self) -> npt.NDArray:
"""
Generate the maze grid.
Expand All @@ -64,7 +68,7 @@ def _generate_maze(self):
)

# Recursive Backtracking algorithm for generating maze paths
def recursive_backtracking(x, y):
def recursive_backtracking(x: int, y: int) -> None:
maze[x, y] = LabyrinthLevelGenerator.EMPTY

# Randomize the order of directions to explore
Expand Down Expand Up @@ -99,7 +103,7 @@ def recursive_backtracking(x, y):

return maze

def _is_reachable(self, maze, x, y):
def _is_reachable(self, maze: npt.NDArray, x: int, y: int) -> bool:
"""
Check if a tile is reachable from the agent's starting position using a flood-fill algorithm.
Expand Down Expand Up @@ -133,11 +137,16 @@ def _is_reachable(self, maze, x, y):
):
stack.append((nx, ny))

return len(visited) == self._width * self._height - np.sum(
len_visited = len(visited)
not_walls: int = self._width * self._height - np.sum(
maze == LabyrinthLevelGenerator.WALL
)

def _place_goals(self, maze, agent_x, agent_y):
return len_visited == not_walls

def _place_goals(
self, maze: npt.NDArray, agent_x: int, agent_y: int
) -> npt.NDArray:
"""
Place the goals in the maze while ensuring they don't block the agent's access to all tiles.
Expand Down Expand Up @@ -167,7 +176,7 @@ def _place_goals(self, maze, agent_x, agent_y):

return maze

def generate(self):
def generate(self) -> str:
"""
Generate a new maze level.
Expand Down Expand Up @@ -204,11 +213,15 @@ def generate(self):


if __name__ == "__main__":
import matplotlib.pyplot as plt

# Use the mechanics from the built in GDY-Labyrinth-v0 environment
env = gym.make("GDY-Labyrinth-v0")
sizes = ["45x45", "21x21", "13x13"]

assert isinstance(env, GymWrapper)

image_renderer = RenderToFile()
global_obs_render_wrapper = RenderWrapper(env, "global", "rgb_array")

for size in sizes:
for i in range(3):
config = {
Expand All @@ -217,9 +230,7 @@ def generate(self):
}

level_generator = LabyrinthLevelGenerator(config)
env.reset(level_string=level_generator.generate())
env.reset(options={"level_string": level_generator.generate()})

obs = env.render(mode="rgb_array")
plt.figure()
plt.imshow(obs)
plt.savefig(f"example_maze_{size}_{i+1}.png")
obs = global_obs_render_wrapper.render()
image_renderer.render(obs, f"example_maze_{size}_{i+1}.png")
5 changes: 3 additions & 2 deletions python/examples/Projectiles/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"projectiles.yaml",
player_observer_type=gd.ObserverType.ISOMETRIC,
global_observer_type=gd.ObserverType.ISOMETRIC,
Expand Down
5 changes: 3 additions & 2 deletions python/examples/Proximity/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"proximity.yaml",
player_observer_type=gd.ObserverType.ISOMETRIC,
global_observer_type=gd.ObserverType.ISOMETRIC,
Expand Down
5 changes: 3 additions & 2 deletions python/examples/Stochasticity/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from griddly import gd, gym
from griddly import gd
from griddly.gym import GymWrapper
from griddly.util.render_tools import RenderToVideo
from griddly.wrappers.render_wrapper import RenderWrapper

if __name__ == "__main__":
env = gym(
env = GymWrapper(
"stochasticity.yaml",
player_observer_type=gd.ObserverType.SPRITE_2D,
global_observer_type=gd.ObserverType.SPRITE_2D,
Expand Down
Loading

0 comments on commit 83d0b69

Please sign in to comment.