-
Notifications
You must be signed in to change notification settings - Fork 47
/
example.py
executable file
·146 lines (126 loc) · 5.86 KB
/
example.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3
import argparse
import signal
import sys
import melee
import random
# This example program demonstrates how to use the Melee API to run a console,
# setup controllers, and send button presses over to a console
def check_port(value):
ivalue = int(value)
if ivalue < 1 or ivalue > 4:
raise argparse.ArgumentTypeError("%s is an invalid controller port. \
Must be 1, 2, 3, or 4." % value)
return ivalue
parser = argparse.ArgumentParser(description='Example of libmelee in action')
parser.add_argument('--port', '-p', type=check_port,
help='The controller port (1-4) your AI will play on',
default=2)
parser.add_argument('--opponent', '-o', type=check_port,
help='The controller port (1-4) the opponent will play on',
default=1)
parser.add_argument('--debug', '-d', action='store_true',
help='Debug mode. Creates a CSV of all game states')
parser.add_argument('--address', '-a', default="127.0.0.1",
help='IP address of Slippi/Wii')
parser.add_argument('--dolphin_executable_path', '-e', default=None,
help='The directory where dolphin is')
parser.add_argument('--connect_code', '-t', default="",
help='Direct connect code to connect to in Slippi Online')
parser.add_argument('--iso', default=None, type=str,
help='Path to melee iso.')
args = parser.parse_args()
# This logger object is useful for retroactively debugging issues in your bot
# You can write things to it each frame, and it will create a CSV file describing the match
log = None
if args.debug:
log = melee.Logger()
# Create our Console object.
# This will be one of the primary objects that we will interface with.
# The Console represents the virtual or hardware system Melee is playing on.
# Through this object, we can get "GameState" objects per-frame so that your
# bot can actually "see" what's happening in the game
console = melee.Console(path=args.dolphin_executable_path,
slippi_address=args.address,
logger=log)
# Create our Controller object
# The controller is the second primary object your bot will interact with
# Your controller is your way of sending button presses to the game, whether
# virtual or physical.
controller = melee.Controller(console=console,
port=args.port,
type=melee.ControllerType.STANDARD)
controller_opponent = melee.Controller(console=console,
port=args.opponent,
type=melee.ControllerType.GCN_ADAPTER)
# This isn't necessary, but makes it so that Dolphin will get killed when you ^C
def signal_handler(sig, frame):
console.stop()
if args.debug:
log.writelog()
print("") #because the ^C will be on the terminal
print("Log file created: " + log.filename)
print("Shutting down cleanly...")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Run the console
console.run(iso_path=args.iso)
# Connect to the console
print("Connecting to console...")
if not console.connect():
print("ERROR: Failed to connect to the console.")
sys.exit(-1)
print("Console connected")
# Plug our controller in
# Due to how named pipes work, this has to come AFTER running dolphin
# NOTE: If you're loading a movie file, don't connect the controller,
# dolphin will hang waiting for input and never receive it
print("Connecting controller to console...")
if not controller.connect():
print("ERROR: Failed to connect the controller.")
sys.exit(-1)
print("Controller connected")
costume = 0
framedata = melee.framedata.FrameData()
# Main loop
while True:
# "step" to the next frame
gamestate = console.step()
if gamestate is None:
continue
# The console object keeps track of how long your bot is taking to process frames
# And can warn you if it's taking too long
if console.processingtime * 1000 > 12:
print("WARNING: Last frame took " + str(console.processingtime*1000) + "ms to process.")
# What menu are we in?
if gamestate.menu_state in [melee.Menu.IN_GAME, melee.Menu.SUDDEN_DEATH]:
# Slippi Online matches assign you a random port once you're in game that's different
# than the one you're physically plugged into. This helper will autodiscover what
# port we actually are.
discovered_port = args.port
if args.connect_code != "":
discovered_port = melee.gamestate.port_detector(gamestate, melee.Character.FOX, costume)
if discovered_port > 0:
# NOTE: This is where your AI does all of its stuff!
# This line will get hit once per frame, so here is where you read
# in the gamestate and decide what buttons to push on the controller
melee.techskill.multishine(ai_state=gamestate.players[discovered_port], controller=controller)
else:
# If the discovered port was unsure, reroll our costume for next time
costume = random.randint(0, 4)
# Log this frame's detailed info if we're in game
if log:
log.logframe(gamestate)
log.writeframe()
else:
melee.MenuHelper.menu_helper_simple(gamestate,
controller,
melee.Character.FOX,
melee.Stage.YOSHIS_STORY,
args.connect_code,
costume=costume,
autostart=True,
swag=False)
# If we're not in game, don't log the frame
if log:
log.skipframe()