Repository navigation
Expand file tree
/
Copy pathp3.py
More file actions
82 lines (69 loc) · 2.46 KB
/
Copy pathp3.py
File metadata and controls
82 lines (69 loc) · 2.46 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
72
73
74
75
76
77
78
79
80
81
82
import os.path
import time
import p3.fox
import p3.memory_watcher
import p3.menu_manager
import p3.pad
import p3.state
import p3.state_manager
import p3.stats
def find_dolphin_dir():
"""Attempts to find the dolphin user directory. None on failure."""
candidates = ['~/.dolphin-emu', '~/.local/share/.dolphin-emu']
for candidate in candidates:
path = os.path.expanduser(candidate)
if os.path.isdir(path):
return path
return None
def write_locations(dolphin_dir, locations):
"""Writes out the locations list to the appropriate place under dolphin_dir."""
path = dolphin_dir + '/MemoryWatcher/Locations.txt'
with open(path, 'w') as f:
f.write('\n'.join(locations))
dolphin_dir = find_dolphin_dir()
if dolphin_dir is None:
print('Could not detect dolphin directory.')
return
def run(fox, state, sm, mw, pad, stats):
mm = p3.menu_manager.MenuManager()
while True:
last_frame = state.frame
res = next(mw)
if res is not None:
sm.handle(*res)
if state.frame > last_frame:
stats.add_frames(state.frame - last_frame)
start = time.time()
make_action(state, pad, mm, fox)
stats.add_thinking_time(time.time() - start)
def make_action(state, pad, mm, fox):
if state.menu == p3.state.Menu.Game:
fox.advance(state, pad)
elif state.menu == p3.state.Menu.Characters:
mm.pick_fox(state, pad)
elif state.menu == p3.state.Menu.Stages:
# Handle this once we know where the cursor position is in memory.
pad.tilt_stick(p3.pad.Stick.C, 0.5, 0.5)
elif state.menu == p3.state.Menu.PostGame:
mm.press_start_lots(state, pad)
def main():
dolphin_dir = find_dolphin_dir()
if dolphin_dir is None:
print('Could not find dolphin config dir.')
return
state = p3.state.State()
sm = p3.state_manager.StateManager(state)
write_locations(dolphin_dir, sm.locations())
stats = p3.stats.Stats()
fox = p3.fox.Fox()
try:
print('Start dolphin now. Press ^C to stop p3.')
pad_path = dolphin_dir + '/Pipes/p3'
mw_path = dolphin_dir + '/MemoryWatcher/MemoryWatcher'
with p3.pad.Pad(pad_path) as pad, p3.memory_watcher.MemoryWatcher(mw_path) as mw:
run(fox, state, sm, mw, pad, stats)
except KeyboardInterrupt:
print('Stopped')
print(stats)
if __name__ == '__main__':
main()