-
Notifications
You must be signed in to change notification settings - Fork 0
/
smashbot.py
executable file
·147 lines (127 loc) · 5.4 KB
/
smashbot.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
147
#!/usr/bin/python3
import argparse
import os
import signal
import sys
import melee
from esagent import ESAgent
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
def is_dir(dirname):
"""Checks if a path is an actual directory"""
if not os.path.isdir(dirname):
msg = "{0} is not a directory".format(dirname)
raise argparse.ArgumentTypeError(msg)
else:
return dirname
parser = argparse.ArgumentParser(description='Example of libmelee in action')
parser.add_argument('--port', '-p', type=check_port,
help='The controller port your AI will play on',
default=2)
parser.add_argument('--opponent', '-o', type=check_port,
help='The controller port the opponent will play on',
default=1)
parser.add_argument('--bot', '-b',
help='Opponent is a second instance of SmashBot',
default=False)
parser.add_argument('--debug', '-d', action='store_true',
help='Debug mode. Creates a CSV of all game state')
parser.add_argument('--difficulty', '-i', type=int, default=-1,
help='Manually specify difficulty level of SmashBot')
parser.add_argument('--dolphinexecutable', '-e', type=is_dir,
help='Manually specify Dolphin executable')
parser.add_argument('--stage', '-s', default="FD",
help='Specify which stage to select')
stagedict = {
"FD": melee.enums.Stage.FINAL_DESTINATION,
"BF": melee.enums.Stage.BATTLEFIELD,
"YS": melee.enums.Stage.YOSHIS_STORY,
"FOD": melee.enums.Stage.FOUNTAIN_OF_DREAMS,
"DL": melee.enums.Stage.DREAMLAND,
"PS": melee.enums.Stage.POKEMON_STADIUM
}
args = parser.parse_args()
log = None
if args.debug:
log = melee.logger.Logger()
# Options here are:
# GCN_ADAPTER will use your WiiU adapter for live human-controlled play
# UNPLUGGED is pretty obvious what it means
# STANDARD is a named pipe input (bot)
opponent_type = melee.enums.ControllerType.STANDARD
if not args.bot:
opponent_type = melee.enums.ControllerType.GCN_ADAPTER
# Create our console object. This will be the primary object that we will interface with
console = melee.console.Console(path=args.dolphinexecutable,
logger=log)
controller_one = melee.controller.Controller(console=console, port=args.port)
controller_two = melee.controller.Controller(console=console,
port=args.opponent,
type=opponent_type)
# initialize our agent
agent1 = ESAgent(console, args.port, args.opponent, controller_one, args.difficulty)
agent2 = None
if args.bot:
controller_two = melee.controller.Controller(console=console, port=args.opponent)
agent2 = ESAgent(console, args.opponent, args.port, controller_two, args.difficulty)
def signal_handler(signal, 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 dolphin
console.run()
# Connect to the console
print("Connecting to console...")
if not console.connect():
print("ERROR: Failed to connect to the console.")
print("\tIf you're trying to autodiscover, local firewall settings can " +
"get in the way. Try specifying the address manually.")
sys.exit(-1)
print("Connected")
# Plug our controller in
controller_one.connect()
controller_two.connect()
supportedcharacters = [melee.enums.Character.PEACH, melee.enums.Character.CPTFALCON, melee.enums.Character.FALCO, \
melee.enums.Character.FOX, melee.enums.Character.SAMUS, melee.enums.Character.ZELDA, melee.enums.Character.SHEIK, \
melee.enums.Character.PIKACHU, melee.enums.Character.JIGGLYPUFF, melee.enums.Character.MARTH, melee.enums.Character.GANONDORF]
# Main loop
while True:
# "step" to the next frame
gamestate = console.step()
# What menu are we in?
if gamestate.menu_state == melee.enums.Menu.IN_GAME:
#try:
agent1.act(gamestate)
if agent2:
agent2.act(gamestate)
# except Exception as error:
# # Do nothing in case of error thrown!
# agent1.controller.empty_input()
# if agent2:
# agent2.controller.empty_input()
# if log:
# log.log("Notes", "Exception thrown: " + repr(error) + " ", concat=True)
# else:
# print("WARNING: Exception thrown: ", error)
if log:
log.log("Notes", "Goals: " + str(agent1.strategy), concat=True)
log.logframe(gamestate)
log.writeframe()
else:
melee.menuhelper.MenuHelper.menu_helper_simple(gamestate,
controller_one,
melee.enums.Character.CPTFALCON,
stagedict.get(args.stage, melee.enums.Stage.FINAL_DESTINATION),
autostart=False,
swag=True)
if log:
log.skipframe()