def reset(self) -> GlulxGameState: self.message_broker.reset() if self.game_running: self.close() self._names_struct = ffi.new('struct sock_names*') lib.init_glulx(self._names_struct) sock_name = ffi.string(self._names_struct.sock_name).decode('utf-8') self._process = subprocess.Popen([ "%s/git-glulx-ml" % (GLULX_PATH, ), self._gamefile, '-g', sock_name, '-q' ]) c_feedback = lib.get_output_nosend(self._names_struct) if c_feedback == ffi.NULL: self.close() raise ValueError("Game failed to start properly: {}.".format( self._gamefile)) c_feedback = ffi.gc(c_feedback, lib.free) start_output = ffi.string(c_feedback).decode('utf-8') self.game_state = GlulxGameState(self) self.game_state.init(start_output, self.game, self._state_tracking, self._compute_intermediate_reward) # TODO: check if the game was compiled in debug mode. You could parse # the output of the following command to check whether debug mode # was used or not (i.e. invalid action not found). self._send('actions') # Turn on debug print for Inform7 action events. self._send('restrict commands') # Restrict Inform7 commands. return self.game_state
def _send(self, command: str) -> Union[str, None]: if not self.game_running: return None if len(command) == 0: command = " " c_command = ffi.new('char[]', command.encode('utf-8')) result = lib.communicate(self._names_struct, c_command) if result == ffi.NULL: self.close() return None result = ffi.gc(result, lib.free) return ffi.string(result).decode('utf-8')
def reset(self) -> GlulxGameState: if self.game_running: self.close() self._names_struct = ffi.new('struct sock_names*') lib.init_glulx(self._names_struct) sock_name = ffi.string(self._names_struct.sock_name).decode('utf-8') self._process = subprocess.Popen([ "%s/git-glulx-ml" % (GLULX_PATH, ), self._gamefile, '-g', sock_name, '-q' ]) c_feedback = lib.get_output_nosend(self._names_struct) if c_feedback == ffi.NULL: self.close() raise ValueError("Game failed to start properly: {}.".format( self._gamefile)) c_feedback = ffi.gc(c_feedback, lib.free) start_output = ffi.string(c_feedback).decode('utf-8') if not self._state_tracking: self.enable_extra_info("score") version = self._send('tw-print version').splitlines()[0] if version != "I didn't understand that sentence." and version != str( Inform7Game.VERSION): raise ValueError( "Cannot play a TextWorld version {} game, expected version {}". format(version, Inform7Game.VERSION)) # TODO: check if the game was compiled in debug mode. You could parse # the output of the following command to check whether debug mode # was used or not (i.e. invalid action not found). self._send('tw-trace-actions' ) # Turn on debug print for Inform7 action events. _extra_output = "" for info in self.extra_info: _extra_output = self._send('tw-extra-infos {}'.format(info)) start_output = start_output[:-1] + _extra_output[: -1] # Add extra infos minus the prompts '>'. self.game_state = GlulxGameState(self) self.game_state.init(start_output, self.game, self._state_tracking, self._compute_intermediate_reward) return self.game_state
def reset(self) -> str: self.close() # Terminate existing process if needed. self._names_struct = ffi.new('struct sock_names*') lib.init_glulx(self._names_struct) sock_name = ffi.string(self._names_struct.sock_name).decode('utf-8') self._process = subprocess.Popen(["%s/git-glulx-ml" % (GLULX_PATH,), self._gamefile, '-g', sock_name, '-q']) c_feedback = lib.get_output_nosend(self._names_struct) if c_feedback == ffi.NULL: self.close() raise ValueError("Game failed to start properly: {}.".format(self._gamefile)) c_feedback = ffi.gc(c_feedback, lib.free) feedback = ffi.string(c_feedback).decode('utf-8') feedback = _strip_input_prompt_symbol(feedback) self.state = GameState(feedback=feedback, raw=feedback) return self.state
def _send(self, command: str) -> Union[str, None]: """ Send a command directly to the interpreter. This method will not affect the internal state variable. """ if not self.game_running: return None if len(command) == 0: command = " " c_command = ffi.new('char[]', command.encode('utf-8')) result = lib.communicate(self._names_struct, c_command) if result == ffi.NULL: self.close() return None result = ffi.gc(result, lib.free) return ffi.string(result).decode('utf-8')