This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from SerpentAI/environments
added base environment class; added concept of environments to game c…
- Loading branch information
Showing
9 changed files
with
346 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
.vscode | ||
|
||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Stub to make unit testing possible... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Stub to make unit testing possible... | ||
|
||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
db: 0 | ||
|
||
analytics: | ||
host: 127.0.0.1 | ||
port: 9999 | ||
realm: serpent | ||
auth: | ||
username: serpent | ||
password: serpent | ||
topic: ANALYTICS_TOPIC | ||
|
||
frame_handlers: | ||
COLLECT_FRAMES_FOR_CONTEXT: | ||
context: game | ||
interval: 1 | ||
|
||
frame_grabber: | ||
redis_key: SERPENT:FRAMES | ||
|
||
input_recorder: | ||
redis_key: SERPENT:INPUTS | ||
|
||
visual_debugger: | ||
redis_key_prefix: SERPENT:VISUAL_DEBUGGER | ||
available_buckets: | ||
- "0" | ||
- "1" | ||
- "2" | ||
- "3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import time | ||
import collections | ||
|
||
|
||
class Environment: | ||
|
||
def __init__(self, name, game_api=None, input_controller=None): | ||
self.name = name | ||
|
||
self.game_api = game_api | ||
self.input_controller = input_controller | ||
|
||
self.game_state = dict() | ||
|
||
self.reset() | ||
|
||
@property | ||
def episode_duration(self): | ||
return time.time() - self.episode_started_at | ||
|
||
@property | ||
def episode_over(self): | ||
if self.episode_maximum_steps is not None: | ||
return self.episode_steps >= self.episode_maximum_steps | ||
else: | ||
return False | ||
|
||
def new_episode(self, maximum_steps=None, reset=False): | ||
self.recent_game_inputs = collections.deque(list(), maxlen=10) | ||
|
||
self.episode_steps = 0 | ||
self.episode_maximum_steps = maximum_steps | ||
|
||
self.episode_started_at = time.time() | ||
|
||
if not reset: | ||
self.episode += 1 | ||
|
||
def episode_step(self): | ||
self.episode_steps += 1 | ||
self.total_steps += 1 | ||
|
||
def reset(self): | ||
self.recent_game_inputs = None | ||
|
||
self.total_steps = 0 | ||
|
||
self.episode = 0 | ||
self.episode_steps = 0 | ||
|
||
self.episode_maximum_steps = None | ||
|
||
self.episode_started_at = None | ||
|
||
def update_game_state(self): | ||
raise NotImplementedError() | ||
|
||
def perform_input(self, label, game_input): | ||
# TODO: Mouse support | ||
# TODO: Consider using sneakysnek events? | ||
|
||
self.input_controller.handle_keys(game_input) | ||
self.recent_game_inputs.appendleft(label) | ||
|
||
def clear_input(self): | ||
# TODO: Mouse support | ||
self.input_controller.handle_keys([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
from serpent.game_api import GameAPI | ||
|
||
from serpent.input_controller import KeyboardKey | ||
|
||
|
||
class MyGameAPI(GameAPI): | ||
# A GameAPI is intended to contain functions and pieces of data that are applicable to the | ||
# game and not agent or environment specific (e.g. Game inputs, Frame processing) | ||
|
||
def __init__(self, game=None): | ||
super().__init__(game=game) | ||
|
||
# SAMPLE - Replace with your own game inputs! | ||
self.game_inputs = { | ||
"MOVEMENT": { | ||
"MOVE UP": [KeyboardKey.KEY_W], | ||
"MOVE LEFT": [KeyboardKey.KEY_A], | ||
"MOVE DOWN": [KeyboardKey.KEY_S], | ||
"MOVE RIGHT": [KeyboardKey.KEY_D], | ||
"MOVE TOP-LEFT": [KeyboardKey.KEY_W, KeyboardKey.KEY_A], | ||
"MOVE TOP-RIGHT": [KeyboardKey.KEY_W, KeyboardKey.KEY_D], | ||
"MOVE DOWN-LEFT": [KeyboardKey.KEY_S, KeyboardKey.KEY_A], | ||
"MOVE DOWN-RIGHT": [KeyboardKey.KEY_S, KeyboardKey.KEY_D], | ||
"DON'T MOVE": [] | ||
}, | ||
"SHOOTING": { | ||
"SHOOT UP": [KeyboardKey.KEY_UP], | ||
"SHOOT LEFT": [KeyboardKey.KEY_LEFT], | ||
"SHOOT DOWN": [KeyboardKey.KEY_DOWN], | ||
"SHOOT RIGHT": [KeyboardKey.KEY_RIGHT], | ||
"DON'T SHOOT": [] | ||
} | ||
} | ||
|
||
def my_api_function(self): | ||
pass | ||
|
||
class MyAPINamespace: | ||
|
||
@classmethod | ||
def my_namespaced_api_function(cls): | ||
api = MyGameAPI.instance | ||
api = MyGameAPI.instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.