forked from Impossibum/kaiyo-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathN_Parser.py
More file actions
71 lines (60 loc) · 2.9 KB
/
N_Parser.py
File metadata and controls
71 lines (60 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import Any
import gym.spaces
import numpy as np
from gym.spaces import Discrete
from rlgym.utils.action_parsers import ActionParser
from rlgym.utils.gamestates import GameState
from rlgym_tools.extra_action_parsers.kbm_act import KBMAction
class NectoAction(ActionParser):
# necto parser courtesy of Rolv/Soren (https://github.com/Rolv-Arild/Necto/blob/master/training/parser.py)
def __init__(self):
super().__init__()
self._lookup_table = self.make_lookup_table()
@staticmethod
def make_lookup_table():
actions = []
# Ground
for throttle in (-1, 0, 1):
for steer in (-1, 0, 1):
for boost in (0, 1):
for handbrake in (0, 1):
if boost == 1 and throttle != 1:
continue
actions.append([throttle or boost, steer, 0, steer, 0, 0, boost, handbrake])
# Aerial
for pitch in (-1, 0, 1):
for yaw in (-1, 0, 1):
for roll in (-1, 0, 1):
for jump in (0, 1):
for boost in (0, 1):
if jump == 1 and yaw != 0: # Only need roll for sideflip
continue
if pitch == roll == jump == 0: # Duplicate with ground
continue
# Enable handbrake for potential wavedashes
handbrake = jump == 1 and (pitch != 0 or yaw != 0 or roll != 0)
actions.append([boost, yaw, pitch, yaw, roll, jump, boost, handbrake])
actions = np.array(actions)
return actions
def get_action_space(self) -> gym.spaces.Space:
return Discrete(len(self._lookup_table))
def parse_actions(self, actions: Any, state: GameState) -> np.ndarray:
#hacky pass through to allow multiple types of agent actions while still parsing nectos
#strip out fillers, pass through 8sets, get look up table values, recombine
parsed_actions = []
for action in actions:
#support reconstruction
if action.size != 8:
if action.shape == 0:
action = np.expand_dims(action, axis=0)
# to allow different action spaces, pad out short ones (assume later unpadding in parser)
action = np.pad(action.astype('float64'), (0, 8 - action.size), 'constant', constant_values=np.NAN)
if np.isnan(action).any(): #its been padded, delete to go back to original
stripped_action = (action[~np.isnan(action)]).squeeze().astype('int')
parsed_actions.append(self._lookup_table[stripped_action])
else:
parsed_actions.append(action)
return np.asarray(parsed_actions)
if __name__ == '__main__':
ap = NectoAction()
print(ap.get_action_space())