Mark
/
celeste-ai
Archived
1
0
Fork 0

Fixed frame timings

master
Mark 2023-02-16 12:11:04 -08:00
parent c1379a0116
commit ce02009d64
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 30 additions and 13 deletions

View File

@ -20,10 +20,7 @@ class Celeste:
"dash-lu" # dash left-up "dash-lu" # dash left-up
] ]
def __init__(self, on_get_state): def __init__(self):
self.on_get_state = on_get_state
# Start pico-8 # Start pico-8
self.process = subprocess.Popen( self.process = subprocess.Popen(
"bin/pico-8/linux/pico8", "bin/pico-8/linux/pico8",
@ -182,11 +179,14 @@ class Celeste:
if k == "!RESTART": if k == "!RESTART":
break break
def update_loop(self):
def update_loop(self, before, after):
# Get state, call callback, wait for state # Get state, call callback, wait for state
# One line => one frame. # One line => one frame.
before_out = None
it = iter(self.process.stdout.readline, "") it = iter(self.process.stdout.readline, "")
@ -232,4 +232,6 @@ class Celeste:
self.dist = dist self.dist = dist
# Call step callback # Call step callback
self.on_get_state(self) if before_out is not None:
after(self, before_out)
before_out = before(self)

View File

@ -303,7 +303,7 @@ def optimize_model():
optimizer.step() optimizer.step()
def on_state(celeste): def on_state_before(celeste):
global steps_done global steps_done
# Conversion to pytorch # Conversion to pytorch
@ -324,14 +324,28 @@ def on_state(celeste):
# Turn number into action string # Turn number into action string
str_action = Celeste.action_space[action] str_action = Celeste.action_space[action]
celeste.act(str_action)
return state, action
def on_state_after(celeste, before_out):
state, action = before_out
pt_state = torch.tensor(
[state[x] for x in state_number_map],
dtype = torch.float32,
device = compute_device
).unsqueeze(0)
pt_action = torch.tensor( pt_action = torch.tensor(
[[ action ]], [[ action ]],
device = compute_device, device = compute_device,
dtype = torch.long dtype = torch.long
) )
celeste.act(str_action)
next_state = celeste.status next_state = celeste.status
if next_state["deaths"] != 0: if next_state["deaths"] != 0:
@ -390,8 +404,9 @@ def on_state(celeste):
c = Celeste( c = Celeste()
on_state
)
c.update_loop() c.update_loop(
on_state_before,
on_state_after
)