-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smooth Moves Murphy.py
125 lines (99 loc) · 4.35 KB
/
Smooth Moves Murphy.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import retro
from datetime import datetime
import time
import random
import itertools
import time
import gzip
def get_all_pairwise_actions(possible_actions):
groupings = []
for each in itertools.permutations(possible_actions.keys(),2):
groupings.append(possible_actions[each[0]] + possible_actions[each[1]])
return groupings
def dash(possible_actions, direction, taps):
set_of_actions = []
for i in range(taps):
set_of_actions.append(possible_actions[direction] + possible_actions["B"])
return set_of_actions
def run(possible_actions, direction, run_distance):
set_of_actions = []
for i in range(run_distance):
set_of_actions.append(possible_actions[direction])
return set_of_actions
def jump(possible_actions, direction, jump_duration):
set_of_actions = []
set_of_actions.append(possible_actions["A"])
set_of_actions.append(possible_actions["A"])
set_of_actions.append(possible_actions["A"])
for i in range(jump_duration):
set_of_actions.append(possible_actions["A"] + possible_actions[direction])
# set_of_actions.append(possible_actions[direction])
return set_of_actions
def shoot(possible_actions, direction, jump_duration):
set_of_actions = []
set_of_actions.append(possible_actions[direction])
for i in range(jump_duration):
set_of_actions.append(possible_actions["X"])
# set_of_actions.append(possible_actions[direction])
return set_of_actions
def actions_available(possible_actions):
#https://info.sonicretro.org/index.php?title=File:Sonic2_MD_US_manual.pdf&page=7
x = {"run_left_X_frames" : run(possible_actions, "LEFT", 100), # This could be a variable amount of running
"run_left_Y_frames" : run(possible_actions, "LEFT", 200), # This could be a variable amount of running
"run_right_X_frames" : run(possible_actions, "RIGHT", 100), # This could be a variable amount of running
"run_right_Y_frames" : run(possible_actions, "RIGHT", 200), # This could be a variable amount of running
"dash_right_X_times" : dash(possible_actions, "RIGHT", 100),
"dash_left_X_times" : dash(possible_actions, "LEFT", 100),
"jump_right_X_frames" : jump(possible_actions, "RIGHT", 50),
"jump_left_X_frames" : jump(possible_actions, "LEFT", 50),
"shoot_left_frames" : shoot(possible_actions, "LEFT", 10),
"shoot_left_frames" : shoot(possible_actions, "RIGHT", 10),
#"jump_up_X_frames" : jump(possible_actions, "RIGHT", 5),
}
return x[list(x.keys())[random.randint(0,len(x.keys())-1)]]
def save_state_to_file(env, name="test.state"):
content = env.em.get_state()
with gzip.open(name, 'wb') as f:
f.write(content)
def main():
actions_by_value = {"B" : 2**12, "Y" : 2**11, "SELECT" : 2**9, "START" : 2**8, "UP" : 2**7, "DOWN" : 2**6, "LEFT" : 2**5, "RIGHT" : 2**4, "A" : 2**3, "X" : 2**2, "L" : 2**1, "R" : 2**0} # "MODE" : 2**9
# Experiment with making an example like actions_by_value with snes
env = retro.make(game='SuperMetroid-Snes')
obs = env.reset()
# ["B", "Y", "SELECT", "START", "UP", "DOWN", "LEFT", "RIGHT", "A", "X", "L", "R"],
# START: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
# Do Nothing [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# "A" [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
while True:
action_sequence = actions_available(actions_by_value)
for i in action_sequence:
binary_value = str(bin(i))
binary_value = ''.join(list(binary_value)[2:])
action = list(map(int,list(str(binary_value).zfill(12))))
obs, rew, done, info = env.step(action)
env.render()
time.sleep(1.0/60.0)
if done:
print ("DONE?")
if info["lives"] != 0:
print ("bdone", done)
print (info)
print (rew)
#print ("WHAT???")
#input()
level_index += 1
env = retro.make(game='SuperMetroid-Snes')
obs = env.reset()
continue
env.close()
if __name__ == "__main__":
main()
'''
/home/hotdog/.local/lib/python3.6/site-packages/retro/data/stable/SonicTheHedgehog2-Genesis/
contest.json
data.json
metadata.json
scenario.json
script.lua
xpos.json
'''