Repository navigation
Expand file tree
/
Copy pathcontroller.py
More file actions
67 lines (48 loc) · 1.95 KB
/
Copy pathcontroller.py
File metadata and controls
67 lines (48 loc) · 1.95 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
import socket
import json
from game_state import GameState
#from bot import fight
import sys
from bot import Bot
import tensorflow.keras.models as models
def connect(port):
#For making a connection with the game
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("127.0.0.1", port))
server_socket.listen(5)
(client_socket, _) = server_socket.accept()
print ("Connected to game!")
return client_socket
def send(client_socket, command):
#This function will send your updated command to Bizhawk so that game reacts according to your command.
command_dict = command.object_to_dict()
pay_load = json.dumps(command_dict).encode()
client_socket.sendall(pay_load)
def receive(client_socket):
#receive the game state and return game state
pay_load = client_socket.recv(4096)
input_dict = json.loads(pay_load.decode())
game_state = GameState(input_dict)
return game_state
def main():
if (sys.argv[1] == '1'):
client_socket = connect(9999)
elif (sys.argv[1] == '2'):
client_socket = connect(10000)
current_game_state = None
#print( current_game_state.is_round_over )
bot=Bot()
# Load the model
loaded_model = models.load_model('model.h5')
while True:
while (current_game_state is None) or (not current_game_state.has_round_started) or current_game_state.is_round_over or current_game_state.player1.health == 0:
current_game_state = receive(client_socket)
bot_command = bot.fight(current_game_state, 3, loaded_model)
send(client_socket, bot_command)
print("waiting for round to start")
while (current_game_state is None) or (not current_game_state.is_round_over):
current_game_state = receive(client_socket)
bot_command = bot.fight(current_game_state,sys.argv[1],loaded_model)
send(client_socket, bot_command)
if __name__ == '__main__':
main()